2012-05-16 [paul] 3.8.0cvs42
[claws.git] / src / gtk / description_window.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2011 Hiroyuki Yamamoto and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <glib/gi18n.h>
26 #include <gtk/gtk.h>
27 #include <gdk/gdkkeysyms.h>
28
29 #include "manage_window.h"
30 #include "description_window.h"
31 #include "gtkutils.h"
32 #include "prefs_gtk.h"
33
34 static void description_create                          (DescriptionWindow *dwindow);
35 static gboolean description_window_key_pressed          (GtkWidget *widget,
36                                                          GdkEventKey *event,
37                                                          gpointer data);
38 static gboolean description_window_focus_in_event       (GtkWidget *widget,
39                                                          GdkEventFocus *event,
40                                                          gpointer data);
41 static gboolean description_window_focus_out_event      (GtkWidget *widget,
42                                                          GdkEventFocus *event,
43                                                          gpointer data);
44 static void description_window_destroy                  (GtkWidget *parent,
45                                                          gpointer data);
46
47 void description_window_create(DescriptionWindow *dwindow)
48 {
49         if (!dwindow->window) {
50                 description_create(dwindow);
51         
52                 gtk_window_set_transient_for(GTK_WINDOW(dwindow->window), GTK_WINDOW(dwindow->parent));
53                 gtk_window_set_modal(GTK_WINDOW(dwindow->parent), TRUE);
54                 gtk_window_set_destroy_with_parent(GTK_WINDOW(dwindow->window), TRUE);
55                 gtk_widget_show(dwindow->window);
56
57                 /* in case the description window is closed using the WM's [X] button */
58                 g_signal_connect(G_OBJECT(dwindow->window), "destroy",
59                                 G_CALLBACK(gtk_widget_destroyed), &dwindow->window);
60
61         } else g_print("windows exist\n");
62 }
63
64 static void description_create(DescriptionWindow * dwindow)
65 {
66         GtkWidget *hbox;
67         GtkWidget *label;
68         GtkWidget *vbox;
69         GtkWidget *table;
70         GtkWidget *hbbox;
71         GtkWidget *close_btn;
72         GtkWidget *scrolledwin;
73         int i;
74         int sz;
75         int line;
76         int j;
77         int *max_width = g_new0(int, dwindow->columns), width=0;
78         GtkRequisition req;
79         
80         dwindow->window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "description_window");
81         
82         gtk_window_set_title(GTK_WINDOW(dwindow->window),
83                              gettext(dwindow->title));
84         gtk_container_set_border_width(GTK_CONTAINER(dwindow->window), 8);
85         gtk_window_set_resizable(GTK_WINDOW(dwindow->window), TRUE);
86
87         /* Check number of lines to be show */
88         sz = 0;
89         for (i = 0; dwindow->symbol_table[i] != NULL; i = i + dwindow->columns) {
90                 sz++;
91         }
92         
93         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
94         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
95                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
96         
97         table = gtk_table_new(sz, dwindow->columns, FALSE);
98         gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolledwin), table);
99         gtk_container_set_border_width(GTK_CONTAINER(table), 4);
100
101         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
102
103         line = 0;
104         for(i = 0; dwindow->symbol_table[i] != NULL; i = i + dwindow->columns) {
105                 if(dwindow->symbol_table[i][0] != '\0') {
106                         GtkWidget *label;
107
108                         for (j = 0; j < dwindow->columns; j++) {
109                                 gint col = j;
110                                 gint colend = j+1;
111                                 /* Expand using next NULL columns */
112                                 while ((colend < dwindow->columns) && 
113                                        (dwindow->symbol_table[i+colend] == NULL)) {
114                                        colend++;
115                                        j++;
116                                 }
117                                 label = gtk_label_new(gettext(dwindow->symbol_table[i+col]));
118                                 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
119                                 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
120                                 gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
121                                 gtk_misc_set_alignment (GTK_MISC(label), 0, 0);
122                                 gtk_table_attach(GTK_TABLE(table), label,
123                                                  col, colend, line, line+1,
124                                                  (GtkAttachOptions) (GTK_FILL),
125                                                  (GtkAttachOptions) (0), 0, 2);
126
127                                 gtk_widget_size_request(label, &req);
128                                 if(req.width > max_width[j])
129                                         max_width[j] = req.width;
130                         }
131                 } else {
132                         GtkWidget *separator;
133                         
134                         separator = gtk_hseparator_new();
135                         gtk_table_attach(GTK_TABLE(table), separator,
136                                          0, dwindow->columns, line, line+1,
137                                          (GtkAttachOptions) (GTK_FILL),
138                                          (GtkAttachOptions) (0), 0, 4);
139                 }
140                 line++;
141         }
142
143         for(j=0; j<dwindow->columns; j++)
144                 width += max_width[j];
145
146         g_free(max_width);
147         width += 100;
148         
149         gtkut_stock_button_set_create(&hbbox, &close_btn, GTK_STOCK_CLOSE,
150                                       NULL, NULL, NULL, NULL);
151
152         vbox = gtk_vbox_new(FALSE, VSPACING_NARROW);
153         gtk_container_add(GTK_CONTAINER(dwindow->window), vbox);
154
155         hbox = gtk_hbox_new(FALSE, 0);
156         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
157
158         label = gtk_label_new(gettext(dwindow->description));
159         gtk_widget_set_size_request(GTK_WIDGET(label), width-2, -1);
160         gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
161         gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
162         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
163
164         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(scrolledwin),
165                            TRUE, TRUE, 0);
166         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(hbbox),
167                            FALSE, FALSE, 3);
168                            
169         gtk_widget_grab_default(close_btn);
170
171         g_signal_connect(G_OBJECT(close_btn), "clicked",
172                          G_CALLBACK(description_window_destroy), dwindow);
173         g_signal_connect(G_OBJECT(dwindow->window), "key_press_event",
174                          G_CALLBACK(description_window_key_pressed), dwindow);
175         g_signal_connect(G_OBJECT(dwindow->window), "focus_in_event",
176                          G_CALLBACK(description_window_focus_in_event), NULL);
177         g_signal_connect(G_OBJECT(dwindow->window), "focus_out_event",
178                          G_CALLBACK(description_window_focus_out_event), NULL);
179         g_signal_connect(G_OBJECT(dwindow->window), "delete_event",
180                          G_CALLBACK(description_window_destroy), dwindow);
181         
182         if(dwindow->parent)
183                 g_signal_connect(G_OBJECT(dwindow->parent), "hide",
184                         G_CALLBACK(description_window_destroy), dwindow);
185
186         gtk_widget_show_all(vbox);
187         gtk_widget_set_size_request(dwindow->window,
188                                (width < 400) ? 400 : width, 450);       
189 }
190
191 static gboolean description_window_key_pressed(GtkWidget *widget,
192                                                GdkEventKey *event,
193                                                gpointer data)
194 {
195         if (event && event->keyval == GDK_KEY_Escape)
196                 description_window_destroy(widget, data);
197         return FALSE;
198 }
199
200 static gboolean description_window_focus_in_event (GtkWidget *widget,
201                                                    GdkEventFocus *event,
202                                                    gpointer data)
203 {
204         if (gtk_grab_get_current() != widget)
205                 gtk_grab_add(GTK_WIDGET(widget));
206
207         return FALSE;
208 }
209
210 static gboolean description_window_focus_out_event (GtkWidget *widget,
211                                                    GdkEventFocus *event,
212                                                    gpointer data)
213 {
214         gtk_grab_remove(GTK_WIDGET(widget));
215                 
216         return FALSE;
217 }
218
219 static void description_window_destroy (GtkWidget *widget, gpointer data)
220 {
221         DescriptionWindow *dwindow = (DescriptionWindow *) data;
222         
223         if(dwindow->window) {
224                 gtk_widget_hide(dwindow->window);
225                 gtk_widget_destroy(dwindow->window);
226                 dwindow->window = NULL;
227         }
228         
229         if(dwindow->parent)
230                 g_signal_handlers_disconnect_by_func(G_OBJECT(dwindow->parent), 
231                                         description_window_destroy, dwindow->parent);
232 }