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