Fix CID 1596595: Resource leak
[claws.git] / src / prefs_fonts.c
blob24dffd1146adb33d2ee531e5251b7cd5fce46ea7
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 2003-2019 the Claws Mail Team and Hiroyuki Yamamoto
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.
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.
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/>.
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #include "claws-features.h"
22 #endif
24 #include "defs.h"
26 #include <stdio.h>
27 #include <stdlib.h>
29 #include <glib.h>
30 #include <glib/gi18n.h>
31 #include <gtk/gtk.h>
32 #include <gdk/gdkkeysyms.h>
34 #include "prefs_common.h"
35 #include "prefs_gtk.h"
37 #include "gtk/prefswindow.h"
39 typedef struct _FontsPage
41 PrefsPage page;
43 GtkWidget *window; /* do not modify */
45 GtkWidget *entry_folderview_smallfont;
46 GtkWidget *entry_folderview_normalfont;
47 GtkWidget *entry_folderview_boldfont;
48 GtkWidget *entry_messageviewfont;
49 GtkWidget *derive_from_normalfont_checkbutton;
50 GtkWidget *print_checkbutton;
51 GtkWidget *entry_messageprintfont;
52 } FontsPage;
54 static void prefs_fonts_create_widget(PrefsPage *_page, GtkWindow *window,
55 gpointer data)
57 FontsPage *prefs_fonts = (FontsPage *) _page;
59 GtkWidget *table;
60 GtkWidget *entry_folderview_smallfont;
61 GtkWidget *entry_folderview_normalfont;
62 GtkWidget *entry_folderview_boldfont;
63 GtkWidget *entry_messageviewfont;
64 GtkWidget *tmplabel;
65 GtkWidget *entry_messageprintfont;
66 GtkWidget *print_checkbutton;
67 GtkWidget *derive_from_normalfont_checkbutton;
68 GtkWidget *vbox;
69 gint row = 0;
71 table = gtk_grid_new();
72 gtk_widget_show(table);
73 gtk_container_set_border_width(GTK_CONTAINER(table), VBOX_BORDER);
74 gtk_grid_set_row_spacing(GTK_GRID(table), 4);
75 gtk_grid_set_column_spacing(GTK_GRID(table), 8);
77 /* normal font label */
78 tmplabel = gtk_label_new (_("Folder and Message Lists"));
79 gtk_widget_show (tmplabel);
80 gtk_label_set_justify(GTK_LABEL(tmplabel), GTK_JUSTIFY_RIGHT);
81 gtk_label_set_xalign(GTK_LABEL(tmplabel), 1.0);
82 gtk_grid_attach(GTK_GRID(table), tmplabel, 0, row, 1, 1);
84 /* normal font button */
85 entry_folderview_normalfont = gtk_font_button_new_with_font (prefs_common.normalfont);
86 g_object_set(G_OBJECT(entry_folderview_normalfont),
87 "use-font", TRUE,
88 NULL);
89 gtk_widget_show (entry_folderview_normalfont);
90 gtk_grid_attach(GTK_GRID(table), entry_folderview_normalfont, 1, row, 1, 1);
91 gtk_widget_set_hexpand(entry_folderview_normalfont, TRUE);
92 gtk_widget_set_halign(entry_folderview_normalfont, GTK_ALIGN_FILL);
93 row++;
95 /* message font label */
96 tmplabel = gtk_label_new (_("Message"));
97 gtk_widget_show (tmplabel);
98 gtk_label_set_justify(GTK_LABEL(tmplabel), GTK_JUSTIFY_RIGHT);
99 gtk_label_set_xalign(GTK_LABEL(tmplabel), 1.0);
100 gtk_grid_attach(GTK_GRID(table), tmplabel, 0, row, 1, 1);
102 /* message font button */
103 entry_messageviewfont = gtk_font_button_new_with_font (prefs_common.textfont);
104 g_object_set(G_OBJECT(entry_messageviewfont),
105 "use-font", TRUE,
106 NULL);
107 gtk_widget_show (entry_messageviewfont);
108 gtk_grid_attach(GTK_GRID(table), entry_messageviewfont, 1, row, 1, 1);
109 gtk_widget_set_hexpand(entry_messageviewfont, TRUE);
110 gtk_widget_set_halign(entry_messageviewfont, GTK_ALIGN_FILL);
111 row++;
113 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, VSPACING_NARROW);
114 gtk_widget_show(vbox);
115 gtk_grid_attach(GTK_GRID(table), vbox, 0, row, 1, 1);
116 row++;
118 /* derive from normal font check button */
119 derive_from_normalfont_checkbutton = gtk_check_button_new_with_label(_("Derive small and bold fonts from Folder and Message Lists font"));
120 gtk_widget_show(derive_from_normalfont_checkbutton);
121 gtk_grid_attach(GTK_GRID(table), derive_from_normalfont_checkbutton, 0, row, 2, 1);
122 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(derive_from_normalfont_checkbutton),
123 prefs_common.derive_from_normal_font);
124 row++;
126 /* small font label */
127 tmplabel = gtk_label_new (_("Small"));
128 gtk_widget_show (tmplabel);
129 gtk_grid_attach(GTK_GRID(table), tmplabel, 0, row, 1, 1);
130 gtk_label_set_justify(GTK_LABEL(tmplabel), GTK_JUSTIFY_RIGHT);
131 gtk_label_set_xalign(GTK_LABEL(tmplabel), 1.0);
132 SET_TOGGLE_SENSITIVITY_REVERSE (derive_from_normalfont_checkbutton, tmplabel);
134 /* small font button */
135 entry_folderview_smallfont = gtk_font_button_new_with_font (prefs_common.smallfont);
136 g_object_set(G_OBJECT(entry_folderview_smallfont),
137 "use-font", TRUE,
138 NULL);
139 gtk_widget_show (entry_folderview_smallfont);
140 gtk_grid_attach(GTK_GRID(table), entry_folderview_smallfont, 1, row, 1, 1);
141 gtk_widget_set_hexpand(entry_folderview_smallfont, TRUE);
142 gtk_widget_set_halign(entry_folderview_smallfont, GTK_ALIGN_FILL);
143 SET_TOGGLE_SENSITIVITY_REVERSE (derive_from_normalfont_checkbutton, entry_folderview_smallfont);
144 row++;
146 /* bold font label */
147 tmplabel = gtk_label_new (_("Bold"));
148 gtk_widget_show (tmplabel);
149 gtk_label_set_justify(GTK_LABEL(tmplabel), GTK_JUSTIFY_RIGHT);
150 gtk_label_set_xalign(GTK_LABEL(tmplabel), 1.0);
151 gtk_grid_attach(GTK_GRID(table), tmplabel, 0, row, 1, 1);
152 SET_TOGGLE_SENSITIVITY_REVERSE (derive_from_normalfont_checkbutton, tmplabel);
154 /* bold font button */
155 entry_folderview_boldfont = gtk_font_button_new_with_font (prefs_common.boldfont);
156 g_object_set(G_OBJECT(entry_folderview_boldfont),
157 "use-font", TRUE,
158 NULL);
159 gtk_widget_show (entry_folderview_boldfont);
160 gtk_grid_attach(GTK_GRID(table), entry_folderview_boldfont, 1, row, 1, 1);
161 gtk_widget_set_hexpand(entry_folderview_boldfont, TRUE);
162 gtk_widget_set_halign(entry_folderview_boldfont, GTK_ALIGN_FILL);
163 SET_TOGGLE_SENSITIVITY_REVERSE (derive_from_normalfont_checkbutton, entry_folderview_boldfont);
164 row++;
166 /* print check button */
167 print_checkbutton = gtk_check_button_new_with_label(_("Use different font for printing"));
168 gtk_widget_show(print_checkbutton);
169 gtk_grid_attach(GTK_GRID(table), print_checkbutton, 0, row, 2, 1);
170 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(print_checkbutton),
171 prefs_common.use_different_print_font);
172 row++;
174 /* print font label */
175 tmplabel = gtk_label_new (_("Message Printing"));
176 gtk_widget_show (tmplabel);
177 gtk_label_set_justify(GTK_LABEL(tmplabel), GTK_JUSTIFY_RIGHT);
178 gtk_label_set_xalign(GTK_LABEL(tmplabel), 1.0);
179 gtk_grid_attach(GTK_GRID(table), tmplabel, 0, row, 1, 1);
180 SET_TOGGLE_SENSITIVITY (print_checkbutton, tmplabel);
182 /* print font button */
183 entry_messageprintfont = gtk_font_button_new_with_font (prefs_common.printfont);
184 g_object_set(G_OBJECT(entry_messageprintfont),
185 "use-font", TRUE,
186 NULL);
187 gtk_widget_show (entry_messageprintfont);
188 gtk_grid_attach(GTK_GRID(table), entry_messageprintfont, 1, row, 1, 1);
189 gtk_widget_set_hexpand(entry_messageprintfont, TRUE);
190 gtk_widget_set_halign(entry_messageprintfont, GTK_ALIGN_FILL);
191 SET_TOGGLE_SENSITIVITY (print_checkbutton, entry_messageprintfont);
192 row++;
194 prefs_fonts->window = GTK_WIDGET(window);
195 prefs_fonts->entry_folderview_smallfont = entry_folderview_smallfont;
196 prefs_fonts->entry_folderview_normalfont = entry_folderview_normalfont;
197 prefs_fonts->entry_folderview_boldfont = entry_folderview_boldfont;
198 prefs_fonts->entry_messageviewfont = entry_messageviewfont;
199 prefs_fonts->derive_from_normalfont_checkbutton = derive_from_normalfont_checkbutton;
200 prefs_fonts->entry_messageprintfont = entry_messageprintfont;
201 prefs_fonts->print_checkbutton = print_checkbutton;
203 prefs_fonts->page.widget = table;
206 static void prefs_fonts_save(PrefsPage *_page)
208 FontsPage *fonts = (FontsPage *) _page;
210 g_free(prefs_common.boldfont);
211 prefs_common.boldfont = g_strdup(gtk_font_chooser_get_font
212 (GTK_FONT_CHOOSER(fonts->entry_folderview_boldfont)));
214 g_free(prefs_common.normalfont);
215 prefs_common.normalfont = g_strdup(gtk_font_chooser_get_font
216 (GTK_FONT_CHOOSER(fonts->entry_folderview_normalfont)));
218 g_free(prefs_common.smallfont);
219 prefs_common.smallfont = g_strdup(gtk_font_chooser_get_font
220 (GTK_FONT_CHOOSER(fonts->entry_folderview_smallfont)));
222 g_free(prefs_common.textfont);
223 prefs_common.textfont = g_strdup(gtk_font_chooser_get_font
224 (GTK_FONT_CHOOSER(fonts->entry_messageviewfont)));
226 prefs_common.derive_from_normal_font = gtk_toggle_button_get_active
227 (GTK_TOGGLE_BUTTON(fonts->derive_from_normalfont_checkbutton));
229 g_free(prefs_common.printfont);
230 prefs_common.printfont = g_strdup(gtk_font_chooser_get_font
231 (GTK_FONT_CHOOSER(fonts->entry_messageprintfont)));
232 prefs_common.use_different_print_font = gtk_toggle_button_get_active
233 (GTK_TOGGLE_BUTTON(fonts->print_checkbutton));
235 main_window_reflect_prefs_all();
238 static void prefs_fonts_destroy_widget(PrefsPage *_page)
240 /* FontsPage *fonts = (FontsPage *) _page; */
244 FontsPage *prefs_fonts;
246 void prefs_fonts_init(void)
248 FontsPage *page;
249 static gchar *path[3];
251 path[0] = _("Display");
252 path[1] = _("Fonts");
253 path[2] = NULL;
255 page = g_new0(FontsPage, 1);
256 page->page.path = path;
257 page->page.create_widget = prefs_fonts_create_widget;
258 page->page.destroy_widget = prefs_fonts_destroy_widget;
259 page->page.save_page = prefs_fonts_save;
260 page->page.weight = 135.0;
261 prefs_gtk_register_page((PrefsPage *) page);
262 prefs_fonts = page;
265 void prefs_fonts_done(void)
267 prefs_gtk_unregister_page((PrefsPage *) prefs_fonts);
268 g_free(prefs_fonts);