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