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