1 | /* Eye of Gnome - default item factory for icons |
---|
2 | * |
---|
3 | * Copyright (C) 2002 The Free Software Foundation |
---|
4 | * |
---|
5 | * Author: Jens Finke <jens@gnome.org> |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or modify |
---|
8 | * it under the terms of the GNU General Public License as published by |
---|
9 | * the Free Software Foundation; either version 2 of the License, or |
---|
10 | * (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
20 | */ |
---|
21 | |
---|
22 | #include <config.h> |
---|
23 | #include <string.h> |
---|
24 | #include <pango/pango-layout.h> |
---|
25 | #include <gtk/gtksignal.h> |
---|
26 | #include <libgnome/gnome-macros.h> |
---|
27 | #include <libgnomecanvas/gnome-canvas-pixbuf.h> |
---|
28 | #include <libgnomecanvas/gnome-canvas-rect-ellipse.h> |
---|
29 | #include <libgnomecanvas/gnome-canvas-text.h> |
---|
30 | #include <libgnomevfs/gnome-vfs-types.h> |
---|
31 | #include <libgnomevfs/gnome-vfs-uri.h> |
---|
32 | #include "eog-item-factory-clean.h" |
---|
33 | #include "eog-collection-model.h" |
---|
34 | #include "cimage.h" |
---|
35 | #include <math.h> |
---|
36 | |
---|
37 | |
---|
38 | #define MAX_CAPTION_LINES 2 |
---|
39 | #define FACTORY_DEBUG 0 |
---|
40 | |
---|
41 | /* Private part of the EogItemFactoryClean structure */ |
---|
42 | struct _EogItemFactoryCleanPrivate { |
---|
43 | EogImageLoader *loader; |
---|
44 | |
---|
45 | /* item metrics */ |
---|
46 | EogCleanMetrics *metrics; |
---|
47 | |
---|
48 | /* default pixmap if the image is not loaded so far */ |
---|
49 | GdkPixbuf *dummy; |
---|
50 | |
---|
51 | /* selection stiple */ |
---|
52 | GdkBitmap *stipple; |
---|
53 | }; |
---|
54 | |
---|
55 | |
---|
56 | |
---|
57 | /* Icon item structure */ |
---|
58 | typedef struct { |
---|
59 | /* unique id */ |
---|
60 | guint id; |
---|
61 | |
---|
62 | /* Base group */ |
---|
63 | GnomeCanvasItem *item; |
---|
64 | |
---|
65 | /* Icon image and its selection rectangle */ |
---|
66 | GnomeCanvasItem *image; |
---|
67 | |
---|
68 | /* Caption and its selection and focus rectangles */ |
---|
69 | GnomeCanvasItem *caption_line[MAX_CAPTION_LINES]; |
---|
70 | |
---|
71 | GnomeCanvasItem *cap_rect; |
---|
72 | |
---|
73 | GnomeCanvasItem *bgr; |
---|
74 | } IconItem; |
---|
75 | |
---|
76 | static void eog_item_factory_clean_class_init (EogItemFactoryCleanClass *class); |
---|
77 | static void eog_item_factory_clean_instance_init (EogItemFactoryClean *factory); |
---|
78 | static void eog_item_factory_clean_dispose (GObject *object); |
---|
79 | static void eog_item_factory_clean_finalize (GObject *object); |
---|
80 | static EogItemFactoryClean* eog_item_factory_clean_construct (EogItemFactoryClean *factory, EogImageLoader *loader); |
---|
81 | |
---|
82 | static GnomeCanvasItem *ii_factory_create_item (EogItemFactory *factory, |
---|
83 | GnomeCanvasGroup *parent, guint id); |
---|
84 | static void ii_factory_update_item (EogItemFactory *factory, |
---|
85 | EogCollectionModel *model, |
---|
86 | GnomeCanvasItem *item, |
---|
87 | EogItemUpdateHint hint); |
---|
88 | static void ii_factory_get_item_size (EogItemFactory *factory, |
---|
89 | gint *width, gint *height); |
---|
90 | |
---|
91 | GNOME_CLASS_BOILERPLATE (EogItemFactoryClean, eog_item_factory_clean, |
---|
92 | EogItemFactory, EOG_TYPE_ITEM_FACTORY); |
---|
93 | |
---|
94 | /* Class initialization function for the icon list item factory */ |
---|
95 | static void |
---|
96 | eog_item_factory_clean_class_init (EogItemFactoryCleanClass *class) |
---|
97 | { |
---|
98 | GObjectClass *object_class; |
---|
99 | EogItemFactoryClass *ei_factory_class; |
---|
100 | |
---|
101 | object_class = (GObjectClass *) class; |
---|
102 | ei_factory_class = (EogItemFactoryClass *) class; |
---|
103 | |
---|
104 | object_class->dispose = eog_item_factory_clean_dispose; |
---|
105 | object_class->finalize = eog_item_factory_clean_finalize; |
---|
106 | |
---|
107 | ei_factory_class->create_item = ii_factory_create_item; |
---|
108 | ei_factory_class->update_item = ii_factory_update_item; |
---|
109 | ei_factory_class->get_item_size = ii_factory_get_item_size; |
---|
110 | } |
---|
111 | |
---|
112 | /* Object initialization function for the icon list item factory */ |
---|
113 | static void |
---|
114 | eog_item_factory_clean_instance_init (EogItemFactoryClean *factory) |
---|
115 | { |
---|
116 | EogItemFactoryCleanPrivate *priv; |
---|
117 | EogCleanMetrics *metrics; |
---|
118 | char stipple_bits[] = { 0x00, 0x01, 0x01, 0x00 }; |
---|
119 | |
---|
120 | priv = g_new0 (EogItemFactoryCleanPrivate, 1); |
---|
121 | |
---|
122 | metrics = g_new0 (EogCleanMetrics, 1); |
---|
123 | metrics->twidth = 96; |
---|
124 | metrics->theight = 96; |
---|
125 | metrics->cspace = 5; |
---|
126 | metrics->cpadding = 2; |
---|
127 | metrics->font_height = 12; |
---|
128 | priv->metrics = metrics; |
---|
129 | |
---|
130 | priv->stipple = gdk_bitmap_create_from_data (NULL, stipple_bits, 2, 2); |
---|
131 | priv->loader = NULL; |
---|
132 | |
---|
133 | factory->priv = priv; |
---|
134 | } |
---|
135 | |
---|
136 | /* Destroy handler for the icon list item factory */ |
---|
137 | static void |
---|
138 | eog_item_factory_clean_dispose (GObject *object) |
---|
139 | { |
---|
140 | EogItemFactoryClean *factory; |
---|
141 | EogItemFactoryCleanPrivate *priv; |
---|
142 | |
---|
143 | g_return_if_fail (object != NULL); |
---|
144 | g_return_if_fail (EOG_IS_ITEM_FACTORY_CLEAN (object)); |
---|
145 | |
---|
146 | factory = EOG_ITEM_FACTORY_CLEAN (object); |
---|
147 | priv = factory->priv; |
---|
148 | |
---|
149 | if (priv->dummy) |
---|
150 | g_object_unref (priv->dummy); |
---|
151 | priv->dummy = NULL; |
---|
152 | |
---|
153 | if (priv->metrics) |
---|
154 | g_free (priv->metrics); |
---|
155 | priv->metrics = NULL; |
---|
156 | |
---|
157 | if (priv->stipple) |
---|
158 | g_object_unref (G_OBJECT (priv->stipple)); |
---|
159 | priv->stipple = NULL; |
---|
160 | |
---|
161 | if (priv->loader) |
---|
162 | g_object_unref (G_OBJECT (priv->loader)); |
---|
163 | priv->loader = NULL; |
---|
164 | |
---|
165 | GNOME_CALL_PARENT (G_OBJECT_CLASS, dispose, (object)); |
---|
166 | } |
---|
167 | |
---|
168 | static void |
---|
169 | eog_item_factory_clean_finalize (GObject *object) |
---|
170 | { |
---|
171 | EogItemFactoryClean *factory; |
---|
172 | |
---|
173 | g_return_if_fail (object != NULL); |
---|
174 | g_return_if_fail (EOG_IS_ITEM_FACTORY_CLEAN (object)); |
---|
175 | |
---|
176 | factory = EOG_ITEM_FACTORY_CLEAN (object); |
---|
177 | g_free (factory->priv); |
---|
178 | |
---|
179 | GNOME_CALL_PARENT (G_OBJECT_CLASS, finalize, (object)); |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | |
---|
184 | static GtkStyle* |
---|
185 | get_style_from_item (GnomeCanvasItem *item) |
---|
186 | { |
---|
187 | return gtk_widget_get_style (GTK_WIDGET (item->canvas)); |
---|
188 | } |
---|
189 | |
---|
190 | |
---|
191 | /* Called when an icon's main group is destroyed */ |
---|
192 | static void |
---|
193 | icon_destroyed (GObject *object, gpointer data) |
---|
194 | { |
---|
195 | IconItem *icon; |
---|
196 | |
---|
197 | icon = (IconItem*)data; |
---|
198 | g_free (icon); |
---|
199 | } |
---|
200 | |
---|
201 | /* Create_item handler for the icon list item factory */ |
---|
202 | static GnomeCanvasItem * |
---|
203 | ii_factory_create_item (EogItemFactory *factory, |
---|
204 | GnomeCanvasGroup *parent, guint unique_id) |
---|
205 | { |
---|
206 | IconItem *icon; |
---|
207 | int width, height; |
---|
208 | int i; |
---|
209 | GtkStyle *style; |
---|
210 | |
---|
211 | g_return_val_if_fail (factory != NULL, NULL); |
---|
212 | g_return_val_if_fail (EOG_IS_ITEM_FACTORY_CLEAN (factory), NULL); |
---|
213 | g_return_val_if_fail (parent != NULL, NULL); |
---|
214 | g_return_val_if_fail (GNOME_IS_CANVAS_GROUP (parent), NULL); |
---|
215 | |
---|
216 | eog_item_factory_get_item_size (EOG_ITEM_FACTORY (factory), &width, &height); |
---|
217 | |
---|
218 | icon = g_new (IconItem, 1); |
---|
219 | |
---|
220 | icon->id = unique_id; |
---|
221 | icon->item = gnome_canvas_item_new (GNOME_CANVAS_GROUP (parent), |
---|
222 | GNOME_TYPE_CANVAS_GROUP, |
---|
223 | NULL); |
---|
224 | |
---|
225 | style = get_style_from_item (icon->item); |
---|
226 | g_assert (style != NULL); |
---|
227 | |
---|
228 | icon->bgr = gnome_canvas_item_new (GNOME_CANVAS_GROUP (icon->item), |
---|
229 | GNOME_TYPE_CANVAS_RECT, |
---|
230 | "x1", 0.0, |
---|
231 | "y1", 0.0, |
---|
232 | "x2", (double) width, |
---|
233 | "y2", (double) height, |
---|
234 | "fill_color_gdk", &style->bg[GTK_STATE_NORMAL], |
---|
235 | NULL); |
---|
236 | |
---|
237 | for (i = 0; i < MAX_CAPTION_LINES; i++) icon->caption_line[i] = NULL; |
---|
238 | icon->cap_rect = NULL; |
---|
239 | icon->image = NULL; |
---|
240 | |
---|
241 | g_object_set_data (G_OBJECT (icon->item), "IconItem", icon); |
---|
242 | g_signal_connect (G_OBJECT (icon->item), "destroy", |
---|
243 | G_CALLBACK (icon_destroyed), |
---|
244 | icon); |
---|
245 | |
---|
246 | return icon->item; |
---|
247 | } |
---|
248 | |
---|
249 | /*********************************************************** |
---|
250 | * |
---|
251 | * Caption Helper Functions |
---|
252 | * |
---|
253 | */ |
---|
254 | |
---|
255 | /* Shrink the string until its pixel width is <= max_width */ |
---|
256 | static char* |
---|
257 | ensure_max_string_width (gchar *str, PangoLayout *layout, int max_width, gchar **tail) |
---|
258 | { |
---|
259 | char *result; |
---|
260 | int len; |
---|
261 | int str_len; |
---|
262 | int str_bytes; |
---|
263 | gchar* str_pt = NULL; |
---|
264 | int px_width, px_height; |
---|
265 | |
---|
266 | if (tail != NULL) *tail = NULL; |
---|
267 | |
---|
268 | str_len = g_utf8_strlen (str, -1); |
---|
269 | str_bytes = strlen (str); |
---|
270 | |
---|
271 | pango_layout_set_text (layout, str, str_bytes); |
---|
272 | pango_layout_get_pixel_size (layout, &px_width, &px_height); |
---|
273 | |
---|
274 | if (px_width <= max_width) { |
---|
275 | return g_strdup (str); |
---|
276 | } |
---|
277 | |
---|
278 | len = str_len; |
---|
279 | g_assert (len > 0); |
---|
280 | |
---|
281 | while (px_width > max_width) { |
---|
282 | len--; |
---|
283 | |
---|
284 | if (len <= 0) |
---|
285 | break; |
---|
286 | |
---|
287 | str_pt = g_utf8_offset_to_pointer (str, len); |
---|
288 | |
---|
289 | pango_layout_set_text (layout, str, (int) (str_pt - str)); |
---|
290 | pango_layout_get_pixel_size (layout, &px_width, &px_height); |
---|
291 | } |
---|
292 | |
---|
293 | if (len > 0) { |
---|
294 | result = g_new0 (gchar, 2 * len); |
---|
295 | result = g_utf8_strncpy (result, str, len); |
---|
296 | if (tail != NULL) { |
---|
297 | *tail = g_strdup (str_pt); |
---|
298 | } |
---|
299 | } |
---|
300 | else { |
---|
301 | result = NULL; |
---|
302 | if (tail != NULL) { |
---|
303 | *tail = g_strdup (str); |
---|
304 | } |
---|
305 | } |
---|
306 | |
---|
307 | return result; |
---|
308 | } |
---|
309 | |
---|
310 | static void |
---|
311 | create_item_caption_lines (gchar *str, PangoLayout *layout, int max_width, |
---|
312 | char **line, int n_lines) |
---|
313 | { |
---|
314 | char *remaining; |
---|
315 | int px_width, px_height; |
---|
316 | gchar *tail; |
---|
317 | int l; |
---|
318 | |
---|
319 | g_return_if_fail (n_lines > 1); |
---|
320 | g_return_if_fail (str != NULL); |
---|
321 | |
---|
322 | for (l = 0; l < n_lines; l++) { |
---|
323 | line[l] = NULL; |
---|
324 | } |
---|
325 | |
---|
326 | remaining = g_strdup (str); |
---|
327 | |
---|
328 | for (l = 0; l < n_lines; l++) |
---|
329 | { |
---|
330 | tail = NULL; |
---|
331 | |
---|
332 | pango_layout_set_text (layout, remaining, -1); |
---|
333 | pango_layout_get_pixel_size (layout, &px_width, &px_height); |
---|
334 | |
---|
335 | if (px_width <= max_width) { |
---|
336 | line[l] = remaining; |
---|
337 | remaining = NULL; |
---|
338 | break; |
---|
339 | } |
---|
340 | else { |
---|
341 | line[l] = ensure_max_string_width (remaining, layout, max_width, &tail); |
---|
342 | g_free (remaining); |
---|
343 | remaining = NULL; |
---|
344 | } |
---|
345 | |
---|
346 | if (tail == NULL) |
---|
347 | break; |
---|
348 | else { |
---|
349 | remaining = tail; |
---|
350 | } |
---|
351 | } |
---|
352 | |
---|
353 | if (remaining != NULL) |
---|
354 | g_free (remaining); |
---|
355 | } |
---|
356 | |
---|
357 | static void |
---|
358 | set_caption_text (GnomeCanvasItem *item, |
---|
359 | char *caption, |
---|
360 | int line, |
---|
361 | EogItemFactory *factory, |
---|
362 | PangoLayout *layout) |
---|
363 | { |
---|
364 | GtkStyle *style; |
---|
365 | IconItem *icon; |
---|
366 | EogCleanMetrics *metrics; |
---|
367 | int caption_w, caption_h; |
---|
368 | int caption_x, caption_y; |
---|
369 | int item_w, item_h; |
---|
370 | |
---|
371 | g_return_if_fail (item != NULL); |
---|
372 | |
---|
373 | if (caption == NULL) return; |
---|
374 | |
---|
375 | icon = g_object_get_data (G_OBJECT (item), "IconItem"); |
---|
376 | g_assert (icon != NULL); |
---|
377 | g_return_if_fail (icon->caption_line[line] == NULL); |
---|
378 | |
---|
379 | metrics = EOG_ITEM_FACTORY_CLEAN (factory)->priv->metrics; |
---|
380 | |
---|
381 | pango_layout_set_text (layout, caption, -1); |
---|
382 | pango_layout_get_pixel_size (layout, &caption_w, &caption_h); |
---|
383 | |
---|
384 | eog_item_factory_get_item_size (EOG_ITEM_FACTORY (factory), &item_w, &item_h); |
---|
385 | |
---|
386 | caption_x = (metrics->twidth - caption_w) / 2; |
---|
387 | caption_y = (metrics->theight + metrics->cspace + metrics->cpadding) |
---|
388 | + line * (caption_h + 4); |
---|
389 | |
---|
390 | style = get_style_from_item (item); |
---|
391 | g_assert (style != NULL); |
---|
392 | |
---|
393 | icon->caption_line[line] = gnome_canvas_item_new (GNOME_CANVAS_GROUP (item), |
---|
394 | GNOME_TYPE_CANVAS_TEXT, |
---|
395 | "text", caption, |
---|
396 | "font_desc", style->font_desc, |
---|
397 | "anchor", GTK_ANCHOR_NW, |
---|
398 | "x", (double) caption_x, |
---|
399 | "y", (double) caption_y, |
---|
400 | "fill_color", style->text[GTK_STATE_NORMAL], |
---|
401 | NULL); |
---|
402 | } |
---|
403 | |
---|
404 | /*********************************************************** |
---|
405 | * |
---|
406 | * Update Functions |
---|
407 | * |
---|
408 | */ |
---|
409 | |
---|
410 | static void |
---|
411 | update_item_image (EogItemFactoryClean *factory, GnomeCanvasItem *item, CImage *cimage) |
---|
412 | { |
---|
413 | EogItemFactoryCleanPrivate *priv; |
---|
414 | EogCleanMetrics *metrics; |
---|
415 | IconItem *icon; |
---|
416 | int image_w, image_h; |
---|
417 | int image_x, image_y; |
---|
418 | GdkPixbuf *thumb = NULL; |
---|
419 | gboolean start_thumb_creation = FALSE; |
---|
420 | |
---|
421 | metrics = factory->priv->metrics; |
---|
422 | priv = factory->priv; |
---|
423 | |
---|
424 | #if FACTORY_DEBUG |
---|
425 | g_message ("update_item_image - id:%i", cimage_get_unique_id (cimage)); |
---|
426 | #endif |
---|
427 | |
---|
428 | icon = g_object_get_data (G_OBJECT (item), "IconItem"); |
---|
429 | g_assert (icon != NULL); |
---|
430 | |
---|
431 | /* obtain thumbnail image */ |
---|
432 | if (cimage_has_thumbnail (cimage)) { |
---|
433 | #if FACTORY_DEBUG |
---|
434 | g_message (" ** have thumbnail - id:%i", cimage_get_unique_id (cimage)); |
---|
435 | #endif |
---|
436 | thumb = cimage_get_thumbnail (cimage); |
---|
437 | } |
---|
438 | else if (priv->dummy != NULL) { /* priv->dummy should be always valid, only in case |
---|
439 | of destroying the factory object this may |
---|
440 | be NULL. */ |
---|
441 | thumb = priv->dummy; |
---|
442 | g_object_ref (thumb); |
---|
443 | |
---|
444 | if (!cimage_has_loading_failed (cimage)) { |
---|
445 | start_thumb_creation = TRUE; |
---|
446 | #if FACTORY_DEBUG |
---|
447 | g_message (" ** start thumbnail creation - id:%i", cimage_get_unique_id (cimage)); |
---|
448 | #endif |
---|
449 | g_object_set_data (G_OBJECT (cimage), "CanvasItem", item); |
---|
450 | } |
---|
451 | } |
---|
452 | |
---|
453 | if (thumb == NULL) { |
---|
454 | /* This may happen if we destroy the factory object. */ |
---|
455 | return; |
---|
456 | } |
---|
457 | |
---|
458 | if (icon->image != NULL) { |
---|
459 | gtk_object_destroy (GTK_OBJECT (icon->image)); |
---|
460 | } |
---|
461 | |
---|
462 | image_w = gdk_pixbuf_get_width (thumb); |
---|
463 | image_h = gdk_pixbuf_get_height (thumb); |
---|
464 | image_x = (metrics->twidth - image_w) / 2; |
---|
465 | image_y = (metrics->theight - image_h) / 2; |
---|
466 | |
---|
467 | icon->image = gnome_canvas_item_new (GNOME_CANVAS_GROUP (item), |
---|
468 | GNOME_TYPE_CANVAS_PIXBUF, |
---|
469 | "pixbuf", thumb, |
---|
470 | "x", (double) image_x, |
---|
471 | "y", (double) image_y, |
---|
472 | "width_set", FALSE, |
---|
473 | "height_set", FALSE, |
---|
474 | NULL); |
---|
475 | |
---|
476 | g_object_unref (thumb); |
---|
477 | |
---|
478 | if (start_thumb_creation) { |
---|
479 | eog_image_loader_start (EOG_IMAGE_LOADER (priv->loader), cimage); |
---|
480 | } |
---|
481 | } |
---|
482 | |
---|
483 | static void |
---|
484 | update_item_caption (EogItemFactoryClean *factory, GnomeCanvasItem *item, CImage *cimage) |
---|
485 | { |
---|
486 | PangoLayout *layout; |
---|
487 | IconItem *icon; |
---|
488 | gchar *caption[MAX_CAPTION_LINES]; |
---|
489 | gchar *full_caption; |
---|
490 | EogCleanMetrics *metrics; |
---|
491 | int i; |
---|
492 | double x1, y1, x2, y2; |
---|
493 | double left, right, top, bottom; |
---|
494 | |
---|
495 | if (!cimage_has_caption (cimage)) return; |
---|
496 | |
---|
497 | /* obtain some objects */ |
---|
498 | metrics = factory->priv->metrics; |
---|
499 | |
---|
500 | icon = g_object_get_data (G_OBJECT (item), "IconItem"); |
---|
501 | g_assert (icon != NULL); |
---|
502 | |
---|
503 | layout = gtk_widget_create_pango_layout (GTK_WIDGET (item->canvas), NULL); |
---|
504 | g_assert (layout != NULL); |
---|
505 | |
---|
506 | /* obtain caption text */ |
---|
507 | full_caption = cimage_get_caption (cimage); |
---|
508 | g_assert (full_caption != NULL); |
---|
509 | |
---|
510 | /* spread caption over two text lines, if neccessary */ |
---|
511 | for (i = 0; i < MAX_CAPTION_LINES; i++) { |
---|
512 | caption[i] = NULL; |
---|
513 | } |
---|
514 | create_item_caption_lines (full_caption, layout, |
---|
515 | metrics->twidth - 2 * metrics->cpadding, |
---|
516 | caption, MAX_CAPTION_LINES); |
---|
517 | |
---|
518 | /* setup canvas text items */ |
---|
519 | for (i = 0; i < MAX_CAPTION_LINES; i++) { |
---|
520 | if (icon->caption_line[i] != NULL) { |
---|
521 | gtk_object_destroy (GTK_OBJECT (icon->caption_line[i])); |
---|
522 | icon->caption_line[i] = NULL; |
---|
523 | } |
---|
524 | |
---|
525 | set_caption_text (item, caption[i], i, EOG_ITEM_FACTORY (factory), layout); |
---|
526 | g_free (caption[i]); |
---|
527 | } |
---|
528 | |
---|
529 | /* setup text selection item */ |
---|
530 | left = top = 10000.0; |
---|
531 | right = bottom = 0; |
---|
532 | |
---|
533 | for (i = 0; i < MAX_CAPTION_LINES; i++) { |
---|
534 | if (icon->caption_line[i] != NULL) { |
---|
535 | gnome_canvas_item_get_bounds (icon->caption_line[i], |
---|
536 | &x1, &y1, &x2, &y2); |
---|
537 | left = MIN (left, x1); |
---|
538 | right = MAX (right, x2); |
---|
539 | top = MIN (top, y1); |
---|
540 | bottom = MAX (bottom, y2); |
---|
541 | } |
---|
542 | } |
---|
543 | |
---|
544 | left = left - metrics->cpadding; |
---|
545 | right = right + metrics->cpadding; |
---|
546 | top = top - metrics->cpadding ; |
---|
547 | bottom = bottom + metrics->cpadding; |
---|
548 | |
---|
549 | if (icon->cap_rect != NULL) |
---|
550 | gtk_object_destroy (GTK_OBJECT (icon->cap_rect)); |
---|
551 | |
---|
552 | icon->cap_rect = gnome_canvas_item_new (GNOME_CANVAS_GROUP (item), |
---|
553 | GNOME_TYPE_CANVAS_RECT, |
---|
554 | "x1", left, |
---|
555 | "y1", top, |
---|
556 | "x2", right, |
---|
557 | "y2", bottom, |
---|
558 | "fill_color", "LightSteelBlue2", |
---|
559 | "outline_color", "Blue", |
---|
560 | "width_pixels", 2, |
---|
561 | "outline_stipple", factory->priv->stipple, |
---|
562 | NULL); |
---|
563 | gnome_canvas_item_hide (GNOME_CANVAS_ITEM (icon->cap_rect)); |
---|
564 | |
---|
565 | for (i = 0; i < MAX_CAPTION_LINES; i++) { |
---|
566 | if (icon->caption_line[i] != NULL) |
---|
567 | gnome_canvas_item_raise_to_top (GNOME_CANVAS_ITEM (icon->caption_line[i])); |
---|
568 | } |
---|
569 | |
---|
570 | /* clean up */ |
---|
571 | g_object_unref (layout); |
---|
572 | g_free (full_caption); |
---|
573 | } |
---|
574 | |
---|
575 | |
---|
576 | static void |
---|
577 | update_item_selection (EogItemFactoryClean *factory, GnomeCanvasItem *item, CImage *cimage) |
---|
578 | { |
---|
579 | IconItem *icon; |
---|
580 | icon = g_object_get_data (G_OBJECT (item), "IconItem"); |
---|
581 | g_assert (icon != NULL); |
---|
582 | |
---|
583 | if (icon->cap_rect == NULL) return; |
---|
584 | |
---|
585 | if (cimage_is_selected (cimage)) { |
---|
586 | gnome_canvas_item_show (GNOME_CANVAS_ITEM (icon->cap_rect)); |
---|
587 | } |
---|
588 | else { |
---|
589 | gnome_canvas_item_hide (GNOME_CANVAS_ITEM (icon->cap_rect)); |
---|
590 | } |
---|
591 | } |
---|
592 | |
---|
593 | /* Configure_item handler for the icon list item factory */ |
---|
594 | static void |
---|
595 | ii_factory_update_item (EogItemFactory *factory, |
---|
596 | EogCollectionModel *model, |
---|
597 | GnomeCanvasItem *item, |
---|
598 | EogItemUpdateHint hint) |
---|
599 | { |
---|
600 | EogItemFactoryClean *ii_factory; |
---|
601 | CImage *cimage = NULL; |
---|
602 | IconItem *icon; |
---|
603 | |
---|
604 | g_return_if_fail (factory != NULL); |
---|
605 | g_return_if_fail (EOG_IS_ITEM_FACTORY_CLEAN (factory)); |
---|
606 | g_return_if_fail (item != NULL); |
---|
607 | g_return_if_fail (GNOME_IS_CANVAS_ITEM (item)); |
---|
608 | g_return_if_fail (model != NULL); |
---|
609 | g_return_if_fail (EOG_IS_COLLECTION_MODEL (model)); |
---|
610 | |
---|
611 | ii_factory = EOG_ITEM_FACTORY_CLEAN (factory); |
---|
612 | |
---|
613 | icon = g_object_get_data (G_OBJECT (item), "IconItem"); |
---|
614 | cimage = eog_collection_model_get_image (model, icon->id); |
---|
615 | g_return_if_fail (cimage != NULL); |
---|
616 | |
---|
617 | if ((hint & EOG_ITEM_UPDATE_IMAGE) == EOG_ITEM_UPDATE_IMAGE) { |
---|
618 | update_item_image (ii_factory, item, cimage); |
---|
619 | } |
---|
620 | |
---|
621 | if ((hint & EOG_ITEM_UPDATE_CAPTION) == EOG_ITEM_UPDATE_CAPTION) { |
---|
622 | update_item_caption (ii_factory, item, cimage); |
---|
623 | } |
---|
624 | |
---|
625 | if ((hint & EOG_ITEM_UPDATE_SELECTION_STATE) == EOG_ITEM_UPDATE_SELECTION_STATE) { |
---|
626 | update_item_selection (ii_factory, item, cimage); |
---|
627 | } |
---|
628 | } |
---|
629 | |
---|
630 | |
---|
631 | /*********************************************************** |
---|
632 | * |
---|
633 | * EogItemFactory API functions |
---|
634 | * |
---|
635 | */ |
---|
636 | |
---|
637 | /* Get_item_size handler for the icon list item factory */ |
---|
638 | static void |
---|
639 | ii_factory_get_item_size (EogItemFactory *factory, |
---|
640 | gint *width, gint *height) |
---|
641 | { |
---|
642 | EogItemFactoryClean *ii_factory; |
---|
643 | EogCleanMetrics *metrics; |
---|
644 | |
---|
645 | g_return_if_fail (factory != NULL); |
---|
646 | g_return_if_fail (EOG_IS_ITEM_FACTORY_CLEAN (factory)); |
---|
647 | |
---|
648 | ii_factory = EOG_ITEM_FACTORY_CLEAN (factory); |
---|
649 | metrics = ii_factory->priv->metrics; |
---|
650 | |
---|
651 | *width = metrics->twidth; |
---|
652 | *height = metrics->theight /* height of the image */ |
---|
653 | + 2 * metrics->cpadding /* padding around the caption */ |
---|
654 | + metrics->cspace /* space between caption and image */ |
---|
655 | + 2 * metrics->font_height /* caption height */ |
---|
656 | + 4; /* padding between caption lines */ |
---|
657 | } |
---|
658 | |
---|
659 | static void |
---|
660 | image_loading_finished_cb (EogImageLoader *loader, CImage *image, EogItemFactoryClean *factory) |
---|
661 | { |
---|
662 | GnomeCanvasItem *item; |
---|
663 | |
---|
664 | #if FACTORY_DEBUG |
---|
665 | g_message ("image_loading_finished - id:%i", cimage_get_unique_id (image)); |
---|
666 | #endif |
---|
667 | |
---|
668 | if (IS_CIMAGE (image)) { |
---|
669 | item = (GnomeCanvasItem*) g_object_get_data (G_OBJECT (image), "CanvasItem"); |
---|
670 | update_item_image (factory, item, image); |
---|
671 | } |
---|
672 | } |
---|
673 | |
---|
674 | static void |
---|
675 | image_loading_failed_cb (EogImageLoader *loader, CImage *image, EogItemFactoryClean *factory) |
---|
676 | { |
---|
677 | GnomeCanvasItem *item; |
---|
678 | |
---|
679 | if (IS_CIMAGE (image)) { |
---|
680 | cimage_set_loading_failed (image); |
---|
681 | item = (GnomeCanvasItem*) g_object_get_data (G_OBJECT (image), "CanvasItem"); |
---|
682 | update_item_image (factory, item, image); |
---|
683 | } |
---|
684 | } |
---|
685 | |
---|
686 | static EogItemFactoryClean* |
---|
687 | eog_item_factory_clean_construct (EogItemFactoryClean *factory, EogImageLoader *loader) |
---|
688 | { |
---|
689 | EogItemFactoryCleanPrivate *priv; |
---|
690 | char *dummy_file; |
---|
691 | |
---|
692 | priv = factory->priv; |
---|
693 | |
---|
694 | /* load dummy pixmap file */ |
---|
695 | dummy_file = gnome_program_locate_file (NULL, GNOME_FILE_DOMAIN_PIXMAP, |
---|
696 | "gnome-eog.png", TRUE, NULL); |
---|
697 | priv->dummy = gdk_pixbuf_new_from_file (dummy_file, NULL); |
---|
698 | g_free (dummy_file); |
---|
699 | |
---|
700 | /* image loader setup */ |
---|
701 | priv->loader = loader; |
---|
702 | if (loader != NULL) { |
---|
703 | g_signal_connect (G_OBJECT (loader), "loading_finished", |
---|
704 | G_CALLBACK (image_loading_finished_cb), factory); |
---|
705 | g_signal_connect (G_OBJECT (loader), "loading_canceled", |
---|
706 | G_CALLBACK (image_loading_failed_cb), factory); |
---|
707 | g_signal_connect (G_OBJECT (loader), "loading_failed", |
---|
708 | G_CALLBACK (image_loading_failed_cb), factory); |
---|
709 | g_object_ref (priv->loader); |
---|
710 | } |
---|
711 | |
---|
712 | return factory; |
---|
713 | } |
---|
714 | |
---|
715 | |
---|
716 | EogItemFactoryClean * |
---|
717 | eog_item_factory_clean_new (EogImageLoader *loader) |
---|
718 | { |
---|
719 | EogItemFactoryClean *factory; |
---|
720 | |
---|
721 | factory = EOG_ITEM_FACTORY_CLEAN (g_object_new (EOG_TYPE_ITEM_FACTORY_CLEAN, NULL)); |
---|
722 | return eog_item_factory_clean_construct (factory, loader); |
---|
723 | } |
---|
724 | |
---|
725 | /** |
---|
726 | * eog_item_factory_clean_set_item_metrics: |
---|
727 | * @factory: An icon list item factory. |
---|
728 | * @item_width: Total width of items, in pixels. |
---|
729 | * @item_height: Total height of items, in pixels. |
---|
730 | * @image_width: Maximum width of images for icons, in pixels. |
---|
731 | * @image_height: Maximum height of images for icons, in pixels. |
---|
732 | * |
---|
733 | * Sets the metrics of the items that will be created in the future by an icon |
---|
734 | * list item factory. This includes the total size of items and the maximum |
---|
735 | * size of icon images. |
---|
736 | **/ |
---|
737 | void |
---|
738 | eog_item_factory_clean_set_metrics (EogItemFactoryClean *factory, |
---|
739 | EogCleanMetrics *metrics) |
---|
740 | { |
---|
741 | EogItemFactoryCleanPrivate *priv; |
---|
742 | |
---|
743 | g_return_if_fail (factory != NULL); |
---|
744 | g_return_if_fail (EOG_IS_ITEM_FACTORY_CLEAN (factory)); |
---|
745 | |
---|
746 | priv = factory->priv; |
---|
747 | |
---|
748 | if (priv->metrics) |
---|
749 | g_free (priv->metrics); |
---|
750 | |
---|
751 | priv->metrics = metrics; |
---|
752 | |
---|
753 | eog_item_factory_configuration_changed (EOG_ITEM_FACTORY (factory)); |
---|
754 | } |
---|
755 | |
---|
756 | EogCleanMetrics* |
---|
757 | eog_item_factory_clean_get_metrics (EogItemFactoryClean *factory) |
---|
758 | { |
---|
759 | g_return_val_if_fail (factory != NULL, NULL); |
---|
760 | g_return_val_if_fail (EOG_IS_ITEM_FACTORY_CLEAN (factory), NULL); |
---|
761 | |
---|
762 | return factory->priv->metrics; |
---|
763 | } |
---|