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