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