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