cf485f7c3ca0115a36d57851dbd244ad846fde89
[projects/chimara/chimara.git] / player / preferences.c
1 /*
2  * Copyright (C) 2008, 2009, 2010, 2011 Philip Chimento and Marijn van Vliet.
3  * All rights reserved.
4  *
5  * Chimara is free software copyrighted by Philip Chimento and Marijn van Vliet.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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 of the names Philip Chimento or Marijn van Vliet, nor the name of
17  *    any other contributor may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR 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 <glib.h>
34 #include <glib-object.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 "error.h"
40 #include "app.h"
41 #include "preferences.h"
42 #include "util.h"
43
44 typedef struct _ChimaraPrefsPrivate {
45         int dummy;
46 } ChimaraPrefsPrivate;
47
48 #define CHIMARA_PREFS_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), CHIMARA_TYPE_PREFS, ChimaraPrefsPrivate))
49 #define CHIMARA_PREFS_USE_PRIVATE ChimaraPrefsPrivate *priv = CHIMARA_PREFS_PRIVATE(self)
50
51 G_DEFINE_TYPE(ChimaraPrefs, chimara_prefs, GTK_TYPE_DIALOG);
52
53 static GtkTextTag *current_tag;
54 static GtkListStore *preferred_list;
55
56 static void style_tree_select_callback(GtkTreeSelection *selection);
57
58 static void
59 chimara_prefs_finalize(GObject *self)
60 {
61         /* Chain up */
62         G_OBJECT_CLASS(chimara_prefs_parent_class)->finalize(self);
63 }
64
65 static void
66 chimara_prefs_class_init(ChimaraPrefsClass *klass)
67 {
68         /* Override methods of parent classes */
69         GObjectClass *object_class = G_OBJECT_CLASS(klass);
70         object_class->finalize = chimara_prefs_finalize;
71
72         /* Private data */
73         g_type_class_add_private(klass, sizeof(ChimaraPrefsPrivate));
74 }
75
76 /* Internal functions to convert from human-readable names in the config file
77 to enums and back. Later: replace with plugin functions. */
78 static ChimaraIFFormat
79 parse_format(const char *format)
80 {
81         if(strcmp(format, "z5") == 0)
82                 return CHIMARA_IF_FORMAT_Z5;
83         if(strcmp(format, "z6") == 0)
84                 return CHIMARA_IF_FORMAT_Z6;
85         if(strcmp(format, "z8") == 0)
86                 return CHIMARA_IF_FORMAT_Z8;
87         if(strcmp(format, "zblorb") == 0)
88                 return CHIMARA_IF_FORMAT_Z_BLORB;
89         if(strcmp(format, "glulx") == 0)
90                 return CHIMARA_IF_FORMAT_GLULX;
91         if(strcmp(format, "gblorb") == 0)
92                 return CHIMARA_IF_FORMAT_GLULX_BLORB;
93         return CHIMARA_IF_FORMAT_NONE;
94 }
95
96 static const char *format_strings[CHIMARA_IF_NUM_FORMATS] = {
97         "z5", "z6", "z8", "zblorb", "glulx", "gblorb"
98 };
99
100 static const char *format_to_string(ChimaraIFFormat format)
101 {
102         if(format >= 0 && format < CHIMARA_IF_NUM_FORMATS)
103                 return format_strings[format];
104         return "unknown";
105 }
106
107 static const char *format_display_strings[CHIMARA_IF_NUM_FORMATS] = {
108         N_("Z-machine version 5"),
109         N_("Z-machine version 6"),
110         N_("Z-machine version 8"),
111         N_("Z-machine Blorb file"),
112         N_("Glulx"),
113         N_("Glulx Blorb file")
114 };
115
116 static const char *
117 format_to_display_string(ChimaraIFFormat format)
118 {
119         if(format >= 0 && format < CHIMARA_IF_NUM_FORMATS)
120                 return gettext(format_display_strings[format]);
121         return _("Unknown");
122 }
123
124 static ChimaraIFInterpreter
125 parse_interpreter(const char *interp)
126 {
127         if(strcmp(interp, "frotz") == 0)
128                 return CHIMARA_IF_INTERPRETER_FROTZ;
129         if(strcmp(interp, "nitfol") == 0)
130                 return CHIMARA_IF_INTERPRETER_NITFOL;
131         if(strcmp(interp, "glulxe") == 0)
132                 return CHIMARA_IF_INTERPRETER_GLULXE;
133         if(strcmp(interp, "git") == 0)
134                 return CHIMARA_IF_INTERPRETER_GIT;
135         return CHIMARA_IF_INTERPRETER_NONE;
136 }
137
138 static const char *interpreter_strings[CHIMARA_IF_NUM_INTERPRETERS] = {
139         "frotz", "nitfol", "glulxe", "git"
140 };
141
142 static const char *
143 interpreter_to_string(ChimaraIFInterpreter interp)
144 {
145         if(interp >= 0 && interp < CHIMARA_IF_NUM_INTERPRETERS)
146                 return interpreter_strings[interp];
147         return "unknown";
148 }
149
150 static const char *interpreter_display_strings[CHIMARA_IF_NUM_INTERPRETERS] = {
151         N_("Frotz"),
152         N_("Nitfol"),
153         N_("Glulxe"),
154         N_("Git")
155 };
156
157 static const char *
158 interpreter_to_display_string(ChimaraIFInterpreter interp)
159 {
160         if(interp >= 0 && interp < CHIMARA_IF_NUM_INTERPRETERS)
161                 return gettext(interpreter_display_strings[interp]);
162         return _("Unknown");
163 }
164
165 /* Create the preferences dialog. */
166 static void
167 chimara_prefs_init(ChimaraPrefs *self)
168 {
169         ChimaraApp *theapp = chimara_app_get();
170
171         /* Set parent properties */
172         g_object_set(self,
173                 "title", _("Chimara Preferences"),
174                 "window-position", GTK_WIN_POS_CENTER,
175                 "type-hint", GDK_WINDOW_TYPE_HINT_DIALOG,
176                 "border-width", 6,
177                 NULL);
178         gtk_dialog_add_buttons(GTK_DIALOG(self),
179                 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
180                 NULL);
181         
182         /* Build user interface */
183         char *object_ids[] = {
184                 "prefs-notebook",
185                 "available_interpreters",
186                 "interpreters",
187                 "style-list",
188                 NULL
189         };
190         GtkBuilder *builder = new_builder_with_objects(object_ids);
191
192         GtkWidget *notebook = GTK_WIDGET( load_object(builder, "prefs-notebook") );
193         GtkWidget *content_area = gtk_dialog_get_content_area( GTK_DIALOG(self) );
194         gtk_container_add( GTK_CONTAINER(content_area), notebook );
195         
196         /* Initialize the tree of style names */
197         GtkTreeStore *style_list = GTK_TREE_STORE( load_object(builder, "style-list") );
198         GtkTreeIter buffer, grid, buffer_child, grid_child;
199
200         gtk_tree_store_append(style_list, &buffer, NULL);
201         gtk_tree_store_append(style_list, &grid, NULL);
202         gtk_tree_store_set(style_list, &buffer, 0, "Text buffer", -1);
203         gtk_tree_store_set(style_list, &grid, 0, "Text grid", -1);
204
205         //int i;
206         //unsigned int num_tags;
207         //const gchar **tag_names = chimara_glk_get_tag_names(glk, &num_tags);
208         //for(i=0; i<num_tags; i++) {
209         //      gtk_tree_store_append(style_list, &buffer_child, &buffer);
210         //      gtk_tree_store_append(style_list, &grid_child, &grid);
211         //      gtk_tree_store_set(style_list, &buffer_child, 0, tag_names[i], -1);
212         //      gtk_tree_store_set(style_list, &grid_child, 0, tag_names[i], -1);
213         //}
214
215         /* Set selection mode to single select */
216         GtkTreeView *view = GTK_TREE_VIEW( load_object(builder, "style-treeview") );
217         GtkTreeSelection *selection = gtk_tree_view_get_selection(view);
218         gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
219         g_signal_connect(selection, "changed", G_CALLBACK(style_tree_select_callback), NULL);
220
221         /* Bind the preferences to the entries in the preferences file */
222         GObject *flep = G_OBJECT( load_object(builder, "flep") );
223         g_settings_bind(theapp->prefs_settings, "flep", flep, "active", G_SETTINGS_BIND_DEFAULT);
224         GtkFileChooser *blorb_chooser = GTK_FILE_CHOOSER( load_object(builder, "blorb_file_chooser") );
225         GtkFileChooser *css_chooser = GTK_FILE_CHOOSER( load_object(builder, "css-filechooser") );
226         char *filename;
227         g_settings_get(theapp->prefs_settings, "resource-path", "ms", &filename);
228         if(filename) {
229                 gtk_file_chooser_set_filename(blorb_chooser, filename);
230                 g_free(filename);
231         }
232         g_settings_get(theapp->prefs_settings, "css-file", "ms", &filename);
233         //if(filename) {
234         //      if(!chimara_glk_set_css_from_file(glk, filename, NULL)) {
235         //              /* If the setting didn't point to a CSS file, fail silently and
236         //               null the setting */
237         //              g_settings_set(theapp->prefs_settings, "css-file", "ms", NULL);
238         //      } else {
239         //              gtk_file_chooser_set_filename(css_chooser, filename);
240         //      }
241         //      g_free(filename);
242         //}
243
244         /* Populate the list of available interpreters */
245         GtkListStore *interp_list = GTK_LIST_STORE( load_object(builder, "available_interpreters") );
246         unsigned int count;
247         GtkTreeIter tree_iter;
248         for(count = 0; count < CHIMARA_IF_NUM_INTERPRETERS; count++) {
249                 gtk_list_store_append(interp_list, &tree_iter);
250                 gtk_list_store_set(interp_list, &tree_iter,
251                         0, interpreter_to_display_string(count),
252                         -1);
253         }
254
255         /* Get the list of preferred interpreters from the preferences */
256         //GVariantIter *iter;
257         //char *format, *plugin;
258         //g_settings_get(prefs_settings, "preferred-interpreters", "a{ss}", &iter);
259         //while(g_variant_iter_loop(iter, "{ss}", &format, &plugin)) {
260         //      ChimaraIFFormat format_num = parse_format(format);
261         //      if(format_num == CHIMARA_IF_FORMAT_NONE)
262         //              continue;
263         //      ChimaraIFInterpreter interp_num = parse_interpreter(plugin);
264         //      if(interp_num == CHIMARA_IF_INTERPRETER_NONE)
265         //              continue;
266         //      chimara_if_set_preferred_interpreter(CHIMARA_IF(glk), format_num, interp_num);
267         //}
268         //g_variant_iter_free(iter);
269
270         /* Display it all in the list */
271         //preferred_list = GTK_LIST_STORE( load_object(builder, "interpreters") );
272         //for(count = 0; count < CHIMARA_IF_NUM_FORMATS; count++) {
273         //      gtk_list_store_append(preferred_list, &tree_iter);
274         //      gtk_list_store_set(preferred_list, &tree_iter,
275         //              0, format_to_display_string(count),
276         //              1, interpreter_to_display_string(chimara_if_get_preferred_interpreter(CHIMARA_IF(glk), count)),
277         //              -1);
278         //}
279
280         gtk_builder_connect_signals(builder, self);
281         g_object_unref(builder);
282
283         /* Connect own signals */
284         g_signal_connect(self, "response", G_CALLBACK(gtk_widget_hide), NULL);
285         g_signal_connect(self, "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL);
286 }
287
288 /* PUBLIC FUNCTIONS */
289 GtkWidget *
290 chimara_prefs_new(void)
291 {
292         return GTK_WIDGET(g_object_new(CHIMARA_TYPE_PREFS,
293                 "type", GTK_WINDOW_TOPLEVEL,
294                 NULL));
295 }
296
297 /* GLADE CALLBACKS */
298
299 static void
300 style_tree_select_callback(GtkTreeSelection *selection)
301 {
302         GtkTreeIter child, parent;
303         gchar *child_name, *parent_name;
304         GtkTreeModel *model;
305
306         if( !gtk_tree_selection_get_selected(selection, &model, &child) )
307                 return;
308
309         gtk_tree_model_get(model, &child, 0, &child_name, -1);
310                 
311         if( !gtk_tree_model_iter_parent(model, &parent, &child) )
312                 return;
313
314         gtk_tree_model_get(model, &parent, 0, &parent_name, -1);
315         //if( !strcmp(parent_name, "Text buffer") )
316         //      current_tag = chimara_glk_get_tag(glk, CHIMARA_GLK_TEXT_BUFFER, child_name);
317         //else
318         //      current_tag = chimara_glk_get_tag(glk, CHIMARA_GLK_TEXT_GRID, child_name);
319 }
320
321 void
322 on_toggle_left(GtkToggleButton *button, ChimaraGlk *glk) {
323         /* No nothing if the button is deactivated */
324         if( !gtk_toggle_button_get_active(button) )
325                 return;
326         g_object_set(current_tag, "justification", GTK_JUSTIFY_LEFT, "justification-set", TRUE, NULL);
327         chimara_glk_update_style(glk);
328 }
329
330 void
331 on_toggle_center(GtkToggleButton *button, ChimaraGlk *glk) {
332         if( !gtk_toggle_button_get_active(button) )
333                 return;
334         g_object_set(current_tag, "justification", GTK_JUSTIFY_CENTER, "justification-set", TRUE, NULL);
335         chimara_glk_update_style(glk);
336 }
337
338 void
339 on_toggle_right(GtkToggleButton *button, ChimaraGlk *glk) {
340         if( !gtk_toggle_button_get_active(button) )
341                 return;
342         g_object_set(current_tag, "justification", GTK_JUSTIFY_RIGHT, "justification-set", TRUE, NULL);
343         chimara_glk_update_style(glk);
344 }
345
346 void
347 on_toggle_justify(GtkToggleButton *button, ChimaraGlk *glk) {
348         if( !gtk_toggle_button_get_active(button) )
349                 return;
350         g_object_set(current_tag, "justification", GTK_JUSTIFY_FILL, "justification-set", TRUE, NULL);
351         chimara_glk_update_style(glk);
352 }
353
354 void
355 on_toggle_bold(GtkToggleButton *button, ChimaraGlk *glk) {
356         if( gtk_toggle_button_get_active(button) )
357                 g_object_set(current_tag, "weight", PANGO_WEIGHT_BOLD, "weight-set", TRUE, NULL);
358         else
359                 g_object_set(current_tag, "weight", PANGO_WEIGHT_NORMAL, "weight-set", TRUE, NULL);
360
361         chimara_glk_update_style(glk);
362 }
363
364 void
365 on_toggle_italic(GtkToggleButton *button, ChimaraGlk *glk) {
366         if( gtk_toggle_button_get_active(button) )
367                 g_object_set(current_tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
368         else
369                 g_object_set(current_tag, "style", PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
370
371         chimara_glk_update_style(glk);
372 }
373
374 void
375 on_toggle_underline(GtkToggleButton *button, ChimaraGlk *glk) {
376         if( gtk_toggle_button_get_active(button) )
377                 g_object_set(current_tag, "underline", PANGO_UNDERLINE_SINGLE, "underline-set", TRUE, NULL);
378         else
379                 g_object_set(current_tag, "underline", PANGO_UNDERLINE_NONE, "underline-set", TRUE, NULL);
380
381         chimara_glk_update_style(glk);
382 }
383
384 void
385 on_foreground_color_set(GtkColorButton *button, ChimaraGlk *glk)
386 {
387         GdkColor color;
388     gtk_color_button_get_color(button, &color);
389         g_object_set(current_tag, "foreground-gdk", &color, "foreground-set", TRUE, NULL);
390         chimara_glk_update_style(glk);
391 }
392
393 void
394 on_background_color_set(GtkColorButton *button, ChimaraGlk *glk)
395 {
396         GdkColor color;
397     gtk_color_button_get_color(button, &color);
398         g_object_set(current_tag, "background-gdk", &color, "background-set", TRUE, NULL);
399         chimara_glk_update_style(glk);
400 }
401
402 void
403 on_font_set(GtkFontButton *button, ChimaraGlk *glk)
404 {
405         const gchar *font_name = gtk_font_button_get_font_name(button);
406         PangoFontDescription *font_description = pango_font_description_from_string(font_name);
407         g_object_set(current_tag, "font-desc", font_description, NULL);
408         chimara_glk_update_style(glk);
409 }
410
411 void
412 on_css_filechooser_file_set(GtkFileChooserButton *button, ChimaraGlk *glk)
413 {
414         //GError *error = NULL;
415         //ChimaraApp *theapp = chimara_app_get();
416         //char *filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(button) );
417         //if(!chimara_glk_set_css_from_file(glk, filename, &error)) {
418         //      error_dialog(NULL, error, "There was a problem reading the CSS file: ");
419         //      g_settings_set(theapp->prefs_settings, "css-file", "ms", NULL);
420         //} else {
421         //      g_settings_set(theapp->prefs_settings, "css-file", "ms", filename);
422         //}
423         //g_free(filename);
424 }
425
426 void
427 on_resource_file_set(GtkFileChooserButton *button, ChimaraGlk *glk)
428 {
429         ChimaraApp *theapp = chimara_app_get();
430         char *filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(button) );
431         g_settings_set(theapp->prefs_settings, "resource-path", "ms", filename);
432         g_free(filename);
433 }
434
435 void
436 on_interpreter_cell_changed(GtkCellRendererCombo *combo, char *path_string, GtkTreeIter *new_iter, ChimaraGlk *glk)
437 {
438         //unsigned int format, interpreter;
439         //format = (unsigned int)strtol(path_string, NULL, 10);
440         //GtkTreeModel *combo_model;
441         //g_object_get(combo, "model", &combo_model, NULL);
442         //char *combo_string = gtk_tree_model_get_string_from_iter(combo_model, new_iter);
443         //interpreter = (unsigned int)strtol(combo_string, NULL, 10);
444         //g_free(combo_string);
445
446         //chimara_if_set_preferred_interpreter(CHIMARA_IF(glk), format, interpreter);
447
448         /* Display the new setting in the list */
449         //GtkTreeIter iter;
450         //GtkTreePath *path = gtk_tree_path_new_from_string(path_string);
451         //gtk_tree_model_get_iter(GTK_TREE_MODEL(preferred_list), &iter, path);
452         //gtk_tree_path_free(path);
453         //gtk_list_store_set(preferred_list, &iter,
454         //      1, interpreter_to_display_string(interpreter),
455         //      -1);
456
457         /* Save the new settings in the preferences file */
458         //ChimaraApp *theapp = chimara_app_get();
459         //GVariantBuilder *builder = g_variant_builder_new( G_VARIANT_TYPE("a{ss}") );
460         //unsigned int count;
461         //for(count = 0; count < CHIMARA_IF_NUM_FORMATS; count++) {
462         //      g_variant_builder_add(builder, "{ss}",
463         //              format_to_string(count),
464         //              interpreter_to_string(chimara_if_get_preferred_interpreter(CHIMARA_IF(glk), count)));
465         //}
466         //g_settings_set(theapp->prefs_settings, "preferred-interpreters", "a{ss}", builder);
467         //g_variant_builder_unref(builder);
468 }