2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2001 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/gtkthemes.h>
33 #include <gtk/gtkbindings.h>
36 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
44 #include "gtksctree.h"
47 gint gtkut_get_font_width(GdkFont *font)
52 if (conv_get_current_charset() == C_UTF_8)
57 width = gdk_string_width(font, str);
63 gint gtkut_get_font_height(GdkFont *font)
68 if (conv_get_current_charset() == C_UTF_8)
73 height = gdk_string_height(font, str);
78 void gtkut_convert_int_to_gdk_color(gint rgbvalue, GdkColor *color)
80 g_return_if_fail(color != NULL);
83 color->red = (int) (((gdouble)((rgbvalue & 0xff0000) >> 16) / 255.0) * 65535.0);
84 color->green = (int) (((gdouble)((rgbvalue & 0x00ff00) >> 8) / 255.0) * 65535.0);
85 color->blue = (int) (((gdouble) (rgbvalue & 0x0000ff) / 255.0) * 65535.0);
88 void gtkut_button_set_create(GtkWidget **bbox,
89 GtkWidget **button1, const gchar *label1,
90 GtkWidget **button2, const gchar *label2,
91 GtkWidget **button3, const gchar *label3)
93 g_return_if_fail(bbox != NULL);
94 g_return_if_fail(button1 != NULL);
96 *bbox = gtk_hbutton_box_new();
97 gtk_button_box_set_layout(GTK_BUTTON_BOX(*bbox), GTK_BUTTONBOX_END);
98 gtk_button_box_set_spacing(GTK_BUTTON_BOX(*bbox), 5);
100 *button1 = gtk_button_new_with_label(label1);
101 GTK_WIDGET_SET_FLAGS(*button1, GTK_CAN_DEFAULT);
102 gtk_box_pack_start(GTK_BOX(*bbox), *button1, TRUE, TRUE, 0);
103 gtk_widget_show(*button1);
106 *button2 = gtk_button_new_with_label(label2);
107 GTK_WIDGET_SET_FLAGS(*button2, GTK_CAN_DEFAULT);
108 gtk_box_pack_start(GTK_BOX(*bbox), *button2, TRUE, TRUE, 0);
109 gtk_widget_show(*button2);
113 *button3 = gtk_button_new_with_label(label3);
114 GTK_WIDGET_SET_FLAGS(*button3, GTK_CAN_DEFAULT);
115 gtk_box_pack_start(GTK_BOX(*bbox), *button3, TRUE, TRUE, 0);
116 gtk_widget_show(*button3);
120 #define CELL_SPACING 1
121 #define ROW_TOP_YPIXEL(clist, row) (((clist)->row_height * (row)) + \
122 (((row) + 1) * CELL_SPACING) + \
124 #define ROW_FROM_YPIXEL(clist, y) (((y) - (clist)->voffset) / \
125 ((clist)->row_height + CELL_SPACING))
127 void gtkut_ctree_node_move_if_on_the_edge(GtkCTree *ctree, GtkCTreeNode *node)
129 GtkCList *clist = GTK_CLIST(ctree);
131 GtkVisibility row_visibility, prev_row_visibility, next_row_visibility;
133 g_return_if_fail(ctree != NULL);
134 g_return_if_fail(node != NULL);
136 row = g_list_position(clist->row_list, (GList *)node);
137 if (row < 0 || row >= clist->rows || clist->row_height == 0) return;
138 row_visibility = gtk_clist_row_is_visible(clist, row);
139 prev_row_visibility = gtk_clist_row_is_visible(clist, row - 1);
140 next_row_visibility = gtk_clist_row_is_visible(clist, row + 1);
142 if (row_visibility == GTK_VISIBILITY_NONE) {
143 gtk_clist_moveto(clist, row, -1, 0.5, 0);
146 if (row_visibility == GTK_VISIBILITY_FULL &&
147 prev_row_visibility == GTK_VISIBILITY_FULL &&
148 next_row_visibility == GTK_VISIBILITY_FULL)
150 if (prev_row_visibility != GTK_VISIBILITY_FULL &&
151 next_row_visibility != GTK_VISIBILITY_FULL)
154 if (prev_row_visibility != GTK_VISIBILITY_FULL) {
155 gtk_clist_moveto(clist, row, -1, 0.2, 0);
158 if (next_row_visibility != GTK_VISIBILITY_FULL) {
159 gtk_clist_moveto(clist, row, -1, 0.8, 0);
165 #undef ROW_TOP_YPIXEL
166 #undef ROW_FROM_YPIXEL
168 gint gtkut_ctree_get_nth_from_node(GtkCTree *ctree, GtkCTreeNode *node)
170 g_return_val_if_fail(ctree != NULL, -1);
171 g_return_val_if_fail(node != NULL, -1);
173 return g_list_position(GTK_CLIST(ctree)->row_list, (GList *)node);
176 /* get the next node, including the invisible one */
177 GtkCTreeNode *gtkut_ctree_node_next(GtkCTree *ctree, GtkCTreeNode *node)
179 GtkCTreeNode *parent;
181 if (!node) return NULL;
183 if (GTK_CTREE_ROW(node)->children)
184 return GTK_CTREE_ROW(node)->children;
186 if (GTK_CTREE_ROW(node)->sibling)
187 return GTK_CTREE_ROW(node)->sibling;
189 for (parent = GTK_CTREE_ROW(node)->parent; parent != NULL;
190 parent = GTK_CTREE_ROW(parent)->parent) {
191 if (GTK_CTREE_ROW(parent)->sibling)
192 return GTK_CTREE_ROW(parent)->sibling;
198 GtkCTreeNode *gtkut_ctree_find_collapsed_parent(GtkCTree *ctree,
201 if (!node) return NULL;
203 while ((node = GTK_CTREE_ROW(node)->parent) != NULL) {
204 if (!GTK_CTREE_ROW(node)->expanded)
211 void gtkut_ctree_expand_parent_all(GtkCTree *ctree, GtkCTreeNode *node)
213 while ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
214 gtk_ctree_expand(ctree, node);
217 void gtkut_ctree_set_focus_row(GtkCTree *ctree, GtkCTreeNode *node)
219 gtkut_clist_set_focus_row(GTK_CLIST(ctree),
220 gtkut_ctree_get_nth_from_node(ctree, node));
223 void gtkut_clist_set_focus_row(GtkCList *clist, gint row)
225 clist->focus_row = row;
226 GTKUT_CTREE_REFRESH(clist);
229 void gtkut_combo_set_items(GtkCombo *combo, const gchar *str1, ...)
233 GList *combo_items = NULL;
235 g_return_if_fail(str1 != NULL);
237 combo_items = g_list_append(combo_items, (gpointer)str1);
238 va_start(args, str1);
239 s = va_arg(args, gchar*);
241 combo_items = g_list_append(combo_items, (gpointer)s);
242 s = va_arg(args, gchar*);
246 gtk_combo_set_popdown_strings(combo, combo_items);
248 g_list_free(combo_items);
252 * Walk through the widget tree and disclaim the selection from all currently
253 * realized GtkEditable widgets.
255 static void gtkut_check_before_remove(GtkWidget *widget, gpointer unused)
257 g_return_if_fail(widget != NULL);
259 if (!GTK_WIDGET_REALIZED(widget))
260 return; /* all nested widgets must be unrealized too */
261 if (GTK_IS_CONTAINER(widget))
262 gtk_container_forall(GTK_CONTAINER(widget),
263 gtkut_check_before_remove, NULL);
264 if (GTK_IS_EDITABLE(widget))
265 gtk_editable_claim_selection(GTK_EDITABLE(widget),
266 FALSE, GDK_CURRENT_TIME);
270 * Wrapper around gtk_container_remove to work around a bug in GtkText and
271 * GtkEntry (in all GTK+ versions up to and including at least 1.2.10).
273 * The problem is that unrealizing a GtkText or GtkEntry widget which has the
274 * active selection completely messes up selection handling, leading to
275 * non-working selections and crashes. Removing a widget from its container
276 * implies unrealizing it and all its child widgets; this triggers the bug if
277 * the removed widget or any of its children is GtkText or GtkEntry. As a
278 * workaround, this function walks through the widget subtree before removing
279 * and disclaims the selection from all GtkEditable widgets found.
281 * A similar workaround may be needed for gtk_widget_reparent(); currently it
282 * is not necessary because Sylpheed does not use gtk_widget_reparent() for
283 * GtkEditable widgets or containers holding such widgets.
285 void gtkut_container_remove(GtkContainer *container, GtkWidget *widget)
287 gtkut_check_before_remove(widget, NULL);
288 gtk_container_remove(container, widget);
291 gboolean gtkut_stext_match_string(GtkSText *text, gint pos, wchar_t *wcs,
292 gint len, gboolean case_sens)
294 gint match_count = 0;
296 for (; match_count < len; pos++, match_count++) {
298 if (GTK_STEXT_INDEX(text, pos) != wcs[match_count])
301 if (towlower(GTK_STEXT_INDEX(text, pos)) !=
302 towlower(wcs[match_count]))
307 if (match_count == len)
313 guint gtkut_stext_str_compare_n(GtkSText *text, guint pos1, guint pos2,
314 guint len, guint text_len)
319 for (i = 0; i < len && pos1 + i < text_len && pos2 + i < text_len; i++) {
320 ch1 = GTK_STEXT_INDEX(text, pos1 + i);
321 ch2 = GTK_STEXT_INDEX(text, pos2 + i);
329 guint gtkut_stext_str_compare(GtkSText *text, guint start_pos, guint text_len,
338 wcs = strdup_mbstowcs(str);
342 if (len > text_len - start_pos)
345 result = gtkut_stext_match_string(text, start_pos, wcs, len,
350 return result ? len : 0;
353 gboolean gtkut_stext_is_uri_string(GtkSText *text,
354 guint start_pos, guint text_len)
356 if (gtkut_stext_str_compare(text, start_pos, text_len, "http://") ||
357 gtkut_stext_str_compare(text, start_pos, text_len, "ftp://") ||
358 gtkut_stext_str_compare(text, start_pos, text_len, "https://"))
364 void gtkut_widget_disable_theme_engine(GtkWidget *widget)
366 GtkStyle *style, *new_style;
368 style = gtk_widget_get_style(widget);
371 GtkThemeEngine *engine;
373 engine = style->engine;
374 style->engine = NULL;
375 new_style = gtk_style_copy(style);
376 style->engine = engine;
377 gtk_widget_set_style(widget, new_style);
381 static void gtkut_widget_draw_cb(GtkWidget *widget, GdkRectangle *area,
385 gtk_signal_disconnect_by_data(GTK_OBJECT(widget), flag);
388 void gtkut_widget_wait_for_draw(GtkWidget *widget)
390 gboolean flag = FALSE;
392 if (!GTK_WIDGET_VISIBLE(widget)) return;
394 gtk_signal_connect(GTK_OBJECT(widget), "draw",
395 GTK_SIGNAL_FUNC(gtkut_widget_draw_cb), &flag);
397 gtk_main_iteration();
400 void gtkut_widget_get_uposition(GtkWidget *widget, gint *px, gint *py)
405 g_return_if_fail(widget != NULL);
406 g_return_if_fail(widget->window != NULL);
408 /* gdk_window_get_root_origin ever return *rootwindow*'s position*/
409 gdk_window_get_root_origin(widget->window, &x, &y);
411 sx = gdk_screen_width();
412 sy = gdk_screen_height();
413 x %= sx; if (x < 0) x = 0;
414 y %= sy; if (y < 0) y = 0;
419 static void gtkut_clist_bindings_add(GtkWidget *clist)
421 GtkBindingSet *binding_set;
423 binding_set = gtk_binding_set_by_class
424 (GTK_CLIST_CLASS(GTK_OBJECT(clist)->klass));
426 gtk_binding_entry_add_signal(binding_set, GDK_n, GDK_CONTROL_MASK,
427 "scroll_vertical", 2,
428 GTK_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
429 GTK_TYPE_FLOAT, 0.0);
430 gtk_binding_entry_add_signal(binding_set, GDK_p, GDK_CONTROL_MASK,
431 "scroll_vertical", 2,
432 GTK_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
433 GTK_TYPE_FLOAT, 0.0);
436 void gtkut_widget_init(void)
440 clist = gtk_clist_new(1);
441 gtkut_clist_bindings_add(clist);
442 gtk_widget_unref(clist);
444 clist = gtk_ctree_new(1, 0);
445 gtkut_clist_bindings_add(clist);
446 gtk_widget_unref(clist);
448 clist = gtk_sctree_new_with_titles(1, 0, NULL);
449 gtkut_clist_bindings_add(clist);
450 gtk_widget_unref(clist);
453 void gtkut_widget_set_app_icon(GtkWidget *widget)
455 #include "pixmaps/sylpheed.xpm"
456 static GdkPixmap *sylpheedxpm;
457 static GdkBitmap *sylpheedxpmmask;
459 g_return_if_fail(widget != NULL);
460 g_return_if_fail(widget->window != NULL);
462 PIXMAP_CREATE(widget, sylpheedxpm, sylpheedxpmmask, sylpheed_xpm);
464 gdk_window_set_icon(widget->window, NULL, sylpheedxpm, sylpheedxpmmask);
467 void gtkut_widget_set_composer_icon(GtkWidget *widget)
469 #include "pixmaps/stock_mail_compose.xpm"
470 static GdkPixmap *xpm;
471 static GdkBitmap *bmp;
473 g_return_if_fail(widget != NULL);
474 g_return_if_fail(widget->window != NULL);
476 PIXMAP_CREATE(widget, xpm, bmp, stock_mail_compose_xpm);
478 gdk_window_set_icon(widget->window, NULL, xpm, bmp);