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 #include "prefs_account.h"
51 gint gtkut_get_font_width(GdkFont *font)
56 if (conv_get_current_charset() == C_UTF_8)
61 width = gdk_string_width(font, str);
67 gint gtkut_get_font_height(GdkFont *font)
72 if (conv_get_current_charset() == C_UTF_8)
77 height = gdk_string_height(font, str);
82 void gtkut_convert_int_to_gdk_color(gint rgbvalue, GdkColor *color)
84 g_return_if_fail(color != NULL);
87 color->red = (int) (((gdouble)((rgbvalue & 0xff0000) >> 16) / 255.0) * 65535.0);
88 color->green = (int) (((gdouble)((rgbvalue & 0x00ff00) >> 8) / 255.0) * 65535.0);
89 color->blue = (int) (((gdouble) (rgbvalue & 0x0000ff) / 255.0) * 65535.0);
92 void gtkut_button_set_create(GtkWidget **bbox,
93 GtkWidget **button1, const gchar *label1,
94 GtkWidget **button2, const gchar *label2,
95 GtkWidget **button3, const gchar *label3)
97 g_return_if_fail(bbox != NULL);
98 g_return_if_fail(button1 != NULL);
100 *bbox = gtk_hbutton_box_new();
101 gtk_button_box_set_layout(GTK_BUTTON_BOX(*bbox), GTK_BUTTONBOX_END);
102 gtk_button_box_set_spacing(GTK_BUTTON_BOX(*bbox), 5);
104 *button1 = gtk_button_new_with_label(label1);
105 GTK_WIDGET_SET_FLAGS(*button1, GTK_CAN_DEFAULT);
106 gtk_box_pack_start(GTK_BOX(*bbox), *button1, TRUE, TRUE, 0);
107 gtk_widget_show(*button1);
110 *button2 = gtk_button_new_with_label(label2);
111 GTK_WIDGET_SET_FLAGS(*button2, GTK_CAN_DEFAULT);
112 gtk_box_pack_start(GTK_BOX(*bbox), *button2, TRUE, TRUE, 0);
113 gtk_widget_show(*button2);
117 *button3 = gtk_button_new_with_label(label3);
118 GTK_WIDGET_SET_FLAGS(*button3, GTK_CAN_DEFAULT);
119 gtk_box_pack_start(GTK_BOX(*bbox), *button3, TRUE, TRUE, 0);
120 gtk_widget_show(*button3);
124 #define CELL_SPACING 1
125 #define ROW_TOP_YPIXEL(clist, row) (((clist)->row_height * (row)) + \
126 (((row) + 1) * CELL_SPACING) + \
128 #define ROW_FROM_YPIXEL(clist, y) (((y) - (clist)->voffset) / \
129 ((clist)->row_height + CELL_SPACING))
131 void gtkut_ctree_node_move_if_on_the_edge(GtkCTree *ctree, GtkCTreeNode *node)
133 GtkCList *clist = GTK_CLIST(ctree);
135 GtkVisibility row_visibility, prev_row_visibility, next_row_visibility;
137 g_return_if_fail(ctree != NULL);
138 g_return_if_fail(node != NULL);
140 row = g_list_position(clist->row_list, (GList *)node);
141 if (row < 0 || row >= clist->rows || clist->row_height == 0) return;
142 row_visibility = gtk_clist_row_is_visible(clist, row);
143 prev_row_visibility = gtk_clist_row_is_visible(clist, row - 1);
144 next_row_visibility = gtk_clist_row_is_visible(clist, row + 1);
146 if (row_visibility == GTK_VISIBILITY_NONE) {
147 gtk_clist_moveto(clist, row, -1, 0.5, 0);
150 if (row_visibility == GTK_VISIBILITY_FULL &&
151 prev_row_visibility == GTK_VISIBILITY_FULL &&
152 next_row_visibility == GTK_VISIBILITY_FULL)
154 if (prev_row_visibility != GTK_VISIBILITY_FULL &&
155 next_row_visibility != GTK_VISIBILITY_FULL)
158 if (prev_row_visibility != GTK_VISIBILITY_FULL) {
159 gtk_clist_moveto(clist, row, -1, 0.2, 0);
162 if (next_row_visibility != GTK_VISIBILITY_FULL) {
163 gtk_clist_moveto(clist, row, -1, 0.8, 0);
169 #undef ROW_TOP_YPIXEL
170 #undef ROW_FROM_YPIXEL
172 gint gtkut_ctree_get_nth_from_node(GtkCTree *ctree, GtkCTreeNode *node)
174 g_return_val_if_fail(ctree != NULL, -1);
175 g_return_val_if_fail(node != NULL, -1);
177 return g_list_position(GTK_CLIST(ctree)->row_list, (GList *)node);
180 /* get the next node, including the invisible one */
181 GtkCTreeNode *gtkut_ctree_node_next(GtkCTree *ctree, GtkCTreeNode *node)
183 GtkCTreeNode *parent;
185 if (!node) return NULL;
187 if (GTK_CTREE_ROW(node)->children)
188 return GTK_CTREE_ROW(node)->children;
190 if (GTK_CTREE_ROW(node)->sibling)
191 return GTK_CTREE_ROW(node)->sibling;
193 for (parent = GTK_CTREE_ROW(node)->parent; parent != NULL;
194 parent = GTK_CTREE_ROW(parent)->parent) {
195 if (GTK_CTREE_ROW(parent)->sibling)
196 return GTK_CTREE_ROW(parent)->sibling;
202 GtkCTreeNode *gtkut_ctree_find_collapsed_parent(GtkCTree *ctree,
205 if (!node) return NULL;
207 while ((node = GTK_CTREE_ROW(node)->parent) != NULL) {
208 if (!GTK_CTREE_ROW(node)->expanded)
215 void gtkut_ctree_expand_parent_all(GtkCTree *ctree, GtkCTreeNode *node)
217 while ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
218 gtk_ctree_expand(ctree, node);
221 void gtkut_ctree_set_focus_row(GtkCTree *ctree, GtkCTreeNode *node)
223 gtkut_clist_set_focus_row(GTK_CLIST(ctree),
224 gtkut_ctree_get_nth_from_node(ctree, node));
227 void gtkut_clist_set_focus_row(GtkCList *clist, gint row)
229 clist->focus_row = row;
230 GTKUT_CTREE_REFRESH(clist);
233 void gtkut_combo_set_items(GtkCombo *combo, const gchar *str1, ...)
237 GList *combo_items = NULL;
239 g_return_if_fail(str1 != NULL);
241 combo_items = g_list_append(combo_items, (gpointer)str1);
242 va_start(args, str1);
243 s = va_arg(args, gchar*);
245 combo_items = g_list_append(combo_items, (gpointer)s);
246 s = va_arg(args, gchar*);
250 gtk_combo_set_popdown_strings(combo, combo_items);
252 g_list_free(combo_items);
255 gchar *gtkut_editable_get_selection(GtkEditable *editable)
257 guint start_pos, end_pos;
259 g_return_val_if_fail(editable != NULL, NULL);
261 if (!editable->has_selection) return NULL;
263 if (editable->selection_start_pos == editable->selection_end_pos)
266 if (editable->selection_start_pos < editable->selection_end_pos) {
267 start_pos = editable->selection_start_pos;
268 end_pos = editable->selection_end_pos;
270 start_pos = editable->selection_end_pos;
271 end_pos = editable->selection_start_pos;
274 return gtk_editable_get_chars(editable, start_pos, end_pos);
278 * Walk through the widget tree and disclaim the selection from all currently
279 * realized GtkEditable widgets.
281 static void gtkut_check_before_remove(GtkWidget *widget, gpointer unused)
283 g_return_if_fail(widget != NULL);
285 if (!GTK_WIDGET_REALIZED(widget))
286 return; /* all nested widgets must be unrealized too */
287 if (GTK_IS_CONTAINER(widget))
288 gtk_container_forall(GTK_CONTAINER(widget),
289 gtkut_check_before_remove, NULL);
290 if (GTK_IS_EDITABLE(widget))
291 gtk_editable_claim_selection(GTK_EDITABLE(widget),
292 FALSE, GDK_CURRENT_TIME);
296 * Wrapper around gtk_container_remove to work around a bug in GtkText and
297 * GtkEntry (in all GTK+ versions up to and including at least 1.2.10).
299 * The problem is that unrealizing a GtkText or GtkEntry widget which has the
300 * active selection completely messes up selection handling, leading to
301 * non-working selections and crashes. Removing a widget from its container
302 * implies unrealizing it and all its child widgets; this triggers the bug if
303 * the removed widget or any of its children is GtkText or GtkEntry. As a
304 * workaround, this function walks through the widget subtree before removing
305 * and disclaims the selection from all GtkEditable widgets found.
307 * A similar workaround may be needed for gtk_widget_reparent(); currently it
308 * is not necessary because Sylpheed does not use gtk_widget_reparent() for
309 * GtkEditable widgets or containers holding such widgets.
311 void gtkut_container_remove(GtkContainer *container, GtkWidget *widget)
313 gtkut_check_before_remove(widget, NULL);
314 gtk_container_remove(container, widget);
317 gboolean gtkut_stext_match_string(GtkSText *text, gint pos, wchar_t *wcs,
318 gint len, gboolean case_sens)
320 gint match_count = 0;
322 for (; match_count < len; pos++, match_count++) {
324 if (GTK_STEXT_INDEX(text, pos) != wcs[match_count])
327 if (towlower(GTK_STEXT_INDEX(text, pos)) !=
328 towlower(wcs[match_count]))
333 if (match_count == len)
339 guint gtkut_stext_str_compare_n(GtkSText *text, guint pos1, guint pos2,
340 guint len, guint text_len)
345 for (i = 0; i < len && pos1 + i < text_len && pos2 + i < text_len; i++) {
346 ch1 = GTK_STEXT_INDEX(text, pos1 + i);
347 ch2 = GTK_STEXT_INDEX(text, pos2 + i);
355 guint gtkut_stext_str_compare(GtkSText *text, guint start_pos, guint text_len,
364 wcs = strdup_mbstowcs(str);
368 if (len > text_len - start_pos)
371 result = gtkut_stext_match_string(text, start_pos, wcs, len,
376 return result ? len : 0;
379 gboolean gtkut_stext_is_uri_string(GtkSText *text,
380 guint start_pos, guint text_len)
382 if (gtkut_stext_str_compare(text, start_pos, text_len, "http://") ||
383 gtkut_stext_str_compare(text, start_pos, text_len, "ftp://") ||
384 gtkut_stext_str_compare(text, start_pos, text_len, "https://")||
385 gtkut_stext_str_compare(text, start_pos, text_len, "www."))
391 void gtk_stext_clear(GtkSText *text)
393 gtk_stext_freeze(text);
394 gtk_stext_set_point(text, 0);
395 gtk_stext_forward_delete(text, gtk_stext_get_length(text));
396 gtk_stext_thaw(text);
399 void gtkut_widget_disable_theme_engine(GtkWidget *widget)
401 GtkStyle *style, *new_style;
403 style = gtk_widget_get_style(widget);
406 GtkThemeEngine *engine;
408 engine = style->engine;
409 style->engine = NULL;
410 new_style = gtk_style_copy(style);
411 style->engine = engine;
412 gtk_widget_set_style(widget, new_style);
416 static void gtkut_widget_draw_cb(GtkWidget *widget, GdkRectangle *area,
420 gtk_signal_disconnect_by_data(GTK_OBJECT(widget), flag);
423 void gtkut_widget_wait_for_draw(GtkWidget *widget)
425 gboolean flag = FALSE;
427 if (!GTK_WIDGET_VISIBLE(widget) || !GTK_WIDGET_MAPPED(widget)) return;
429 gtk_signal_connect(GTK_OBJECT(widget), "draw",
430 GTK_SIGNAL_FUNC(gtkut_widget_draw_cb), &flag);
432 gtk_main_iteration();
435 void gtkut_widget_get_uposition(GtkWidget *widget, gint *px, gint *py)
440 g_return_if_fail(widget != NULL);
441 g_return_if_fail(widget->window != NULL);
443 /* gdk_window_get_root_origin ever return *rootwindow*'s position*/
444 gdk_window_get_root_origin(widget->window, &x, &y);
446 sx = gdk_screen_width();
447 sy = gdk_screen_height();
448 x %= sx; if (x < 0) x = 0;
449 y %= sy; if (y < 0) y = 0;
454 static void gtkut_clist_bindings_add(GtkWidget *clist)
456 GtkBindingSet *binding_set;
458 binding_set = gtk_binding_set_by_class
459 (GTK_CLIST_CLASS(GTK_OBJECT(clist)->klass));
461 gtk_binding_entry_add_signal(binding_set, GDK_n, GDK_CONTROL_MASK,
462 "scroll_vertical", 2,
463 GTK_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
464 GTK_TYPE_FLOAT, 0.0);
465 gtk_binding_entry_add_signal(binding_set, GDK_p, GDK_CONTROL_MASK,
466 "scroll_vertical", 2,
467 GTK_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
468 GTK_TYPE_FLOAT, 0.0);
471 void gtkut_widget_init(void)
475 clist = gtk_clist_new(1);
476 gtkut_clist_bindings_add(clist);
477 gtk_widget_unref(clist);
479 clist = gtk_ctree_new(1, 0);
480 gtkut_clist_bindings_add(clist);
481 gtk_widget_unref(clist);
483 clist = gtk_sctree_new_with_titles(1, 0, NULL);
484 gtkut_clist_bindings_add(clist);
485 gtk_widget_unref(clist);
488 void gtkut_widget_set_app_icon(GtkWidget *widget)
490 #include "pixmaps/sylpheed.xpm"
491 static GdkPixmap *sylpheedxpm;
492 static GdkBitmap *sylpheedxpmmask;
494 g_return_if_fail(widget != NULL);
495 g_return_if_fail(widget->window != NULL);
497 PIXMAP_CREATE(widget, sylpheedxpm, sylpheedxpmmask, sylpheed_xpm);
499 gdk_window_set_icon(widget->window, NULL, sylpheedxpm, sylpheedxpmmask);
502 void gtkut_widget_set_composer_icon(GtkWidget *widget)
504 static GdkPixmap *xpm;
505 static GdkBitmap *bmp;
507 g_return_if_fail(widget != NULL);
508 g_return_if_fail(widget->window != NULL);
510 stock_pixmap_gdk(widget, STOCK_PIXMAP_MAIL_COMPOSE, &xpm, &bmp);
512 gdk_window_set_icon(widget->window, NULL, xpm, bmp);
515 GtkWidget *gtkut_account_menu_new(GList *ac_list,
516 GtkSignalFunc callback,
522 g_return_val_if_fail(ac_list != NULL, NULL);
524 menu = gtk_menu_new();
526 for (cur_ac = ac_list; cur_ac != NULL; cur_ac = cur_ac->next) {
529 PrefsAccount *account;
531 account = (PrefsAccount *) cur_ac->data;
533 name = g_strdup_printf("%s: %s <%s>",
534 account->account_name,
538 name = g_strdup_printf("%s: %s",
539 account->account_name,
541 MENUITEM_ADD(menu, menuitem, name, account->account_id);
543 if (callback != NULL)
544 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",