From 805a3f5f8be69a4e02670114615224a888fb1168 Mon Sep 17 00:00:00 2001 From: Martin Nordholts Date: Sun, 13 Jul 2008 20:23:15 +0000 Subject: [PATCH] Allow to choose what axes to center on. 2008-07-13 Martin Nordholts * app/display/gimpdisplayshell-scale.[ch] (gimp_display_shell_center_image): Allow to choose what axes to center on. (gimp_display_shell_scale_fill) (gimp_display_shell_scale_fit_in): Explicitly center on both axes. * app/display/gimpdisplayshell.c (gimp_display_shell_fill): Center the image in the filled display shell. Rather hackish, but seems to work fine. svn path=/trunk/; revision=26185 --- ChangeLog | 13 ++++++++ app/display/gimpdisplayshell-scale.c | 45 ++++++++++++++++++---------- app/display/gimpdisplayshell-scale.h | 4 ++- app/display/gimpdisplayshell.c | 38 +++++++++++++++++++++++ 4 files changed, 83 insertions(+), 17 deletions(-) diff --git a/ChangeLog b/ChangeLog index 11bd8dc203..ad14ac91f4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2008-07-13 Martin Nordholts + + * app/display/gimpdisplayshell-scale.[ch] + (gimp_display_shell_center_image): Allow to choose what axes to + center on. + + (gimp_display_shell_scale_fill) + (gimp_display_shell_scale_fit_in): Explicitly center on both axes. + + * app/display/gimpdisplayshell.c (gimp_display_shell_fill): Center + the image in the filled display shell. Rather hackish, but seems + to work fine. + 2008-07-13 Sven Neumann * app/widgets/Makefile.am diff --git a/app/display/gimpdisplayshell-scale.c b/app/display/gimpdisplayshell-scale.c index 3014521236..a33dcbf9b6 100644 --- a/app/display/gimpdisplayshell-scale.c +++ b/app/display/gimpdisplayshell-scale.c @@ -431,7 +431,7 @@ gimp_display_shell_scale_fit_in (GimpDisplayShell *shell) (gdouble) shell->disp_height / (gdouble) image_height); gimp_display_shell_scale (shell, GIMP_ZOOM_TO, zoom_factor); - gimp_display_shell_center_image (shell); + gimp_display_shell_center_image (shell, TRUE, TRUE); } /** @@ -470,18 +470,22 @@ gimp_display_shell_scale_fill (GimpDisplayShell *shell) (gdouble) shell->disp_height / (gdouble) image_height); gimp_display_shell_scale (shell, GIMP_ZOOM_TO, zoom_factor); - gimp_display_shell_center_image (shell); + gimp_display_shell_center_image (shell, TRUE, TRUE); } /** * gimp_display_shell_center_image: * @shell: + * @horizontally: + * @vertically: * - * Centers the image in the display shell. + * Centers the image in the display shell on the desired axes. * **/ void -gimp_display_shell_center_image (GimpDisplayShell *shell) +gimp_display_shell_center_image (GimpDisplayShell *shell, + gboolean horizontally, + gboolean vertically) { gint sw, sh; gint target_offset_x, target_offset_y; @@ -491,24 +495,33 @@ gimp_display_shell_center_image (GimpDisplayShell *shell) if (! shell->display) return; + target_offset_x = shell->offset_x; + target_offset_y = shell->offset_y; + gimp_display_shell_get_scaled_image_size (shell, &sw, &sh); - if (sw < shell->disp_width) + if (horizontally) { - target_offset_x = -(shell->disp_width - sw) / 2; - } - else - { - target_offset_x = (sw - shell->disp_width) / 2; + if (sw < shell->disp_width) + { + target_offset_x = -(shell->disp_width - sw) / 2; + } + else + { + target_offset_x = (sw - shell->disp_width) / 2; + } } - if (sh < shell->disp_height) + if (vertically) { - target_offset_y = -(shell->disp_height - sh) / 2; - } - else - { - target_offset_y = (sh - shell->disp_height) / 2; + if (sh < shell->disp_height) + { + target_offset_y = -(shell->disp_height - sh) / 2; + } + else + { + target_offset_y = (sh - shell->disp_height) / 2; + } } /* Note that we can't use gimp_display_shell_scroll_private() here diff --git a/app/display/gimpdisplayshell-scale.h b/app/display/gimpdisplayshell-scale.h index c0dc0d2ea0..9d4eef36ee 100644 --- a/app/display/gimpdisplayshell-scale.h +++ b/app/display/gimpdisplayshell-scale.h @@ -38,7 +38,9 @@ void gimp_display_shell_scale_to (GimpDisplayShell *shell, gdouble y); void gimp_display_shell_scale_fit_in (GimpDisplayShell *shell); void gimp_display_shell_scale_fill (GimpDisplayShell *shell); -void gimp_display_shell_center_image (GimpDisplayShell *shell); +void gimp_display_shell_center_image (GimpDisplayShell *shell, + gboolean horizontally, + gboolean vertically); void gimp_display_shell_scale_by_values (GimpDisplayShell *shell, gdouble scale, gint offset_x, diff --git a/app/display/gimpdisplayshell.c b/app/display/gimpdisplayshell.c index 3d2ef44ee6..eb4d5dc15f 100644 --- a/app/display/gimpdisplayshell.c +++ b/app/display/gimpdisplayshell.c @@ -1316,6 +1316,35 @@ gimp_display_shell_empty (GimpDisplayShell *shell) gimp_ui_manager_update (shell->popup_manager, shell->display); } +static void +gimp_display_shell_center_image_callback (GimpDisplayShell *shell, + GtkAllocation *allocation, + GtkWidget *canvas) +{ + gint sw, sh; + gboolean center_horizontally; + gboolean center_vertically; + + gimp_display_shell_get_scaled_image_size (shell, &sw, &sh); + + /* We only want to center on the axes on which the image is smaller + * than the display canvas. If it is larger, it will be centered on + * that axis later, and if we center on all axis unconditionally, we + * end up with the wrong centering if the image is larger than the + * display canvas. + */ + center_horizontally = sw < shell->disp_width; + center_vertically = sh < shell->disp_height; + + gimp_display_shell_center_image (shell, + center_horizontally, + center_vertically); + + g_signal_handlers_disconnect_by_func (canvas, + gimp_display_shell_center_image_callback, + shell); +} + static gboolean gimp_display_shell_fill_idle (GimpDisplayShell *shell) { @@ -1351,6 +1380,15 @@ gimp_display_shell_fill (GimpDisplayShell *shell, gimp_help_set_help_data (shell->canvas, NULL, NULL); + /* Not pretty, but we need to center the image as soon as the canvas + * has got its new size allocated. The centering will be wrong if we + * do it too early, and if we do it too late flickering will occur + * due to the image being rendered twice. + */ + g_signal_connect_swapped (shell->canvas, "size-allocate", + G_CALLBACK (gimp_display_shell_center_image_callback), + shell); + shell->fill_idle_id = g_idle_add_full (G_PRIORITY_LOW, (GSourceFunc) gimp_display_shell_fill_idle, shell, NULL);