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