dd1a1fd12ed39e8317a84d15c5c2275be2073fcd
[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/gtkthemes.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 "intl.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
53 gint gtkut_get_font_width(GdkFont *font)
54 {
55         gchar *str;
56         gint width;
57
58         if (conv_get_current_charset() == C_UTF_8)
59                 str = "Abcdef";
60         else
61                 str = _("Abcdef");
62
63         width = gdk_string_width(font, str);
64         width /= strlen(str);
65
66         return width;
67 }
68
69 gint gtkut_get_font_height(GdkFont *font)
70 {
71         gchar *str;
72         gint height;
73
74         if (conv_get_current_charset() == C_UTF_8)
75                 str = "Abcdef";
76         else
77                 str = _("Abcdef");
78
79         height = gdk_string_height(font, str);
80
81         return height;
82 }
83
84 GdkFont *gtkut_font_load(const gchar *fontset_name)
85 {
86         GdkFont *font;
87
88         g_return_val_if_fail(fontset_name != NULL, NULL);
89
90         if (conv_get_current_charset() == C_US_ASCII)
91                 font = gtkut_font_load_from_fontset(fontset_name);
92         else
93                 font = gdk_fontset_load(fontset_name);
94
95         return font;
96 }
97
98 GdkFont *gtkut_font_load_from_fontset(const gchar *fontset_name)
99 {
100         GdkFont *font = NULL;
101
102         if (strchr(fontset_name, ',') == NULL) {
103                 font = gdk_font_load(fontset_name);
104         } else {
105                 gchar **fonts;
106                 gint i;
107
108                 fonts = g_strsplit(fontset_name, ",", -1);
109                 for (i = 0; fonts[i] != NULL && fonts[i][0] != '\0';
110                      i++) {
111                         font = gdk_font_load(fonts[i]);
112                         if (font) break;
113                 }
114                 g_strfreev(fonts);
115         }
116
117         return font;
118 }
119
120 void gtkut_convert_int_to_gdk_color(gint rgbvalue, GdkColor *color)
121 {
122         g_return_if_fail(color != NULL);
123
124         color->pixel = 0L;
125         color->red   = (int) (((gdouble)((rgbvalue & 0xff0000) >> 16) / 255.0) * 65535.0);
126         color->green = (int) (((gdouble)((rgbvalue & 0x00ff00) >>  8) / 255.0) * 65535.0);
127         color->blue  = (int) (((gdouble) (rgbvalue & 0x0000ff)        / 255.0) * 65535.0);
128 }
129
130 void gtkut_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_button_box_set_spacing(GTK_BUTTON_BOX(*bbox), 5);
141
142         *button1 = gtk_button_new_with_label(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_with_label(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_with_label(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_usize(combo->arrow, -1, requisition->height);
170 }
171
172 static void combo_button_enter(GtkWidget *widget, gpointer data)
173 {
174         ComboButton *combo = (ComboButton *)data;
175
176         if (GTK_WIDGET_STATE(combo->arrow) != GTK_STATE_PRELIGHT) {
177                 gtk_widget_set_state(combo->arrow, GTK_STATE_PRELIGHT);
178                 gtk_widget_queue_draw(combo->arrow);
179         }
180         if (GTK_WIDGET_STATE(combo->button) != GTK_STATE_PRELIGHT) {
181                 gtk_widget_set_state(combo->button, GTK_STATE_PRELIGHT);
182                 gtk_widget_queue_draw(combo->button);
183         }
184 }
185
186 static void combo_button_leave(GtkWidget *widget, gpointer data)
187 {
188         ComboButton *combo = (ComboButton *)data;
189
190         if (GTK_WIDGET_STATE(combo->arrow) != GTK_STATE_NORMAL) {
191                 gtk_widget_set_state(combo->arrow, GTK_STATE_NORMAL);
192                 gtk_widget_queue_draw(combo->arrow);
193         }
194         if (GTK_WIDGET_STATE(combo->button) != GTK_STATE_NORMAL) {
195                 gtk_widget_set_state(combo->button, GTK_STATE_NORMAL);
196                 gtk_widget_queue_draw(combo->button);
197         }
198 }
199
200 static gint combo_button_arrow_pressed(GtkWidget *widget, GdkEventButton *event,
201                                        gpointer data)
202 {
203         ComboButton *combo = (ComboButton *)data;
204
205         if (!event) return FALSE;
206
207         gtk_menu_popup(GTK_MENU(combo->menu), NULL, NULL,
208                        menu_button_position, combo->button,
209                        event->button, event->time);
210
211         return FALSE;
212 }
213
214 static void combo_button_destroy(GtkWidget *widget, gpointer data)
215 {
216         ComboButton *combo = (ComboButton *)data;
217
218         gtk_object_destroy(GTK_OBJECT(combo->factory));
219         g_free(combo);
220 }
221
222 ComboButton *gtkut_combo_button_create(GtkWidget *button,
223                                        GtkItemFactoryEntry *entries,
224                                        gint n_entries, const gchar *path,
225                                        gpointer data)
226 {
227         ComboButton *combo;
228         GtkWidget *arrow;
229
230         combo = g_new0(ComboButton, 1);
231
232         combo->arrow = gtk_button_new();
233         arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
234         gtk_container_add(GTK_CONTAINER(combo->arrow), arrow);
235         GTK_WIDGET_UNSET_FLAGS(combo->arrow, GTK_CAN_FOCUS);
236         gtk_widget_show_all(combo->arrow);
237
238         combo->button = button;
239         combo->menu = menu_create_items(entries, n_entries, path,
240                                         &combo->factory, data);
241         combo->data = data;
242
243         gtk_signal_connect(GTK_OBJECT(combo->button), "size_request",
244                            GTK_SIGNAL_FUNC(combo_button_size_request), combo);
245         gtk_signal_connect(GTK_OBJECT(combo->button), "enter",
246                            GTK_SIGNAL_FUNC(combo_button_enter), combo);
247         gtk_signal_connect(GTK_OBJECT(combo->button), "leave",
248                            GTK_SIGNAL_FUNC(combo_button_leave), combo);
249         gtk_signal_connect(GTK_OBJECT(combo->arrow), "enter",
250                            GTK_SIGNAL_FUNC(combo_button_enter), combo);
251         gtk_signal_connect(GTK_OBJECT(combo->arrow), "leave",
252                            GTK_SIGNAL_FUNC(combo_button_leave), combo);
253         gtk_signal_connect(GTK_OBJECT(combo->arrow), "button_press_event",
254                            GTK_SIGNAL_FUNC(combo_button_arrow_pressed), combo);
255         gtk_signal_connect(GTK_OBJECT(combo->arrow), "destroy",
256                            GTK_SIGNAL_FUNC(combo_button_destroy), combo);
257
258         return combo;
259 }
260
261 #define CELL_SPACING 1
262 #define ROW_TOP_YPIXEL(clist, row) (((clist)->row_height * (row)) + \
263                                     (((row) + 1) * CELL_SPACING) + \
264                                     (clist)->voffset)
265 #define ROW_FROM_YPIXEL(clist, y) (((y) - (clist)->voffset) / \
266                                    ((clist)->row_height + CELL_SPACING))
267
268 void gtkut_ctree_node_move_if_on_the_edge(GtkCTree *ctree, GtkCTreeNode *node)
269 {
270         GtkCList *clist = GTK_CLIST(ctree);
271         gint row;
272         GtkVisibility row_visibility, prev_row_visibility, next_row_visibility;
273
274         g_return_if_fail(ctree != NULL);
275         g_return_if_fail(node != NULL);
276
277         row = g_list_position(clist->row_list, (GList *)node);
278         if (row < 0 || row >= clist->rows || clist->row_height == 0) return;
279         row_visibility = gtk_clist_row_is_visible(clist, row);
280         prev_row_visibility = gtk_clist_row_is_visible(clist, row - 1);
281         next_row_visibility = gtk_clist_row_is_visible(clist, row + 1);
282
283         if (row_visibility == GTK_VISIBILITY_NONE) {
284                 gtk_clist_moveto(clist, row, -1, 0.5, 0);
285                 return;
286         }
287         if (row_visibility == GTK_VISIBILITY_FULL &&
288             prev_row_visibility == GTK_VISIBILITY_FULL &&
289             next_row_visibility == GTK_VISIBILITY_FULL)
290                 return;
291         if (prev_row_visibility != GTK_VISIBILITY_FULL &&
292             next_row_visibility != GTK_VISIBILITY_FULL)
293                 return;
294
295         if (prev_row_visibility != GTK_VISIBILITY_FULL) {
296                 gtk_clist_moveto(clist, row, -1, 0.2, 0);
297                 return;
298         }
299         if (next_row_visibility != GTK_VISIBILITY_FULL) {
300                 gtk_clist_moveto(clist, row, -1, 0.8, 0);
301                 return;
302         }
303 }
304
305 #undef CELL_SPACING
306 #undef ROW_TOP_YPIXEL
307 #undef ROW_FROM_YPIXEL
308
309 gint gtkut_ctree_get_nth_from_node(GtkCTree *ctree, GtkCTreeNode *node)
310 {
311         g_return_val_if_fail(ctree != NULL, -1);
312         g_return_val_if_fail(node != NULL, -1);
313
314         return g_list_position(GTK_CLIST(ctree)->row_list, (GList *)node);
315 }
316
317 /* get the next node, including the invisible one */
318 GtkCTreeNode *gtkut_ctree_node_next(GtkCTree *ctree, GtkCTreeNode *node)
319 {
320         GtkCTreeNode *parent;
321
322         if (!node) return NULL;
323
324         if (GTK_CTREE_ROW(node)->children)
325                 return GTK_CTREE_ROW(node)->children;
326
327         if (GTK_CTREE_ROW(node)->sibling)
328                 return GTK_CTREE_ROW(node)->sibling;
329
330         for (parent = GTK_CTREE_ROW(node)->parent; parent != NULL;
331              parent = GTK_CTREE_ROW(parent)->parent) {
332                 if (GTK_CTREE_ROW(parent)->sibling)
333                         return GTK_CTREE_ROW(parent)->sibling;
334         }
335
336         return NULL;
337 }
338
339 /* get the previous node, including the invisible one */
340 GtkCTreeNode *gtkut_ctree_node_prev(GtkCTree *ctree, GtkCTreeNode *node)
341 {
342         GtkCTreeNode *prev;
343         GtkCTreeNode *child;
344
345         if (!node) return NULL;
346
347         prev = GTK_CTREE_NODE_PREV(node);
348         if (prev == GTK_CTREE_ROW(node)->parent)
349                 return prev;
350
351         child = prev;
352         while (GTK_CTREE_ROW(child)->children != NULL) {
353                 child = GTK_CTREE_ROW(child)->children;
354                 while (GTK_CTREE_ROW(child)->sibling != NULL)
355                         child = GTK_CTREE_ROW(child)->sibling;
356         }
357
358         return child;
359 }
360
361 gboolean gtkut_ctree_node_is_selected(GtkCTree *ctree, GtkCTreeNode *node)
362 {
363         GtkCList *clist = GTK_CLIST(ctree);
364         GList *cur;
365
366         for (cur = clist->selection; cur != NULL; cur = cur->next) {
367                 if (node == GTK_CTREE_NODE(cur->data))
368                         return TRUE;
369         }
370
371         return FALSE;
372 }
373
374 GtkCTreeNode *gtkut_ctree_find_collapsed_parent(GtkCTree *ctree,
375                                                 GtkCTreeNode *node)
376 {
377         if (!node) return NULL;
378
379         while ((node = GTK_CTREE_ROW(node)->parent) != NULL) {
380                 if (!GTK_CTREE_ROW(node)->expanded)
381                         return node;
382         }
383
384         return NULL;
385 }
386
387 void gtkut_ctree_expand_parent_all(GtkCTree *ctree, GtkCTreeNode *node)
388 {
389         while ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
390                 gtk_ctree_expand(ctree, node);
391 }
392
393 gboolean gtkut_ctree_node_is_parent(GtkCTreeNode *parent, GtkCTreeNode *node)
394 {
395         GtkCTreeNode *tmp;
396         g_return_val_if_fail(node != NULL, FALSE);
397         g_return_val_if_fail(parent != NULL, FALSE);
398         tmp = node;
399         
400         while (tmp) {
401                 if(GTK_CTREE_ROW(tmp)->parent && GTK_CTREE_ROW(tmp)->parent == parent)
402                         return TRUE;
403                 tmp = GTK_CTREE_ROW(tmp)->parent;
404         }
405         
406         return FALSE;
407 }
408
409 void gtkut_ctree_set_focus_row(GtkCTree *ctree, GtkCTreeNode *node)
410 {
411         gtkut_clist_set_focus_row(GTK_CLIST(ctree),
412                                   gtkut_ctree_get_nth_from_node(ctree, node));
413 }
414
415 void gtkut_clist_set_focus_row(GtkCList *clist, gint row)
416 {
417         clist->focus_row = row;
418         GTKUT_CTREE_REFRESH(clist);
419 }
420
421 void gtkut_combo_set_items(GtkCombo *combo, const gchar *str1, ...)
422 {
423         va_list args;
424         gchar *s;
425         GList *combo_items = NULL;
426
427         g_return_if_fail(str1 != NULL);
428
429         combo_items = g_list_append(combo_items, (gpointer)str1);
430         va_start(args, str1);
431         s = va_arg(args, gchar*);
432         while (s) {
433                 combo_items = g_list_append(combo_items, (gpointer)s);
434                 s = va_arg(args, gchar*);
435         }
436         va_end(args);
437
438         gtk_combo_set_popdown_strings(combo, combo_items);
439
440         g_list_free(combo_items);
441 }
442
443 gchar *gtkut_editable_get_selection(GtkEditable *editable)
444 {
445         guint start_pos, end_pos;
446
447         g_return_val_if_fail(editable != NULL, NULL);
448
449         if (!editable->has_selection) return NULL;
450
451         if (editable->selection_start_pos == editable->selection_end_pos)
452                 return NULL;
453
454         if (editable->selection_start_pos < editable->selection_end_pos) {
455                 start_pos = editable->selection_start_pos;
456                 end_pos = editable->selection_end_pos;
457         } else {
458                 start_pos = editable->selection_end_pos;
459                 end_pos = editable->selection_start_pos;
460         }
461
462         return gtk_editable_get_chars(editable, start_pos, end_pos);
463 }
464
465 /*
466  * Walk through the widget tree and disclaim the selection from all currently
467  * realized GtkEditable widgets.
468  */
469 static void gtkut_check_before_remove(GtkWidget *widget, gpointer unused)
470 {
471         g_return_if_fail(widget != NULL);
472
473         if (!GTK_WIDGET_REALIZED(widget))
474                 return; /* all nested widgets must be unrealized too */
475         if (GTK_IS_CONTAINER(widget))
476                 gtk_container_forall(GTK_CONTAINER(widget),
477                                      gtkut_check_before_remove, NULL);
478         if (GTK_IS_EDITABLE(widget))
479                 gtk_editable_claim_selection(GTK_EDITABLE(widget),
480                                              FALSE, GDK_CURRENT_TIME);
481 }
482
483 /*
484  * Wrapper around gtk_container_remove to work around a bug in GtkText and
485  * GtkEntry (in all GTK+ versions up to and including at least 1.2.10).
486  *
487  * The problem is that unrealizing a GtkText or GtkEntry widget which has the
488  * active selection completely messes up selection handling, leading to
489  * non-working selections and crashes.  Removing a widget from its container
490  * implies unrealizing it and all its child widgets; this triggers the bug if
491  * the removed widget or any of its children is GtkText or GtkEntry.  As a
492  * workaround, this function walks through the widget subtree before removing
493  * and disclaims the selection from all GtkEditable widgets found.
494  *
495  * A similar workaround may be needed for gtk_widget_reparent(); currently it
496  * is not necessary because Sylpheed does not use gtk_widget_reparent() for
497  * GtkEditable widgets or containers holding such widgets.
498  */
499 void gtkut_container_remove(GtkContainer *container, GtkWidget *widget)
500 {
501         gtkut_check_before_remove(widget, NULL);
502         gtk_container_remove(container, widget);
503 }
504
505 void gtkut_window_popup(GtkWidget *window)
506 {
507         gint x, y, sx, sy, new_x, new_y;
508
509         g_return_if_fail(window != NULL);
510         g_return_if_fail(window->window != NULL);
511
512         sx = gdk_screen_width();
513         sy = gdk_screen_height();
514
515         gdk_window_get_origin(window->window, &x, &y);
516         new_x = x % sx; if (new_x < 0) new_x = 0;
517         new_y = y % sy; if (new_y < 0) new_y = 0;
518         if (new_x != x || new_y != y)
519                 gdk_window_move(window->window, new_x, new_y);
520
521         gdk_window_raise(window->window);
522         gdk_window_show(window->window);
523 }
524
525 void gtkut_widget_get_uposition(GtkWidget *widget, gint *px, gint *py)
526 {
527         gint x, y;
528         gint sx, sy;
529
530         g_return_if_fail(widget != NULL);
531         g_return_if_fail(widget->window != NULL);
532
533         sx = gdk_screen_width();
534         sy = gdk_screen_height();
535
536         /* gdk_window_get_root_origin ever return *rootwindow*'s position */
537         gdk_window_get_root_origin(widget->window, &x, &y);
538
539         x %= sx; if (x < 0) x = 0;
540         y %= sy; if (y < 0) y = 0;
541         *px = x;
542         *py = y;
543 }
544
545 void gtkut_widget_disable_theme_engine(GtkWidget *widget)
546 {
547         GtkStyle *style, *new_style;
548
549         style = gtk_widget_get_style(widget);
550
551         if (style->engine) {
552                 GtkThemeEngine *engine;
553
554                 engine = style->engine;
555                 style->engine = NULL;
556                 new_style = gtk_style_copy(style);
557                 style->engine = engine;
558                 gtk_widget_set_style(widget, new_style);
559         }
560 }
561
562 static void gtkut_widget_draw_cb(GtkWidget *widget, GdkRectangle *area,
563                                  gboolean *flag)
564 {
565         *flag = TRUE;
566         gtk_signal_disconnect_by_data(GTK_OBJECT(widget), flag);
567 }
568
569 void gtkut_widget_wait_for_draw(GtkWidget *widget)
570 {
571         gboolean flag = FALSE;
572
573         if (!GTK_WIDGET_DRAWABLE(widget)) return;
574
575         gtk_signal_connect(GTK_OBJECT(widget), "draw",
576                            GTK_SIGNAL_FUNC(gtkut_widget_draw_cb), &flag);
577         while (!flag)
578                 gtk_main_iteration();
579 }
580
581 static void gtkut_clist_bindings_add(GtkWidget *clist)
582 {
583         GtkBindingSet *binding_set;
584
585         binding_set = gtk_binding_set_by_class
586                 (GTK_CLIST_CLASS(GTK_OBJECT(clist)->klass));
587
588         gtk_binding_entry_add_signal(binding_set, GDK_n, GDK_CONTROL_MASK,
589                                      "scroll_vertical", 2,
590                                      GTK_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
591                                      GTK_TYPE_FLOAT, 0.0);
592         gtk_binding_entry_add_signal(binding_set, GDK_p, GDK_CONTROL_MASK,
593                                      "scroll_vertical", 2,
594                                      GTK_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
595                                      GTK_TYPE_FLOAT, 0.0);
596 }
597
598 void gtkut_widget_init(void)
599 {
600         GtkWidget *clist;
601
602         clist = gtk_clist_new(1);
603         gtkut_clist_bindings_add(clist);
604         gtk_widget_unref(clist);
605
606         clist = gtk_ctree_new(1, 0);
607         gtkut_clist_bindings_add(clist);
608         gtk_widget_unref(clist);
609
610         clist = gtk_sctree_new_with_titles(1, 0, NULL);
611         gtkut_clist_bindings_add(clist);
612         gtk_widget_unref(clist);
613 }
614
615 void gtkut_widget_set_app_icon(GtkWidget *widget)
616 {
617 #include "pixmaps/sylpheed.xpm" 
618         static GdkPixmap *sylpheedxpm;
619         static GdkBitmap *sylpheedxpmmask;
620         
621         g_return_if_fail(widget != NULL);
622         g_return_if_fail(widget->window != NULL);
623         if (!sylpheedxpm) {
624                 PIXMAP_CREATE(widget, sylpheedxpm, sylpheedxpmmask, sylpheed_xpm);
625         }               
626         gdk_window_set_icon(widget->window, NULL, sylpheedxpm, sylpheedxpmmask);
627 }
628
629 void gtkut_widget_set_composer_icon(GtkWidget *widget)
630 {
631         static GdkPixmap *xpm;
632         static GdkBitmap *bmp;
633
634         g_return_if_fail(widget != NULL);
635         g_return_if_fail(widget->window != NULL);
636         if (!xpm) {
637                 stock_pixmap_gdk(widget, STOCK_PIXMAP_MAIL_COMPOSE, &xpm, &bmp);
638         }
639         gdk_window_set_icon(widget->window, NULL, xpm, bmp);    
640 }
641
642 GtkWidget *gtkut_account_menu_new(GList                 *ac_list,
643                                   GtkSignalFunc          callback,
644                                   gpointer               data)
645 {
646         GList *cur_ac;
647         GtkWidget *menu;
648         
649         g_return_val_if_fail(ac_list != NULL, NULL);
650
651         menu = gtk_menu_new();
652
653         for (cur_ac = ac_list; cur_ac != NULL; cur_ac = cur_ac->next) {
654                 gchar *name;
655                 GtkWidget *menuitem;
656                 PrefsAccount *account;
657                 
658                 account = (PrefsAccount *) cur_ac->data;
659                 if (account->name)
660                         name = g_strdup_printf("%s: %s <%s>",
661                                                account->account_name,
662                                                account->name,
663                                                account->address);
664                 else
665                         name = g_strdup_printf("%s: %s",
666                                                account->account_name,
667                                                account->address);
668                 MENUITEM_ADD(menu, menuitem, name, account->account_id);
669                 g_free(name);
670                 if (callback != NULL)
671                         gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
672                                            callback,
673                                            data);
674         }
675         return menu;
676 }
677
678 void gtkut_set_widget_bgcolor_rgb(GtkWidget *widget, guint rgbvalue)
679 {
680         GtkStyle *newstyle;
681         GdkColor gdk_color;
682
683         gtkut_convert_int_to_gdk_color(rgbvalue, &gdk_color);
684         newstyle = gtk_style_copy(gtk_widget_get_default_style());
685         newstyle->bg[GTK_STATE_NORMAL]   = gdk_color;
686         newstyle->bg[GTK_STATE_PRELIGHT] = gdk_color;
687         newstyle->bg[GTK_STATE_ACTIVE]   = gdk_color;
688         gtk_widget_set_style(widget, newstyle);
689 }
690