2007-02-27 [colin] 2.8.0cvs5
[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 2 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, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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_present(GTK_WINDOW(window));
760 }
761
762 void gtkut_widget_get_uposition(GtkWidget *widget, gint *px, gint *py)
763 {
764         gint x, y;
765         gint sx, sy;
766
767         g_return_if_fail(widget != NULL);
768         g_return_if_fail(widget->window != NULL);
769
770         sx = gdk_screen_width();
771         sy = gdk_screen_height();
772
773         /* gdk_window_get_root_origin ever return *rootwindow*'s position */
774         gdk_window_get_root_origin(widget->window, &x, &y);
775
776         x %= sx; if (x < 0) x = 0;
777         y %= sy; if (y < 0) y = 0;
778         *px = x;
779         *py = y;
780 }
781
782 void gtkut_widget_draw_now(GtkWidget *widget)
783 {
784         if (GTK_WIDGET_VISIBLE(widget) && GTK_WIDGET_DRAWABLE(widget))
785                 gdk_window_process_updates(widget->window, FALSE);
786 }
787
788 static void gtkut_clist_bindings_add(GtkWidget *clist)
789 {
790         GtkBindingSet *binding_set;
791
792         binding_set = gtk_binding_set_by_class
793                 (GTK_CLIST_GET_CLASS(clist));
794
795         gtk_binding_entry_add_signal(binding_set, GDK_n, GDK_CONTROL_MASK,
796                                      "scroll_vertical", 2,
797                                      G_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
798                                      G_TYPE_FLOAT, 0.0);
799         gtk_binding_entry_add_signal(binding_set, GDK_p, GDK_CONTROL_MASK,
800                                      "scroll_vertical", 2,
801                                      G_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
802                                      G_TYPE_FLOAT, 0.0);
803 }
804
805 void gtkut_widget_init(void)
806 {
807         GtkWidget *clist;
808
809         clist = gtk_clist_new(1);
810         gtk_object_ref(GTK_OBJECT(clist));
811         gtk_object_sink(GTK_OBJECT(clist));
812         gtkut_clist_bindings_add(clist);
813         gtk_object_unref(GTK_OBJECT(clist));
814
815         clist = gtk_ctree_new(1, 0);
816         gtk_object_ref(GTK_OBJECT(clist));
817         gtk_object_sink(GTK_OBJECT(clist));
818         gtkut_clist_bindings_add(clist);
819         gtk_object_unref(GTK_OBJECT(clist));
820
821         clist = gtk_sctree_new_with_titles(1, 0, NULL);
822         gtk_object_ref(GTK_OBJECT(clist));
823         gtk_object_sink(GTK_OBJECT(clist));
824         gtkut_clist_bindings_add(clist);
825         gtk_object_unref(GTK_OBJECT(clist));
826 }
827
828 void gtkut_widget_set_app_icon(GtkWidget *widget)
829 {
830 #include "pixmaps/claws-mail.xpm"
831         static GdkPixmap *sylpheedclawsxpm;
832         static GdkBitmap *sylpheedclawsxpmmask;
833         
834         g_return_if_fail(widget != NULL);
835         g_return_if_fail(widget->window != NULL);
836         if (!sylpheedclawsxpm) {
837                 PIXMAP_CREATE(widget, sylpheedclawsxpm, sylpheedclawsxpmmask,
838                               claws_mail_xpm);
839         }               
840         gdk_window_set_icon(widget->window, NULL, sylpheedclawsxpm, sylpheedclawsxpmmask);
841 }
842
843 void gtkut_widget_set_composer_icon(GtkWidget *widget)
844 {
845         static GdkPixmap *xpm;
846         static GdkBitmap *bmp;
847
848         g_return_if_fail(widget != NULL);
849         g_return_if_fail(widget->window != NULL);
850         if (!xpm) {
851                 stock_pixmap_gdk(widget, STOCK_PIXMAP_MAIL_COMPOSE, &xpm, &bmp);
852         }
853         gdk_window_set_icon(widget->window, NULL, xpm, bmp);    
854 }
855
856 GtkWidget *label_window_create(const gchar *str)
857 {
858         GtkWidget *window;
859         GtkWidget *label;
860
861         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "gtkutils");
862         gtk_widget_set_size_request(window, 380, 60);
863         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
864         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
865         gtk_window_set_title(GTK_WINDOW(window), str);
866         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
867         gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
868         manage_window_set_transient(GTK_WINDOW(window));
869
870         label = gtk_label_new(str);
871         gtk_container_add(GTK_CONTAINER(window), label);
872         gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
873         gtk_widget_show(label);
874
875         gtk_widget_show_now(window);
876         
877         GTK_EVENTS_FLUSH();
878
879         return window;
880 }
881
882 GtkWidget *gtkut_account_menu_new(GList                 *ac_list,
883                                   GCallback              callback,
884                                   gpointer               data)
885 {
886         GList *cur_ac;
887         GtkWidget *menu;
888         
889         g_return_val_if_fail(ac_list != NULL, NULL);
890
891         menu = gtk_menu_new();
892
893         for (cur_ac = ac_list; cur_ac != NULL; cur_ac = cur_ac->next) {
894                 gchar *name;
895                 GtkWidget *menuitem;
896                 PrefsAccount *account;
897                 
898                 account = (PrefsAccount *) cur_ac->data;
899                 if (account->name)
900                         name = g_strdup_printf("%s: %s <%s>",
901                                                account->account_name,
902                                                account->name,
903                                                account->address);
904                 else
905                         name = g_strdup_printf("%s: %s",
906                                                account->account_name,
907                                                account->address);
908                 MENUITEM_ADD(menu, menuitem, name, account->account_id);
909                 g_free(name);
910                 if (callback != NULL)
911                         g_signal_connect(G_OBJECT(menuitem), "activate",
912                                          callback, data);
913         }
914         return menu;
915 }
916
917 void gtkut_set_widget_bgcolor_rgb(GtkWidget *widget, guint rgbvalue)
918 {
919         GtkStyle *newstyle;
920         GdkColor gdk_color;
921
922         gtkut_convert_int_to_gdk_color(rgbvalue, &gdk_color);
923         newstyle = gtk_style_copy(gtk_widget_get_default_style());
924         newstyle->bg[GTK_STATE_NORMAL]   = gdk_color;
925         newstyle->bg[GTK_STATE_PRELIGHT] = gdk_color;
926         newstyle->bg[GTK_STATE_ACTIVE]   = gdk_color;
927         gtk_widget_set_style(widget, newstyle);
928 }
929   
930 /*!
931  *\brief        Tries to find a focused child using a lame strategy
932  */
933 GtkWidget *gtkut_get_focused_child(GtkContainer *parent)
934 {
935         GtkWidget *result = NULL;
936         GList *child_list = NULL;
937         GList *c;
938
939         g_return_val_if_fail(parent, NULL);
940
941         /* Get children list and see which has the focus. */
942         child_list = gtk_container_get_children(parent);
943         if (!child_list)
944                 return NULL;
945
946         for (c = child_list; c != NULL; c = g_list_next(c)) {
947                 if (c->data && GTK_IS_WIDGET(c->data)) {
948                         if (GTK_WIDGET_HAS_FOCUS(GTK_WIDGET(c->data))) {
949                                 result = GTK_WIDGET(c->data);
950                                 break;
951                         }
952                 }
953         }
954         
955         /* See if the returned widget is a container itself; if it is,
956          * see if one of its children is focused. If the focused 
957          * container has no focused child, it is itself a focusable 
958          * child, and has focus. */
959         if (result && GTK_IS_CONTAINER(result)) {
960                 GtkWidget *tmp =  gtkut_get_focused_child(GTK_CONTAINER(result)); 
961                 
962                 if (tmp) 
963                         result = tmp;
964         } else {
965                 /* Try the same for each container in the chain */
966                 for (c = child_list; c != NULL && !result; c = g_list_next(c)) {
967                         if (c->data && GTK_IS_WIDGET(c->data) 
968                         &&  GTK_IS_CONTAINER(c->data)) {
969                                 result = gtkut_get_focused_child
970                                         (GTK_CONTAINER(c->data));
971                         }
972                 }
973         
974         }
975         
976         g_list_free(child_list);
977                 
978         return result;
979 }
980
981 /*!
982  *\brief        Create a Browse (file) button based on GTK+ stock
983  */
984 GtkWidget *gtkut_get_browse_file_btn(const gchar *button_label)
985 {
986         GtkWidget *button;
987
988         button = gtk_button_new_with_mnemonic(button_label);
989         gtk_button_set_image(GTK_BUTTON(button),
990                 gtk_image_new_from_stock(GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_BUTTON));
991
992         return button;
993 }
994
995 /*!
996  *\brief        Create a Browse (directory) button based on GTK+ stock
997  */
998 GtkWidget *gtkut_get_browse_directory_btn(const gchar *button_label)
999 {
1000         GtkWidget *button;
1001
1002         button = gtk_button_new_with_mnemonic(button_label);
1003         gtk_button_set_image(GTK_BUTTON(button),
1004                 gtk_image_new_from_stock(GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_BUTTON));
1005
1006         return button;
1007 }
1008
1009 GtkWidget *gtkut_get_replace_btn(const gchar *button_label)
1010 {
1011         GtkWidget *button;
1012
1013         button = gtk_button_new_with_mnemonic(button_label);
1014         gtk_button_set_image(GTK_BUTTON(button),
1015                 gtk_image_new_from_stock(GTK_STOCK_REFRESH, GTK_ICON_SIZE_BUTTON));
1016
1017         return button;
1018 }
1019
1020 /**
1021  * merge some part of code into one function : it creates a frame and add
1022  *      these into gtk box widget passed in param.
1023  * \param box gtk box where adding new created frame.
1024  * \param pframe pointer with which to assign the frame. If NULL, no pointer
1025  *      is assigned but the frame is anyway created and added to @box.
1026  * \param frame_label frame label of new created frame.
1027  */
1028 GtkWidget *gtkut_get_options_frame(GtkWidget *box, GtkWidget **pframe,
1029                 const gchar *frame_label)
1030 {
1031         GtkWidget *vbox;
1032         GtkWidget *frame;
1033
1034         frame = gtk_frame_new(frame_label);
1035         gtk_widget_show(frame);
1036         gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
1037         gtk_frame_set_label_align(GTK_FRAME(frame), 0.01, 0.5);
1038
1039         vbox = gtk_vbox_new (FALSE, 4);
1040         gtk_widget_show(vbox);
1041         gtk_container_add(GTK_CONTAINER (frame), vbox);
1042         gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
1043
1044         if (pframe != NULL)
1045                 *pframe = frame;
1046
1047         return vbox;
1048 }
1049
1050 #if HAVE_LIBCOMPFACE
1051 static gint create_xpm_from_xface(gchar *xpm[], const gchar *xface)
1052 {
1053         static gchar *bit_pattern[] = {
1054                 "....",
1055                 "...#",
1056                 "..#.",
1057                 "..##",
1058                 ".#..",
1059                 ".#.#",
1060                 ".##.",
1061                 ".###",
1062                 "#...",
1063                 "#..#",
1064                 "#.#.",
1065                 "#.##",
1066                 "##..",
1067                 "##.#",
1068                 "###.",
1069                 "####"
1070         };
1071
1072         static gchar *xface_header = "48 48 2 1";
1073         static gchar *xface_black  = "# c #000000";
1074         static gchar *xface_white  = ". c #ffffff";
1075
1076         gint i, line = 0;
1077         const guchar *p;
1078         gchar buf[WIDTH * 4 + 1];  /* 4 = strlen("0x0000") */
1079
1080         p = xface;
1081
1082         strcpy(xpm[line++], xface_header);
1083         strcpy(xpm[line++], xface_black);
1084         strcpy(xpm[line++], xface_white);
1085
1086         for (i = 0; i < HEIGHT; i++) {
1087                 gint col;
1088
1089                 buf[0] = '\0';
1090      
1091                 for (col = 0; col < 3; col++) {
1092                         gint figure;
1093
1094                         p += 2;  /* skip '0x' */
1095
1096                         for (figure = 0; figure < 4; figure++) {
1097                                 gint n = 0;
1098
1099                                 if ('0' <= *p && *p <= '9') {
1100                                         n = *p - '0';
1101                                 } else if ('a' <= *p && *p <= 'f') {
1102                                         n = *p - 'a' + 10;
1103                                 } else if ('A' <= *p && *p <= 'F') {
1104                                         n = *p - 'A' + 10;
1105                                 }
1106
1107                                 strcat(buf, bit_pattern[n]);
1108                                 p++;  /* skip ',' */
1109                         }
1110
1111                         p++;  /* skip '\n' */
1112                 }
1113
1114                 strcpy(xpm[line++], buf);
1115                 p++;
1116         }
1117
1118         return 0;
1119 }
1120 #endif
1121
1122 gboolean get_tag_range(GtkTextIter *iter,
1123                                        GtkTextTag *tag,
1124                                        GtkTextIter *start_iter,
1125                                        GtkTextIter *end_iter)
1126 {
1127         GtkTextIter _start_iter, _end_iter;
1128
1129         _end_iter = *iter;
1130         if (!gtk_text_iter_forward_to_tag_toggle(&_end_iter, tag)) {
1131                 debug_print("Can't find end");
1132                 return FALSE;
1133         }
1134
1135         _start_iter = _end_iter;
1136         if (!gtk_text_iter_backward_to_tag_toggle(&_start_iter, tag)) {
1137                 debug_print("Can't find start.");
1138                 return FALSE;
1139         }
1140
1141         *start_iter = _start_iter;
1142         *end_iter = _end_iter;
1143
1144         return TRUE;
1145 }
1146
1147 #if HAVE_LIBCOMPFACE
1148 GtkWidget *xface_get_from_header(const gchar *o_xface, GdkColor *background,
1149                                  GdkWindow *window)
1150 {
1151         static gchar *xpm_xface[XPM_XFACE_HEIGHT];
1152         static gboolean xpm_xface_init = TRUE;
1153         GdkPixmap *pixmap;
1154         GdkBitmap *mask;
1155         gchar xface[2048];
1156         strncpy(xface, o_xface, sizeof(xface));
1157
1158         if (!window) {
1159                 g_warning("no window\n");
1160                 return NULL;
1161         }
1162         if (uncompface(xface) < 0) {
1163                 g_warning("uncompface failed\n");
1164                 return NULL;
1165         }
1166
1167         if (xpm_xface_init) {
1168                 gint i;
1169
1170                 for (i = 0; i < XPM_XFACE_HEIGHT; i++) {
1171                         xpm_xface[i] = g_malloc(WIDTH + 1);
1172                         *xpm_xface[i] = '\0';
1173                 }
1174                 xpm_xface_init = FALSE;
1175         }
1176
1177         create_xpm_from_xface(xpm_xface, xface);
1178
1179         pixmap = gdk_pixmap_create_from_xpm_d
1180                 (window, &mask, 
1181                  background, xpm_xface);
1182         return gtk_image_new_from_pixmap(pixmap, mask);
1183 }
1184 #endif
1185
1186 GtkWidget *face_get_from_header(const gchar *o_face)
1187 {
1188         gchar face[2048];
1189         gchar face_png[2048];
1190         gint pngsize;
1191         GdkPixbuf *pixbuf;
1192         GError *error = NULL;
1193         GdkPixbufLoader *loader = gdk_pixbuf_loader_new ();
1194         GtkWidget *image;
1195         
1196         if (o_face == NULL || strlen(o_face) == 0)
1197                 return NULL;
1198
1199         strncpy2(face, o_face, sizeof(face));
1200
1201         unfold_line(face); /* strip all whitespace and linebreaks */
1202         remove_space(face);
1203
1204         pngsize = base64_decode(face_png, face, strlen(face));
1205
1206         if (!gdk_pixbuf_loader_write (loader, face_png, pngsize, &error) ||
1207             !gdk_pixbuf_loader_close (loader, &error)) {
1208                 g_warning("loading face failed\n");
1209                 g_object_unref(loader);
1210                 return NULL;
1211         }
1212
1213         pixbuf = g_object_ref(gdk_pixbuf_loader_get_pixbuf(loader));
1214
1215         g_object_unref(loader);
1216
1217         if ((gdk_pixbuf_get_width(pixbuf) != 48) || (gdk_pixbuf_get_height(pixbuf) != 48)) {
1218                 g_object_unref(pixbuf);
1219                 g_warning("wrong_size");
1220                 return NULL;
1221         }
1222
1223         image = gtk_image_new_from_pixbuf(pixbuf);
1224         g_object_unref(pixbuf);
1225         return image;
1226 }
1227
1228 static GdkCursor *hand_cursor = NULL;
1229
1230 static void link_btn_enter(GtkButton *button, gpointer data)
1231 {
1232         GtkWidget *window = (GtkWidget *)data;
1233
1234         if (!hand_cursor)
1235                 hand_cursor = gdk_cursor_new(GDK_HAND2);
1236         if (window && window->window)
1237                 gdk_window_set_cursor(window->window, hand_cursor);
1238
1239         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1240         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1241         
1242 }
1243
1244 static void link_btn_leave(GtkButton *button, gpointer data)
1245 {
1246         GtkWidget *window = (GtkWidget *)data;
1247
1248         if (window && window->window)
1249                 gdk_window_set_cursor(window->window, NULL);
1250
1251         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1252         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1253 }
1254
1255 static void link_btn_pressed(GtkButton *button, gpointer data)
1256 {
1257         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1258         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1259 }
1260
1261 static void link_btn_released(GtkButton *button, gpointer data)
1262 {
1263         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1264         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1265 }
1266
1267 static void link_btn_clicked(GtkButton *button, gpointer data)
1268 {
1269         gchar *url = (gchar *)data;
1270         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1271         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1272         open_uri(url, prefs_common.uri_cmd);
1273 }
1274
1275 static void link_btn_unrealize(GtkButton *button, gpointer data)
1276 {
1277         gchar *url = (gchar *)data;
1278         g_signal_handlers_disconnect_by_func(G_OBJECT(button), 
1279                          G_CALLBACK(link_btn_clicked), url);
1280         g_free(url);
1281 }
1282
1283 GtkWidget *gtkut_get_link_btn(GtkWidget *window, const gchar *url, const gchar *label)
1284 {
1285         GtkWidget *btn;
1286         GtkWidget *btn_label;
1287         GdkColormap *cmap;
1288         GdkColor uri_color[2] = {{0, 0, 0, 0xffff}, {0, 0xffff, 0, 0}};
1289         gboolean success[2];
1290         gchar *local_url = NULL;
1291         if (!url)
1292                 return NULL;
1293
1294         gtkut_convert_int_to_gdk_color(prefs_common.uri_col,
1295                                                &uri_color[0]);
1296         gtkut_convert_int_to_gdk_color(prefs_common.uri_col,
1297                                                &uri_color[1]);
1298
1299         btn = gtk_button_new_with_label(label?label:url);
1300         gtk_button_set_relief(GTK_BUTTON(btn), GTK_RELIEF_NONE);
1301         btn_label = GTK_BIN(btn)->child;
1302         cmap = gdk_drawable_get_colormap(window->window);
1303         gdk_colormap_alloc_colors(cmap, uri_color, 2, FALSE, TRUE, success);
1304         if (success[0] == TRUE && success[1] == TRUE) {
1305                 GtkStyle *style;
1306                 gtk_widget_ensure_style(btn_label);
1307                 style = gtk_style_copy
1308                         (gtk_widget_get_style(btn_label));
1309                 style->fg[GTK_STATE_NORMAL]   = uri_color[0];
1310                 style->fg[GTK_STATE_ACTIVE]   = uri_color[1];
1311                 style->fg[GTK_STATE_PRELIGHT] = uri_color[0];
1312                 gtk_widget_set_style(btn_label, style);
1313         } else
1314                 g_warning("about_create(): color allocation failed.\n");
1315
1316         g_signal_connect(G_OBJECT(btn), "enter",
1317                          G_CALLBACK(link_btn_enter), window);
1318         g_signal_connect(G_OBJECT(btn), "leave",
1319                          G_CALLBACK(link_btn_leave), window);
1320         g_signal_connect(G_OBJECT(btn), "pressed",
1321                          G_CALLBACK(link_btn_pressed), window);
1322         g_signal_connect(G_OBJECT(btn), "released",
1323                          G_CALLBACK(link_btn_released), window);
1324                          
1325         local_url = g_strdup(url);
1326         g_signal_connect(G_OBJECT(btn), "clicked",
1327                          G_CALLBACK(link_btn_clicked), local_url);
1328         g_signal_connect(G_OBJECT(btn), "unrealize",
1329                          G_CALLBACK(link_btn_unrealize), local_url);
1330         return btn;
1331 }
1332
1333 GtkWidget *gtkut_sc_combobox_create(GtkWidget *eventbox, gboolean focus_on_click)
1334 {
1335         GtkWidget *combobox;
1336         GtkListStore *menu;
1337         GtkCellRenderer *rend;
1338
1339         menu = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
1340
1341         combobox = gtk_combo_box_new_with_model(GTK_TREE_MODEL(menu));
1342
1343         rend = gtk_cell_renderer_text_new();
1344         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox), rend, TRUE);
1345         gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combobox), rend,
1346                         "markup", 0,
1347                         NULL);
1348
1349         if( eventbox != NULL )
1350                 gtk_container_add(GTK_CONTAINER(eventbox), combobox);
1351 #if GTK_CHECK_VERSION(2,6,0)
1352         gtk_combo_box_set_focus_on_click(GTK_COMBO_BOX(combobox), focus_on_click);
1353 #endif
1354         return combobox;
1355 }
1356
1357 static void gtkutils_smooth_scroll_do(GtkWidget *widget, GtkAdjustment *vadj,
1358                                       gfloat old_value, gfloat last_value,
1359                                       gint step)
1360 {
1361         gint change_value;
1362         gboolean up;
1363         gint i;
1364
1365         if (old_value < last_value) {
1366                 change_value = last_value - old_value;
1367                 up = FALSE;
1368         } else {
1369                 change_value = old_value - last_value;
1370                 up = TRUE;
1371         }
1372
1373         for (i = step; i <= change_value; i += step) {
1374                 vadj->value = old_value + (up ? -i : i);
1375                 g_signal_emit_by_name(G_OBJECT(vadj),
1376                                       "value_changed", 0);
1377         }
1378
1379         vadj->value = last_value;
1380         g_signal_emit_by_name(G_OBJECT(vadj), "value_changed", 0);
1381
1382         gtk_widget_queue_draw(widget);
1383 }
1384
1385 static gboolean gtkutils_smooth_scroll_page(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1386 {
1387         gfloat upper;
1388         gfloat page_incr;
1389         gfloat old_value;
1390         gfloat last_value;
1391
1392         if (prefs_common.scroll_halfpage)
1393                 page_incr = vadj->page_increment / 2;
1394         else
1395                 page_incr = vadj->page_increment;
1396
1397         if (!up) {
1398                 upper = vadj->upper - vadj->page_size;
1399                 if (vadj->value < upper) {
1400                         old_value = vadj->value;
1401                         last_value = vadj->value + page_incr;
1402                         last_value = MIN(last_value, upper);
1403
1404                         gtkutils_smooth_scroll_do(widget, vadj, old_value,
1405                                                   last_value,
1406                                                   prefs_common.scroll_step);
1407                 } else
1408                         return FALSE;
1409         } else {
1410                 if (vadj->value > 0.0) {
1411                         old_value = vadj->value;
1412                         last_value = vadj->value - page_incr;
1413                         last_value = MAX(last_value, 0.0);
1414
1415                         gtkutils_smooth_scroll_do(widget, vadj, old_value,
1416                                                   last_value,
1417                                                   prefs_common.scroll_step);
1418                 } else
1419                         return FALSE;
1420         }
1421
1422         return TRUE;
1423 }
1424
1425 gboolean gtkutils_scroll_page(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1426 {
1427         gfloat upper;
1428         gfloat page_incr;
1429
1430         if (prefs_common.enable_smooth_scroll)
1431                 return gtkutils_smooth_scroll_page(widget, vadj, up);
1432
1433         if (prefs_common.scroll_halfpage)
1434                 page_incr = vadj->page_increment / 2;
1435         else
1436                 page_incr = vadj->page_increment;
1437
1438         if (!up) {
1439                 upper = vadj->upper - vadj->page_size;
1440                 if (vadj->value < upper) {
1441                         vadj->value += page_incr;
1442                         vadj->value = MIN(vadj->value, upper);
1443                         g_signal_emit_by_name(G_OBJECT(vadj),
1444                                               "value_changed", 0);
1445                 } else
1446                         return FALSE;
1447         } else {
1448                 if (vadj->value > 0.0) {
1449                         vadj->value -= page_incr;
1450                         vadj->value = MAX(vadj->value, 0.0);
1451                         g_signal_emit_by_name(G_OBJECT(vadj),
1452                                               "value_changed", 0);
1453                 } else
1454                         return FALSE;
1455         }
1456         return TRUE;
1457 }
1458
1459 static void gtkutils_smooth_scroll_one_line(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1460 {
1461         gfloat upper;
1462         gfloat old_value;
1463         gfloat last_value;
1464
1465         if (!up) {
1466                 upper = vadj->upper - vadj->page_size;
1467                 if (vadj->value < upper) {
1468                         old_value = vadj->value;
1469                         last_value = vadj->value + vadj->step_increment;
1470                         last_value = MIN(last_value, upper);
1471
1472                         gtkutils_smooth_scroll_do(widget, vadj, old_value,
1473                                                   last_value,
1474                                                   prefs_common.scroll_step);
1475                 }
1476         } else {
1477                 if (vadj->value > 0.0) {
1478                         old_value = vadj->value;
1479                         last_value = vadj->value - vadj->step_increment;
1480                         last_value = MAX(last_value, 0.0);
1481
1482                         gtkutils_smooth_scroll_do(widget, vadj, old_value,
1483                                                   last_value,
1484                                                   prefs_common.scroll_step);
1485                 }
1486         }
1487 }
1488
1489 void gtkutils_scroll_one_line(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1490 {
1491         gfloat upper;
1492
1493         if (prefs_common.enable_smooth_scroll) {
1494                 gtkutils_smooth_scroll_one_line(widget, vadj, up);
1495                 return;
1496         }
1497
1498         if (!up) {
1499                 upper = vadj->upper - vadj->page_size;
1500                 if (vadj->value < upper) {
1501                         vadj->value += vadj->step_increment;
1502                         vadj->value = MIN(vadj->value, upper);
1503                         g_signal_emit_by_name(G_OBJECT(vadj),
1504                                               "value_changed", 0);
1505                 }
1506         } else {
1507                 if (vadj->value > 0.0) {
1508                         vadj->value -= vadj->step_increment;
1509                         vadj->value = MAX(vadj->value, 0.0);
1510                         g_signal_emit_by_name(G_OBJECT(vadj),
1511                                               "value_changed", 0);
1512                 }
1513         }
1514 }
1515
1516 gboolean gtkut_tree_model_text_iter_prev(GtkTreeModel *model,
1517                                  GtkTreeIter *iter,
1518                                  const gchar* text)
1519 /* do the same as gtk_tree_model_iter_next, but _prev instead.
1520    to use with widgets with one text column (gtk_combo_box_new_text()
1521    and with GtkComboBoxEntry's for instance),
1522 */
1523 {
1524         GtkTreeIter cur_iter;
1525         gchar *cur_value;
1526         gboolean valid;
1527         gint count;
1528
1529         g_return_val_if_fail(model != NULL, FALSE);
1530         g_return_val_if_fail(iter != NULL, FALSE);
1531
1532         if (text == NULL || *text == '\0')
1533                 return FALSE;
1534
1535         valid = gtk_tree_model_get_iter_first(model, &cur_iter);
1536         count = 0;
1537         while (valid) {
1538                 gtk_tree_model_get(model, &cur_iter, 0, &cur_value, -1);
1539
1540                 if (strcmp(text, cur_value) == 0) {
1541                         if (count <= 0)
1542                                 return FALSE;
1543
1544                         return gtk_tree_model_iter_nth_child(model, iter, NULL, count - 1);
1545                 }
1546
1547                 valid = gtk_tree_model_iter_next(model, &cur_iter);
1548                 count++;
1549         }
1550         return FALSE;           
1551 }
1552
1553 gboolean gtkut_tree_model_get_iter_last(GtkTreeModel *model,
1554                                  GtkTreeIter *iter)
1555 /* do the same as gtk_tree_model_get_iter_first, but _last instead.
1556 */
1557 {
1558         gint count;
1559
1560         g_return_val_if_fail(model != NULL, FALSE);
1561         g_return_val_if_fail(iter != NULL, FALSE);
1562
1563         count = gtk_tree_model_iter_n_children(model, NULL);
1564
1565         if (count <= 0)
1566                 return FALSE;
1567
1568         return gtk_tree_model_iter_nth_child(model, iter, NULL, count - 1);
1569 }
1570
1571 GtkWidget *gtkut_window_new             (GtkWindowType   type,
1572                                          const gchar    *class)
1573 {
1574         GtkWidget *window = gtk_window_new(type);
1575         gtk_window_set_role(GTK_WINDOW(window), class);
1576         return window;
1577 }