2006-12-01 [wwp] 2.6.0cvs77
[claws.git] / src / gtk / gtkutils.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 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         g_object_ref(G_OBJECT(clist));
811         gtk_object_sink(G_OBJECT(clist));
812         gtkut_clist_bindings_add(clist);
813         g_object_unref(G_OBJECT(clist));
814
815         clist = gtk_ctree_new(1, 0);
816         g_object_ref(G_OBJECT(clist));
817         gtk_object_sink(G_OBJECT(clist));
818         gtkut_clist_bindings_add(clist);
819         g_object_unref(G_OBJECT(clist));
820
821         clist = gtk_sctree_new_with_titles(1, 0, NULL);
822         g_object_ref(G_OBJECT(clist));
823         gtk_object_sink(G_OBJECT(clist));
824         gtkut_clist_bindings_add(clist);
825         g_object_unref(G_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 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
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 #if HAVE_LIBCOMPFACE
1021 gint create_xpm_from_xface(gchar *xpm[], const gchar *xface)
1022 {
1023         static gchar *bit_pattern[] = {
1024                 "....",
1025                 "...#",
1026                 "..#.",
1027                 "..##",
1028                 ".#..",
1029                 ".#.#",
1030                 ".##.",
1031                 ".###",
1032                 "#...",
1033                 "#..#",
1034                 "#.#.",
1035                 "#.##",
1036                 "##..",
1037                 "##.#",
1038                 "###.",
1039                 "####"
1040         };
1041
1042         static gchar *xface_header = "48 48 2 1";
1043         static gchar *xface_black  = "# c #000000";
1044         static gchar *xface_white  = ". c #ffffff";
1045
1046         gint i, line = 0;
1047         const guchar *p;
1048         gchar buf[WIDTH * 4 + 1];  /* 4 = strlen("0x0000") */
1049
1050         p = xface;
1051
1052         strcpy(xpm[line++], xface_header);
1053         strcpy(xpm[line++], xface_black);
1054         strcpy(xpm[line++], xface_white);
1055
1056         for (i = 0; i < HEIGHT; i++) {
1057                 gint col;
1058
1059                 buf[0] = '\0';
1060      
1061                 for (col = 0; col < 3; col++) {
1062                         gint figure;
1063
1064                         p += 2;  /* skip '0x' */
1065
1066                         for (figure = 0; figure < 4; figure++) {
1067                                 gint n = 0;
1068
1069                                 if ('0' <= *p && *p <= '9') {
1070                                         n = *p - '0';
1071                                 } else if ('a' <= *p && *p <= 'f') {
1072                                         n = *p - 'a' + 10;
1073                                 } else if ('A' <= *p && *p <= 'F') {
1074                                         n = *p - 'A' + 10;
1075                                 }
1076
1077                                 strcat(buf, bit_pattern[n]);
1078                                 p++;  /* skip ',' */
1079                         }
1080
1081                         p++;  /* skip '\n' */
1082                 }
1083
1084                 strcpy(xpm[line++], buf);
1085                 p++;
1086         }
1087
1088         return 0;
1089 }
1090 #endif
1091
1092 gboolean get_tag_range(GtkTextIter *iter,
1093                                        GtkTextTag *tag,
1094                                        GtkTextIter *start_iter,
1095                                        GtkTextIter *end_iter)
1096 {
1097         GtkTextIter _start_iter, _end_iter;
1098
1099         _end_iter = *iter;
1100         if (!gtk_text_iter_forward_to_tag_toggle(&_end_iter, tag)) {
1101                 debug_print("Can't find end");
1102                 return FALSE;
1103         }
1104
1105         _start_iter = _end_iter;
1106         if (!gtk_text_iter_backward_to_tag_toggle(&_start_iter, tag)) {
1107                 debug_print("Can't find start.");
1108                 return FALSE;
1109         }
1110
1111         *start_iter = _start_iter;
1112         *end_iter = _end_iter;
1113
1114         return TRUE;
1115 }
1116
1117 #if HAVE_LIBCOMPFACE
1118 GtkWidget *xface_get_from_header(const gchar *o_xface, GdkColor *background,
1119                                  GdkWindow *window)
1120 {
1121         static gchar *xpm_xface[XPM_XFACE_HEIGHT];
1122         static gboolean xpm_xface_init = TRUE;
1123         GdkPixmap *pixmap;
1124         GdkBitmap *mask;
1125         gchar xface[2048];
1126         strncpy(xface, o_xface, sizeof(xface));
1127
1128         if (!window) {
1129                 g_warning("no window\n");
1130                 return NULL;
1131         }
1132         if (uncompface(xface) < 0) {
1133                 g_warning("uncompface failed\n");
1134                 return NULL;
1135         }
1136
1137         if (xpm_xface_init) {
1138                 gint i;
1139
1140                 for (i = 0; i < XPM_XFACE_HEIGHT; i++) {
1141                         xpm_xface[i] = g_malloc(WIDTH + 1);
1142                         *xpm_xface[i] = '\0';
1143                 }
1144                 xpm_xface_init = FALSE;
1145         }
1146
1147         create_xpm_from_xface(xpm_xface, xface);
1148
1149         pixmap = gdk_pixmap_create_from_xpm_d
1150                 (window, &mask, 
1151                  background, xpm_xface);
1152         return gtk_image_new_from_pixmap(pixmap, mask);
1153 }
1154 #endif
1155
1156 GtkWidget *face_get_from_header(const gchar *o_face)
1157 {
1158         gchar face[2048];
1159         gchar face_png[2048];
1160         gint pngsize;
1161         GdkPixbuf *pixbuf;
1162         GError *error = NULL;
1163         GdkPixbufLoader *loader = gdk_pixbuf_loader_new ();
1164         GtkWidget *image;
1165         
1166         if (o_face == NULL || strlen(o_face) == 0)
1167                 return NULL;
1168
1169         strncpy2(face, o_face, sizeof(face));
1170
1171         unfold_line(face); /* strip all whitespace and linebreaks */
1172         remove_space(face);
1173
1174         pngsize = base64_decode(face_png, face, strlen(face));
1175
1176         if (!gdk_pixbuf_loader_write (loader, face_png, pngsize, &error) ||
1177             !gdk_pixbuf_loader_close (loader, &error)) {
1178                 g_warning("loading face failed\n");
1179                 g_object_unref(loader);
1180                 return NULL;
1181         }
1182
1183         pixbuf = g_object_ref(gdk_pixbuf_loader_get_pixbuf(loader));
1184
1185         g_object_unref(loader);
1186
1187         if ((gdk_pixbuf_get_width(pixbuf) != 48) || (gdk_pixbuf_get_height(pixbuf) != 48)) {
1188                 g_object_unref(pixbuf);
1189                 g_warning("wrong_size");
1190                 return NULL;
1191         }
1192
1193         image = gtk_image_new_from_pixbuf(pixbuf);
1194         g_object_unref(pixbuf);
1195         return image;
1196 }
1197
1198 static GdkCursor *hand_cursor = NULL;
1199
1200 static void link_btn_enter(GtkButton *button, gpointer data)
1201 {
1202         GtkWidget *window = (GtkWidget *)data;
1203
1204         if (!hand_cursor)
1205                 hand_cursor = gdk_cursor_new(GDK_HAND2);
1206         if (window && window->window)
1207                 gdk_window_set_cursor(window->window, hand_cursor);
1208
1209         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1210         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1211         
1212 }
1213
1214 static void link_btn_leave(GtkButton *button, gpointer data)
1215 {
1216         GtkWidget *window = (GtkWidget *)data;
1217
1218         if (window && window->window)
1219                 gdk_window_set_cursor(window->window, NULL);
1220
1221         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1222         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1223 }
1224
1225 static void link_btn_pressed(GtkButton *button, gpointer data)
1226 {
1227         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1228         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1229 }
1230
1231 static void link_btn_released(GtkButton *button, gpointer data)
1232 {
1233         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1234         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1235 }
1236
1237 static void link_btn_clicked(GtkButton *button, gpointer data)
1238 {
1239         gchar *url = (gchar *)data;
1240         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1241         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1242         open_uri(url, prefs_common.uri_cmd);
1243 }
1244
1245 static void link_btn_unrealize(GtkButton *button, gpointer data)
1246 {
1247         gchar *url = (gchar *)data;
1248         g_signal_handlers_disconnect_by_func(G_OBJECT(button), 
1249                          G_CALLBACK(link_btn_clicked), url);
1250         g_free(url);
1251 }
1252
1253 GtkWidget *gtkut_get_link_btn(GtkWidget *window, const gchar *url, const gchar *label)
1254 {
1255         GtkWidget *btn;
1256         GtkWidget *btn_label;
1257         GdkColormap *cmap;
1258         GdkColor uri_color[2] = {{0, 0, 0, 0xffff}, {0, 0xffff, 0, 0}};
1259         gboolean success[2];
1260         gchar *local_url = NULL;
1261         if (!url)
1262                 return NULL;
1263
1264         gtkut_convert_int_to_gdk_color(prefs_common.uri_col,
1265                                                &uri_color[0]);
1266         gtkut_convert_int_to_gdk_color(prefs_common.uri_col,
1267                                                &uri_color[1]);
1268
1269         btn = gtk_button_new_with_label(label?label:url);
1270         gtk_button_set_relief(GTK_BUTTON(btn), GTK_RELIEF_NONE);
1271         btn_label = GTK_BIN(btn)->child;
1272         cmap = gdk_drawable_get_colormap(window->window);
1273         gdk_colormap_alloc_colors(cmap, uri_color, 2, FALSE, TRUE, success);
1274         if (success[0] == TRUE && success[1] == TRUE) {
1275                 GtkStyle *style;
1276                 gtk_widget_ensure_style(btn_label);
1277                 style = gtk_style_copy
1278                         (gtk_widget_get_style(btn_label));
1279                 style->fg[GTK_STATE_NORMAL]   = uri_color[0];
1280                 style->fg[GTK_STATE_ACTIVE]   = uri_color[1];
1281                 style->fg[GTK_STATE_PRELIGHT] = uri_color[0];
1282                 gtk_widget_set_style(btn_label, style);
1283         } else
1284                 g_warning("about_create(): color allocation failed.\n");
1285
1286         g_signal_connect(G_OBJECT(btn), "enter",
1287                          G_CALLBACK(link_btn_enter), window);
1288         g_signal_connect(G_OBJECT(btn), "leave",
1289                          G_CALLBACK(link_btn_leave), window);
1290         g_signal_connect(G_OBJECT(btn), "pressed",
1291                          G_CALLBACK(link_btn_pressed), window);
1292         g_signal_connect(G_OBJECT(btn), "released",
1293                          G_CALLBACK(link_btn_released), window);
1294                          
1295         local_url = g_strdup(url);
1296         g_signal_connect(G_OBJECT(btn), "clicked",
1297                          G_CALLBACK(link_btn_clicked), local_url);
1298         g_signal_connect(G_OBJECT(btn), "unrealize",
1299                          G_CALLBACK(link_btn_unrealize), local_url);
1300         return btn;
1301 }
1302
1303 GtkWidget *gtkut_sc_combobox_create(GtkWidget *eventbox, gboolean focus_on_click)
1304 {
1305         GtkWidget *combobox;
1306         GtkListStore *menu;
1307         GtkCellRenderer *rend;
1308
1309         menu = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
1310
1311         combobox = gtk_combo_box_new_with_model(GTK_TREE_MODEL(menu));
1312
1313         rend = gtk_cell_renderer_text_new();
1314         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox), rend, TRUE);
1315         gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combobox), rend,
1316                         "markup", 0,
1317                         NULL);
1318
1319         if( eventbox != NULL )
1320                 gtk_container_add(GTK_CONTAINER(eventbox), combobox);
1321 #if GTK_CHECK_VERSION(2,6,0)
1322         gtk_combo_box_set_focus_on_click(GTK_COMBO_BOX(combobox), focus_on_click);
1323 #endif
1324         return combobox;
1325 }
1326
1327 static void gtkutils_smooth_scroll_do(GtkWidget *widget, GtkAdjustment *vadj,
1328                                       gfloat old_value, gfloat last_value,
1329                                       gint step)
1330 {
1331         gint change_value;
1332         gboolean up;
1333         gint i;
1334
1335         if (old_value < last_value) {
1336                 change_value = last_value - old_value;
1337                 up = FALSE;
1338         } else {
1339                 change_value = old_value - last_value;
1340                 up = TRUE;
1341         }
1342
1343         for (i = step; i <= change_value; i += step) {
1344                 vadj->value = old_value + (up ? -i : i);
1345                 g_signal_emit_by_name(G_OBJECT(vadj),
1346                                       "value_changed", 0);
1347         }
1348
1349         vadj->value = last_value;
1350         g_signal_emit_by_name(G_OBJECT(vadj), "value_changed", 0);
1351
1352         gtk_widget_queue_draw(widget);
1353 }
1354
1355 static gboolean gtkutils_smooth_scroll_page(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1356 {
1357         gfloat upper;
1358         gfloat page_incr;
1359         gfloat old_value;
1360         gfloat last_value;
1361
1362         if (prefs_common.scroll_halfpage)
1363                 page_incr = vadj->page_increment / 2;
1364         else
1365                 page_incr = vadj->page_increment;
1366
1367         if (!up) {
1368                 upper = vadj->upper - vadj->page_size;
1369                 if (vadj->value < upper) {
1370                         old_value = vadj->value;
1371                         last_value = vadj->value + page_incr;
1372                         last_value = MIN(last_value, upper);
1373
1374                         gtkutils_smooth_scroll_do(widget, vadj, old_value,
1375                                                   last_value,
1376                                                   prefs_common.scroll_step);
1377                 } else
1378                         return FALSE;
1379         } else {
1380                 if (vadj->value > 0.0) {
1381                         old_value = vadj->value;
1382                         last_value = vadj->value - page_incr;
1383                         last_value = MAX(last_value, 0.0);
1384
1385                         gtkutils_smooth_scroll_do(widget, vadj, old_value,
1386                                                   last_value,
1387                                                   prefs_common.scroll_step);
1388                 } else
1389                         return FALSE;
1390         }
1391
1392         return TRUE;
1393 }
1394
1395 gboolean gtkutils_scroll_page(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1396 {
1397         gfloat upper;
1398         gfloat page_incr;
1399
1400         if (prefs_common.enable_smooth_scroll)
1401                 return gtkutils_smooth_scroll_page(widget, vadj, up);
1402
1403         if (prefs_common.scroll_halfpage)
1404                 page_incr = vadj->page_increment / 2;
1405         else
1406                 page_incr = vadj->page_increment;
1407
1408         if (!up) {
1409                 upper = vadj->upper - vadj->page_size;
1410                 if (vadj->value < upper) {
1411                         vadj->value += page_incr;
1412                         vadj->value = MIN(vadj->value, upper);
1413                         g_signal_emit_by_name(G_OBJECT(vadj),
1414                                               "value_changed", 0);
1415                 } else
1416                         return FALSE;
1417         } else {
1418                 if (vadj->value > 0.0) {
1419                         vadj->value -= page_incr;
1420                         vadj->value = MAX(vadj->value, 0.0);
1421                         g_signal_emit_by_name(G_OBJECT(vadj),
1422                                               "value_changed", 0);
1423                 } else
1424                         return FALSE;
1425         }
1426         return TRUE;
1427 }
1428
1429 static void gtkutils_smooth_scroll_one_line(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1430 {
1431         gfloat upper;
1432         gfloat old_value;
1433         gfloat last_value;
1434
1435         if (!up) {
1436                 upper = vadj->upper - vadj->page_size;
1437                 if (vadj->value < upper) {
1438                         old_value = vadj->value;
1439                         last_value = vadj->value + vadj->step_increment;
1440                         last_value = MIN(last_value, upper);
1441
1442                         gtkutils_smooth_scroll_do(widget, vadj, old_value,
1443                                                   last_value,
1444                                                   prefs_common.scroll_step);
1445                 }
1446         } else {
1447                 if (vadj->value > 0.0) {
1448                         old_value = vadj->value;
1449                         last_value = vadj->value - vadj->step_increment;
1450                         last_value = MAX(last_value, 0.0);
1451
1452                         gtkutils_smooth_scroll_do(widget, vadj, old_value,
1453                                                   last_value,
1454                                                   prefs_common.scroll_step);
1455                 }
1456         }
1457 }
1458
1459 void gtkutils_scroll_one_line(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1460 {
1461         gfloat upper;
1462
1463         if (prefs_common.enable_smooth_scroll) {
1464                 gtkutils_smooth_scroll_one_line(widget, vadj, up);
1465                 return;
1466         }
1467
1468         if (!up) {
1469                 upper = vadj->upper - vadj->page_size;
1470                 if (vadj->value < upper) {
1471                         vadj->value += vadj->step_increment;
1472                         vadj->value = MIN(vadj->value, upper);
1473                         g_signal_emit_by_name(G_OBJECT(vadj),
1474                                               "value_changed", 0);
1475                 }
1476         } else {
1477                 if (vadj->value > 0.0) {
1478                         vadj->value -= vadj->step_increment;
1479                         vadj->value = MAX(vadj->value, 0.0);
1480                         g_signal_emit_by_name(G_OBJECT(vadj),
1481                                               "value_changed", 0);
1482                 }
1483         }
1484 }