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