prefs_fonts: be a little more user-friendly with the labels; news.c: remove unneeded...
[claws.git] / src / prefs_fonts.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2003-2004 Hiroyuki Yamamoto & The Sylpheed-Claws 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 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 "defs.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #include <glib.h>
30 #include <gtk/gtk.h>
31 #include <gdk/gdkkeysyms.h>
32
33 #include "intl.h"
34 #include "utils.h"
35 #include "prefs_common.h"
36 #include "prefs_gtk.h"
37
38 #include "gtk/gtkutils.h"
39 #include "gtk/prefswindow.h"
40
41 #include "manage_window.h"
42
43 typedef struct _FontsPage
44 {
45         PrefsPage page;
46
47         GtkWidget *window;              /* do not modify */
48
49         GtkWidget *entry_folderviewfont;
50         GtkWidget *entry_summaryviewfont;
51         GtkWidget *entry_messageviewfont;
52         GtkWidget *entry_boldfont;
53 } FontsPage;
54
55 static GtkWidget *font_sel_win;
56 static guint font_sel_conn_id;
57
58 static void prefs_font_select   (GtkButton *button, GtkEntry *entry);
59
60 static void prefs_font_selection_key_pressed    (GtkWidget      *widget,
61                                                  GdkEventKey    *event,
62                                                  gpointer        data);
63 static void prefs_font_selection_ok             (GtkButton      *button, GtkEntry *entry);
64
65 static void prefs_font_select(GtkButton *button, GtkEntry *entry)
66 {
67         gchar *font_name;
68         
69         g_return_if_fail(entry != NULL);
70         
71         if (!font_sel_win) {
72                 font_sel_win = gtk_font_selection_dialog_new
73                         (_("Font selection"));
74                 gtk_window_position(GTK_WINDOW(font_sel_win),
75                                     GTK_WIN_POS_CENTER);
76                 gtk_signal_connect(GTK_OBJECT(font_sel_win), "delete_event",
77                                    GTK_SIGNAL_FUNC(gtk_widget_hide_on_delete),
78                                    NULL);
79                 gtk_signal_connect
80                         (GTK_OBJECT(font_sel_win), "key_press_event",
81                          GTK_SIGNAL_FUNC(prefs_font_selection_key_pressed),
82                          NULL);
83                 gtk_signal_connect_object
84                         (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(font_sel_win)->cancel_button),
85                          "clicked",
86                          GTK_SIGNAL_FUNC(gtk_widget_hide_on_delete),
87                          GTK_OBJECT(font_sel_win));
88         }
89
90         if(font_sel_conn_id) {
91                 gtk_signal_disconnect(GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(font_sel_win)->ok_button), font_sel_conn_id);
92         }
93         font_sel_conn_id = gtk_signal_connect
94                 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(font_sel_win)->ok_button),
95                  "clicked",
96                  GTK_SIGNAL_FUNC(prefs_font_selection_ok),
97                  entry);
98         printf("%i\n", font_sel_conn_id);
99
100         font_name = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
101         gtk_font_selection_dialog_set_font_name(GTK_FONT_SELECTION_DIALOG(font_sel_win), font_name);
102         g_free(font_name);
103         manage_window_set_transient(GTK_WINDOW(font_sel_win));
104         gtk_window_set_modal(GTK_WINDOW(font_sel_win), TRUE);
105         gtk_widget_grab_focus
106                 (GTK_FONT_SELECTION_DIALOG(font_sel_win)->ok_button);
107         gtk_widget_show(font_sel_win);
108 }
109
110 static void prefs_font_selection_key_pressed(GtkWidget *widget,
111                                              GdkEventKey *event,
112                                              gpointer data)
113 {
114         if (event && event->keyval == GDK_Escape)
115                 gtk_widget_hide(font_sel_win);
116 }
117
118 static void prefs_font_selection_ok(GtkButton *button, GtkEntry *entry)
119 {
120         gchar *fontname;
121
122         fontname = gtk_font_selection_dialog_get_font_name
123                 (GTK_FONT_SELECTION_DIALOG(font_sel_win));
124
125         if (fontname) {
126                 gtk_entry_set_text(entry, fontname);
127
128                 g_free(fontname);
129         }
130
131         gtk_widget_hide(font_sel_win);
132 }
133
134 void prefs_fonts_create_widget(PrefsPage *_page, GtkWindow *window, 
135                                gpointer data)
136 {
137         FontsPage *prefs_fonts = (FontsPage *) _page;
138
139         GtkWidget *table;
140         GtkWidget *entry_folderviewfont;
141         GtkWidget *entry_summaryviewfont;
142         GtkWidget *entry_messageviewfont;
143         GtkWidget *entry_boldfont;
144         GtkWidget *tmplabel, *tmpbutton;
145         GtkWidget *vbox;
146         GtkWidget *hint_label;
147
148         table = gtk_table_new(8, 3, FALSE);
149         gtk_widget_show(table);
150         gtk_container_set_border_width(GTK_CONTAINER(table), 8);
151         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
152         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
153
154         tmplabel = gtk_label_new (_("Folder List"));
155         gtk_widget_show (tmplabel);
156         gtk_table_attach (GTK_TABLE (table), tmplabel, 0, 1, 0, 1,
157                          (GtkAttachOptions) GTK_FILL,
158                          (GtkAttachOptions) (0), 0, 0);
159         gtk_label_set_justify(GTK_LABEL(tmplabel), GTK_JUSTIFY_RIGHT);
160         gtk_misc_set_alignment(GTK_MISC(tmplabel), 1, 0.5);
161
162         entry_folderviewfont = gtk_entry_new ();
163         gtk_widget_show (entry_folderviewfont);
164         gtk_table_attach (GTK_TABLE (table), entry_folderviewfont, 1, 2, 0, 1,
165                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
166                          (GtkAttachOptions) (0), 0, 0);
167         gtk_entry_set_text(GTK_ENTRY(entry_folderviewfont), prefs_common.normalfont);
168
169         tmpbutton = gtk_button_new_with_label (" ... ");
170         gtk_widget_show (tmpbutton);
171         gtk_table_attach (GTK_TABLE (table), tmpbutton, 2, 3, 0, 1,
172                           0, 0, 0, 0);
173         gtk_signal_connect (GTK_OBJECT(tmpbutton), "clicked",
174                                 GTK_SIGNAL_FUNC(prefs_font_select), entry_folderviewfont);
175
176         tmplabel = gtk_label_new (_("Message List"));
177         gtk_widget_show (tmplabel);
178         gtk_table_attach (GTK_TABLE (table), tmplabel, 0, 1, 1, 2,
179                          (GtkAttachOptions) GTK_FILL,
180                          (GtkAttachOptions) (0), 0, 0);
181         gtk_label_set_justify(GTK_LABEL(tmplabel), GTK_JUSTIFY_RIGHT);
182         gtk_misc_set_alignment(GTK_MISC(tmplabel), 1, 0.5);
183
184         entry_summaryviewfont = gtk_entry_new ();
185         gtk_widget_show (entry_summaryviewfont);
186         gtk_table_attach (GTK_TABLE (table), entry_summaryviewfont, 1, 2, 1, 2,
187                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
188                          (GtkAttachOptions) (0), 0, 0);
189         gtk_entry_set_text(GTK_ENTRY(entry_summaryviewfont), prefs_common.smallfont);
190
191         tmpbutton = gtk_button_new_with_label (" ... ");
192         gtk_widget_show (tmpbutton);
193         gtk_table_attach (GTK_TABLE (table), tmpbutton, 2, 3, 1, 2,
194                           0, 0, 0, 0);
195         gtk_signal_connect (GTK_OBJECT(tmpbutton), "clicked",
196                             GTK_SIGNAL_FUNC(prefs_font_select), entry_summaryviewfont);
197
198         tmplabel = gtk_label_new (_("Message"));
199         gtk_widget_show (tmplabel);
200         gtk_table_attach (GTK_TABLE (table), tmplabel, 0, 1, 2, 3,
201                          (GtkAttachOptions) GTK_FILL,
202                          (GtkAttachOptions) (0), 0, 0);
203         gtk_label_set_justify(GTK_LABEL(tmplabel), GTK_JUSTIFY_RIGHT);
204         gtk_misc_set_alignment(GTK_MISC(tmplabel), 1, 0.5);
205
206         entry_messageviewfont = gtk_entry_new ();
207         gtk_widget_show (entry_messageviewfont);
208         gtk_table_attach (GTK_TABLE (table), entry_messageviewfont, 1, 2, 2, 3,
209                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
210                          (GtkAttachOptions) (0), 0, 0);
211         gtk_entry_set_text(GTK_ENTRY(entry_messageviewfont), prefs_common.textfont);
212
213         tmpbutton = gtk_button_new_with_label (" ... ");
214         gtk_widget_show (tmpbutton);
215         gtk_table_attach (GTK_TABLE (table), tmpbutton, 2, 3, 2, 3,
216                           0, 0, 0, 0);
217         gtk_signal_connect (GTK_OBJECT (tmpbutton), "clicked",
218                             GTK_SIGNAL_FUNC (prefs_font_select), entry_messageviewfont);
219
220         tmplabel = gtk_label_new (_("Bold"));
221         gtk_widget_show (tmplabel);
222         gtk_table_attach (GTK_TABLE (table), tmplabel, 0, 1, 3, 4,
223                          (GtkAttachOptions) GTK_FILL,
224                          (GtkAttachOptions) (0), 0, 0);
225         gtk_label_set_justify(GTK_LABEL(tmplabel), GTK_JUSTIFY_RIGHT);
226         gtk_misc_set_alignment(GTK_MISC(tmplabel), 1, 0.5);
227
228         entry_boldfont = gtk_entry_new ();
229         gtk_widget_show (entry_boldfont);
230         gtk_table_attach (GTK_TABLE (table), entry_boldfont, 1, 2, 3, 4,
231                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
232                          (GtkAttachOptions) (0), 0, 0);
233         gtk_entry_set_text(GTK_ENTRY(entry_boldfont), prefs_common.boldfont);
234
235         tmpbutton = gtk_button_new_with_label (" ... ");
236         gtk_widget_show (tmpbutton);
237         gtk_table_attach (GTK_TABLE (table), tmpbutton, 2, 3, 3, 4,
238                           0, 0, 0, 0);
239         gtk_signal_connect (GTK_OBJECT(tmpbutton), "clicked",
240                                 GTK_SIGNAL_FUNC(prefs_font_select), entry_boldfont);
241
242         vbox = gtk_vbox_new(FALSE, VSPACING_NARROW);
243         gtk_widget_show(vbox);
244         gtk_table_attach (GTK_TABLE (table), vbox, 0, 4, 4, 5,
245                          (GtkAttachOptions) GTK_FILL,
246                          (GtkAttachOptions) (0), 0, 0);
247         
248         hint_label = gtk_label_new (_("You will need to restart for the "
249                                       "changes to take effect"));
250         gtk_widget_show (hint_label);
251         gtk_box_pack_start (GTK_BOX (vbox), 
252                             hint_label, FALSE, FALSE, 0);
253         gtk_misc_set_alignment(GTK_MISC(hint_label), 0.5, 0.5);
254
255         prefs_fonts->window                = GTK_WIDGET(window);
256         prefs_fonts->entry_folderviewfont  = entry_folderviewfont;
257         prefs_fonts->entry_summaryviewfont = entry_summaryviewfont;
258         prefs_fonts->entry_messageviewfont = entry_messageviewfont;
259         prefs_fonts->entry_boldfont        = entry_boldfont;
260
261         prefs_fonts->page.widget = table;
262 }
263
264 void prefs_fonts_save(PrefsPage *_page)
265 {
266         FontsPage *fonts = (FontsPage *) _page;
267
268         prefs_common.normalfont = gtk_editable_get_chars
269                 (GTK_EDITABLE(fonts->entry_folderviewfont), 0, -1);
270         prefs_common.smallfont  = gtk_editable_get_chars
271                 (GTK_EDITABLE(fonts->entry_summaryviewfont), 0, -1);
272         prefs_common.textfont   = gtk_editable_get_chars
273                 (GTK_EDITABLE(fonts->entry_messageviewfont), 0, -1);
274         prefs_common.boldfont   = gtk_editable_get_chars
275                 (GTK_EDITABLE(fonts->entry_boldfont), 0, -1);
276 }
277
278 static void prefs_fonts_destroy_widget(PrefsPage *_page)
279 {
280         /* FontsPage *fonts = (FontsPage *) _page; */
281
282 }
283
284 FontsPage *prefs_fonts;
285
286 void prefs_fonts_init(void)
287 {
288         FontsPage *page;
289
290         page = g_new0(FontsPage, 1);
291         page->page.path = _("Display/Fonts");
292         page->page.create_widget = prefs_fonts_create_widget;
293         page->page.destroy_widget = prefs_fonts_destroy_widget;
294         page->page.save_page = prefs_fonts_save;
295         page->page.weight = 60.0;
296         prefs_gtk_register_page((PrefsPage *) page);
297         prefs_fonts = page;
298 }
299
300 void prefs_fonts_done(void)
301 {
302         prefs_gtk_unregister_page((PrefsPage *) prefs_fonts);
303         g_free(prefs_fonts);
304 }