487c82ba72a66cddc61b7d8828d90e865e044665
[claws.git] / src / gtk / gtkutils.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 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         g_return_val_if_fail(GTK_IS_WIDGET(widget), FALSE);
70
71         layout = gtk_widget_create_pango_layout(widget, str);
72         g_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         g_return_if_fail(widget != NULL);
87         g_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         g_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         g_return_if_fail(bbox != NULL);
126
127         *help_btn = gtk_button_new_from_stock(GTK_STOCK_HELP);
128
129         GTK_WIDGET_SET_FLAGS(*help_btn, GTK_CAN_DEFAULT);
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         g_return_if_fail(bbox != NULL);
145         g_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         g_return_if_fail(bbox != NULL);
159         g_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         GTK_WIDGET_SET_FLAGS(*button1, GTK_CAN_DEFAULT);
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                 GTK_WIDGET_SET_FLAGS(*button2, GTK_CAN_DEFAULT);
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                 GTK_WIDGET_SET_FLAGS(*button3, GTK_CAN_DEFAULT);
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         g_return_if_fail(bbox != NULL);
191         g_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         GTK_WIDGET_SET_FLAGS(*button1, GTK_CAN_DEFAULT);
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                 GTK_WIDGET_SET_FLAGS(*button2, GTK_CAN_DEFAULT);
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                 GTK_WIDGET_SET_FLAGS(*button3, GTK_CAN_DEFAULT);
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         g_return_if_fail(ctree != NULL);
237         g_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         g_return_val_if_fail(ctree != NULL, -1);
275         g_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         g_return_val_if_fail(node != NULL, FALSE);
360         g_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         g_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         g_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         g_return_val_if_fail(text != NULL, FALSE);
550         g_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         g_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         g_return_val_if_fail(text != NULL, FALSE);
583         g_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         g_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         g_return_if_fail(window != NULL);
611         g_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 #if GTK_CHECK_VERSION(2,8,0)
624         gtk_window_present_with_time(GTK_WINDOW(window), time(NULL));
625 #else
626         gtk_window_present(GTK_WINDOW(window));
627 #endif
628 }
629
630 void gtkut_widget_get_uposition(GtkWidget *widget, gint *px, gint *py)
631 {
632         gint x, y;
633         gint sx, sy;
634
635         g_return_if_fail(widget != NULL);
636         g_return_if_fail(widget->window != NULL);
637
638         sx = gdk_screen_width();
639         sy = gdk_screen_height();
640
641         /* gdk_window_get_root_origin ever return *rootwindow*'s position */
642         gdk_window_get_root_origin(widget->window, &x, &y);
643
644         x %= sx; if (x < 0) x = 0;
645         y %= sy; if (y < 0) y = 0;
646         *px = x;
647         *py = y;
648 }
649
650 void gtkut_widget_draw_now(GtkWidget *widget)
651 {
652         if (widget && GTK_WIDGET_VISIBLE(widget) && GTK_WIDGET_DRAWABLE(widget))
653                 gdk_window_process_updates(widget->window, FALSE);
654 }
655
656 static void gtkut_clist_bindings_add(GtkWidget *clist)
657 {
658         GtkBindingSet *binding_set;
659
660         binding_set = gtk_binding_set_by_class
661                 (GTK_CMCLIST_GET_CLASS(clist));
662
663         gtk_binding_entry_add_signal(binding_set, GDK_n, GDK_CONTROL_MASK,
664                                      "scroll_vertical", 2,
665                                      G_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
666                                      G_TYPE_FLOAT, 0.0);
667         gtk_binding_entry_add_signal(binding_set, GDK_p, GDK_CONTROL_MASK,
668                                      "scroll_vertical", 2,
669                                      G_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
670                                      G_TYPE_FLOAT, 0.0);
671 }
672
673 void gtkut_widget_init(void)
674 {
675         GtkWidget *clist;
676
677         clist = gtk_cmclist_new(1);
678         g_object_ref(G_OBJECT(clist));
679 #if GLIB_CHECK_VERSION(2,10,0)
680         g_object_ref_sink (G_OBJECT(clist));
681 #else
682         gtk_object_ref (G_OBJECT(clist));
683         gtk_object_sink (G_OBJECT(clist));
684 #endif
685         gtkut_clist_bindings_add(clist);
686         g_object_unref(G_OBJECT(clist));
687
688         clist = gtk_cmctree_new(1, 0);
689         g_object_ref(G_OBJECT(clist));
690 #if GLIB_CHECK_VERSION(2,10,0)
691         g_object_ref_sink (G_OBJECT(clist));
692 #else
693         gtk_object_ref (G_OBJECT(clist));
694         gtk_object_sink (G_OBJECT(clist));
695 #endif
696         gtkut_clist_bindings_add(clist);
697         g_object_unref(G_OBJECT(clist));
698
699         clist = gtk_sctree_new_with_titles(1, 0, NULL);
700         g_object_ref(G_OBJECT(clist));
701 #if GLIB_CHECK_VERSION(2,10,0)
702         g_object_ref_sink (G_OBJECT(clist));
703 #else
704         gtk_object_ref (G_OBJECT(clist));
705         gtk_object_sink (G_OBJECT(clist));
706 #endif
707         gtkut_clist_bindings_add(clist);
708         g_object_unref(G_OBJECT(clist));
709 }
710
711 void gtkut_widget_set_app_icon(GtkWidget *widget)
712 {
713 #include "pixmaps/claws-mail.xpm"
714         static GdkPixmap *sylpheedclawsxpm;
715         static GdkBitmap *sylpheedclawsxpmmask;
716         
717         g_return_if_fail(widget != NULL);
718         g_return_if_fail(widget->window != NULL);
719         if (!sylpheedclawsxpm) {
720                 PIXMAP_CREATE(widget, sylpheedclawsxpm, sylpheedclawsxpmmask,
721                               claws_mail_xpm);
722         }               
723         gdk_window_set_icon(widget->window, NULL, sylpheedclawsxpm, sylpheedclawsxpmmask);
724 }
725
726 void gtkut_widget_set_composer_icon(GtkWidget *widget)
727 {
728         static GdkPixmap *xpm;
729         static GdkBitmap *bmp;
730
731         g_return_if_fail(widget != NULL);
732         g_return_if_fail(widget->window != NULL);
733         if (!xpm) {
734                 stock_pixmap_gdk(widget, STOCK_PIXMAP_MAIL_COMPOSE, &xpm, &bmp);
735         }
736         gdk_window_set_icon(widget->window, NULL, xpm, bmp);    
737 }
738
739 static gboolean move_bar = FALSE;
740 static gint move_bar_id = -1;
741
742 static gboolean move_bar_cb(gpointer data)
743 {
744         GtkWidget *w = (GtkWidget *)data;
745         if (!move_bar)
746                 return FALSE;
747
748         if (!GTK_IS_PROGRESS_BAR(w)) {
749                 return FALSE;
750         }
751
752         gtk_progress_bar_pulse(GTK_PROGRESS_BAR(w));
753         GTK_EVENTS_FLUSH();
754         return TRUE;
755 }
756
757 GtkWidget *label_window_create(const gchar *str)
758 {
759         GtkWidget *window;
760         GtkWidget *label, *vbox, *hbox;
761         GtkWidget *wait_progress = gtk_progress_bar_new();
762
763         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "gtkutils");
764         gtk_widget_set_size_request(window, 380, 70);
765         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
766         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
767         gtk_window_set_title(GTK_WINDOW(window), str);
768         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
769         gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
770         manage_window_set_transient(GTK_WINDOW(window));
771
772         label = gtk_label_new(str);
773         
774         vbox = gtk_vbox_new(FALSE, 6);
775         hbox = gtk_hbox_new(FALSE, 6);
776         gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, FALSE, 0);
777         gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, FALSE, 0);
778         hbox = gtk_hbox_new(FALSE, 6);
779         gtk_box_pack_start(GTK_BOX(hbox), wait_progress, TRUE, FALSE, 0);
780         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
781         
782         gtk_container_add(GTK_CONTAINER(window), vbox);
783         gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
784         gtk_misc_set_alignment(GTK_MISC(label), 0.5, 0.5);
785         gtk_widget_show_all(vbox);
786
787         gtk_widget_show_now(window);
788         
789         if (move_bar_id == -1) {
790                 move_bar_id = g_timeout_add(200, move_bar_cb, wait_progress);
791                 move_bar = TRUE;
792         }
793
794         GTK_EVENTS_FLUSH();
795
796         return window;
797 }
798
799 void label_window_destroy(GtkWidget *window)
800 {
801         move_bar = FALSE;
802         g_source_remove(move_bar_id);
803         move_bar_id = -1;
804         GTK_EVENTS_FLUSH();
805         gtk_widget_destroy(window);     
806 }
807
808 GtkWidget *gtkut_account_menu_new(GList                 *ac_list,
809                                         GCallback               callback,
810                                   gpointer              data)
811 {
812         GList *cur_ac;
813         GtkWidget *optmenu;
814         GtkListStore *menu;
815         GtkTreeIter iter;
816         PrefsAccount *account;
817         gchar *name;
818         
819         g_return_val_if_fail(ac_list != NULL, NULL);
820
821         optmenu = gtkut_sc_combobox_create(NULL, FALSE);
822         menu = GTK_LIST_STORE(gtk_combo_box_get_model(GTK_COMBO_BOX(optmenu)));
823
824         for (cur_ac = ac_list; cur_ac != NULL; cur_ac = cur_ac->next) {
825                 account = (PrefsAccount *) cur_ac->data;
826                 if (account->name)
827                         name = g_strdup_printf("%s: %s <%s>",
828                                                account->account_name,
829                                                account->name,
830                                                account->address);
831                 else
832                         name = g_strdup_printf("%s: %s",
833                                                account->account_name,
834                                                account->address);
835                 COMBOBOX_ADD_ESCAPED(menu, name, account->account_id);
836                 g_free(name);
837         }
838         gtk_combo_box_set_active(GTK_COMBO_BOX(optmenu), 0);
839
840         if( callback != NULL )
841                 g_signal_connect(G_OBJECT(optmenu), "changed", callback, data);
842
843         return optmenu;
844 }
845
846 void gtkut_set_widget_bgcolor_rgb(GtkWidget *widget, guint rgbvalue)
847 {
848         GtkStyle *newstyle;
849         GdkColor gdk_color;
850
851         gtkut_convert_int_to_gdk_color(rgbvalue, &gdk_color);
852         newstyle = gtk_style_copy(gtk_widget_get_default_style());
853         newstyle->bg[GTK_STATE_NORMAL]   = gdk_color;
854         newstyle->bg[GTK_STATE_PRELIGHT] = gdk_color;
855         newstyle->bg[GTK_STATE_ACTIVE]   = gdk_color;
856         gtk_widget_set_style(widget, newstyle);
857 }
858   
859 /*!
860  *\brief        Tries to find a focused child using a lame strategy
861  */
862 GtkWidget *gtkut_get_focused_child(GtkContainer *parent)
863 {
864         GtkWidget *result = NULL;
865         GList *child_list = NULL;
866         GList *c;
867
868         g_return_val_if_fail(parent, NULL);
869
870         /* Get children list and see which has the focus. */
871         child_list = gtk_container_get_children(parent);
872         if (!child_list)
873                 return NULL;
874
875         for (c = child_list; c != NULL; c = g_list_next(c)) {
876                 if (c->data && GTK_IS_WIDGET(c->data)) {
877                         if (GTK_WIDGET_HAS_FOCUS(GTK_WIDGET(c->data))) {
878                                 result = GTK_WIDGET(c->data);
879                                 break;
880                         }
881                 }
882         }
883         
884         /* See if the returned widget is a container itself; if it is,
885          * see if one of its children is focused. If the focused 
886          * container has no focused child, it is itself a focusable 
887          * child, and has focus. */
888         if (result && GTK_IS_CONTAINER(result)) {
889                 GtkWidget *tmp =  gtkut_get_focused_child(GTK_CONTAINER(result)); 
890                 
891                 if (tmp) 
892                         result = tmp;
893         } else {
894                 /* Try the same for each container in the chain */
895                 for (c = child_list; c != NULL && !result; c = g_list_next(c)) {
896                         if (c->data && GTK_IS_WIDGET(c->data) 
897                         &&  GTK_IS_CONTAINER(c->data)) {
898                                 result = gtkut_get_focused_child
899                                         (GTK_CONTAINER(c->data));
900                         }
901                 }
902         
903         }
904         
905         g_list_free(child_list);
906                 
907         return result;
908 }
909
910 /*!
911  *\brief        Create a Browse (file) button based on GTK+ stock
912  */
913 GtkWidget *gtkut_get_browse_file_btn(const gchar *button_label)
914 {
915         GtkWidget *button;
916
917         button = gtk_button_new_with_mnemonic(button_label);
918         gtk_button_set_image(GTK_BUTTON(button),
919                 gtk_image_new_from_stock(GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_BUTTON));
920
921         return button;
922 }
923
924 /*!
925  *\brief        Create a Browse (directory) button based on GTK+ stock
926  */
927 GtkWidget *gtkut_get_browse_directory_btn(const gchar *button_label)
928 {
929         GtkWidget *button;
930
931         button = gtk_button_new_with_mnemonic(button_label);
932         gtk_button_set_image(GTK_BUTTON(button),
933                 gtk_image_new_from_stock(GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_BUTTON));
934
935         return button;
936 }
937
938 GtkWidget *gtkut_get_replace_btn(const gchar *button_label)
939 {
940         GtkWidget *button;
941
942         button = gtk_button_new_with_mnemonic(button_label);
943         gtk_button_set_image(GTK_BUTTON(button),
944                 gtk_image_new_from_stock(GTK_STOCK_REFRESH, GTK_ICON_SIZE_BUTTON));
945
946         return button;
947 }
948
949 /**
950  * merge some part of code into one function : it creates a frame and add
951  *      these into gtk box widget passed in param.
952  * \param box gtk box where adding new created frame.
953  * \param pframe pointer with which to assign the frame. If NULL, no pointer
954  *      is assigned but the frame is anyway created and added to @box.
955  * \param frame_label frame label of new created frame.
956  */
957 GtkWidget *gtkut_get_options_frame(GtkWidget *box, GtkWidget **pframe,
958                 const gchar *frame_label)
959 {
960         GtkWidget *vbox;
961         GtkWidget *frame;
962
963         frame = gtk_frame_new(frame_label);
964         gtk_widget_show(frame);
965         gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
966         gtk_frame_set_label_align(GTK_FRAME(frame), 0.01, 0.5);
967
968         vbox = gtk_vbox_new (FALSE, 4);
969         gtk_widget_show(vbox);
970         gtk_container_add(GTK_CONTAINER (frame), vbox);
971         gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
972
973         if (pframe != NULL)
974                 *pframe = frame;
975
976         return vbox;
977 }
978
979 #if HAVE_LIBCOMPFACE
980 static gint create_xpm_from_xface(gchar *xpm[], const gchar *xface)
981 {
982         static gchar *bit_pattern[] = {
983                 "....",
984                 "...#",
985                 "..#.",
986                 "..##",
987                 ".#..",
988                 ".#.#",
989                 ".##.",
990                 ".###",
991                 "#...",
992                 "#..#",
993                 "#.#.",
994                 "#.##",
995                 "##..",
996                 "##.#",
997                 "###.",
998                 "####"
999         };
1000
1001         static gchar *xface_header = "48 48 2 1";
1002         static gchar *xface_black  = "# c #000000";
1003         static gchar *xface_white  = ". c #ffffff";
1004
1005         gint i, line = 0;
1006         const guchar *p;
1007         gchar buf[WIDTH * 4 + 1];  /* 4 = strlen("0x0000") */
1008
1009         p = xface;
1010
1011         strcpy(xpm[line++], xface_header);
1012         strcpy(xpm[line++], xface_black);
1013         strcpy(xpm[line++], xface_white);
1014
1015         for (i = 0; i < HEIGHT; i++) {
1016                 gint col;
1017
1018                 buf[0] = '\0';
1019      
1020                 for (col = 0; col < 3; col++) {
1021                         gint figure;
1022
1023                         p += 2;  /* skip '0x' */
1024
1025                         for (figure = 0; figure < 4; figure++) {
1026                                 gint n = 0;
1027
1028                                 if ('0' <= *p && *p <= '9') {
1029                                         n = *p - '0';
1030                                 } else if ('a' <= *p && *p <= 'f') {
1031                                         n = *p - 'a' + 10;
1032                                 } else if ('A' <= *p && *p <= 'F') {
1033                                         n = *p - 'A' + 10;
1034                                 }
1035
1036                                 strcat(buf, bit_pattern[n]);
1037                                 p++;  /* skip ',' */
1038                         }
1039
1040                         p++;  /* skip '\n' */
1041                 }
1042
1043                 strcpy(xpm[line++], buf);
1044                 p++;
1045         }
1046
1047         return 0;
1048 }
1049 #endif
1050
1051 gboolean get_tag_range(GtkTextIter *iter,
1052                                        GtkTextTag *tag,
1053                                        GtkTextIter *start_iter,
1054                                        GtkTextIter *end_iter)
1055 {
1056         GtkTextIter _start_iter, _end_iter;
1057
1058         _end_iter = *iter;
1059         if (!gtk_text_iter_forward_to_tag_toggle(&_end_iter, tag)) {
1060                 debug_print("Can't find end");
1061                 return FALSE;
1062         }
1063
1064         _start_iter = _end_iter;
1065         if (!gtk_text_iter_backward_to_tag_toggle(&_start_iter, tag)) {
1066                 debug_print("Can't find start.");
1067                 return FALSE;
1068         }
1069
1070         *start_iter = _start_iter;
1071         *end_iter = _end_iter;
1072
1073         return TRUE;
1074 }
1075
1076 #if HAVE_LIBCOMPFACE
1077 GtkWidget *xface_get_from_header(const gchar *o_xface, GdkColor *background,
1078                                  GdkWindow *window)
1079 {
1080         static gchar *xpm_xface[XPM_XFACE_HEIGHT];
1081         static gboolean xpm_xface_init = TRUE;
1082         GdkPixmap *pixmap;
1083         GdkBitmap *mask;
1084         gchar xface[2048];
1085         strncpy(xface, o_xface, sizeof(xface));
1086
1087         if (!window) {
1088                 g_warning("no window\n");
1089                 return NULL;
1090         }
1091         if (uncompface(xface) < 0) {
1092                 g_warning("uncompface failed\n");
1093                 return NULL;
1094         }
1095
1096         if (xpm_xface_init) {
1097                 gint i;
1098
1099                 for (i = 0; i < XPM_XFACE_HEIGHT; i++) {
1100                         xpm_xface[i] = g_malloc(WIDTH + 1);
1101                         *xpm_xface[i] = '\0';
1102                 }
1103                 xpm_xface_init = FALSE;
1104         }
1105
1106         create_xpm_from_xface(xpm_xface, xface);
1107
1108         pixmap = gdk_pixmap_create_from_xpm_d
1109                 (window, &mask, 
1110                  background, xpm_xface);
1111         return gtk_image_new_from_pixbuf(
1112                 gdk_pixbuf_new_from_xpm_data((const char **)xpm_xface));
1113 }
1114 #endif
1115
1116 GtkWidget *face_get_from_header(const gchar *o_face)
1117 {
1118         gchar face[2048];
1119         gchar face_png[2048];
1120         gint pngsize;
1121         GdkPixbuf *pixbuf;
1122         GError *error = NULL;
1123         GdkPixbufLoader *loader = gdk_pixbuf_loader_new ();
1124         GtkWidget *image;
1125         
1126         if (o_face == NULL || strlen(o_face) == 0)
1127                 return NULL;
1128
1129         strncpy2(face, o_face, sizeof(face));
1130
1131         unfold_line(face); /* strip all whitespace and linebreaks */
1132         remove_space(face);
1133
1134         pngsize = base64_decode(face_png, face, strlen(face));
1135
1136         if (!gdk_pixbuf_loader_write (loader, face_png, pngsize, &error) ||
1137             !gdk_pixbuf_loader_close (loader, &error)) {
1138                 g_warning("loading face failed\n");
1139                 g_object_unref(loader);
1140                 return NULL;
1141         }
1142
1143         pixbuf = g_object_ref(gdk_pixbuf_loader_get_pixbuf(loader));
1144
1145         g_object_unref(loader);
1146
1147         if ((gdk_pixbuf_get_width(pixbuf) != 48) || (gdk_pixbuf_get_height(pixbuf) != 48)) {
1148                 g_object_unref(pixbuf);
1149                 g_warning("wrong_size");
1150                 return NULL;
1151         }
1152
1153         image = gtk_image_new_from_pixbuf(pixbuf);
1154         g_object_unref(pixbuf);
1155         return image;
1156 }
1157
1158 static GdkCursor *hand_cursor = NULL;
1159
1160 static void link_btn_enter(GtkButton *button, gpointer data)
1161 {
1162         GtkWidget *window = (GtkWidget *)data;
1163
1164         if (!hand_cursor)
1165                 hand_cursor = gdk_cursor_new(GDK_HAND2);
1166         if (window && window->window)
1167                 gdk_window_set_cursor(window->window, hand_cursor);
1168
1169         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1170         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1171         
1172 }
1173
1174 static void link_btn_leave(GtkButton *button, gpointer data)
1175 {
1176         GtkWidget *window = (GtkWidget *)data;
1177
1178         if (window && window->window)
1179                 gdk_window_set_cursor(window->window, NULL);
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_pressed(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_released(GtkButton *button, gpointer data)
1192 {
1193         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1194         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1195 }
1196
1197 static void link_btn_clicked(GtkButton *button, gpointer data)
1198 {
1199         gchar *url = (gchar *)data;
1200         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1201         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1202         open_uri(url, prefs_common_get_uri_cmd());
1203 }
1204
1205 static void link_btn_unrealize(GtkButton *button, gpointer data)
1206 {
1207         gchar *url = (gchar *)data;
1208         g_signal_handlers_disconnect_by_func(G_OBJECT(button), 
1209                          G_CALLBACK(link_btn_clicked), url);
1210         g_free(url);
1211 }
1212
1213 GtkWidget *gtkut_get_link_btn(GtkWidget *window, const gchar *url, const gchar *label)
1214 {
1215         GtkWidget *btn;
1216         GtkWidget *btn_label;
1217         GdkColormap *cmap;
1218         GdkColor uri_color[2] = {{0, 0, 0, 0xffff}, {0, 0xffff, 0, 0}};
1219         gboolean success[2];
1220         gchar *local_url = NULL;
1221         if (!url)
1222                 return NULL;
1223
1224         gtkut_convert_int_to_gdk_color(prefs_common.uri_col,
1225                                                &uri_color[0]);
1226         gtkut_convert_int_to_gdk_color(prefs_common.uri_col,
1227                                                &uri_color[1]);
1228
1229         btn = gtk_button_new_with_label(label?label:url);
1230         gtk_button_set_relief(GTK_BUTTON(btn), GTK_RELIEF_NONE);
1231         btn_label = gtk_bin_get_child(GTK_BIN((btn)));
1232         cmap = gdk_drawable_get_colormap(window->window);
1233         gdk_colormap_alloc_colors(cmap, uri_color, 2, FALSE, TRUE, success);
1234         if (success[0] == TRUE && success[1] == TRUE) {
1235                 GtkStyle *style;
1236                 gtk_widget_ensure_style(btn_label);
1237                 style = gtk_style_copy
1238                         (gtk_widget_get_style(btn_label));
1239                 style->fg[GTK_STATE_NORMAL]   = uri_color[0];
1240                 style->fg[GTK_STATE_ACTIVE]   = uri_color[1];
1241                 style->fg[GTK_STATE_PRELIGHT] = uri_color[0];
1242                 gtk_widget_set_style(btn_label, style);
1243         } else
1244                 g_warning("about_create(): color allocation failed.\n");
1245
1246         g_signal_connect(G_OBJECT(btn), "enter",
1247                          G_CALLBACK(link_btn_enter), window);
1248         g_signal_connect(G_OBJECT(btn), "leave",
1249                          G_CALLBACK(link_btn_leave), window);
1250         g_signal_connect(G_OBJECT(btn), "pressed",
1251                          G_CALLBACK(link_btn_pressed), window);
1252         g_signal_connect(G_OBJECT(btn), "released",
1253                          G_CALLBACK(link_btn_released), window);
1254                          
1255         local_url = g_strdup(url);
1256         g_signal_connect(G_OBJECT(btn), "clicked",
1257                          G_CALLBACK(link_btn_clicked), local_url);
1258         g_signal_connect(G_OBJECT(btn), "unrealize",
1259                          G_CALLBACK(link_btn_unrealize), local_url);
1260         return btn;
1261 }
1262
1263 static gboolean _combobox_separator_func(GtkTreeModel *model,
1264                 GtkTreeIter *iter, gpointer data)
1265 {
1266         gchar *txt = NULL;
1267
1268         g_return_val_if_fail(model != NULL, FALSE);
1269
1270         gtk_tree_model_get(model, iter, COMBOBOX_TEXT, &txt, -1);
1271
1272         if( txt == NULL )
1273                 return TRUE;
1274         return FALSE;
1275 }
1276
1277 GtkWidget *gtkut_sc_combobox_create(GtkWidget *eventbox, gboolean focus_on_click)
1278 {
1279         GtkWidget *combobox;
1280         GtkListStore *menu;
1281         GtkCellRenderer *rend;
1282
1283         menu = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_INT, G_TYPE_BOOLEAN);
1284
1285         combobox = gtk_combo_box_new_with_model(GTK_TREE_MODEL(menu));
1286
1287         rend = gtk_cell_renderer_text_new();
1288         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox), rend, TRUE);
1289         gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combobox), rend,
1290                         "markup", COMBOBOX_TEXT,
1291                         "sensitive", COMBOBOX_SENS,
1292                         NULL);
1293
1294         if( eventbox != NULL )
1295                 gtk_container_add(GTK_CONTAINER(eventbox), combobox);
1296 #if GTK_CHECK_VERSION(2,6,0)
1297         gtk_combo_box_set_focus_on_click(GTK_COMBO_BOX(combobox), focus_on_click);
1298 #endif
1299
1300         gtk_combo_box_set_row_separator_func(GTK_COMBO_BOX(combobox),
1301                         (GtkTreeViewRowSeparatorFunc)_combobox_separator_func, NULL, NULL);
1302
1303         return combobox;
1304 }
1305
1306 static void gtkutils_smooth_scroll_do(GtkWidget *widget, GtkAdjustment *vadj,
1307                                       gfloat old_value, gfloat last_value,
1308                                       gint step)
1309 {
1310         gint change_value;
1311         gboolean up;
1312         gint i;
1313
1314         if (old_value < last_value) {
1315                 change_value = last_value - old_value;
1316                 up = FALSE;
1317         } else {
1318                 change_value = old_value - last_value;
1319                 up = TRUE;
1320         }
1321
1322         for (i = step; i <= change_value; i += step) {
1323                 vadj->value = old_value + (up ? -i : i);
1324                 g_signal_emit_by_name(G_OBJECT(vadj),
1325                                       "value_changed", 0);
1326         }
1327
1328         vadj->value = last_value;
1329         g_signal_emit_by_name(G_OBJECT(vadj), "value_changed", 0);
1330
1331         gtk_widget_queue_draw(widget);
1332 }
1333
1334 static gboolean gtkutils_smooth_scroll_page(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1335 {
1336         gfloat upper;
1337         gfloat page_incr;
1338         gfloat old_value;
1339         gfloat last_value;
1340
1341         if (prefs_common.scroll_halfpage)
1342                 page_incr = vadj->page_increment / 2;
1343         else
1344                 page_incr = vadj->page_increment;
1345
1346         if (!up) {
1347                 upper = vadj->upper - vadj->page_size;
1348                 if (vadj->value < upper) {
1349                         old_value = vadj->value;
1350                         last_value = vadj->value + page_incr;
1351                         last_value = MIN(last_value, upper);
1352
1353                         gtkutils_smooth_scroll_do(widget, vadj, old_value,
1354                                                   last_value,
1355                                                   prefs_common.scroll_step);
1356                 } else
1357                         return FALSE;
1358         } else {
1359                 if (vadj->value > 0.0) {
1360                         old_value = vadj->value;
1361                         last_value = vadj->value - page_incr;
1362                         last_value = MAX(last_value, 0.0);
1363
1364                         gtkutils_smooth_scroll_do(widget, vadj, old_value,
1365                                                   last_value,
1366                                                   prefs_common.scroll_step);
1367                 } else
1368                         return FALSE;
1369         }
1370
1371         return TRUE;
1372 }
1373
1374 gboolean gtkutils_scroll_page(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1375 {
1376         gfloat upper;
1377         gfloat page_incr;
1378
1379         if (prefs_common.enable_smooth_scroll)
1380                 return gtkutils_smooth_scroll_page(widget, vadj, up);
1381
1382         if (prefs_common.scroll_halfpage)
1383                 page_incr = vadj->page_increment / 2;
1384         else
1385                 page_incr = vadj->page_increment;
1386
1387         if (!up) {
1388                 upper = vadj->upper - vadj->page_size;
1389                 if (vadj->value < upper) {
1390                         vadj->value += page_incr;
1391                         vadj->value = MIN(vadj->value, upper);
1392                         g_signal_emit_by_name(G_OBJECT(vadj),
1393                                               "value_changed", 0);
1394                 } else
1395                         return FALSE;
1396         } else {
1397                 if (vadj->value > 0.0) {
1398                         vadj->value -= page_incr;
1399                         vadj->value = MAX(vadj->value, 0.0);
1400                         g_signal_emit_by_name(G_OBJECT(vadj),
1401                                               "value_changed", 0);
1402                 } else
1403                         return FALSE;
1404         }
1405         return TRUE;
1406 }
1407
1408 static void gtkutils_smooth_scroll_one_line(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1409 {
1410         gfloat upper;
1411         gfloat old_value;
1412         gfloat last_value;
1413
1414         if (!up) {
1415                 upper = vadj->upper - vadj->page_size;
1416                 if (vadj->value < upper) {
1417                         old_value = vadj->value;
1418                         last_value = vadj->value + vadj->step_increment;
1419                         last_value = MIN(last_value, upper);
1420
1421                         gtkutils_smooth_scroll_do(widget, vadj, old_value,
1422                                                   last_value,
1423                                                   prefs_common.scroll_step);
1424                 }
1425         } else {
1426                 if (vadj->value > 0.0) {
1427                         old_value = vadj->value;
1428                         last_value = vadj->value - vadj->step_increment;
1429                         last_value = MAX(last_value, 0.0);
1430
1431                         gtkutils_smooth_scroll_do(widget, vadj, old_value,
1432                                                   last_value,
1433                                                   prefs_common.scroll_step);
1434                 }
1435         }
1436 }
1437
1438 void gtkutils_scroll_one_line(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1439 {
1440         gfloat upper;
1441
1442         if (prefs_common.enable_smooth_scroll) {
1443                 gtkutils_smooth_scroll_one_line(widget, vadj, up);
1444                 return;
1445         }
1446
1447         if (!up) {
1448                 upper = vadj->upper - vadj->page_size;
1449                 if (vadj->value < upper) {
1450                         vadj->value += vadj->step_increment;
1451                         vadj->value = MIN(vadj->value, upper);
1452                         g_signal_emit_by_name(G_OBJECT(vadj),
1453                                               "value_changed", 0);
1454                 }
1455         } else {
1456                 if (vadj->value > 0.0) {
1457                         vadj->value -= vadj->step_increment;
1458                         vadj->value = MAX(vadj->value, 0.0);
1459                         g_signal_emit_by_name(G_OBJECT(vadj),
1460                                               "value_changed", 0);
1461                 }
1462         }
1463 }
1464
1465 gboolean gtkut_tree_model_text_iter_prev(GtkTreeModel *model,
1466                                  GtkTreeIter *iter,
1467                                  const gchar* text)
1468 /* do the same as gtk_tree_model_iter_next, but _prev instead.
1469    to use with widgets with one text column (gtk_combo_box_new_text()
1470    and with GtkComboBoxEntry's for instance),
1471 */
1472 {
1473         GtkTreeIter cur_iter;
1474         gchar *cur_value;
1475         gboolean valid;
1476         gint count;
1477
1478         g_return_val_if_fail(model != NULL, FALSE);
1479         g_return_val_if_fail(iter != NULL, FALSE);
1480
1481         if (text == NULL || *text == '\0')
1482                 return FALSE;
1483
1484         valid = gtk_tree_model_get_iter_first(model, &cur_iter);
1485         count = 0;
1486         while (valid) {
1487                 gtk_tree_model_get(model, &cur_iter, 0, &cur_value, -1);
1488
1489                 if (strcmp(text, cur_value) == 0) {
1490                         if (count <= 0)
1491                                 return FALSE;
1492
1493                         return gtk_tree_model_iter_nth_child(model, iter, NULL, count - 1);
1494                 }
1495
1496                 valid = gtk_tree_model_iter_next(model, &cur_iter);
1497                 count++;
1498         }
1499         return FALSE;           
1500 }
1501
1502 gboolean gtkut_tree_model_get_iter_last(GtkTreeModel *model,
1503                                  GtkTreeIter *iter)
1504 /* do the same as gtk_tree_model_get_iter_first, but _last instead.
1505 */
1506 {
1507         gint count;
1508
1509         g_return_val_if_fail(model != NULL, FALSE);
1510         g_return_val_if_fail(iter != NULL, FALSE);
1511
1512         count = gtk_tree_model_iter_n_children(model, NULL);
1513
1514         if (count <= 0)
1515                 return FALSE;
1516
1517         return gtk_tree_model_iter_nth_child(model, iter, NULL, count - 1);
1518 }
1519
1520 GtkWidget *gtkut_window_new             (GtkWindowType   type,
1521                                          const gchar    *class)
1522 {
1523 #ifndef MAEMO
1524         GtkWidget *window = gtk_window_new(type);
1525 #else
1526         GtkWidget *window = hildon_window_new();
1527         hildon_program_add_window(hildon_program, HILDON_WINDOW(window));
1528 #endif
1529         gtk_window_set_role(GTK_WINDOW(window), class);
1530         return window;
1531 }
1532
1533 static gboolean gtkut_tree_iter_comp(GtkTreeModel *model, 
1534                                      GtkTreeIter *iter1, 
1535                                      GtkTreeIter *iter2)
1536 {
1537         GtkTreePath *path1 = gtk_tree_model_get_path(model, iter1);
1538         GtkTreePath *path2 = gtk_tree_model_get_path(model, iter2);
1539         gboolean result;
1540
1541         result = gtk_tree_path_compare(path1, path2) == 0;
1542
1543         gtk_tree_path_free(path1);
1544         gtk_tree_path_free(path2);
1545         
1546         return result;
1547 }
1548
1549 /*!
1550  *\brief        Get selected row number.
1551  */
1552 gint gtkut_list_view_get_selected_row(GtkWidget *list_view)
1553 {
1554         GtkTreeView *view = GTK_TREE_VIEW(list_view);
1555         GtkTreeModel *model = gtk_tree_view_get_model(view);
1556         int n_rows = gtk_tree_model_iter_n_children(model, NULL);
1557         GtkTreeSelection *selection;
1558         GtkTreeIter iter;
1559         int row;
1560
1561         if (n_rows == 0) 
1562                 return -1;
1563         
1564         selection = gtk_tree_view_get_selection(view);
1565         if (!gtk_tree_selection_get_selected(selection, &model, &iter))
1566                 return -1;
1567         
1568         /* get all iterators and compare them... */
1569         for (row = 0; row < n_rows; row++) {
1570                 GtkTreeIter itern;
1571
1572                 gtk_tree_model_iter_nth_child(model, &itern, NULL, row);
1573                 if (gtkut_tree_iter_comp(model, &iter, &itern))
1574                         return row;
1575         }
1576         
1577         return -1;
1578 }
1579
1580 /*!
1581  *\brief        Select a row by its number.
1582  */
1583 gboolean gtkut_list_view_select_row(GtkWidget *list, gint row)
1584 {
1585         GtkTreeView *list_view = GTK_TREE_VIEW(list);
1586         GtkTreeSelection *selection = gtk_tree_view_get_selection(list_view);
1587         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1588         GtkTreeIter iter;
1589         GtkTreePath *path;
1590
1591         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
1592                 return FALSE;
1593         
1594         gtk_tree_selection_select_iter(selection, &iter);
1595
1596         path = gtk_tree_model_get_path(model, &iter);
1597         gtk_tree_view_set_cursor(list_view, path, NULL, FALSE);
1598         gtk_tree_path_free(path);
1599         
1600         return TRUE;
1601 }
1602
1603 static GtkUIManager *gui_manager = NULL;
1604
1605 GtkUIManager *gtkut_create_ui_manager(void)
1606 {
1607         g_return_val_if_fail(gui_manager == NULL, gui_manager);
1608         return (gui_manager = gtk_ui_manager_new());
1609 }
1610
1611 GtkUIManager *gtkut_ui_manager(void)
1612 {
1613         return gui_manager;
1614 }
1615
1616 #define READ_CONDITION (G_IO_IN | G_IO_HUP | G_IO_ERR)
1617 #define WRITE_CONDITION (G_IO_OUT | G_IO_ERR)
1618 #define EXCEPTION_CONDITION (G_IO_PRI)
1619 typedef struct _ClawsIOClosure ClawsIOClosure;
1620
1621 struct _ClawsIOClosure
1622 {
1623   GdkInputFunction function;
1624   GdkInputCondition condition;
1625   GDestroyNotify notify;
1626   gpointer data;
1627 };
1628
1629 static gboolean  
1630 claws_io_invoke (GIOChannel   *source,
1631                GIOCondition  condition,
1632                gpointer      data)
1633 {
1634   ClawsIOClosure *closure = data;
1635   GdkInputCondition gdk_cond = 0;
1636
1637   if (condition & READ_CONDITION)
1638     gdk_cond |= GDK_INPUT_READ;
1639   if (condition & WRITE_CONDITION)
1640     gdk_cond |= GDK_INPUT_WRITE;
1641   if (condition & EXCEPTION_CONDITION)
1642     gdk_cond |= GDK_INPUT_EXCEPTION;
1643
1644   if (closure->condition & gdk_cond)
1645     closure->function (closure->data, g_io_channel_unix_get_fd (source), gdk_cond);
1646
1647   return TRUE;
1648 }
1649
1650 static void
1651 claws_io_destroy (gpointer data)
1652 {
1653   ClawsIOClosure *closure = data;
1654
1655   if (closure->notify)
1656     closure->notify (closure->data);
1657
1658   g_free (closure);
1659 }
1660
1661 gint
1662 claws_input_add    (gint              source,
1663                     GdkInputCondition condition,
1664                     GdkInputFunction  function,
1665                     gpointer          data)
1666 {
1667   guint result;
1668   ClawsIOClosure *closure = g_new (ClawsIOClosure, 1);
1669   GIOChannel *channel;
1670   GIOCondition cond = 0;
1671
1672   closure->function = function;
1673   closure->condition = condition;
1674   closure->notify = NULL;
1675   closure->data = data;
1676
1677   if (condition & GDK_INPUT_READ)
1678     cond |= READ_CONDITION;
1679   if (condition & GDK_INPUT_WRITE)
1680     cond |= WRITE_CONDITION;
1681   if (condition & GDK_INPUT_EXCEPTION)
1682     cond |= EXCEPTION_CONDITION;
1683
1684 #ifndef G_OS_WIN32
1685   channel = g_io_channel_unix_new (source);
1686 #else
1687   channel = g_io_channel_win32_new_fd(source);
1688 #endif
1689   result = g_io_add_watch_full (channel, G_PRIORITY_DEFAULT, cond, 
1690                                 claws_io_invoke,
1691                                 closure, claws_io_destroy);
1692   g_io_channel_unref (channel);
1693
1694   return result;
1695 }