2012-01-31 [wwp] 3.8.0cvs19
[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 occurred 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 occurred while converting a string from UTF-8 to UCS-4: %s\n",
491                           error->message);
492                 g_error_free(error);
493         }
494         if (!wcs || items_written <= 0) return FALSE;
495         len = (gint)items_written;
496
497         iter_ = *iter;
498         while (gtk_text_iter_backward_char(&iter_)) {
499                 found = gtkut_text_buffer_match_string
500                         (buffer, &iter_, wcs, len, case_sens);
501                 if (found) {
502                         *match_pos = iter_;
503                         break;
504                 }
505         }
506
507         g_free(wcs);
508
509         return found;
510 }
511
512 gchar *gtkut_text_view_get_selection(GtkTextView *textview)
513 {
514         GtkTextBuffer *buffer;
515         GtkTextIter start_iter, end_iter;
516         gboolean found;
517
518         cm_return_val_if_fail(GTK_IS_TEXT_VIEW(textview), NULL);
519
520         buffer = gtk_text_view_get_buffer(textview);
521         found = gtk_text_buffer_get_selection_bounds(buffer,
522                                                      &start_iter,
523                                                      &end_iter);
524         if (found)
525                 return gtk_text_buffer_get_text(buffer, &start_iter, &end_iter,
526                                                 FALSE);
527         else
528                 return NULL;
529 }
530
531
532 void gtkut_text_view_set_position(GtkTextView *text, gint pos)
533 {
534         GtkTextBuffer *buffer;
535         GtkTextIter iter;
536         GtkTextMark *mark;
537
538         cm_return_if_fail(text != NULL);
539
540         buffer = gtk_text_view_get_buffer(text);
541
542         gtk_text_buffer_get_iter_at_offset(buffer, &iter, pos);
543         gtk_text_buffer_place_cursor(buffer, &iter);
544         mark = gtk_text_buffer_create_mark(buffer, NULL, &iter, TRUE);
545         gtk_text_view_scroll_to_mark(text, mark, 0.0, FALSE, 0.0, 0.0);
546 }
547
548 gboolean gtkut_text_view_search_string(GtkTextView *text, const gchar *str,
549                                         gboolean case_sens)
550 {
551         GtkTextBuffer *buffer;
552         GtkTextIter iter, match_pos;
553         GtkTextMark *mark;
554         gint len;
555
556         cm_return_val_if_fail(text != NULL, FALSE);
557         cm_return_val_if_fail(str != NULL, FALSE);
558
559         buffer = gtk_text_view_get_buffer(text);
560
561         len = g_utf8_strlen(str, -1);
562         cm_return_val_if_fail(len >= 0, FALSE);
563
564         mark = gtk_text_buffer_get_insert(buffer);
565         gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
566
567         if (gtkut_text_buffer_find(buffer, &iter, str, case_sens,
568                                    &match_pos)) {
569                 GtkTextIter end = match_pos;
570
571                 gtk_text_iter_forward_chars(&end, len);
572                 /* place "insert" at the last character */
573                 gtk_text_buffer_select_range(buffer, &end, &match_pos);
574                 gtk_text_view_scroll_to_mark(text, mark, 0.0, TRUE, 0.0, 0.5);
575                 return TRUE;
576         }
577
578         return FALSE;
579 }
580
581 gboolean gtkut_text_view_search_string_backward(GtkTextView *text, const gchar *str,
582                                         gboolean case_sens)
583 {
584         GtkTextBuffer *buffer;
585         GtkTextIter iter, match_pos;
586         GtkTextMark *mark;
587         gint len;
588
589         cm_return_val_if_fail(text != NULL, FALSE);
590         cm_return_val_if_fail(str != NULL, FALSE);
591
592         buffer = gtk_text_view_get_buffer(text);
593
594         len = g_utf8_strlen(str, -1);
595         cm_return_val_if_fail(len >= 0, FALSE);
596
597         mark = gtk_text_buffer_get_insert(buffer);
598         gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
599
600         if (gtkut_text_buffer_find_backward(buffer, &iter, str, case_sens,
601                                             &match_pos)) {
602                 GtkTextIter end = match_pos;
603
604                 gtk_text_iter_forward_chars(&end, len);
605                 gtk_text_buffer_select_range(buffer, &match_pos, &end);
606                 gtk_text_view_scroll_to_mark(text, mark, 0.0, TRUE, 0.0, 0.5);
607                 return TRUE;
608         }
609
610         return FALSE;
611 }
612
613 void gtkut_window_popup(GtkWidget *window)
614 {
615         GdkWindow *gdkwin;
616         gint x, y, sx, sy, new_x, new_y;
617
618         gdkwin = gtk_widget_get_window(window);
619
620         cm_return_if_fail(window != NULL);
621         cm_return_if_fail(gdkwin != NULL);
622
623         sx = gdk_screen_width();
624         sy = gdk_screen_height();
625
626         gdk_window_get_origin(gdkwin, &x, &y);
627         new_x = x % sx; if (new_x < 0) new_x = 0;
628         new_y = y % sy; if (new_y < 0) new_y = 0;
629         if (new_x != x || new_y != y)
630                 gdk_window_move(gdkwin, new_x, new_y);
631
632         gtk_window_set_skip_taskbar_hint(GTK_WINDOW(window), FALSE);
633         gtk_window_present_with_time(GTK_WINDOW(window), time(NULL));
634 }
635
636 void gtkut_widget_get_uposition(GtkWidget *widget, gint *px, gint *py)
637 {
638         GdkWindow *gdkwin;
639         gint x, y;
640         gint sx, sy;
641
642         gdkwin = gtk_widget_get_window(widget);
643
644         cm_return_if_fail(widget != NULL);
645         cm_return_if_fail(gdkwin != NULL);
646
647         sx = gdk_screen_width();
648         sy = gdk_screen_height();
649
650         /* gdk_window_get_root_origin ever return *rootwindow*'s position */
651         gdk_window_get_root_origin(gdkwin, &x, &y);
652
653         x %= sx; if (x < 0) x = 0;
654         y %= sy; if (y < 0) y = 0;
655         *px = x;
656         *py = y;
657 }
658
659 void gtkut_widget_draw_now(GtkWidget *widget)
660 {
661         if (widget && gtk_widget_get_visible(widget) && gtk_widget_is_drawable(widget))
662                 gdk_window_process_updates(gtk_widget_get_window(widget), FALSE);
663 }
664
665 static void gtkut_clist_bindings_add(GtkWidget *clist)
666 {
667         GtkBindingSet *binding_set;
668
669         binding_set = gtk_binding_set_by_class
670                 (GTK_CMCLIST_GET_CLASS(clist));
671
672         gtk_binding_entry_add_signal(binding_set, GDK_KEY_n, GDK_CONTROL_MASK,
673                                      "scroll_vertical", 2,
674                                      G_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
675                                      G_TYPE_FLOAT, 0.0);
676         gtk_binding_entry_add_signal(binding_set, GDK_p, GDK_CONTROL_MASK,
677                                      "scroll_vertical", 2,
678                                      G_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
679                                      G_TYPE_FLOAT, 0.0);
680 }
681
682 void gtkut_widget_init(void)
683 {
684         GtkWidget *clist;
685
686         clist = gtk_cmclist_new(1);
687         g_object_ref(G_OBJECT(clist));
688 #if GLIB_CHECK_VERSION(2,10,0)
689         g_object_ref_sink (G_OBJECT(clist));
690 #else
691         gtk_object_ref (G_OBJECT(clist));
692         gtk_object_sink (G_OBJECT(clist));
693 #endif
694         gtkut_clist_bindings_add(clist);
695         g_object_unref(G_OBJECT(clist));
696
697         clist = gtk_cmctree_new(1, 0);
698         g_object_ref(G_OBJECT(clist));
699 #if GLIB_CHECK_VERSION(2,10,0)
700         g_object_ref_sink (G_OBJECT(clist));
701 #else
702         gtk_object_ref (G_OBJECT(clist));
703         gtk_object_sink (G_OBJECT(clist));
704 #endif
705         gtkut_clist_bindings_add(clist);
706         g_object_unref(G_OBJECT(clist));
707
708         clist = gtk_sctree_new_with_titles(1, 0, NULL);
709         g_object_ref(G_OBJECT(clist));
710 #if GLIB_CHECK_VERSION(2,10,0)
711         g_object_ref_sink (G_OBJECT(clist));
712 #else
713         gtk_object_ref (G_OBJECT(clist));
714         gtk_object_sink (G_OBJECT(clist));
715 #endif
716         gtkut_clist_bindings_add(clist);
717         g_object_unref(G_OBJECT(clist));
718 }
719
720 void gtkut_widget_set_app_icon(GtkWidget *widget)
721 {
722         static GdkPixbuf *icon = NULL;
723         
724         cm_return_if_fail(widget != NULL);
725         cm_return_if_fail(gtk_widget_get_window(widget) != NULL);
726         if (!icon) {
727                 stock_pixbuf_gdk(widget, STOCK_PIXMAP_CLAWS_MAIL_ICON, &icon);
728         }               
729         if (icon)
730                 gtk_window_set_icon(GTK_WINDOW(widget), icon);
731 }
732
733 void gtkut_widget_set_composer_icon(GtkWidget *widget)
734 {
735         static GdkPixbuf *icon = NULL;
736         
737         cm_return_if_fail(widget != NULL);
738         cm_return_if_fail(gtk_widget_get_window(widget) != NULL);
739         if (!icon) {
740                 stock_pixbuf_gdk(widget, STOCK_PIXMAP_MAIL_COMPOSE, &icon);
741         }               
742         if (icon)
743                 gtk_window_set_icon(GTK_WINDOW(widget), icon);
744 }
745
746 static gboolean move_bar = FALSE;
747 static gint move_bar_id = -1;
748
749 static gboolean move_bar_cb(gpointer data)
750 {
751         GtkWidget *w = (GtkWidget *)data;
752         if (!move_bar)
753                 return FALSE;
754
755         if (!GTK_IS_PROGRESS_BAR(w)) {
756                 return FALSE;
757         }
758
759         gtk_progress_bar_pulse(GTK_PROGRESS_BAR(w));
760         GTK_EVENTS_FLUSH();
761         return TRUE;
762 }
763
764 GtkWidget *label_window_create(const gchar *str)
765 {
766         GtkWidget *window;
767         GtkWidget *label, *vbox, *hbox;
768         GtkWidget *wait_progress = gtk_progress_bar_new();
769
770         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "gtkutils");
771         gtk_widget_set_size_request(window, 380, 70);
772         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
773         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
774         gtk_window_set_title(GTK_WINDOW(window), str);
775         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
776         gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
777         manage_window_set_transient(GTK_WINDOW(window));
778
779         label = gtk_label_new(str);
780         
781         vbox = gtk_vbox_new(FALSE, 6);
782         hbox = gtk_hbox_new(FALSE, 6);
783         gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, FALSE, 0);
784         gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, FALSE, 0);
785         hbox = gtk_hbox_new(FALSE, 6);
786         gtk_box_pack_start(GTK_BOX(hbox), wait_progress, TRUE, FALSE, 0);
787         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
788         
789         gtk_container_add(GTK_CONTAINER(window), vbox);
790         gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
791         gtk_misc_set_alignment(GTK_MISC(label), 0.5, 0.5);
792         gtk_widget_show_all(vbox);
793
794         gtk_widget_show_now(window);
795         
796         if (move_bar_id == -1) {
797                 move_bar_id = g_timeout_add(200, move_bar_cb, wait_progress);
798                 move_bar = TRUE;
799         }
800
801         GTK_EVENTS_FLUSH();
802
803         return window;
804 }
805
806 void label_window_destroy(GtkWidget *window)
807 {
808         move_bar = FALSE;
809         g_source_remove(move_bar_id);
810         move_bar_id = -1;
811         GTK_EVENTS_FLUSH();
812         gtk_widget_destroy(window);     
813 }
814
815 GtkWidget *gtkut_account_menu_new(GList                 *ac_list,
816                                         GCallback               callback,
817                                   gpointer              data)
818 {
819         GList *cur_ac;
820         GtkWidget *optmenu;
821         GtkListStore *menu;
822         GtkTreeIter iter;
823         PrefsAccount *account;
824         gchar *name;
825         
826         cm_return_val_if_fail(ac_list != NULL, NULL);
827
828         optmenu = gtkut_sc_combobox_create(NULL, FALSE);
829         menu = GTK_LIST_STORE(gtk_combo_box_get_model(GTK_COMBO_BOX(optmenu)));
830
831         for (cur_ac = ac_list; cur_ac != NULL; cur_ac = cur_ac->next) {
832                 account = (PrefsAccount *) cur_ac->data;
833                 if (account->name)
834                         name = g_strdup_printf("%s: %s <%s>",
835                                                account->account_name,
836                                                account->name,
837                                                account->address);
838                 else
839                         name = g_strdup_printf("%s: %s",
840                                                account->account_name,
841                                                account->address);
842                 COMBOBOX_ADD_ESCAPED(menu, name, account->account_id);
843                 g_free(name);
844         }
845         gtk_combo_box_set_active(GTK_COMBO_BOX(optmenu), 0);
846
847         if( callback != NULL )
848                 g_signal_connect(G_OBJECT(optmenu), "changed", callback, data);
849
850         return optmenu;
851 }
852
853 void gtkut_set_widget_bgcolor_rgb(GtkWidget *widget, guint rgbvalue)
854 {
855         GtkStyle *newstyle;
856         GdkColor gdk_color;
857
858         gtkut_convert_int_to_gdk_color(rgbvalue, &gdk_color);
859         newstyle = gtk_style_copy(gtk_widget_get_default_style());
860         newstyle->bg[GTK_STATE_NORMAL]   = gdk_color;
861         newstyle->bg[GTK_STATE_PRELIGHT] = gdk_color;
862         newstyle->bg[GTK_STATE_ACTIVE]   = gdk_color;
863         gtk_widget_set_style(widget, newstyle);
864 }
865   
866 /*!
867  *\brief        Tries to find a focused child using a lame strategy
868  */
869 GtkWidget *gtkut_get_focused_child(GtkContainer *parent)
870 {
871         GtkWidget *result = NULL;
872         GList *child_list = NULL;
873         GList *c;
874
875         cm_return_val_if_fail(parent, NULL);
876
877         /* Get children list and see which has the focus. */
878         child_list = gtk_container_get_children(parent);
879         if (!child_list)
880                 return NULL;
881
882         for (c = child_list; c != NULL; c = g_list_next(c)) {
883                 if (c->data && GTK_IS_WIDGET(c->data)) {
884                         if (gtk_widget_has_focus(GTK_WIDGET(c->data))) {
885                                 result = GTK_WIDGET(c->data);
886                                 break;
887                         }
888                 }
889         }
890         
891         /* See if the returned widget is a container itself; if it is,
892          * see if one of its children is focused. If the focused 
893          * container has no focused child, it is itself a focusable 
894          * child, and has focus. */
895         if (result && GTK_IS_CONTAINER(result)) {
896                 GtkWidget *tmp =  gtkut_get_focused_child(GTK_CONTAINER(result)); 
897                 
898                 if (tmp) 
899                         result = tmp;
900         } else {
901                 /* Try the same for each container in the chain */
902                 for (c = child_list; c != NULL && !result; c = g_list_next(c)) {
903                         if (c->data && GTK_IS_WIDGET(c->data) 
904                         &&  GTK_IS_CONTAINER(c->data)) {
905                                 result = gtkut_get_focused_child
906                                         (GTK_CONTAINER(c->data));
907                         }
908                 }
909         
910         }
911         
912         g_list_free(child_list);
913                 
914         return result;
915 }
916
917 /*!
918  *\brief        Create a Browse (file) button based on GTK+ stock
919  */
920 GtkWidget *gtkut_get_browse_file_btn(const gchar *button_label)
921 {
922         GtkWidget *button;
923
924         button = gtk_button_new_with_mnemonic(button_label);
925         gtk_button_set_image(GTK_BUTTON(button),
926                 gtk_image_new_from_stock(GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_BUTTON));
927
928         return button;
929 }
930
931 /*!
932  *\brief        Create a Browse (directory) button based on GTK+ stock
933  */
934 GtkWidget *gtkut_get_browse_directory_btn(const gchar *button_label)
935 {
936         GtkWidget *button;
937
938         button = gtk_button_new_with_mnemonic(button_label);
939         gtk_button_set_image(GTK_BUTTON(button),
940                 gtk_image_new_from_stock(GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_BUTTON));
941
942         return button;
943 }
944
945 GtkWidget *gtkut_get_replace_btn(const gchar *button_label)
946 {
947         GtkWidget *button;
948
949         button = gtk_button_new_with_mnemonic(button_label);
950         gtk_button_set_image(GTK_BUTTON(button),
951                 gtk_image_new_from_stock(GTK_STOCK_REFRESH, GTK_ICON_SIZE_BUTTON));
952
953         return button;
954 }
955
956 /**
957  * merge some part of code into one function : it creates a frame and add
958  *      these into gtk box widget passed in param.
959  * \param box gtk box where adding new created frame.
960  * \param pframe pointer with which to assign the frame. If NULL, no pointer
961  *      is assigned but the frame is anyway created and added to @box.
962  * \param frame_label frame label of new created frame.
963  */
964 GtkWidget *gtkut_get_options_frame(GtkWidget *box, GtkWidget **pframe,
965                 const gchar *frame_label)
966 {
967         GtkWidget *vbox;
968         GtkWidget *frame;
969
970         frame = gtk_frame_new(frame_label);
971         gtk_widget_show(frame);
972         gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
973         gtk_frame_set_label_align(GTK_FRAME(frame), 0.01, 0.5);
974
975         vbox = gtk_vbox_new (FALSE, 4);
976         gtk_widget_show(vbox);
977         gtk_container_add(GTK_CONTAINER (frame), vbox);
978         gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
979
980         if (pframe != NULL)
981                 *pframe = frame;
982
983         return vbox;
984 }
985
986 #if HAVE_LIBCOMPFACE
987 static gint create_xpm_from_xface(gchar *xpm[], const gchar *xface)
988 {
989         static gchar *bit_pattern[] = {
990                 "....",
991                 "...#",
992                 "..#.",
993                 "..##",
994                 ".#..",
995                 ".#.#",
996                 ".##.",
997                 ".###",
998                 "#...",
999                 "#..#",
1000                 "#.#.",
1001                 "#.##",
1002                 "##..",
1003                 "##.#",
1004                 "###.",
1005                 "####"
1006         };
1007
1008         static gchar *xface_header = "48 48 2 1";
1009         static gchar *xface_black  = "# c #000000";
1010         static gchar *xface_white  = ". c #ffffff";
1011
1012         gint i, line = 0;
1013         const guchar *p;
1014         gchar buf[WIDTH * 4 + 1];  /* 4 = strlen("0x0000") */
1015
1016         p = xface;
1017
1018         strcpy(xpm[line++], xface_header);
1019         strcpy(xpm[line++], xface_black);
1020         strcpy(xpm[line++], xface_white);
1021
1022         for (i = 0; i < HEIGHT; i++) {
1023                 gint col;
1024
1025                 buf[0] = '\0';
1026      
1027                 for (col = 0; col < 3; col++) {
1028                         gint figure;
1029
1030                         p += 2;  /* skip '0x' */
1031
1032                         for (figure = 0; figure < 4; figure++) {
1033                                 gint n = 0;
1034
1035                                 if ('0' <= *p && *p <= '9') {
1036                                         n = *p - '0';
1037                                 } else if ('a' <= *p && *p <= 'f') {
1038                                         n = *p - 'a' + 10;
1039                                 } else if ('A' <= *p && *p <= 'F') {
1040                                         n = *p - 'A' + 10;
1041                                 }
1042
1043                                 strcat(buf, bit_pattern[n]);
1044                                 p++;  /* skip ',' */
1045                         }
1046
1047                         p++;  /* skip '\n' */
1048                 }
1049
1050                 strcpy(xpm[line++], buf);
1051                 p++;
1052         }
1053
1054         return 0;
1055 }
1056 #endif
1057
1058 gboolean get_tag_range(GtkTextIter *iter,
1059                                        GtkTextTag *tag,
1060                                        GtkTextIter *start_iter,
1061                                        GtkTextIter *end_iter)
1062 {
1063         GtkTextIter _start_iter, _end_iter;
1064
1065         _end_iter = *iter;
1066         if (!gtk_text_iter_forward_to_tag_toggle(&_end_iter, tag)) {
1067                 debug_print("Can't find end");
1068                 return FALSE;
1069         }
1070
1071         _start_iter = _end_iter;
1072         if (!gtk_text_iter_backward_to_tag_toggle(&_start_iter, tag)) {
1073                 debug_print("Can't find start.");
1074                 return FALSE;
1075         }
1076
1077         *start_iter = _start_iter;
1078         *end_iter = _end_iter;
1079
1080         return TRUE;
1081 }
1082
1083 #if HAVE_LIBCOMPFACE
1084 GtkWidget *xface_get_from_header(const gchar *o_xface, GdkColor *background,
1085                                  GdkWindow *window)
1086 {
1087         static gchar *xpm_xface[XPM_XFACE_HEIGHT];
1088         static gboolean xpm_xface_init = TRUE;
1089         gchar xface[2048];
1090         strncpy(xface, o_xface, sizeof(xface));
1091
1092         if (!window) {
1093                 g_warning("no window\n");
1094                 return NULL;
1095         }
1096         if (uncompface(xface) < 0) {
1097                 g_warning("uncompface failed\n");
1098                 return NULL;
1099         }
1100
1101         if (xpm_xface_init) {
1102                 gint i;
1103
1104                 for (i = 0; i < XPM_XFACE_HEIGHT; i++) {
1105                         xpm_xface[i] = g_malloc(WIDTH + 1);
1106                         *xpm_xface[i] = '\0';
1107                 }
1108                 xpm_xface_init = FALSE;
1109         }
1110
1111         create_xpm_from_xface(xpm_xface, xface);
1112
1113         return gtk_image_new_from_pixbuf(
1114                 gdk_pixbuf_new_from_xpm_data((const char **)xpm_xface));
1115 }
1116 #endif
1117
1118 GtkWidget *face_get_from_header(const gchar *o_face)
1119 {
1120         gchar face[2048];
1121         gchar face_png[2048];
1122         gint pngsize;
1123         GdkPixbuf *pixbuf;
1124         GError *error = NULL;
1125         GdkPixbufLoader *loader = gdk_pixbuf_loader_new ();
1126         GtkWidget *image;
1127         
1128         if (o_face == NULL || strlen(o_face) == 0)
1129                 return NULL;
1130
1131         strncpy2(face, o_face, sizeof(face));
1132
1133         unfold_line(face); /* strip all whitespace and linebreaks */
1134         remove_space(face);
1135
1136         pngsize = base64_decode(face_png, face, strlen(face));
1137
1138         if (!gdk_pixbuf_loader_write (loader, face_png, pngsize, &error) ||
1139             !gdk_pixbuf_loader_close (loader, &error)) {
1140                 g_warning("loading face failed\n");
1141                 g_object_unref(loader);
1142                 return NULL;
1143         }
1144
1145         pixbuf = g_object_ref(gdk_pixbuf_loader_get_pixbuf(loader));
1146
1147         g_object_unref(loader);
1148
1149         if ((gdk_pixbuf_get_width(pixbuf) != 48) || (gdk_pixbuf_get_height(pixbuf) != 48)) {
1150                 g_object_unref(pixbuf);
1151                 g_warning("wrong_size\n");
1152                 return NULL;
1153         }
1154
1155         image = gtk_image_new_from_pixbuf(pixbuf);
1156         g_object_unref(pixbuf);
1157         return image;
1158 }
1159
1160 static GdkCursor *hand_cursor = NULL;
1161
1162 static void link_btn_enter(GtkButton *button, gpointer data)
1163 {
1164         GdkWindow *gdkwin;
1165         GtkWidget *window = (GtkWidget *)data;
1166
1167         gdkwin = gtk_widget_get_window(window);
1168
1169         if (!hand_cursor)
1170                 hand_cursor = gdk_cursor_new(GDK_HAND2);
1171         if (window && gdkwin)
1172                 gdk_window_set_cursor(gdkwin, hand_cursor);
1173
1174         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1175         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1176         
1177 }
1178
1179 static void link_btn_leave(GtkButton *button, gpointer data)
1180 {
1181         GdkWindow *gdkwin;
1182         GtkWidget *window = (GtkWidget *)data;
1183
1184         gdkwin = gtk_widget_get_window(window);
1185
1186         if (window && gdkwin)
1187                 gdk_window_set_cursor(gdkwin, NULL);
1188
1189         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1190         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1191 }
1192
1193 static void link_btn_pressed(GtkButton *button, gpointer data)
1194 {
1195         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1196         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1197 }
1198
1199 static void link_btn_released(GtkButton *button, gpointer data)
1200 {
1201         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1202         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1203 }
1204
1205 static void link_btn_clicked(GtkButton *button, gpointer data)
1206 {
1207         gchar *url = (gchar *)data;
1208         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1209         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1210         open_uri(url, prefs_common_get_uri_cmd());
1211 }
1212
1213 static void link_btn_unrealize(GtkButton *button, gpointer data)
1214 {
1215         gchar *url = (gchar *)data;
1216         g_signal_handlers_disconnect_by_func(G_OBJECT(button), 
1217                          G_CALLBACK(link_btn_clicked), url);
1218         g_free(url);
1219 }
1220
1221 GtkWidget *gtkut_get_link_btn(GtkWidget *window, const gchar *url, const gchar *label)
1222 {
1223         GtkWidget *btn;
1224         GtkWidget *btn_label;
1225 #if !GTK_CHECK_VERSION(3, 0, 0)
1226         GdkColormap *cmap;
1227         gboolean success[2];
1228 #endif
1229         GdkColor uri_color[2] = {{0, 0, 0, 0xffff}, {0, 0xffff, 0, 0}};
1230         gchar *local_url = NULL;
1231         if (!url)
1232                 return NULL;
1233
1234         gtkut_convert_int_to_gdk_color(prefs_common.uri_col,
1235                                                &uri_color[0]);
1236         gtkut_convert_int_to_gdk_color(prefs_common.uri_col,
1237                                                &uri_color[1]);
1238
1239         btn = gtk_button_new_with_label(label?label:url);
1240         gtk_button_set_relief(GTK_BUTTON(btn), GTK_RELIEF_NONE);
1241         btn_label = gtk_bin_get_child(GTK_BIN((btn)));
1242 #if !GTK_CHECK_VERSION(3, 0, 0)
1243         cmap = gdk_drawable_get_colormap(gtk_widget_get_window(window));
1244         gdk_colormap_alloc_colors(cmap, uri_color, 2, FALSE, TRUE, success);
1245         if (success[0] == TRUE && success[1] == TRUE) {
1246 #endif
1247                 GtkStyle *style;
1248                 gtk_widget_ensure_style(btn_label);
1249                 style = gtk_style_copy
1250                         (gtk_widget_get_style(btn_label));
1251                 style->fg[GTK_STATE_NORMAL]   = uri_color[0];
1252                 style->fg[GTK_STATE_ACTIVE]   = uri_color[1];
1253                 style->fg[GTK_STATE_PRELIGHT] = uri_color[0];
1254                 gtk_widget_set_style(btn_label, style);
1255 #if !GTK_CHECK_VERSION(3, 0, 0)
1256         } else
1257                 g_warning("color allocation failed\n");
1258 #endif
1259
1260         g_signal_connect(G_OBJECT(btn), "enter",
1261                          G_CALLBACK(link_btn_enter), window);
1262         g_signal_connect(G_OBJECT(btn), "leave",
1263                          G_CALLBACK(link_btn_leave), window);
1264         g_signal_connect(G_OBJECT(btn), "pressed",
1265                          G_CALLBACK(link_btn_pressed), window);
1266         g_signal_connect(G_OBJECT(btn), "released",
1267                          G_CALLBACK(link_btn_released), window);
1268                          
1269         local_url = g_strdup(url);
1270         g_signal_connect(G_OBJECT(btn), "clicked",
1271                          G_CALLBACK(link_btn_clicked), local_url);
1272         g_signal_connect(G_OBJECT(btn), "unrealize",
1273                          G_CALLBACK(link_btn_unrealize), local_url);
1274         return btn;
1275 }
1276
1277 static gboolean _combobox_separator_func(GtkTreeModel *model,
1278                 GtkTreeIter *iter, gpointer data)
1279 {
1280         gchar *txt = NULL;
1281
1282         cm_return_val_if_fail(model != NULL, FALSE);
1283
1284         gtk_tree_model_get(model, iter, COMBOBOX_TEXT, &txt, -1);
1285
1286         if( txt == NULL )
1287                 return TRUE;
1288         return FALSE;
1289 }
1290
1291 GtkWidget *gtkut_sc_combobox_create(GtkWidget *eventbox, gboolean focus_on_click)
1292 {
1293         GtkWidget *combobox;
1294         GtkListStore *menu;
1295         GtkCellRenderer *rend;
1296
1297         menu = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_INT, G_TYPE_BOOLEAN);
1298
1299         combobox = gtk_combo_box_new_with_model(GTK_TREE_MODEL(menu));
1300
1301         rend = gtk_cell_renderer_text_new();
1302         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox), rend, TRUE);
1303         gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combobox), rend,
1304                         "markup", COMBOBOX_TEXT,
1305                         "sensitive", COMBOBOX_SENS,
1306                         NULL);
1307
1308         if( eventbox != NULL )
1309                 gtk_container_add(GTK_CONTAINER(eventbox), combobox);
1310         gtk_combo_box_set_focus_on_click(GTK_COMBO_BOX(combobox), focus_on_click);
1311
1312         gtk_combo_box_set_row_separator_func(GTK_COMBO_BOX(combobox),
1313                         (GtkTreeViewRowSeparatorFunc)_combobox_separator_func, NULL, NULL);
1314
1315         return combobox;
1316 }
1317
1318 static void gtkutils_smooth_scroll_do(GtkWidget *widget, GtkAdjustment *vadj,
1319                                       gfloat old_value, gfloat last_value,
1320                                       gint step)
1321 {
1322         gint change_value;
1323         gboolean up;
1324         gint i;
1325
1326         if (old_value < last_value) {
1327                 change_value = last_value - old_value;
1328                 up = FALSE;
1329         } else {
1330                 change_value = old_value - last_value;
1331                 up = TRUE;
1332         }
1333
1334         for (i = step; i <= change_value; i += step) {
1335                 gtk_adjustment_set_value(vadj, old_value + (up ? -i : i));
1336                 g_signal_emit_by_name(G_OBJECT(vadj),
1337                                       "value_changed", 0);
1338         }
1339
1340         gtk_adjustment_set_value(vadj, last_value);
1341         g_signal_emit_by_name(G_OBJECT(vadj), "value_changed", 0);
1342
1343         gtk_widget_queue_draw(widget);
1344 }
1345
1346 static gboolean gtkutils_smooth_scroll_page(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1347 {
1348         gfloat upper;
1349         gfloat page_incr;
1350         gfloat old_value;
1351         gfloat last_value;
1352
1353         page_incr = gtk_adjustment_get_page_increment(vadj);
1354         if (prefs_common.scroll_halfpage)
1355                 page_incr /= 2;
1356
1357         old_value = gtk_adjustment_get_value(vadj);
1358         if (!up) {
1359                 upper = gtk_adjustment_get_upper(vadj) - gtk_adjustment_get_page_size(vadj);
1360                 if (old_value < upper) {
1361                         last_value = old_value + page_incr;
1362                         last_value = MIN(last_value, upper);
1363
1364                         gtkutils_smooth_scroll_do(widget, vadj, old_value,
1365                                                   last_value,
1366                                                   prefs_common.scroll_step);
1367                 } else
1368                         return FALSE;
1369         } else {
1370                 if (old_value > 0.0) {
1371                         last_value = old_value - page_incr;
1372                         last_value = MAX(last_value, 0.0);
1373
1374                         gtkutils_smooth_scroll_do(widget, vadj, old_value,
1375                                                   last_value,
1376                                                   prefs_common.scroll_step);
1377                 } else
1378                         return FALSE;
1379         }
1380
1381         return TRUE;
1382 }
1383
1384 gboolean gtkutils_scroll_page(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1385 {
1386         gfloat upper;
1387         gfloat page_incr;
1388         gfloat old_value;
1389
1390         if (prefs_common.enable_smooth_scroll)
1391                 return gtkutils_smooth_scroll_page(widget, vadj, up);
1392
1393         page_incr = gtk_adjustment_get_page_increment(vadj);
1394         if (prefs_common.scroll_halfpage)
1395                 page_incr /= 2;
1396
1397         old_value = gtk_adjustment_get_value(vadj);
1398         if (!up) {
1399                 upper = gtk_adjustment_get_upper(vadj) - gtk_adjustment_get_page_size(vadj);
1400                 if (old_value < upper) {
1401                         old_value += page_incr;
1402                         old_value = MIN(old_value, upper);
1403                         gtk_adjustment_set_value(vadj, old_value);
1404                         g_signal_emit_by_name(G_OBJECT(vadj),
1405                                               "value_changed", 0);
1406                 } else
1407                         return FALSE;
1408         } else {
1409                 if (old_value > 0.0) {
1410                         old_value -= page_incr;
1411                         old_value = MAX(old_value, 0.0);
1412                         gtk_adjustment_set_value(vadj, old_value);
1413                         g_signal_emit_by_name(G_OBJECT(vadj),
1414                                               "value_changed", 0);
1415                 } else
1416                         return FALSE;
1417         }
1418         return TRUE;
1419 }
1420
1421 static void gtkutils_smooth_scroll_one_line(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1422 {
1423         gfloat upper;
1424         gfloat old_value;
1425         gfloat last_value;
1426
1427         old_value = gtk_adjustment_get_value(vadj);
1428         if (!up) {
1429                 upper = gtk_adjustment_get_upper(vadj) - gtk_adjustment_get_page_size(vadj);
1430                 if (old_value < upper) {
1431                         last_value = old_value + gtk_adjustment_get_step_increment(vadj);
1432                         last_value = MIN(last_value, upper);
1433
1434                         gtkutils_smooth_scroll_do(widget, vadj, old_value,
1435                                                   last_value,
1436                                                   prefs_common.scroll_step);
1437                 }
1438         } else {
1439                 if (old_value > 0.0) {
1440                         last_value = old_value - gtk_adjustment_get_step_increment(vadj);
1441                         last_value = MAX(last_value, 0.0);
1442
1443                         gtkutils_smooth_scroll_do(widget, vadj, old_value,
1444                                                   last_value,
1445                                                   prefs_common.scroll_step);
1446                 }
1447         }
1448 }
1449
1450 void gtkutils_scroll_one_line(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1451 {
1452         gfloat upper;
1453         gfloat old_value;
1454
1455         if (prefs_common.enable_smooth_scroll) {
1456                 gtkutils_smooth_scroll_one_line(widget, vadj, up);
1457                 return;
1458         }
1459
1460         old_value = gtk_adjustment_get_value(vadj);
1461         if (!up) {
1462                 upper = gtk_adjustment_get_upper(vadj) - gtk_adjustment_get_page_size(vadj);
1463                 if (old_value < upper) {
1464                         old_value += gtk_adjustment_get_step_increment(vadj);
1465                         old_value = MIN(old_value, upper);
1466                         gtk_adjustment_set_value(vadj, old_value);
1467                         g_signal_emit_by_name(G_OBJECT(vadj),
1468                                               "value_changed", 0);
1469                 }
1470         } else {
1471                 if (old_value > 0.0) {
1472                         old_value -= gtk_adjustment_get_step_increment(vadj);
1473                         old_value = MAX(old_value, 0.0);
1474                         gtk_adjustment_set_value(vadj, old_value);
1475                         g_signal_emit_by_name(G_OBJECT(vadj),
1476                                               "value_changed", 0);
1477                 }
1478         }
1479 }
1480
1481 gboolean gtkut_tree_model_text_iter_prev(GtkTreeModel *model,
1482                                  GtkTreeIter *iter,
1483                                  const gchar* text)
1484 /* do the same as gtk_tree_model_iter_next, but _prev instead.
1485    to use with widgets with one text column (gtk_combo_box_new_text()
1486    and with GtkComboBoxEntry's for instance),
1487 */
1488 {
1489         GtkTreeIter cur_iter;
1490         gchar *cur_value;
1491         gboolean valid;
1492         gint count;
1493
1494         cm_return_val_if_fail(model != NULL, FALSE);
1495         cm_return_val_if_fail(iter != NULL, FALSE);
1496
1497         if (text == NULL || *text == '\0')
1498                 return FALSE;
1499
1500         valid = gtk_tree_model_get_iter_first(model, &cur_iter);
1501         count = 0;
1502         while (valid) {
1503                 gtk_tree_model_get(model, &cur_iter, 0, &cur_value, -1);
1504
1505                 if (strcmp(text, cur_value) == 0) {
1506                         if (count <= 0)
1507                                 return FALSE;
1508
1509                         return gtk_tree_model_iter_nth_child(model, iter, NULL, count - 1);
1510                 }
1511
1512                 valid = gtk_tree_model_iter_next(model, &cur_iter);
1513                 count++;
1514         }
1515         return FALSE;           
1516 }
1517
1518 gboolean gtkut_tree_model_get_iter_last(GtkTreeModel *model,
1519                                  GtkTreeIter *iter)
1520 /* do the same as gtk_tree_model_get_iter_first, but _last instead.
1521 */
1522 {
1523         gint count;
1524
1525         cm_return_val_if_fail(model != NULL, FALSE);
1526         cm_return_val_if_fail(iter != NULL, FALSE);
1527
1528         count = gtk_tree_model_iter_n_children(model, NULL);
1529
1530         if (count <= 0)
1531                 return FALSE;
1532
1533         return gtk_tree_model_iter_nth_child(model, iter, NULL, count - 1);
1534 }
1535
1536 GtkWidget *gtkut_window_new             (GtkWindowType   type,
1537                                          const gchar    *class)
1538 {
1539 #ifndef MAEMO
1540         GtkWidget *window = gtk_window_new(type);
1541 #else
1542         GtkWidget *window = hildon_window_new();
1543         hildon_program_add_window(hildon_program, HILDON_WINDOW(window));
1544 #endif
1545         gtk_window_set_role(GTK_WINDOW(window), class);
1546         return window;
1547 }
1548
1549 static gboolean gtkut_tree_iter_comp(GtkTreeModel *model, 
1550                                      GtkTreeIter *iter1, 
1551                                      GtkTreeIter *iter2)
1552 {
1553         GtkTreePath *path1 = gtk_tree_model_get_path(model, iter1);
1554         GtkTreePath *path2 = gtk_tree_model_get_path(model, iter2);
1555         gboolean result;
1556
1557         result = gtk_tree_path_compare(path1, path2) == 0;
1558
1559         gtk_tree_path_free(path1);
1560         gtk_tree_path_free(path2);
1561         
1562         return result;
1563 }
1564
1565 /*!
1566  *\brief        Get selected row number.
1567  */
1568 gint gtkut_list_view_get_selected_row(GtkWidget *list_view)
1569 {
1570         GtkTreeView *view = GTK_TREE_VIEW(list_view);
1571         GtkTreeModel *model = gtk_tree_view_get_model(view);
1572         int n_rows = gtk_tree_model_iter_n_children(model, NULL);
1573         GtkTreeSelection *selection;
1574         GtkTreeIter iter;
1575         int row;
1576
1577         if (n_rows == 0) 
1578                 return -1;
1579         
1580         selection = gtk_tree_view_get_selection(view);
1581         if (!gtk_tree_selection_get_selected(selection, &model, &iter))
1582                 return -1;
1583         
1584         /* get all iterators and compare them... */
1585         for (row = 0; row < n_rows; row++) {
1586                 GtkTreeIter itern;
1587
1588                 gtk_tree_model_iter_nth_child(model, &itern, NULL, row);
1589                 if (gtkut_tree_iter_comp(model, &iter, &itern))
1590                         return row;
1591         }
1592         
1593         return -1;
1594 }
1595
1596 /*!
1597  *\brief        Select a row by its number.
1598  */
1599 gboolean gtkut_list_view_select_row(GtkWidget *list, gint row)
1600 {
1601         GtkTreeView *list_view = GTK_TREE_VIEW(list);
1602         GtkTreeSelection *selection = gtk_tree_view_get_selection(list_view);
1603         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1604         GtkTreeIter iter;
1605         GtkTreePath *path;
1606
1607         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
1608                 return FALSE;
1609         
1610         gtk_tree_selection_select_iter(selection, &iter);
1611
1612         path = gtk_tree_model_get_path(model, &iter);
1613         gtk_tree_view_set_cursor(list_view, path, NULL, FALSE);
1614         gtk_tree_path_free(path);
1615         
1616         return TRUE;
1617 }
1618
1619 static GtkUIManager *gui_manager = NULL;
1620
1621 GtkUIManager *gtkut_create_ui_manager(void)
1622 {
1623         cm_return_val_if_fail(gui_manager == NULL, gui_manager);
1624         return (gui_manager = gtk_ui_manager_new());
1625 }
1626
1627 GtkUIManager *gtkut_ui_manager(void)
1628 {
1629         return gui_manager;
1630 }
1631
1632 typedef struct _ClawsIOClosure ClawsIOClosure;
1633
1634 struct _ClawsIOClosure
1635 {
1636   ClawsIOFunc function;
1637   GIOCondition 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
1649   if (closure->condition & condition)
1650     closure->function (closure->data, g_io_channel_unix_get_fd (source),
1651                        condition);
1652
1653   return TRUE;
1654 }
1655
1656 static void
1657 claws_io_destroy (gpointer data)
1658 {
1659   ClawsIOClosure *closure = data;
1660
1661   if (closure->notify)
1662     closure->notify (closure->data);
1663
1664   g_free (closure);
1665 }
1666
1667 gint
1668 claws_input_add    (gint              source,
1669                     GIOCondition      condition,
1670                     ClawsIOFunc       function,
1671                     gpointer          data,
1672                     gboolean          is_sock)
1673 {
1674   guint result;
1675   ClawsIOClosure *closure = g_new (ClawsIOClosure, 1);
1676   GIOChannel *channel;
1677
1678   closure->function = function;
1679   closure->condition = condition;
1680   closure->notify = NULL;
1681   closure->data = data;
1682
1683 #ifndef G_OS_WIN32
1684   channel = g_io_channel_unix_new (source);
1685 #else
1686   if (is_sock)
1687     channel = g_io_channel_win32_new_socket(source);
1688   else
1689     channel = g_io_channel_win32_new_fd(source);
1690 #endif
1691   result = g_io_add_watch_full (channel, G_PRIORITY_DEFAULT, condition, 
1692                                 claws_io_invoke,
1693                                 closure, claws_io_destroy);
1694   g_io_channel_unref (channel);
1695
1696   return result;
1697 }
1698
1699 void gtkut_widget_set_mapped(GtkWidget *widget, gboolean mapped)
1700 {
1701 #if GTK_CHECK_VERSION(2,20,0)
1702         gtk_widget_set_mapped(widget, mapped);
1703 #else
1704         if (mapped)
1705                 GTK_WIDGET_SET_FLAGS(widget, GTK_MAPPED);
1706         else
1707                 GTK_WIDGET_UNSET_FLAGS(widget, GTK_MAPPED);
1708 #endif
1709 }
1710
1711 void gtkut_widget_set_realized(GtkWidget *widget, gboolean realized)
1712 {
1713 #if GTK_CHECK_VERSION(2,20,0)
1714         gtk_widget_set_realized(widget, realized);
1715 #else
1716         if (realized)
1717                 GTK_WIDGET_SET_FLAGS(widget, GTK_REALIZED);
1718         else
1719                 GTK_WIDGET_UNSET_FLAGS(widget, GTK_REALIZED);
1720 #endif
1721 }
1722
1723 void gtkut_widget_set_can_default(GtkWidget *widget, gboolean can_default)
1724 {
1725 #if GTK_CHECK_VERSION(2,20,0)
1726         gtk_widget_set_can_default(widget, can_default);
1727 #else
1728         if (can_default)
1729                 GTK_WIDGET_SET_FLAGS(widget, GTK_CAN_DEFAULT);
1730         else
1731                 GTK_WIDGET_UNSET_FLAGS(widget, GTK_CAN_DEFAULT);
1732 #endif
1733 }
1734
1735 void gtkut_widget_set_receives_default(GtkWidget *widget, gboolean receives_default)
1736 {
1737 #if GTK_CHECK_VERSION(2,20,0)
1738         gtk_widget_set_receives_default(widget, receives_default);
1739 #else
1740         if (receives_default)
1741                 GTK_WIDGET_SET_FLAGS(widget, GTK_RECEIVES_DEFAULT);
1742         else
1743                 GTK_WIDGET_UNSET_FLAGS(widget, GTK_RECEIVES_DEFAULT);
1744 #endif
1745 }
1746
1747 void gtkut_widget_set_can_focus(GtkWidget *widget, gboolean can_focus)
1748 {
1749 #if GTK_CHECK_VERSION(2,20,0)
1750         gtk_widget_set_can_focus(widget, can_focus);
1751 #else
1752         if (can_focus)
1753                 GTK_WIDGET_SET_FLAGS(widget, GTK_CAN_FOCUS);
1754         else
1755                 GTK_WIDGET_UNSET_FLAGS(widget, GTK_CAN_FOCUS);
1756 #endif
1757 }
1758
1759 void gtkut_widget_set_has_window(GtkWidget *widget, gboolean has_window)
1760 {
1761 #if GTK_CHECK_VERSION(2,20,0)
1762         gtk_widget_set_has_window(widget, has_window);
1763 #else
1764         if (has_window) /* Inverted logic there */
1765                 GTK_WIDGET_UNSET_FLAGS(widget, GTK_NO_WINDOW);
1766         else
1767                 GTK_WIDGET_SET_FLAGS(widget, GTK_NO_WINDOW);
1768 #endif
1769 }
1770
1771 /**
1772  * Load a pixbuf fitting inside the specified size. EXIF orientation is
1773  * respected if available.
1774  *
1775  * @param[in] filename          the file to load
1776  * @param[in] box_width         the max width (-1 for no resize)
1777  * @param[in] box_height        the max height (-1 for no resize)
1778  * @param[out] error            the possible load error
1779  *
1780  * @return a GdkPixbuf
1781  */
1782 GdkPixbuf *claws_load_pixbuf_fitting(GdkPixbuf *src_pixbuf, int box_width,
1783                                      int box_height)
1784 {
1785         gint w, h, orientation, angle;
1786         gint avail_width, avail_height;
1787         gboolean flip_horiz, flip_vert;
1788         const gchar *orient_str;
1789         GdkPixbuf *pixbuf, *t_pixbuf;
1790
1791         pixbuf = src_pixbuf;
1792
1793         if (pixbuf == NULL)
1794                 return NULL;
1795
1796         angle = 0;
1797         flip_horiz = flip_vert = FALSE;
1798
1799         /* EXIF orientation */
1800         orient_str = gdk_pixbuf_get_option(pixbuf, "orientation");
1801         if (orient_str != NULL && *orient_str != '\0') {
1802                 orientation = atoi(orient_str);
1803                 switch(orientation) {
1804                         /* See EXIF standard for different values */
1805                         case 1: break;
1806                         case 2: flip_horiz = 1;
1807                                 break;
1808                         case 3: angle = 180;
1809                                 break;
1810                         case 4: flip_vert = 1;
1811                                 break;
1812                         case 5: angle = 90;
1813                                 flip_horiz = 1;
1814                                 break;
1815                         case 6: angle = 270;
1816                                 break;
1817                         case 7: angle = 90;
1818                                 flip_vert = 1;
1819                                 break;
1820                         case 8: angle = 90;
1821                                 break;
1822                 }
1823         }
1824
1825         w = gdk_pixbuf_get_width(pixbuf);
1826         h = gdk_pixbuf_get_height(pixbuf);
1827
1828         if (angle == 90 || angle == 270) {
1829                 avail_height = box_width;
1830                 avail_width = box_height;
1831         } else {
1832                 avail_width = box_width;
1833                 avail_height = box_height;
1834         }
1835
1836         /* Scale first */
1837         if (box_width != -1 && box_height != -1 && avail_width - 100 > 0) {
1838                 if (w > avail_width) {
1839                         h = (avail_width * h) / w;
1840                         w = avail_width;
1841                 }
1842                 if (h > avail_height) {
1843                         w = (avail_height * w) / h;
1844                         h = avail_height;
1845                 }
1846                 t_pixbuf = gdk_pixbuf_scale_simple(pixbuf, 
1847                         w, h, GDK_INTERP_BILINEAR);
1848                 g_object_unref(pixbuf);
1849                 pixbuf = t_pixbuf;
1850         }
1851
1852         /* Rotate if needed */
1853         if (angle != 0) {
1854                 t_pixbuf = gdk_pixbuf_rotate_simple(pixbuf, angle);
1855                 g_object_unref(pixbuf);
1856                 pixbuf = t_pixbuf;
1857         }
1858
1859         /* Flip horizontally if needed */
1860         if (flip_horiz) {
1861                 t_pixbuf = gdk_pixbuf_flip(pixbuf, TRUE);
1862                 g_object_unref(pixbuf);
1863                 pixbuf = t_pixbuf;
1864         }
1865
1866         /* Flip vertically if needed */
1867         if (flip_vert) {
1868                 t_pixbuf = gdk_pixbuf_flip(pixbuf, FALSE);
1869                 g_object_unref(pixbuf);
1870                 pixbuf = t_pixbuf;
1871         }
1872
1873         return pixbuf;
1874 }