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