2005-02-24 [colin] 1.0.1cvs15.11
[claws.git] / src / gtk / gtkutils.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2005 Hiroyuki Yamamoto
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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_WCTYPE_H && HAVE_WCHAR_H)
40 #  include <wchar.h>
41 #  include <wctype.h>
42 #endif
43
44 #include "defs.h"
45 #include "gtkutils.h"
46 #include "utils.h"
47 #include "gtksctree.h"
48 #include "codeconv.h"
49 #include "stock_pixmap.h"
50 #include "menu.h"
51 #include "prefs_account.h"
52 #include "prefs_common.h"
53
54 #warning FIXME_GTK2
55 gboolean gtkut_get_font_size(GtkWidget *widget,
56                              gint *width, gint *height)
57 {
58         PangoLayout *layout;
59         const gchar *str = "Abcdef";
60
61         g_return_val_if_fail(GTK_IS_WIDGET(widget), FALSE);
62
63         layout = gtk_widget_create_pango_layout(widget, str);
64         g_return_val_if_fail(layout, FALSE);
65         pango_layout_get_pixel_size(layout, width, height);
66         if (width)
67                 *width = *width / g_utf8_strlen(str, -1);
68         g_object_unref(layout);
69
70         return TRUE;
71 }
72
73 void gtkut_widget_set_small_font_size(GtkWidget *widget)
74 {
75         PangoFontDescription *font_desc;
76         gint size;
77
78         g_return_if_fail(widget != NULL);
79         g_return_if_fail(widget->style != NULL);
80
81         font_desc = pango_font_description_from_string(NORMAL_FONT);
82         size = pango_font_description_get_size(font_desc);
83         pango_font_description_set_size(font_desc, size * PANGO_SCALE_SMALL);
84         gtk_widget_modify_font(widget, font_desc);
85         pango_font_description_free(font_desc);
86 }
87
88 void gtkut_convert_int_to_gdk_color(gint rgbvalue, GdkColor *color)
89 {
90         g_return_if_fail(color != NULL);
91
92         color->pixel = 0L;
93         color->red   = (int) (((gdouble)((rgbvalue & 0xff0000) >> 16) / 255.0) * 65535.0);
94         color->green = (int) (((gdouble)((rgbvalue & 0x00ff00) >>  8) / 255.0) * 65535.0);
95         color->blue  = (int) (((gdouble) (rgbvalue & 0x0000ff)        / 255.0) * 65535.0);
96 }
97
98 void gtkut_button_set_create(GtkWidget **bbox,
99                              GtkWidget **button1, const gchar *label1,
100                              GtkWidget **button2, const gchar *label2,
101                              GtkWidget **button3, const gchar *label3)
102 {
103         g_return_if_fail(bbox != NULL);
104         g_return_if_fail(button1 != NULL);
105
106         *bbox = gtk_hbutton_box_new();
107         gtk_button_box_set_layout(GTK_BUTTON_BOX(*bbox), GTK_BUTTONBOX_END);
108         gtk_box_set_spacing(GTK_BOX(*bbox), 5);
109
110         *button1 = gtk_button_new_with_label(label1);
111         GTK_WIDGET_SET_FLAGS(*button1, GTK_CAN_DEFAULT);
112         gtk_box_pack_start(GTK_BOX(*bbox), *button1, TRUE, TRUE, 0);
113         gtk_widget_show(*button1);
114
115         if (button2) {
116                 *button2 = gtk_button_new_with_label(label2);
117                 GTK_WIDGET_SET_FLAGS(*button2, GTK_CAN_DEFAULT);
118                 gtk_box_pack_start(GTK_BOX(*bbox), *button2, TRUE, TRUE, 0);
119                 gtk_widget_show(*button2);
120         }
121
122         if (button3) {
123                 *button3 = gtk_button_new_with_label(label3);
124                 GTK_WIDGET_SET_FLAGS(*button3, GTK_CAN_DEFAULT);
125                 gtk_box_pack_start(GTK_BOX(*bbox), *button3, TRUE, TRUE, 0);
126                 gtk_widget_show(*button3);
127         }
128 }
129
130 void gtkut_stock_button_set_create(GtkWidget **bbox,
131                                    GtkWidget **button1, const gchar *label1,
132                                    GtkWidget **button2, const gchar *label2,
133                                    GtkWidget **button3, const gchar *label3)
134 {
135         g_return_if_fail(bbox != NULL);
136         g_return_if_fail(button1 != NULL);
137
138         *bbox = gtk_hbutton_box_new();
139         gtk_button_box_set_layout(GTK_BUTTON_BOX(*bbox), GTK_BUTTONBOX_END);
140         gtk_box_set_spacing(GTK_BOX(*bbox), 5);
141
142         *button1 = gtk_button_new_from_stock(label1);
143         GTK_WIDGET_SET_FLAGS(*button1, GTK_CAN_DEFAULT);
144         gtk_box_pack_start(GTK_BOX(*bbox), *button1, TRUE, TRUE, 0);
145         gtk_widget_show(*button1);
146
147         if (button2) {
148                 *button2 = gtk_button_new_from_stock(label2);
149                 GTK_WIDGET_SET_FLAGS(*button2, GTK_CAN_DEFAULT);
150                 gtk_box_pack_start(GTK_BOX(*bbox), *button2, TRUE, TRUE, 0);
151                 gtk_widget_show(*button2);
152         }
153
154         if (button3) {
155                 *button3 = gtk_button_new_from_stock(label3);
156                 GTK_WIDGET_SET_FLAGS(*button3, GTK_CAN_DEFAULT);
157                 gtk_box_pack_start(GTK_BOX(*bbox), *button3, TRUE, TRUE, 0);
158                 gtk_widget_show(*button3);
159         }
160 }
161
162 static void combo_button_size_request(GtkWidget *widget,
163                                       GtkRequisition *requisition,
164                                       gpointer data)
165 {
166         ComboButton *combo = (ComboButton *)data;
167
168         if (combo->arrow->allocation.height != requisition->height)
169                 gtk_widget_set_size_request(combo->arrow,
170                                             -1, requisition->height);
171 }
172
173 static void combo_button_enter(GtkWidget *widget, gpointer data)
174 {
175         ComboButton *combo = (ComboButton *)data;
176
177         if (GTK_WIDGET_STATE(combo->arrow) != GTK_STATE_PRELIGHT) {
178                 gtk_widget_set_state(combo->arrow, GTK_STATE_PRELIGHT);
179                 gtk_widget_queue_draw(combo->arrow);
180         }
181         if (GTK_WIDGET_STATE(combo->button) != GTK_STATE_PRELIGHT) {
182                 gtk_widget_set_state(combo->button, GTK_STATE_PRELIGHT);
183                 gtk_widget_queue_draw(combo->button);
184         }
185 }
186
187 static void combo_button_leave(GtkWidget *widget, gpointer data)
188 {
189         ComboButton *combo = (ComboButton *)data;
190
191         if (GTK_WIDGET_STATE(combo->arrow) != GTK_STATE_NORMAL) {
192                 gtk_widget_set_state(combo->arrow, GTK_STATE_NORMAL);
193                 gtk_widget_queue_draw(combo->arrow);
194         }
195         if (GTK_WIDGET_STATE(combo->button) != GTK_STATE_NORMAL) {
196                 gtk_widget_set_state(combo->button, GTK_STATE_NORMAL);
197                 gtk_widget_queue_draw(combo->button);
198         }
199 }
200
201 static gint combo_button_arrow_pressed(GtkWidget *widget, GdkEventButton *event,
202                                        gpointer data)
203 {
204         ComboButton *combo = (ComboButton *)data;
205
206         if (!event) return FALSE;
207
208         gtk_menu_popup(GTK_MENU(combo->menu), NULL, NULL,
209                        menu_button_position, combo->button,
210                        event->button, event->time);
211
212         return FALSE;
213 }
214
215 static void combo_button_destroy(GtkWidget *widget, gpointer data)
216 {
217         ComboButton *combo = (ComboButton *)data;
218
219         gtk_object_destroy(GTK_OBJECT(combo->factory));
220         g_free(combo);
221 }
222
223 ComboButton *gtkut_combo_button_create(GtkWidget *button,
224                                        GtkItemFactoryEntry *entries,
225                                        gint n_entries, const gchar *path,
226                                        gpointer data)
227 {
228         ComboButton *combo;
229         GtkWidget *arrow;
230
231         combo = g_new0(ComboButton, 1);
232
233         combo->arrow = gtk_button_new();
234         arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
235         gtk_widget_set_size_request(arrow, 7, -1);
236         gtk_container_add(GTK_CONTAINER(combo->arrow), arrow);
237         GTK_WIDGET_UNSET_FLAGS(combo->arrow, GTK_CAN_FOCUS);
238         gtk_widget_show_all(combo->arrow);
239
240         combo->button = button;
241         combo->menu = menu_create_items(entries, n_entries, path,
242                                         &combo->factory, data);
243         combo->data = data;
244
245         g_signal_connect(G_OBJECT(combo->button), "size_request",
246                          G_CALLBACK(combo_button_size_request), combo);
247         g_signal_connect(G_OBJECT(combo->button), "enter",
248                          G_CALLBACK(combo_button_enter), combo);
249         g_signal_connect(G_OBJECT(combo->button), "leave",
250                          G_CALLBACK(combo_button_leave), combo);
251         g_signal_connect(G_OBJECT(combo->arrow), "enter",
252                          G_CALLBACK(combo_button_enter), combo);
253         g_signal_connect(G_OBJECT(combo->arrow), "leave",
254                          G_CALLBACK(combo_button_leave), combo);
255         g_signal_connect(G_OBJECT(combo->arrow), "button_press_event",
256                          G_CALLBACK(combo_button_arrow_pressed), combo);
257         g_signal_connect(G_OBJECT(combo->arrow), "destroy",
258                          G_CALLBACK(combo_button_destroy), combo);
259
260         return combo;
261 }
262
263 #define CELL_SPACING 1
264 #define ROW_TOP_YPIXEL(clist, row) (((clist)->row_height * (row)) + \
265                                     (((row) + 1) * CELL_SPACING) + \
266                                     (clist)->voffset)
267 #define ROW_FROM_YPIXEL(clist, y) (((y) - (clist)->voffset) / \
268                                    ((clist)->row_height + CELL_SPACING))
269
270 void gtkut_ctree_node_move_if_on_the_edge(GtkCTree *ctree, GtkCTreeNode *node)
271 {
272         GtkCList *clist = GTK_CLIST(ctree);
273         gint row;
274         GtkVisibility row_visibility, prev_row_visibility, next_row_visibility;
275
276         g_return_if_fail(ctree != NULL);
277         g_return_if_fail(node != NULL);
278
279         row = g_list_position(clist->row_list, (GList *)node);
280         if (row < 0 || row >= clist->rows || clist->row_height == 0) return;
281         row_visibility = gtk_clist_row_is_visible(clist, row);
282         prev_row_visibility = gtk_clist_row_is_visible(clist, row - 1);
283         next_row_visibility = gtk_clist_row_is_visible(clist, row + 1);
284
285         if (row_visibility == GTK_VISIBILITY_NONE) {
286                 gtk_clist_moveto(clist, row, -1, 0.5, 0);
287                 return;
288         }
289         if (row_visibility == GTK_VISIBILITY_FULL &&
290             prev_row_visibility == GTK_VISIBILITY_FULL &&
291             next_row_visibility == GTK_VISIBILITY_FULL)
292                 return;
293         if (prev_row_visibility != GTK_VISIBILITY_FULL &&
294             next_row_visibility != GTK_VISIBILITY_FULL)
295                 return;
296
297         if (prev_row_visibility != GTK_VISIBILITY_FULL) {
298                 gtk_clist_moveto(clist, row, -1, 0.2, 0);
299                 return;
300         }
301         if (next_row_visibility != GTK_VISIBILITY_FULL) {
302                 gtk_clist_moveto(clist, row, -1, 0.8, 0);
303                 return;
304         }
305 }
306
307 #undef CELL_SPACING
308 #undef ROW_TOP_YPIXEL
309 #undef ROW_FROM_YPIXEL
310
311 gint gtkut_ctree_get_nth_from_node(GtkCTree *ctree, GtkCTreeNode *node)
312 {
313         g_return_val_if_fail(ctree != NULL, -1);
314         g_return_val_if_fail(node != NULL, -1);
315
316         return g_list_position(GTK_CLIST(ctree)->row_list, (GList *)node);
317 }
318
319 /* get the next node, including the invisible one */
320 GtkCTreeNode *gtkut_ctree_node_next(GtkCTree *ctree, GtkCTreeNode *node)
321 {
322         GtkCTreeNode *parent;
323
324         if (!node) return NULL;
325
326         if (GTK_CTREE_ROW(node)->children)
327                 return GTK_CTREE_ROW(node)->children;
328
329         if (GTK_CTREE_ROW(node)->sibling)
330                 return GTK_CTREE_ROW(node)->sibling;
331
332         for (parent = GTK_CTREE_ROW(node)->parent; parent != NULL;
333              parent = GTK_CTREE_ROW(parent)->parent) {
334                 if (GTK_CTREE_ROW(parent)->sibling)
335                         return GTK_CTREE_ROW(parent)->sibling;
336         }
337
338         return NULL;
339 }
340
341 /* get the previous node, including the invisible one */
342 GtkCTreeNode *gtkut_ctree_node_prev(GtkCTree *ctree, GtkCTreeNode *node)
343 {
344         GtkCTreeNode *prev;
345         GtkCTreeNode *child;
346
347         if (!node) return NULL;
348
349         prev = GTK_CTREE_NODE_PREV(node);
350         if (prev == GTK_CTREE_ROW(node)->parent)
351                 return prev;
352
353         child = prev;
354         while (GTK_CTREE_ROW(child)->children != NULL) {
355                 child = GTK_CTREE_ROW(child)->children;
356                 while (GTK_CTREE_ROW(child)->sibling != NULL)
357                         child = GTK_CTREE_ROW(child)->sibling;
358         }
359
360         return child;
361 }
362
363 gboolean gtkut_ctree_node_is_selected(GtkCTree *ctree, GtkCTreeNode *node)
364 {
365         GtkCList *clist = GTK_CLIST(ctree);
366         GList *cur;
367
368         for (cur = clist->selection; cur != NULL; cur = cur->next) {
369                 if (node == GTK_CTREE_NODE(cur->data))
370                         return TRUE;
371         }
372
373         return FALSE;
374 }
375
376 GtkCTreeNode *gtkut_ctree_find_collapsed_parent(GtkCTree *ctree,
377                                                 GtkCTreeNode *node)
378 {
379         if (!node) return NULL;
380
381         while ((node = GTK_CTREE_ROW(node)->parent) != NULL) {
382                 if (!GTK_CTREE_ROW(node)->expanded)
383                         return node;
384         }
385
386         return NULL;
387 }
388
389 void gtkut_ctree_expand_parent_all(GtkCTree *ctree, GtkCTreeNode *node)
390 {
391         while ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
392                 gtk_ctree_expand(ctree, node);
393 }
394
395 gboolean gtkut_ctree_node_is_parent(GtkCTreeNode *parent, GtkCTreeNode *node)
396 {
397         GtkCTreeNode *tmp;
398         g_return_val_if_fail(node != NULL, FALSE);
399         g_return_val_if_fail(parent != NULL, FALSE);
400         tmp = node;
401         
402         while (tmp) {
403                 if(GTK_CTREE_ROW(tmp)->parent && GTK_CTREE_ROW(tmp)->parent == parent)
404                         return TRUE;
405                 tmp = GTK_CTREE_ROW(tmp)->parent;
406         }
407         
408         return FALSE;
409 }
410
411 void gtkut_ctree_set_focus_row(GtkCTree *ctree, GtkCTreeNode *node)
412 {
413         gtkut_clist_set_focus_row(GTK_CLIST(ctree),
414                                   gtkut_ctree_get_nth_from_node(ctree, node));
415 }
416
417 void gtkut_clist_set_focus_row(GtkCList *clist, gint row)
418 {
419         clist->focus_row = row;
420         GTKUT_CTREE_REFRESH(clist);
421 }
422
423 void gtkut_combo_set_items(GtkCombo *combo, const gchar *str1, ...)
424 {
425         va_list args;
426         gchar *s;
427         GList *combo_items = NULL;
428
429         g_return_if_fail(str1 != NULL);
430
431         combo_items = g_list_append(combo_items, (gpointer)str1);
432         va_start(args, str1);
433         s = va_arg(args, gchar*);
434         while (s) {
435                 combo_items = g_list_append(combo_items, (gpointer)s);
436                 s = va_arg(args, gchar*);
437         }
438         va_end(args);
439
440         gtk_combo_set_popdown_strings(combo, combo_items);
441
442         g_list_free(combo_items);
443 }
444
445 gchar *gtkut_editable_get_selection(GtkEditable *editable)
446 {
447         guint start_pos, end_pos;
448         gboolean found;
449
450         g_return_val_if_fail(GTK_IS_EDITABLE(editable), NULL);
451
452         found = gtk_editable_get_selection_bounds(editable,
453                                                   &start_pos, &end_pos);
454         if (found)
455                 return gtk_editable_get_chars(editable, start_pos, end_pos);
456         else
457                 return NULL;
458 }
459
460 void gtkut_editable_disable_im(GtkEditable *editable)
461 {
462         g_return_if_fail(editable != NULL);
463
464 #if USE_XIM
465         if (editable->ic) {
466                 gdk_ic_destroy(editable->ic);
467                 editable->ic = NULL;
468         }
469         if (editable->ic_attr) {
470                 gdk_ic_attr_destroy(editable->ic_attr);
471                 editable->ic_attr = NULL;
472         }
473 #endif
474 }
475
476 /*
477  * Walk through the widget tree and disclaim the selection from all currently
478  * realized GtkEditable widgets.
479  */
480 static void gtkut_check_before_remove(GtkWidget *widget, gpointer unused)
481 {
482         g_return_if_fail(widget != NULL);
483
484         if (!GTK_WIDGET_REALIZED(widget))
485                 return; /* all nested widgets must be unrealized too */
486         if (GTK_IS_CONTAINER(widget))
487                 gtk_container_forall(GTK_CONTAINER(widget),
488                                      gtkut_check_before_remove, NULL);
489 #warning FIXME_GTK2
490 #if 0
491         if (GTK_IS_EDITABLE(widget))
492                 gtk_editable_claim_selection(GTK_EDITABLE(widget),
493                                              FALSE, GDK_CURRENT_TIME);
494 #endif
495 }
496
497 /*
498  * Wrapper around gtk_container_remove to work around a bug in GtkText and
499  * GtkEntry (in all GTK+ versions up to and including at least 1.2.10).
500  *
501  * The problem is that unrealizing a GtkText or GtkEntry widget which has the
502  * active selection completely messes up selection handling, leading to
503  * non-working selections and crashes.  Removing a widget from its container
504  * implies unrealizing it and all its child widgets; this triggers the bug if
505  * the removed widget or any of its children is GtkText or GtkEntry.  As a
506  * workaround, this function walks through the widget subtree before removing
507  * and disclaims the selection from all GtkEditable widgets found.
508  *
509  * A similar workaround may be needed for gtk_widget_reparent(); currently it
510  * is not necessary because Sylpheed does not use gtk_widget_reparent() for
511  * GtkEditable widgets or containers holding such widgets.
512  */
513 void gtkut_container_remove(GtkContainer *container, GtkWidget *widget)
514 {
515         gtkut_check_before_remove(widget, NULL);
516         gtk_container_remove(container, widget);
517 }
518
519 void gtkut_window_popup(GtkWidget *window)
520 {
521         gint x, y, sx, sy, new_x, new_y;
522
523         g_return_if_fail(window != NULL);
524         g_return_if_fail(window->window != NULL);
525
526         sx = gdk_screen_width();
527         sy = gdk_screen_height();
528
529         gdk_window_get_origin(window->window, &x, &y);
530         new_x = x % sx; if (new_x < 0) new_x = 0;
531         new_y = y % sy; if (new_y < 0) new_y = 0;
532         if (new_x != x || new_y != y)
533                 gdk_window_move(window->window, new_x, new_y);
534
535         gtk_window_present(GTK_WINDOW(window));
536 }
537
538 void gtkut_widget_get_uposition(GtkWidget *widget, gint *px, gint *py)
539 {
540         gint x, y;
541         gint sx, sy;
542
543         g_return_if_fail(widget != NULL);
544         g_return_if_fail(widget->window != NULL);
545
546         sx = gdk_screen_width();
547         sy = gdk_screen_height();
548
549         /* gdk_window_get_root_origin ever return *rootwindow*'s position */
550         gdk_window_get_root_origin(widget->window, &x, &y);
551
552         x %= sx; if (x < 0) x = 0;
553         y %= sy; if (y < 0) y = 0;
554         *px = x;
555         *py = y;
556 }
557
558 void gtkut_widget_disable_theme_engine(GtkWidget *widget)
559 {
560 #warning FIXME_GTK2
561 #if 0
562         GtkStyle *style, *new_style;
563
564         style = gtk_widget_get_style(widget);
565
566         if (style->engine) {
567                 GtkThemeEngine *engine;
568
569                 engine = style->engine;
570                 style->engine = NULL;
571                 new_style = gtk_style_copy(style);
572                 style->engine = engine;
573                 gtk_widget_set_style(widget, new_style);
574         }
575 #endif
576 }
577
578 void gtkut_widget_wait_for_draw(GtkWidget *widget)
579 {
580         if (!GTK_WIDGET_DRAWABLE(widget)) return;
581
582         while (gtk_events_pending())
583                 gtk_main_iteration();
584 }
585
586 static void gtkut_clist_bindings_add(GtkWidget *clist)
587 {
588         GtkBindingSet *binding_set;
589
590         binding_set = gtk_binding_set_by_class
591                 (GTK_CLIST_GET_CLASS(clist));
592
593         gtk_binding_entry_add_signal(binding_set, GDK_n, GDK_CONTROL_MASK,
594                                      "scroll_vertical", 2,
595                                      G_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
596                                      G_TYPE_FLOAT, 0.0);
597         gtk_binding_entry_add_signal(binding_set, GDK_p, GDK_CONTROL_MASK,
598                                      "scroll_vertical", 2,
599                                      G_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
600                                      G_TYPE_FLOAT, 0.0);
601 }
602
603 void gtkut_widget_init(void)
604 {
605         GtkWidget *clist;
606
607         clist = gtk_clist_new(1);
608         g_object_ref(G_OBJECT(clist));
609         gtk_object_sink(GTK_OBJECT(clist));
610         gtkut_clist_bindings_add(clist);
611         g_object_unref(G_OBJECT(clist));
612
613         clist = gtk_ctree_new(1, 0);
614         g_object_ref(G_OBJECT(clist));
615         gtk_object_sink(GTK_OBJECT(clist));
616         gtkut_clist_bindings_add(clist);
617         g_object_unref(G_OBJECT(clist));
618
619         clist = gtk_sctree_new_with_titles(1, 0, NULL);
620         g_object_ref(G_OBJECT(clist));
621         gtk_object_sink(GTK_OBJECT(clist));
622         gtkut_clist_bindings_add(clist);
623         g_object_unref(G_OBJECT(clist));
624 }
625
626 void gtkut_widget_set_app_icon(GtkWidget *widget)
627 {
628 #include "pixmaps/sylpheed.xpm" 
629         static GdkPixmap *sylpheedxpm;
630         static GdkBitmap *sylpheedxpmmask;
631         
632         g_return_if_fail(widget != NULL);
633         g_return_if_fail(widget->window != NULL);
634         if (!sylpheedxpm) {
635                 PIXMAP_CREATE(widget, sylpheedxpm, sylpheedxpmmask, sylpheed_xpm);
636         }               
637         gdk_window_set_icon(widget->window, NULL, sylpheedxpm, sylpheedxpmmask);
638 }
639
640 void gtkut_widget_set_composer_icon(GtkWidget *widget)
641 {
642         static GdkPixmap *xpm;
643         static GdkBitmap *bmp;
644
645         g_return_if_fail(widget != NULL);
646         g_return_if_fail(widget->window != NULL);
647         if (!xpm) {
648                 stock_pixmap_gdk(widget, STOCK_PIXMAP_MAIL_COMPOSE, &xpm, &bmp);
649         }
650         gdk_window_set_icon(widget->window, NULL, xpm, bmp);    
651 }
652
653 GtkWidget *gtkut_account_menu_new(GList                 *ac_list,
654                                   GCallback              callback,
655                                   gpointer               data)
656 {
657         GList *cur_ac;
658         GtkWidget *menu;
659         
660         g_return_val_if_fail(ac_list != NULL, NULL);
661
662         menu = gtk_menu_new();
663
664         for (cur_ac = ac_list; cur_ac != NULL; cur_ac = cur_ac->next) {
665                 gchar *name;
666                 GtkWidget *menuitem;
667                 PrefsAccount *account;
668                 
669                 account = (PrefsAccount *) cur_ac->data;
670                 if (account->name)
671                         name = g_strdup_printf("%s: %s <%s>",
672                                                account->account_name,
673                                                account->name,
674                                                account->address);
675                 else
676                         name = g_strdup_printf("%s: %s",
677                                                account->account_name,
678                                                account->address);
679                 MENUITEM_ADD(menu, menuitem, name, account->account_id);
680                 g_free(name);
681                 if (callback != NULL)
682                         g_signal_connect(G_OBJECT(menuitem), "activate",
683                                          callback, data);
684         }
685         return menu;
686 }
687
688 void gtkut_set_widget_bgcolor_rgb(GtkWidget *widget, guint rgbvalue)
689 {
690         GtkStyle *newstyle;
691         GdkColor gdk_color;
692
693         gtkut_convert_int_to_gdk_color(rgbvalue, &gdk_color);
694         newstyle = gtk_style_copy(gtk_widget_get_default_style());
695         newstyle->bg[GTK_STATE_NORMAL]   = gdk_color;
696         newstyle->bg[GTK_STATE_PRELIGHT] = gdk_color;
697         newstyle->bg[GTK_STATE_ACTIVE]   = gdk_color;
698         gtk_widget_set_style(widget, newstyle);
699 }
700   
701 gboolean gtkut_text_buffer_match_string(GtkTextBuffer *textbuf, gint pos, gunichar *wcs,
702                                         gint len, gboolean case_sens)
703 {
704         GtkTextIter start_iter, end_iter;
705         gchar *utf8str;
706         gint match_count = 0;
707
708         gtk_text_buffer_get_iter_at_offset(textbuf, &start_iter, pos);
709         gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter, pos + len);
710
711         utf8str = gtk_text_buffer_get_text(textbuf, &start_iter, &end_iter, FALSE);
712         if (!utf8str) return FALSE;
713
714         if ((gint) g_utf8_strlen(utf8str, -1) != len) {
715                 g_free(utf8str);
716                 return FALSE;
717         }
718
719         for (; match_count < len; match_count++) {
720                 gchar *ptr;
721                 gunichar ch;
722
723                 ptr = g_utf8_offset_to_pointer(utf8str, match_count);
724                 if (!ptr) break;
725                 ch = g_utf8_get_char(ptr);
726
727                 if (case_sens) {
728                         if (ch != wcs[match_count])
729                                 break;
730                 } else {
731                         if (g_unichar_tolower(ch) !=
732                             g_unichar_tolower(wcs[match_count]))
733                                 break;
734                 }
735         }
736
737         g_free(utf8str);
738
739         if (match_count == len)
740                 return TRUE;
741         else
742                 return FALSE;
743 }
744
745 guint gtkut_text_buffer_str_compare_n(GtkTextBuffer *textbuf,
746                                       guint pos1, guint pos2,
747                                       guint len, guint text_len)
748 {
749         guint i;
750
751         for (i = 0; i < len && pos1 + i < text_len && pos2 + i < text_len; i++) {
752                 GtkTextIter start_iter, end_iter;
753                 gchar *utf8str1, *utf8str2;
754
755                 gtk_text_buffer_get_iter_at_offset(textbuf, &start_iter,
756                                                    pos1 + i);
757                 gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter,
758                                                    pos1 + i + 1);
759                 utf8str1 = gtk_text_buffer_get_text(textbuf,
760                                                     &start_iter,
761                                                     &end_iter,
762                                                     FALSE);
763
764                 gtk_text_buffer_get_iter_at_offset(textbuf, &start_iter,
765                                                    pos2 + i);
766                 gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter,
767                                                    pos2 + i + 1);
768                 utf8str2 = gtk_text_buffer_get_text(textbuf,
769                                                     &start_iter,
770                                                     &end_iter,
771                                                     FALSE);
772
773                 if (!utf8str1 || !utf8str2 || strcmp(utf8str1, utf8str2)) {
774                         g_free(utf8str1);
775                         g_free(utf8str2);
776                         break;
777                 }
778
779                 g_free(utf8str1);
780                 g_free(utf8str2);
781         }
782
783         return i;
784 }
785
786 guint gtkut_text_buffer_str_compare(GtkTextBuffer *textbuf,
787                                     guint start_pos, guint text_len,
788                                     const gchar *str)
789 {
790         gunichar *wcs;
791         guint len = 0;
792         glong items_read = 0, items_written = 0;
793         gboolean result;
794         GError *error = NULL;
795
796         if (!str) return 0;
797
798         wcs = g_utf8_to_ucs4(str, -1, &items_read, &items_written, &error);
799         if (error != NULL) {
800                 g_warning("An error occured while converting a string from UTF-8 to UCS-4: %s\n",
801                           error->message);
802                 g_error_free(error);
803         }
804         if (!wcs || items_written <= 0) return 0;
805         len = (guint) items_written;
806
807         if (len > text_len - start_pos)
808                 result = FALSE;
809         else
810                 result = gtkut_text_buffer_match_string(textbuf, start_pos,
811                                                         wcs, len,
812                                                         TRUE);
813
814         g_free(wcs);
815
816         return result ? len : 0;
817 }
818
819 gint gtkut_text_buffer_find(GtkTextBuffer *buffer, guint start_pos,
820                             const gchar *str, gboolean case_sens)
821 {
822         gint pos;
823         gunichar *wcs;
824         gint len;
825         glong items_read = 0, items_written = 0;
826         GError *error = NULL;
827         GtkTextIter iter;
828         gint found_pos = -1;
829
830         wcs = g_utf8_to_ucs4(str, -1, &items_read, &items_written, &error);
831         if (error != NULL) {
832                 g_warning("An error occured while converting a string from UTF-8 to UCS-4: %s\n", error->message);
833                 g_error_free(error);
834         }
835         if (!wcs || items_written <= 0) return -1;
836         len = (gint)items_written;
837
838         gtk_text_buffer_get_iter_at_offset(buffer, &iter, start_pos);
839         do {
840                 pos = gtk_text_iter_get_offset(&iter);
841                 if (gtkut_text_buffer_match_string
842                         (buffer, pos, wcs, len, case_sens) == TRUE) {
843                         found_pos = pos;
844                         break;
845                 }
846         } while (gtk_text_iter_forward_char(&iter));
847
848         g_free(wcs);
849         return found_pos;
850 }
851
852 gboolean gtkut_text_buffer_is_uri_string(GtkTextBuffer *textbuf,
853                                          guint start_pos, guint text_len)
854 {
855         if (gtkut_text_buffer_str_compare(textbuf, start_pos, text_len, "http://")  ||
856             gtkut_text_buffer_str_compare(textbuf, start_pos, text_len, "ftp://")   ||
857             gtkut_text_buffer_str_compare(textbuf, start_pos, text_len, "https://") ||
858             gtkut_text_buffer_str_compare(textbuf, start_pos, text_len, "www."))
859                 return TRUE;
860
861         return FALSE;
862 }
863   
864 gchar *gtkut_text_view_get_selection(GtkTextView *textview)
865 {
866         GtkTextBuffer *buffer;
867         GtkTextIter start_iter, end_iter;
868         gboolean found;
869
870         g_return_val_if_fail(GTK_IS_TEXT_VIEW(textview), NULL);
871
872         buffer = gtk_text_view_get_buffer(textview);
873         found = gtk_text_buffer_get_selection_bounds(buffer,
874                                                      &start_iter,
875                                                      &end_iter);
876         if (found)
877                 return gtk_text_buffer_get_text(buffer, &start_iter, &end_iter,
878                                                 FALSE);
879         else
880                 return NULL;
881 }
882
883 /*!
884  *\brief        Tries to find a focused child using a lame strategy
885  */
886 GtkWidget *gtkut_get_focused_child(GtkContainer *parent)
887 {
888         GtkWidget *result = NULL;
889         GList *child_list = NULL;
890         GList *c;
891
892         g_return_val_if_fail(parent, NULL);
893
894         /* Get children list and see which has the focus. */
895         child_list = gtk_container_get_children(parent);
896         if (!child_list)
897                 return NULL;
898
899         for (c = child_list; c != NULL; c = g_list_next(c)) {
900                 if (c->data && GTK_IS_WIDGET(c->data)) {
901                         if (GTK_WIDGET_HAS_FOCUS(GTK_WIDGET(c->data))) {
902                                 result = GTK_WIDGET(c->data);
903                                 break;
904                         }
905                 }
906         }
907         
908         /* See if the returned widget is a container itself; if it is,
909          * see if one of its children is focused. If the focused 
910          * container has no focused child, it is itself a focusable 
911          * child, and has focus. */
912         if (result && GTK_IS_CONTAINER(result)) {
913                 GtkWidget *tmp =  gtkut_get_focused_child(GTK_CONTAINER(result)); 
914                 
915                 if (tmp) 
916                         result = tmp;
917         } else {
918                 /* Try the same for each container in the chain */
919                 for (c = child_list; c != NULL && !result; c = g_list_next(c)) {
920                         if (c->data && GTK_IS_WIDGET(c->data) 
921                         &&  GTK_IS_CONTAINER(c->data)) {
922                                 result = gtkut_get_focused_child
923                                         (GTK_CONTAINER(c->data));
924                         }
925                 }
926         
927         }
928         
929         g_list_free(child_list);
930                 
931         return result;
932 }