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