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>
33 /* #include <gtk/gtkthemes.h> */
34 #include <gtk/gtkbindings.h>
35 #include <gtk/gtkitemfactory.h>
40 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
48 #include "gtksctree.h"
50 #include "stock_pixmap.h"
52 #include "prefs_account.h"
55 gboolean gtkut_get_font_size(GtkWidget *widget,
56 gint *width, gint *height)
59 const gchar *str = "Abcdef";
61 g_return_val_if_fail(GTK_IS_WIDGET(widget), FALSE);
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);
67 *width = *width / g_utf8_strlen(str, -1);
68 g_object_unref(layout);
73 void gtkut_convert_int_to_gdk_color(gint rgbvalue, GdkColor *color)
75 g_return_if_fail(color != NULL);
78 color->red = (int) (((gdouble)((rgbvalue & 0xff0000) >> 16) / 255.0) * 65535.0);
79 color->green = (int) (((gdouble)((rgbvalue & 0x00ff00) >> 8) / 255.0) * 65535.0);
80 color->blue = (int) (((gdouble) (rgbvalue & 0x0000ff) / 255.0) * 65535.0);
83 void gtkut_button_set_create(GtkWidget **bbox,
84 GtkWidget **button1, const gchar *label1,
85 GtkWidget **button2, const gchar *label2,
86 GtkWidget **button3, const gchar *label3)
88 g_return_if_fail(bbox != NULL);
89 g_return_if_fail(button1 != NULL);
91 *bbox = gtk_hbutton_box_new();
92 gtk_button_box_set_layout(GTK_BUTTON_BOX(*bbox), GTK_BUTTONBOX_END);
93 gtk_box_set_spacing(GTK_BOX(*bbox), 5);
95 *button1 = gtk_button_new_with_label(label1);
96 GTK_WIDGET_SET_FLAGS(*button1, GTK_CAN_DEFAULT);
97 gtk_box_pack_start(GTK_BOX(*bbox), *button1, TRUE, TRUE, 0);
98 gtk_widget_show(*button1);
101 *button2 = gtk_button_new_with_label(label2);
102 GTK_WIDGET_SET_FLAGS(*button2, GTK_CAN_DEFAULT);
103 gtk_box_pack_start(GTK_BOX(*bbox), *button2, TRUE, TRUE, 0);
104 gtk_widget_show(*button2);
108 *button3 = gtk_button_new_with_label(label3);
109 GTK_WIDGET_SET_FLAGS(*button3, GTK_CAN_DEFAULT);
110 gtk_box_pack_start(GTK_BOX(*bbox), *button3, TRUE, TRUE, 0);
111 gtk_widget_show(*button3);
115 static void combo_button_size_request(GtkWidget *widget,
116 GtkRequisition *requisition,
119 ComboButton *combo = (ComboButton *)data;
121 if (combo->arrow->allocation.height != requisition->height)
122 gtk_widget_set_usize(combo->arrow, -1, requisition->height);
125 static void combo_button_enter(GtkWidget *widget, gpointer data)
127 ComboButton *combo = (ComboButton *)data;
129 if (GTK_WIDGET_STATE(combo->arrow) != GTK_STATE_PRELIGHT) {
130 gtk_widget_set_state(combo->arrow, GTK_STATE_PRELIGHT);
131 gtk_widget_queue_draw(combo->arrow);
133 if (GTK_WIDGET_STATE(combo->button) != GTK_STATE_PRELIGHT) {
134 gtk_widget_set_state(combo->button, GTK_STATE_PRELIGHT);
135 gtk_widget_queue_draw(combo->button);
139 static void combo_button_leave(GtkWidget *widget, gpointer data)
141 ComboButton *combo = (ComboButton *)data;
143 if (GTK_WIDGET_STATE(combo->arrow) != GTK_STATE_NORMAL) {
144 gtk_widget_set_state(combo->arrow, GTK_STATE_NORMAL);
145 gtk_widget_queue_draw(combo->arrow);
147 if (GTK_WIDGET_STATE(combo->button) != GTK_STATE_NORMAL) {
148 gtk_widget_set_state(combo->button, GTK_STATE_NORMAL);
149 gtk_widget_queue_draw(combo->button);
153 static gint combo_button_arrow_pressed(GtkWidget *widget, GdkEventButton *event,
156 ComboButton *combo = (ComboButton *)data;
158 if (!event) return FALSE;
160 gtk_menu_popup(GTK_MENU(combo->menu), NULL, NULL,
161 menu_button_position, combo->button,
162 event->button, event->time);
167 static void combo_button_destroy(GtkWidget *widget, gpointer data)
169 ComboButton *combo = (ComboButton *)data;
171 gtk_object_destroy(GTK_OBJECT(combo->factory));
175 ComboButton *gtkut_combo_button_create(GtkWidget *button,
176 GtkItemFactoryEntry *entries,
177 gint n_entries, const gchar *path,
183 combo = g_new0(ComboButton, 1);
185 combo->arrow = gtk_button_new();
186 arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
187 gtk_container_add(GTK_CONTAINER(combo->arrow), arrow);
188 GTK_WIDGET_UNSET_FLAGS(combo->arrow, GTK_CAN_FOCUS);
189 gtk_widget_show_all(combo->arrow);
191 combo->button = button;
192 combo->menu = menu_create_items(entries, n_entries, path,
193 &combo->factory, data);
196 g_signal_connect(G_OBJECT(combo->button), "size_request",
197 G_CALLBACK(combo_button_size_request), combo);
198 g_signal_connect(G_OBJECT(combo->button), "enter",
199 G_CALLBACK(combo_button_enter), combo);
200 g_signal_connect(G_OBJECT(combo->button), "leave",
201 G_CALLBACK(combo_button_leave), combo);
202 g_signal_connect(G_OBJECT(combo->arrow), "enter",
203 G_CALLBACK(combo_button_enter), combo);
204 g_signal_connect(G_OBJECT(combo->arrow), "leave",
205 G_CALLBACK(combo_button_leave), combo);
206 g_signal_connect(G_OBJECT(combo->arrow), "button_press_event",
207 G_CALLBACK(combo_button_arrow_pressed), combo);
208 g_signal_connect(G_OBJECT(combo->arrow), "destroy",
209 G_CALLBACK(combo_button_destroy), combo);
214 #define CELL_SPACING 1
215 #define ROW_TOP_YPIXEL(clist, row) (((clist)->row_height * (row)) + \
216 (((row) + 1) * CELL_SPACING) + \
218 #define ROW_FROM_YPIXEL(clist, y) (((y) - (clist)->voffset) / \
219 ((clist)->row_height + CELL_SPACING))
221 void gtkut_ctree_node_move_if_on_the_edge(GtkCTree *ctree, GtkCTreeNode *node)
223 GtkCList *clist = GTK_CLIST(ctree);
225 GtkVisibility row_visibility, prev_row_visibility, next_row_visibility;
227 g_return_if_fail(ctree != NULL);
228 g_return_if_fail(node != NULL);
230 row = g_list_position(clist->row_list, (GList *)node);
231 if (row < 0 || row >= clist->rows || clist->row_height == 0) return;
232 row_visibility = gtk_clist_row_is_visible(clist, row);
233 prev_row_visibility = gtk_clist_row_is_visible(clist, row - 1);
234 next_row_visibility = gtk_clist_row_is_visible(clist, row + 1);
236 if (row_visibility == GTK_VISIBILITY_NONE) {
237 gtk_clist_moveto(clist, row, -1, 0.5, 0);
240 if (row_visibility == GTK_VISIBILITY_FULL &&
241 prev_row_visibility == GTK_VISIBILITY_FULL &&
242 next_row_visibility == GTK_VISIBILITY_FULL)
244 if (prev_row_visibility != GTK_VISIBILITY_FULL &&
245 next_row_visibility != GTK_VISIBILITY_FULL)
248 if (prev_row_visibility != GTK_VISIBILITY_FULL) {
249 gtk_clist_moveto(clist, row, -1, 0.2, 0);
252 if (next_row_visibility != GTK_VISIBILITY_FULL) {
253 gtk_clist_moveto(clist, row, -1, 0.8, 0);
259 #undef ROW_TOP_YPIXEL
260 #undef ROW_FROM_YPIXEL
262 gint gtkut_ctree_get_nth_from_node(GtkCTree *ctree, GtkCTreeNode *node)
264 g_return_val_if_fail(ctree != NULL, -1);
265 g_return_val_if_fail(node != NULL, -1);
267 return g_list_position(GTK_CLIST(ctree)->row_list, (GList *)node);
270 /* get the next node, including the invisible one */
271 GtkCTreeNode *gtkut_ctree_node_next(GtkCTree *ctree, GtkCTreeNode *node)
273 GtkCTreeNode *parent;
275 if (!node) return NULL;
277 if (GTK_CTREE_ROW(node)->children)
278 return GTK_CTREE_ROW(node)->children;
280 if (GTK_CTREE_ROW(node)->sibling)
281 return GTK_CTREE_ROW(node)->sibling;
283 for (parent = GTK_CTREE_ROW(node)->parent; parent != NULL;
284 parent = GTK_CTREE_ROW(parent)->parent) {
285 if (GTK_CTREE_ROW(parent)->sibling)
286 return GTK_CTREE_ROW(parent)->sibling;
292 /* get the previous node, including the invisible one */
293 GtkCTreeNode *gtkut_ctree_node_prev(GtkCTree *ctree, GtkCTreeNode *node)
298 if (!node) return NULL;
300 prev = GTK_CTREE_NODE_PREV(node);
301 if (prev == GTK_CTREE_ROW(node)->parent)
305 while (GTK_CTREE_ROW(child)->children != NULL) {
306 child = GTK_CTREE_ROW(child)->children;
307 while (GTK_CTREE_ROW(child)->sibling != NULL)
308 child = GTK_CTREE_ROW(child)->sibling;
314 gboolean gtkut_ctree_node_is_selected(GtkCTree *ctree, GtkCTreeNode *node)
316 GtkCList *clist = GTK_CLIST(ctree);
319 for (cur = clist->selection; cur != NULL; cur = cur->next) {
320 if (node == GTK_CTREE_NODE(cur->data))
327 GtkCTreeNode *gtkut_ctree_find_collapsed_parent(GtkCTree *ctree,
330 if (!node) return NULL;
332 while ((node = GTK_CTREE_ROW(node)->parent) != NULL) {
333 if (!GTK_CTREE_ROW(node)->expanded)
340 void gtkut_ctree_expand_parent_all(GtkCTree *ctree, GtkCTreeNode *node)
342 while ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
343 gtk_ctree_expand(ctree, node);
346 gboolean gtkut_ctree_node_is_parent(GtkCTreeNode *parent, GtkCTreeNode *node)
349 g_return_val_if_fail(node != NULL, FALSE);
350 g_return_val_if_fail(parent != NULL, FALSE);
354 if(GTK_CTREE_ROW(tmp)->parent && GTK_CTREE_ROW(tmp)->parent == parent)
356 tmp = GTK_CTREE_ROW(tmp)->parent;
362 void gtkut_ctree_set_focus_row(GtkCTree *ctree, GtkCTreeNode *node)
364 gtkut_clist_set_focus_row(GTK_CLIST(ctree),
365 gtkut_ctree_get_nth_from_node(ctree, node));
368 void gtkut_clist_set_focus_row(GtkCList *clist, gint row)
370 clist->focus_row = row;
371 GTKUT_CTREE_REFRESH(clist);
374 void gtkut_combo_set_items(GtkCombo *combo, const gchar *str1, ...)
378 GList *combo_items = NULL;
380 g_return_if_fail(str1 != NULL);
382 combo_items = g_list_append(combo_items, (gpointer)str1);
383 va_start(args, str1);
384 s = va_arg(args, gchar*);
386 combo_items = g_list_append(combo_items, (gpointer)s);
387 s = va_arg(args, gchar*);
391 gtk_combo_set_popdown_strings(combo, combo_items);
393 g_list_free(combo_items);
396 gchar *gtkut_editable_get_selection(GtkEditable *editable)
398 guint start_pos, end_pos;
401 g_return_val_if_fail(GTK_IS_EDITABLE(editable), NULL);
403 found = gtk_editable_get_selection_bounds(editable,
404 &start_pos, &end_pos);
406 return gtk_editable_get_chars(editable, start_pos, end_pos);
411 void gtkut_editable_disable_im(GtkEditable *editable)
413 g_return_if_fail(editable != NULL);
417 gdk_ic_destroy(editable->ic);
420 if (editable->ic_attr) {
421 gdk_ic_attr_destroy(editable->ic_attr);
422 editable->ic_attr = NULL;
428 * Walk through the widget tree and disclaim the selection from all currently
429 * realized GtkEditable widgets.
431 static void gtkut_check_before_remove(GtkWidget *widget, gpointer unused)
433 g_return_if_fail(widget != NULL);
435 if (!GTK_WIDGET_REALIZED(widget))
436 return; /* all nested widgets must be unrealized too */
437 if (GTK_IS_CONTAINER(widget))
438 gtk_container_forall(GTK_CONTAINER(widget),
439 gtkut_check_before_remove, NULL);
442 if (GTK_IS_EDITABLE(widget))
443 gtk_editable_claim_selection(GTK_EDITABLE(widget),
444 FALSE, GDK_CURRENT_TIME);
449 * Wrapper around gtk_container_remove to work around a bug in GtkText and
450 * GtkEntry (in all GTK+ versions up to and including at least 1.2.10).
452 * The problem is that unrealizing a GtkText or GtkEntry widget which has the
453 * active selection completely messes up selection handling, leading to
454 * non-working selections and crashes. Removing a widget from its container
455 * implies unrealizing it and all its child widgets; this triggers the bug if
456 * the removed widget or any of its children is GtkText or GtkEntry. As a
457 * workaround, this function walks through the widget subtree before removing
458 * and disclaims the selection from all GtkEditable widgets found.
460 * A similar workaround may be needed for gtk_widget_reparent(); currently it
461 * is not necessary because Sylpheed does not use gtk_widget_reparent() for
462 * GtkEditable widgets or containers holding such widgets.
464 void gtkut_container_remove(GtkContainer *container, GtkWidget *widget)
466 gtkut_check_before_remove(widget, NULL);
467 gtk_container_remove(container, widget);
470 void gtkut_window_popup(GtkWidget *window)
472 gint x, y, sx, sy, new_x, new_y;
474 g_return_if_fail(window != NULL);
475 g_return_if_fail(window->window != NULL);
477 sx = gdk_screen_width();
478 sy = gdk_screen_height();
480 gdk_window_get_origin(window->window, &x, &y);
481 new_x = x % sx; if (new_x < 0) new_x = 0;
482 new_y = y % sy; if (new_y < 0) new_y = 0;
483 if (new_x != x || new_y != y)
484 gdk_window_move(window->window, new_x, new_y);
486 gdk_window_raise(window->window);
487 gdk_window_show(window->window);
490 void gtkut_widget_get_uposition(GtkWidget *widget, gint *px, gint *py)
495 g_return_if_fail(widget != NULL);
496 g_return_if_fail(widget->window != NULL);
498 sx = gdk_screen_width();
499 sy = gdk_screen_height();
501 /* gdk_window_get_root_origin ever return *rootwindow*'s position */
502 gdk_window_get_root_origin(widget->window, &x, &y);
504 x %= sx; if (x < 0) x = 0;
505 y %= sy; if (y < 0) y = 0;
510 void gtkut_widget_disable_theme_engine(GtkWidget *widget)
514 GtkStyle *style, *new_style;
516 style = gtk_widget_get_style(widget);
519 GtkThemeEngine *engine;
521 engine = style->engine;
522 style->engine = NULL;
523 new_style = gtk_style_copy(style);
524 style->engine = engine;
525 gtk_widget_set_style(widget, new_style);
530 void gtkut_widget_wait_for_draw(GtkWidget *widget)
532 if (!GTK_WIDGET_DRAWABLE(widget)) return;
534 while (gtk_events_pending())
535 gtk_main_iteration();
538 static void gtkut_clist_bindings_add(GtkWidget *clist)
540 GtkBindingSet *binding_set;
542 binding_set = gtk_binding_set_by_class
543 (GTK_CLIST_GET_CLASS(clist));
545 gtk_binding_entry_add_signal(binding_set, GDK_n, GDK_CONTROL_MASK,
546 "scroll_vertical", 2,
547 G_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
549 gtk_binding_entry_add_signal(binding_set, GDK_p, GDK_CONTROL_MASK,
550 "scroll_vertical", 2,
551 G_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
555 void gtkut_widget_init(void)
559 clist = gtk_clist_new(1);
560 g_object_ref(G_OBJECT(clist));
561 gtk_object_sink(GTK_OBJECT(clist));
562 gtkut_clist_bindings_add(clist);
563 g_object_unref(G_OBJECT(clist));
565 clist = gtk_ctree_new(1, 0);
566 g_object_ref(G_OBJECT(clist));
567 gtk_object_sink(GTK_OBJECT(clist));
568 gtkut_clist_bindings_add(clist);
569 g_object_unref(G_OBJECT(clist));
571 clist = gtk_sctree_new_with_titles(1, 0, NULL);
572 g_object_ref(G_OBJECT(clist));
573 gtk_object_sink(GTK_OBJECT(clist));
574 gtkut_clist_bindings_add(clist);
575 g_object_unref(G_OBJECT(clist));
578 void gtkut_widget_set_app_icon(GtkWidget *widget)
580 #include "pixmaps/sylpheed.xpm"
581 static GdkPixmap *sylpheedxpm;
582 static GdkBitmap *sylpheedxpmmask;
584 g_return_if_fail(widget != NULL);
585 g_return_if_fail(widget->window != NULL);
587 PIXMAP_CREATE(widget, sylpheedxpm, sylpheedxpmmask, sylpheed_xpm);
589 gdk_window_set_icon(widget->window, NULL, sylpheedxpm, sylpheedxpmmask);
592 void gtkut_widget_set_composer_icon(GtkWidget *widget)
594 static GdkPixmap *xpm;
595 static GdkBitmap *bmp;
597 g_return_if_fail(widget != NULL);
598 g_return_if_fail(widget->window != NULL);
600 stock_pixmap_gdk(widget, STOCK_PIXMAP_MAIL_COMPOSE, &xpm, &bmp);
602 gdk_window_set_icon(widget->window, NULL, xpm, bmp);
605 GtkWidget *gtkut_account_menu_new(GList *ac_list,
612 g_return_val_if_fail(ac_list != NULL, NULL);
614 menu = gtk_menu_new();
616 for (cur_ac = ac_list; cur_ac != NULL; cur_ac = cur_ac->next) {
619 PrefsAccount *account;
621 account = (PrefsAccount *) cur_ac->data;
623 name = g_strdup_printf("%s: %s <%s>",
624 account->account_name,
628 name = g_strdup_printf("%s: %s",
629 account->account_name,
631 MENUITEM_ADD(menu, menuitem, name, account->account_id);
633 if (callback != NULL)
634 g_signal_connect(G_OBJECT(menuitem), "activate",
640 void gtkut_set_widget_bgcolor_rgb(GtkWidget *widget, guint rgbvalue)
645 gtkut_convert_int_to_gdk_color(rgbvalue, &gdk_color);
646 newstyle = gtk_style_copy(gtk_widget_get_default_style());
647 newstyle->bg[GTK_STATE_NORMAL] = gdk_color;
648 newstyle->bg[GTK_STATE_PRELIGHT] = gdk_color;
649 newstyle->bg[GTK_STATE_ACTIVE] = gdk_color;
650 gtk_widget_set_style(widget, newstyle);
654 #if 1 /* FIXME_GTK2 */
655 gboolean gtkut_text_buffer_match_string(GtkTextBuffer *textbuf, gint pos, gunichar *wcs,
656 gint len, gboolean case_sens)
658 GtkTextIter start_iter, end_iter;
660 gint match_count = 0;
662 gtk_text_buffer_get_iter_at_offset(textbuf, &start_iter, pos);
663 gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter, pos + len);
665 utf8str = gtk_text_buffer_get_text(textbuf, &start_iter, &end_iter, FALSE);
666 if (!utf8str) return FALSE;
668 if ((gint) g_utf8_strlen(utf8str, -1) != len) {
673 for (; match_count < len; pos++, match_count++) {
674 gchar *ptr = g_utf8_offset_to_pointer(utf8str, match_count);
678 ch = g_utf8_get_char(ptr);
681 if (ch != wcs[match_count])
684 if (g_unichar_tolower(ch) !=
685 g_unichar_tolower(wcs[match_count]))
692 if (match_count == len)
698 guint gtkut_text_buffer_str_compare_n(GtkTextBuffer *textbuf,
699 guint pos1, guint pos2,
700 guint len, guint text_len)
704 for (i = 0; i < len && pos1 + i < text_len && pos2 + i < text_len; i++) {
705 GtkTextIter start_iter, end_iter;
706 gchar *utf8str1, *utf8str2;
708 gtk_text_buffer_get_iter_at_offset(textbuf, &start_iter,
710 gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter,
712 utf8str1 = gtk_text_buffer_get_text(textbuf,
717 gtk_text_buffer_get_iter_at_offset(textbuf, &start_iter,
719 gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter,
721 utf8str2 = gtk_text_buffer_get_text(textbuf,
726 if (!utf8str1 || !utf8str2 || strcmp(utf8str1, utf8str2)) {
739 guint gtkut_text_buffer_str_compare(GtkTextBuffer *textbuf,
740 guint start_pos, guint text_len,
745 glong items_read = 0, items_written = 0;
747 GError *error = NULL;
751 wcs = g_utf8_to_ucs4(str, -1, &items_read, &items_written, &error);
753 g_warning("An error occured while converting a string from UTF-8 to UCS-4: %s\n",
757 if (!wcs || items_written <= 0) return 0;
758 len = (guint) items_written;
760 if (len > text_len - start_pos)
763 result = gtkut_text_buffer_match_string(textbuf, start_pos,
769 return result ? len : 0;
772 gboolean gtkut_text_buffer_is_uri_string(GtkTextBuffer *textbuf,
773 guint start_pos, guint text_len)
775 if (gtkut_text_buffer_str_compare(textbuf, start_pos, text_len, "http://") ||
776 gtkut_text_buffer_str_compare(textbuf, start_pos, text_len, "ftp://") ||
777 gtkut_text_buffer_str_compare(textbuf, start_pos, text_len, "https://") ||
778 gtkut_text_buffer_str_compare(textbuf, start_pos, text_len, "www."))
784 gchar *gtkut_text_view_get_selection(GtkTextView *textview)
786 GtkTextBuffer *buffer;
787 GtkTextIter start_iter, end_iter;
790 g_return_val_if_fail(GTK_IS_TEXT_VIEW(textview), NULL);
792 buffer = gtk_text_view_get_buffer(textview);
793 found = gtk_text_buffer_get_selection_bounds(buffer,
797 return gtk_text_buffer_get_text(buffer, &start_iter, &end_iter,
802 #endif /* FIXME_GTK2 */