fa534c1338f4723a645ed24f78b242180add0be8
[claws.git] / src / plugins / image_viewer / viewerprefs.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto and 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 <glib.h>
27 #include <gtk/gtk.h>
28
29 #include "intl.h"
30 #include "common/utils.h"
31 #include "prefs.h"
32 #include "prefs_gtk.h"
33 #include "prefswindow.h"
34
35 #include "viewerprefs.h"
36
37 #define PREFS_BLOCK_NAME "ImageViewer"
38
39 struct ImageViewerPage
40 {
41         PrefsPage page;
42         
43         GtkWidget *autoload;
44         GtkWidget *resize;
45 };
46
47 ImageViewerPrefs imageviewerprefs;
48
49 static PrefParam param[] = {
50         {"display_img", "TRUE", &imageviewerprefs.display_img, P_BOOL,
51          NULL, NULL, NULL},
52         {"resize_image", "TRUE", &imageviewerprefs.resize_img, P_BOOL,
53          NULL, NULL, NULL},
54
55         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
56 };
57
58 static void imageviewer_create_widget_func(PrefsPage * _page,
59                                            GtkWindow * window,
60                                            gpointer data)
61 {
62         struct ImageViewerPage *page = (struct ImageViewerPage *) _page;
63
64         /* ------------------ code made by glade -------------------- */
65         GtkWidget *table2;
66         GtkWidget *label14;
67         GtkWidget *label15;
68         GtkWidget *autoload;
69         GtkWidget *resize;
70
71         table2 = gtk_table_new(2, 2, FALSE);
72         gtk_widget_show(table2);
73         gtk_container_set_border_width(GTK_CONTAINER(table2), 8);
74         gtk_table_set_row_spacings(GTK_TABLE(table2), 4);
75         gtk_table_set_col_spacings(GTK_TABLE(table2), 8);
76
77         label14 =
78             gtk_label_new(_("Automatically display attached images"));
79         gtk_widget_show(label14);
80         gtk_table_attach(GTK_TABLE(table2), label14, 0, 1, 0, 1,
81                          (GtkAttachOptions) (GTK_FILL),
82                          (GtkAttachOptions) (0), 0, 0);
83         gtk_misc_set_alignment(GTK_MISC(label14), 0, 0.5);
84
85         label15 = gtk_label_new(_("Resize attached images by default\n(Clicking image toggles scaling)"));
86         gtk_widget_show(label15);
87         gtk_table_attach(GTK_TABLE(table2), label15, 0, 1, 1, 2,
88                          (GtkAttachOptions) (GTK_FILL),
89                          (GtkAttachOptions) (0), 0, 0);
90         gtk_misc_set_alignment(GTK_MISC(label15), 0, 0.5);
91
92         autoload = gtk_check_button_new_with_label("");
93         gtk_widget_show(autoload);
94         gtk_table_attach(GTK_TABLE(table2), autoload, 1, 2, 0, 1,
95                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
96                          (GtkAttachOptions) (0), 0, 0);
97
98         resize = gtk_check_button_new_with_label("");
99         gtk_widget_show(resize);
100         gtk_table_attach(GTK_TABLE(table2), resize, 1, 2, 1, 2,
101                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
102                          (GtkAttachOptions) (0), 0, 0);
103         /* --------------------------------------------------------- */
104
105         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(resize), imageviewerprefs.resize_img);
106         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(autoload), imageviewerprefs.display_img);
107
108         page->autoload = autoload;
109         page->resize = resize;
110
111         page->page.widget = table2;
112 }
113
114 static void imageviewer_destroy_widget_func(PrefsPage *_page)
115 {
116 }
117
118 static void imageviewer_save_func(PrefsPage * _page)
119 {
120         struct ImageViewerPage *page = (struct ImageViewerPage *) _page;
121         PrefFile *pfile;
122         gchar *rcpath;
123
124         imageviewerprefs.display_img =
125             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON
126                                          (page->autoload));
127         imageviewerprefs.resize_img =
128             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->resize));
129
130         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
131         pfile = prefs_write_open(rcpath);
132         g_free(rcpath);
133         if (!pfile || (prefs_set_block_label(pfile, PREFS_BLOCK_NAME) < 0))
134                 return;
135
136         if (prefs_write_param(param, pfile->fp) < 0) {
137                 g_warning("failed to write ImageViewer configuration to file\n");
138                 prefs_file_close_revert(pfile);
139                 return;
140         }
141         fprintf(pfile->fp, "\n");
142
143         prefs_file_close(pfile);
144 }
145
146 static struct ImageViewerPage imageviewer_page;
147
148 void image_viewer_prefs_init(void)
149 {
150         prefs_set_default(param);
151         prefs_read_config(param, PREFS_BLOCK_NAME, COMMON_RC);
152
153         imageviewer_page.page.path = _("Message View/Image Viewer");
154         imageviewer_page.page.create_widget = imageviewer_create_widget_func;
155         imageviewer_page.page.destroy_widget = imageviewer_destroy_widget_func;
156         imageviewer_page.page.save_page = imageviewer_save_func;
157
158         prefs_gtk_register_page((PrefsPage *) &imageviewer_page);
159 }
160
161 void image_viewer_prefs_done(void)
162 {
163         prefs_gtk_unregister_page((PrefsPage *) &imageviewer_page);
164 }