2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 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>
37 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
45 #include "gtksctree.h"
47 #include "stock_pixmap.h"
49 gint gtkut_get_font_width(GdkFont *font)
54 if (conv_get_current_charset() == C_UTF_8)
59 width = gdk_string_width(font, str);
65 gint gtkut_get_font_height(GdkFont *font)
70 if (conv_get_current_charset() == C_UTF_8)
75 height = gdk_string_height(font, str);
80 void gtkut_convert_int_to_gdk_color(gint rgbvalue, GdkColor *color)
82 g_return_if_fail(color != NULL);
85 color->red = (int) (((gdouble)((rgbvalue & 0xff0000) >> 16) / 255.0) * 65535.0);
86 color->green = (int) (((gdouble)((rgbvalue & 0x00ff00) >> 8) / 255.0) * 65535.0);
87 color->blue = (int) (((gdouble) (rgbvalue & 0x0000ff) / 255.0) * 65535.0);
90 void gtkut_button_set_create(GtkWidget **bbox,
91 GtkWidget **button1, const gchar *label1,
92 GtkWidget **button2, const gchar *label2,
93 GtkWidget **button3, const gchar *label3)
95 g_return_if_fail(bbox != NULL);
96 g_return_if_fail(button1 != NULL);
98 *bbox = gtk_hbutton_box_new();
99 gtk_button_box_set_layout(GTK_BUTTON_BOX(*bbox), GTK_BUTTONBOX_END);
100 gtk_button_box_set_spacing(GTK_BUTTON_BOX(*bbox), 5);
102 *button1 = gtk_button_new_with_label(label1);
103 GTK_WIDGET_SET_FLAGS(*button1, GTK_CAN_DEFAULT);
104 gtk_box_pack_start(GTK_BOX(*bbox), *button1, TRUE, TRUE, 0);
105 gtk_widget_show(*button1);
108 *button2 = gtk_button_new_with_label(label2);
109 GTK_WIDGET_SET_FLAGS(*button2, GTK_CAN_DEFAULT);
110 gtk_box_pack_start(GTK_BOX(*bbox), *button2, TRUE, TRUE, 0);
111 gtk_widget_show(*button2);
115 *button3 = gtk_button_new_with_label(label3);
116 GTK_WIDGET_SET_FLAGS(*button3, GTK_CAN_DEFAULT);
117 gtk_box_pack_start(GTK_BOX(*bbox), *button3, TRUE, TRUE, 0);
118 gtk_widget_show(*button3);
122 #define CELL_SPACING 1
123 #define ROW_TOP_YPIXEL(clist, row) (((clist)->row_height * (row)) + \
124 (((row) + 1) * CELL_SPACING) + \
126 #define ROW_FROM_YPIXEL(clist, y) (((y) - (clist)->voffset) / \
127 ((clist)->row_height + CELL_SPACING))
129 void gtkut_ctree_node_move_if_on_the_edge(GtkCTree *ctree, GtkCTreeNode *node)
131 GtkCList *clist = GTK_CLIST(ctree);
133 GtkVisibility row_visibility, prev_row_visibility, next_row_visibility;
135 g_return_if_fail(ctree != NULL);
136 g_return_if_fail(node != NULL);
138 row = g_list_position(clist->row_list, (GList *)node);
139 if (row < 0 || row >= clist->rows || clist->row_height == 0) return;
140 row_visibility = gtk_clist_row_is_visible(clist, row);
141 prev_row_visibility = gtk_clist_row_is_visible(clist, row - 1);
142 next_row_visibility = gtk_clist_row_is_visible(clist, row + 1);
144 if (row_visibility == GTK_VISIBILITY_NONE) {
145 gtk_clist_moveto(clist, row, -1, 0.5, 0);
148 if (row_visibility == GTK_VISIBILITY_FULL &&
149 prev_row_visibility == GTK_VISIBILITY_FULL &&
150 next_row_visibility == GTK_VISIBILITY_FULL)
152 if (prev_row_visibility != GTK_VISIBILITY_FULL &&
153 next_row_visibility != GTK_VISIBILITY_FULL)
156 if (prev_row_visibility != GTK_VISIBILITY_FULL) {
157 gtk_clist_moveto(clist, row, -1, 0.2, 0);
160 if (next_row_visibility != GTK_VISIBILITY_FULL) {
161 gtk_clist_moveto(clist, row, -1, 0.8, 0);
167 #undef ROW_TOP_YPIXEL
168 #undef ROW_FROM_YPIXEL
170 gint gtkut_ctree_get_nth_from_node(GtkCTree *ctree, GtkCTreeNode *node)
172 g_return_val_if_fail(ctree != NULL, -1);
173 g_return_val_if_fail(node != NULL, -1);
175 return g_list_position(GTK_CLIST(ctree)->row_list, (GList *)node);
178 /* get the next node, including the invisible one */
179 GtkCTreeNode *gtkut_ctree_node_next(GtkCTree *ctree, GtkCTreeNode *node)
181 GtkCTreeNode *parent;
183 if (!node) return NULL;
185 if (GTK_CTREE_ROW(node)->children)
186 return GTK_CTREE_ROW(node)->children;
188 if (GTK_CTREE_ROW(node)->sibling)
189 return GTK_CTREE_ROW(node)->sibling;
191 for (parent = GTK_CTREE_ROW(node)->parent; parent != NULL;
192 parent = GTK_CTREE_ROW(parent)->parent) {
193 if (GTK_CTREE_ROW(parent)->sibling)
194 return GTK_CTREE_ROW(parent)->sibling;
200 GtkCTreeNode *gtkut_ctree_find_collapsed_parent(GtkCTree *ctree,
203 if (!node) return NULL;
205 while ((node = GTK_CTREE_ROW(node)->parent) != NULL) {
206 if (!GTK_CTREE_ROW(node)->expanded)
213 void gtkut_ctree_expand_parent_all(GtkCTree *ctree, GtkCTreeNode *node)
215 while ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
216 gtk_ctree_expand(ctree, node);
219 void gtkut_ctree_set_focus_row(GtkCTree *ctree, GtkCTreeNode *node)
221 gtkut_clist_set_focus_row(GTK_CLIST(ctree),
222 gtkut_ctree_get_nth_from_node(ctree, node));
225 void gtkut_clist_set_focus_row(GtkCList *clist, gint row)
227 clist->focus_row = row;
228 GTKUT_CTREE_REFRESH(clist);
231 void gtkut_combo_set_items(GtkCombo *combo, const gchar *str1, ...)
235 GList *combo_items = NULL;
237 g_return_if_fail(str1 != NULL);
239 combo_items = g_list_append(combo_items, (gpointer)str1);
240 va_start(args, str1);
241 s = va_arg(args, gchar*);
243 combo_items = g_list_append(combo_items, (gpointer)s);
244 s = va_arg(args, gchar*);
248 gtk_combo_set_popdown_strings(combo, combo_items);
250 g_list_free(combo_items);
253 gchar *gtkut_editable_get_selection(GtkEditable *editable)
255 guint start_pos, end_pos;
257 g_return_val_if_fail(editable != NULL, NULL);
259 if (!editable->has_selection) return NULL;
261 if (editable->selection_start_pos == editable->selection_end_pos)
264 if (editable->selection_start_pos < editable->selection_end_pos) {
265 start_pos = editable->selection_start_pos;
266 end_pos = editable->selection_end_pos;
268 start_pos = editable->selection_end_pos;
269 end_pos = editable->selection_start_pos;
272 return gtk_editable_get_chars(editable, start_pos, end_pos);
276 * Walk through the widget tree and disclaim the selection from all currently
277 * realized GtkEditable widgets.
279 static void gtkut_check_before_remove(GtkWidget *widget, gpointer unused)
281 g_return_if_fail(widget != NULL);
283 if (!GTK_WIDGET_REALIZED(widget))
284 return; /* all nested widgets must be unrealized too */
285 if (GTK_IS_CONTAINER(widget))
286 gtk_container_forall(GTK_CONTAINER(widget),
287 gtkut_check_before_remove, NULL);
288 if (GTK_IS_EDITABLE(widget))
289 gtk_editable_claim_selection(GTK_EDITABLE(widget),
290 FALSE, GDK_CURRENT_TIME);
294 * Wrapper around gtk_container_remove to work around a bug in GtkText and
295 * GtkEntry (in all GTK+ versions up to and including at least 1.2.10).
297 * The problem is that unrealizing a GtkText or GtkEntry widget which has the
298 * active selection completely messes up selection handling, leading to
299 * non-working selections and crashes. Removing a widget from its container
300 * implies unrealizing it and all its child widgets; this triggers the bug if
301 * the removed widget or any of its children is GtkText or GtkEntry. As a
302 * workaround, this function walks through the widget subtree before removing
303 * and disclaims the selection from all GtkEditable widgets found.
305 * A similar workaround may be needed for gtk_widget_reparent(); currently it
306 * is not necessary because Sylpheed does not use gtk_widget_reparent() for
307 * GtkEditable widgets or containers holding such widgets.
309 void gtkut_container_remove(GtkContainer *container, GtkWidget *widget)
311 gtkut_check_before_remove(widget, NULL);
312 gtk_container_remove(container, widget);
315 gboolean gtkut_stext_match_string(GtkSText *text, gint pos, wchar_t *wcs,
316 gint len, gboolean case_sens)
318 gint match_count = 0;
320 for (; match_count < len; pos++, match_count++) {
322 if (GTK_STEXT_INDEX(text, pos) != wcs[match_count])
325 if (towlower(GTK_STEXT_INDEX(text, pos)) !=
326 towlower(wcs[match_count]))
331 if (match_count == len)
337 guint gtkut_stext_str_compare_n(GtkSText *text, guint pos1, guint pos2,
338 guint len, guint text_len)
343 for (i = 0; i < len && pos1 + i < text_len && pos2 + i < text_len; i++) {
344 ch1 = GTK_STEXT_INDEX(text, pos1 + i);
345 ch2 = GTK_STEXT_INDEX(text, pos2 + i);
353 guint gtkut_stext_str_compare(GtkSText *text, guint start_pos, guint text_len,
362 wcs = strdup_mbstowcs(str);
366 if (len > text_len - start_pos)
369 result = gtkut_stext_match_string(text, start_pos, wcs, len,
374 return result ? len : 0;
377 gboolean gtkut_stext_is_uri_string(GtkSText *text,
378 guint start_pos, guint text_len)
380 if (gtkut_stext_str_compare(text, start_pos, text_len, "http://") ||
381 gtkut_stext_str_compare(text, start_pos, text_len, "ftp://") ||
382 gtkut_stext_str_compare(text, start_pos, text_len, "https://")||
383 gtkut_stext_str_compare(text, start_pos, text_len, "www."))
389 void gtk_stext_clear(GtkSText *text)
391 gtk_stext_freeze(text);
392 gtk_stext_set_point(text, 0);
393 gtk_stext_forward_delete(text, gtk_stext_get_length(text));
394 gtk_stext_thaw(text);
397 void gtkut_widget_disable_theme_engine(GtkWidget *widget)
399 GtkStyle *style, *new_style;
401 style = gtk_widget_get_style(widget);
404 GtkThemeEngine *engine;
406 engine = style->engine;
407 style->engine = NULL;
408 new_style = gtk_style_copy(style);
409 style->engine = engine;
410 gtk_widget_set_style(widget, new_style);
414 static void gtkut_widget_draw_cb(GtkWidget *widget, GdkRectangle *area,
418 gtk_signal_disconnect_by_data(GTK_OBJECT(widget), flag);
421 void gtkut_widget_wait_for_draw(GtkWidget *widget)
423 gboolean flag = FALSE;
425 if (!GTK_WIDGET_VISIBLE(widget) || !GTK_WIDGET_MAPPED(widget)) return;
427 gtk_signal_connect(GTK_OBJECT(widget), "draw",
428 GTK_SIGNAL_FUNC(gtkut_widget_draw_cb), &flag);
430 gtk_main_iteration();
433 void gtkut_widget_get_uposition(GtkWidget *widget, gint *px, gint *py)
438 g_return_if_fail(widget != NULL);
439 g_return_if_fail(widget->window != NULL);
441 /* gdk_window_get_root_origin ever return *rootwindow*'s position*/
442 gdk_window_get_root_origin(widget->window, &x, &y);
444 sx = gdk_screen_width();
445 sy = gdk_screen_height();
446 x %= sx; if (x < 0) x = 0;
447 y %= sy; if (y < 0) y = 0;
452 static void gtkut_clist_bindings_add(GtkWidget *clist)
454 GtkBindingSet *binding_set;
456 binding_set = gtk_binding_set_by_class
457 (GTK_CLIST_CLASS(GTK_OBJECT(clist)->klass));
459 gtk_binding_entry_add_signal(binding_set, GDK_n, GDK_CONTROL_MASK,
460 "scroll_vertical", 2,
461 GTK_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
462 GTK_TYPE_FLOAT, 0.0);
463 gtk_binding_entry_add_signal(binding_set, GDK_p, GDK_CONTROL_MASK,
464 "scroll_vertical", 2,
465 GTK_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
466 GTK_TYPE_FLOAT, 0.0);
469 void gtkut_widget_init(void)
473 clist = gtk_clist_new(1);
474 gtkut_clist_bindings_add(clist);
475 gtk_widget_unref(clist);
477 clist = gtk_ctree_new(1, 0);
478 gtkut_clist_bindings_add(clist);
479 gtk_widget_unref(clist);
481 clist = gtk_sctree_new_with_titles(1, 0, NULL);
482 gtkut_clist_bindings_add(clist);
483 gtk_widget_unref(clist);
486 void gtkut_widget_set_app_icon(GtkWidget *widget)
488 #include "pixmaps/sylpheed.xpm"
489 static GdkPixmap *sylpheedxpm;
490 static GdkBitmap *sylpheedxpmmask;
492 g_return_if_fail(widget != NULL);
493 g_return_if_fail(widget->window != NULL);
495 PIXMAP_CREATE(widget, sylpheedxpm, sylpheedxpmmask, sylpheed_xpm);
497 gdk_window_set_icon(widget->window, NULL, sylpheedxpm, sylpheedxpmmask);
500 void gtkut_widget_set_composer_icon(GtkWidget *widget)
502 static GdkPixmap *xpm;
503 static GdkBitmap *bmp;
505 g_return_if_fail(widget != NULL);
506 g_return_if_fail(widget->window != NULL);
508 stock_pixmap_gdk(widget, STOCK_PIXMAP_MAIL_COMPOSE, &xpm, &bmp);
510 gdk_window_set_icon(widget->window, NULL, xpm, bmp);