From d3bf6766abeb0650990463f69fd7118c0bf1d188 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristian=20H=C3=B8gsberg?= Date: Tue, 30 Oct 2012 15:46:25 -0400 Subject: [PATCH] image: Combine clamp_view() and center_view () We need to clamp or center on a per axis basis. If the window is wider but the image is taller, we need to center horizontally but clamp vertically. We can only do that if by combining the two functions. --- clients/image.c | 63 ++++++++++++++++++------------------------------- 1 file changed, 23 insertions(+), 40 deletions(-) diff --git a/clients/image.c b/clients/image.c index 2cdce416..1c240a58 100644 --- a/clients/image.c +++ b/clients/image.c @@ -69,17 +69,6 @@ get_scale(struct image *image) return image->matrix.xx; } -static void -center_view(struct image *image) -{ - struct rectangle allocation; - double scale = get_scale(image); - - widget_get_allocation(image->widget, &allocation); - image->matrix.x0 = (allocation.width - image->width * scale) / 2; - image->matrix.y0 = (allocation.height - image->height * scale) / 2; -} - static void clamp_view(struct image *image) { @@ -91,14 +80,25 @@ clamp_view(struct image *image) sh = image->height * scale; widget_get_allocation(image->widget, &allocation); - if (image->matrix.x0 > 0.0) - image->matrix.x0 = 0.0; - if (image->matrix.y0 > 0.0) - image->matrix.y0 = 0.0; - if (sw + image->matrix.x0 < allocation.width) - image->matrix.x0 = allocation.width - sw; - if (sh + image->matrix.y0 < allocation.height) - image->matrix.y0 = allocation.height - sh; + if (sw < allocation.width) { + image->matrix.x0 = + (allocation.width - image->width * scale) / 2; + } else { + if (image->matrix.x0 > 0.0) + image->matrix.x0 = 0.0; + if (sw + image->matrix.x0 < allocation.width) + image->matrix.x0 = allocation.width - sw; + } + + if (sh < allocation.width) { + image->matrix.y0 = + (allocation.height - image->height * scale) / 2; + } else { + if (image->matrix.y0 > 0.0) + image->matrix.y0 = 0.0; + if (sh + image->matrix.y0 < allocation.height) + image->matrix.y0 = allocation.height - sh; + } } static void @@ -141,7 +141,7 @@ redraw_handler(struct widget *widget, void *data) image->height = height; cairo_matrix_init_scale(&image->matrix, scale, scale); - center_view(image); + clamp_view(image); } matrix = image->matrix; @@ -166,7 +166,7 @@ resize_handler(struct widget *widget, { struct image *image = data; - center_view(image); + clamp_view(image); } static void @@ -196,18 +196,6 @@ enter_handler(struct widget *widget, return 1; } -static bool -image_is_smaller(struct image *image) -{ - double scale; - struct rectangle allocation; - - scale = get_scale(image); - widget_get_allocation(image->widget, &allocation); - - return scale * image->width < allocation.width; -} - static void move_viewport(struct image *image, double dx, double dy) { @@ -217,11 +205,7 @@ move_viewport(struct image *image, double dx, double dy) return; cairo_matrix_translate(&image->matrix, -dx/scale, -dy/scale); - - if (image_is_smaller(image)) - center_view(image); - else - clamp_view(image); + clamp_view(image); window_schedule_redraw(image->window); } @@ -288,8 +272,7 @@ zoom(struct image *image, double scale) cairo_matrix_translate(&scale_matrix, -x, -y); cairo_matrix_multiply(&image->matrix, &image->matrix, &scale_matrix); - if (image_is_smaller(image)) - center_view(image); + clamp_view(image); } static void