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