sync with sylpheed 0.6.5cvs14
[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 gboolean gtkut_text_match_string(GtkText *text, gint pos, wchar_t *wcs,
252                                  gint len, gboolean case_sens)
253 {
254         gint match_count = 0;
255
256         for (; match_count < len; pos++, match_count++) {
257                 if (case_sens) {
258                         if (GTK_TEXT_INDEX(text, pos) != wcs[match_count])
259                                 break;
260                 } else {
261                         if (towlower(GTK_TEXT_INDEX(text, pos)) !=
262                             towlower(wcs[match_count]))
263                                 break;
264                 }
265         }
266
267         if (match_count == len)
268                 return TRUE;
269         else
270                 return FALSE;
271 }
272
273 guint gtkut_text_str_compare_n(GtkText *text, guint pos1, guint pos2,
274                                guint len, guint text_len)
275 {
276         guint i;
277         GdkWChar ch1, ch2;
278
279         for (i = 0; i < len && pos1 + i < text_len && pos2 + i < text_len; i++) {
280                 ch1 = GTK_TEXT_INDEX(text, pos1 + i);
281                 ch2 = GTK_TEXT_INDEX(text, pos2 + i);
282                 if (ch1 != ch2)
283                         break;
284         }
285
286         return i;
287 }
288
289 guint gtkut_text_str_compare(GtkText *text, guint start_pos, guint text_len,
290                              const gchar *str)
291 {
292         wchar_t *wcs;
293         guint len;
294         gboolean result;
295
296         if (!str) return 0;
297
298         wcs = strdup_mbstowcs(str);
299         if (!wcs) return 0;
300         len = wcslen(wcs);
301
302         if (len > text_len - start_pos)
303                 result = FALSE;
304         else
305                 result = gtkut_text_match_string(text, start_pos, wcs, len,
306                                                  TRUE);
307
308         g_free(wcs);
309
310         return result ? len : 0;
311 }
312
313 gboolean gtkut_text_is_uri_string(GtkText *text,
314                                   guint start_pos, guint text_len)
315 {
316         if (gtkut_text_str_compare(text, start_pos, text_len, "http://") ||
317             gtkut_text_str_compare(text, start_pos, text_len, "ftp://")  ||
318             gtkut_text_str_compare(text, start_pos, text_len, "https://"))
319                 return TRUE;
320
321         return FALSE;
322 }
323
324 void gtkut_widget_disable_theme_engine(GtkWidget *widget)
325 {
326         GtkStyle *style, *new_style;
327
328         style = gtk_widget_get_style(widget);
329
330         if (style->engine) {
331                 GtkThemeEngine *engine;
332
333                 engine = style->engine;
334                 style->engine = NULL;
335                 new_style = gtk_style_copy(style);
336                 style->engine = engine;
337                 gtk_widget_set_style(widget, new_style);
338         }
339 }
340
341 static void gtkut_widget_draw_cb(GtkWidget *widget, GdkRectangle *area,
342                                  gboolean *flag)
343 {
344         *flag = TRUE;
345         gtk_signal_disconnect_by_data(GTK_OBJECT(widget), flag);
346 }
347
348 void gtkut_widget_wait_for_draw(GtkWidget *widget)
349 {
350         gboolean flag = FALSE;
351
352         if (!GTK_WIDGET_VISIBLE(widget)) return;
353
354         gtk_signal_connect(GTK_OBJECT(widget), "draw",
355                            GTK_SIGNAL_FUNC(gtkut_widget_draw_cb), &flag);
356         while (!flag)
357                 gtk_main_iteration();
358 }
359
360 void gtkut_widget_get_uposition(GtkWidget *widget, gint *px, gint *py)
361 {
362         gint x, y;
363         gint sx, sy;
364
365         g_return_if_fail(widget != NULL);
366         g_return_if_fail(widget->window != NULL);
367
368         /* gdk_window_get_root_origin ever return *rootwindow*'s position*/
369         gdk_window_get_root_origin(widget->window, &x, &y);
370
371         sx = gdk_screen_width();
372         sy = gdk_screen_height();
373         x %= sx; if (x < 0) x = 0;
374         y %= sy; if (y < 0) y = 0;
375         *px = x;
376         *py = y;
377 }
378
379 static void gtkut_clist_bindings_add(GtkWidget *clist)
380 {
381         GtkBindingSet *binding_set;
382
383         binding_set = gtk_binding_set_by_class
384                 (GTK_CLIST_CLASS(GTK_OBJECT(clist)->klass));
385
386         gtk_binding_entry_add_signal(binding_set, GDK_n, GDK_CONTROL_MASK,
387                                      "scroll_vertical", 2,
388                                      GTK_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
389                                      GTK_TYPE_FLOAT, 0.0);
390         gtk_binding_entry_add_signal(binding_set, GDK_p, GDK_CONTROL_MASK,
391                                      "scroll_vertical", 2,
392                                      GTK_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
393                                      GTK_TYPE_FLOAT, 0.0);
394 }
395
396 void gtkut_widget_init(void)
397 {
398         GtkWidget *clist;
399
400         clist = gtk_clist_new(1);
401         gtkut_clist_bindings_add(clist);
402         gtk_widget_unref(clist);
403
404         clist = gtk_ctree_new(1, 0);
405         gtkut_clist_bindings_add(clist);
406         gtk_widget_unref(clist);
407
408         clist = gtk_sctree_new_with_titles(1, 0, NULL);
409         gtkut_clist_bindings_add(clist);
410         gtk_widget_unref(clist);
411 }
412
413 void gtkut_widget_set_app_icon(GtkWidget *widget)
414 {
415 #include "pixmaps/sylpheed.xpm" 
416         static GdkPixmap *sylpheedxpm;
417         static GdkBitmap *sylpheedxpmmask;
418         
419         g_return_if_fail(widget != NULL);
420         g_return_if_fail(widget->window != NULL);
421         if (!sylpheedxpm) {
422                 PIXMAP_CREATE(widget, sylpheedxpm, sylpheedxpmmask, sylpheed_xpm);
423         }               
424         gdk_window_set_icon(widget->window, NULL, sylpheedxpm, sylpheedxpmmask);
425 }
426
427 void gtkut_widget_set_composer_icon(GtkWidget *widget)
428 {
429 #include "pixmaps/stock_mail_compose.xpm"
430         static GdkPixmap *xpm;
431         static GdkBitmap *bmp;
432
433         g_return_if_fail(widget != NULL);
434         g_return_if_fail(widget->window != NULL);
435         if (!xpm) {
436                 PIXMAP_CREATE(widget, xpm, bmp, stock_mail_compose_xpm);
437         }
438         gdk_window_set_icon(widget->window, NULL, xpm, bmp);    
439 }
440
441