evolution-3.6.4/widgets/misc/e-canvas-utils.c

No issues found

  1 /*
  2  *
  3  * This program is free software; you can redistribute it and/or
  4  * modify it under the terms of the GNU Lesser General Public
  5  * License as published by the Free Software Foundation; either
  6  * version 2 of the License, or (at your option) version 3.
  7  *
  8  * This program is distributed in the hope that it will be useful,
  9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 11  * Lesser General Public License for more details.
 12  *
 13  * You should have received a copy of the GNU Lesser General Public
 14  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 15  *
 16  *
 17  * Authors:
 18  *		Chris Lahey <clahey@ximian.com>
 19  *
 20  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 21  *
 22  */
 23 
 24 #ifdef HAVE_CONFIG_H
 25 #include <config.h>
 26 #endif
 27 
 28 #include "e-canvas-utils.h"
 29 
 30 void
 31 e_canvas_item_move_absolute (GnomeCanvasItem *item,
 32                              gdouble dx,
 33                              gdouble dy)
 34 {
 35 	cairo_matrix_t translate;
 36 
 37 	g_return_if_fail (GNOME_IS_CANVAS_ITEM (item));
 38 
 39 	cairo_matrix_init_translate (&translate, dx, dy);
 40 
 41 	gnome_canvas_item_set_matrix (item, &translate);
 42 }
 43 
 44 static double
 45 compute_offset (gint top,
 46                 gint bottom,
 47                 gint page_top,
 48                 gint page_bottom)
 49 {
 50 	gint size = bottom - top;
 51 	gint offset = 0;
 52 
 53 	if (top <= page_top && bottom >= page_bottom)
 54 		return 0;
 55 
 56 	if (bottom > page_bottom)
 57 		offset = (bottom - page_bottom);
 58 	if (top < page_top + offset)
 59 		offset = (top - page_top);
 60 
 61 	if (top <= page_top + offset && bottom >= page_bottom + offset)
 62 		return offset;
 63 
 64 	if (top < page_top + size * 3 / 2 + offset)
 65 		offset = top - (page_top + size * 3 / 2);
 66 	if (bottom > page_bottom - size * 3 / 2 + offset)
 67 		offset = bottom - (page_bottom - size * 3 / 2);
 68 	if (top < page_top + size * 3 / 2 + offset)
 69 		offset = top - ((page_top + page_bottom - (bottom - top)) / 2);
 70 
 71 	return offset;
 72 }
 73 
 74 static void
 75 e_canvas_show_area (GnomeCanvas *canvas,
 76                     gdouble x1,
 77                     gdouble y1,
 78                     gdouble x2,
 79                     gdouble y2)
 80 {
 81 	GtkAdjustment *h, *v;
 82 	gint dx = 0, dy = 0;
 83 	gdouble page_size;
 84 	gdouble lower;
 85 	gdouble upper;
 86 	gdouble value;
 87 
 88 	g_return_if_fail (canvas != NULL);
 89 	g_return_if_fail (GNOME_IS_CANVAS (canvas));
 90 
 91 	h = gtk_scrollable_get_hadjustment (GTK_SCROLLABLE (canvas));
 92 	page_size = gtk_adjustment_get_page_size (h);
 93 	lower = gtk_adjustment_get_lower (h);
 94 	upper = gtk_adjustment_get_upper (h);
 95 	value = gtk_adjustment_get_value (h);
 96 	dx = compute_offset (x1, x2, value, value + page_size);
 97 	if (dx)
 98 		gtk_adjustment_set_value (h, CLAMP (value + dx, lower, upper - page_size));
 99 
100 	v = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (canvas));
101 	page_size = gtk_adjustment_get_page_size (v);
102 	lower = gtk_adjustment_get_lower (v);
103 	upper = gtk_adjustment_get_upper (v);
104 	value = gtk_adjustment_get_value (v);
105 	dy = compute_offset (y1, y2, value, value + page_size);
106 	if (dy)
107 		gtk_adjustment_set_value (v, CLAMP (value + dy, lower, upper - page_size));
108 }
109 
110 void
111 e_canvas_item_show_area (GnomeCanvasItem *item,
112                          gdouble x1,
113                          gdouble y1,
114                          gdouble x2,
115                          gdouble y2)
116 {
117 	g_return_if_fail (item != NULL);
118 	g_return_if_fail (GNOME_IS_CANVAS_ITEM (item));
119 
120 	gnome_canvas_item_i2w (item, &x1, &y1);
121 	gnome_canvas_item_i2w (item, &x2, &y2);
122 
123 	e_canvas_show_area (item->canvas, x1, y1, x2, y2);
124 }
125 
126 static gboolean
127 e_canvas_area_shown (GnomeCanvas *canvas,
128                      gdouble x1,
129                      gdouble y1,
130                      gdouble x2,
131                      gdouble y2)
132 {
133 	GtkAdjustment *h, *v;
134 	gint dx = 0, dy = 0;
135 	gdouble page_size;
136 	gdouble lower;
137 	gdouble upper;
138 	gdouble value;
139 
140 	g_return_val_if_fail (canvas != NULL, FALSE);
141 	g_return_val_if_fail (GNOME_IS_CANVAS (canvas), FALSE);
142 
143 	h = gtk_scrollable_get_hadjustment (GTK_SCROLLABLE (canvas));
144 	page_size = gtk_adjustment_get_page_size (h);
145 	lower = gtk_adjustment_get_lower (h);
146 	upper = gtk_adjustment_get_upper (h);
147 	value = gtk_adjustment_get_value (h);
148 	dx = compute_offset (x1, x2, value, value + page_size);
149 	if (CLAMP (value + dx, lower, upper - page_size) - value != 0)
150 		return FALSE;
151 
152 	v = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (canvas));
153 	page_size = gtk_adjustment_get_page_size (v);
154 	lower = gtk_adjustment_get_lower (v);
155 	upper = gtk_adjustment_get_upper (v);
156 	value = gtk_adjustment_get_value (v);
157 	dy = compute_offset (y1, y2, value, value + page_size);
158 	if (CLAMP (value + dy, lower, upper - page_size) - value != 0)
159 		return FALSE;
160 	return TRUE;
161 }
162 
163 gboolean
164 e_canvas_item_area_shown (GnomeCanvasItem *item,
165                           gdouble x1,
166                           gdouble y1,
167                           gdouble x2,
168                           gdouble y2)
169 {
170 	g_return_val_if_fail (item != NULL, FALSE);
171 	g_return_val_if_fail (GNOME_IS_CANVAS_ITEM (item), FALSE);
172 
173 	gnome_canvas_item_i2w (item, &x1, &y1);
174 	gnome_canvas_item_i2w (item, &x2, &y2);
175 
176 	return e_canvas_area_shown (item->canvas, x1, y1, x2, y2);
177 }
178 
179 typedef struct {
180 	gdouble x1;
181 	gdouble y1;
182 	gdouble x2;
183 	gdouble y2;
184 	GnomeCanvas *canvas;
185 } DoubsAndCanvas;
186 
187 static gboolean
188 show_area_timeout (gpointer data)
189 {
190 	DoubsAndCanvas *dac = data;
191 
192 	e_canvas_show_area (dac->canvas, dac->x1, dac->y1, dac->x2, dac->y2);
193 	g_object_unref (dac->canvas);
194 	g_free (dac);
195 	return FALSE;
196 }
197 
198 void
199 e_canvas_item_show_area_delayed (GnomeCanvasItem *item,
200                                  gdouble x1,
201                                  gdouble y1,
202                                  gdouble x2,
203                                  gdouble y2,
204                                  gint delay)
205 {
206 	DoubsAndCanvas *dac;
207 
208 	g_return_if_fail (item != NULL);
209 	g_return_if_fail (GNOME_IS_CANVAS_ITEM (item));
210 
211 	gnome_canvas_item_i2w (item, &x1, &y1);
212 	gnome_canvas_item_i2w (item, &x2, &y2);
213 
214 	dac = g_new (DoubsAndCanvas, 1);
215 	dac->x1 = x1;
216 	dac->y1 = y1;
217 	dac->x2 = x2;
218 	dac->y2 = y2;
219 	dac->canvas = item->canvas;
220 	g_object_ref (item->canvas);
221 	g_timeout_add (delay, show_area_timeout, dac);
222 }