2011-10-07 [colin] 3.7.10cvs21
[claws.git] / src / gtk / gtkutils.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2011 Hiroyuki Yamamoto and the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <glib/gi18n.h>
26 #include <gdk/gdkkeysyms.h>
27 #include <gdk/gdk.h>
28 #include <gtk/gtk.h>
29 #if !GTK_CHECK_VERSION(3, 0, 0)
30 #include "gtk/gtksctree.h"
31 #endif
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <sys/stat.h>
35
36 #include "combobox.h"
37
38 #if HAVE_LIBCOMPFACE
39 #  include <compface.h>
40 #endif
41
42 #if HAVE_LIBCOMPFACE
43 #define XPM_XFACE_HEIGHT        (HEIGHT + 3)  /* 3 = 1 header + 2 colors */
44 #endif
45
46 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
47 #  include <wchar.h>
48 #  include <wctype.h>
49 #endif
50
51 #include "defs.h"
52 #include "gtkutils.h"
53 #include "utils.h"
54 #if !GTK_CHECK_VERSION(3, 0, 0)
55 #include "gtksctree.h"
56 #endif
57 #include "codeconv.h"
58 #include "stock_pixmap.h"
59 #include "menu.h"
60 #include "prefs_account.h"
61 #include "prefs_common.h"
62 #include "manage_window.h"
63 #include "base64.h"
64 #include "manual.h"
65 #include "combobox.h"
66
67 gboolean gtkut_get_font_size(GtkWidget *widget,
68                              gint *width, gint *height)
69 {
70         PangoLayout *layout;
71         const gchar *str = "Abcdef";
72
73         cm_return_val_if_fail(GTK_IS_WIDGET(widget), FALSE);
74
75         layout = gtk_widget_create_pango_layout(widget, str);
76         cm_return_val_if_fail(layout, FALSE);
77         pango_layout_get_pixel_size(layout, width, height);
78         if (width)
79                 *width = *width / g_utf8_strlen(str, -1);
80         g_object_unref(layout);
81
82         return TRUE;
83 }
84
85 void gtkut_widget_set_small_font_size(GtkWidget *widget)
86 {
87         PangoFontDescription *font_desc;
88         gint size;
89
90         cm_return_if_fail(widget != NULL);
91         cm_return_if_fail(gtk_widget_get_style(widget) != NULL);
92
93         if (prefs_common.derive_from_normal_font || !SMALL_FONT) {
94                 font_desc = pango_font_description_from_string(NORMAL_FONT);
95                 size = pango_font_description_get_size(font_desc);
96                 pango_font_description_set_size(font_desc, size * PANGO_SCALE_SMALL);
97                 gtk_widget_modify_font(widget, font_desc);
98                 pango_font_description_free(font_desc);
99         } else {
100                 font_desc = pango_font_description_from_string(SMALL_FONT);
101                 gtk_widget_modify_font(widget, font_desc);
102                 pango_font_description_free(font_desc);
103         }
104 }
105
106 void gtkut_convert_int_to_gdk_color(gint rgbvalue, GdkColor *color)
107 {
108         cm_return_if_fail(color != NULL);
109
110         color->pixel = 0L;
111         color->red   = (int) (((gdouble)((rgbvalue & 0xff0000) >> 16) / 255.0) * 65535.0);
112         color->green = (int) (((gdouble)((rgbvalue & 0x00ff00) >>  8) / 255.0) * 65535.0);
113         color->blue  = (int) (((gdouble) (rgbvalue & 0x0000ff)        / 255.0) * 65535.0);
114 }
115
116 #define CL(x)   (((gulong) (x) >> (gulong) 8) & 0xFFUL)
117 #define RGB_FROM_GDK_COLOR(c) \
118         ((CL(c->red)   << (gulong) 16) | \
119          (CL(c->green) << (gulong)  8) | \
120          (CL(c->blue)))
121
122 gint gtkut_convert_gdk_color_to_int(GdkColor *color)
123 {
124         return RGB_FROM_GDK_COLOR(color);
125 }
126
127 void gtkut_stock_button_add_help(GtkWidget *bbox, GtkWidget **help_btn)
128 {
129         cm_return_if_fail(bbox != NULL);
130
131         *help_btn = gtk_button_new_from_stock(GTK_STOCK_HELP);
132
133         gtkut_widget_set_can_default(*help_btn, TRUE);
134         gtk_box_pack_end(GTK_BOX (bbox), *help_btn, TRUE, TRUE, 0);
135         gtk_button_box_set_child_secondary(GTK_BUTTON_BOX (bbox),
136                         *help_btn, TRUE);
137         gtk_widget_set_sensitive(*help_btn,
138                         manual_available(MANUAL_MANUAL_CLAWS));
139         gtk_widget_show(*help_btn);
140 }
141
142 void gtkut_stock_button_set_create_with_help(GtkWidget **bbox,
143                 GtkWidget **help_button,
144                 GtkWidget **button1, const gchar *label1,
145                 GtkWidget **button2, const gchar *label2,
146                 GtkWidget **button3, const gchar *label3)
147 {
148         cm_return_if_fail(bbox != NULL);
149         cm_return_if_fail(button1 != NULL);
150
151         gtkut_stock_button_set_create(bbox, button1, label1,
152                         button2, label2, button3, label3);
153
154         gtkut_stock_button_add_help(*bbox, help_button);
155 }
156
157 void gtkut_stock_button_set_create(GtkWidget **bbox,
158                                    GtkWidget **button1, const gchar *label1,
159                                    GtkWidget **button2, const gchar *label2,
160                                    GtkWidget **button3, const gchar *label3)
161 {
162         cm_return_if_fail(bbox != NULL);
163         cm_return_if_fail(button1 != NULL);
164
165         *bbox = gtk_hbutton_box_new();
166         gtk_button_box_set_layout(GTK_BUTTON_BOX(*bbox), GTK_BUTTONBOX_END);
167         gtk_box_set_spacing(GTK_BOX(*bbox), 5);
168
169         *button1 = gtk_button_new_from_stock(label1);
170         gtkut_widget_set_can_default(*button1, TRUE);
171         gtk_box_pack_start(GTK_BOX(*bbox), *button1, TRUE, TRUE, 0);
172         gtk_widget_show(*button1);
173
174         if (button2) {
175                 *button2 = gtk_button_new_from_stock(label2);
176                 gtkut_widget_set_can_default(*button2, TRUE);
177                 gtk_box_pack_start(GTK_BOX(*bbox), *button2, TRUE, TRUE, 0);
178                 gtk_widget_show(*button2);
179         }
180
181         if (button3) {
182                 *button3 = gtk_button_new_from_stock(label3);
183                 gtkut_widget_set_can_default(*button3, TRUE);
184                 gtk_box_pack_start(GTK_BOX(*bbox), *button3, TRUE, TRUE, 0);
185                 gtk_widget_show(*button3);
186         }
187 }
188
189 void gtkut_stock_with_text_button_set_create(GtkWidget **bbox,
190                                    GtkWidget **button1, const gchar *label1, const gchar *text1,
191                                    GtkWidget **button2, const gchar *label2, const gchar *text2,
192                                    GtkWidget **button3, const gchar *label3, const gchar *text3)
193 {
194         cm_return_if_fail(bbox != NULL);
195         cm_return_if_fail(button1 != NULL);
196
197         *bbox = gtk_hbutton_box_new();
198         gtk_button_box_set_layout(GTK_BUTTON_BOX(*bbox), GTK_BUTTONBOX_END);
199         gtk_box_set_spacing(GTK_BOX(*bbox), 5);
200
201         *button1 = gtk_button_new_with_mnemonic(text1);
202         gtk_button_set_image(GTK_BUTTON(*button1),
203                 gtk_image_new_from_stock(label1, GTK_ICON_SIZE_BUTTON));
204         gtkut_widget_set_can_default(*button1, TRUE);
205         gtk_box_pack_start(GTK_BOX(*bbox), *button1, TRUE, TRUE, 0);
206         gtk_widget_show(*button1);
207
208         if (button2) {
209                 *button2 = gtk_button_new_with_mnemonic(text2);
210                 gtk_button_set_image(GTK_BUTTON(*button2),
211                         gtk_image_new_from_stock(label2, GTK_ICON_SIZE_BUTTON));
212                 gtkut_widget_set_can_default(*button2, TRUE);
213                 gtk_box_pack_start(GTK_BOX(*bbox), *button2, TRUE, TRUE, 0);
214                 gtk_widget_show(*button2);
215         }
216
217         if (button3) {
218                 *button3 = gtk_button_new_with_mnemonic(text3);
219                 gtk_button_set_image(GTK_BUTTON(*button3),
220                         gtk_image_new_from_stock(label3, GTK_ICON_SIZE_BUTTON));
221                 gtkut_widget_set_can_default(*button3, TRUE);
222                 gtk_box_pack_start(GTK_BOX(*bbox), *button3, TRUE, TRUE, 0);
223                 gtk_widget_show(*button3);
224         }
225 }
226
227 #define CELL_SPACING 1
228 #define ROW_TOP_YPIXEL(clist, row) (((clist)->row_height * (row)) + \
229                                     (((row) + 1) * CELL_SPACING) + \
230                                     (clist)->voffset)
231 #define ROW_FROM_YPIXEL(clist, y) (((y) - (clist)->voffset) / \
232                                    ((clist)->row_height + CELL_SPACING))
233
234 void gtkut_ctree_node_move_if_on_the_edge(GtkCMCTree *ctree, GtkCMCTreeNode *node, gint _row)
235 {
236         GtkCMCList *clist = GTK_CMCLIST(ctree);
237         gint row;
238         GtkVisibility row_visibility, prev_row_visibility, next_row_visibility;
239
240         cm_return_if_fail(ctree != NULL);
241         cm_return_if_fail(node != NULL);
242
243         row = (_row != -1 ? _row : g_list_position(clist->row_list, (GList *)node));
244
245         if (row < 0 || row >= clist->rows || clist->row_height == 0) return;
246         row_visibility = gtk_cmclist_row_is_visible(clist, row);
247         prev_row_visibility = gtk_cmclist_row_is_visible(clist, row - 1);
248         next_row_visibility = gtk_cmclist_row_is_visible(clist, row + 1);
249
250         if (row_visibility == GTK_VISIBILITY_NONE) {
251                 gtk_cmclist_moveto(clist, row, -1, 0.5, 0);
252                 return;
253         }
254         if (row_visibility == GTK_VISIBILITY_FULL &&
255             prev_row_visibility == GTK_VISIBILITY_FULL &&
256             next_row_visibility == GTK_VISIBILITY_FULL)
257                 return;
258         if (prev_row_visibility != GTK_VISIBILITY_FULL &&
259             next_row_visibility != GTK_VISIBILITY_FULL)
260                 return;
261
262         if (prev_row_visibility != GTK_VISIBILITY_FULL) {
263                 gtk_cmclist_moveto(clist, row, -1, 0.2, 0);
264                 return;
265         }
266         if (next_row_visibility != GTK_VISIBILITY_FULL) {
267                 gtk_cmclist_moveto(clist, row, -1, 0.8, 0);
268                 return;
269         }
270 }
271
272 #undef CELL_SPACING
273 #undef ROW_TOP_YPIXEL
274 #undef ROW_FROM_YPIXEL
275
276 gint gtkut_ctree_get_nth_from_node(GtkCMCTree *ctree, GtkCMCTreeNode *node)
277 {
278         cm_return_val_if_fail(ctree != NULL, -1);
279         cm_return_val_if_fail(node != NULL, -1);
280
281         return g_list_position(GTK_CMCLIST(ctree)->row_list, (GList *)node);
282 }
283
284 /* get the next node, including the invisible one */
285 GtkCMCTreeNode *gtkut_ctree_node_next(GtkCMCTree *ctree, GtkCMCTreeNode *node)
286 {
287         GtkCMCTreeNode *parent;
288
289         if (!node) return NULL;
290
291         if (GTK_CMCTREE_ROW(node)->children)
292                 return GTK_CMCTREE_ROW(node)->children;
293
294         if (GTK_CMCTREE_ROW(node)->sibling)
295                 return GTK_CMCTREE_ROW(node)->sibling;
296
297         for (parent = GTK_CMCTREE_ROW(node)->parent; parent != NULL;
298              parent = GTK_CMCTREE_ROW(parent)->parent) {
299                 if (GTK_CMCTREE_ROW(parent)->sibling)
300                         return GTK_CMCTREE_ROW(parent)->sibling;
301         }
302
303         return NULL;
304 }
305
306 /* get the previous node, including the invisible one */
307 GtkCMCTreeNode *gtkut_ctree_node_prev(GtkCMCTree *ctree, GtkCMCTreeNode *node)
308 {
309         GtkCMCTreeNode *prev;
310         GtkCMCTreeNode *child;
311
312         if (!node) return NULL;
313
314         prev = GTK_CMCTREE_NODE_PREV(node);
315         if (prev == GTK_CMCTREE_ROW(node)->parent)
316                 return prev;
317
318         child = prev;
319         while (GTK_CMCTREE_ROW(child)->children != NULL) {
320                 child = GTK_CMCTREE_ROW(child)->children;
321                 while (GTK_CMCTREE_ROW(child)->sibling != NULL)
322                         child = GTK_CMCTREE_ROW(child)->sibling;
323         }
324
325         return child;
326 }
327
328 gboolean gtkut_ctree_node_is_selected(GtkCMCTree *ctree, GtkCMCTreeNode *node)
329 {
330         GtkCMCList *clist = GTK_CMCLIST(ctree);
331         GList *cur;
332
333         for (cur = clist->selection; cur != NULL; cur = cur->next) {
334                 if (node == GTK_CMCTREE_NODE(cur->data))
335                         return TRUE;
336         }
337
338         return FALSE;
339 }
340
341 GtkCMCTreeNode *gtkut_ctree_find_collapsed_parent(GtkCMCTree *ctree,
342                                                 GtkCMCTreeNode *node)
343 {
344         if (!node) return NULL;
345
346         while ((node = GTK_CMCTREE_ROW(node)->parent) != NULL) {
347                 if (!GTK_CMCTREE_ROW(node)->expanded)
348                         return node;
349         }
350
351         return NULL;
352 }
353
354 void gtkut_ctree_expand_parent_all(GtkCMCTree *ctree, GtkCMCTreeNode *node)
355 {
356         while ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
357                 gtk_cmctree_expand(ctree, node);
358 }
359
360 gboolean gtkut_ctree_node_is_parent(GtkCMCTreeNode *parent, GtkCMCTreeNode *node)
361 {
362         GtkCMCTreeNode *tmp;
363         cm_return_val_if_fail(node != NULL, FALSE);
364         cm_return_val_if_fail(parent != NULL, FALSE);
365         tmp = node;
366         
367         while (tmp) {
368                 if(GTK_CMCTREE_ROW(tmp)->parent && GTK_CMCTREE_ROW(tmp)->parent == parent)
369                         return TRUE;
370                 tmp = GTK_CMCTREE_ROW(tmp)->parent;
371         }
372         
373         return FALSE;
374 }
375
376 void gtkut_ctree_set_focus_row(GtkCMCTree *ctree, GtkCMCTreeNode *node)
377 {
378         if (node == NULL)
379                 return;
380         gtkut_clist_set_focus_row(GTK_CMCLIST(ctree),
381                                   gtkut_ctree_get_nth_from_node(ctree, node));
382 }
383
384 void gtkut_clist_set_focus_row(GtkCMCList *clist, gint row)
385 {
386         clist->focus_row = row;
387         GTKUT_CTREE_REFRESH(clist);
388 }
389
390 void gtkut_container_remove(GtkContainer *container, GtkWidget *widget)
391 {
392         gtk_container_remove(container, widget);
393 }
394
395 static gboolean gtkut_text_buffer_match_string(GtkTextBuffer *textbuf,
396                                         const GtkTextIter *iter,
397                                         gunichar *wcs, gint len,
398                                         gboolean case_sens)
399 {
400         GtkTextIter start_iter, end_iter;
401         gchar *utf8str, *p;
402         gint match_count;
403
404         start_iter = end_iter = *iter;
405         gtk_text_iter_forward_chars(&end_iter, len);
406
407         utf8str = gtk_text_buffer_get_text(textbuf, &start_iter, &end_iter,
408                                            FALSE);
409         if (!utf8str) return FALSE;
410
411         if ((gint)g_utf8_strlen(utf8str, -1) != len) {
412                 g_free(utf8str);
413                 return FALSE;
414         }
415
416         for (p = utf8str, match_count = 0;
417              *p != '\0' && match_count < len;
418              p = g_utf8_next_char(p), match_count++) {
419                 gunichar wc;
420
421                 wc = g_utf8_get_char(p);
422
423                 if (case_sens) {
424                         if (wc != wcs[match_count])
425                                 break;
426                 } else {
427                         if (g_unichar_tolower(wc) !=
428                             g_unichar_tolower(wcs[match_count]))
429                                 break;
430                 }
431         }
432
433         g_free(utf8str);
434
435         if (match_count == len)
436                 return TRUE;
437         else
438                 return FALSE;
439 }
440
441 static gboolean gtkut_text_buffer_find(GtkTextBuffer *buffer, const GtkTextIter *iter,
442                                 const gchar *str, gboolean case_sens,
443                                 GtkTextIter *match_pos)
444 {
445         gunichar *wcs;
446         gint len;
447         glong items_read = 0, items_written = 0;
448         GError *error = NULL;
449         GtkTextIter iter_;
450         gboolean found = FALSE;
451
452         wcs = g_utf8_to_ucs4(str, -1, &items_read, &items_written, &error);
453         if (error != NULL) {
454                 g_warning("An error occured while converting a string from UTF-8 to UCS-4: %s\n",
455                           error->message);
456                 g_error_free(error);
457         }
458         if (!wcs || items_written <= 0) return FALSE;
459         len = (gint)items_written;
460
461         iter_ = *iter;
462         do {
463                 found = gtkut_text_buffer_match_string
464                         (buffer, &iter_, wcs, len, case_sens);
465                 if (found) {
466                         *match_pos = iter_;
467                         break;
468                 }
469         } while (gtk_text_iter_forward_char(&iter_));
470
471         g_free(wcs);
472
473         return found;
474 }
475
476 static gboolean gtkut_text_buffer_find_backward(GtkTextBuffer *buffer,
477                                          const GtkTextIter *iter,
478                                          const gchar *str, gboolean case_sens,
479                                          GtkTextIter *match_pos)
480 {
481         gunichar *wcs;
482         gint len;
483         glong items_read = 0, items_written = 0;
484         GError *error = NULL;
485         GtkTextIter iter_;
486         gboolean found = FALSE;
487
488         wcs = g_utf8_to_ucs4(str, -1, &items_read, &items_written, &error);
489         if (error != NULL) {
490                 g_warning("An error occured while converting a string from UTF-8 to UCS-4: %s\n", error->message);
491                 g_error_free(error);
492         }
493         if (!wcs || items_written <= 0) return FALSE;
494         len = (gint)items_written;
495
496         iter_ = *iter;
497         while (gtk_text_iter_backward_char(&iter_)) {
498                 found = gtkut_text_buffer_match_string
499                         (buffer, &iter_, wcs, len, case_sens);
500                 if (found) {
501                         *match_pos = iter_;
502                         break;
503                 }
504         }
505
506         g_free(wcs);
507
508         return found;
509 }
510
511 gchar *gtkut_text_view_get_selection(GtkTextView *textview)
512 {
513         GtkTextBuffer *buffer;
514         GtkTextIter start_iter, end_iter;
515         gboolean found;
516
517         cm_return_val_if_fail(GTK_IS_TEXT_VIEW(textview), NULL);
518
519         buffer = gtk_text_view_get_buffer(textview);
520         found = gtk_text_buffer_get_selection_bounds(buffer,
521                                                      &start_iter,
522                                                      &end_iter);
523         if (found)
524                 return gtk_text_buffer_get_text(buffer, &start_iter, &end_iter,
525                                                 FALSE);
526         else
527                 return NULL;
528 }
529
530
531 void gtkut_text_view_set_position(GtkTextView *text, gint pos)
532 {
533         GtkTextBuffer *buffer;
534         GtkTextIter iter;
535
536         cm_return_if_fail(text != NULL);
537
538         buffer = gtk_text_view_get_buffer(text);
539
540         gtk_text_buffer_get_iter_at_offset(buffer, &iter, pos);
541         gtk_text_buffer_place_cursor(buffer, &iter);
542         gtk_text_view_scroll_to_iter(text, &iter, 0.0, FALSE, 0.0, 0.0);
543 }
544
545 gboolean gtkut_text_view_search_string(GtkTextView *text, const gchar *str,
546                                         gboolean case_sens)
547 {
548         GtkTextBuffer *buffer;
549         GtkTextIter iter, match_pos;
550         GtkTextMark *mark;
551         gint len;
552
553         cm_return_val_if_fail(text != NULL, FALSE);
554         cm_return_val_if_fail(str != NULL, FALSE);
555
556         buffer = gtk_text_view_get_buffer(text);
557
558         len = g_utf8_strlen(str, -1);
559         cm_return_val_if_fail(len >= 0, FALSE);
560
561         mark = gtk_text_buffer_get_insert(buffer);
562         gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
563
564         if (gtkut_text_buffer_find(buffer, &iter, str, case_sens,
565                                    &match_pos)) {
566                 GtkTextIter end = match_pos;
567
568                 gtk_text_iter_forward_chars(&end, len);
569                 /* place "insert" at the last character */
570                 gtk_text_buffer_select_range(buffer, &end, &match_pos);
571                 gtk_text_view_scroll_to_mark(text, mark, 0.0, TRUE, 0.0, 0.5);
572                 return TRUE;
573         }
574
575         return FALSE;
576 }
577
578 gboolean gtkut_text_view_search_string_backward(GtkTextView *text, const gchar *str,
579                                         gboolean case_sens)
580 {
581         GtkTextBuffer *buffer;
582         GtkTextIter iter, match_pos;
583         GtkTextMark *mark;
584         gint len;
585
586         cm_return_val_if_fail(text != NULL, FALSE);
587         cm_return_val_if_fail(str != NULL, FALSE);
588
589         buffer = gtk_text_view_get_buffer(text);
590
591         len = g_utf8_strlen(str, -1);
592         cm_return_val_if_fail(len >= 0, FALSE);
593
594         mark = gtk_text_buffer_get_insert(buffer);
595         gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
596
597         if (gtkut_text_buffer_find_backward(buffer, &iter, str, case_sens,
598                                             &match_pos)) {
599                 GtkTextIter end = match_pos;
600
601                 gtk_text_iter_forward_chars(&end, len);
602                 gtk_text_buffer_select_range(buffer, &match_pos, &end);
603                 gtk_text_view_scroll_to_mark(text, mark, 0.0, TRUE, 0.0, 0.5);
604                 return TRUE;
605         }
606
607         return FALSE;
608 }
609
610 void gtkut_window_popup(GtkWidget *window)
611 {
612         GdkWindow *gdkwin;
613         gint x, y, sx, sy, new_x, new_y;
614
615         gdkwin = gtk_widget_get_window(window);
616
617         cm_return_if_fail(window != NULL);
618         cm_return_if_fail(gdkwin != NULL);
619
620         sx = gdk_screen_width();
621         sy = gdk_screen_height();
622
623         gdk_window_get_origin(gdkwin, &x, &y);
624         new_x = x % sx; if (new_x < 0) new_x = 0;
625         new_y = y % sy; if (new_y < 0) new_y = 0;
626         if (new_x != x || new_y != y)
627                 gdk_window_move(gdkwin, new_x, new_y);
628
629         gtk_window_set_skip_taskbar_hint(GTK_WINDOW(window), FALSE);
630         gtk_window_present_with_time(GTK_WINDOW(window), time(NULL));
631 }
632
633 void gtkut_widget_get_uposition(GtkWidget *widget, gint *px, gint *py)
634 {
635         GdkWindow *gdkwin;
636         gint x, y;
637         gint sx, sy;
638
639         gdkwin = gtk_widget_get_window(widget);
640
641         cm_return_if_fail(widget != NULL);
642         cm_return_if_fail(gdkwin != NULL);
643
644         sx = gdk_screen_width();
645         sy = gdk_screen_height();
646
647         /* gdk_window_get_root_origin ever return *rootwindow*'s position */
648         gdk_window_get_root_origin(gdkwin, &x, &y);
649
650         x %= sx; if (x < 0) x = 0;
651         y %= sy; if (y < 0) y = 0;
652         *px = x;
653         *py = y;
654 }
655
656 void gtkut_widget_draw_now(GtkWidget *widget)
657 {
658         if (widget && gtkut_widget_get_visible(widget) && gtkut_widget_is_drawable(widget))
659                 gdk_window_process_updates(gtk_widget_get_window(widget), FALSE);
660 }
661
662 static void gtkut_clist_bindings_add(GtkWidget *clist)
663 {
664         GtkBindingSet *binding_set;
665
666         binding_set = gtk_binding_set_by_class
667                 (GTK_CMCLIST_GET_CLASS(clist));
668
669         gtk_binding_entry_add_signal(binding_set, GDK_KEY_n, GDK_CONTROL_MASK,
670                                      "scroll_vertical", 2,
671                                      G_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
672                                      G_TYPE_FLOAT, 0.0);
673         gtk_binding_entry_add_signal(binding_set, GDK_p, GDK_CONTROL_MASK,
674                                      "scroll_vertical", 2,
675                                      G_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
676                                      G_TYPE_FLOAT, 0.0);
677 }
678
679 void gtkut_widget_init(void)
680 {
681         GtkWidget *clist;
682
683         clist = gtk_cmclist_new(1);
684         g_object_ref(G_OBJECT(clist));
685 #if GLIB_CHECK_VERSION(2,10,0)
686         g_object_ref_sink (G_OBJECT(clist));
687 #else
688         gtk_object_ref (G_OBJECT(clist));
689         gtk_object_sink (G_OBJECT(clist));
690 #endif
691         gtkut_clist_bindings_add(clist);
692         g_object_unref(G_OBJECT(clist));
693
694         clist = gtk_cmctree_new(1, 0);
695         g_object_ref(G_OBJECT(clist));
696 #if GLIB_CHECK_VERSION(2,10,0)
697         g_object_ref_sink (G_OBJECT(clist));
698 #else
699         gtk_object_ref (G_OBJECT(clist));
700         gtk_object_sink (G_OBJECT(clist));
701 #endif
702         gtkut_clist_bindings_add(clist);
703         g_object_unref(G_OBJECT(clist));
704
705         clist = gtk_sctree_new_with_titles(1, 0, NULL);
706         g_object_ref(G_OBJECT(clist));
707 #if GLIB_CHECK_VERSION(2,10,0)
708         g_object_ref_sink (G_OBJECT(clist));
709 #else
710         gtk_object_ref (G_OBJECT(clist));
711         gtk_object_sink (G_OBJECT(clist));
712 #endif
713         gtkut_clist_bindings_add(clist);
714         g_object_unref(G_OBJECT(clist));
715 }
716
717 void gtkut_widget_set_app_icon(GtkWidget *widget)
718 {
719         static GdkPixbuf *icon = NULL;
720         
721         cm_return_if_fail(widget != NULL);
722         cm_return_if_fail(gtk_widget_get_window(widget) != NULL);
723         if (!icon) {
724                 stock_pixbuf_gdk(widget, STOCK_PIXMAP_CLAWS_MAIL_ICON, &icon);
725         }               
726         if (icon)
727                 gtk_window_set_icon(GTK_WINDOW(widget), icon);
728 }
729
730 void gtkut_widget_set_composer_icon(GtkWidget *widget)
731 {
732         static GdkPixbuf *icon = NULL;
733         
734         cm_return_if_fail(widget != NULL);
735         cm_return_if_fail(gtk_widget_get_window(widget) != NULL);
736         if (!icon) {
737                 stock_pixbuf_gdk(widget, STOCK_PIXMAP_MAIL_COMPOSE, &icon);
738         }               
739         if (icon)
740                 gtk_window_set_icon(GTK_WINDOW(widget), icon);
741 }
742
743 static gboolean move_bar = FALSE;
744 static gint move_bar_id = -1;
745
746 static gboolean move_bar_cb(gpointer data)
747 {
748         GtkWidget *w = (GtkWidget *)data;
749         if (!move_bar)
750                 return FALSE;
751
752         if (!GTK_IS_PROGRESS_BAR(w)) {
753                 return FALSE;
754         }
755
756         gtk_progress_bar_pulse(GTK_PROGRESS_BAR(w));
757         GTK_EVENTS_FLUSH();
758         return TRUE;
759 }
760
761 GtkWidget *label_window_create(const gchar *str)
762 {
763         GtkWidget *window;
764         GtkWidget *label, *vbox, *hbox;
765         GtkWidget *wait_progress = gtk_progress_bar_new();
766
767         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "gtkutils");
768         gtk_widget_set_size_request(window, 380, 70);
769         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
770         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
771         gtk_window_set_title(GTK_WINDOW(window), str);
772         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
773         gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
774         manage_window_set_transient(GTK_WINDOW(window));
775
776         label = gtk_label_new(str);
777         
778         vbox = gtk_vbox_new(FALSE, 6);
779         hbox = gtk_hbox_new(FALSE, 6);
780         gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, FALSE, 0);
781         gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, FALSE, 0);
782         hbox = gtk_hbox_new(FALSE, 6);
783         gtk_box_pack_start(GTK_BOX(hbox), wait_progress, TRUE, FALSE, 0);
784         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
785         
786         gtk_container_add(GTK_CONTAINER(window), vbox);
787         gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
788         gtk_misc_set_alignment(GTK_MISC(label), 0.5, 0.5);
789         gtk_widget_show_all(vbox);
790
791         gtk_widget_show_now(window);
792         
793         if (move_bar_id == -1) {
794                 move_bar_id = g_timeout_add(200, move_bar_cb, wait_progress);
795                 move_bar = TRUE;
796         }
797
798         GTK_EVENTS_FLUSH();
799
800         return window;
801 }
802
803 void label_window_destroy(GtkWidget *window)
804 {
805         move_bar = FALSE;
806         g_source_remove(move_bar_id);
807         move_bar_id = -1;
808         GTK_EVENTS_FLUSH();
809         gtk_widget_destroy(window);     
810 }
811
812 GtkWidget *gtkut_account_menu_new(GList                 *ac_list,
813                                         GCallback               callback,
814                                   gpointer              data)
815 {
816         GList *cur_ac;
817         GtkWidget *optmenu;
818         GtkListStore *menu;
819         GtkTreeIter iter;
820         PrefsAccount *account;
821         gchar *name;
822         
823         cm_return_val_if_fail(ac_list != NULL, NULL);
824
825         optmenu = gtkut_sc_combobox_create(NULL, FALSE);
826         menu = GTK_LIST_STORE(gtk_combo_box_get_model(GTK_COMBO_BOX(optmenu)));
827
828         for (cur_ac = ac_list; cur_ac != NULL; cur_ac = cur_ac->next) {
829                 account = (PrefsAccount *) cur_ac->data;
830                 if (account->name)
831                         name = g_strdup_printf("%s: %s <%s>",
832                                                account->account_name,
833                                                account->name,
834                                                account->address);
835                 else
836                         name = g_strdup_printf("%s: %s",
837                                                account->account_name,
838                                                account->address);
839                 COMBOBOX_ADD_ESCAPED(menu, name, account->account_id);
840                 g_free(name);
841         }
842         gtk_combo_box_set_active(GTK_COMBO_BOX(optmenu), 0);
843
844         if( callback != NULL )
845                 g_signal_connect(G_OBJECT(optmenu), "changed", callback, data);
846
847         return optmenu;
848 }
849
850 void gtkut_set_widget_bgcolor_rgb(GtkWidget *widget, guint rgbvalue)
851 {
852         GtkStyle *newstyle;
853         GdkColor gdk_color;
854
855         gtkut_convert_int_to_gdk_color(rgbvalue, &gdk_color);
856         newstyle = gtk_style_copy(gtk_widget_get_default_style());
857         newstyle->bg[GTK_STATE_NORMAL]   = gdk_color;
858         newstyle->bg[GTK_STATE_PRELIGHT] = gdk_color;
859         newstyle->bg[GTK_STATE_ACTIVE]   = gdk_color;
860         gtk_widget_set_style(widget, newstyle);
861 }
862   
863 /*!
864  *\brief        Tries to find a focused child using a lame strategy
865  */
866 GtkWidget *gtkut_get_focused_child(GtkContainer *parent)
867 {
868         GtkWidget *result = NULL;
869         GList *child_list = NULL;
870         GList *c;
871
872         cm_return_val_if_fail(parent, NULL);
873
874         /* Get children list and see which has the focus. */
875         child_list = gtk_container_get_children(parent);
876         if (!child_list)
877                 return NULL;
878
879         for (c = child_list; c != NULL; c = g_list_next(c)) {
880                 if (c->data && GTK_IS_WIDGET(c->data)) {
881                         if (gtkut_widget_has_focus(GTK_WIDGET(c->data))) {
882                                 result = GTK_WIDGET(c->data);
883                                 break;
884                         }
885                 }
886         }
887         
888         /* See if the returned widget is a container itself; if it is,
889          * see if one of its children is focused. If the focused 
890          * container has no focused child, it is itself a focusable 
891          * child, and has focus. */
892         if (result && GTK_IS_CONTAINER(result)) {
893                 GtkWidget *tmp =  gtkut_get_focused_child(GTK_CONTAINER(result)); 
894                 
895                 if (tmp) 
896                         result = tmp;
897         } else {
898                 /* Try the same for each container in the chain */
899                 for (c = child_list; c != NULL && !result; c = g_list_next(c)) {
900                         if (c->data && GTK_IS_WIDGET(c->data) 
901                         &&  GTK_IS_CONTAINER(c->data)) {
902                                 result = gtkut_get_focused_child
903                                         (GTK_CONTAINER(c->data));
904                         }
905                 }
906         
907         }
908         
909         g_list_free(child_list);
910                 
911         return result;
912 }
913
914 /*!
915  *\brief        Create a Browse (file) button based on GTK+ stock
916  */
917 GtkWidget *gtkut_get_browse_file_btn(const gchar *button_label)
918 {
919         GtkWidget *button;
920
921         button = gtk_button_new_with_mnemonic(button_label);
922         gtk_button_set_image(GTK_BUTTON(button),
923                 gtk_image_new_from_stock(GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_BUTTON));
924
925         return button;
926 }
927
928 /*!
929  *\brief        Create a Browse (directory) button based on GTK+ stock
930  */
931 GtkWidget *gtkut_get_browse_directory_btn(const gchar *button_label)
932 {
933         GtkWidget *button;
934
935         button = gtk_button_new_with_mnemonic(button_label);
936         gtk_button_set_image(GTK_BUTTON(button),
937                 gtk_image_new_from_stock(GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_BUTTON));
938
939         return button;
940 }
941
942 GtkWidget *gtkut_get_replace_btn(const gchar *button_label)
943 {
944         GtkWidget *button;
945
946         button = gtk_button_new_with_mnemonic(button_label);
947         gtk_button_set_image(GTK_BUTTON(button),
948                 gtk_image_new_from_stock(GTK_STOCK_REFRESH, GTK_ICON_SIZE_BUTTON));
949
950         return button;
951 }
952
953 /**
954  * merge some part of code into one function : it creates a frame and add
955  *      these into gtk box widget passed in param.
956  * \param box gtk box where adding new created frame.
957  * \param pframe pointer with which to assign the frame. If NULL, no pointer
958  *      is assigned but the frame is anyway created and added to @box.
959  * \param frame_label frame label of new created frame.
960  */
961 GtkWidget *gtkut_get_options_frame(GtkWidget *box, GtkWidget **pframe,
962                 const gchar *frame_label)
963 {
964         GtkWidget *vbox;
965         GtkWidget *frame;
966
967         frame = gtk_frame_new(frame_label);
968         gtk_widget_show(frame);
969         gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
970         gtk_frame_set_label_align(GTK_FRAME(frame), 0.01, 0.5);
971
972         vbox = gtk_vbox_new (FALSE, 4);
973         gtk_widget_show(vbox);
974         gtk_container_add(GTK_CONTAINER (frame), vbox);
975         gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
976
977         if (pframe != NULL)
978                 *pframe = frame;
979
980         return vbox;
981 }
982
983 #if HAVE_LIBCOMPFACE
984 static gint create_xpm_from_xface(gchar *xpm[], const gchar *xface)
985 {
986         static gchar *bit_pattern[] = {
987                 "....",
988                 "...#",
989                 "..#.",
990                 "..##",
991                 ".#..",
992                 ".#.#",
993                 ".##.",
994                 ".###",
995                 "#...",
996                 "#..#",
997                 "#.#.",
998                 "#.##",
999                 "##..",
1000                 "##.#",
1001                 "###.",
1002                 "####"
1003         };
1004
1005         static gchar *xface_header = "48 48 2 1";
1006         static gchar *xface_black  = "# c #000000";
1007         static gchar *xface_white  = ". c #ffffff";
1008
1009         gint i, line = 0;
1010         const guchar *p;
1011         gchar buf[WIDTH * 4 + 1];  /* 4 = strlen("0x0000") */
1012
1013         p = xface;
1014
1015         strcpy(xpm[line++], xface_header);
1016         strcpy(xpm[line++], xface_black);
1017         strcpy(xpm[line++], xface_white);
1018
1019         for (i = 0; i < HEIGHT; i++) {
1020                 gint col;
1021
1022                 buf[0] = '\0';
1023      
1024                 for (col = 0; col < 3; col++) {
1025                         gint figure;
1026
1027                         p += 2;  /* skip '0x' */
1028
1029                         for (figure = 0; figure < 4; figure++) {
1030                                 gint n = 0;
1031
1032                                 if ('0' <= *p && *p <= '9') {
1033                                         n = *p - '0';
1034                                 } else if ('a' <= *p && *p <= 'f') {
1035                                         n = *p - 'a' + 10;
1036                                 } else if ('A' <= *p && *p <= 'F') {
1037                                         n = *p - 'A' + 10;
1038                                 }
1039
1040                                 strcat(buf, bit_pattern[n]);
1041                                 p++;  /* skip ',' */
1042                         }
1043
1044                         p++;  /* skip '\n' */
1045                 }
1046
1047                 strcpy(xpm[line++], buf);
1048                 p++;
1049         }
1050
1051         return 0;
1052 }
1053 #endif
1054
1055 gboolean get_tag_range(GtkTextIter *iter,
1056                                        GtkTextTag *tag,
1057                                        GtkTextIter *start_iter,
1058                                        GtkTextIter *end_iter)
1059 {
1060         GtkTextIter _start_iter, _end_iter;
1061
1062         _end_iter = *iter;
1063         if (!gtk_text_iter_forward_to_tag_toggle(&_end_iter, tag)) {
1064                 debug_print("Can't find end");
1065                 return FALSE;
1066         }
1067
1068         _start_iter = _end_iter;
1069         if (!gtk_text_iter_backward_to_tag_toggle(&_start_iter, tag)) {
1070                 debug_print("Can't find start.");
1071                 return FALSE;
1072         }
1073
1074         *start_iter = _start_iter;
1075         *end_iter = _end_iter;
1076
1077         return TRUE;
1078 }
1079
1080 #if HAVE_LIBCOMPFACE
1081 GtkWidget *xface_get_from_header(const gchar *o_xface, GdkColor *background,
1082                                  GdkWindow *window)
1083 {
1084         static gchar *xpm_xface[XPM_XFACE_HEIGHT];
1085         static gboolean xpm_xface_init = TRUE;
1086         gchar xface[2048];
1087         strncpy(xface, o_xface, sizeof(xface));
1088
1089         if (!window) {
1090                 g_warning("no window\n");
1091                 return NULL;
1092         }
1093         if (uncompface(xface) < 0) {
1094                 g_warning("uncompface failed\n");
1095                 return NULL;
1096         }
1097
1098         if (xpm_xface_init) {
1099                 gint i;
1100
1101                 for (i = 0; i < XPM_XFACE_HEIGHT; i++) {
1102                         xpm_xface[i] = g_malloc(WIDTH + 1);
1103                         *xpm_xface[i] = '\0';
1104                 }
1105                 xpm_xface_init = FALSE;
1106         }
1107
1108         create_xpm_from_xface(xpm_xface, xface);
1109
1110         return gtk_image_new_from_pixbuf(
1111                 gdk_pixbuf_new_from_xpm_data((const char **)xpm_xface));
1112 }
1113 #endif
1114
1115 GtkWidget *face_get_from_header(const gchar *o_face)
1116 {
1117         gchar face[2048];
1118         gchar face_png[2048];
1119         gint pngsize;
1120         GdkPixbuf *pixbuf;
1121         GError *error = NULL;
1122         GdkPixbufLoader *loader = gdk_pixbuf_loader_new ();
1123         GtkWidget *image;
1124         
1125         if (o_face == NULL || strlen(o_face) == 0)
1126                 return NULL;
1127
1128         strncpy2(face, o_face, sizeof(face));
1129
1130         unfold_line(face); /* strip all whitespace and linebreaks */
1131         remove_space(face);
1132
1133         pngsize = base64_decode(face_png, face, strlen(face));
1134
1135         if (!gdk_pixbuf_loader_write (loader, face_png, pngsize, &error) ||
1136             !gdk_pixbuf_loader_close (loader, &error)) {
1137                 g_warning("loading face failed\n");
1138                 g_object_unref(loader);
1139                 return NULL;
1140         }
1141
1142         pixbuf = g_object_ref(gdk_pixbuf_loader_get_pixbuf(loader));
1143
1144         g_object_unref(loader);
1145
1146         if ((gdk_pixbuf_get_width(pixbuf) != 48) || (gdk_pixbuf_get_height(pixbuf) != 48)) {
1147                 g_object_unref(pixbuf);
1148                 g_warning("wrong_size");
1149                 return NULL;
1150         }
1151
1152         image = gtk_image_new_from_pixbuf(pixbuf);
1153         g_object_unref(pixbuf);
1154         return image;
1155 }
1156
1157 static GdkCursor *hand_cursor = NULL;
1158
1159 static void link_btn_enter(GtkButton *button, gpointer data)
1160 {
1161         GdkWindow *gdkwin;
1162         GtkWidget *window = (GtkWidget *)data;
1163
1164         gdkwin = gtk_widget_get_window(window);
1165
1166         if (!hand_cursor)
1167                 hand_cursor = gdk_cursor_new(GDK_HAND2);
1168         if (window && gdkwin)
1169                 gdk_window_set_cursor(gdkwin, hand_cursor);
1170
1171         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1172         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1173         
1174 }
1175
1176 static void link_btn_leave(GtkButton *button, gpointer data)
1177 {
1178         GdkWindow *gdkwin;
1179         GtkWidget *window = (GtkWidget *)data;
1180
1181         gdkwin = gtk_widget_get_window(window);
1182
1183         if (window && gdkwin)
1184                 gdk_window_set_cursor(gdkwin, NULL);
1185
1186         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1187         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1188 }
1189
1190 static void link_btn_pressed(GtkButton *button, gpointer data)
1191 {
1192         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1193         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1194 }
1195
1196 static void link_btn_released(GtkButton *button, gpointer data)
1197 {
1198         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1199         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1200 }
1201
1202 static void link_btn_clicked(GtkButton *button, gpointer data)
1203 {
1204         gchar *url = (gchar *)data;
1205         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1206         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1207         open_uri(url, prefs_common_get_uri_cmd());
1208 }
1209
1210 static void link_btn_unrealize(GtkButton *button, gpointer data)
1211 {
1212         gchar *url = (gchar *)data;
1213         g_signal_handlers_disconnect_by_func(G_OBJECT(button), 
1214                          G_CALLBACK(link_btn_clicked), url);
1215         g_free(url);
1216 }
1217
1218 GtkWidget *gtkut_get_link_btn(GtkWidget *window, const gchar *url, const gchar *label)
1219 {
1220         GtkWidget *btn;
1221         GtkWidget *btn_label;
1222 #if !GTK_CHECK_VERSION(3, 0, 0)
1223         GdkColormap *cmap;
1224         gboolean success[2];
1225 #endif
1226         GdkColor uri_color[2] = {{0, 0, 0, 0xffff}, {0, 0xffff, 0, 0}};
1227         gchar *local_url = NULL;
1228         if (!url)
1229                 return NULL;
1230
1231         gtkut_convert_int_to_gdk_color(prefs_common.uri_col,
1232                                                &uri_color[0]);
1233         gtkut_convert_int_to_gdk_color(prefs_common.uri_col,
1234                                                &uri_color[1]);
1235
1236         btn = gtk_button_new_with_label(label?label:url);
1237         gtk_button_set_relief(GTK_BUTTON(btn), GTK_RELIEF_NONE);
1238         btn_label = gtk_bin_get_child(GTK_BIN((btn)));
1239 #if !GTK_CHECK_VERSION(3, 0, 0)
1240         cmap = gdk_drawable_get_colormap(gtk_widget_get_window(window));
1241         gdk_colormap_alloc_colors(cmap, uri_color, 2, FALSE, TRUE, success);
1242         if (success[0] == TRUE && success[1] == TRUE) {
1243 #endif
1244                 GtkStyle *style;
1245                 gtk_widget_ensure_style(btn_label);
1246                 style = gtk_style_copy
1247                         (gtk_widget_get_style(btn_label));
1248                 style->fg[GTK_STATE_NORMAL]   = uri_color[0];
1249                 style->fg[GTK_STATE_ACTIVE]   = uri_color[1];
1250                 style->fg[GTK_STATE_PRELIGHT] = uri_color[0];
1251                 gtk_widget_set_style(btn_label, style);
1252 #if !GTK_CHECK_VERSION(3, 0, 0)
1253         } else
1254                 g_warning("about_create(): color allocation failed.\n");
1255 #endif
1256
1257         g_signal_connect(G_OBJECT(btn), "enter",
1258                          G_CALLBACK(link_btn_enter), window);
1259         g_signal_connect(G_OBJECT(btn), "leave",
1260                          G_CALLBACK(link_btn_leave), window);
1261         g_signal_connect(G_OBJECT(btn), "pressed",
1262                          G_CALLBACK(link_btn_pressed), window);
1263         g_signal_connect(G_OBJECT(btn), "released",
1264                          G_CALLBACK(link_btn_released), window);
1265                          
1266         local_url = g_strdup(url);
1267         g_signal_connect(G_OBJECT(btn), "clicked",
1268                          G_CALLBACK(link_btn_clicked), local_url);
1269         g_signal_connect(G_OBJECT(btn), "unrealize",
1270                          G_CALLBACK(link_btn_unrealize), local_url);
1271         return btn;
1272 }
1273
1274 static gboolean _combobox_separator_func(GtkTreeModel *model,
1275                 GtkTreeIter *iter, gpointer data)
1276 {
1277         gchar *txt = NULL;
1278
1279         cm_return_val_if_fail(model != NULL, FALSE);
1280
1281         gtk_tree_model_get(model, iter, COMBOBOX_TEXT, &txt, -1);
1282
1283         if( txt == NULL )
1284                 return TRUE;
1285         return FALSE;
1286 }
1287
1288 GtkWidget *gtkut_sc_combobox_create(GtkWidget *eventbox, gboolean focus_on_click)
1289 {
1290         GtkWidget *combobox;
1291         GtkListStore *menu;
1292         GtkCellRenderer *rend;
1293
1294         menu = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_INT, G_TYPE_BOOLEAN);
1295
1296         combobox = gtk_combo_box_new_with_model(GTK_TREE_MODEL(menu));
1297
1298         rend = gtk_cell_renderer_text_new();
1299         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox), rend, TRUE);
1300         gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combobox), rend,
1301                         "markup", COMBOBOX_TEXT,
1302                         "sensitive", COMBOBOX_SENS,
1303                         NULL);
1304
1305         if( eventbox != NULL )
1306                 gtk_container_add(GTK_CONTAINER(eventbox), combobox);
1307         gtk_combo_box_set_focus_on_click(GTK_COMBO_BOX(combobox), focus_on_click);
1308
1309         gtk_combo_box_set_row_separator_func(GTK_COMBO_BOX(combobox),
1310                         (GtkTreeViewRowSeparatorFunc)_combobox_separator_func, NULL, NULL);
1311
1312         return combobox;
1313 }
1314
1315 static void gtkutils_smooth_scroll_do(GtkWidget *widget, GtkAdjustment *vadj,
1316                                       gfloat old_value, gfloat last_value,
1317                                       gint step)
1318 {
1319         gint change_value;
1320         gboolean up;
1321         gint i;
1322
1323         if (old_value < last_value) {
1324                 change_value = last_value - old_value;
1325                 up = FALSE;
1326         } else {
1327                 change_value = old_value - last_value;
1328                 up = TRUE;
1329         }
1330
1331         for (i = step; i <= change_value; i += step) {
1332                 gtk_adjustment_set_value(vadj, old_value + (up ? -i : i));
1333                 g_signal_emit_by_name(G_OBJECT(vadj),
1334                                       "value_changed", 0);
1335         }
1336
1337         gtk_adjustment_set_value(vadj, last_value);
1338         g_signal_emit_by_name(G_OBJECT(vadj), "value_changed", 0);
1339
1340         gtk_widget_queue_draw(widget);
1341 }
1342
1343 static gboolean gtkutils_smooth_scroll_page(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1344 {
1345         gfloat upper;
1346         gfloat page_incr;
1347         gfloat old_value;
1348         gfloat last_value;
1349
1350         page_incr = gtk_adjustment_get_page_increment(vadj);
1351         if (prefs_common.scroll_halfpage)
1352                 page_incr /= 2;
1353
1354         old_value = gtk_adjustment_get_value(vadj);
1355         if (!up) {
1356                 upper = gtk_adjustment_get_upper(vadj) - gtk_adjustment_get_page_size(vadj);
1357                 if (old_value < upper) {
1358                         last_value = old_value + page_incr;
1359                         last_value = MIN(last_value, upper);
1360
1361                         gtkutils_smooth_scroll_do(widget, vadj, old_value,
1362                                                   last_value,
1363                                                   prefs_common.scroll_step);
1364                 } else
1365                         return FALSE;
1366         } else {
1367                 if (old_value > 0.0) {
1368                         last_value = old_value - page_incr;
1369                         last_value = MAX(last_value, 0.0);
1370
1371                         gtkutils_smooth_scroll_do(widget, vadj, old_value,
1372                                                   last_value,
1373                                                   prefs_common.scroll_step);
1374                 } else
1375                         return FALSE;
1376         }
1377
1378         return TRUE;
1379 }
1380
1381 gboolean gtkutils_scroll_page(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1382 {
1383         gfloat upper;
1384         gfloat page_incr;
1385         gfloat old_value;
1386
1387         if (prefs_common.enable_smooth_scroll)
1388                 return gtkutils_smooth_scroll_page(widget, vadj, up);
1389
1390         page_incr = gtk_adjustment_get_page_increment(vadj);
1391         if (prefs_common.scroll_halfpage)
1392                 page_incr /= 2;
1393
1394         old_value = gtk_adjustment_get_value(vadj);
1395         if (!up) {
1396                 upper = gtk_adjustment_get_upper(vadj) - gtk_adjustment_get_page_size(vadj);
1397                 if (old_value < upper) {
1398                         old_value += page_incr;
1399                         old_value = MIN(old_value, upper);
1400                         gtk_adjustment_set_value(vadj, old_value);
1401                         g_signal_emit_by_name(G_OBJECT(vadj),
1402                                               "value_changed", 0);
1403                 } else
1404                         return FALSE;
1405         } else {
1406                 if (old_value > 0.0) {
1407                         old_value -= page_incr;
1408                         old_value = MAX(old_value, 0.0);
1409                         gtk_adjustment_set_value(vadj, old_value);
1410                         g_signal_emit_by_name(G_OBJECT(vadj),
1411                                               "value_changed", 0);
1412                 } else
1413                         return FALSE;
1414         }
1415         return TRUE;
1416 }
1417
1418 static void gtkutils_smooth_scroll_one_line(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1419 {
1420         gfloat upper;
1421         gfloat old_value;
1422         gfloat last_value;
1423
1424         old_value = gtk_adjustment_get_value(vadj);
1425         if (!up) {
1426                 upper = gtk_adjustment_get_upper(vadj) - gtk_adjustment_get_page_size(vadj);
1427                 if (old_value < upper) {
1428                         last_value = old_value + gtk_adjustment_get_step_increment(vadj);
1429                         last_value = MIN(last_value, upper);
1430
1431                         gtkutils_smooth_scroll_do(widget, vadj, old_value,
1432                                                   last_value,
1433                                                   prefs_common.scroll_step);
1434                 }
1435         } else {
1436                 if (old_value > 0.0) {
1437                         last_value = old_value - gtk_adjustment_get_step_increment(vadj);
1438                         last_value = MAX(last_value, 0.0);
1439
1440                         gtkutils_smooth_scroll_do(widget, vadj, old_value,
1441                                                   last_value,
1442                                                   prefs_common.scroll_step);
1443                 }
1444         }
1445 }
1446
1447 void gtkutils_scroll_one_line(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1448 {
1449         gfloat upper;
1450         gfloat old_value;
1451
1452         if (prefs_common.enable_smooth_scroll) {
1453                 gtkutils_smooth_scroll_one_line(widget, vadj, up);
1454                 return;
1455         }
1456
1457         old_value = gtk_adjustment_get_value(vadj);
1458         if (!up) {
1459                 upper = gtk_adjustment_get_upper(vadj) - gtk_adjustment_get_page_size(vadj);
1460                 if (old_value < upper) {
1461                         old_value += gtk_adjustment_get_step_increment(vadj);
1462                         old_value = MIN(old_value, upper);
1463                         gtk_adjustment_set_value(vadj, old_value);
1464                         g_signal_emit_by_name(G_OBJECT(vadj),
1465                                               "value_changed", 0);
1466                 }
1467         } else {
1468                 if (old_value > 0.0) {
1469                         old_value -= gtk_adjustment_get_step_increment(vadj);
1470                         old_value = MAX(old_value, 0.0);
1471                         gtk_adjustment_set_value(vadj, old_value);
1472                         g_signal_emit_by_name(G_OBJECT(vadj),
1473                                               "value_changed", 0);
1474                 }
1475         }
1476 }
1477
1478 gboolean gtkut_tree_model_text_iter_prev(GtkTreeModel *model,
1479                                  GtkTreeIter *iter,
1480                                  const gchar* text)
1481 /* do the same as gtk_tree_model_iter_next, but _prev instead.
1482    to use with widgets with one text column (gtk_combo_box_new_text()
1483    and with GtkComboBoxEntry's for instance),
1484 */
1485 {
1486         GtkTreeIter cur_iter;
1487         gchar *cur_value;
1488         gboolean valid;
1489         gint count;
1490
1491         cm_return_val_if_fail(model != NULL, FALSE);
1492         cm_return_val_if_fail(iter != NULL, FALSE);
1493
1494         if (text == NULL || *text == '\0')
1495                 return FALSE;
1496
1497         valid = gtk_tree_model_get_iter_first(model, &cur_iter);
1498         count = 0;
1499         while (valid) {
1500                 gtk_tree_model_get(model, &cur_iter, 0, &cur_value, -1);
1501
1502                 if (strcmp(text, cur_value) == 0) {
1503                         if (count <= 0)
1504                                 return FALSE;
1505
1506                         return gtk_tree_model_iter_nth_child(model, iter, NULL, count - 1);
1507                 }
1508
1509                 valid = gtk_tree_model_iter_next(model, &cur_iter);
1510                 count++;
1511         }
1512         return FALSE;           
1513 }
1514
1515 gboolean gtkut_tree_model_get_iter_last(GtkTreeModel *model,
1516                                  GtkTreeIter *iter)
1517 /* do the same as gtk_tree_model_get_iter_first, but _last instead.
1518 */
1519 {
1520         gint count;
1521
1522         cm_return_val_if_fail(model != NULL, FALSE);
1523         cm_return_val_if_fail(iter != NULL, FALSE);
1524
1525         count = gtk_tree_model_iter_n_children(model, NULL);
1526
1527         if (count <= 0)
1528                 return FALSE;
1529
1530         return gtk_tree_model_iter_nth_child(model, iter, NULL, count - 1);
1531 }
1532
1533 GtkWidget *gtkut_window_new             (GtkWindowType   type,
1534                                          const gchar    *class)
1535 {
1536 #ifndef MAEMO
1537         GtkWidget *window = gtk_window_new(type);
1538 #else
1539         GtkWidget *window = hildon_window_new();
1540         hildon_program_add_window(hildon_program, HILDON_WINDOW(window));
1541 #endif
1542         gtk_window_set_role(GTK_WINDOW(window), class);
1543         return window;
1544 }
1545
1546 static gboolean gtkut_tree_iter_comp(GtkTreeModel *model, 
1547                                      GtkTreeIter *iter1, 
1548                                      GtkTreeIter *iter2)
1549 {
1550         GtkTreePath *path1 = gtk_tree_model_get_path(model, iter1);
1551         GtkTreePath *path2 = gtk_tree_model_get_path(model, iter2);
1552         gboolean result;
1553
1554         result = gtk_tree_path_compare(path1, path2) == 0;
1555
1556         gtk_tree_path_free(path1);
1557         gtk_tree_path_free(path2);
1558         
1559         return result;
1560 }
1561
1562 /*!
1563  *\brief        Get selected row number.
1564  */
1565 gint gtkut_list_view_get_selected_row(GtkWidget *list_view)
1566 {
1567         GtkTreeView *view = GTK_TREE_VIEW(list_view);
1568         GtkTreeModel *model = gtk_tree_view_get_model(view);
1569         int n_rows = gtk_tree_model_iter_n_children(model, NULL);
1570         GtkTreeSelection *selection;
1571         GtkTreeIter iter;
1572         int row;
1573
1574         if (n_rows == 0) 
1575                 return -1;
1576         
1577         selection = gtk_tree_view_get_selection(view);
1578         if (!gtk_tree_selection_get_selected(selection, &model, &iter))
1579                 return -1;
1580         
1581         /* get all iterators and compare them... */
1582         for (row = 0; row < n_rows; row++) {
1583                 GtkTreeIter itern;
1584
1585                 gtk_tree_model_iter_nth_child(model, &itern, NULL, row);
1586                 if (gtkut_tree_iter_comp(model, &iter, &itern))
1587                         return row;
1588         }
1589         
1590         return -1;
1591 }
1592
1593 /*!
1594  *\brief        Select a row by its number.
1595  */
1596 gboolean gtkut_list_view_select_row(GtkWidget *list, gint row)
1597 {
1598         GtkTreeView *list_view = GTK_TREE_VIEW(list);
1599         GtkTreeSelection *selection = gtk_tree_view_get_selection(list_view);
1600         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1601         GtkTreeIter iter;
1602         GtkTreePath *path;
1603
1604         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
1605                 return FALSE;
1606         
1607         gtk_tree_selection_select_iter(selection, &iter);
1608
1609         path = gtk_tree_model_get_path(model, &iter);
1610         gtk_tree_view_set_cursor(list_view, path, NULL, FALSE);
1611         gtk_tree_path_free(path);
1612         
1613         return TRUE;
1614 }
1615
1616 static GtkUIManager *gui_manager = NULL;
1617
1618 GtkUIManager *gtkut_create_ui_manager(void)
1619 {
1620         cm_return_val_if_fail(gui_manager == NULL, gui_manager);
1621         return (gui_manager = gtk_ui_manager_new());
1622 }
1623
1624 GtkUIManager *gtkut_ui_manager(void)
1625 {
1626         return gui_manager;
1627 }
1628
1629 #define READ_CONDITION (G_IO_IN | G_IO_HUP | G_IO_ERR)
1630 #define WRITE_CONDITION (G_IO_OUT | G_IO_ERR)
1631 #define EXCEPTION_CONDITION (G_IO_PRI)
1632 typedef struct _ClawsIOClosure ClawsIOClosure;
1633
1634 struct _ClawsIOClosure
1635 {
1636   GdkInputFunction function;
1637   GdkInputCondition condition;
1638   GDestroyNotify notify;
1639   gpointer data;
1640 };
1641
1642 static gboolean  
1643 claws_io_invoke (GIOChannel   *source,
1644                GIOCondition  condition,
1645                gpointer      data)
1646 {
1647   ClawsIOClosure *closure = data;
1648   GdkInputCondition gdk_cond = 0;
1649
1650   if (condition & READ_CONDITION)
1651     gdk_cond |= GDK_INPUT_READ;
1652   if (condition & WRITE_CONDITION)
1653     gdk_cond |= GDK_INPUT_WRITE;
1654   if (condition & EXCEPTION_CONDITION)
1655     gdk_cond |= GDK_INPUT_EXCEPTION;
1656
1657   if (closure->condition & gdk_cond)
1658     closure->function (closure->data, g_io_channel_unix_get_fd (source), gdk_cond);
1659
1660   return TRUE;
1661 }
1662
1663 static void
1664 claws_io_destroy (gpointer data)
1665 {
1666   ClawsIOClosure *closure = data;
1667
1668   if (closure->notify)
1669     closure->notify (closure->data);
1670
1671   g_free (closure);
1672 }
1673
1674 gint
1675 claws_input_add    (gint              source,
1676                     GdkInputCondition condition,
1677                     GdkInputFunction  function,
1678                     gpointer          data,
1679                     gboolean          is_sock)
1680 {
1681   guint result;
1682   ClawsIOClosure *closure = g_new (ClawsIOClosure, 1);
1683   GIOChannel *channel;
1684   GIOCondition cond = 0;
1685
1686   closure->function = function;
1687   closure->condition = condition;
1688   closure->notify = NULL;
1689   closure->data = data;
1690
1691   if (condition & GDK_INPUT_READ)
1692     cond |= READ_CONDITION;
1693   if (condition & GDK_INPUT_WRITE)
1694     cond |= WRITE_CONDITION;
1695   if (condition & GDK_INPUT_EXCEPTION)
1696     cond |= EXCEPTION_CONDITION;
1697
1698 #ifndef G_OS_WIN32
1699   channel = g_io_channel_unix_new (source);
1700 #else
1701   if (is_sock)
1702     channel = g_io_channel_win32_new_socket(source);
1703   else
1704     channel = g_io_channel_win32_new_fd(source);
1705 #endif
1706   result = g_io_add_watch_full (channel, G_PRIORITY_DEFAULT, cond, 
1707                                 claws_io_invoke,
1708                                 closure, claws_io_destroy);
1709   g_io_channel_unref (channel);
1710
1711   return result;
1712 }
1713
1714 gboolean gtkut_widget_has_focus(GtkWidget *widget)
1715 {
1716 #if GTK_CHECK_VERSION(2,18,0)
1717         return gtk_widget_has_focus(widget);
1718 #else
1719         return GTK_WIDGET_HAS_FOCUS(widget);
1720 #endif
1721 }
1722
1723 gboolean gtkut_widget_get_can_focus(GtkWidget *widget)
1724 {
1725 #if GTK_CHECK_VERSION(2,18,0)
1726         return gtk_widget_get_can_focus(widget);
1727 #else
1728         return GTK_WIDGET_CAN_FOCUS(widget);
1729 #endif
1730 }
1731
1732 gboolean gtkut_widget_has_grab(GtkWidget *widget)
1733 {
1734 #if GTK_CHECK_VERSION(2,18,0)
1735         return gtk_widget_has_grab(widget);
1736 #else
1737         return GTK_WIDGET_HAS_GRAB(widget);
1738 #endif
1739 }
1740
1741 gboolean gtkut_widget_get_visible(GtkWidget *widget)
1742 {
1743 #if GTK_CHECK_VERSION(2,18,0)
1744         return gtk_widget_get_visible(widget);
1745 #else
1746         return GTK_WIDGET_VISIBLE(widget);
1747 #endif
1748 }
1749
1750 gboolean gtkut_widget_get_realized(GtkWidget *widget)
1751 {
1752 #if GTK_CHECK_VERSION(2,19,0)
1753         return gtk_widget_get_realized(widget);
1754 #else
1755         return GTK_WIDGET_REALIZED(widget);
1756 #endif
1757 }
1758
1759 gboolean gtkut_widget_is_sensitive(GtkWidget *widget)
1760 {
1761 #if GTK_CHECK_VERSION(2,18,0)
1762         return gtk_widget_is_sensitive(widget);
1763 #else
1764         return GTK_WIDGET_IS_SENSITIVE(widget);
1765 #endif
1766 }
1767
1768 gboolean gtkut_widget_get_sensitive(GtkWidget *widget)
1769 {
1770 #if GTK_CHECK_VERSION(2,18,0)
1771         return gtk_widget_get_sensitive(widget);
1772 #else
1773         return GTK_WIDGET_SENSITIVE(widget);
1774 #endif
1775 }
1776
1777 gboolean gtkut_widget_is_drawable(GtkWidget *widget)
1778 {
1779 #if GTK_CHECK_VERSION(2,18,0)
1780         return gtk_widget_is_drawable(widget);
1781 #else
1782         return GTK_WIDGET_DRAWABLE(widget);
1783 #endif
1784 }
1785
1786 GtkStateType gtkut_widget_get_state(GtkWidget *widget)
1787 {
1788 #if GTK_CHECK_VERSION(2,18,0)
1789         return gtk_widget_get_state(widget);
1790 #else
1791         return GTK_WIDGET_STATE(widget);
1792 #endif
1793 }
1794
1795 gboolean gtkut_widget_get_mapped(GtkWidget *widget)
1796 {
1797 #if GTK_CHECK_VERSION(2,19,0)
1798         return gtk_widget_get_mapped(widget);
1799 #else
1800         return GTK_WIDGET_MAPPED(widget);
1801 #endif
1802 }
1803
1804 gboolean gtkut_widget_get_has_window(GtkWidget *widget)
1805 {
1806 #if GTK_CHECK_VERSION(2,18,0)
1807         return gtk_widget_get_has_window(widget);
1808 #else
1809         return GTK_WIDGET_NO_WINDOW(widget);
1810 #endif
1811 }
1812
1813 gboolean gtkut_widget_get_app_paintable(GtkWidget *widget)
1814 {
1815 #if GTK_CHECK_VERSION(2,18,0)
1816         return gtk_widget_get_app_paintable(widget);
1817 #else
1818         return GTK_WIDGET_APP_PAINTABLE(widget);
1819 #endif
1820 }
1821
1822 void gtkut_widget_set_mapped(GtkWidget *widget, gboolean mapped)
1823 {
1824 #if GTK_CHECK_VERSION(2,20,0)
1825         gtk_widget_set_mapped(widget, mapped);
1826 #else
1827         if (mapped)
1828                 GTK_WIDGET_SET_FLAGS(widget, GTK_MAPPED);
1829         else
1830                 GTK_WIDGET_UNSET_FLAGS(widget, GTK_MAPPED);
1831 #endif
1832 }
1833
1834 void gtkut_widget_set_realized(GtkWidget *widget, gboolean realized)
1835 {
1836 #if GTK_CHECK_VERSION(2,20,0)
1837         gtk_widget_set_realized(widget, realized);
1838 #else
1839         if (realized)
1840                 GTK_WIDGET_SET_FLAGS(widget, GTK_REALIZED);
1841         else
1842                 GTK_WIDGET_UNSET_FLAGS(widget, GTK_REALIZED);
1843 #endif
1844 }
1845
1846 void gtkut_widget_set_can_default(GtkWidget *widget, gboolean can_default)
1847 {
1848 #if GTK_CHECK_VERSION(2,20,0)
1849         gtk_widget_set_can_default(widget, can_default);
1850 #else
1851         if (can_default)
1852                 GTK_WIDGET_SET_FLAGS(widget, GTK_CAN_DEFAULT);
1853         else
1854                 GTK_WIDGET_UNSET_FLAGS(widget, GTK_CAN_DEFAULT);
1855 #endif
1856 }
1857
1858 void gtkut_widget_set_receives_default(GtkWidget *widget, gboolean receives_default)
1859 {
1860 #if GTK_CHECK_VERSION(2,20,0)
1861         gtk_widget_set_receives_default(widget, receives_default);
1862 #else
1863         if (receives_default)
1864                 GTK_WIDGET_SET_FLAGS(widget, GTK_RECEIVES_DEFAULT);
1865         else
1866                 GTK_WIDGET_UNSET_FLAGS(widget, GTK_RECEIVES_DEFAULT);
1867 #endif
1868 }
1869
1870 void gtkut_widget_set_can_focus(GtkWidget *widget, gboolean can_focus)
1871 {
1872 #if GTK_CHECK_VERSION(2,20,0)
1873         gtk_widget_set_can_focus(widget, can_focus);
1874 #else
1875         if (can_focus)
1876                 GTK_WIDGET_SET_FLAGS(widget, GTK_CAN_FOCUS);
1877         else
1878                 GTK_WIDGET_UNSET_FLAGS(widget, GTK_CAN_FOCUS);
1879 #endif
1880 }
1881
1882 void gtkut_widget_set_has_window(GtkWidget *widget, gboolean has_window)
1883 {
1884 #if GTK_CHECK_VERSION(2,20,0)
1885         gtk_widget_set_has_window(widget, has_window);
1886 #else
1887         if (has_window) /* Inverted logic there */
1888                 GTK_WIDGET_UNSET_FLAGS(widget, GTK_NO_WINDOW);
1889         else
1890                 GTK_WIDGET_SET_FLAGS(widget, GTK_NO_WINDOW);
1891 #endif
1892 }
1893
1894 /**
1895  * Load a pixbuf fitting inside the specified size. EXIF orientation is
1896  * respected if available.
1897  *
1898  * @param[in] filename          the file to load
1899  * @param[in] box_width         the max width (-1 for no resize)
1900  * @param[in] box_height        the max height (-1 for no resize)
1901  * @param[out] error            the possible load error
1902  *
1903  * @return a GdkPixbuf
1904  */
1905 GdkPixbuf *claws_load_pixbuf_fitting(GdkPixbuf *src_pixbuf, int box_width,
1906                                      int box_height)
1907 {
1908         gint w, h, orientation, angle;
1909         gint avail_width, avail_height;
1910         gboolean flip_horiz, flip_vert;
1911         const gchar *orient_str;
1912         GdkPixbuf *pixbuf, *t_pixbuf;
1913
1914         pixbuf = src_pixbuf;
1915
1916         if (pixbuf == NULL)
1917                 return NULL;
1918
1919         angle = 0;
1920         flip_horiz = flip_vert = FALSE;
1921
1922         /* EXIF orientation */
1923         orient_str = gdk_pixbuf_get_option(pixbuf, "orientation");
1924         if (orient_str != NULL && *orient_str != '\0') {
1925                 orientation = atoi(orient_str);
1926                 switch(orientation) {
1927                         /* See EXIF standard for different values */
1928                         case 1: break;
1929                         case 2: flip_horiz = 1;
1930                                 break;
1931                         case 3: angle = 180;
1932                                 break;
1933                         case 4: flip_vert = 1;
1934                                 break;
1935                         case 5: angle = 90;
1936                                 flip_horiz = 1;
1937                                 break;
1938                         case 6: angle = 270;
1939                                 break;
1940                         case 7: angle = 90;
1941                                 flip_vert = 1;
1942                                 break;
1943                         case 8: angle = 90;
1944                                 break;
1945                 }
1946         }
1947
1948         w = gdk_pixbuf_get_width(pixbuf);
1949         h = gdk_pixbuf_get_height(pixbuf);
1950
1951         if (angle == 90 || angle == 270) {
1952                 avail_height = box_width;
1953                 avail_width = box_height;
1954         } else {
1955                 avail_width = box_width;
1956                 avail_height = box_height;
1957         }
1958
1959         /* Scale first */
1960         if (box_width != -1 && box_height != -1 && avail_width - 100 > 0) {
1961                 if (w > avail_width) {
1962                         h = (avail_width * h) / w;
1963                         w = avail_width;
1964                 }
1965                 if (h > avail_height) {
1966                         w = (avail_height * w) / h;
1967                         h = avail_height;
1968                 }
1969                 t_pixbuf = gdk_pixbuf_scale_simple(pixbuf, 
1970                         w, h, GDK_INTERP_BILINEAR);
1971                 g_object_unref(pixbuf);
1972                 pixbuf = t_pixbuf;
1973         }
1974
1975         /* Rotate if needed */
1976         if (angle != 0) {
1977                 t_pixbuf = gdk_pixbuf_rotate_simple(pixbuf, angle);
1978                 g_object_unref(pixbuf);
1979                 pixbuf = t_pixbuf;
1980         }
1981
1982         /* Flip horizontally if needed */
1983         if (flip_horiz) {
1984                 t_pixbuf = gdk_pixbuf_flip(pixbuf, TRUE);
1985                 g_object_unref(pixbuf);
1986                 pixbuf = t_pixbuf;
1987         }
1988
1989         /* Flip vertically if needed */
1990         if (flip_vert) {
1991                 t_pixbuf = gdk_pixbuf_flip(pixbuf, FALSE);
1992                 g_object_unref(pixbuf);
1993                 pixbuf = t_pixbuf;
1994         }
1995
1996         return pixbuf;
1997 }