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