2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2004 Hiroyuki Yamamoto
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.
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.
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.
25 #include <gdk/gdkkeysyms.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>
38 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
46 #include "gtksctree.h"
48 #include "stock_pixmap.h"
50 #include "prefs_account.h"
53 gboolean gtkut_get_font_size(GtkWidget *widget,
54 gint *width, gint *height)
57 const gchar *str = "Abcdef";
59 g_return_val_if_fail(GTK_IS_WIDGET(widget), FALSE);
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);
65 *width = *width / g_utf8_strlen(str, -1);
66 g_object_unref(layout);
71 void gtkut_convert_int_to_gdk_color(gint rgbvalue, GdkColor *color)
73 g_return_if_fail(color != NULL);
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);
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)
86 g_return_if_fail(bbox != NULL);
87 g_return_if_fail(button1 != NULL);
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);
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);
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);
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);
113 void gtkut_button_set_create_stock(GtkWidget **bbox,
114 GtkWidget **button1, const gchar *label1,
115 GtkWidget **button2, const gchar *label2,
116 GtkWidget **button3, const gchar *label3)
118 g_return_if_fail(bbox != NULL);
119 g_return_if_fail(button1 != NULL);
121 *bbox = gtk_hbutton_box_new();
122 gtk_button_box_set_layout(GTK_BUTTON_BOX(*bbox), GTK_BUTTONBOX_END);
123 gtk_box_set_spacing(GTK_BOX(*bbox), 5);
125 *button1 = gtk_button_new_from_stock(label1);
126 GTK_WIDGET_SET_FLAGS(*button1, GTK_CAN_DEFAULT);
127 gtk_box_pack_start(GTK_BOX(*bbox), *button1, TRUE, TRUE, 0);
128 gtk_widget_show(*button1);
131 *button2 = gtk_button_new_from_stock(label2);
132 GTK_WIDGET_SET_FLAGS(*button2, GTK_CAN_DEFAULT);
133 gtk_box_pack_start(GTK_BOX(*bbox), *button2, TRUE, TRUE, 0);
134 gtk_widget_show(*button2);
138 *button3 = gtk_button_new_from_stock(label3);
139 GTK_WIDGET_SET_FLAGS(*button3, GTK_CAN_DEFAULT);
140 gtk_box_pack_start(GTK_BOX(*bbox), *button3, TRUE, TRUE, 0);
141 gtk_widget_show(*button3);
145 static void combo_button_size_request(GtkWidget *widget,
146 GtkRequisition *requisition,
149 ComboButton *combo = (ComboButton *)data;
151 if (combo->arrow->allocation.height != requisition->height)
152 gtk_widget_set_usize(combo->arrow, -1, requisition->height);
155 static void combo_button_enter(GtkWidget *widget, gpointer data)
157 ComboButton *combo = (ComboButton *)data;
159 if (GTK_WIDGET_STATE(combo->arrow) != GTK_STATE_PRELIGHT) {
160 gtk_widget_set_state(combo->arrow, GTK_STATE_PRELIGHT);
161 gtk_widget_queue_draw(combo->arrow);
163 if (GTK_WIDGET_STATE(combo->button) != GTK_STATE_PRELIGHT) {
164 gtk_widget_set_state(combo->button, GTK_STATE_PRELIGHT);
165 gtk_widget_queue_draw(combo->button);
169 static void combo_button_leave(GtkWidget *widget, gpointer data)
171 ComboButton *combo = (ComboButton *)data;
173 if (GTK_WIDGET_STATE(combo->arrow) != GTK_STATE_NORMAL) {
174 gtk_widget_set_state(combo->arrow, GTK_STATE_NORMAL);
175 gtk_widget_queue_draw(combo->arrow);
177 if (GTK_WIDGET_STATE(combo->button) != GTK_STATE_NORMAL) {
178 gtk_widget_set_state(combo->button, GTK_STATE_NORMAL);
179 gtk_widget_queue_draw(combo->button);
183 static gint combo_button_arrow_pressed(GtkWidget *widget, GdkEventButton *event,
186 ComboButton *combo = (ComboButton *)data;
188 if (!event) return FALSE;
190 gtk_menu_popup(GTK_MENU(combo->menu), NULL, NULL,
191 menu_button_position, combo->button,
192 event->button, event->time);
197 static void combo_button_destroy(GtkWidget *widget, gpointer data)
199 ComboButton *combo = (ComboButton *)data;
201 gtk_object_destroy(GTK_OBJECT(combo->factory));
205 ComboButton *gtkut_combo_button_create(GtkWidget *button,
206 GtkItemFactoryEntry *entries,
207 gint n_entries, const gchar *path,
213 combo = g_new0(ComboButton, 1);
215 combo->arrow = gtk_button_new();
216 arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
217 gtk_container_add(GTK_CONTAINER(combo->arrow), arrow);
218 GTK_WIDGET_UNSET_FLAGS(combo->arrow, GTK_CAN_FOCUS);
219 gtk_widget_show_all(combo->arrow);
221 combo->button = button;
222 combo->menu = menu_create_items(entries, n_entries, path,
223 &combo->factory, data);
226 g_signal_connect(G_OBJECT(combo->button), "size_request",
227 G_CALLBACK(combo_button_size_request), combo);
228 g_signal_connect(G_OBJECT(combo->button), "enter",
229 G_CALLBACK(combo_button_enter), combo);
230 g_signal_connect(G_OBJECT(combo->button), "leave",
231 G_CALLBACK(combo_button_leave), combo);
232 g_signal_connect(G_OBJECT(combo->arrow), "enter",
233 G_CALLBACK(combo_button_enter), combo);
234 g_signal_connect(G_OBJECT(combo->arrow), "leave",
235 G_CALLBACK(combo_button_leave), combo);
236 g_signal_connect(G_OBJECT(combo->arrow), "button_press_event",
237 G_CALLBACK(combo_button_arrow_pressed), combo);
238 g_signal_connect(G_OBJECT(combo->arrow), "destroy",
239 G_CALLBACK(combo_button_destroy), combo);
244 #define CELL_SPACING 1
245 #define ROW_TOP_YPIXEL(clist, row) (((clist)->row_height * (row)) + \
246 (((row) + 1) * CELL_SPACING) + \
248 #define ROW_FROM_YPIXEL(clist, y) (((y) - (clist)->voffset) / \
249 ((clist)->row_height + CELL_SPACING))
251 void gtkut_ctree_node_move_if_on_the_edge(GtkCTree *ctree, GtkCTreeNode *node)
253 GtkCList *clist = GTK_CLIST(ctree);
255 GtkVisibility row_visibility, prev_row_visibility, next_row_visibility;
257 g_return_if_fail(ctree != NULL);
258 g_return_if_fail(node != NULL);
260 row = g_list_position(clist->row_list, (GList *)node);
261 if (row < 0 || row >= clist->rows || clist->row_height == 0) return;
262 row_visibility = gtk_clist_row_is_visible(clist, row);
263 prev_row_visibility = gtk_clist_row_is_visible(clist, row - 1);
264 next_row_visibility = gtk_clist_row_is_visible(clist, row + 1);
266 if (row_visibility == GTK_VISIBILITY_NONE) {
267 gtk_clist_moveto(clist, row, -1, 0.5, 0);
270 if (row_visibility == GTK_VISIBILITY_FULL &&
271 prev_row_visibility == GTK_VISIBILITY_FULL &&
272 next_row_visibility == GTK_VISIBILITY_FULL)
274 if (prev_row_visibility != GTK_VISIBILITY_FULL &&
275 next_row_visibility != GTK_VISIBILITY_FULL)
278 if (prev_row_visibility != GTK_VISIBILITY_FULL) {
279 gtk_clist_moveto(clist, row, -1, 0.2, 0);
282 if (next_row_visibility != GTK_VISIBILITY_FULL) {
283 gtk_clist_moveto(clist, row, -1, 0.8, 0);
289 #undef ROW_TOP_YPIXEL
290 #undef ROW_FROM_YPIXEL
292 gint gtkut_ctree_get_nth_from_node(GtkCTree *ctree, GtkCTreeNode *node)
294 g_return_val_if_fail(ctree != NULL, -1);
295 g_return_val_if_fail(node != NULL, -1);
297 return g_list_position(GTK_CLIST(ctree)->row_list, (GList *)node);
300 /* get the next node, including the invisible one */
301 GtkCTreeNode *gtkut_ctree_node_next(GtkCTree *ctree, GtkCTreeNode *node)
303 GtkCTreeNode *parent;
305 if (!node) return NULL;
307 if (GTK_CTREE_ROW(node)->children)
308 return GTK_CTREE_ROW(node)->children;
310 if (GTK_CTREE_ROW(node)->sibling)
311 return GTK_CTREE_ROW(node)->sibling;
313 for (parent = GTK_CTREE_ROW(node)->parent; parent != NULL;
314 parent = GTK_CTREE_ROW(parent)->parent) {
315 if (GTK_CTREE_ROW(parent)->sibling)
316 return GTK_CTREE_ROW(parent)->sibling;
322 /* get the previous node, including the invisible one */
323 GtkCTreeNode *gtkut_ctree_node_prev(GtkCTree *ctree, GtkCTreeNode *node)
328 if (!node) return NULL;
330 prev = GTK_CTREE_NODE_PREV(node);
331 if (prev == GTK_CTREE_ROW(node)->parent)
335 while (GTK_CTREE_ROW(child)->children != NULL) {
336 child = GTK_CTREE_ROW(child)->children;
337 while (GTK_CTREE_ROW(child)->sibling != NULL)
338 child = GTK_CTREE_ROW(child)->sibling;
344 gboolean gtkut_ctree_node_is_selected(GtkCTree *ctree, GtkCTreeNode *node)
346 GtkCList *clist = GTK_CLIST(ctree);
349 for (cur = clist->selection; cur != NULL; cur = cur->next) {
350 if (node == GTK_CTREE_NODE(cur->data))
357 GtkCTreeNode *gtkut_ctree_find_collapsed_parent(GtkCTree *ctree,
360 if (!node) return NULL;
362 while ((node = GTK_CTREE_ROW(node)->parent) != NULL) {
363 if (!GTK_CTREE_ROW(node)->expanded)
370 void gtkut_ctree_expand_parent_all(GtkCTree *ctree, GtkCTreeNode *node)
372 while ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
373 gtk_ctree_expand(ctree, node);
376 gboolean gtkut_ctree_node_is_parent(GtkCTreeNode *parent, GtkCTreeNode *node)
379 g_return_val_if_fail(node != NULL, FALSE);
380 g_return_val_if_fail(parent != NULL, FALSE);
384 if(GTK_CTREE_ROW(tmp)->parent && GTK_CTREE_ROW(tmp)->parent == parent)
386 tmp = GTK_CTREE_ROW(tmp)->parent;
392 void gtkut_ctree_set_focus_row(GtkCTree *ctree, GtkCTreeNode *node)
394 gtkut_clist_set_focus_row(GTK_CLIST(ctree),
395 gtkut_ctree_get_nth_from_node(ctree, node));
398 void gtkut_clist_set_focus_row(GtkCList *clist, gint row)
400 clist->focus_row = row;
401 GTKUT_CTREE_REFRESH(clist);
404 void gtkut_combo_set_items(GtkCombo *combo, const gchar *str1, ...)
408 GList *combo_items = NULL;
410 g_return_if_fail(str1 != NULL);
412 combo_items = g_list_append(combo_items, (gpointer)str1);
413 va_start(args, str1);
414 s = va_arg(args, gchar*);
416 combo_items = g_list_append(combo_items, (gpointer)s);
417 s = va_arg(args, gchar*);
421 gtk_combo_set_popdown_strings(combo, combo_items);
423 g_list_free(combo_items);
426 gchar *gtkut_editable_get_selection(GtkEditable *editable)
428 guint start_pos, end_pos;
431 g_return_val_if_fail(GTK_IS_EDITABLE(editable), NULL);
433 found = gtk_editable_get_selection_bounds(editable,
434 &start_pos, &end_pos);
436 return gtk_editable_get_chars(editable, start_pos, end_pos);
441 void gtkut_editable_disable_im(GtkEditable *editable)
443 g_return_if_fail(editable != NULL);
447 gdk_ic_destroy(editable->ic);
450 if (editable->ic_attr) {
451 gdk_ic_attr_destroy(editable->ic_attr);
452 editable->ic_attr = NULL;
458 * Walk through the widget tree and disclaim the selection from all currently
459 * realized GtkEditable widgets.
461 static void gtkut_check_before_remove(GtkWidget *widget, gpointer unused)
463 g_return_if_fail(widget != NULL);
465 if (!GTK_WIDGET_REALIZED(widget))
466 return; /* all nested widgets must be unrealized too */
467 if (GTK_IS_CONTAINER(widget))
468 gtk_container_forall(GTK_CONTAINER(widget),
469 gtkut_check_before_remove, NULL);
472 if (GTK_IS_EDITABLE(widget))
473 gtk_editable_claim_selection(GTK_EDITABLE(widget),
474 FALSE, GDK_CURRENT_TIME);
479 * Wrapper around gtk_container_remove to work around a bug in GtkText and
480 * GtkEntry (in all GTK+ versions up to and including at least 1.2.10).
482 * The problem is that unrealizing a GtkText or GtkEntry widget which has the
483 * active selection completely messes up selection handling, leading to
484 * non-working selections and crashes. Removing a widget from its container
485 * implies unrealizing it and all its child widgets; this triggers the bug if
486 * the removed widget or any of its children is GtkText or GtkEntry. As a
487 * workaround, this function walks through the widget subtree before removing
488 * and disclaims the selection from all GtkEditable widgets found.
490 * A similar workaround may be needed for gtk_widget_reparent(); currently it
491 * is not necessary because Sylpheed does not use gtk_widget_reparent() for
492 * GtkEditable widgets or containers holding such widgets.
494 void gtkut_container_remove(GtkContainer *container, GtkWidget *widget)
496 gtkut_check_before_remove(widget, NULL);
497 gtk_container_remove(container, widget);
500 void gtkut_window_popup(GtkWidget *window)
502 gint x, y, sx, sy, new_x, new_y;
504 g_return_if_fail(window != NULL);
505 g_return_if_fail(window->window != NULL);
507 sx = gdk_screen_width();
508 sy = gdk_screen_height();
510 gdk_window_get_origin(window->window, &x, &y);
511 new_x = x % sx; if (new_x < 0) new_x = 0;
512 new_y = y % sy; if (new_y < 0) new_y = 0;
513 if (new_x != x || new_y != y)
514 gdk_window_move(window->window, new_x, new_y);
516 gdk_window_raise(window->window);
517 gdk_window_show(window->window);
520 void gtkut_widget_get_uposition(GtkWidget *widget, gint *px, gint *py)
525 g_return_if_fail(widget != NULL);
526 g_return_if_fail(widget->window != NULL);
528 sx = gdk_screen_width();
529 sy = gdk_screen_height();
531 /* gdk_window_get_root_origin ever return *rootwindow*'s position */
532 gdk_window_get_root_origin(widget->window, &x, &y);
534 x %= sx; if (x < 0) x = 0;
535 y %= sy; if (y < 0) y = 0;
540 void gtkut_widget_disable_theme_engine(GtkWidget *widget)
544 GtkStyle *style, *new_style;
546 style = gtk_widget_get_style(widget);
549 GtkThemeEngine *engine;
551 engine = style->engine;
552 style->engine = NULL;
553 new_style = gtk_style_copy(style);
554 style->engine = engine;
555 gtk_widget_set_style(widget, new_style);
560 void gtkut_widget_wait_for_draw(GtkWidget *widget)
562 if (!GTK_WIDGET_DRAWABLE(widget)) return;
564 while (gtk_events_pending())
565 gtk_main_iteration();
568 static void gtkut_clist_bindings_add(GtkWidget *clist)
570 GtkBindingSet *binding_set;
572 binding_set = gtk_binding_set_by_class
573 (GTK_CLIST_GET_CLASS(clist));
575 gtk_binding_entry_add_signal(binding_set, GDK_n, GDK_CONTROL_MASK,
576 "scroll_vertical", 2,
577 G_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
579 gtk_binding_entry_add_signal(binding_set, GDK_p, GDK_CONTROL_MASK,
580 "scroll_vertical", 2,
581 G_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
585 void gtkut_widget_init(void)
589 clist = gtk_clist_new(1);
590 g_object_ref(G_OBJECT(clist));
591 gtk_object_sink(GTK_OBJECT(clist));
592 gtkut_clist_bindings_add(clist);
593 g_object_unref(G_OBJECT(clist));
595 clist = gtk_ctree_new(1, 0);
596 g_object_ref(G_OBJECT(clist));
597 gtk_object_sink(GTK_OBJECT(clist));
598 gtkut_clist_bindings_add(clist);
599 g_object_unref(G_OBJECT(clist));
601 clist = gtk_sctree_new_with_titles(1, 0, NULL);
602 g_object_ref(G_OBJECT(clist));
603 gtk_object_sink(GTK_OBJECT(clist));
604 gtkut_clist_bindings_add(clist);
605 g_object_unref(G_OBJECT(clist));
608 void gtkut_widget_set_app_icon(GtkWidget *widget)
610 #include "pixmaps/sylpheed.xpm"
611 static GdkPixmap *sylpheedxpm;
612 static GdkBitmap *sylpheedxpmmask;
614 g_return_if_fail(widget != NULL);
615 g_return_if_fail(widget->window != NULL);
617 PIXMAP_CREATE(widget, sylpheedxpm, sylpheedxpmmask, sylpheed_xpm);
619 gdk_window_set_icon(widget->window, NULL, sylpheedxpm, sylpheedxpmmask);
622 void gtkut_widget_set_composer_icon(GtkWidget *widget)
624 static GdkPixmap *xpm;
625 static GdkBitmap *bmp;
627 g_return_if_fail(widget != NULL);
628 g_return_if_fail(widget->window != NULL);
630 stock_pixmap_gdk(widget, STOCK_PIXMAP_MAIL_COMPOSE, &xpm, &bmp);
632 gdk_window_set_icon(widget->window, NULL, xpm, bmp);
635 GtkWidget *gtkut_account_menu_new(GList *ac_list,
642 g_return_val_if_fail(ac_list != NULL, NULL);
644 menu = gtk_menu_new();
646 for (cur_ac = ac_list; cur_ac != NULL; cur_ac = cur_ac->next) {
649 PrefsAccount *account;
651 account = (PrefsAccount *) cur_ac->data;
653 name = g_strdup_printf("%s: %s <%s>",
654 account->account_name,
658 name = g_strdup_printf("%s: %s",
659 account->account_name,
661 MENUITEM_ADD(menu, menuitem, name, account->account_id);
663 if (callback != NULL)
664 g_signal_connect(G_OBJECT(menuitem), "activate",
670 void gtkut_set_widget_bgcolor_rgb(GtkWidget *widget, guint rgbvalue)
675 gtkut_convert_int_to_gdk_color(rgbvalue, &gdk_color);
676 newstyle = gtk_style_copy(gtk_widget_get_default_style());
677 newstyle->bg[GTK_STATE_NORMAL] = gdk_color;
678 newstyle->bg[GTK_STATE_PRELIGHT] = gdk_color;
679 newstyle->bg[GTK_STATE_ACTIVE] = gdk_color;
680 gtk_widget_set_style(widget, newstyle);
683 gboolean gtkut_text_buffer_match_string(GtkTextBuffer *textbuf, gint pos, gunichar *wcs,
684 gint len, gboolean case_sens)
686 GtkTextIter start_iter, end_iter;
688 gint match_count = 0;
690 gtk_text_buffer_get_iter_at_offset(textbuf, &start_iter, pos);
691 gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter, pos + len);
693 utf8str = gtk_text_buffer_get_text(textbuf, &start_iter, &end_iter, FALSE);
694 if (!utf8str) return FALSE;
696 if ((gint) g_utf8_strlen(utf8str, -1) != len) {
701 for (; match_count < len; pos++, match_count++) {
702 gchar *ptr = g_utf8_offset_to_pointer(utf8str, match_count);
706 ch = g_utf8_get_char(ptr);
709 if (ch != wcs[match_count])
712 if (g_unichar_tolower(ch) !=
713 g_unichar_tolower(wcs[match_count]))
720 if (match_count == len)
726 guint gtkut_text_buffer_str_compare_n(GtkTextBuffer *textbuf,
727 guint pos1, guint pos2,
728 guint len, guint text_len)
732 for (i = 0; i < len && pos1 + i < text_len && pos2 + i < text_len; i++) {
733 GtkTextIter start_iter, end_iter;
734 gchar *utf8str1, *utf8str2;
736 gtk_text_buffer_get_iter_at_offset(textbuf, &start_iter,
738 gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter,
740 utf8str1 = gtk_text_buffer_get_text(textbuf,
745 gtk_text_buffer_get_iter_at_offset(textbuf, &start_iter,
747 gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter,
749 utf8str2 = gtk_text_buffer_get_text(textbuf,
754 if (!utf8str1 || !utf8str2 || strcmp(utf8str1, utf8str2)) {
767 guint gtkut_text_buffer_str_compare(GtkTextBuffer *textbuf,
768 guint start_pos, guint text_len,
773 glong items_read = 0, items_written = 0;
775 GError *error = NULL;
779 wcs = g_utf8_to_ucs4(str, -1, &items_read, &items_written, &error);
781 g_warning("An error occured while converting a string from UTF-8 to UCS-4: %s\n",
785 if (!wcs || items_written <= 0) return 0;
786 len = (guint) items_written;
788 if (len > text_len - start_pos)
791 result = gtkut_text_buffer_match_string(textbuf, start_pos,
797 return result ? len : 0;
800 gboolean gtkut_text_buffer_is_uri_string(GtkTextBuffer *textbuf,
801 guint start_pos, guint text_len)
803 if (gtkut_text_buffer_str_compare(textbuf, start_pos, text_len, "http://") ||
804 gtkut_text_buffer_str_compare(textbuf, start_pos, text_len, "ftp://") ||
805 gtkut_text_buffer_str_compare(textbuf, start_pos, text_len, "https://") ||
806 gtkut_text_buffer_str_compare(textbuf, start_pos, text_len, "www."))
812 gchar *gtkut_text_view_get_selection(GtkTextView *textview)
814 GtkTextBuffer *buffer;
815 GtkTextIter start_iter, end_iter;
818 g_return_val_if_fail(GTK_IS_TEXT_VIEW(textview), NULL);
820 buffer = gtk_text_view_get_buffer(textview);
821 found = gtk_text_buffer_get_selection_bounds(buffer,
825 return gtk_text_buffer_get_text(buffer, &start_iter, &end_iter,
832 *\brief Tries to find a focused child using a lame strategy
834 GtkWidget *gtkut_get_focused_child(GtkContainer *parent)
836 GtkWidget *result = NULL;
837 GList *child_list = NULL;
840 g_return_val_if_fail(parent, NULL);
842 /* Get children list and see which has the focus. */
843 child_list = gtk_container_get_children(parent);
847 for (c = child_list; c != NULL; c = g_list_next(c)) {
848 if (c->data && GTK_IS_WIDGET(c->data)) {
849 if (GTK_WIDGET_HAS_FOCUS(GTK_WIDGET(c->data))) {
850 result = GTK_WIDGET(c->data);
856 /* See if the returned widget is a container itself; if it is,
857 * see if one of its children is focused. If the focused
858 * container has no focused child, it is itself a focusable
859 * child, and has focus. */
860 if (result && GTK_IS_CONTAINER(result)) {
861 GtkWidget *tmp = gtkut_get_focused_child(GTK_CONTAINER(result));
866 /* Try the same for each container in the chain */
867 for (c = child_list; c != NULL && !result; c = g_list_next(c)) {
868 if (c->data && GTK_IS_WIDGET(c->data)
869 && GTK_IS_CONTAINER(c->data)) {
870 result = gtkut_get_focused_child
871 (GTK_CONTAINER(c->data));
877 g_list_free(child_list);