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