2005-09-16 [colin] 1.9.14cvs34
[claws.git] / src / gtk / description_window.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
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 2 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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
33 static void description_create                          (DescriptionWindow *dwindow);
34 static gboolean description_window_key_pressed          (GtkWidget *widget,
35                                                          GdkEventKey *event,
36                                                          gpointer data);
37 static gboolean description_window_focus_in_event       (GtkWidget *widget,
38                                                          GdkEventFocus *event,
39                                                          gpointer data);
40 static gboolean description_window_focus_out_event      (GtkWidget *widget,
41                                                          GdkEventFocus *event,
42                                                          gpointer data);
43
44
45 void description_window_create(DescriptionWindow *dwindow)
46 {
47         if (!dwindow->window) {
48                 description_create(dwindow);
49         
50                 gtk_window_set_transient_for(GTK_WINDOW(dwindow->window), GTK_WINDOW(dwindow->parent));
51         
52                 gtk_widget_show(dwindow->window);
53                 gtk_main();
54                 gtk_widget_hide(dwindow->window);
55                 gtk_widget_destroy(dwindow->window);
56                 dwindow->window = NULL; 
57         } else printf("windows exist\n");
58 }
59
60 static void description_create(DescriptionWindow * dwindow)
61 {
62         GtkWidget *vbox;
63         GtkWidget *table;
64         GtkWidget *hbbox;
65         GtkWidget *close_btn;
66         GtkWidget *scrolledwin;
67         int i;
68         int sz;
69         int line;
70         int j;
71
72         dwindow->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
73         gtk_widget_set_size_request(dwindow->window,400,450);
74         
75         gtk_window_set_title(GTK_WINDOW(dwindow->window),
76                              gettext(dwindow->title));
77         gtk_container_set_border_width(GTK_CONTAINER(dwindow->window), 8);
78         gtk_window_set_resizable(GTK_WINDOW(dwindow->window), TRUE);
79
80         /* Check number of lines to be show */
81         sz = 0;
82         for (i = 0; dwindow->symbol_table[i] != NULL; i = i + dwindow->columns) {
83                 sz++;
84         }
85         
86         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
87         gtk_widget_show(scrolledwin);
88         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
89         
90         table = gtk_table_new(sz, dwindow->columns, FALSE);
91         gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolledwin), table);
92         gtk_container_set_border_width(GTK_CONTAINER(table), 4);
93
94         gtk_table_set_col_spacings(GTK_TABLE(table), 10);
95
96         line = 0;
97         for(i = 0; dwindow->symbol_table[i] != NULL; i = i + dwindow->columns) {
98                 if(dwindow->symbol_table[i][0] != '\0') {
99                         GtkWidget *label;
100
101                         for (j = 0; j < dwindow->columns; j++) {
102                                 gint col = j;
103                                 gint colend = j+1;
104                                 /* Expand using next NULL columns */
105                                 while ((colend < dwindow->columns) && 
106                                        (dwindow->symbol_table[i+colend] == NULL)) {
107                                        colend++;
108                                        j++;
109                                 }
110                                 label = gtk_label_new(gettext(dwindow->symbol_table[i+col]));
111                                 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
112                                 gtk_misc_set_alignment (GTK_MISC(label), 0, 0);
113                                 gtk_table_attach(GTK_TABLE(table), label,
114                                                  col, colend, line, line+1,
115                                                  GTK_EXPAND | GTK_FILL, 0,
116                                                  0, 0);
117                         }
118                 } else {
119                         GtkWidget *separator;
120                         
121                         separator = gtk_hseparator_new();
122                         gtk_table_attach(GTK_TABLE(table), separator,
123                                          0, dwindow->columns, line, line+1,
124                                          GTK_EXPAND | GTK_FILL, 0,
125                                          0, 4);
126                 }
127                 line++;
128         }
129
130         gtkut_stock_button_set_create(&hbbox, &close_btn, GTK_STOCK_CLOSE,
131                                       NULL, NULL, NULL, NULL);
132         gtk_widget_show(hbbox);
133
134         vbox = gtk_vbox_new(FALSE, 0);
135         gtk_widget_show(vbox);
136         gtk_container_add(GTK_CONTAINER(dwindow->window), vbox);
137         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(scrolledwin),
138                            TRUE, TRUE, 0);
139         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(hbbox),
140                            FALSE, FALSE, 3);
141                            
142 /* OLD CODE
143         gtk_table_attach_defaults(GTK_TABLE(table), hbbox,
144                                   1, 2, i, i+1);
145 */
146         gtk_widget_grab_default(close_btn);
147         g_signal_connect(G_OBJECT(close_btn), "clicked",
148                          G_CALLBACK(gtk_main_quit), NULL);
149         g_signal_connect(G_OBJECT(dwindow->window), "key_press_event",
150                          G_CALLBACK(description_window_key_pressed), NULL);
151         g_signal_connect(G_OBJECT(dwindow->window), "focus_in_event",
152                          G_CALLBACK(description_window_focus_in_event), NULL);
153         g_signal_connect(G_OBJECT(dwindow->window), "focus_out_event",
154                          G_CALLBACK(description_window_focus_out_event), NULL);
155         g_signal_connect(G_OBJECT(dwindow->window), "delete_event",
156                          G_CALLBACK(gtk_main_quit), NULL);
157
158         gtk_widget_show_all(table);
159 }
160
161 static gboolean description_window_key_pressed(GtkWidget *widget,
162                                                GdkEventKey *event,
163                                                gpointer data)
164 {
165         if (event && event->keyval == GDK_Escape)
166                 gtk_main_quit();
167         return FALSE;
168 }
169
170 static gboolean description_window_focus_in_event (GtkWidget *widget,
171                                                    GdkEventFocus *event,
172                                                    gpointer data)
173 {
174         if (gtk_grab_get_current() != widget)
175                 gtk_grab_add(GTK_WIDGET(widget));
176
177         return FALSE;
178 }
179
180 static gboolean description_window_focus_out_event (GtkWidget *widget,
181                                                    GdkEventFocus *event,
182                                                    gpointer data)
183 {
184         gtk_grab_remove(GTK_WIDGET(widget));
185                 
186         return FALSE;
187 }
188
189
190