03401fa743d4f8e24cf60c9db95b8362f7d4cdc6
[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_ref(table2);
73         gtk_widget_show(table2);
74         gtk_container_set_border_width(GTK_CONTAINER(table2), 8);
75         gtk_table_set_row_spacings(GTK_TABLE(table2), 4);
76         gtk_table_set_col_spacings(GTK_TABLE(table2), 8);
77
78         label14 =
79             gtk_label_new(_("Automatically display attached images"));
80         gtk_widget_ref(label14);
81         gtk_widget_show(label14);
82         gtk_table_attach(GTK_TABLE(table2), label14, 0, 1, 0, 1,
83                          (GtkAttachOptions) (GTK_FILL),
84                          (GtkAttachOptions) (0), 0, 0);
85         gtk_misc_set_alignment(GTK_MISC(label14), 0, 0.5);
86
87         label15 = gtk_label_new(_("Resize attached images"));
88         gtk_widget_ref(label15);
89         gtk_widget_show(label15);
90         gtk_table_attach(GTK_TABLE(table2), label15, 0, 1, 1, 2,
91                          (GtkAttachOptions) (GTK_FILL),
92                          (GtkAttachOptions) (0), 0, 0);
93         gtk_misc_set_alignment(GTK_MISC(label15), 0, 0.5);
94
95         autoload = gtk_check_button_new_with_label("");
96         gtk_widget_ref(autoload);
97         gtk_widget_show(autoload);
98         gtk_table_attach(GTK_TABLE(table2), autoload, 1, 2, 0, 1,
99                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
100                          (GtkAttachOptions) (0), 0, 0);
101
102         resize = gtk_check_button_new_with_label("");
103         gtk_widget_ref(resize);
104         gtk_widget_show(resize);
105         gtk_table_attach(GTK_TABLE(table2), resize, 1, 2, 1, 2,
106                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
107                          (GtkAttachOptions) (0), 0, 0);
108         /* --------------------------------------------------------- */
109         gtk_widget_set_sensitive(GTK_WIDGET(autoload), FALSE);
110
111         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(resize), imageviewerprefs.resize_img);
112
113         page->autoload = autoload;
114         page->resize = resize;
115
116         page->page.widget = table2;
117 }
118
119 static void imageviewer_destroy_widget_func(PrefsPage *_page)
120 {
121 }
122
123 static void imageviewer_save_func(PrefsPage * _page)
124 {
125         struct ImageViewerPage *page = (struct ImageViewerPage *) _page;
126         PrefFile *pfile;
127         gchar *rcpath;
128
129         imageviewerprefs.display_img =
130             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON
131                                          (page->autoload));
132         imageviewerprefs.resize_img =
133             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->resize));
134
135         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
136         pfile = prefs_write_open(rcpath);
137         g_free(rcpath);
138         if (!pfile || (prefs_set_block_label(pfile, PREFS_BLOCK_NAME) < 0))
139                 return;
140
141         if (prefs_write_param(param, pfile->fp) < 0) {
142                 g_warning("failed to write ImageViewer configuration to file\n");
143                 prefs_file_close_revert(pfile);
144                 return;
145         }
146         fprintf(pfile->fp, "\n");
147
148         prefs_file_close(pfile);
149 }
150
151 static struct ImageViewerPage imageviewer_page;
152
153 void image_viewer_prefs_init()
154 {
155         prefs_set_default(param);
156         prefs_read_config(param, PREFS_BLOCK_NAME, COMMON_RC);
157
158         imageviewer_page.page.path = "Message View/Image Viewer";
159         imageviewer_page.page.create_widget = imageviewer_create_widget_func;
160         imageviewer_page.page.destroy_widget = imageviewer_destroy_widget_func;
161         imageviewer_page.page.save_page = imageviewer_save_func;
162
163         prefs_gtk_register_page((PrefsPage *) &imageviewer_page);
164 }
165
166 void image_viewer_prefs_done()
167 {
168         prefs_gtk_unregister_page((PrefsPage *) &imageviewer_page);
169 }