5
5
#include <stdio.h>
6
6
#include <time.h>
7
7
8
- #include "buffer.h"
9
8
#include "clipboard.h"
10
9
#include "config.h"
11
10
#include "file.h"
@@ -231,8 +230,6 @@ void blur_clicked_handler(GtkWidget *widget, struct swappy_state *state) {
231
230
232
231
void application_finish (struct swappy_state * state ) {
233
232
paint_free_all (state );
234
- buffer_free_all (state );
235
- g_free (state -> drawing_area_rect );
236
233
cairo_surface_destroy (state -> rendered_surface );
237
234
cairo_surface_destroy (state -> original_image_surface );
238
235
cairo_surface_destroy (state -> scaled_image_surface );
@@ -247,6 +244,7 @@ void application_finish(struct swappy_state *state) {
247
244
g_free (state -> geometry );
248
245
g_free (state -> window );
249
246
g_free (state -> ui );
247
+ g_object_unref (state -> original_image );
250
248
g_object_unref (state -> app );
251
249
252
250
config_free (state );
@@ -394,27 +392,21 @@ gboolean draw_area_handler(GtkWidget *widget, cairo_t *cr,
394
392
gboolean draw_area_configure_handler (GtkWidget * widget ,
395
393
GdkEventConfigure * event ,
396
394
struct swappy_state * state ) {
397
- g_debug ("received configure_event handler " );
395
+ g_debug ("received configure_event callback " );
398
396
cairo_surface_destroy (state -> rendered_surface );
399
- g_free (state -> drawing_area_rect );
400
397
401
398
cairo_surface_t * surface = gdk_window_create_similar_surface (
402
399
gtk_widget_get_window (widget ), CAIRO_CONTENT_COLOR_ALPHA ,
403
400
gtk_widget_get_allocated_width (widget ),
404
401
gtk_widget_get_allocated_height (widget ));
405
402
406
- state -> rendered_surface = surface ;
403
+ g_info ("size of drawing area surface: %ux%u" ,
404
+ cairo_image_surface_get_width (surface ),
405
+ cairo_image_surface_get_height (surface ));
407
406
408
- GtkAllocation * alloc = g_new (GtkAllocation , 1 );
409
- gtk_widget_get_allocation (widget , alloc );
410
- state -> drawing_area_rect = alloc ;
411
- buffer_resize_patterns (state );
407
+ state -> rendered_surface = surface ;
412
408
413
- g_info ("size of cairo_surface: %ux%u with type: %d" ,
414
- cairo_image_surface_get_width (surface ),
415
- cairo_image_surface_get_height (surface ),
416
- cairo_image_surface_get_format (surface ));
417
- g_info ("size of area to render: %ux%u" , alloc -> width , alloc -> height );
409
+ pixbuf_scale_surface_from_widget (state , widget );
418
410
419
411
render_state (state );
420
412
@@ -562,18 +554,34 @@ static void compute_window_size(struct swappy_state *state) {
562
554
state -> window -> y = workarea .y ;
563
555
564
556
double threshold = 0.75 ;
565
- double scaling = 1.0 ;
566
557
567
- if (state -> geometry -> width > workarea .width * threshold ) {
568
- scaling = workarea .width * threshold / state -> geometry -> width ;
569
- } else if (state -> geometry -> height > workarea .height * threshold ) {
570
- scaling = workarea .height * threshold / state -> geometry -> height ;
571
- }
558
+ int image_width = gdk_pixbuf_get_width (state -> original_image );
559
+ int image_height = gdk_pixbuf_get_height (state -> original_image );
572
560
573
- state -> window -> width = state -> geometry -> width * scaling ;
574
- state -> window -> height = state -> geometry -> height * scaling ;
561
+ int max_width = workarea . width * threshold ;
562
+ int max_height = workarea . height * threshold ;
575
563
564
+ g_info ("size of image: %ux%u" , image_width , image_height );
576
565
g_info ("size of monitor at window: %ux%u" , workarea .width , workarea .height );
566
+ g_info ("maxium size allowed for window: %ux%u" , max_width , max_height );
567
+
568
+ int scaled_width = image_width ;
569
+ int scaled_height = image_height ;
570
+
571
+ double scaling_factor_width = (double )max_width / image_width ;
572
+ double scaling_factor_height = (double )max_height / image_height ;
573
+
574
+ if (scaling_factor_height < 1.0 || scaling_factor_width < 1.0 ) {
575
+ double scaling_factor = MIN (scaling_factor_width , scaling_factor_height );
576
+ scaled_width = image_width * scaling_factor ;
577
+ scaled_height = image_height * scaling_factor ;
578
+ g_info ("rendering area will be scaled by a factor of: %.2lf" ,
579
+ scaling_factor );
580
+ }
581
+
582
+ state -> window -> width = scaled_width ;
583
+ state -> window -> height = scaled_height ;
584
+
577
585
g_info ("size of window to render: %ux%u" , state -> window -> width ,
578
586
state -> window -> height );
579
587
}
@@ -675,8 +683,8 @@ static bool load_layout(struct swappy_state *state) {
675
683
}
676
684
677
685
static bool init_gtk_window (struct swappy_state * state ) {
678
- if (!state -> geometry ) {
679
- g_critical ("no geometry found, did you use -f option? " );
686
+ if (!state -> original_image ) {
687
+ g_critical ("original image not loaded " );
680
688
return false;
681
689
}
682
690
@@ -725,7 +733,7 @@ static gint command_line_handler(GtkApplication *app,
725
733
state -> temp_file_str = temp_file_str ;
726
734
}
727
735
728
- if (!buffer_init_from_file (state )) {
736
+ if (!pixbuf_init_from_file (state )) {
729
737
return EXIT_FAILURE ;
730
738
}
731
739
}
0 commit comments