2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 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>
39 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
47 #include "gtksctree.h"
49 #include "stock_pixmap.h"
51 #include "prefs_account.h"
54 gboolean gtkut_get_font_size(GtkWidget *widget,
55 gint *width, gint *height)
58 const gchar *str = "Abcdef";
60 g_return_val_if_fail(GTK_IS_WIDGET(widget), FALSE);
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);
66 *width = *width / g_utf8_strlen(str, -1);
67 g_object_unref(layout);
72 void gtkut_convert_int_to_gdk_color(gint rgbvalue, GdkColor *color)
74 g_return_if_fail(color != NULL);
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);
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)
87 g_return_if_fail(bbox != NULL);
88 g_return_if_fail(button1 != NULL);
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);
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);
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);
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);
114 #define CELL_SPACING 1
115 #define ROW_TOP_YPIXEL(clist, row) (((clist)->row_height * (row)) + \
116 (((row) + 1) * CELL_SPACING) + \
118 #define ROW_FROM_YPIXEL(clist, y) (((y) - (clist)->voffset) / \
119 ((clist)->row_height + CELL_SPACING))
121 void gtkut_ctree_node_move_if_on_the_edge(GtkCTree *ctree, GtkCTreeNode *node)
123 GtkCList *clist = GTK_CLIST(ctree);
125 GtkVisibility row_visibility, prev_row_visibility, next_row_visibility;
127 g_return_if_fail(ctree != NULL);
128 g_return_if_fail(node != NULL);
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);
136 if (row_visibility == GTK_VISIBILITY_NONE) {
137 gtk_clist_moveto(clist, row, -1, 0.5, 0);
140 if (row_visibility == GTK_VISIBILITY_FULL &&
141 prev_row_visibility == GTK_VISIBILITY_FULL &&
142 next_row_visibility == GTK_VISIBILITY_FULL)
144 if (prev_row_visibility != GTK_VISIBILITY_FULL &&
145 next_row_visibility != GTK_VISIBILITY_FULL)
148 if (prev_row_visibility != GTK_VISIBILITY_FULL) {
149 gtk_clist_moveto(clist, row, -1, 0.2, 0);
152 if (next_row_visibility != GTK_VISIBILITY_FULL) {
153 gtk_clist_moveto(clist, row, -1, 0.8, 0);
159 #undef ROW_TOP_YPIXEL
160 #undef ROW_FROM_YPIXEL
162 gint gtkut_ctree_get_nth_from_node(GtkCTree *ctree, GtkCTreeNode *node)
164 g_return_val_if_fail(ctree != NULL, -1);
165 g_return_val_if_fail(node != NULL, -1);
167 return g_list_position(GTK_CLIST(ctree)->row_list, (GList *)node);
170 /* get the next node, including the invisible one */
171 GtkCTreeNode *gtkut_ctree_node_next(GtkCTree *ctree, GtkCTreeNode *node)
173 GtkCTreeNode *parent;
175 if (!node) return NULL;
177 if (GTK_CTREE_ROW(node)->children)
178 return GTK_CTREE_ROW(node)->children;
180 if (GTK_CTREE_ROW(node)->sibling)
181 return GTK_CTREE_ROW(node)->sibling;
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;
192 /* get the previous node, including the invisible one */
193 GtkCTreeNode *gtkut_ctree_node_prev(GtkCTree *ctree, GtkCTreeNode *node)
198 if (!node) return NULL;
200 prev = GTK_CTREE_NODE_PREV(node);
201 if (prev == GTK_CTREE_ROW(node)->parent)
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;
214 gboolean gtkut_ctree_node_is_selected(GtkCTree *ctree, GtkCTreeNode *node)
216 GtkCList *clist = GTK_CLIST(ctree);
219 for (cur = clist->selection; cur != NULL; cur = cur->next) {
220 if (node == GTK_CTREE_NODE(cur->data))
227 GtkCTreeNode *gtkut_ctree_find_collapsed_parent(GtkCTree *ctree,
230 if (!node) return NULL;
232 while ((node = GTK_CTREE_ROW(node)->parent) != NULL) {
233 if (!GTK_CTREE_ROW(node)->expanded)
240 void gtkut_ctree_expand_parent_all(GtkCTree *ctree, GtkCTreeNode *node)
242 while ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
243 gtk_ctree_expand(ctree, node);
246 gboolean gtkut_ctree_node_is_parent(GtkCTreeNode *parent, GtkCTreeNode *node)
249 g_return_val_if_fail(node != NULL, FALSE);
250 g_return_val_if_fail(parent != NULL, FALSE);
254 if(GTK_CTREE_ROW(tmp)->parent && GTK_CTREE_ROW(tmp)->parent == parent)
256 tmp = GTK_CTREE_ROW(tmp)->parent;
262 void gtkut_ctree_set_focus_row(GtkCTree *ctree, GtkCTreeNode *node)
264 gtkut_clist_set_focus_row(GTK_CLIST(ctree),
265 gtkut_ctree_get_nth_from_node(ctree, node));
268 void gtkut_clist_set_focus_row(GtkCList *clist, gint row)
270 clist->focus_row = row;
271 GTKUT_CTREE_REFRESH(clist);
274 void gtkut_combo_set_items(GtkCombo *combo, const gchar *str1, ...)
278 GList *combo_items = NULL;
280 g_return_if_fail(str1 != NULL);
282 combo_items = g_list_append(combo_items, (gpointer)str1);
283 va_start(args, str1);
284 s = va_arg(args, gchar*);
286 combo_items = g_list_append(combo_items, (gpointer)s);
287 s = va_arg(args, gchar*);
291 gtk_combo_set_popdown_strings(combo, combo_items);
293 g_list_free(combo_items);
296 gchar *gtkut_editable_get_selection(GtkEditable *editable)
298 guint start_pos, end_pos;
301 g_return_val_if_fail(GTK_IS_EDITABLE(editable), NULL);
303 found = gtk_editable_get_selection_bounds(editable,
304 &start_pos, &end_pos);
306 return gtk_editable_get_chars(editable, start_pos, end_pos);
312 * Walk through the widget tree and disclaim the selection from all currently
313 * realized GtkEditable widgets.
315 static void gtkut_check_before_remove(GtkWidget *widget, gpointer unused)
317 g_return_if_fail(widget != NULL);
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);
326 if (GTK_IS_EDITABLE(widget))
327 gtk_editable_claim_selection(GTK_EDITABLE(widget),
328 FALSE, GDK_CURRENT_TIME);
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).
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.
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.
348 void gtkut_container_remove(GtkContainer *container, GtkWidget *widget)
350 gtkut_check_before_remove(widget, NULL);
351 gtk_container_remove(container, widget);
354 void gtkut_window_popup(GtkWidget *window)
356 gint x, y, sx, sy, new_x, new_y;
358 g_return_if_fail(window != NULL);
359 g_return_if_fail(window->window != NULL);
361 sx = gdk_screen_width();
362 sy = gdk_screen_height();
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);
370 gdk_window_raise(window->window);
371 gdk_window_show(window->window);
374 void gtkut_widget_get_uposition(GtkWidget *widget, gint *px, gint *py)
379 g_return_if_fail(widget != NULL);
380 g_return_if_fail(widget->window != NULL);
382 sx = gdk_screen_width();
383 sy = gdk_screen_height();
385 /* gdk_window_get_root_origin ever return *rootwindow*'s position */
386 gdk_window_get_root_origin(widget->window, &x, &y);
388 x %= sx; if (x < 0) x = 0;
389 y %= sy; if (y < 0) y = 0;
394 void gtkut_widget_disable_theme_engine(GtkWidget *widget)
398 GtkStyle *style, *new_style;
400 style = gtk_widget_get_style(widget);
403 GtkThemeEngine *engine;
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);
414 void gtkut_widget_wait_for_draw(GtkWidget *widget)
416 if (!GTK_WIDGET_DRAWABLE(widget)) return;
418 while (gtk_events_pending())
419 gtk_main_iteration();
422 static void gtkut_clist_bindings_add(GtkWidget *clist)
424 GtkBindingSet *binding_set;
426 binding_set = gtk_binding_set_by_class
427 (GTK_CLIST_GET_CLASS(clist));
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,
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,
439 void gtkut_widget_init(void)
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));
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));
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));
462 void gtkut_widget_set_app_icon(GtkWidget *widget)
464 #include "pixmaps/sylpheed.xpm"
465 static GdkPixmap *sylpheedxpm;
466 static GdkBitmap *sylpheedxpmmask;
468 g_return_if_fail(widget != NULL);
469 g_return_if_fail(widget->window != NULL);
471 PIXMAP_CREATE(widget, sylpheedxpm, sylpheedxpmmask, sylpheed_xpm);
473 gdk_window_set_icon(widget->window, NULL, sylpheedxpm, sylpheedxpmmask);
476 void gtkut_widget_set_composer_icon(GtkWidget *widget)
478 static GdkPixmap *xpm;
479 static GdkBitmap *bmp;
481 g_return_if_fail(widget != NULL);
482 g_return_if_fail(widget->window != NULL);
484 stock_pixmap_gdk(widget, STOCK_PIXMAP_MAIL_COMPOSE, &xpm, &bmp);
486 gdk_window_set_icon(widget->window, NULL, xpm, bmp);
489 GtkWidget *gtkut_account_menu_new(GList *ac_list,
490 GtkSignalFunc callback,
496 g_return_val_if_fail(ac_list != NULL, NULL);
498 menu = gtk_menu_new();
500 for (cur_ac = ac_list; cur_ac != NULL; cur_ac = cur_ac->next) {
503 PrefsAccount *account;
505 account = (PrefsAccount *) cur_ac->data;
507 name = g_strdup_printf("%s: %s <%s>",
508 account->account_name,
512 name = g_strdup_printf("%s: %s",
513 account->account_name,
515 MENUITEM_ADD(menu, menuitem, name, account->account_id);
517 if (callback != NULL)
518 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
525 void gtkut_set_widget_bgcolor_rgb(GtkWidget *widget, guint rgbvalue)
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);
539 #if 1 /* FIXME_GTK2 */
540 gboolean gtkut_text_buffer_match_string(GtkTextBuffer *textbuf, gint pos, gunichar *wcs,
541 gint len, gboolean case_sens)
543 GtkTextIter start_iter, end_iter;
545 gint match_count = 0;
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);
550 utf8str = gtk_text_buffer_get_text(textbuf, &start_iter, &end_iter, FALSE);
551 if (!utf8str) return FALSE;
553 if ((gint) g_utf8_strlen(utf8str, -1) != len) {
558 for (; match_count < len; pos++, match_count++) {
559 gchar *ptr = g_utf8_offset_to_pointer(utf8str, match_count);
563 ch = g_utf8_get_char(ptr);
566 if (ch != wcs[match_count])
569 if (g_unichar_tolower(ch) !=
570 g_unichar_tolower(wcs[match_count]))
577 if (match_count == len)
583 guint gtkut_text_buffer_str_compare_n(GtkTextBuffer *textbuf,
584 guint pos1, guint pos2,
585 guint len, guint text_len)
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;
593 gtk_text_buffer_get_iter_at_offset(textbuf, &start_iter,
595 gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter,
597 utf8str1 = gtk_text_buffer_get_text(textbuf,
602 gtk_text_buffer_get_iter_at_offset(textbuf, &start_iter,
604 gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter,
606 utf8str2 = gtk_text_buffer_get_text(textbuf,
611 if (!utf8str1 || !utf8str2 || strcmp(utf8str1, utf8str2)) {
624 guint gtkut_text_buffer_str_compare(GtkTextBuffer *textbuf,
625 guint start_pos, guint text_len,
630 glong items_read = 0, items_written = 0;
632 GError *error = NULL;
636 wcs = g_utf8_to_ucs4(str, -1, &items_read, &items_written, &error);
638 g_warning("An error occured while converting a string from UTF-8 to UCS-4: %s\n",
642 if (!wcs || items_written <= 0) return 0;
643 len = (guint) items_written;
645 if (len > text_len - start_pos)
648 result = gtkut_text_buffer_match_string(textbuf, start_pos,
654 return result ? len : 0;
657 gboolean gtkut_text_buffer_is_uri_string(GtkTextBuffer *textbuf,
658 guint start_pos, guint text_len)
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."))
669 gchar *gtkut_text_view_get_selection(GtkTextView *textview)
671 GtkTextBuffer *buffer;
672 GtkTextIter start_iter, end_iter;
675 g_return_val_if_fail(GTK_IS_TEXT_VIEW(textview), NULL);
677 buffer = gtk_text_view_get_buffer(textview);
678 found = gtk_text_buffer_get_selection_bounds(buffer,
682 return gtk_text_buffer_get_text(buffer, &start_iter, &end_iter,
687 #endif /* FIXME_GTK2 */