Updated Spanish translation
[evolution.git] / e-util / e-canvas-utils.c
blobfa753c597cd4db41b77adff7daad5be04ea663f0
1 /*
3 * This program is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU Lesser General Public License as published by
5 * the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
10 * for more details.
12 * You should have received a copy of the GNU Lesser General Public License
13 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 * Authors:
17 * Chris Lahey <clahey@ximian.com>
19 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
23 #include "e-canvas-utils.h"
25 #include <config.h>
27 #include <libedataserver/libedataserver.h>
29 void
30 e_canvas_item_move_absolute (GnomeCanvasItem *item,
31 gdouble dx,
32 gdouble dy)
34 cairo_matrix_t translate;
36 g_return_if_fail (GNOME_IS_CANVAS_ITEM (item));
38 cairo_matrix_init_translate (&translate, dx, dy);
40 gnome_canvas_item_set_matrix (item, &translate);
43 static double
44 compute_offset (gint top,
45 gint bottom,
46 gint page_top,
47 gint page_bottom)
49 gint size = bottom - top;
50 gint offset = 0;
52 if (top <= page_top && bottom >= page_bottom)
53 return 0;
55 if (bottom > page_bottom)
56 offset = (bottom - page_bottom);
57 if (top < page_top + offset)
58 offset = (top - page_top);
60 if (top <= page_top + offset && bottom >= page_bottom + offset)
61 return offset;
63 if (top < page_top + size * 3 / 2 + offset)
64 offset = top - (page_top + size * 3 / 2);
65 if (bottom > page_bottom - size * 3 / 2 + offset)
66 offset = bottom - (page_bottom - size * 3 / 2);
67 if (top < page_top + size * 3 / 2 + offset)
68 offset = top - ((page_top + page_bottom - (bottom - top)) / 2);
70 return offset;
73 static void
74 e_canvas_show_area (GnomeCanvas *canvas,
75 gdouble x1,
76 gdouble y1,
77 gdouble x2,
78 gdouble y2)
80 GtkAdjustment *h, *v;
81 gint dx = 0, dy = 0;
82 gdouble page_size;
83 gdouble lower;
84 gdouble upper;
85 gdouble value;
87 g_return_if_fail (canvas != NULL);
88 g_return_if_fail (GNOME_IS_CANVAS (canvas));
90 h = gtk_scrollable_get_hadjustment (GTK_SCROLLABLE (canvas));
91 page_size = gtk_adjustment_get_page_size (h);
92 lower = gtk_adjustment_get_lower (h);
93 upper = gtk_adjustment_get_upper (h);
94 value = gtk_adjustment_get_value (h);
95 dx = compute_offset (x1, x2, value, value + page_size);
96 if (dx)
97 gtk_adjustment_set_value (h, CLAMP (value + dx, lower, upper - page_size));
99 v = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (canvas));
100 page_size = gtk_adjustment_get_page_size (v);
101 lower = gtk_adjustment_get_lower (v);
102 upper = gtk_adjustment_get_upper (v);
103 value = gtk_adjustment_get_value (v);
104 dy = compute_offset (y1, y2, value, value + page_size);
105 if (dy)
106 gtk_adjustment_set_value (v, CLAMP (value + dy, lower, upper - page_size));
109 void
110 e_canvas_item_show_area (GnomeCanvasItem *item,
111 gdouble x1,
112 gdouble y1,
113 gdouble x2,
114 gdouble y2)
116 g_return_if_fail (item != NULL);
117 g_return_if_fail (GNOME_IS_CANVAS_ITEM (item));
119 gnome_canvas_item_i2w (item, &x1, &y1);
120 gnome_canvas_item_i2w (item, &x2, &y2);
122 e_canvas_show_area (item->canvas, x1, y1, x2, y2);
125 static gboolean
126 e_canvas_area_shown (GnomeCanvas *canvas,
127 gdouble x1,
128 gdouble y1,
129 gdouble x2,
130 gdouble y2)
132 GtkAdjustment *h, *v;
133 gint dx = 0, dy = 0;
134 gdouble page_size;
135 gdouble lower;
136 gdouble upper;
137 gdouble value;
139 g_return_val_if_fail (canvas != NULL, FALSE);
140 g_return_val_if_fail (GNOME_IS_CANVAS (canvas), FALSE);
142 h = gtk_scrollable_get_hadjustment (GTK_SCROLLABLE (canvas));
143 page_size = gtk_adjustment_get_page_size (h);
144 lower = gtk_adjustment_get_lower (h);
145 upper = gtk_adjustment_get_upper (h);
146 value = gtk_adjustment_get_value (h);
147 dx = compute_offset (x1, x2, value, value + page_size);
148 if (CLAMP (value + dx, lower, upper - page_size) - value != 0)
149 return FALSE;
151 v = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (canvas));
152 page_size = gtk_adjustment_get_page_size (v);
153 lower = gtk_adjustment_get_lower (v);
154 upper = gtk_adjustment_get_upper (v);
155 value = gtk_adjustment_get_value (v);
156 dy = compute_offset (y1, y2, value, value + page_size);
157 if (CLAMP (value + dy, lower, upper - page_size) - value != 0)
158 return FALSE;
159 return TRUE;
162 gboolean
163 e_canvas_item_area_shown (GnomeCanvasItem *item,
164 gdouble x1,
165 gdouble y1,
166 gdouble x2,
167 gdouble y2)
169 g_return_val_if_fail (item != NULL, FALSE);
170 g_return_val_if_fail (GNOME_IS_CANVAS_ITEM (item), FALSE);
172 gnome_canvas_item_i2w (item, &x1, &y1);
173 gnome_canvas_item_i2w (item, &x2, &y2);
175 return e_canvas_area_shown (item->canvas, x1, y1, x2, y2);
178 typedef struct {
179 gdouble x1;
180 gdouble y1;
181 gdouble x2;
182 gdouble y2;
183 GnomeCanvas *canvas;
184 } DoubsAndCanvas;
186 static void
187 doubs_and_canvas_free (gpointer ptr)
189 DoubsAndCanvas *dac = ptr;
191 if (dac) {
192 g_object_unref (dac->canvas);
193 g_free (dac);
197 static gboolean
198 show_area_timeout (gpointer data)
200 DoubsAndCanvas *dac = data;
202 e_canvas_show_area (dac->canvas, dac->x1, dac->y1, dac->x2, dac->y2);
204 return FALSE;
207 void
208 e_canvas_item_show_area_delayed (GnomeCanvasItem *item,
209 gdouble x1,
210 gdouble y1,
211 gdouble x2,
212 gdouble y2,
213 gint delay)
215 GSource *source;
217 source = e_canvas_item_show_area_delayed_ex (item, x1, y1, x2, y2, delay);
218 if (source)
219 g_source_unref (source);
222 /* Use g_source_unref() when done with the returned pointer. */
223 GSource *
224 e_canvas_item_show_area_delayed_ex (GnomeCanvasItem *item,
225 gdouble x1,
226 gdouble y1,
227 gdouble x2,
228 gdouble y2,
229 gint delay)
231 GSource *source;
232 DoubsAndCanvas *dac;
234 g_return_val_if_fail (item != NULL, NULL);
235 g_return_val_if_fail (GNOME_IS_CANVAS_ITEM (item), NULL);
237 gnome_canvas_item_i2w (item, &x1, &y1);
238 gnome_canvas_item_i2w (item, &x2, &y2);
240 dac = g_new (DoubsAndCanvas, 1);
241 dac->x1 = x1;
242 dac->y1 = y1;
243 dac->x2 = x2;
244 dac->y2 = y2;
245 dac->canvas = g_object_ref (item->canvas);
247 source = g_timeout_source_new (delay);
248 g_source_set_callback (source, show_area_timeout, dac, doubs_and_canvas_free);
249 g_source_set_name (source, G_STRFUNC);
250 g_source_attach (source, NULL);
252 return source;