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