1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) Philip en Marijn 2008 <>
6 * preferences.c is free software copyrighted by Philip en Marijn.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
35 #include <glib/gi18n.h>
37 #include <libchimara/chimara-glk.h>
38 #include <libchimara/chimara-if.h>
43 static GtkTextTag *current_tag;
44 static GtkListStore *preferred_list;
46 static void style_tree_select_callback(GtkTreeSelection *selection);
49 load_object(GtkBuilder *builder, const gchar *name)
52 if( (retval = gtk_builder_get_object(builder, name)) == NULL) {
53 error_dialog(NULL, NULL, "Error while getting object '%s'", name);
54 g_error("Error while getting object '%s'", name);
59 /* Internal functions to convert from human-readable names in the config file
60 to enums and back. Later: replace with plugin functions. */
61 static ChimaraIFFormat
62 parse_format(const char *format)
64 if(strcmp(format, "z5") == 0)
65 return CHIMARA_IF_FORMAT_Z5;
66 if(strcmp(format, "z6") == 0)
67 return CHIMARA_IF_FORMAT_Z6;
68 if(strcmp(format, "z8") == 0)
69 return CHIMARA_IF_FORMAT_Z8;
70 if(strcmp(format, "zblorb") == 0)
71 return CHIMARA_IF_FORMAT_Z_BLORB;
72 if(strcmp(format, "glulx") == 0)
73 return CHIMARA_IF_FORMAT_GLULX;
74 if(strcmp(format, "gblorb") == 0)
75 return CHIMARA_IF_FORMAT_GLULX_BLORB;
76 return CHIMARA_IF_FORMAT_NONE;
79 static const char *format_strings[CHIMARA_IF_NUM_FORMATS] = {
80 "z5", "z6", "z8", "zblorb", "glulx", "gblorb"
83 static const char *format_to_string(ChimaraIFFormat format)
85 if(format >= 0 && format < CHIMARA_IF_NUM_FORMATS)
86 return format_strings[format];
90 static const char *format_display_strings[CHIMARA_IF_NUM_FORMATS] = {
91 N_("Z-machine version 5"),
92 N_("Z-machine version 6"),
93 N_("Z-machine version 8"),
94 N_("Z-machine Blorb file"),
96 N_("Glulx Blorb file")
100 format_to_display_string(ChimaraIFFormat format)
102 if(format >= 0 && format < CHIMARA_IF_NUM_FORMATS)
103 return gettext(format_display_strings[format]);
107 static ChimaraIFInterpreter
108 parse_interpreter(const char *interp)
110 if(strcmp(interp, "frotz") == 0)
111 return CHIMARA_IF_INTERPRETER_FROTZ;
112 if(strcmp(interp, "nitfol") == 0)
113 return CHIMARA_IF_INTERPRETER_NITFOL;
114 if(strcmp(interp, "glulxe") == 0)
115 return CHIMARA_IF_INTERPRETER_GLULXE;
116 if(strcmp(interp, "git") == 0)
117 return CHIMARA_IF_INTERPRETER_GIT;
118 return CHIMARA_IF_INTERPRETER_NONE;
121 static const char *interpreter_strings[CHIMARA_IF_NUM_INTERPRETERS] = {
122 "frotz", "nitfol", "glulxe", "git"
126 interpreter_to_string(ChimaraIFInterpreter interp)
128 if(interp >= 0 && interp < CHIMARA_IF_NUM_INTERPRETERS)
129 return interpreter_strings[interp];
133 static const char *interpreter_display_strings[CHIMARA_IF_NUM_INTERPRETERS] = {
141 interpreter_to_display_string(ChimaraIFInterpreter interp)
143 if(interp >= 0 && interp < CHIMARA_IF_NUM_INTERPRETERS)
144 return gettext(interpreter_display_strings[interp]);
148 /* Create the preferences dialog. */
150 preferences_create(ChimaraApp *theapp, GtkBuilder *builder)
152 /* Initialize the tree of style names */
153 GtkTreeStore *style_list = GTK_TREE_STORE( load_object(builder, "style-list") );
154 GtkTreeIter buffer, grid, buffer_child, grid_child;
156 gtk_tree_store_append(style_list, &buffer, NULL);
157 gtk_tree_store_append(style_list, &grid, NULL);
158 gtk_tree_store_set(style_list, &buffer, 0, "Text buffer", -1);
159 gtk_tree_store_set(style_list, &grid, 0, "Text grid", -1);
162 //unsigned int num_tags;
163 //const gchar **tag_names = chimara_glk_get_tag_names(glk, &num_tags);
164 //for(i=0; i<num_tags; i++) {
165 // gtk_tree_store_append(style_list, &buffer_child, &buffer);
166 // gtk_tree_store_append(style_list, &grid_child, &grid);
167 // gtk_tree_store_set(style_list, &buffer_child, 0, tag_names[i], -1);
168 // gtk_tree_store_set(style_list, &grid_child, 0, tag_names[i], -1);
171 /* Set selection mode to single select */
172 GtkTreeView *view = GTK_TREE_VIEW( load_object(builder, "style-treeview") );
173 GtkTreeSelection *selection = gtk_tree_view_get_selection(view);
174 gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
175 g_signal_connect(selection, "changed", G_CALLBACK(style_tree_select_callback), NULL);
177 /* Bind the preferences to the entries in the preferences file */
178 GObject *flep = G_OBJECT( load_object(builder, "flep") );
179 g_settings_bind(theapp->prefs_settings, "flep", flep, "active", G_SETTINGS_BIND_DEFAULT);
180 GtkFileChooser *blorb_chooser = GTK_FILE_CHOOSER( load_object(builder, "blorb_file_chooser") );
181 GtkFileChooser *css_chooser = GTK_FILE_CHOOSER( load_object(builder, "css-filechooser") );
183 g_settings_get(theapp->prefs_settings, "resource-path", "ms", &filename);
185 gtk_file_chooser_set_filename(blorb_chooser, filename);
188 g_settings_get(theapp->prefs_settings, "css-file", "ms", &filename);
190 // if(!chimara_glk_set_css_from_file(glk, filename, NULL)) {
191 // /* If the setting didn't point to a CSS file, fail silently and
192 // null the setting */
193 // g_settings_set(theapp->prefs_settings, "css-file", "ms", NULL);
195 // gtk_file_chooser_set_filename(css_chooser, filename);
200 /* Populate the list of available interpreters */
201 GtkListStore *interp_list = GTK_LIST_STORE( load_object(builder, "available_interpreters") );
203 GtkTreeIter tree_iter;
204 for(count = 0; count < CHIMARA_IF_NUM_INTERPRETERS; count++) {
205 gtk_list_store_append(interp_list, &tree_iter);
206 gtk_list_store_set(interp_list, &tree_iter,
207 0, interpreter_to_display_string(count),
211 /* Get the list of preferred interpreters from the preferences */
212 //GVariantIter *iter;
213 //char *format, *plugin;
214 //g_settings_get(prefs_settings, "preferred-interpreters", "a{ss}", &iter);
215 //while(g_variant_iter_loop(iter, "{ss}", &format, &plugin)) {
216 // ChimaraIFFormat format_num = parse_format(format);
217 // if(format_num == CHIMARA_IF_FORMAT_NONE)
219 // ChimaraIFInterpreter interp_num = parse_interpreter(plugin);
220 // if(interp_num == CHIMARA_IF_INTERPRETER_NONE)
222 // chimara_if_set_preferred_interpreter(CHIMARA_IF(glk), format_num, interp_num);
224 //g_variant_iter_free(iter);
226 /* Display it all in the list */
227 //preferred_list = GTK_LIST_STORE( load_object(builder, "interpreters") );
228 //for(count = 0; count < CHIMARA_IF_NUM_FORMATS; count++) {
229 // gtk_list_store_append(preferred_list, &tree_iter);
230 // gtk_list_store_set(preferred_list, &tree_iter,
231 // 0, format_to_display_string(count),
232 // 1, interpreter_to_display_string(chimara_if_get_preferred_interpreter(CHIMARA_IF(glk), count)),
238 style_tree_select_callback(GtkTreeSelection *selection)
240 GtkTreeIter child, parent;
241 gchar *child_name, *parent_name;
244 if( !gtk_tree_selection_get_selected(selection, &model, &child) )
247 gtk_tree_model_get(model, &child, 0, &child_name, -1);
249 if( !gtk_tree_model_iter_parent(model, &parent, &child) )
252 gtk_tree_model_get(model, &parent, 0, &parent_name, -1);
253 //if( !strcmp(parent_name, "Text buffer") )
254 // current_tag = chimara_glk_get_tag(glk, CHIMARA_GLK_TEXT_BUFFER, child_name);
256 // current_tag = chimara_glk_get_tag(glk, CHIMARA_GLK_TEXT_GRID, child_name);
260 on_toggle_left(GtkToggleButton *button, ChimaraGlk *glk) {
261 /* No nothing if the button is deactivated */
262 if( !gtk_toggle_button_get_active(button) )
264 g_object_set(current_tag, "justification", GTK_JUSTIFY_LEFT, "justification-set", TRUE, NULL);
265 chimara_glk_update_style(glk);
269 on_toggle_center(GtkToggleButton *button, ChimaraGlk *glk) {
270 if( !gtk_toggle_button_get_active(button) )
272 g_object_set(current_tag, "justification", GTK_JUSTIFY_CENTER, "justification-set", TRUE, NULL);
273 chimara_glk_update_style(glk);
277 on_toggle_right(GtkToggleButton *button, ChimaraGlk *glk) {
278 if( !gtk_toggle_button_get_active(button) )
280 g_object_set(current_tag, "justification", GTK_JUSTIFY_RIGHT, "justification-set", TRUE, NULL);
281 chimara_glk_update_style(glk);
285 on_toggle_justify(GtkToggleButton *button, ChimaraGlk *glk) {
286 if( !gtk_toggle_button_get_active(button) )
288 g_object_set(current_tag, "justification", GTK_JUSTIFY_FILL, "justification-set", TRUE, NULL);
289 chimara_glk_update_style(glk);
293 on_toggle_bold(GtkToggleButton *button, ChimaraGlk *glk) {
294 if( gtk_toggle_button_get_active(button) )
295 g_object_set(current_tag, "weight", PANGO_WEIGHT_BOLD, "weight-set", TRUE, NULL);
297 g_object_set(current_tag, "weight", PANGO_WEIGHT_NORMAL, "weight-set", TRUE, NULL);
299 chimara_glk_update_style(glk);
303 on_toggle_italic(GtkToggleButton *button, ChimaraGlk *glk) {
304 if( gtk_toggle_button_get_active(button) )
305 g_object_set(current_tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
307 g_object_set(current_tag, "style", PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
309 chimara_glk_update_style(glk);
313 on_toggle_underline(GtkToggleButton *button, ChimaraGlk *glk) {
314 if( gtk_toggle_button_get_active(button) )
315 g_object_set(current_tag, "underline", PANGO_UNDERLINE_SINGLE, "underline-set", TRUE, NULL);
317 g_object_set(current_tag, "underline", PANGO_UNDERLINE_NONE, "underline-set", TRUE, NULL);
319 chimara_glk_update_style(glk);
323 on_foreground_color_set(GtkColorButton *button, ChimaraGlk *glk)
326 gtk_color_button_get_color(button, &color);
327 g_object_set(current_tag, "foreground-gdk", &color, "foreground-set", TRUE, NULL);
328 chimara_glk_update_style(glk);
332 on_background_color_set(GtkColorButton *button, ChimaraGlk *glk)
335 gtk_color_button_get_color(button, &color);
336 g_object_set(current_tag, "background-gdk", &color, "background-set", TRUE, NULL);
337 chimara_glk_update_style(glk);
341 on_font_set(GtkFontButton *button, ChimaraGlk *glk)
343 const gchar *font_name = gtk_font_button_get_font_name(button);
344 PangoFontDescription *font_description = pango_font_description_from_string(font_name);
345 g_object_set(current_tag, "font-desc", font_description, NULL);
346 chimara_glk_update_style(glk);
350 on_css_filechooser_file_set(GtkFileChooserButton *button, ChimaraGlk *glk)
352 GError *error = NULL;
353 ChimaraApp *theapp = chimara_app_get();
354 char *filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(button) );
355 if(!chimara_glk_set_css_from_file(glk, filename, &error)) {
356 error_dialog(NULL, error, "There was a problem reading the CSS file: ");
357 g_settings_set(theapp->prefs_settings, "css-file", "ms", NULL);
359 g_settings_set(theapp->prefs_settings, "css-file", "ms", filename);
365 on_resource_file_set(GtkFileChooserButton *button, ChimaraGlk *glk)
367 ChimaraApp *theapp = chimara_app_get();
368 char *filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(button) );
369 g_settings_set(theapp->prefs_settings, "resource-path", "ms", filename);
374 on_interpreter_cell_changed(GtkCellRendererCombo *combo, char *path_string, GtkTreeIter *new_iter, ChimaraGlk *glk)
376 unsigned int format, interpreter;
377 format = (unsigned int)strtol(path_string, NULL, 10);
378 GtkTreeModel *combo_model;
379 g_object_get(combo, "model", &combo_model, NULL);
380 char *combo_string = gtk_tree_model_get_string_from_iter(combo_model, new_iter);
381 interpreter = (unsigned int)strtol(combo_string, NULL, 10);
382 g_free(combo_string);
384 chimara_if_set_preferred_interpreter(CHIMARA_IF(glk), format, interpreter);
386 /* Display the new setting in the list */
388 GtkTreePath *path = gtk_tree_path_new_from_string(path_string);
389 gtk_tree_model_get_iter(GTK_TREE_MODEL(preferred_list), &iter, path);
390 gtk_tree_path_free(path);
391 gtk_list_store_set(preferred_list, &iter,
392 1, interpreter_to_display_string(interpreter),
395 /* Save the new settings in the preferences file */
396 ChimaraApp *theapp = chimara_app_get();
397 GVariantBuilder *builder = g_variant_builder_new( G_VARIANT_TYPE("a{ss}") );
399 for(count = 0; count < CHIMARA_IF_NUM_FORMATS; count++) {
400 g_variant_builder_add(builder, "{ss}",
401 format_to_string(count),
402 interpreter_to_string(chimara_if_get_preferred_interpreter(CHIMARA_IF(glk), count)));
404 g_settings_set(theapp->prefs_settings, "preferred-interpreters", "a{ss}", builder);
405 g_variant_builder_unref(builder);