New address book.
[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 (!node) return NULL;
177
178         if (GTK_CTREE_ROW(node)->children)
179                 return GTK_CTREE_ROW(node)->children;
180
181         if (GTK_CTREE_ROW(node)->sibling)
182                 return GTK_CTREE_ROW(node)->sibling;
183
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;
188         }
189
190         return NULL;
191 }
192
193 GtkCTreeNode *gtkut_ctree_find_collapsed_parent(GtkCTree *ctree,
194                                                 GtkCTreeNode *node)
195 {
196         if (!node) return NULL;
197
198         while ((node = GTK_CTREE_ROW(node)->parent) != NULL) {
199                 if (!GTK_CTREE_ROW(node)->expanded)
200                         return node;
201         }
202
203         return NULL;
204 }
205
206 void gtkut_ctree_expand_parent_all(GtkCTree *ctree, GtkCTreeNode *node)
207 {
208         while ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
209                 gtk_ctree_expand(ctree, node);
210 }
211
212 void gtkut_ctree_set_focus_row(GtkCTree *ctree, GtkCTreeNode *node)
213 {
214         gtkut_clist_set_focus_row(GTK_CLIST(ctree),
215                                   gtkut_ctree_get_nth_from_node(ctree, node));
216 }
217
218 void gtkut_clist_set_focus_row(GtkCList *clist, gint row)
219 {
220         clist->focus_row = row;
221         GTKUT_CTREE_REFRESH(clist);
222 }
223
224 void gtkut_combo_set_items(GtkCombo *combo, const gchar *str1, ...)
225 {
226         va_list args;
227         gchar *s;
228         GList *combo_items = NULL;
229
230         g_return_if_fail(str1 != NULL);
231
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*);
238         }
239         va_end(args);
240
241         gtk_combo_set_popdown_strings(combo, combo_items);
242
243         g_list_free(combo_items);
244 }
245
246 void gtkut_widget_disable_theme_engine(GtkWidget *widget)
247 {
248         GtkStyle *style, *new_style;
249
250         style = gtk_widget_get_style(widget);
251
252         if (style->engine) {
253                 GtkThemeEngine *engine;
254
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);
260         }
261 }
262
263 static void gtkut_widget_draw_cb(GtkWidget *widget, GdkRectangle *area,
264                                  gboolean *flag)
265 {
266         *flag = TRUE;
267         gtk_signal_disconnect_by_data(GTK_OBJECT(widget), flag);
268 }
269
270 void gtkut_widget_wait_for_draw(GtkWidget *widget)
271 {
272         gboolean flag = FALSE;
273
274         if (!GTK_WIDGET_VISIBLE(widget)) return;
275
276         gtk_signal_connect(GTK_OBJECT(widget), "draw",
277                            GTK_SIGNAL_FUNC(gtkut_widget_draw_cb), &flag);
278         while (!flag)
279                 gtk_main_iteration();
280 }
281
282 void gtkut_widget_get_uposition(GtkWidget *widget, gint *px, gint *py)
283 {
284         gint x, y;
285         gint sx, sy;
286
287         g_return_if_fail(widget != NULL);
288         g_return_if_fail(widget->window != NULL);
289
290         /* gdk_window_get_root_origin ever return *rootwindow*'s position*/
291         gdk_window_get_root_origin(widget->window, &x, &y);
292
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;
299 }
300
301 static void gtkut_clist_bindings_add(GtkWidget *clist)
302 {
303         GtkBindingSet *binding_set;
304
305         binding_set = gtk_binding_set_by_class
306                 (GTK_CLIST_CLASS(GTK_OBJECT(clist)->klass));
307
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);
316 }
317
318 void gtkut_widget_init(void)
319 {
320         GtkWidget *clist;
321
322         clist = gtk_clist_new(1);
323         gtkut_clist_bindings_add(clist);
324         gtk_widget_unref(clist);
325
326         clist = gtk_ctree_new(1, 0);
327         gtkut_clist_bindings_add(clist);
328         gtk_widget_unref(clist);
329
330         clist = gtk_sctree_new_with_titles(1, 0, NULL);
331         gtkut_clist_bindings_add(clist);
332         gtk_widget_unref(clist);
333 }
334
335 void gtkut_widget_set_app_icon(GtkWidget *widget)
336 {
337 #include "pixmaps/sylpheed.xpm" 
338         static GdkPixmap *sylpheedxpm;
339         static GdkBitmap *sylpheedxpmmask;
340         
341         g_return_if_fail(widget != NULL);
342         g_return_if_fail(widget->window != NULL);
343         if (!sylpheedxpm) {
344                 PIXMAP_CREATE(widget, sylpheedxpm, sylpheedxpmmask, sylpheed_xpm);
345         }               
346         gdk_window_set_icon(widget->window, NULL, sylpheedxpm, sylpheedxpmmask);
347 }
348
349 void gtkut_widget_set_composer_icon(GtkWidget *widget)
350 {
351 #include "pixmaps/stock_mail_compose.xpm"
352         static GdkPixmap *xpm;
353         static GdkBitmap *bmp;
354
355         g_return_if_fail(widget != NULL);
356         g_return_if_fail(widget->window != NULL);
357         if (!xpm) {
358                 PIXMAP_CREATE(widget, xpm, bmp, stock_mail_compose_xpm);
359         }
360         gdk_window_set_icon(widget->window, NULL, xpm, bmp);    
361 }
362
363