some minor updates
[claws.git] / src / gtkutils.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999,2000 Hiroyuki Yamamoto
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
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>
35
36 #include "intl.h"
37 #include "gtkutils.h"
38 #include "utils.h"
39 #include "gtksctree.h"
40 #include "codeconv.h"
41
42 gint gtkut_get_font_width(GdkFont *font)
43 {
44         gchar *str;
45         gint width;
46
47         if (conv_get_current_charset() == C_UTF_8)
48                 str = "Abcdef";
49         else
50                 str = _("Abcdef");
51
52         width = gdk_string_width(font, str);
53         width /= strlen(str);
54
55         return width;
56 }
57
58 gint gtkut_get_font_height(GdkFont *font)
59 {
60         gchar *str;
61         gint height;
62
63         if (conv_get_current_charset() == C_UTF_8)
64                 str = "Abcdef";
65         else
66                 str = _("Abcdef");
67
68         height = gdk_string_height(font, str);
69
70         return height;
71 }
72
73 void gtkut_convert_int_to_gdk_color(gint rgbvalue, GdkColor *color)
74 {
75         g_return_if_fail(color != NULL);
76
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);
81 }
82
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)
87 {
88         g_return_if_fail(bbox != NULL);
89         g_return_if_fail(button1 != NULL);
90
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);
94
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);
99
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);
105         }
106
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);
112         }
113 }
114
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))
121
122 void gtkut_ctree_node_move_if_on_the_edge(GtkCTree *ctree, GtkCTreeNode *node)
123 {
124         GtkCList *clist = GTK_CLIST(ctree);
125         gint row;
126         GtkVisibility row_visibility, prev_row_visibility, next_row_visibility;
127
128         g_return_if_fail(ctree != NULL);
129         g_return_if_fail(node != NULL);
130
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);
136
137         if (row_visibility == GTK_VISIBILITY_NONE) {
138                 gtk_clist_moveto(clist, row, -1, 0.5, 0);
139                 return;
140         }
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;
148
149         if (prev_row_visibility != GTK_VISIBILITY_FULL) {
150                 gtk_clist_moveto(clist, row, -1, 0.2, 0);
151                 return;
152         }
153         if (next_row_visibility != GTK_VISIBILITY_FULL) {
154                 gtk_clist_moveto(clist, row, -1, 0.8, 0);
155                 return;
156         }
157 }
158
159 #undef CELL_SPACING
160 #undef ROW_TOP_YPIXEL
161 #undef ROW_FROM_YPIXEL
162
163 gint gtkut_ctree_get_nth_from_node(GtkCTree *ctree, GtkCTreeNode *node)
164 {
165         g_return_val_if_fail(ctree != NULL, -1);
166         g_return_val_if_fail(node != NULL, -1);
167
168         return g_list_position(GTK_CLIST(ctree)->row_list, (GList *)node);
169 }
170
171 /* get the next node, including the invisible one */
172 GtkCTreeNode *gtkut_ctree_node_next(GtkCTree *ctree, GtkCTreeNode *node)
173 {
174         GtkCTreeNode *parent;
175
176         if (GTK_CTREE_ROW(node)->children)
177                 return GTK_CTREE_ROW(node)->children;
178
179         if (GTK_CTREE_ROW(node)->sibling)
180                 return GTK_CTREE_ROW(node)->sibling;
181
182         for (parent = GTK_CTREE_ROW(node)->parent; parent != NULL;
183              parent = GTK_CTREE_ROW(parent)->parent) {
184                 if (GTK_CTREE_ROW(parent)->sibling)
185                         return GTK_CTREE_ROW(parent)->sibling;
186         }
187
188         return NULL;
189 }
190
191 GtkCTreeNode *gtkut_ctree_find_collapsed_parent(GtkCTree *ctree,
192                                                 GtkCTreeNode *node)
193 {
194         if (!node) return NULL;
195
196         while ((node = GTK_CTREE_ROW(node)->parent) != NULL) {
197                 if (!GTK_CTREE_ROW(node)->expanded)
198                         return node;
199         }
200
201         return NULL;
202 }
203
204 void gtkut_ctree_set_focus_row(GtkCTree *ctree, GtkCTreeNode *node)
205 {
206         gtkut_clist_set_focus_row(GTK_CLIST(ctree),
207                                   gtkut_ctree_get_nth_from_node(ctree, node));
208 }
209
210 void gtkut_clist_set_focus_row(GtkCList *clist, gint row)
211 {
212         clist->focus_row = row;
213         GTKUT_CTREE_REFRESH(clist);
214 }
215
216 void gtkut_combo_set_items(GtkCombo *combo, const gchar *str1, ...)
217 {
218         va_list args;
219         gchar *s;
220         GList *combo_items = NULL;
221
222         g_return_if_fail(str1 != NULL);
223
224         combo_items = g_list_append(combo_items, (gpointer)str1);
225         va_start(args, str1);
226         s = va_arg(args, gchar*);
227         while (s) {
228                 combo_items = g_list_append(combo_items, (gpointer)s);
229                 s = va_arg(args, gchar*);
230         }
231         va_end(args);
232
233         gtk_combo_set_popdown_strings(combo, combo_items);
234
235         g_list_free(combo_items);
236 }
237
238 void gtkut_widget_disable_theme_engine(GtkWidget *widget)
239 {
240         GtkStyle *style, *new_style;
241
242         style = gtk_widget_get_style(widget);
243
244         if (style->engine) {
245                 GtkThemeEngine *engine;
246
247                 engine = style->engine;
248                 style->engine = NULL;
249                 new_style = gtk_style_copy(style);
250                 style->engine = engine;
251                 gtk_widget_set_style(widget, new_style);
252         }
253 }
254
255 static void gtkut_widget_draw_cb(GtkWidget *widget, GdkRectangle *area,
256                                  gboolean *flag)
257 {
258         *flag = TRUE;
259         gtk_signal_disconnect_by_data(GTK_OBJECT(widget), flag);
260 }
261
262 void gtkut_widget_wait_for_draw(GtkWidget *widget)
263 {
264         gboolean flag = FALSE;
265
266         if (!GTK_WIDGET_VISIBLE(widget)) return;
267
268         gtk_signal_connect(GTK_OBJECT(widget), "draw",
269                            GTK_SIGNAL_FUNC(gtkut_widget_draw_cb), &flag);
270         while (!flag)
271                 gtk_main_iteration();
272 }
273
274 void gtkut_widget_get_uposition(GtkWidget *widget, gint *px, gint *py)
275 {
276         gint x, y;
277         gint sx, sy;
278
279         g_return_if_fail(widget != NULL);
280         g_return_if_fail(widget->window != NULL);
281
282         /* gdk_window_get_root_origin ever return *rootwindow*'s position*/
283         gdk_window_get_root_origin(widget->window, &x, &y);
284
285         sx = gdk_screen_width();
286         sy = gdk_screen_height();
287         x %= sx; if (x < 0) x += sx;
288         y %= sy; if (y < 0) y += sy;
289         *px = x;
290         *py = y;
291 }
292
293 static void gtkut_clist_bindings_add(GtkWidget *clist)
294 {
295         GtkBindingSet *binding_set;
296
297         binding_set = gtk_binding_set_by_class
298                 (GTK_CLIST_CLASS(GTK_OBJECT(clist)->klass));
299
300         gtk_binding_entry_add_signal(binding_set, GDK_n, GDK_CONTROL_MASK,
301                                      "scroll_vertical", 2,
302                                      GTK_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
303                                      GTK_TYPE_FLOAT, 0.0);
304         gtk_binding_entry_add_signal(binding_set, GDK_p, GDK_CONTROL_MASK,
305                                      "scroll_vertical", 2,
306                                      GTK_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
307                                      GTK_TYPE_FLOAT, 0.0);
308 }
309
310 void gtkut_widget_init(void)
311 {
312         GtkWidget *clist;
313
314         clist = gtk_clist_new(1);
315         gtkut_clist_bindings_add(clist);
316         gtk_widget_unref(clist);
317
318         clist = gtk_ctree_new(1, 0);
319         gtkut_clist_bindings_add(clist);
320         gtk_widget_unref(clist);
321
322         clist = gtk_sctree_new_with_titles(1, 0, NULL);
323         gtkut_clist_bindings_add(clist);
324         gtk_widget_unref(clist);
325 }