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