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