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