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