Fix a Gtk+ warning; tiny UI cleanup
[claws.git] / src / prefs_fonts.c
1
2
3 /*
4  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
5  * Copyright (C) 2003 Hiroyuki Yamamoto & the Sylpheed-Claws team
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #endif
25
26 #include "defs.h"
27
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include <glib.h>
32 #include <gtk/gtk.h>
33 #include <gdk/gdkkeysyms.h>
34
35 #include "intl.h"
36 #include "utils.h"
37 #include "prefs_common.h"
38 #include "prefs_gtk.h"
39
40 #include "gtk/gtkutils.h"
41 #include "gtk/prefswindow.h"
42
43 typedef struct _FontsPage
44 {
45         PrefsPage page;
46
47         GtkWidget *window;              /* do not modify */
48
49         GtkWidget *label_textfont;
50         GtkWidget *entry_textfont;
51         GtkWidget *entry_smallfont;
52         GtkWidget *entry_normalfont;
53         GtkWidget *entry_boldfont;
54         GtkWidget *button_textfont;
55 } FontsPage;
56
57 static GtkWidget *font_sel_win;
58 static guint font_sel_conn_id;
59
60 static void prefs_font_select   (GtkButton *button, GtkEntry *entry);
61
62 static void prefs_font_selection_key_pressed    (GtkWidget      *widget,
63                                                  GdkEventKey    *event,
64                                                  gpointer        data);
65 static void prefs_font_selection_ok             (GtkButton      *button, GtkEntry *entry);
66
67 static void prefs_font_select(GtkButton *button, GtkEntry *entry)
68 {
69         gchar *font_name;
70         
71         g_return_if_fail(entry != NULL);
72         
73         if (!font_sel_win) {
74                 font_sel_win = gtk_font_selection_dialog_new
75                         (_("Font selection"));
76                 gtk_window_position(GTK_WINDOW(font_sel_win),
77                                     GTK_WIN_POS_CENTER);
78                 gtk_signal_connect(GTK_OBJECT(font_sel_win), "delete_event",
79                                    GTK_SIGNAL_FUNC(gtk_widget_hide_on_delete),
80                                    NULL);
81                 gtk_signal_connect
82                         (GTK_OBJECT(font_sel_win), "key_press_event",
83                          GTK_SIGNAL_FUNC(prefs_font_selection_key_pressed),
84                          NULL);
85                 gtk_signal_connect_object
86                         (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(font_sel_win)->cancel_button),
87                          "clicked",
88                          GTK_SIGNAL_FUNC(gtk_widget_hide_on_delete),
89                          GTK_OBJECT(font_sel_win));
90         }
91
92         if(font_sel_conn_id) {
93                 gtk_signal_disconnect(GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(font_sel_win)->ok_button), font_sel_conn_id);
94         }
95         font_sel_conn_id = gtk_signal_connect
96                 (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(font_sel_win)->ok_button),
97                  "clicked",
98                  GTK_SIGNAL_FUNC(prefs_font_selection_ok),
99                  entry);
100         printf("%i\n", font_sel_conn_id);
101
102         font_name = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
103         gtk_font_selection_dialog_set_font_name(GTK_FONT_SELECTION_DIALOG(font_sel_win), font_name);
104         g_free(font_name);
105         manage_window_set_transient(GTK_WINDOW(font_sel_win));
106         gtk_window_set_modal(GTK_WINDOW(font_sel_win), TRUE);
107         gtk_widget_grab_focus
108                 (GTK_FONT_SELECTION_DIALOG(font_sel_win)->ok_button);
109         gtk_widget_show(font_sel_win);
110 }
111
112 static void prefs_font_selection_key_pressed(GtkWidget *widget,
113                                              GdkEventKey *event,
114                                              gpointer data)
115 {
116         if (event && event->keyval == GDK_Escape)
117                 gtk_widget_hide(font_sel_win);
118 }
119
120 static void prefs_font_selection_ok(GtkButton *button, GtkEntry *entry)
121 {
122         gchar *fontname;
123
124         fontname = gtk_font_selection_dialog_get_font_name
125                 (GTK_FONT_SELECTION_DIALOG(font_sel_win));
126
127         if (fontname) {
128                 gtk_entry_set_text(entry, fontname);
129
130                 g_free(fontname);
131         }
132
133         gtk_widget_hide(font_sel_win);
134 }
135
136 void prefs_fonts_create_widget(PrefsPage *_page, GtkWindow *window, 
137                                gpointer data)
138 {
139         FontsPage *prefs_fonts = (FontsPage *) _page;
140
141         GtkWidget *table;
142         GtkWidget *frame_font;
143         GtkWidget *table1;
144         GtkWidget *label_textfont;
145         GtkWidget *entry_textfont;
146         GtkWidget *entry_smallfont;
147         GtkWidget *entry_boldfont;
148         GtkWidget *entry_normalfont;
149         GtkWidget *button_textfont;
150         GtkWidget *tmplabel, *tmpbutton;
151         GtkWidget *vbox;
152         GtkWidget *hint_label;
153
154         table = gtk_table_new(8, 3, FALSE);
155         gtk_widget_show(table);
156         gtk_container_set_border_width(GTK_CONTAINER(table), 8);
157         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
158         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
159
160         label_textfont = gtk_label_new (_("Text"));
161         gtk_widget_show (label_textfont);
162         gtk_table_attach (GTK_TABLE (table), label_textfont, 0, 1, 0, 1,
163                          (GtkAttachOptions) (GTK_FILL),
164                          (GtkAttachOptions) (0), 0, 0);
165         gtk_label_set_justify(GTK_LABEL(label_textfont), GTK_JUSTIFY_RIGHT);
166         gtk_misc_set_alignment(GTK_MISC(label_textfont), 1, 0.5);
167
168         entry_textfont = gtk_entry_new ();
169         gtk_widget_show (entry_textfont);
170         gtk_table_attach (GTK_TABLE (table), entry_textfont, 1, 2, 0, 1,
171                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
172                          (GtkAttachOptions) (0), 0, 0);
173         gtk_entry_set_text(GTK_ENTRY(entry_textfont), prefs_common.textfont);
174
175         button_textfont = gtk_button_new_with_label (" ... ");
176
177         gtk_widget_show (button_textfont);
178         gtk_table_attach (GTK_TABLE (table), button_textfont, 2, 3, 0, 1,
179                           0, 0, 0, 0);
180         gtk_signal_connect (GTK_OBJECT (button_textfont), "clicked",
181                             GTK_SIGNAL_FUNC (prefs_font_select), entry_textfont);
182
183         tmplabel = gtk_label_new (_("Small"));
184         gtk_widget_show (tmplabel);
185         gtk_table_attach (GTK_TABLE (table), tmplabel, 0, 1, 1, 2,
186                          (GtkAttachOptions) GTK_FILL,
187                          (GtkAttachOptions) (0), 0, 0);
188         gtk_label_set_justify(GTK_LABEL(tmplabel), GTK_JUSTIFY_RIGHT);
189         gtk_misc_set_alignment(GTK_MISC(tmplabel), 1, 0.5);
190
191         entry_smallfont = gtk_entry_new ();
192         gtk_widget_show (entry_smallfont);
193         gtk_table_attach (GTK_TABLE (table), entry_smallfont, 1, 2, 1, 2,
194                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
195                          (GtkAttachOptions) (0), 0, 0);
196         gtk_entry_set_text(GTK_ENTRY(entry_smallfont), prefs_common.smallfont);
197
198         tmpbutton = gtk_button_new_with_label (" ... ");
199         gtk_widget_show (tmpbutton);
200         gtk_table_attach (GTK_TABLE (table), tmpbutton, 2, 3, 1, 2,
201                           0, 0, 0, 0);
202         gtk_signal_connect (GTK_OBJECT(tmpbutton), "clicked",
203                             GTK_SIGNAL_FUNC(prefs_font_select), entry_smallfont);
204
205         tmplabel = gtk_label_new (_("Normal"));
206         gtk_widget_show (tmplabel);
207         gtk_table_attach (GTK_TABLE (table), tmplabel, 0, 1, 2, 3,
208                          (GtkAttachOptions) GTK_FILL,
209                          (GtkAttachOptions) (0), 0, 0);
210         gtk_label_set_justify(GTK_LABEL(tmplabel), GTK_JUSTIFY_RIGHT);
211         gtk_misc_set_alignment(GTK_MISC(tmplabel), 1, 0.5);
212
213         entry_normalfont = gtk_entry_new ();
214         gtk_widget_show (entry_normalfont);
215         gtk_table_attach (GTK_TABLE (table), entry_normalfont, 1, 2, 2, 3,
216                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
217                          (GtkAttachOptions) (0), 0, 0);
218         gtk_entry_set_text(GTK_ENTRY(entry_normalfont), prefs_common.normalfont);
219
220         tmpbutton = gtk_button_new_with_label (" ... ");
221         gtk_widget_show (tmpbutton);
222         gtk_table_attach (GTK_TABLE (table), tmpbutton, 2, 3, 2, 3,
223                           0, 0, 0, 0);
224         gtk_signal_connect (GTK_OBJECT(tmpbutton), "clicked",
225                                 GTK_SIGNAL_FUNC(prefs_font_select), entry_normalfont);
226
227         tmplabel = gtk_label_new (_("Bold"));
228         gtk_widget_show (tmplabel);
229         gtk_table_attach (GTK_TABLE (table), tmplabel, 0, 1, 3, 4,
230                          (GtkAttachOptions) GTK_FILL,
231                          (GtkAttachOptions) (0), 0, 0);
232         gtk_label_set_justify(GTK_LABEL(tmplabel), GTK_JUSTIFY_RIGHT);
233         gtk_misc_set_alignment(GTK_MISC(tmplabel), 1, 0.5);
234
235         entry_boldfont = gtk_entry_new ();
236         gtk_widget_show (entry_boldfont);
237         gtk_table_attach (GTK_TABLE (table), entry_boldfont, 1, 2, 3, 4,
238                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
239                          (GtkAttachOptions) (0), 0, 0);
240         gtk_entry_set_text(GTK_ENTRY(entry_boldfont), prefs_common.boldfont);
241
242         tmpbutton = gtk_button_new_with_label (" ... ");
243         gtk_widget_show (tmpbutton);
244         gtk_table_attach (GTK_TABLE (table), tmpbutton, 2, 3, 3, 4,
245                           0, 0, 0, 0);
246         gtk_signal_connect (GTK_OBJECT(tmpbutton), "clicked",
247                                 GTK_SIGNAL_FUNC(prefs_font_select), entry_boldfont);
248
249         vbox = gtk_vbox_new(FALSE, VSPACING_NARROW);
250         gtk_widget_show(vbox);
251         gtk_table_attach (GTK_TABLE (table), vbox, 0, 4, 4, 5,
252                          (GtkAttachOptions) (GTK_FILL),
253                          (GtkAttachOptions) (0), 0, 0);
254         
255         hint_label = gtk_label_new (_("You will need to restart for the "
256                                       "changes to take effect"));
257         gtk_widget_show (hint_label);
258         gtk_box_pack_start (GTK_BOX (vbox), 
259                             hint_label, FALSE, FALSE, 0);
260         gtk_misc_set_alignment(GTK_MISC(hint_label), 0.5, 0.5);
261
262         prefs_fonts->window             = GTK_WIDGET(window);
263         prefs_fonts->entry_textfont     = entry_textfont;
264         prefs_fonts->entry_smallfont    = entry_smallfont;
265         prefs_fonts->entry_normalfont   = entry_normalfont;
266         prefs_fonts->entry_boldfont     = entry_boldfont;
267         prefs_fonts->button_textfont    = button_textfont;
268
269         prefs_fonts->page.widget = table;
270 }
271
272 void prefs_fonts_save(PrefsPage *_page)
273 {
274         FontsPage *fonts = (FontsPage *) _page;
275
276         prefs_common.textfont   = gtk_editable_get_chars
277                 (GTK_EDITABLE(fonts->entry_textfont), 0, -1);
278         prefs_common.smallfont  = gtk_editable_get_chars
279                 (GTK_EDITABLE(fonts->entry_smallfont), 0, -1);
280         prefs_common.boldfont   = gtk_editable_get_chars
281                 (GTK_EDITABLE(fonts->entry_boldfont), 0, -1);
282         prefs_common.normalfont = gtk_editable_get_chars
283                 (GTK_EDITABLE(fonts->entry_normalfont), 0, -1);
284 }
285 static void prefs_fonts_destroy_widget(PrefsPage *_page)
286 {
287         /* FontsPage *fonts = (FontsPage *) _page; */
288
289 }
290
291 FontsPage *prefs_fonts;
292
293 void prefs_fonts_init(void)
294 {
295         FontsPage *page;
296
297         page = g_new0(FontsPage, 1);
298         page->page.path = _("Display/Fonts");
299         page->page.create_widget = prefs_fonts_create_widget;
300         page->page.destroy_widget = prefs_fonts_destroy_widget;
301         page->page.save_page = prefs_fonts_save;
302         page->page.weight = 60.0;
303         prefs_gtk_register_page((PrefsPage *) page);
304         prefs_fonts = page;
305 }
306
307 void prefs_fonts_done(void)
308 {
309         prefs_gtk_unregister_page((PrefsPage *) prefs_fonts);
310         g_free(prefs_fonts);
311 }