git: Line endings of README.txt
[projects/chimara/chimara.git] / player / preferences.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  * callbacks.c
4  * Copyright (C) Philip en Marijn 2008 <>
5  * 
6  * preferences.c is free software copyrighted by Philip en Marijn.
7  * 
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name ``Philip en Marijn'' nor the name of any other
17  *    contributor may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  * 
20  * preferences.c IS PROVIDED BY Philip en Marijn ``AS IS'' AND ANY EXPRESS
21  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Philip en Marijn OR ANY OTHER CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <stdlib.h>
34 #include <glib.h>
35 #include <glib/gi18n.h>
36 #include <gtk/gtk.h>
37 #include <libchimara/chimara-glk.h>
38 #include <libchimara/chimara-if.h>
39 #include <config.h>
40 #include "error.h"
41
42 GObject *load_object(const gchar *name);
43 static GtkTextTag *current_tag;
44 static GtkListStore *preferred_list;
45
46 static void style_tree_select_callback(GtkTreeSelection *selection, ChimaraGlk *glk);
47
48 /* Internal functions to convert from human-readable names in the config file
49 to enums and back. Later: replace with plugin functions. */
50 static ChimaraIFFormat
51 parse_format(const char *format)
52 {
53         if(strcmp(format, "z5") == 0)
54                 return CHIMARA_IF_FORMAT_Z5;
55         if(strcmp(format, "z6") == 0)
56                 return CHIMARA_IF_FORMAT_Z6;
57         if(strcmp(format, "z8") == 0)
58                 return CHIMARA_IF_FORMAT_Z8;
59         if(strcmp(format, "zblorb") == 0)
60                 return CHIMARA_IF_FORMAT_Z_BLORB;
61         if(strcmp(format, "glulx") == 0)
62                 return CHIMARA_IF_FORMAT_GLULX;
63         if(strcmp(format, "gblorb") == 0)
64                 return CHIMARA_IF_FORMAT_GLULX_BLORB;
65         return CHIMARA_IF_FORMAT_NONE;
66 }
67
68 static const char *format_strings[CHIMARA_IF_NUM_FORMATS] = {
69         "z5", "z6", "z8", "zblorb", "glulx", "gblorb"
70 };
71
72 static const char *format_to_string(ChimaraIFFormat format)
73 {
74         if(format >= 0 && format < CHIMARA_IF_NUM_FORMATS)
75                 return format_strings[format];
76         return "unknown";
77 }
78
79 static const char *format_display_strings[CHIMARA_IF_NUM_FORMATS] = {
80         N_("Z-machine version 5"),
81         N_("Z-machine version 6"),
82         N_("Z-machine version 8"),
83         N_("Z-machine Blorb file"),
84         N_("Glulx"),
85         N_("Glulx Blorb file")
86 };
87
88 static const char *
89 format_to_display_string(ChimaraIFFormat format)
90 {
91         if(format >= 0 && format < CHIMARA_IF_NUM_FORMATS)
92                 return gettext(format_display_strings[format]);
93         return _("Unknown");
94 }
95
96 static ChimaraIFInterpreter
97 parse_interpreter(const char *interp)
98 {
99         if(strcmp(interp, "frotz") == 0)
100                 return CHIMARA_IF_INTERPRETER_FROTZ;
101         if(strcmp(interp, "nitfol") == 0)
102                 return CHIMARA_IF_INTERPRETER_NITFOL;
103         if(strcmp(interp, "glulxe") == 0)
104                 return CHIMARA_IF_INTERPRETER_GLULXE;
105         if(strcmp(interp, "git") == 0)
106                 return CHIMARA_IF_INTERPRETER_GIT;
107         if(strcmp(interp, "bocfel") == 0)
108                 return CHIMARA_IF_INTERPRETER_BOCFEL;
109         return CHIMARA_IF_INTERPRETER_NONE;
110 }
111
112 static const char *interpreter_strings[CHIMARA_IF_NUM_INTERPRETERS] = {
113         "frotz", "nitfol", "glulxe", "git", "bocfel"
114 };
115
116 static const char *
117 interpreter_to_string(ChimaraIFInterpreter interp)
118 {
119         if(interp >= 0 && interp < CHIMARA_IF_NUM_INTERPRETERS)
120                 return interpreter_strings[interp];
121         return "unknown";
122 }
123
124 static const char *interpreter_display_strings[CHIMARA_IF_NUM_INTERPRETERS] = {
125         N_("Frotz"),
126         N_("Nitfol"),
127         N_("Glulxe"),
128         N_("Git"),
129         N_("Bocfel")
130 };
131
132 static const char *
133 interpreter_to_display_string(ChimaraIFInterpreter interp)
134 {
135         if(interp >= 0 && interp < CHIMARA_IF_NUM_INTERPRETERS)
136                 return gettext(interpreter_display_strings[interp]);
137         return _("Unknown");
138 }
139
140 /* Create the preferences dialog. */
141 void
142 preferences_create(ChimaraGlk *glk)
143 {
144         /* Initialize the tree of style names */
145         GtkTreeStore *style_list = GTK_TREE_STORE( load_object("style-list") );
146         GtkTreeIter buffer, grid, buffer_child, grid_child;
147
148         gtk_tree_store_append(style_list, &buffer, NULL);
149         gtk_tree_store_append(style_list, &grid, NULL);
150         gtk_tree_store_set(style_list, &buffer, 0, "Text buffer", -1);
151         gtk_tree_store_set(style_list, &grid, 0, "Text grid", -1);
152
153         int i;
154         unsigned int num_tags;
155         const gchar **tag_names = chimara_glk_get_tag_names(glk, &num_tags);
156         for(i=0; i<num_tags; i++) {
157                 gtk_tree_store_append(style_list, &buffer_child, &buffer);
158                 gtk_tree_store_append(style_list, &grid_child, &grid);
159                 gtk_tree_store_set(style_list, &buffer_child, 0, tag_names[i], -1);
160                 gtk_tree_store_set(style_list, &grid_child, 0, tag_names[i], -1);
161         }
162
163         /* Set selection mode to single select */
164         GtkTreeView *view = GTK_TREE_VIEW( load_object("style-treeview") );
165         GtkTreeSelection *selection = gtk_tree_view_get_selection(view);
166         gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
167         g_signal_connect(selection, "changed", G_CALLBACK(style_tree_select_callback), glk);
168
169         /* Bind the preferences to the entries in the preferences file */
170         extern GSettings *prefs_settings;
171         GObject *flep = G_OBJECT( load_object("flep") );
172         g_settings_bind(prefs_settings, "flep", flep, "active", G_SETTINGS_BIND_DEFAULT);
173         GtkFileChooser *blorb_chooser = GTK_FILE_CHOOSER( load_object("blorb_file_chooser") );
174         GtkFileChooser *css_chooser = GTK_FILE_CHOOSER( load_object("css-filechooser") );
175         char *filename;
176         g_settings_get(prefs_settings, "resource-path", "ms", &filename);
177         if(filename) {
178                 gtk_file_chooser_set_filename(blorb_chooser, filename);
179                 g_free(filename);
180         }
181         g_settings_get(prefs_settings, "css-file", "ms", &filename);
182         if(filename) {
183                 if(!chimara_glk_set_css_from_file(glk, filename, NULL)) {
184                         /* If the setting didn't point to a CSS file, fail silently and
185                          null the setting */
186                         g_settings_set(prefs_settings, "css-file", "ms", NULL);
187                 } else {
188                         gtk_file_chooser_set_filename(css_chooser, filename);
189                 }
190                 g_free(filename);
191         }
192
193         /* Populate the list of available interpreters */
194         GtkListStore *interp_list = GTK_LIST_STORE( load_object("available_interpreters") );
195         unsigned int count;
196         GtkTreeIter tree_iter;
197         for(count = 0; count < CHIMARA_IF_NUM_INTERPRETERS; count++) {
198                 gtk_list_store_append(interp_list, &tree_iter);
199                 gtk_list_store_set(interp_list, &tree_iter,
200                         0, interpreter_to_display_string(count),
201                         -1);
202         }
203
204         /* Get the list of preferred interpreters from the preferences */
205         GVariantIter *iter;
206         char *format, *plugin;
207         g_settings_get(prefs_settings, "preferred-interpreters", "a{ss}", &iter);
208         while(g_variant_iter_loop(iter, "{ss}", &format, &plugin)) {
209                 ChimaraIFFormat format_num = parse_format(format);
210                 if(format_num == CHIMARA_IF_FORMAT_NONE)
211                         continue;
212                 ChimaraIFInterpreter interp_num = parse_interpreter(plugin);
213                 if(interp_num == CHIMARA_IF_INTERPRETER_NONE)
214                         continue;
215                 chimara_if_set_preferred_interpreter(CHIMARA_IF(glk), format_num, interp_num);
216         }
217         g_variant_iter_free(iter);
218
219         /* Display it all in the list */
220         preferred_list = GTK_LIST_STORE( load_object("interpreters") );
221         for(count = 0; count < CHIMARA_IF_NUM_FORMATS; count++) {
222                 gtk_list_store_append(preferred_list, &tree_iter);
223                 gtk_list_store_set(preferred_list, &tree_iter,
224                         0, format_to_display_string(count),
225                         1, interpreter_to_display_string(chimara_if_get_preferred_interpreter(CHIMARA_IF(glk), count)),
226                         -1);
227         }
228 }
229
230 static void
231 style_tree_select_callback(GtkTreeSelection *selection, ChimaraGlk *glk)
232 {
233         GtkTreeIter child, parent;
234         gchar *child_name, *parent_name;
235         GtkTreeModel *model;
236
237         if( !gtk_tree_selection_get_selected(selection, &model, &child) )
238                 return;
239
240         gtk_tree_model_get(model, &child, 0, &child_name, -1);
241                 
242         if( !gtk_tree_model_iter_parent(model, &parent, &child) )
243                 return;
244
245         gtk_tree_model_get(model, &parent, 0, &parent_name, -1);
246         if( !strcmp(parent_name, "Text buffer") ) 
247                 current_tag = chimara_glk_get_tag(glk, CHIMARA_GLK_TEXT_BUFFER, child_name);
248         else
249                 current_tag = chimara_glk_get_tag(glk, CHIMARA_GLK_TEXT_GRID, child_name);
250 }
251
252 void
253 on_toggle_left(GtkToggleButton *button, ChimaraGlk *glk) {
254         /* No nothing if the button is deactivated */
255         if( !gtk_toggle_button_get_active(button) )
256                 return;
257         g_object_set(current_tag, "justification", GTK_JUSTIFY_LEFT, "justification-set", TRUE, NULL);
258         chimara_glk_update_style(glk);
259 }
260
261 void
262 on_toggle_center(GtkToggleButton *button, ChimaraGlk *glk) {
263         if( !gtk_toggle_button_get_active(button) )
264                 return;
265         g_object_set(current_tag, "justification", GTK_JUSTIFY_CENTER, "justification-set", TRUE, NULL);
266         chimara_glk_update_style(glk);
267 }
268
269 void
270 on_toggle_right(GtkToggleButton *button, ChimaraGlk *glk) {
271         if( !gtk_toggle_button_get_active(button) )
272                 return;
273         g_object_set(current_tag, "justification", GTK_JUSTIFY_RIGHT, "justification-set", TRUE, NULL);
274         chimara_glk_update_style(glk);
275 }
276
277 void
278 on_toggle_justify(GtkToggleButton *button, ChimaraGlk *glk) {
279         if( !gtk_toggle_button_get_active(button) )
280                 return;
281         g_object_set(current_tag, "justification", GTK_JUSTIFY_FILL, "justification-set", TRUE, NULL);
282         chimara_glk_update_style(glk);
283 }
284
285 void
286 on_toggle_bold(GtkToggleButton *button, ChimaraGlk *glk) {
287         if( gtk_toggle_button_get_active(button) )
288                 g_object_set(current_tag, "weight", PANGO_WEIGHT_BOLD, "weight-set", TRUE, NULL);
289         else
290                 g_object_set(current_tag, "weight", PANGO_WEIGHT_NORMAL, "weight-set", TRUE, NULL);
291
292         chimara_glk_update_style(glk);
293 }
294
295 void
296 on_toggle_italic(GtkToggleButton *button, ChimaraGlk *glk) {
297         if( gtk_toggle_button_get_active(button) )
298                 g_object_set(current_tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
299         else
300                 g_object_set(current_tag, "style", PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
301
302         chimara_glk_update_style(glk);
303 }
304
305 void
306 on_toggle_underline(GtkToggleButton *button, ChimaraGlk *glk) {
307         if( gtk_toggle_button_get_active(button) )
308                 g_object_set(current_tag, "underline", PANGO_UNDERLINE_SINGLE, "underline-set", TRUE, NULL);
309         else
310                 g_object_set(current_tag, "underline", PANGO_UNDERLINE_NONE, "underline-set", TRUE, NULL);
311
312         chimara_glk_update_style(glk);
313 }
314
315 void
316 on_foreground_color_set(GtkColorButton *button, ChimaraGlk *glk)
317 {
318         GdkColor color;
319     gtk_color_button_get_color(button, &color);
320         g_object_set(current_tag, "foreground-gdk", &color, "foreground-set", TRUE, NULL);
321         chimara_glk_update_style(glk);
322 }
323
324 void
325 on_background_color_set(GtkColorButton *button, ChimaraGlk *glk)
326 {
327         GdkColor color;
328     gtk_color_button_get_color(button, &color);
329         g_object_set(current_tag, "background-gdk", &color, "background-set", TRUE, NULL);
330         chimara_glk_update_style(glk);
331 }
332
333 void
334 on_font_set(GtkFontButton *button, ChimaraGlk *glk)
335 {
336         const gchar *font_name = gtk_font_button_get_font_name(button);
337         PangoFontDescription *font_description = pango_font_description_from_string(font_name);
338         g_object_set(current_tag, "font-desc", font_description, NULL);
339         chimara_glk_update_style(glk);
340 }
341
342 void
343 on_css_filechooser_file_set(GtkFileChooserButton *button, ChimaraGlk *glk)
344 {
345         GError *error = NULL;
346         extern GSettings *prefs_settings;
347         char *filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(button) );
348         if(!chimara_glk_set_css_from_file(glk, filename, &error)) {
349                 error_dialog(NULL, error, "There was a problem reading the CSS file: ");
350                 g_settings_set(prefs_settings, "css-file", "ms", NULL);
351         } else {
352                 g_settings_set(prefs_settings, "css-file", "ms", filename);
353         }
354         g_free(filename);
355 }
356
357 void
358 on_resource_file_set(GtkFileChooserButton *button, ChimaraGlk *glk)
359 {
360         extern GSettings *prefs_settings;
361         char *filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(button) );
362         g_settings_set(prefs_settings, "resource-path", "ms", filename);
363         g_free(filename);
364 }
365
366 void
367 on_interpreter_cell_changed(GtkCellRendererCombo *combo, char *path_string, GtkTreeIter *new_iter, ChimaraGlk *glk)
368 {
369         unsigned int format, interpreter;
370         format = (unsigned int)strtol(path_string, NULL, 10);
371         GtkTreeModel *combo_model;
372         g_object_get(combo, "model", &combo_model, NULL);
373         char *combo_string = gtk_tree_model_get_string_from_iter(combo_model, new_iter);
374         interpreter = (unsigned int)strtol(combo_string, NULL, 10);
375         g_free(combo_string);
376
377         chimara_if_set_preferred_interpreter(CHIMARA_IF(glk), format, interpreter);
378
379         /* Display the new setting in the list */
380         GtkTreeIter iter;
381         GtkTreePath *path = gtk_tree_path_new_from_string(path_string);
382         gtk_tree_model_get_iter(GTK_TREE_MODEL(preferred_list), &iter, path);
383         gtk_tree_path_free(path);
384         gtk_list_store_set(preferred_list, &iter,
385                 1, interpreter_to_display_string(interpreter),
386                 -1);
387
388         /* Save the new settings in the preferences file */
389         extern GSettings *prefs_settings;
390         GVariantBuilder *builder = g_variant_builder_new( G_VARIANT_TYPE("a{ss}") );
391         unsigned int count;
392         for(count = 0; count < CHIMARA_IF_NUM_FORMATS; count++) {
393                 g_variant_builder_add(builder, "{ss}",
394                         format_to_string(count),
395                         interpreter_to_string(chimara_if_get_preferred_interpreter(CHIMARA_IF(glk), count)));
396         }
397         g_settings_set(prefs_settings, "preferred-interpreters", "a{ss}", builder);
398         g_variant_builder_unref(builder);
399 }