filtering dialog box and some changes
[claws.git] / src / gtkutils.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999,2000 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 #include "intl.h"
37 #include "gtkutils.h"
38 #include "utils.h"
39 #include "gtksctree.h"
40 #include "codeconv.h"
41
42 gint gtkut_get_font_width(GdkFont *font)
43 {
44         gchar *str;
45         gint width;
46
47         if (conv_get_current_charset() == C_UTF_8)
48                 str = "Abcdef";
49         else
50                 str = _("Abcdef");
51
52         width = gdk_string_width(font, str);
53         width /= strlen(str);
54
55         return width;
56 }
57
58 gint gtkut_get_font_height(GdkFont *font)
59 {
60         gchar *str;
61         gint height;
62
63         if (conv_get_current_charset() == C_UTF_8)
64                 str = "Abcdef";
65         else
66                 str = _("Abcdef");
67
68         height = gdk_string_height(font, str);
69
70         return height;
71 }
72
73 void gtkut_convert_int_to_gdk_color(gint rgbvalue, GdkColor *color)
74 {
75         g_return_if_fail(color != NULL);
76
77         color->pixel = 0L;
78         color->red   = (int) (((gdouble)((rgbvalue & 0xff0000) >> 16) / 255.0) * 65535.0);
79         color->green = (int) (((gdouble)((rgbvalue & 0x00ff00) >>  8) / 255.0) * 65535.0);
80         color->blue  = (int) (((gdouble) (rgbvalue & 0x0000ff)        / 255.0) * 65535.0);
81 }
82
83 void gtkut_button_set_create(GtkWidget **bbox,
84                              GtkWidget **button1, const gchar *label1,
85                              GtkWidget **button2, const gchar *label2,
86                              GtkWidget **button3, const gchar *label3)
87 {
88         g_return_if_fail(bbox != NULL);
89         g_return_if_fail(button1 != NULL);
90
91         *bbox = gtk_hbutton_box_new();
92         gtk_button_box_set_layout(GTK_BUTTON_BOX(*bbox), GTK_BUTTONBOX_END);
93         gtk_button_box_set_spacing(GTK_BUTTON_BOX(*bbox), 5);
94
95         *button1 = gtk_button_new_with_label(label1);
96         GTK_WIDGET_SET_FLAGS(*button1, GTK_CAN_DEFAULT);
97         gtk_box_pack_start(GTK_BOX(*bbox), *button1, TRUE, TRUE, 0);
98         gtk_widget_show(*button1);
99
100         if (button2) {
101                 *button2 = gtk_button_new_with_label(label2);
102                 GTK_WIDGET_SET_FLAGS(*button2, GTK_CAN_DEFAULT);
103                 gtk_box_pack_start(GTK_BOX(*bbox), *button2, TRUE, TRUE, 0);
104                 gtk_widget_show(*button2);
105         }
106
107         if (button3) {
108                 *button3 = gtk_button_new_with_label(label3);
109                 GTK_WIDGET_SET_FLAGS(*button3, GTK_CAN_DEFAULT);
110                 gtk_box_pack_start(GTK_BOX(*bbox), *button3, TRUE, TRUE, 0);
111                 gtk_widget_show(*button3);
112         }
113 }
114
115 #define CELL_SPACING 1
116 #define ROW_TOP_YPIXEL(clist, row) (((clist)->row_height * (row)) + \
117                                     (((row) + 1) * CELL_SPACING) + \
118                                     (clist)->voffset)
119 #define ROW_FROM_YPIXEL(clist, y) (((y) - (clist)->voffset) / \
120                                    ((clist)->row_height + CELL_SPACING))
121
122 void gtkut_ctree_node_move_if_on_the_edge(GtkCTree *ctree, GtkCTreeNode *node)
123 {
124         GtkCList *clist = GTK_CLIST(ctree);
125         gint row;
126         GtkVisibility row_visibility, prev_row_visibility, next_row_visibility;
127
128         g_return_if_fail(ctree != NULL);
129         g_return_if_fail(node != NULL);
130
131         row = g_list_position(clist->row_list, (GList *)node);
132         if (row < 0 || row >= clist->rows || clist->row_height == 0) return;
133         row_visibility = gtk_clist_row_is_visible(clist, row);
134         prev_row_visibility = gtk_clist_row_is_visible(clist, row - 1);
135         next_row_visibility = gtk_clist_row_is_visible(clist, row + 1);
136
137         if (row_visibility == GTK_VISIBILITY_NONE) {
138                 gtk_clist_moveto(clist, row, -1, 0.5, 0);
139                 return;
140         }
141         if (row_visibility == GTK_VISIBILITY_FULL &&
142             prev_row_visibility == GTK_VISIBILITY_FULL &&
143             next_row_visibility == GTK_VISIBILITY_FULL)
144                 return;
145         if (prev_row_visibility != GTK_VISIBILITY_FULL &&
146             next_row_visibility != GTK_VISIBILITY_FULL)
147                 return;
148
149         if (prev_row_visibility != GTK_VISIBILITY_FULL) {
150                 gtk_clist_moveto(clist, row, -1, 0.2, 0);
151                 return;
152         }
153         if (next_row_visibility != GTK_VISIBILITY_FULL) {
154                 gtk_clist_moveto(clist, row, -1, 0.8, 0);
155                 return;
156         }
157 }
158
159 #undef CELL_SPACING
160 #undef ROW_TOP_YPIXEL
161 #undef ROW_FROM_YPIXEL
162
163 gint gtkut_ctree_get_nth_from_node(GtkCTree *ctree, GtkCTreeNode *node)
164 {
165         g_return_val_if_fail(ctree != NULL, -1);
166         g_return_val_if_fail(node != NULL, -1);
167
168         return g_list_position(GTK_CLIST(ctree)->row_list, (GList *)node);
169 }
170
171 void gtkut_ctree_set_focus_row(GtkCTree *ctree, GtkCTreeNode *node)
172 {
173         gtkut_clist_set_focus_row(GTK_CLIST(ctree),
174                                   gtkut_ctree_get_nth_from_node(ctree, node));
175 }
176
177 void gtkut_clist_set_focus_row(GtkCList *clist, gint row)
178 {
179         clist->focus_row = row;
180         GTKUT_CTREE_REFRESH(clist);
181 }
182
183 void gtkut_combo_set_items(GtkCombo *combo, const gchar *str1, ...)
184 {
185         va_list args;
186         gchar *s;
187         GList *combo_items = NULL;
188
189         g_return_if_fail(str1 != NULL);
190
191         combo_items = g_list_append(combo_items, (gpointer)str1);
192         va_start(args, str1);
193         s = va_arg(args, gchar*);
194         while (s) {
195                 combo_items = g_list_append(combo_items, (gpointer)s);
196                 s = va_arg(args, gchar*);
197         }
198         va_end(args);
199
200         gtk_combo_set_popdown_strings(combo, combo_items);
201
202         g_list_free(combo_items);
203 }
204
205 void gtkut_widget_disable_theme_engine(GtkWidget *widget)
206 {
207         GtkStyle *style, *new_style;
208
209         style = gtk_widget_get_style(widget);
210
211         if (style->engine) {
212                 GtkThemeEngine *engine;
213
214                 engine = style->engine;
215                 style->engine = NULL;
216                 new_style = gtk_style_copy(style);
217                 style->engine = engine;
218                 gtk_widget_set_style(widget, new_style);
219         }
220 }
221
222 static void gtkut_widget_draw_cb(GtkWidget *widget, GdkRectangle *area,
223                                  gboolean *flag)
224 {
225         *flag = TRUE;
226         gtk_signal_disconnect_by_data(GTK_OBJECT(widget), flag);
227 }
228
229 void gtkut_widget_wait_for_draw(GtkWidget *widget)
230 {
231         gboolean flag = FALSE;
232
233         if (!GTK_WIDGET_VISIBLE(widget)) return;
234
235         gtk_signal_connect(GTK_OBJECT(widget), "draw",
236                            GTK_SIGNAL_FUNC(gtkut_widget_draw_cb), &flag);
237         while (!flag)
238                 gtk_main_iteration();
239 }
240
241 void gtkut_widget_get_uposition(GtkWidget *widget, gint *px, gint *py)
242 {
243         gint x, y;
244         gint sx, sy;
245
246         g_return_if_fail(widget != NULL);
247         g_return_if_fail(widget->window != NULL);
248
249         /* gdk_window_get_root_origin ever return *rootwindow*'s position*/
250         gdk_window_get_root_origin(widget->window, &x, &y);
251
252         sx = gdk_screen_width();
253         sy = gdk_screen_height();
254         x %= sx; if (x < 0) x += sx;
255         y %= sy; if (y < 0) y += sy;
256         *px = x;
257         *py = y;
258 }
259
260 static void gtkut_clist_bindings_add(GtkWidget *clist)
261 {
262         GtkBindingSet *binding_set;
263
264         binding_set = gtk_binding_set_by_class
265                 (GTK_CLIST_CLASS(GTK_OBJECT(clist)->klass));
266
267         gtk_binding_entry_add_signal(binding_set, GDK_n, GDK_CONTROL_MASK,
268                                      "scroll_vertical", 2,
269                                      GTK_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
270                                      GTK_TYPE_FLOAT, 0.0);
271         gtk_binding_entry_add_signal(binding_set, GDK_p, GDK_CONTROL_MASK,
272                                      "scroll_vertical", 2,
273                                      GTK_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
274                                      GTK_TYPE_FLOAT, 0.0);
275 }
276
277 void gtkut_widget_init(void)
278 {
279         GtkWidget *clist;
280
281         clist = gtk_clist_new(1);
282         gtkut_clist_bindings_add(clist);
283         gtk_widget_unref(clist);
284
285         clist = gtk_ctree_new(1, 0);
286         gtkut_clist_bindings_add(clist);
287         gtk_widget_unref(clist);
288
289         clist = gtk_sctree_new_with_titles(1, 0, NULL);
290         gtkut_clist_bindings_add(clist);
291         gtk_widget_unref(clist);
292 }