a3df65462bf61c70c6f83b54c36ef32e1c2ba8a0
[claws.git] / src / gtk / gtkutils.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws 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 static void combo_button_size_request(GtkWidget *widget,
171                                       GtkRequisition *requisition,
172                                       gpointer data)
173 {
174         ComboButton *combo = (ComboButton *)data;
175
176         if (combo->arrow->allocation.height != requisition->height)
177                 gtk_widget_set_size_request(combo->arrow,
178                                             -1, requisition->height);
179 }
180
181 static void combo_button_enter(GtkWidget *widget, gpointer data)
182 {
183         ComboButton *combo = (ComboButton *)data;
184
185         if (GTK_WIDGET_STATE(combo->arrow) != GTK_STATE_PRELIGHT) {
186                 gtk_widget_set_state(combo->arrow, GTK_STATE_PRELIGHT);
187                 gtk_widget_queue_draw(combo->arrow);
188         }
189         if (GTK_WIDGET_STATE(combo->button) != GTK_STATE_PRELIGHT) {
190                 gtk_widget_set_state(combo->button, GTK_STATE_PRELIGHT);
191                 gtk_widget_queue_draw(combo->button);
192         }
193 }
194
195 static void combo_button_leave(GtkWidget *widget, gpointer data)
196 {
197         ComboButton *combo = (ComboButton *)data;
198
199         if (GTK_WIDGET_STATE(combo->arrow) != GTK_STATE_NORMAL) {
200                 gtk_widget_set_state(combo->arrow, GTK_STATE_NORMAL);
201                 gtk_widget_queue_draw(combo->arrow);
202         }
203         if (GTK_WIDGET_STATE(combo->button) != GTK_STATE_NORMAL) {
204                 gtk_widget_set_state(combo->button, GTK_STATE_NORMAL);
205                 gtk_widget_queue_draw(combo->button);
206         }
207 }
208
209 static gint combo_button_arrow_pressed(GtkWidget *widget, GdkEventButton *event,
210                                        gpointer data)
211 {
212         ComboButton *combo = (ComboButton *)data;
213
214         if (!event) return FALSE;
215
216         gtk_menu_popup(GTK_MENU(combo->menu), NULL, NULL,
217                        menu_button_position, combo->button,
218                        event->button, event->time);
219
220         return FALSE;
221 }
222
223 static void combo_button_destroy(GtkWidget *widget, gpointer data)
224 {
225         ComboButton *combo = (ComboButton *)data;
226
227         gtk_object_destroy(GTK_OBJECT(combo->factory));
228         g_free(combo);
229 }
230
231 ComboButton *gtkut_combo_button_create(GtkWidget *button,
232                                        GtkItemFactoryEntry *entries,
233                                        gint n_entries, const gchar *path,
234                                        gpointer data)
235 {
236         ComboButton *combo;
237         GtkWidget *arrow;
238
239         combo = g_new0(ComboButton, 1);
240
241         combo->arrow = gtk_button_new();
242         arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
243         gtk_widget_set_size_request(arrow, 7, -1);
244         gtk_container_add(GTK_CONTAINER(combo->arrow), arrow);
245         GTK_WIDGET_UNSET_FLAGS(combo->arrow, GTK_CAN_FOCUS);
246         gtk_widget_show_all(combo->arrow);
247
248         combo->button = button;
249         combo->menu = menu_create_items(entries, n_entries, path,
250                                         &combo->factory, data);
251         combo->data = data;
252
253         g_signal_connect(G_OBJECT(combo->button), "size_request",
254                          G_CALLBACK(combo_button_size_request), combo);
255         g_signal_connect(G_OBJECT(combo->button), "enter",
256                          G_CALLBACK(combo_button_enter), combo);
257         g_signal_connect(G_OBJECT(combo->button), "leave",
258                          G_CALLBACK(combo_button_leave), combo);
259         g_signal_connect(G_OBJECT(combo->arrow), "enter",
260                          G_CALLBACK(combo_button_enter), combo);
261         g_signal_connect(G_OBJECT(combo->arrow), "leave",
262                          G_CALLBACK(combo_button_leave), combo);
263         g_signal_connect(G_OBJECT(combo->arrow), "button_press_event",
264                          G_CALLBACK(combo_button_arrow_pressed), combo);
265         g_signal_connect(G_OBJECT(combo->arrow), "destroy",
266                          G_CALLBACK(combo_button_destroy), combo);
267
268         return combo;
269 }
270
271 #define CELL_SPACING 1
272 #define ROW_TOP_YPIXEL(clist, row) (((clist)->row_height * (row)) + \
273                                     (((row) + 1) * CELL_SPACING) + \
274                                     (clist)->voffset)
275 #define ROW_FROM_YPIXEL(clist, y) (((y) - (clist)->voffset) / \
276                                    ((clist)->row_height + CELL_SPACING))
277
278 void gtkut_ctree_node_move_if_on_the_edge(GtkCTree *ctree, GtkCTreeNode *node)
279 {
280         GtkCList *clist = GTK_CLIST(ctree);
281         gint row;
282         GtkVisibility row_visibility, prev_row_visibility, next_row_visibility;
283
284         g_return_if_fail(ctree != NULL);
285         g_return_if_fail(node != NULL);
286
287         row = g_list_position(clist->row_list, (GList *)node);
288         if (row < 0 || row >= clist->rows || clist->row_height == 0) return;
289         row_visibility = gtk_clist_row_is_visible(clist, row);
290         prev_row_visibility = gtk_clist_row_is_visible(clist, row - 1);
291         next_row_visibility = gtk_clist_row_is_visible(clist, row + 1);
292
293         if (row_visibility == GTK_VISIBILITY_NONE) {
294                 gtk_clist_moveto(clist, row, -1, 0.5, 0);
295                 return;
296         }
297         if (row_visibility == GTK_VISIBILITY_FULL &&
298             prev_row_visibility == GTK_VISIBILITY_FULL &&
299             next_row_visibility == GTK_VISIBILITY_FULL)
300                 return;
301         if (prev_row_visibility != GTK_VISIBILITY_FULL &&
302             next_row_visibility != GTK_VISIBILITY_FULL)
303                 return;
304
305         if (prev_row_visibility != GTK_VISIBILITY_FULL) {
306                 gtk_clist_moveto(clist, row, -1, 0.2, 0);
307                 return;
308         }
309         if (next_row_visibility != GTK_VISIBILITY_FULL) {
310                 gtk_clist_moveto(clist, row, -1, 0.8, 0);
311                 return;
312         }
313 }
314
315 #undef CELL_SPACING
316 #undef ROW_TOP_YPIXEL
317 #undef ROW_FROM_YPIXEL
318
319 gint gtkut_ctree_get_nth_from_node(GtkCTree *ctree, GtkCTreeNode *node)
320 {
321         g_return_val_if_fail(ctree != NULL, -1);
322         g_return_val_if_fail(node != NULL, -1);
323
324         return g_list_position(GTK_CLIST(ctree)->row_list, (GList *)node);
325 }
326
327 /* get the next node, including the invisible one */
328 GtkCTreeNode *gtkut_ctree_node_next(GtkCTree *ctree, GtkCTreeNode *node)
329 {
330         GtkCTreeNode *parent;
331
332         if (!node) return NULL;
333
334         if (GTK_CTREE_ROW(node)->children)
335                 return GTK_CTREE_ROW(node)->children;
336
337         if (GTK_CTREE_ROW(node)->sibling)
338                 return GTK_CTREE_ROW(node)->sibling;
339
340         for (parent = GTK_CTREE_ROW(node)->parent; parent != NULL;
341              parent = GTK_CTREE_ROW(parent)->parent) {
342                 if (GTK_CTREE_ROW(parent)->sibling)
343                         return GTK_CTREE_ROW(parent)->sibling;
344         }
345
346         return NULL;
347 }
348
349 /* get the previous node, including the invisible one */
350 GtkCTreeNode *gtkut_ctree_node_prev(GtkCTree *ctree, GtkCTreeNode *node)
351 {
352         GtkCTreeNode *prev;
353         GtkCTreeNode *child;
354
355         if (!node) return NULL;
356
357         prev = GTK_CTREE_NODE_PREV(node);
358         if (prev == GTK_CTREE_ROW(node)->parent)
359                 return prev;
360
361         child = prev;
362         while (GTK_CTREE_ROW(child)->children != NULL) {
363                 child = GTK_CTREE_ROW(child)->children;
364                 while (GTK_CTREE_ROW(child)->sibling != NULL)
365                         child = GTK_CTREE_ROW(child)->sibling;
366         }
367
368         return child;
369 }
370
371 gboolean gtkut_ctree_node_is_selected(GtkCTree *ctree, GtkCTreeNode *node)
372 {
373         GtkCList *clist = GTK_CLIST(ctree);
374         GList *cur;
375
376         for (cur = clist->selection; cur != NULL; cur = cur->next) {
377                 if (node == GTK_CTREE_NODE(cur->data))
378                         return TRUE;
379         }
380
381         return FALSE;
382 }
383
384 GtkCTreeNode *gtkut_ctree_find_collapsed_parent(GtkCTree *ctree,
385                                                 GtkCTreeNode *node)
386 {
387         if (!node) return NULL;
388
389         while ((node = GTK_CTREE_ROW(node)->parent) != NULL) {
390                 if (!GTK_CTREE_ROW(node)->expanded)
391                         return node;
392         }
393
394         return NULL;
395 }
396
397 void gtkut_ctree_expand_parent_all(GtkCTree *ctree, GtkCTreeNode *node)
398 {
399         while ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
400                 gtk_ctree_expand(ctree, node);
401 }
402
403 gboolean gtkut_ctree_node_is_parent(GtkCTreeNode *parent, GtkCTreeNode *node)
404 {
405         GtkCTreeNode *tmp;
406         g_return_val_if_fail(node != NULL, FALSE);
407         g_return_val_if_fail(parent != NULL, FALSE);
408         tmp = node;
409         
410         while (tmp) {
411                 if(GTK_CTREE_ROW(tmp)->parent && GTK_CTREE_ROW(tmp)->parent == parent)
412                         return TRUE;
413                 tmp = GTK_CTREE_ROW(tmp)->parent;
414         }
415         
416         return FALSE;
417 }
418
419 void gtkut_ctree_set_focus_row(GtkCTree *ctree, GtkCTreeNode *node)
420 {
421         if (node == NULL)
422                 return;
423         gtkut_clist_set_focus_row(GTK_CLIST(ctree),
424                                   gtkut_ctree_get_nth_from_node(ctree, node));
425 }
426
427 void gtkut_clist_set_focus_row(GtkCList *clist, gint row)
428 {
429         clist->focus_row = row;
430         GTKUT_CTREE_REFRESH(clist);
431 }
432
433 void gtkut_combo_set_items(GtkCombo *combo, const gchar *str1, ...)
434 {
435         va_list args;
436         gchar *s;
437         GList *combo_items = NULL;
438
439         g_return_if_fail(str1 != NULL);
440
441         combo_items = g_list_append(combo_items, (gpointer)str1);
442         va_start(args, str1);
443         s = va_arg(args, gchar*);
444         while (s) {
445                 combo_items = g_list_append(combo_items, (gpointer)s);
446                 s = va_arg(args, gchar*);
447         }
448         va_end(args);
449
450         gtk_combo_set_popdown_strings(combo, combo_items);
451
452         g_list_free(combo_items);
453 }
454
455 gchar *gtkut_editable_get_selection(GtkEditable *editable)
456 {
457         guint start_pos, end_pos;
458         gboolean found;
459
460         g_return_val_if_fail(GTK_IS_EDITABLE(editable), NULL);
461
462         found = gtk_editable_get_selection_bounds(editable,
463                                                   &start_pos, &end_pos);
464         if (found)
465                 return gtk_editable_get_chars(editable, start_pos, end_pos);
466         else
467                 return NULL;
468 }
469
470 void gtkut_editable_disable_im(GtkEditable *editable)
471 {
472         g_return_if_fail(editable != NULL);
473
474 #if USE_XIM
475         if (editable->ic) {
476                 gdk_ic_destroy(editable->ic);
477                 editable->ic = NULL;
478         }
479         if (editable->ic_attr) {
480                 gdk_ic_attr_destroy(editable->ic_attr);
481                 editable->ic_attr = NULL;
482         }
483 #endif
484 }
485
486 void gtkut_container_remove(GtkContainer *container, GtkWidget *widget)
487 {
488         gtk_container_remove(container, widget);
489 }
490
491 gboolean gtkut_text_buffer_match_string(GtkTextBuffer *textbuf,
492                                         const GtkTextIter *iter,
493                                         gunichar *wcs, gint len,
494                                         gboolean case_sens)
495 {
496         GtkTextIter start_iter, end_iter;
497         gchar *utf8str, *p;
498         gint match_count;
499
500         start_iter = end_iter = *iter;
501         gtk_text_iter_forward_chars(&end_iter, len);
502
503         utf8str = gtk_text_buffer_get_text(textbuf, &start_iter, &end_iter,
504                                            FALSE);
505         if (!utf8str) return FALSE;
506
507         if ((gint)g_utf8_strlen(utf8str, -1) != len) {
508                 g_free(utf8str);
509                 return FALSE;
510         }
511
512         for (p = utf8str, match_count = 0;
513              *p != '\0' && match_count < len;
514              p = g_utf8_next_char(p), match_count++) {
515                 gunichar wc;
516
517                 wc = g_utf8_get_char(p);
518
519                 if (case_sens) {
520                         if (wc != wcs[match_count])
521                                 break;
522                 } else {
523                         if (g_unichar_tolower(wc) !=
524                             g_unichar_tolower(wcs[match_count]))
525                                 break;
526                 }
527         }
528
529         g_free(utf8str);
530
531         if (match_count == len)
532                 return TRUE;
533         else
534                 return FALSE;
535 }
536
537 gboolean gtkut_text_buffer_find(GtkTextBuffer *buffer, const GtkTextIter *iter,
538                                 const gchar *str, gboolean case_sens,
539                                 GtkTextIter *match_pos)
540 {
541         gunichar *wcs;
542         gint len;
543         glong items_read = 0, items_written = 0;
544         GError *error = NULL;
545         GtkTextIter iter_;
546         gboolean found = FALSE;
547
548         wcs = g_utf8_to_ucs4(str, -1, &items_read, &items_written, &error);
549         if (error != NULL) {
550                 g_warning("An error occured while converting a string from UTF-8 to UCS-4: %s\n",
551                           error->message);
552                 g_error_free(error);
553         }
554         if (!wcs || items_written <= 0) return FALSE;
555         len = (gint)items_written;
556
557         iter_ = *iter;
558         do {
559                 found = gtkut_text_buffer_match_string
560                         (buffer, &iter_, wcs, len, case_sens);
561                 if (found) {
562                         *match_pos = iter_;
563                         break;
564                 }
565         } while (gtk_text_iter_forward_char(&iter_));
566
567         g_free(wcs);
568
569         return found;
570 }
571
572 gboolean gtkut_text_buffer_find_backward(GtkTextBuffer *buffer,
573                                          const GtkTextIter *iter,
574                                          const gchar *str, gboolean case_sens,
575                                          GtkTextIter *match_pos)
576 {
577         gunichar *wcs;
578         gint len;
579         glong items_read = 0, items_written = 0;
580         GError *error = NULL;
581         GtkTextIter iter_;
582         gboolean found = FALSE;
583
584         wcs = g_utf8_to_ucs4(str, -1, &items_read, &items_written, &error);
585         if (error != NULL) {
586                 g_warning("An error occured while converting a string from UTF-8 to UCS-4: %s\n", error->message);
587                 g_error_free(error);
588         }
589         if (!wcs || items_written <= 0) return FALSE;
590         len = (gint)items_written;
591
592         iter_ = *iter;
593         while (gtk_text_iter_backward_char(&iter_)) {
594                 found = gtkut_text_buffer_match_string
595                         (buffer, &iter_, wcs, len, case_sens);
596                 if (found) {
597                         *match_pos = iter_;
598                         break;
599                 }
600         }
601
602         g_free(wcs);
603
604         return found;
605 }
606
607 gchar *gtkut_text_view_get_selection(GtkTextView *textview)
608 {
609         GtkTextBuffer *buffer;
610         GtkTextIter start_iter, end_iter;
611         gboolean found;
612
613         g_return_val_if_fail(GTK_IS_TEXT_VIEW(textview), NULL);
614
615         buffer = gtk_text_view_get_buffer(textview);
616         found = gtk_text_buffer_get_selection_bounds(buffer,
617                                                      &start_iter,
618                                                      &end_iter);
619         if (found)
620                 return gtk_text_buffer_get_text(buffer, &start_iter, &end_iter,
621                                                 FALSE);
622         else
623                 return NULL;
624 }
625
626
627 void gtkut_text_view_set_position(GtkTextView *text, gint pos)
628 {
629         GtkTextBuffer *buffer;
630         GtkTextIter iter;
631
632         g_return_if_fail(text != NULL);
633
634         buffer = gtk_text_view_get_buffer(text);
635
636         gtk_text_buffer_get_iter_at_offset(buffer, &iter, pos);
637         gtk_text_buffer_place_cursor(buffer, &iter);
638         gtk_text_view_scroll_to_iter(text, &iter, 0.0, FALSE, 0.0, 0.0);
639 }
640
641 gboolean gtkut_text_view_search_string(GtkTextView *text, const gchar *str,
642                                         gboolean case_sens)
643 {
644         GtkTextBuffer *buffer;
645         GtkTextIter iter, match_pos;
646         GtkTextMark *mark;
647         gint len;
648
649         g_return_val_if_fail(text != NULL, FALSE);
650         g_return_val_if_fail(str != NULL, FALSE);
651
652         buffer = gtk_text_view_get_buffer(text);
653
654         len = g_utf8_strlen(str, -1);
655         g_return_val_if_fail(len >= 0, FALSE);
656
657         mark = gtk_text_buffer_get_insert(buffer);
658         gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
659
660         if (gtkut_text_buffer_find(buffer, &iter, str, case_sens,
661                                    &match_pos)) {
662                 GtkTextIter end = match_pos;
663
664                 gtk_text_iter_forward_chars(&end, len);
665                 /* place "insert" at the last character */
666                 gtk_text_buffer_select_range(buffer, &end, &match_pos);
667                 gtk_text_view_scroll_to_mark(text, mark, 0.0, FALSE, 0.0, 0.0);
668                 return TRUE;
669         }
670
671         return FALSE;
672 }
673
674 gboolean gtkut_text_view_search_string_backward(GtkTextView *text, const gchar *str,
675                                         gboolean case_sens)
676 {
677         GtkTextBuffer *buffer;
678         GtkTextIter iter, match_pos;
679         GtkTextMark *mark;
680         gint len;
681
682         g_return_val_if_fail(text != NULL, FALSE);
683         g_return_val_if_fail(str != NULL, FALSE);
684
685         buffer = gtk_text_view_get_buffer(text);
686
687         len = g_utf8_strlen(str, -1);
688         g_return_val_if_fail(len >= 0, FALSE);
689
690         mark = gtk_text_buffer_get_insert(buffer);
691         gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
692
693         if (gtkut_text_buffer_find_backward(buffer, &iter, str, case_sens,
694                                             &match_pos)) {
695                 GtkTextIter end = match_pos;
696
697                 gtk_text_iter_forward_chars(&end, len);
698                 gtk_text_buffer_select_range(buffer, &match_pos, &end);
699                 gtk_text_view_scroll_to_mark(text, mark, 0.0, FALSE, 0.0, 0.0);
700                 return TRUE;
701         }
702
703         return FALSE;
704 }
705
706 void gtkut_window_popup(GtkWidget *window)
707 {
708         gint x, y, sx, sy, new_x, new_y;
709
710         g_return_if_fail(window != NULL);
711         g_return_if_fail(window->window != NULL);
712
713         sx = gdk_screen_width();
714         sy = gdk_screen_height();
715
716         gdk_window_get_origin(window->window, &x, &y);
717         new_x = x % sx; if (new_x < 0) new_x = 0;
718         new_y = y % sy; if (new_y < 0) new_y = 0;
719         if (new_x != x || new_y != y)
720                 gdk_window_move(window->window, new_x, new_y);
721
722         gtk_window_present(GTK_WINDOW(window));
723 }
724
725 void gtkut_widget_get_uposition(GtkWidget *widget, gint *px, gint *py)
726 {
727         gint x, y;
728         gint sx, sy;
729
730         g_return_if_fail(widget != NULL);
731         g_return_if_fail(widget->window != NULL);
732
733         sx = gdk_screen_width();
734         sy = gdk_screen_height();
735
736         /* gdk_window_get_root_origin ever return *rootwindow*'s position */
737         gdk_window_get_root_origin(widget->window, &x, &y);
738
739         x %= sx; if (x < 0) x = 0;
740         y %= sy; if (y < 0) y = 0;
741         *px = x;
742         *py = y;
743 }
744
745 void gtkut_widget_draw_now(GtkWidget *widget)
746 {
747         if (GTK_WIDGET_VISIBLE(widget) && GTK_WIDGET_DRAWABLE(widget))
748                 gdk_window_process_updates(widget->window, FALSE);
749 }
750
751 static void gtkut_clist_bindings_add(GtkWidget *clist)
752 {
753         GtkBindingSet *binding_set;
754
755         binding_set = gtk_binding_set_by_class
756                 (GTK_CLIST_GET_CLASS(clist));
757
758         gtk_binding_entry_add_signal(binding_set, GDK_n, GDK_CONTROL_MASK,
759                                      "scroll_vertical", 2,
760                                      G_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
761                                      G_TYPE_FLOAT, 0.0);
762         gtk_binding_entry_add_signal(binding_set, GDK_p, GDK_CONTROL_MASK,
763                                      "scroll_vertical", 2,
764                                      G_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
765                                      G_TYPE_FLOAT, 0.0);
766 }
767
768 void gtkut_widget_init(void)
769 {
770         GtkWidget *clist;
771
772         clist = gtk_clist_new(1);
773         g_object_ref(G_OBJECT(clist));
774         gtk_object_sink(GTK_OBJECT(clist));
775         gtkut_clist_bindings_add(clist);
776         g_object_unref(G_OBJECT(clist));
777
778         clist = gtk_ctree_new(1, 0);
779         g_object_ref(G_OBJECT(clist));
780         gtk_object_sink(GTK_OBJECT(clist));
781         gtkut_clist_bindings_add(clist);
782         g_object_unref(G_OBJECT(clist));
783
784         clist = gtk_sctree_new_with_titles(1, 0, NULL);
785         g_object_ref(G_OBJECT(clist));
786         gtk_object_sink(GTK_OBJECT(clist));
787         gtkut_clist_bindings_add(clist);
788         g_object_unref(G_OBJECT(clist));
789 }
790
791 void gtkut_widget_set_app_icon(GtkWidget *widget)
792 {
793 #include "pixmaps/sylpheed-claws.xpm"
794         static GdkPixmap *sylpheedclawsxpm;
795         static GdkBitmap *sylpheedclawsxpmmask;
796         
797         g_return_if_fail(widget != NULL);
798         g_return_if_fail(widget->window != NULL);
799         if (!sylpheedclawsxpm) {
800                 PIXMAP_CREATE(widget, sylpheedclawsxpm, sylpheedclawsxpmmask,
801                               sylpheed_claws_xpm);
802         }               
803         gdk_window_set_icon(widget->window, NULL, sylpheedclawsxpm, sylpheedclawsxpmmask);
804 }
805
806 void gtkut_widget_set_composer_icon(GtkWidget *widget)
807 {
808         static GdkPixmap *xpm;
809         static GdkBitmap *bmp;
810
811         g_return_if_fail(widget != NULL);
812         g_return_if_fail(widget->window != NULL);
813         if (!xpm) {
814                 stock_pixmap_gdk(widget, STOCK_PIXMAP_MAIL_COMPOSE, &xpm, &bmp);
815         }
816         gdk_window_set_icon(widget->window, NULL, xpm, bmp);    
817 }
818
819 GtkWidget *label_window_create(const gchar *str)
820 {
821         GtkWidget *window;
822         GtkWidget *label;
823
824         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
825         gtk_widget_set_size_request(window, 380, 60);
826         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
827         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
828         gtk_window_set_title(GTK_WINDOW(window), str);
829         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
830         gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
831         manage_window_set_transient(GTK_WINDOW(window));
832
833         label = gtk_label_new(str);
834         gtk_container_add(GTK_CONTAINER(window), label);
835         gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
836         gtk_widget_show(label);
837
838         gtk_widget_show_now(window);
839         
840         GTK_EVENTS_FLUSH();
841
842         return window;
843 }
844
845 GtkWidget *gtkut_account_menu_new(GList                 *ac_list,
846                                   GCallback              callback,
847                                   gpointer               data)
848 {
849         GList *cur_ac;
850         GtkWidget *menu;
851         
852         g_return_val_if_fail(ac_list != NULL, NULL);
853
854         menu = gtk_menu_new();
855
856         for (cur_ac = ac_list; cur_ac != NULL; cur_ac = cur_ac->next) {
857                 gchar *name;
858                 GtkWidget *menuitem;
859                 PrefsAccount *account;
860                 
861                 account = (PrefsAccount *) cur_ac->data;
862                 if (account->name)
863                         name = g_strdup_printf("%s: %s <%s>",
864                                                account->account_name,
865                                                account->name,
866                                                account->address);
867                 else
868                         name = g_strdup_printf("%s: %s",
869                                                account->account_name,
870                                                account->address);
871                 MENUITEM_ADD(menu, menuitem, name, account->account_id);
872                 g_free(name);
873                 if (callback != NULL)
874                         g_signal_connect(G_OBJECT(menuitem), "activate",
875                                          callback, data);
876         }
877         return menu;
878 }
879
880 void gtkut_set_widget_bgcolor_rgb(GtkWidget *widget, guint rgbvalue)
881 {
882         GtkStyle *newstyle;
883         GdkColor gdk_color;
884
885         gtkut_convert_int_to_gdk_color(rgbvalue, &gdk_color);
886         newstyle = gtk_style_copy(gtk_widget_get_default_style());
887         newstyle->bg[GTK_STATE_NORMAL]   = gdk_color;
888         newstyle->bg[GTK_STATE_PRELIGHT] = gdk_color;
889         newstyle->bg[GTK_STATE_ACTIVE]   = gdk_color;
890         gtk_widget_set_style(widget, newstyle);
891 }
892   
893 /*!
894  *\brief        Tries to find a focused child using a lame strategy
895  */
896 GtkWidget *gtkut_get_focused_child(GtkContainer *parent)
897 {
898         GtkWidget *result = NULL;
899         GList *child_list = NULL;
900         GList *c;
901
902         g_return_val_if_fail(parent, NULL);
903
904         /* Get children list and see which has the focus. */
905         child_list = gtk_container_get_children(parent);
906         if (!child_list)
907                 return NULL;
908
909         for (c = child_list; c != NULL; c = g_list_next(c)) {
910                 if (c->data && GTK_IS_WIDGET(c->data)) {
911                         if (GTK_WIDGET_HAS_FOCUS(GTK_WIDGET(c->data))) {
912                                 result = GTK_WIDGET(c->data);
913                                 break;
914                         }
915                 }
916         }
917         
918         /* See if the returned widget is a container itself; if it is,
919          * see if one of its children is focused. If the focused 
920          * container has no focused child, it is itself a focusable 
921          * child, and has focus. */
922         if (result && GTK_IS_CONTAINER(result)) {
923                 GtkWidget *tmp =  gtkut_get_focused_child(GTK_CONTAINER(result)); 
924                 
925                 if (tmp) 
926                         result = tmp;
927         } else {
928                 /* Try the same for each container in the chain */
929                 for (c = child_list; c != NULL && !result; c = g_list_next(c)) {
930                         if (c->data && GTK_IS_WIDGET(c->data) 
931                         &&  GTK_IS_CONTAINER(c->data)) {
932                                 result = gtkut_get_focused_child
933                                         (GTK_CONTAINER(c->data));
934                         }
935                 }
936         
937         }
938         
939         g_list_free(child_list);
940                 
941         return result;
942 }
943
944 /*!
945  *\brief        Create a Browse (file) button based on GTK+ stock
946  */
947 GtkWidget *gtkut_get_browse_file_btn(const gchar *button_label)
948 {
949         GtkWidget *button;
950
951         button = gtk_button_new_with_mnemonic(button_label);
952         gtk_button_set_image(GTK_BUTTON(button),
953                 gtk_image_new_from_stock(GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_BUTTON));
954
955         return button;
956 }
957
958 /*!
959  *\brief        Create a Browse (directory) button based on GTK+ stock
960  */
961 GtkWidget *gtkut_get_browse_directory_btn(const gchar *button_label)
962 {
963         GtkWidget *button;
964
965         button = gtk_button_new_with_mnemonic(button_label);
966         gtk_button_set_image(GTK_BUTTON(button),
967                 gtk_image_new_from_stock(GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_BUTTON));
968
969         return button;
970 }
971
972 GtkWidget *gtkut_get_replace_btn(const gchar *button_label)
973 {
974         GtkWidget *button;
975
976         button = gtk_button_new_with_mnemonic(button_label);
977         gtk_button_set_image(GTK_BUTTON(button),
978                 gtk_image_new_from_stock(GTK_STOCK_REFRESH, GTK_ICON_SIZE_BUTTON));
979
980         return button;
981 }
982
983 #if HAVE_LIBCOMPFACE
984 gint create_xpm_from_xface(gchar *xpm[], const gchar *xface)
985 {
986         static gchar *bit_pattern[] = {
987                 "....",
988                 "...#",
989                 "..#.",
990                 "..##",
991                 ".#..",
992                 ".#.#",
993                 ".##.",
994                 ".###",
995                 "#...",
996                 "#..#",
997                 "#.#.",
998                 "#.##",
999                 "##..",
1000                 "##.#",
1001                 "###.",
1002                 "####"
1003         };
1004
1005         static gchar *xface_header = "48 48 2 1";
1006         static gchar *xface_black  = "# c #000000";
1007         static gchar *xface_white  = ". c #ffffff";
1008
1009         gint i, line = 0;
1010         const guchar *p;
1011         gchar buf[WIDTH * 4 + 1];  /* 4 = strlen("0x0000") */
1012
1013         p = xface;
1014
1015         strcpy(xpm[line++], xface_header);
1016         strcpy(xpm[line++], xface_black);
1017         strcpy(xpm[line++], xface_white);
1018
1019         for (i = 0; i < HEIGHT; i++) {
1020                 gint col;
1021
1022                 buf[0] = '\0';
1023      
1024                 for (col = 0; col < 3; col++) {
1025                         gint figure;
1026
1027                         p += 2;  /* skip '0x' */
1028
1029                         for (figure = 0; figure < 4; figure++) {
1030                                 gint n = 0;
1031
1032                                 if ('0' <= *p && *p <= '9') {
1033                                         n = *p - '0';
1034                                 } else if ('a' <= *p && *p <= 'f') {
1035                                         n = *p - 'a' + 10;
1036                                 } else if ('A' <= *p && *p <= 'F') {
1037                                         n = *p - 'A' + 10;
1038                                 }
1039
1040                                 strcat(buf, bit_pattern[n]);
1041                                 p++;  /* skip ',' */
1042                         }
1043
1044                         p++;  /* skip '\n' */
1045                 }
1046
1047                 strcpy(xpm[line++], buf);
1048                 p++;
1049         }
1050
1051         return 0;
1052 }
1053 #endif
1054
1055 gboolean get_tag_range(GtkTextIter *iter,
1056                                        GtkTextTag *tag,
1057                                        GtkTextIter *start_iter,
1058                                        GtkTextIter *end_iter)
1059 {
1060         GtkTextIter _start_iter, _end_iter;
1061
1062         _end_iter = *iter;
1063         if (!gtk_text_iter_forward_to_tag_toggle(&_end_iter, tag)) {
1064                 debug_print("Can't find end");
1065                 return FALSE;
1066         }
1067
1068         _start_iter = _end_iter;
1069         if (!gtk_text_iter_backward_to_tag_toggle(&_start_iter, tag)) {
1070                 debug_print("Can't find start.");
1071                 return FALSE;
1072         }
1073
1074         *start_iter = _start_iter;
1075         *end_iter = _end_iter;
1076
1077         return TRUE;
1078 }
1079
1080 #if HAVE_LIBCOMPFACE
1081 GtkWidget *xface_get_from_header(const gchar *o_xface, GdkColor *background,
1082                                  GdkWindow *window)
1083 {
1084         static gchar *xpm_xface[XPM_XFACE_HEIGHT];
1085         static gboolean xpm_xface_init = TRUE;
1086         GdkPixmap *pixmap;
1087         GdkBitmap *mask;
1088         gchar xface[2048];
1089         strncpy(xface, o_xface, sizeof(xface));
1090
1091         if (uncompface(xface) < 0) {
1092                 g_warning("uncompface failed\n");
1093                 return NULL;
1094         }
1095
1096         if (xpm_xface_init) {
1097                 gint i;
1098
1099                 for (i = 0; i < XPM_XFACE_HEIGHT; i++) {
1100                         xpm_xface[i] = g_malloc(WIDTH + 1);
1101                         *xpm_xface[i] = '\0';
1102                 }
1103                 xpm_xface_init = FALSE;
1104         }
1105
1106         create_xpm_from_xface(xpm_xface, xface);
1107
1108         pixmap = gdk_pixmap_create_from_xpm_d
1109                 (window, &mask, 
1110                  background, xpm_xface);
1111         return gtk_image_new_from_pixmap(pixmap, mask);
1112 }
1113 #endif
1114
1115 GtkWidget *face_get_from_header(const gchar *o_face)
1116 {
1117         gchar face[2048];
1118         gchar face_png[2048];
1119         gint pngsize;
1120         GdkPixbuf *pixbuf;
1121         GError *error = NULL;
1122         GdkPixbufLoader *loader = gdk_pixbuf_loader_new ();
1123         GtkWidget *image;
1124         
1125         if (o_face == NULL || strlen(o_face) == 0)
1126                 return NULL;
1127
1128         strncpy2(face, o_face, sizeof(face));
1129
1130         unfold_line(face); /* strip all whitespace and linebreaks */
1131         remove_space(face);
1132
1133         pngsize = base64_decode(face_png, face, strlen(face));
1134
1135         if (!gdk_pixbuf_loader_write (loader, face_png, pngsize, &error) ||
1136             !gdk_pixbuf_loader_close (loader, &error)) {
1137                 g_warning("loading face failed\n");
1138                 g_object_unref(loader);
1139                 return NULL;
1140         }
1141
1142         pixbuf = g_object_ref(gdk_pixbuf_loader_get_pixbuf(loader));
1143
1144         g_object_unref(loader);
1145
1146         if ((gdk_pixbuf_get_width(pixbuf) != 48) || (gdk_pixbuf_get_height(pixbuf) != 48)) {
1147                 g_object_unref(pixbuf);
1148                 g_warning("wrong_size");
1149                 return NULL;
1150         }
1151
1152         image = gtk_image_new_from_pixbuf(pixbuf);
1153         g_object_unref(pixbuf);
1154         return image;
1155 }
1156
1157 static GdkCursor *hand_cursor = NULL;
1158
1159 static void link_btn_enter(GtkButton *button, gpointer data)
1160 {
1161         GtkWidget *window = (GtkWidget *)data;
1162
1163         if (!hand_cursor)
1164                 hand_cursor = gdk_cursor_new(GDK_HAND2);
1165         if (window && window->window)
1166                 gdk_window_set_cursor(window->window, hand_cursor);
1167
1168         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1169         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1170         
1171 }
1172
1173 static void link_btn_leave(GtkButton *button, gpointer data)
1174 {
1175         GtkWidget *window = (GtkWidget *)data;
1176
1177         if (window && window->window)
1178                 gdk_window_set_cursor(window->window, NULL);
1179
1180         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1181         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1182 }
1183
1184 static void link_btn_pressed(GtkButton *button, gpointer data)
1185 {
1186         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1187         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1188 }
1189
1190 static void link_btn_released(GtkButton *button, gpointer data)
1191 {
1192         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1193         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1194 }
1195
1196 static void link_btn_clicked(GtkButton *button, gpointer data)
1197 {
1198         gchar *url = (gchar *)data;
1199         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1200         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1201         open_uri(url, prefs_common.uri_cmd);
1202 }
1203
1204 static void link_btn_unrealize(GtkButton *button, gpointer data)
1205 {
1206         gchar *url = (gchar *)data;
1207         g_signal_handlers_disconnect_by_func(G_OBJECT(button), 
1208                          G_CALLBACK(link_btn_clicked), url);
1209         g_free(url);
1210 }
1211
1212 GtkWidget *gtkut_get_link_btn(GtkWidget *window, const gchar *url, const gchar *label)
1213 {
1214         GtkWidget *btn;
1215         GtkWidget *btn_label;
1216         GdkColormap *cmap;
1217         GdkColor uri_color[2] = {{0, 0, 0, 0xffff}, {0, 0xffff, 0, 0}};
1218         gboolean success[2];
1219         gchar *local_url = NULL;
1220         if (!url)
1221                 return NULL;
1222
1223         gtkut_convert_int_to_gdk_color(prefs_common.uri_col,
1224                                                &uri_color[0]);
1225         gtkut_convert_int_to_gdk_color(prefs_common.uri_col,
1226                                                &uri_color[1]);
1227
1228         btn = gtk_button_new_with_label(label?label:url);
1229         gtk_button_set_relief(GTK_BUTTON(btn), GTK_RELIEF_NONE);
1230         btn_label = GTK_BIN(btn)->child;
1231         cmap = gdk_drawable_get_colormap(window->window);
1232         gdk_colormap_alloc_colors(cmap, uri_color, 2, FALSE, TRUE, success);
1233         if (success[0] == TRUE && success[1] == TRUE) {
1234                 GtkStyle *style;
1235                 gtk_widget_ensure_style(btn_label);
1236                 style = gtk_style_copy
1237                         (gtk_widget_get_style(btn_label));
1238                 style->fg[GTK_STATE_NORMAL]   = uri_color[0];
1239                 style->fg[GTK_STATE_ACTIVE]   = uri_color[1];
1240                 style->fg[GTK_STATE_PRELIGHT] = uri_color[0];
1241                 gtk_widget_set_style(btn_label, style);
1242         } else
1243                 g_warning("about_create(): color allocation failed.\n");
1244
1245         g_signal_connect(G_OBJECT(btn), "enter",
1246                          G_CALLBACK(link_btn_enter), window);
1247         g_signal_connect(G_OBJECT(btn), "leave",
1248                          G_CALLBACK(link_btn_leave), window);
1249         g_signal_connect(G_OBJECT(btn), "pressed",
1250                          G_CALLBACK(link_btn_pressed), window);
1251         g_signal_connect(G_OBJECT(btn), "released",
1252                          G_CALLBACK(link_btn_released), window);
1253                          
1254         local_url = g_strdup(url);
1255         g_signal_connect(G_OBJECT(btn), "clicked",
1256                          G_CALLBACK(link_btn_clicked), local_url);
1257         g_signal_connect(G_OBJECT(btn), "unrealize",
1258                          G_CALLBACK(link_btn_unrealize), local_url);
1259         return btn;
1260 }
1261
1262 GtkWidget *gtkut_sc_combobox_create(GtkWidget *eventbox, gboolean focus_on_click)
1263 {
1264         GtkWidget *combobox;
1265         GtkListStore *menu;
1266         GtkCellRenderer *rend;
1267
1268         menu = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
1269
1270         combobox = gtk_combo_box_new_with_model(GTK_TREE_MODEL(menu));
1271
1272         rend = gtk_cell_renderer_text_new();
1273         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox), rend, TRUE);
1274         gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combobox), rend,
1275                         "markup", 0,
1276                         NULL);
1277
1278         if( eventbox != NULL )
1279                 gtk_container_add(GTK_CONTAINER(eventbox), combobox);
1280 #if GTK_CHECK_VERSION(2,6,0)
1281         gtk_combo_box_set_focus_on_click(GTK_COMBO_BOX(combobox), focus_on_click);
1282 #endif
1283         return combobox;
1284 }