separate username/password for SMTP Auth
[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 void gtkut_widget_disable_theme_engine(GtkWidget *widget)
274 {
275         GtkStyle *style, *new_style;
276
277         style = gtk_widget_get_style(widget);
278
279         if (style->engine) {
280                 GtkThemeEngine *engine;
281
282                 engine = style->engine;
283                 style->engine = NULL;
284                 new_style = gtk_style_copy(style);
285                 style->engine = engine;
286                 gtk_widget_set_style(widget, new_style);
287         }
288 }
289
290 static void gtkut_widget_draw_cb(GtkWidget *widget, GdkRectangle *area,
291                                  gboolean *flag)
292 {
293         *flag = TRUE;
294         gtk_signal_disconnect_by_data(GTK_OBJECT(widget), flag);
295 }
296
297 void gtkut_widget_wait_for_draw(GtkWidget *widget)
298 {
299         gboolean flag = FALSE;
300
301         if (!GTK_WIDGET_VISIBLE(widget)) return;
302
303         gtk_signal_connect(GTK_OBJECT(widget), "draw",
304                            GTK_SIGNAL_FUNC(gtkut_widget_draw_cb), &flag);
305         while (!flag)
306                 gtk_main_iteration();
307 }
308
309 void gtkut_widget_get_uposition(GtkWidget *widget, gint *px, gint *py)
310 {
311         gint x, y;
312         gint sx, sy;
313
314         g_return_if_fail(widget != NULL);
315         g_return_if_fail(widget->window != NULL);
316
317         /* gdk_window_get_root_origin ever return *rootwindow*'s position*/
318         gdk_window_get_root_origin(widget->window, &x, &y);
319
320         sx = gdk_screen_width();
321         sy = gdk_screen_height();
322         x %= sx; if (x < 0) x = 0;
323         y %= sy; if (y < 0) y = 0;
324         *px = x;
325         *py = y;
326 }
327
328 static void gtkut_clist_bindings_add(GtkWidget *clist)
329 {
330         GtkBindingSet *binding_set;
331
332         binding_set = gtk_binding_set_by_class
333                 (GTK_CLIST_CLASS(GTK_OBJECT(clist)->klass));
334
335         gtk_binding_entry_add_signal(binding_set, GDK_n, GDK_CONTROL_MASK,
336                                      "scroll_vertical", 2,
337                                      GTK_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
338                                      GTK_TYPE_FLOAT, 0.0);
339         gtk_binding_entry_add_signal(binding_set, GDK_p, GDK_CONTROL_MASK,
340                                      "scroll_vertical", 2,
341                                      GTK_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
342                                      GTK_TYPE_FLOAT, 0.0);
343 }
344
345 void gtkut_widget_init(void)
346 {
347         GtkWidget *clist;
348
349         clist = gtk_clist_new(1);
350         gtkut_clist_bindings_add(clist);
351         gtk_widget_unref(clist);
352
353         clist = gtk_ctree_new(1, 0);
354         gtkut_clist_bindings_add(clist);
355         gtk_widget_unref(clist);
356
357         clist = gtk_sctree_new_with_titles(1, 0, NULL);
358         gtkut_clist_bindings_add(clist);
359         gtk_widget_unref(clist);
360 }
361
362 void gtkut_widget_set_app_icon(GtkWidget *widget)
363 {
364 #include "pixmaps/sylpheed.xpm" 
365         static GdkPixmap *sylpheedxpm;
366         static GdkBitmap *sylpheedxpmmask;
367         
368         g_return_if_fail(widget != NULL);
369         g_return_if_fail(widget->window != NULL);
370         if (!sylpheedxpm) {
371                 PIXMAP_CREATE(widget, sylpheedxpm, sylpheedxpmmask, sylpheed_xpm);
372         }               
373         gdk_window_set_icon(widget->window, NULL, sylpheedxpm, sylpheedxpmmask);
374 }
375
376 void gtkut_widget_set_composer_icon(GtkWidget *widget)
377 {
378 #include "pixmaps/stock_mail_compose.xpm"
379         static GdkPixmap *xpm;
380         static GdkBitmap *bmp;
381
382         g_return_if_fail(widget != NULL);
383         g_return_if_fail(widget->window != NULL);
384         if (!xpm) {
385                 PIXMAP_CREATE(widget, xpm, bmp, stock_mail_compose_xpm);
386         }
387         gdk_window_set_icon(widget->window, NULL, xpm, bmp);    
388 }
389
390