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