sync with sylpheed 0.7.0cvs24
[claws.git] / src / gtkutils.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 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 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
37 #  include <wchar.h>
38 #  include <wctype.h>
39 #endif
40
41 #include "intl.h"
42 #include "gtkutils.h"
43 #include "utils.h"
44 #include "gtksctree.h"
45 #include "codeconv.h"
46
47 gint gtkut_get_font_width(GdkFont *font)
48 {
49         gchar *str;
50         gint width;
51
52         if (conv_get_current_charset() == C_UTF_8)
53                 str = "Abcdef";
54         else
55                 str = _("Abcdef");
56
57         width = gdk_string_width(font, str);
58         width /= strlen(str);
59
60         return width;
61 }
62
63 gint gtkut_get_font_height(GdkFont *font)
64 {
65         gchar *str;
66         gint height;
67
68         if (conv_get_current_charset() == C_UTF_8)
69                 str = "Abcdef";
70         else
71                 str = _("Abcdef");
72
73         height = gdk_string_height(font, str);
74
75         return height;
76 }
77
78 void gtkut_convert_int_to_gdk_color(gint rgbvalue, GdkColor *color)
79 {
80         g_return_if_fail(color != NULL);
81
82         color->pixel = 0L;
83         color->red   = (int) (((gdouble)((rgbvalue & 0xff0000) >> 16) / 255.0) * 65535.0);
84         color->green = (int) (((gdouble)((rgbvalue & 0x00ff00) >>  8) / 255.0) * 65535.0);
85         color->blue  = (int) (((gdouble) (rgbvalue & 0x0000ff)        / 255.0) * 65535.0);
86 }
87
88 void gtkut_button_set_create(GtkWidget **bbox,
89                              GtkWidget **button1, const gchar *label1,
90                              GtkWidget **button2, const gchar *label2,
91                              GtkWidget **button3, const gchar *label3)
92 {
93         g_return_if_fail(bbox != NULL);
94         g_return_if_fail(button1 != NULL);
95
96         *bbox = gtk_hbutton_box_new();
97         gtk_button_box_set_layout(GTK_BUTTON_BOX(*bbox), GTK_BUTTONBOX_END);
98         gtk_button_box_set_spacing(GTK_BUTTON_BOX(*bbox), 5);
99
100         *button1 = gtk_button_new_with_label(label1);
101         GTK_WIDGET_SET_FLAGS(*button1, GTK_CAN_DEFAULT);
102         gtk_box_pack_start(GTK_BOX(*bbox), *button1, TRUE, TRUE, 0);
103         gtk_widget_show(*button1);
104
105         if (button2) {
106                 *button2 = gtk_button_new_with_label(label2);
107                 GTK_WIDGET_SET_FLAGS(*button2, GTK_CAN_DEFAULT);
108                 gtk_box_pack_start(GTK_BOX(*bbox), *button2, TRUE, TRUE, 0);
109                 gtk_widget_show(*button2);
110         }
111
112         if (button3) {
113                 *button3 = gtk_button_new_with_label(label3);
114                 GTK_WIDGET_SET_FLAGS(*button3, GTK_CAN_DEFAULT);
115                 gtk_box_pack_start(GTK_BOX(*bbox), *button3, TRUE, TRUE, 0);
116                 gtk_widget_show(*button3);
117         }
118 }
119
120 #define CELL_SPACING 1
121 #define ROW_TOP_YPIXEL(clist, row) (((clist)->row_height * (row)) + \
122                                     (((row) + 1) * CELL_SPACING) + \
123                                     (clist)->voffset)
124 #define ROW_FROM_YPIXEL(clist, y) (((y) - (clist)->voffset) / \
125                                    ((clist)->row_height + CELL_SPACING))
126
127 void gtkut_ctree_node_move_if_on_the_edge(GtkCTree *ctree, GtkCTreeNode *node)
128 {
129         GtkCList *clist = GTK_CLIST(ctree);
130         gint row;
131         GtkVisibility row_visibility, prev_row_visibility, next_row_visibility;
132
133         g_return_if_fail(ctree != NULL);
134         g_return_if_fail(node != NULL);
135
136         row = g_list_position(clist->row_list, (GList *)node);
137         if (row < 0 || row >= clist->rows || clist->row_height == 0) return;
138         row_visibility = gtk_clist_row_is_visible(clist, row);
139         prev_row_visibility = gtk_clist_row_is_visible(clist, row - 1);
140         next_row_visibility = gtk_clist_row_is_visible(clist, row + 1);
141
142         if (row_visibility == GTK_VISIBILITY_NONE) {
143                 gtk_clist_moveto(clist, row, -1, 0.5, 0);
144                 return;
145         }
146         if (row_visibility == GTK_VISIBILITY_FULL &&
147             prev_row_visibility == GTK_VISIBILITY_FULL &&
148             next_row_visibility == GTK_VISIBILITY_FULL)
149                 return;
150         if (prev_row_visibility != GTK_VISIBILITY_FULL &&
151             next_row_visibility != GTK_VISIBILITY_FULL)
152                 return;
153
154         if (prev_row_visibility != GTK_VISIBILITY_FULL) {
155                 gtk_clist_moveto(clist, row, -1, 0.2, 0);
156                 return;
157         }
158         if (next_row_visibility != GTK_VISIBILITY_FULL) {
159                 gtk_clist_moveto(clist, row, -1, 0.8, 0);
160                 return;
161         }
162 }
163
164 #undef CELL_SPACING
165 #undef ROW_TOP_YPIXEL
166 #undef ROW_FROM_YPIXEL
167
168 gint gtkut_ctree_get_nth_from_node(GtkCTree *ctree, GtkCTreeNode *node)
169 {
170         g_return_val_if_fail(ctree != NULL, -1);
171         g_return_val_if_fail(node != NULL, -1);
172
173         return g_list_position(GTK_CLIST(ctree)->row_list, (GList *)node);
174 }
175
176 /* get the next node, including the invisible one */
177 GtkCTreeNode *gtkut_ctree_node_next(GtkCTree *ctree, GtkCTreeNode *node)
178 {
179         GtkCTreeNode *parent;
180
181         if (!node) return NULL;
182
183         if (GTK_CTREE_ROW(node)->children)
184                 return GTK_CTREE_ROW(node)->children;
185
186         if (GTK_CTREE_ROW(node)->sibling)
187                 return GTK_CTREE_ROW(node)->sibling;
188
189         for (parent = GTK_CTREE_ROW(node)->parent; parent != NULL;
190              parent = GTK_CTREE_ROW(parent)->parent) {
191                 if (GTK_CTREE_ROW(parent)->sibling)
192                         return GTK_CTREE_ROW(parent)->sibling;
193         }
194
195         return NULL;
196 }
197
198 GtkCTreeNode *gtkut_ctree_find_collapsed_parent(GtkCTree *ctree,
199                                                 GtkCTreeNode *node)
200 {
201         if (!node) return NULL;
202
203         while ((node = GTK_CTREE_ROW(node)->parent) != NULL) {
204                 if (!GTK_CTREE_ROW(node)->expanded)
205                         return node;
206         }
207
208         return NULL;
209 }
210
211 void gtkut_ctree_expand_parent_all(GtkCTree *ctree, GtkCTreeNode *node)
212 {
213         while ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
214                 gtk_ctree_expand(ctree, node);
215 }
216
217 void gtkut_ctree_set_focus_row(GtkCTree *ctree, GtkCTreeNode *node)
218 {
219         gtkut_clist_set_focus_row(GTK_CLIST(ctree),
220                                   gtkut_ctree_get_nth_from_node(ctree, node));
221 }
222
223 void gtkut_clist_set_focus_row(GtkCList *clist, gint row)
224 {
225         clist->focus_row = row;
226         GTKUT_CTREE_REFRESH(clist);
227 }
228
229 void gtkut_combo_set_items(GtkCombo *combo, const gchar *str1, ...)
230 {
231         va_list args;
232         gchar *s;
233         GList *combo_items = NULL;
234
235         g_return_if_fail(str1 != NULL);
236
237         combo_items = g_list_append(combo_items, (gpointer)str1);
238         va_start(args, str1);
239         s = va_arg(args, gchar*);
240         while (s) {
241                 combo_items = g_list_append(combo_items, (gpointer)s);
242                 s = va_arg(args, gchar*);
243         }
244         va_end(args);
245
246         gtk_combo_set_popdown_strings(combo, combo_items);
247
248         g_list_free(combo_items);
249 }
250
251 /*
252  * Walk through the widget tree and disclaim the selection from all currently
253  * realized GtkEditable widgets.
254  */
255 static void gtkut_check_before_remove(GtkWidget *widget, gpointer unused)
256 {
257         g_return_if_fail(widget != NULL);
258
259         if (!GTK_WIDGET_REALIZED(widget))
260                 return; /* all nested widgets must be unrealized too */
261         if (GTK_IS_CONTAINER(widget))
262                 gtk_container_forall(GTK_CONTAINER(widget),
263                                      gtkut_check_before_remove, NULL);
264         if (GTK_IS_EDITABLE(widget))
265                 gtk_editable_claim_selection(GTK_EDITABLE(widget),
266                                              FALSE, GDK_CURRENT_TIME);
267 }
268
269 /*
270  * Wrapper around gtk_container_remove to work around a bug in GtkText and
271  * GtkEntry (in all GTK+ versions up to and including at least 1.2.10).
272  *
273  * The problem is that unrealizing a GtkText or GtkEntry widget which has the
274  * active selection completely messes up selection handling, leading to
275  * non-working selections and crashes.  Removing a widget from its container
276  * implies unrealizing it and all its child widgets; this triggers the bug if
277  * the removed widget or any of its children is GtkText or GtkEntry.  As a
278  * workaround, this function walks through the widget subtree before removing
279  * and disclaims the selection from all GtkEditable widgets found.
280  *
281  * A similar workaround may be needed for gtk_widget_reparent(); currently it
282  * is not necessary because Sylpheed does not use gtk_widget_reparent() for
283  * GtkEditable widgets or containers holding such widgets.
284  */
285 void gtkut_container_remove(GtkContainer *container, GtkWidget *widget)
286 {
287         gtkut_check_before_remove(widget, NULL);
288         gtk_container_remove(container, widget);
289 }
290
291 gboolean gtkut_stext_match_string(GtkSText *text, gint pos, wchar_t *wcs,
292                                   gint len, gboolean case_sens)
293 {
294         gint match_count = 0;
295
296         for (; match_count < len; pos++, match_count++) {
297                 if (case_sens) {
298                         if (GTK_STEXT_INDEX(text, pos) != wcs[match_count])
299                                 break;
300                 } else {
301                         if (towlower(GTK_STEXT_INDEX(text, pos)) !=
302                             towlower(wcs[match_count]))
303                                 break;
304                 }
305         }
306
307         if (match_count == len)
308                 return TRUE;
309         else
310                 return FALSE;
311 }
312
313 guint gtkut_stext_str_compare_n(GtkSText *text, guint pos1, guint pos2,
314                                 guint len, guint text_len)
315 {
316         guint i;
317         GdkWChar ch1, ch2;
318
319         for (i = 0; i < len && pos1 + i < text_len && pos2 + i < text_len; i++) {
320                 ch1 = GTK_STEXT_INDEX(text, pos1 + i);
321                 ch2 = GTK_STEXT_INDEX(text, pos2 + i);
322                 if (ch1 != ch2)
323                         break;
324         }
325
326         return i;
327 }
328
329 guint gtkut_stext_str_compare(GtkSText *text, guint start_pos, guint text_len,
330                               const gchar *str)
331 {
332         wchar_t *wcs;
333         guint len;
334         gboolean result;
335
336         if (!str) return 0;
337
338         wcs = strdup_mbstowcs(str);
339         if (!wcs) return 0;
340         len = wcslen(wcs);
341
342         if (len > text_len - start_pos)
343                 result = FALSE;
344         else
345                 result = gtkut_stext_match_string(text, start_pos, wcs, len,
346                                                   TRUE);
347
348         g_free(wcs);
349
350         return result ? len : 0;
351 }
352
353 gboolean gtkut_stext_is_uri_string(GtkSText *text,
354                                    guint start_pos, guint text_len)
355 {
356         if (gtkut_stext_str_compare(text, start_pos, text_len, "http://") ||
357             gtkut_stext_str_compare(text, start_pos, text_len, "ftp://")  ||
358             gtkut_stext_str_compare(text, start_pos, text_len, "https://"))
359                 return TRUE;
360
361         return FALSE;
362 }
363
364 void gtkut_widget_disable_theme_engine(GtkWidget *widget)
365 {
366         GtkStyle *style, *new_style;
367
368         style = gtk_widget_get_style(widget);
369
370         if (style->engine) {
371                 GtkThemeEngine *engine;
372
373                 engine = style->engine;
374                 style->engine = NULL;
375                 new_style = gtk_style_copy(style);
376                 style->engine = engine;
377                 gtk_widget_set_style(widget, new_style);
378         }
379 }
380
381 static void gtkut_widget_draw_cb(GtkWidget *widget, GdkRectangle *area,
382                                  gboolean *flag)
383 {
384         *flag = TRUE;
385         gtk_signal_disconnect_by_data(GTK_OBJECT(widget), flag);
386 }
387
388 void gtkut_widget_wait_for_draw(GtkWidget *widget)
389 {
390         gboolean flag = FALSE;
391
392         if (!GTK_WIDGET_VISIBLE(widget)) return;
393
394         gtk_signal_connect(GTK_OBJECT(widget), "draw",
395                            GTK_SIGNAL_FUNC(gtkut_widget_draw_cb), &flag);
396         while (!flag)
397                 gtk_main_iteration();
398 }
399
400 void gtkut_widget_get_uposition(GtkWidget *widget, gint *px, gint *py)
401 {
402         gint x, y;
403         gint sx, sy;
404
405         g_return_if_fail(widget != NULL);
406         g_return_if_fail(widget->window != NULL);
407
408         /* gdk_window_get_root_origin ever return *rootwindow*'s position*/
409         gdk_window_get_root_origin(widget->window, &x, &y);
410
411         sx = gdk_screen_width();
412         sy = gdk_screen_height();
413         x %= sx; if (x < 0) x = 0;
414         y %= sy; if (y < 0) y = 0;
415         *px = x;
416         *py = y;
417 }
418
419 static void gtkut_clist_bindings_add(GtkWidget *clist)
420 {
421         GtkBindingSet *binding_set;
422
423         binding_set = gtk_binding_set_by_class
424                 (GTK_CLIST_CLASS(GTK_OBJECT(clist)->klass));
425
426         gtk_binding_entry_add_signal(binding_set, GDK_n, GDK_CONTROL_MASK,
427                                      "scroll_vertical", 2,
428                                      GTK_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
429                                      GTK_TYPE_FLOAT, 0.0);
430         gtk_binding_entry_add_signal(binding_set, GDK_p, GDK_CONTROL_MASK,
431                                      "scroll_vertical", 2,
432                                      GTK_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
433                                      GTK_TYPE_FLOAT, 0.0);
434 }
435
436 void gtkut_widget_init(void)
437 {
438         GtkWidget *clist;
439
440         clist = gtk_clist_new(1);
441         gtkut_clist_bindings_add(clist);
442         gtk_widget_unref(clist);
443
444         clist = gtk_ctree_new(1, 0);
445         gtkut_clist_bindings_add(clist);
446         gtk_widget_unref(clist);
447
448         clist = gtk_sctree_new_with_titles(1, 0, NULL);
449         gtkut_clist_bindings_add(clist);
450         gtk_widget_unref(clist);
451 }
452
453 void gtkut_widget_set_app_icon(GtkWidget *widget)
454 {
455 #include "pixmaps/sylpheed.xpm" 
456         static GdkPixmap *sylpheedxpm;
457         static GdkBitmap *sylpheedxpmmask;
458         
459         g_return_if_fail(widget != NULL);
460         g_return_if_fail(widget->window != NULL);
461         if (!sylpheedxpm) {
462                 PIXMAP_CREATE(widget, sylpheedxpm, sylpheedxpmmask, sylpheed_xpm);
463         }               
464         gdk_window_set_icon(widget->window, NULL, sylpheedxpm, sylpheedxpmmask);
465 }
466
467 void gtkut_widget_set_composer_icon(GtkWidget *widget)
468 {
469 #include "pixmaps/stock_mail_compose.xpm"
470         static GdkPixmap *xpm;
471         static GdkBitmap *bmp;
472
473         g_return_if_fail(widget != NULL);
474         g_return_if_fail(widget->window != NULL);
475         if (!xpm) {
476                 PIXMAP_CREATE(widget, xpm, bmp, stock_mail_compose_xpm);
477         }
478         gdk_window_set_icon(widget->window, NULL, xpm, bmp);    
479 }
480
481