4abec525f8596b07a90cfb9449fa89cf811427b8
[claws.git] / src / gtk / gtkutils.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 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 #warning FIXME_GTK2
33 /* #include <gtk/gtkthemes.h> */
34 #include <gtk/gtkbindings.h>
35 #include <stdlib.h>
36 #include <stdarg.h>
37 #include <sys/stat.h>
38
39 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
40 #  include <wchar.h>
41 #  include <wctype.h>
42 #endif
43
44 #include "intl.h"
45 #include "gtkutils.h"
46 #include "utils.h"
47 #include "gtksctree.h"
48 #include "codeconv.h"
49 #include "stock_pixmap.h"
50 #include "menu.h"
51 #include "prefs_account.h"
52
53 #warning FIXME_GTK2
54 gboolean gtkut_get_font_size(GtkWidget *widget,
55                              gint *width, gint *height)
56 {
57         PangoLayout *layout;
58         const gchar *str = "Abcdef";
59
60         g_return_val_if_fail(GTK_IS_WIDGET(widget), FALSE);
61
62         layout = gtk_widget_create_pango_layout(widget, str);
63         g_return_val_if_fail(layout, FALSE);
64         pango_layout_get_pixel_size(layout, width, height);
65         if (width)
66                 *width = *width / g_utf8_strlen(str, -1);
67         g_object_unref(layout);
68
69         return TRUE;
70 }
71
72 void gtkut_convert_int_to_gdk_color(gint rgbvalue, GdkColor *color)
73 {
74         g_return_if_fail(color != NULL);
75
76         color->pixel = 0L;
77         color->red   = (int) (((gdouble)((rgbvalue & 0xff0000) >> 16) / 255.0) * 65535.0);
78         color->green = (int) (((gdouble)((rgbvalue & 0x00ff00) >>  8) / 255.0) * 65535.0);
79         color->blue  = (int) (((gdouble) (rgbvalue & 0x0000ff)        / 255.0) * 65535.0);
80 }
81
82 void gtkut_button_set_create(GtkWidget **bbox,
83                              GtkWidget **button1, const gchar *label1,
84                              GtkWidget **button2, const gchar *label2,
85                              GtkWidget **button3, const gchar *label3)
86 {
87         g_return_if_fail(bbox != NULL);
88         g_return_if_fail(button1 != NULL);
89
90         *bbox = gtk_hbutton_box_new();
91         gtk_button_box_set_layout(GTK_BUTTON_BOX(*bbox), GTK_BUTTONBOX_END);
92         gtk_box_set_spacing(GTK_BOX(*bbox), 5);
93
94         *button1 = gtk_button_new_with_label(label1);
95         GTK_WIDGET_SET_FLAGS(*button1, GTK_CAN_DEFAULT);
96         gtk_box_pack_start(GTK_BOX(*bbox), *button1, TRUE, TRUE, 0);
97         gtk_widget_show(*button1);
98
99         if (button2) {
100                 *button2 = gtk_button_new_with_label(label2);
101                 GTK_WIDGET_SET_FLAGS(*button2, GTK_CAN_DEFAULT);
102                 gtk_box_pack_start(GTK_BOX(*bbox), *button2, TRUE, TRUE, 0);
103                 gtk_widget_show(*button2);
104         }
105
106         if (button3) {
107                 *button3 = gtk_button_new_with_label(label3);
108                 GTK_WIDGET_SET_FLAGS(*button3, GTK_CAN_DEFAULT);
109                 gtk_box_pack_start(GTK_BOX(*bbox), *button3, TRUE, TRUE, 0);
110                 gtk_widget_show(*button3);
111         }
112 }
113
114 #define CELL_SPACING 1
115 #define ROW_TOP_YPIXEL(clist, row) (((clist)->row_height * (row)) + \
116                                     (((row) + 1) * CELL_SPACING) + \
117                                     (clist)->voffset)
118 #define ROW_FROM_YPIXEL(clist, y) (((y) - (clist)->voffset) / \
119                                    ((clist)->row_height + CELL_SPACING))
120
121 void gtkut_ctree_node_move_if_on_the_edge(GtkCTree *ctree, GtkCTreeNode *node)
122 {
123         GtkCList *clist = GTK_CLIST(ctree);
124         gint row;
125         GtkVisibility row_visibility, prev_row_visibility, next_row_visibility;
126
127         g_return_if_fail(ctree != NULL);
128         g_return_if_fail(node != NULL);
129
130         row = g_list_position(clist->row_list, (GList *)node);
131         if (row < 0 || row >= clist->rows || clist->row_height == 0) return;
132         row_visibility = gtk_clist_row_is_visible(clist, row);
133         prev_row_visibility = gtk_clist_row_is_visible(clist, row - 1);
134         next_row_visibility = gtk_clist_row_is_visible(clist, row + 1);
135
136         if (row_visibility == GTK_VISIBILITY_NONE) {
137                 gtk_clist_moveto(clist, row, -1, 0.5, 0);
138                 return;
139         }
140         if (row_visibility == GTK_VISIBILITY_FULL &&
141             prev_row_visibility == GTK_VISIBILITY_FULL &&
142             next_row_visibility == GTK_VISIBILITY_FULL)
143                 return;
144         if (prev_row_visibility != GTK_VISIBILITY_FULL &&
145             next_row_visibility != GTK_VISIBILITY_FULL)
146                 return;
147
148         if (prev_row_visibility != GTK_VISIBILITY_FULL) {
149                 gtk_clist_moveto(clist, row, -1, 0.2, 0);
150                 return;
151         }
152         if (next_row_visibility != GTK_VISIBILITY_FULL) {
153                 gtk_clist_moveto(clist, row, -1, 0.8, 0);
154                 return;
155         }
156 }
157
158 #undef CELL_SPACING
159 #undef ROW_TOP_YPIXEL
160 #undef ROW_FROM_YPIXEL
161
162 gint gtkut_ctree_get_nth_from_node(GtkCTree *ctree, GtkCTreeNode *node)
163 {
164         g_return_val_if_fail(ctree != NULL, -1);
165         g_return_val_if_fail(node != NULL, -1);
166
167         return g_list_position(GTK_CLIST(ctree)->row_list, (GList *)node);
168 }
169
170 /* get the next node, including the invisible one */
171 GtkCTreeNode *gtkut_ctree_node_next(GtkCTree *ctree, GtkCTreeNode *node)
172 {
173         GtkCTreeNode *parent;
174
175         if (!node) return NULL;
176
177         if (GTK_CTREE_ROW(node)->children)
178                 return GTK_CTREE_ROW(node)->children;
179
180         if (GTK_CTREE_ROW(node)->sibling)
181                 return GTK_CTREE_ROW(node)->sibling;
182
183         for (parent = GTK_CTREE_ROW(node)->parent; parent != NULL;
184              parent = GTK_CTREE_ROW(parent)->parent) {
185                 if (GTK_CTREE_ROW(parent)->sibling)
186                         return GTK_CTREE_ROW(parent)->sibling;
187         }
188
189         return NULL;
190 }
191
192 /* get the previous node, including the invisible one */
193 GtkCTreeNode *gtkut_ctree_node_prev(GtkCTree *ctree, GtkCTreeNode *node)
194 {
195         GtkCTreeNode *prev;
196         GtkCTreeNode *child;
197
198         if (!node) return NULL;
199
200         prev = GTK_CTREE_NODE_PREV(node);
201         if (prev == GTK_CTREE_ROW(node)->parent)
202                 return prev;
203
204         child = prev;
205         while (GTK_CTREE_ROW(child)->children != NULL) {
206                 child = GTK_CTREE_ROW(child)->children;
207                 while (GTK_CTREE_ROW(child)->sibling != NULL)
208                         child = GTK_CTREE_ROW(child)->sibling;
209         }
210
211         return child;
212 }
213
214 gboolean gtkut_ctree_node_is_selected(GtkCTree *ctree, GtkCTreeNode *node)
215 {
216         GtkCList *clist = GTK_CLIST(ctree);
217         GList *cur;
218
219         for (cur = clist->selection; cur != NULL; cur = cur->next) {
220                 if (node == GTK_CTREE_NODE(cur->data))
221                         return TRUE;
222         }
223
224         return FALSE;
225 }
226
227 GtkCTreeNode *gtkut_ctree_find_collapsed_parent(GtkCTree *ctree,
228                                                 GtkCTreeNode *node)
229 {
230         if (!node) return NULL;
231
232         while ((node = GTK_CTREE_ROW(node)->parent) != NULL) {
233                 if (!GTK_CTREE_ROW(node)->expanded)
234                         return node;
235         }
236
237         return NULL;
238 }
239
240 void gtkut_ctree_expand_parent_all(GtkCTree *ctree, GtkCTreeNode *node)
241 {
242         while ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
243                 gtk_ctree_expand(ctree, node);
244 }
245
246 gboolean gtkut_ctree_node_is_parent(GtkCTreeNode *parent, GtkCTreeNode *node)
247 {
248         GtkCTreeNode *tmp;
249         g_return_val_if_fail(node != NULL, FALSE);
250         g_return_val_if_fail(parent != NULL, FALSE);
251         tmp = node;
252         
253         while (tmp) {
254                 if(GTK_CTREE_ROW(tmp)->parent && GTK_CTREE_ROW(tmp)->parent == parent)
255                         return TRUE;
256                 tmp = GTK_CTREE_ROW(tmp)->parent;
257         }
258         
259         return FALSE;
260 }
261
262 void gtkut_ctree_set_focus_row(GtkCTree *ctree, GtkCTreeNode *node)
263 {
264         gtkut_clist_set_focus_row(GTK_CLIST(ctree),
265                                   gtkut_ctree_get_nth_from_node(ctree, node));
266 }
267
268 void gtkut_clist_set_focus_row(GtkCList *clist, gint row)
269 {
270         clist->focus_row = row;
271         GTKUT_CTREE_REFRESH(clist);
272 }
273
274 void gtkut_combo_set_items(GtkCombo *combo, const gchar *str1, ...)
275 {
276         va_list args;
277         gchar *s;
278         GList *combo_items = NULL;
279
280         g_return_if_fail(str1 != NULL);
281
282         combo_items = g_list_append(combo_items, (gpointer)str1);
283         va_start(args, str1);
284         s = va_arg(args, gchar*);
285         while (s) {
286                 combo_items = g_list_append(combo_items, (gpointer)s);
287                 s = va_arg(args, gchar*);
288         }
289         va_end(args);
290
291         gtk_combo_set_popdown_strings(combo, combo_items);
292
293         g_list_free(combo_items);
294 }
295
296 gchar *gtkut_editable_get_selection(GtkEditable *editable)
297 {
298         guint start_pos, end_pos;
299         gboolean found;
300
301         g_return_val_if_fail(GTK_IS_EDITABLE(editable), NULL);
302
303         found = gtk_editable_get_selection_bounds(editable,
304                                                   &start_pos, &end_pos);
305         if (found)
306                 return gtk_editable_get_chars(editable, start_pos, end_pos);
307         else
308                 return NULL;
309 }
310
311 /*
312  * Walk through the widget tree and disclaim the selection from all currently
313  * realized GtkEditable widgets.
314  */
315 static void gtkut_check_before_remove(GtkWidget *widget, gpointer unused)
316 {
317         g_return_if_fail(widget != NULL);
318
319         if (!GTK_WIDGET_REALIZED(widget))
320                 return; /* all nested widgets must be unrealized too */
321         if (GTK_IS_CONTAINER(widget))
322                 gtk_container_forall(GTK_CONTAINER(widget),
323                                      gtkut_check_before_remove, NULL);
324 #warning FIXME_GTK2
325 #if 0
326         if (GTK_IS_EDITABLE(widget))
327                 gtk_editable_claim_selection(GTK_EDITABLE(widget),
328                                              FALSE, GDK_CURRENT_TIME);
329 #endif
330 }
331
332 /*
333  * Wrapper around gtk_container_remove to work around a bug in GtkText and
334  * GtkEntry (in all GTK+ versions up to and including at least 1.2.10).
335  *
336  * The problem is that unrealizing a GtkText or GtkEntry widget which has the
337  * active selection completely messes up selection handling, leading to
338  * non-working selections and crashes.  Removing a widget from its container
339  * implies unrealizing it and all its child widgets; this triggers the bug if
340  * the removed widget or any of its children is GtkText or GtkEntry.  As a
341  * workaround, this function walks through the widget subtree before removing
342  * and disclaims the selection from all GtkEditable widgets found.
343  *
344  * A similar workaround may be needed for gtk_widget_reparent(); currently it
345  * is not necessary because Sylpheed does not use gtk_widget_reparent() for
346  * GtkEditable widgets or containers holding such widgets.
347  */
348 void gtkut_container_remove(GtkContainer *container, GtkWidget *widget)
349 {
350         gtkut_check_before_remove(widget, NULL);
351         gtk_container_remove(container, widget);
352 }
353
354 void gtkut_window_popup(GtkWidget *window)
355 {
356         gint x, y, sx, sy, new_x, new_y;
357
358         g_return_if_fail(window != NULL);
359         g_return_if_fail(window->window != NULL);
360
361         sx = gdk_screen_width();
362         sy = gdk_screen_height();
363
364         gdk_window_get_origin(window->window, &x, &y);
365         new_x = x % sx; if (new_x < 0) new_x = 0;
366         new_y = y % sy; if (new_y < 0) new_y = 0;
367         if (new_x != x || new_y != y)
368                 gdk_window_move(window->window, new_x, new_y);
369
370         gdk_window_raise(window->window);
371         gdk_window_show(window->window);
372 }
373
374 void gtkut_widget_get_uposition(GtkWidget *widget, gint *px, gint *py)
375 {
376         gint x, y;
377         gint sx, sy;
378
379         g_return_if_fail(widget != NULL);
380         g_return_if_fail(widget->window != NULL);
381
382         sx = gdk_screen_width();
383         sy = gdk_screen_height();
384
385         /* gdk_window_get_root_origin ever return *rootwindow*'s position */
386         gdk_window_get_root_origin(widget->window, &x, &y);
387
388         x %= sx; if (x < 0) x = 0;
389         y %= sy; if (y < 0) y = 0;
390         *px = x;
391         *py = y;
392 }
393
394 void gtkut_widget_disable_theme_engine(GtkWidget *widget)
395 {
396 #warning FIXME_GTK2
397 #if 0
398         GtkStyle *style, *new_style;
399
400         style = gtk_widget_get_style(widget);
401
402         if (style->engine) {
403                 GtkThemeEngine *engine;
404
405                 engine = style->engine;
406                 style->engine = NULL;
407                 new_style = gtk_style_copy(style);
408                 style->engine = engine;
409                 gtk_widget_set_style(widget, new_style);
410         }
411 #endif
412 }
413
414 void gtkut_widget_wait_for_draw(GtkWidget *widget)
415 {
416         if (!GTK_WIDGET_DRAWABLE(widget)) return;
417
418         while (gtk_events_pending())
419                 gtk_main_iteration();
420 }
421
422 static void gtkut_clist_bindings_add(GtkWidget *clist)
423 {
424         GtkBindingSet *binding_set;
425
426         binding_set = gtk_binding_set_by_class
427                 (GTK_CLIST_GET_CLASS(clist));
428
429         gtk_binding_entry_add_signal(binding_set, GDK_n, GDK_CONTROL_MASK,
430                                      "scroll_vertical", 2,
431                                      G_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
432                                      G_TYPE_FLOAT, 0.0);
433         gtk_binding_entry_add_signal(binding_set, GDK_p, GDK_CONTROL_MASK,
434                                      "scroll_vertical", 2,
435                                      G_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
436                                      G_TYPE_FLOAT, 0.0);
437 }
438
439 void gtkut_widget_init(void)
440 {
441         GtkWidget *clist;
442
443         clist = gtk_clist_new(1);
444         g_object_ref(G_OBJECT(clist));
445         gtk_object_sink(GTK_OBJECT(clist));
446         gtkut_clist_bindings_add(clist);
447         g_object_unref(G_OBJECT(clist));
448
449         clist = gtk_ctree_new(1, 0);
450         g_object_ref(G_OBJECT(clist));
451         gtk_object_sink(GTK_OBJECT(clist));
452         gtkut_clist_bindings_add(clist);
453         g_object_unref(G_OBJECT(clist));
454
455         clist = gtk_sctree_new_with_titles(1, 0, NULL);
456         g_object_ref(G_OBJECT(clist));
457         gtk_object_sink(GTK_OBJECT(clist));
458         gtkut_clist_bindings_add(clist);
459         g_object_unref(G_OBJECT(clist));
460 }
461
462 void gtkut_widget_set_app_icon(GtkWidget *widget)
463 {
464 #include "pixmaps/sylpheed.xpm" 
465         static GdkPixmap *sylpheedxpm;
466         static GdkBitmap *sylpheedxpmmask;
467         
468         g_return_if_fail(widget != NULL);
469         g_return_if_fail(widget->window != NULL);
470         if (!sylpheedxpm) {
471                 PIXMAP_CREATE(widget, sylpheedxpm, sylpheedxpmmask, sylpheed_xpm);
472         }               
473         gdk_window_set_icon(widget->window, NULL, sylpheedxpm, sylpheedxpmmask);
474 }
475
476 void gtkut_widget_set_composer_icon(GtkWidget *widget)
477 {
478         static GdkPixmap *xpm;
479         static GdkBitmap *bmp;
480
481         g_return_if_fail(widget != NULL);
482         g_return_if_fail(widget->window != NULL);
483         if (!xpm) {
484                 stock_pixmap_gdk(widget, STOCK_PIXMAP_MAIL_COMPOSE, &xpm, &bmp);
485         }
486         gdk_window_set_icon(widget->window, NULL, xpm, bmp);    
487 }
488
489 GtkWidget *gtkut_account_menu_new(GList                 *ac_list,
490                                   GtkSignalFunc          callback,
491                                   gpointer               data)
492 {
493         GList *cur_ac;
494         GtkWidget *menu;
495         
496         g_return_val_if_fail(ac_list != NULL, NULL);
497
498         menu = gtk_menu_new();
499
500         for (cur_ac = ac_list; cur_ac != NULL; cur_ac = cur_ac->next) {
501                 gchar *name;
502                 GtkWidget *menuitem;
503                 PrefsAccount *account;
504                 
505                 account = (PrefsAccount *) cur_ac->data;
506                 if (account->name)
507                         name = g_strdup_printf("%s: %s <%s>",
508                                                account->account_name,
509                                                account->name,
510                                                account->address);
511                 else
512                         name = g_strdup_printf("%s: %s",
513                                                account->account_name,
514                                                account->address);
515                 MENUITEM_ADD(menu, menuitem, name, account->account_id);
516                 g_free(name);
517                 if (callback != NULL)
518                         gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
519                                            callback,
520                                            data);
521         }
522         return menu;
523 }
524
525 void gtkut_set_widget_bgcolor_rgb(GtkWidget *widget, guint rgbvalue)
526 {
527         GtkStyle *newstyle;
528         GdkColor gdk_color;
529
530         gtkut_convert_int_to_gdk_color(rgbvalue, &gdk_color);
531         newstyle = gtk_style_copy(gtk_widget_get_default_style());
532         newstyle->bg[GTK_STATE_NORMAL]   = gdk_color;
533         newstyle->bg[GTK_STATE_PRELIGHT] = gdk_color;
534         newstyle->bg[GTK_STATE_ACTIVE]   = gdk_color;
535         gtk_widget_set_style(widget, newstyle);
536 }
537   
538 #warning FIXME_GTK2
539 #if 1 /* FIXME_GTK2 */
540 gboolean gtkut_text_buffer_match_string(GtkTextBuffer *textbuf, gint pos, gunichar *wcs,
541                                         gint len, gboolean case_sens)
542 {
543         GtkTextIter start_iter, end_iter;
544         gchar *utf8str;
545         gint match_count = 0;
546
547         gtk_text_buffer_get_iter_at_offset(textbuf, &start_iter, pos);
548         gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter, pos + len);
549
550         utf8str = gtk_text_buffer_get_text(textbuf, &start_iter, &end_iter, FALSE);
551         if (!utf8str) return FALSE;
552
553         if ((gint) g_utf8_strlen(utf8str, -1) != len) {
554                 g_free(utf8str);
555                 return FALSE;
556         }
557
558         for (; match_count < len; pos++, match_count++) {
559                 gchar *ptr = g_utf8_offset_to_pointer(utf8str, match_count);
560                 gunichar ch;
561
562                 if (!ptr) break;
563                 ch = g_utf8_get_char(ptr);
564
565                 if (case_sens) {
566                         if (ch != wcs[match_count])
567                                 break;
568                 } else {
569                         if (g_unichar_tolower(ch) !=
570                             g_unichar_tolower(wcs[match_count]))
571                                 break;
572                 }
573         }
574
575         g_free(utf8str);
576
577         if (match_count == len)
578                 return TRUE;
579         else
580                 return FALSE;
581 }
582
583 guint gtkut_text_buffer_str_compare_n(GtkTextBuffer *textbuf,
584                                       guint pos1, guint pos2,
585                                       guint len, guint text_len)
586 {
587         guint i;
588
589         for (i = 0; i < len && pos1 + i < text_len && pos2 + i < text_len; i++) {
590                 GtkTextIter start_iter, end_iter;
591                 gchar *utf8str1, *utf8str2;
592
593                 gtk_text_buffer_get_iter_at_offset(textbuf, &start_iter,
594                                                    pos1 + i);
595                 gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter,
596                                                    pos1 + i + 1);
597                 utf8str1 = gtk_text_buffer_get_text(textbuf,
598                                                     &start_iter,
599                                                     &end_iter,
600                                                     FALSE);
601
602                 gtk_text_buffer_get_iter_at_offset(textbuf, &start_iter,
603                                                    pos2 + i);
604                 gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter,
605                                                    pos2 + i + 1);
606                 utf8str2 = gtk_text_buffer_get_text(textbuf,
607                                                     &start_iter,
608                                                     &end_iter,
609                                                     FALSE);
610
611                 if (!utf8str1 || !utf8str2 || strcmp(utf8str1, utf8str2)) {
612                         g_free(utf8str1);
613                         g_free(utf8str2);
614                         break;
615                 }
616
617                 g_free(utf8str1);
618                 g_free(utf8str2);
619         }
620
621         return i;
622 }
623
624 guint gtkut_text_buffer_str_compare(GtkTextBuffer *textbuf,
625                                     guint start_pos, guint text_len,
626                                     const gchar *str)
627 {
628         gunichar *wcs;
629         guint len = 0;
630         glong items_read = 0, items_written = 0;
631         gboolean result;
632         GError *error = NULL;
633
634         if (!str) return 0;
635
636         wcs = g_utf8_to_ucs4(str, -1, &items_read, &items_written, &error);
637         if (error != NULL) {
638                 g_warning("An error occured while converting a string from UTF-8 to UCS-4: %s\n",
639                           error->message);
640                 g_error_free(error);
641         }
642         if (!wcs || items_written <= 0) return 0;
643         len = (guint) items_written;
644
645         if (len > text_len - start_pos)
646                 result = FALSE;
647         else
648                 result = gtkut_text_buffer_match_string(textbuf, start_pos,
649                                                         wcs, len,
650                                                         TRUE);
651
652         g_free(wcs);
653
654         return result ? len : 0;
655 }
656
657 gboolean gtkut_text_buffer_is_uri_string(GtkTextBuffer *textbuf,
658                                          guint start_pos, guint text_len)
659 {
660         if (gtkut_text_buffer_str_compare(textbuf, start_pos, text_len, "http://")  ||
661             gtkut_text_buffer_str_compare(textbuf, start_pos, text_len, "ftp://")   ||
662             gtkut_text_buffer_str_compare(textbuf, start_pos, text_len, "https://") ||
663             gtkut_text_buffer_str_compare(textbuf, start_pos, text_len, "www."))
664                 return TRUE;
665
666         return FALSE;
667 }
668   
669 gchar *gtkut_text_view_get_selection(GtkTextView *textview)
670 {
671         GtkTextBuffer *buffer;
672         GtkTextIter start_iter, end_iter;
673         gboolean found;
674
675         g_return_val_if_fail(GTK_IS_TEXT_VIEW(textview), NULL);
676
677         buffer = gtk_text_view_get_buffer(textview);
678         found = gtk_text_buffer_get_selection_bounds(buffer,
679                                                      &start_iter,
680                                                      &end_iter);
681         if (found)
682                 return gtk_text_buffer_get_text(buffer, &start_iter, &end_iter,
683                                                 FALSE);
684         else
685                 return NULL;
686 }
687 #endif /* FIXME_GTK2 */