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