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