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