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>
32 #include <gtk/gtkthemes.h>
33 #include <gtk/gtkbindings.h>
38 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
46 #include "gtksctree.h"
48 #include "stock_pixmap.h"
50 #include "prefs_account.h"
52 gint gtkut_get_font_width(GdkFont *font)
57 if (conv_get_current_charset() == C_UTF_8)
62 width = gdk_string_width(font, str);
68 gint gtkut_get_font_height(GdkFont *font)
73 if (conv_get_current_charset() == C_UTF_8)
78 height = gdk_string_height(font, str);
83 GdkFont *gtkut_font_load(const gchar *fontset_name)
87 g_return_val_if_fail(fontset_name != NULL, NULL);
89 if (conv_get_current_charset() == C_US_ASCII)
90 font = gtkut_font_load_from_fontset(fontset_name);
92 font = gdk_fontset_load(fontset_name);
97 GdkFont *gtkut_font_load_from_fontset(const gchar *fontset_name)
101 if (strchr(fontset_name, ',') == NULL) {
102 font = gdk_font_load(fontset_name);
107 fonts = g_strsplit(fontset_name, ",", -1);
108 for (i = 0; fonts[i] != NULL && fonts[i][0] != '\0';
110 font = gdk_font_load(fonts[i]);
119 void gtkut_convert_int_to_gdk_color(gint rgbvalue, GdkColor *color)
121 g_return_if_fail(color != NULL);
124 color->red = (int) (((gdouble)((rgbvalue & 0xff0000) >> 16) / 255.0) * 65535.0);
125 color->green = (int) (((gdouble)((rgbvalue & 0x00ff00) >> 8) / 255.0) * 65535.0);
126 color->blue = (int) (((gdouble) (rgbvalue & 0x0000ff) / 255.0) * 65535.0);
129 void gtkut_button_set_create(GtkWidget **bbox,
130 GtkWidget **button1, const gchar *label1,
131 GtkWidget **button2, const gchar *label2,
132 GtkWidget **button3, const gchar *label3)
134 g_return_if_fail(bbox != NULL);
135 g_return_if_fail(button1 != NULL);
137 *bbox = gtk_hbutton_box_new();
138 gtk_button_box_set_layout(GTK_BUTTON_BOX(*bbox), GTK_BUTTONBOX_END);
139 gtk_button_box_set_spacing(GTK_BUTTON_BOX(*bbox), 5);
141 *button1 = gtk_button_new_with_label(label1);
142 GTK_WIDGET_SET_FLAGS(*button1, GTK_CAN_DEFAULT);
143 gtk_box_pack_start(GTK_BOX(*bbox), *button1, TRUE, TRUE, 0);
144 gtk_widget_show(*button1);
147 *button2 = gtk_button_new_with_label(label2);
148 GTK_WIDGET_SET_FLAGS(*button2, GTK_CAN_DEFAULT);
149 gtk_box_pack_start(GTK_BOX(*bbox), *button2, TRUE, TRUE, 0);
150 gtk_widget_show(*button2);
154 *button3 = gtk_button_new_with_label(label3);
155 GTK_WIDGET_SET_FLAGS(*button3, GTK_CAN_DEFAULT);
156 gtk_box_pack_start(GTK_BOX(*bbox), *button3, TRUE, TRUE, 0);
157 gtk_widget_show(*button3);
161 #define CELL_SPACING 1
162 #define ROW_TOP_YPIXEL(clist, row) (((clist)->row_height * (row)) + \
163 (((row) + 1) * CELL_SPACING) + \
165 #define ROW_FROM_YPIXEL(clist, y) (((y) - (clist)->voffset) / \
166 ((clist)->row_height + CELL_SPACING))
168 void gtkut_ctree_node_move_if_on_the_edge(GtkCTree *ctree, GtkCTreeNode *node)
170 GtkCList *clist = GTK_CLIST(ctree);
172 GtkVisibility row_visibility, prev_row_visibility, next_row_visibility;
174 g_return_if_fail(ctree != NULL);
175 g_return_if_fail(node != NULL);
177 row = g_list_position(clist->row_list, (GList *)node);
178 if (row < 0 || row >= clist->rows || clist->row_height == 0) return;
179 row_visibility = gtk_clist_row_is_visible(clist, row);
180 prev_row_visibility = gtk_clist_row_is_visible(clist, row - 1);
181 next_row_visibility = gtk_clist_row_is_visible(clist, row + 1);
183 if (row_visibility == GTK_VISIBILITY_NONE) {
184 gtk_clist_moveto(clist, row, -1, 0.5, 0);
187 if (row_visibility == GTK_VISIBILITY_FULL &&
188 prev_row_visibility == GTK_VISIBILITY_FULL &&
189 next_row_visibility == GTK_VISIBILITY_FULL)
191 if (prev_row_visibility != GTK_VISIBILITY_FULL &&
192 next_row_visibility != GTK_VISIBILITY_FULL)
195 if (prev_row_visibility != GTK_VISIBILITY_FULL) {
196 gtk_clist_moveto(clist, row, -1, 0.2, 0);
199 if (next_row_visibility != GTK_VISIBILITY_FULL) {
200 gtk_clist_moveto(clist, row, -1, 0.8, 0);
206 #undef ROW_TOP_YPIXEL
207 #undef ROW_FROM_YPIXEL
209 gint gtkut_ctree_get_nth_from_node(GtkCTree *ctree, GtkCTreeNode *node)
211 g_return_val_if_fail(ctree != NULL, -1);
212 g_return_val_if_fail(node != NULL, -1);
214 return g_list_position(GTK_CLIST(ctree)->row_list, (GList *)node);
217 /* get the next node, including the invisible one */
218 GtkCTreeNode *gtkut_ctree_node_next(GtkCTree *ctree, GtkCTreeNode *node)
220 GtkCTreeNode *parent;
222 if (!node) return NULL;
224 if (GTK_CTREE_ROW(node)->children)
225 return GTK_CTREE_ROW(node)->children;
227 if (GTK_CTREE_ROW(node)->sibling)
228 return GTK_CTREE_ROW(node)->sibling;
230 for (parent = GTK_CTREE_ROW(node)->parent; parent != NULL;
231 parent = GTK_CTREE_ROW(parent)->parent) {
232 if (GTK_CTREE_ROW(parent)->sibling)
233 return GTK_CTREE_ROW(parent)->sibling;
239 /* get the previous node, including the invisible one */
240 GtkCTreeNode *gtkut_ctree_node_prev(GtkCTree *ctree, GtkCTreeNode *node)
245 if (!node) return NULL;
247 prev = GTK_CTREE_NODE_PREV(node);
248 if (prev == GTK_CTREE_ROW(node)->parent)
252 while (GTK_CTREE_ROW(child)->children != NULL) {
253 child = GTK_CTREE_ROW(child)->children;
254 while (GTK_CTREE_ROW(child)->sibling != NULL)
255 child = GTK_CTREE_ROW(child)->sibling;
261 gboolean gtkut_ctree_node_is_selected(GtkCTree *ctree, GtkCTreeNode *node)
263 GtkCList *clist = GTK_CLIST(ctree);
266 for (cur = clist->selection; cur != NULL; cur = cur->next) {
267 if (node == GTK_CTREE_NODE(cur->data))
274 GtkCTreeNode *gtkut_ctree_find_collapsed_parent(GtkCTree *ctree,
277 if (!node) return NULL;
279 while ((node = GTK_CTREE_ROW(node)->parent) != NULL) {
280 if (!GTK_CTREE_ROW(node)->expanded)
287 void gtkut_ctree_expand_parent_all(GtkCTree *ctree, GtkCTreeNode *node)
289 while ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
290 gtk_ctree_expand(ctree, node);
293 gboolean gtkut_ctree_node_is_parent(GtkCTreeNode *parent, GtkCTreeNode *node)
296 g_return_val_if_fail(node != NULL, FALSE);
297 g_return_val_if_fail(parent != NULL, FALSE);
301 if(GTK_CTREE_ROW(tmp)->parent && GTK_CTREE_ROW(tmp)->parent == parent)
303 tmp = GTK_CTREE_ROW(tmp)->parent;
309 void gtkut_ctree_set_focus_row(GtkCTree *ctree, GtkCTreeNode *node)
311 gtkut_clist_set_focus_row(GTK_CLIST(ctree),
312 gtkut_ctree_get_nth_from_node(ctree, node));
315 void gtkut_clist_set_focus_row(GtkCList *clist, gint row)
317 clist->focus_row = row;
318 GTKUT_CTREE_REFRESH(clist);
321 void gtkut_combo_set_items(GtkCombo *combo, const gchar *str1, ...)
325 GList *combo_items = NULL;
327 g_return_if_fail(str1 != NULL);
329 combo_items = g_list_append(combo_items, (gpointer)str1);
330 va_start(args, str1);
331 s = va_arg(args, gchar*);
333 combo_items = g_list_append(combo_items, (gpointer)s);
334 s = va_arg(args, gchar*);
338 gtk_combo_set_popdown_strings(combo, combo_items);
340 g_list_free(combo_items);
343 gchar *gtkut_editable_get_selection(GtkEditable *editable)
345 guint start_pos, end_pos;
347 g_return_val_if_fail(editable != NULL, NULL);
349 if (!editable->has_selection) return NULL;
351 if (editable->selection_start_pos == editable->selection_end_pos)
354 if (editable->selection_start_pos < editable->selection_end_pos) {
355 start_pos = editable->selection_start_pos;
356 end_pos = editable->selection_end_pos;
358 start_pos = editable->selection_end_pos;
359 end_pos = editable->selection_start_pos;
362 return gtk_editable_get_chars(editable, start_pos, end_pos);
366 * Walk through the widget tree and disclaim the selection from all currently
367 * realized GtkEditable widgets.
369 static void gtkut_check_before_remove(GtkWidget *widget, gpointer unused)
371 g_return_if_fail(widget != NULL);
373 if (!GTK_WIDGET_REALIZED(widget))
374 return; /* all nested widgets must be unrealized too */
375 if (GTK_IS_CONTAINER(widget))
376 gtk_container_forall(GTK_CONTAINER(widget),
377 gtkut_check_before_remove, NULL);
378 if (GTK_IS_EDITABLE(widget))
379 gtk_editable_claim_selection(GTK_EDITABLE(widget),
380 FALSE, GDK_CURRENT_TIME);
384 * Wrapper around gtk_container_remove to work around a bug in GtkText and
385 * GtkEntry (in all GTK+ versions up to and including at least 1.2.10).
387 * The problem is that unrealizing a GtkText or GtkEntry widget which has the
388 * active selection completely messes up selection handling, leading to
389 * non-working selections and crashes. Removing a widget from its container
390 * implies unrealizing it and all its child widgets; this triggers the bug if
391 * the removed widget or any of its children is GtkText or GtkEntry. As a
392 * workaround, this function walks through the widget subtree before removing
393 * and disclaims the selection from all GtkEditable widgets found.
395 * A similar workaround may be needed for gtk_widget_reparent(); currently it
396 * is not necessary because Sylpheed does not use gtk_widget_reparent() for
397 * GtkEditable widgets or containers holding such widgets.
399 void gtkut_container_remove(GtkContainer *container, GtkWidget *widget)
401 gtkut_check_before_remove(widget, NULL);
402 gtk_container_remove(container, widget);
405 void gtkut_window_popup(GtkWidget *window)
407 gint x, y, sx, sy, new_x, new_y;
409 g_return_if_fail(window != NULL);
410 g_return_if_fail(window->window != NULL);
412 sx = gdk_screen_width();
413 sy = gdk_screen_height();
415 gdk_window_get_origin(window->window, &x, &y);
416 new_x = x % sx; if (new_x < 0) new_x = 0;
417 new_y = y % sy; if (new_y < 0) new_y = 0;
418 if (new_x != x || new_y != y)
419 gdk_window_move(window->window, new_x, new_y);
421 gdk_window_raise(window->window);
422 gdk_window_show(window->window);
425 void gtkut_widget_get_uposition(GtkWidget *widget, gint *px, gint *py)
430 g_return_if_fail(widget != NULL);
431 g_return_if_fail(widget->window != NULL);
433 sx = gdk_screen_width();
434 sy = gdk_screen_height();
436 /* gdk_window_get_root_origin ever return *rootwindow*'s position */
437 gdk_window_get_root_origin(widget->window, &x, &y);
439 x %= sx; if (x < 0) x = 0;
440 y %= sy; if (y < 0) y = 0;
445 void gtkut_widget_disable_theme_engine(GtkWidget *widget)
447 GtkStyle *style, *new_style;
449 style = gtk_widget_get_style(widget);
452 GtkThemeEngine *engine;
454 engine = style->engine;
455 style->engine = NULL;
456 new_style = gtk_style_copy(style);
457 style->engine = engine;
458 gtk_widget_set_style(widget, new_style);
462 static void gtkut_widget_draw_cb(GtkWidget *widget, GdkRectangle *area,
466 gtk_signal_disconnect_by_data(GTK_OBJECT(widget), flag);
469 void gtkut_widget_wait_for_draw(GtkWidget *widget)
471 gboolean flag = FALSE;
473 if (!GTK_WIDGET_VISIBLE(widget) || !GTK_WIDGET_MAPPED(widget)) return;
475 gtk_signal_connect(GTK_OBJECT(widget), "draw",
476 GTK_SIGNAL_FUNC(gtkut_widget_draw_cb), &flag);
478 gtk_main_iteration();
481 static void gtkut_clist_bindings_add(GtkWidget *clist)
483 GtkBindingSet *binding_set;
485 binding_set = gtk_binding_set_by_class
486 (GTK_CLIST_CLASS(GTK_OBJECT(clist)->klass));
488 gtk_binding_entry_add_signal(binding_set, GDK_n, GDK_CONTROL_MASK,
489 "scroll_vertical", 2,
490 GTK_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
491 GTK_TYPE_FLOAT, 0.0);
492 gtk_binding_entry_add_signal(binding_set, GDK_p, GDK_CONTROL_MASK,
493 "scroll_vertical", 2,
494 GTK_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
495 GTK_TYPE_FLOAT, 0.0);
498 void gtkut_widget_init(void)
502 clist = gtk_clist_new(1);
503 gtkut_clist_bindings_add(clist);
504 gtk_widget_unref(clist);
506 clist = gtk_ctree_new(1, 0);
507 gtkut_clist_bindings_add(clist);
508 gtk_widget_unref(clist);
510 clist = gtk_sctree_new_with_titles(1, 0, NULL);
511 gtkut_clist_bindings_add(clist);
512 gtk_widget_unref(clist);
515 void gtkut_widget_set_app_icon(GtkWidget *widget)
517 #include "pixmaps/sylpheed.xpm"
518 static GdkPixmap *sylpheedxpm;
519 static GdkBitmap *sylpheedxpmmask;
521 g_return_if_fail(widget != NULL);
522 g_return_if_fail(widget->window != NULL);
524 PIXMAP_CREATE(widget, sylpheedxpm, sylpheedxpmmask, sylpheed_xpm);
526 gdk_window_set_icon(widget->window, NULL, sylpheedxpm, sylpheedxpmmask);
529 void gtkut_widget_set_composer_icon(GtkWidget *widget)
531 static GdkPixmap *xpm;
532 static GdkBitmap *bmp;
534 g_return_if_fail(widget != NULL);
535 g_return_if_fail(widget->window != NULL);
537 stock_pixmap_gdk(widget, STOCK_PIXMAP_MAIL_COMPOSE, &xpm, &bmp);
539 gdk_window_set_icon(widget->window, NULL, xpm, bmp);
542 GtkWidget *gtkut_account_menu_new(GList *ac_list,
543 GtkSignalFunc callback,
549 g_return_val_if_fail(ac_list != NULL, NULL);
551 menu = gtk_menu_new();
553 for (cur_ac = ac_list; cur_ac != NULL; cur_ac = cur_ac->next) {
556 PrefsAccount *account;
558 account = (PrefsAccount *) cur_ac->data;
560 name = g_strdup_printf("%s: %s <%s>",
561 account->account_name,
565 name = g_strdup_printf("%s: %s",
566 account->account_name,
568 MENUITEM_ADD(menu, menuitem, name, account->account_id);
570 if (callback != NULL)
571 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",