initial message templates support
[claws.git] / src / gtkutils.c
blobe65adbabdb05ab13c9d80f2c03c1ff434758232d
1 /*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999,2000 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.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
24 #include <glib.h>
25 #include <gdk/gdkkeysyms.h>
26 #include <gdk/gdk.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>
34 #include <stdarg.h>
36 #include "intl.h"
37 #include "gtkutils.h"
38 #include "utils.h"
39 #include "gtksctree.h"
40 #include "codeconv.h"
42 gint gtkut_get_font_width(GdkFont *font)
44 gchar *str;
45 gint width;
47 if (conv_get_current_charset() == C_UTF_8)
48 str = "Abcdef";
49 else
50 str = _("Abcdef");
52 width = gdk_string_width(font, str);
53 width /= strlen(str);
55 return width;
58 gint gtkut_get_font_height(GdkFont *font)
60 gchar *str;
61 gint height;
63 if (conv_get_current_charset() == C_UTF_8)
64 str = "Abcdef";
65 else
66 str = _("Abcdef");
68 height = gdk_string_height(font, str);
70 return height;
73 void gtkut_convert_int_to_gdk_color(gint rgbvalue, GdkColor *color)
75 g_return_if_fail(color != NULL);
77 color->pixel = 0L;
78 color->red = (int) (((gdouble)((rgbvalue & 0xff0000) >> 16) / 255.0) * 65535.0);
79 color->green = (int) (((gdouble)((rgbvalue & 0x00ff00) >> 8) / 255.0) * 65535.0);
80 color->blue = (int) (((gdouble) (rgbvalue & 0x0000ff) / 255.0) * 65535.0);
83 void gtkut_button_set_create(GtkWidget **bbox,
84 GtkWidget **button1, const gchar *label1,
85 GtkWidget **button2, const gchar *label2,
86 GtkWidget **button3, const gchar *label3)
88 g_return_if_fail(bbox != NULL);
89 g_return_if_fail(button1 != NULL);
91 *bbox = gtk_hbutton_box_new();
92 gtk_button_box_set_layout(GTK_BUTTON_BOX(*bbox), GTK_BUTTONBOX_END);
93 gtk_button_box_set_spacing(GTK_BUTTON_BOX(*bbox), 5);
95 *button1 = gtk_button_new_with_label(label1);
96 GTK_WIDGET_SET_FLAGS(*button1, GTK_CAN_DEFAULT);
97 gtk_box_pack_start(GTK_BOX(*bbox), *button1, TRUE, TRUE, 0);
98 gtk_widget_show(*button1);
100 if (button2) {
101 *button2 = gtk_button_new_with_label(label2);
102 GTK_WIDGET_SET_FLAGS(*button2, GTK_CAN_DEFAULT);
103 gtk_box_pack_start(GTK_BOX(*bbox), *button2, TRUE, TRUE, 0);
104 gtk_widget_show(*button2);
107 if (button3) {
108 *button3 = gtk_button_new_with_label(label3);
109 GTK_WIDGET_SET_FLAGS(*button3, GTK_CAN_DEFAULT);
110 gtk_box_pack_start(GTK_BOX(*bbox), *button3, TRUE, TRUE, 0);
111 gtk_widget_show(*button3);
115 #define CELL_SPACING 1
116 #define ROW_TOP_YPIXEL(clist, row) (((clist)->row_height * (row)) + \
117 (((row) + 1) * CELL_SPACING) + \
118 (clist)->voffset)
119 #define ROW_FROM_YPIXEL(clist, y) (((y) - (clist)->voffset) / \
120 ((clist)->row_height + CELL_SPACING))
122 void gtkut_ctree_node_move_if_on_the_edge(GtkCTree *ctree, GtkCTreeNode *node)
124 GtkCList *clist = GTK_CLIST(ctree);
125 gint row;
126 GtkVisibility row_visibility, prev_row_visibility, next_row_visibility;
128 g_return_if_fail(ctree != NULL);
129 g_return_if_fail(node != NULL);
131 row = g_list_position(clist->row_list, (GList *)node);
132 if (row < 0 || row >= clist->rows || clist->row_height == 0) return;
133 row_visibility = gtk_clist_row_is_visible(clist, row);
134 prev_row_visibility = gtk_clist_row_is_visible(clist, row - 1);
135 next_row_visibility = gtk_clist_row_is_visible(clist, row + 1);
137 if (row_visibility == GTK_VISIBILITY_NONE) {
138 gtk_clist_moveto(clist, row, -1, 0.5, 0);
139 return;
141 if (row_visibility == GTK_VISIBILITY_FULL &&
142 prev_row_visibility == GTK_VISIBILITY_FULL &&
143 next_row_visibility == GTK_VISIBILITY_FULL)
144 return;
145 if (prev_row_visibility != GTK_VISIBILITY_FULL &&
146 next_row_visibility != GTK_VISIBILITY_FULL)
147 return;
149 if (prev_row_visibility != GTK_VISIBILITY_FULL) {
150 gtk_clist_moveto(clist, row, -1, 0.2, 0);
151 return;
153 if (next_row_visibility != GTK_VISIBILITY_FULL) {
154 gtk_clist_moveto(clist, row, -1, 0.8, 0);
155 return;
159 #undef CELL_SPACING
160 #undef ROW_TOP_YPIXEL
161 #undef ROW_FROM_YPIXEL
163 gint gtkut_ctree_get_nth_from_node(GtkCTree *ctree, GtkCTreeNode *node)
165 g_return_val_if_fail(ctree != NULL, -1);
166 g_return_val_if_fail(node != NULL, -1);
168 return g_list_position(GTK_CLIST(ctree)->row_list, (GList *)node);
171 /* get the next node, including the invisible one */
172 GtkCTreeNode *gtkut_ctree_node_next(GtkCTree *ctree, GtkCTreeNode *node)
174 GtkCTreeNode *parent;
176 if (!node) return NULL;
178 if (GTK_CTREE_ROW(node)->children)
179 return GTK_CTREE_ROW(node)->children;
181 if (GTK_CTREE_ROW(node)->sibling)
182 return GTK_CTREE_ROW(node)->sibling;
184 for (parent = GTK_CTREE_ROW(node)->parent; parent != NULL;
185 parent = GTK_CTREE_ROW(parent)->parent) {
186 if (GTK_CTREE_ROW(parent)->sibling)
187 return GTK_CTREE_ROW(parent)->sibling;
190 return NULL;
193 GtkCTreeNode *gtkut_ctree_find_collapsed_parent(GtkCTree *ctree,
194 GtkCTreeNode *node)
196 if (!node) return NULL;
198 while ((node = GTK_CTREE_ROW(node)->parent) != NULL) {
199 if (!GTK_CTREE_ROW(node)->expanded)
200 return node;
203 return NULL;
206 void gtkut_ctree_expand_parent_all(GtkCTree *ctree, GtkCTreeNode *node)
208 while ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
209 gtk_ctree_expand(ctree, node);
212 void gtkut_ctree_set_focus_row(GtkCTree *ctree, GtkCTreeNode *node)
214 gtkut_clist_set_focus_row(GTK_CLIST(ctree),
215 gtkut_ctree_get_nth_from_node(ctree, node));
218 void gtkut_clist_set_focus_row(GtkCList *clist, gint row)
220 clist->focus_row = row;
221 GTKUT_CTREE_REFRESH(clist);
224 void gtkut_combo_set_items(GtkCombo *combo, const gchar *str1, ...)
226 va_list args;
227 gchar *s;
228 GList *combo_items = NULL;
230 g_return_if_fail(str1 != NULL);
232 combo_items = g_list_append(combo_items, (gpointer)str1);
233 va_start(args, str1);
234 s = va_arg(args, gchar*);
235 while (s) {
236 combo_items = g_list_append(combo_items, (gpointer)s);
237 s = va_arg(args, gchar*);
239 va_end(args);
241 gtk_combo_set_popdown_strings(combo, combo_items);
243 g_list_free(combo_items);
246 void gtkut_widget_disable_theme_engine(GtkWidget *widget)
248 GtkStyle *style, *new_style;
250 style = gtk_widget_get_style(widget);
252 if (style->engine) {
253 GtkThemeEngine *engine;
255 engine = style->engine;
256 style->engine = NULL;
257 new_style = gtk_style_copy(style);
258 style->engine = engine;
259 gtk_widget_set_style(widget, new_style);
263 static void gtkut_widget_draw_cb(GtkWidget *widget, GdkRectangle *area,
264 gboolean *flag)
266 *flag = TRUE;
267 gtk_signal_disconnect_by_data(GTK_OBJECT(widget), flag);
270 void gtkut_widget_wait_for_draw(GtkWidget *widget)
272 gboolean flag = FALSE;
274 if (!GTK_WIDGET_VISIBLE(widget)) return;
276 gtk_signal_connect(GTK_OBJECT(widget), "draw",
277 GTK_SIGNAL_FUNC(gtkut_widget_draw_cb), &flag);
278 while (!flag)
279 gtk_main_iteration();
282 void gtkut_widget_get_uposition(GtkWidget *widget, gint *px, gint *py)
284 gint x, y;
285 gint sx, sy;
287 g_return_if_fail(widget != NULL);
288 g_return_if_fail(widget->window != NULL);
290 /* gdk_window_get_root_origin ever return *rootwindow*'s position*/
291 gdk_window_get_root_origin(widget->window, &x, &y);
293 sx = gdk_screen_width();
294 sy = gdk_screen_height();
295 x %= sx; if (x < 0) x += sx;
296 y %= sy; if (y < 0) y += sy;
297 *px = x;
298 *py = y;
301 static void gtkut_clist_bindings_add(GtkWidget *clist)
303 GtkBindingSet *binding_set;
305 binding_set = gtk_binding_set_by_class
306 (GTK_CLIST_CLASS(GTK_OBJECT(clist)->klass));
308 gtk_binding_entry_add_signal(binding_set, GDK_n, GDK_CONTROL_MASK,
309 "scroll_vertical", 2,
310 GTK_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
311 GTK_TYPE_FLOAT, 0.0);
312 gtk_binding_entry_add_signal(binding_set, GDK_p, GDK_CONTROL_MASK,
313 "scroll_vertical", 2,
314 GTK_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
315 GTK_TYPE_FLOAT, 0.0);
318 void gtkut_widget_init(void)
320 GtkWidget *clist;
322 clist = gtk_clist_new(1);
323 gtkut_clist_bindings_add(clist);
324 gtk_widget_unref(clist);
326 clist = gtk_ctree_new(1, 0);
327 gtkut_clist_bindings_add(clist);
328 gtk_widget_unref(clist);
330 clist = gtk_sctree_new_with_titles(1, 0, NULL);
331 gtkut_clist_bindings_add(clist);
332 gtk_widget_unref(clist);