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-object.h>
36 #include <glib/gi18n.h>
38 #include <libchimara/chimara-glk.h>
39 #include <libchimara/chimara-if.h>
43 #include "preferences.h"
46 typedef struct _ChimaraPrefsPrivate {
48 } ChimaraPrefsPrivate;
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)
53 G_DEFINE_TYPE(ChimaraPrefs, chimara_prefs, GTK_TYPE_DIALOG);
55 static GtkTextTag *current_tag;
56 static GtkListStore *preferred_list;
58 static void style_tree_select_callback(GtkTreeSelection *selection);
61 chimara_prefs_finalize(GObject *self)
63 //CHIMARA_APP_USE_PRIVATE;
64 //g_object_unref(priv->action_group);
67 G_OBJECT_CLASS(chimara_prefs_parent_class)->finalize(self);
71 chimara_prefs_class_init(ChimaraPrefsClass *klass)
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;
84 g_type_class_add_private(klass, sizeof(ChimaraPrefsPrivate));
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)
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;
107 static const char *format_strings[CHIMARA_IF_NUM_FORMATS] = {
108 "z5", "z6", "z8", "zblorb", "glulx", "gblorb"
111 static const char *format_to_string(ChimaraIFFormat format)
113 if(format >= 0 && format < CHIMARA_IF_NUM_FORMATS)
114 return format_strings[format];
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"),
124 N_("Glulx Blorb file")
128 format_to_display_string(ChimaraIFFormat format)
130 if(format >= 0 && format < CHIMARA_IF_NUM_FORMATS)
131 return gettext(format_display_strings[format]);
135 static ChimaraIFInterpreter
136 parse_interpreter(const char *interp)
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;
149 static const char *interpreter_strings[CHIMARA_IF_NUM_INTERPRETERS] = {
150 "frotz", "nitfol", "glulxe", "git"
154 interpreter_to_string(ChimaraIFInterpreter interp)
156 if(interp >= 0 && interp < CHIMARA_IF_NUM_INTERPRETERS)
157 return interpreter_strings[interp];
161 static const char *interpreter_display_strings[CHIMARA_IF_NUM_INTERPRETERS] = {
169 interpreter_to_display_string(ChimaraIFInterpreter interp)
171 if(interp >= 0 && interp < CHIMARA_IF_NUM_INTERPRETERS)
172 return gettext(interpreter_display_strings[interp]);
176 /* Create the preferences dialog. */
178 chimara_prefs_init(ChimaraPrefs *self)
180 GError *error = NULL;
181 ChimaraApp *theapp = chimara_app_get();
183 /* Set parent properties */
185 "title", _("Chimara Preferences"),
186 "window-position", GTK_WIN_POS_CENTER,
187 "type-hint", GDK_WINDOW_TYPE_HINT_DIALOG,
190 gtk_dialog_add_buttons(GTK_DIALOG(self),
191 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
194 /* Build user interface */
195 char *object_ids[] = {
197 "available_interpreters",
202 GtkBuilder *builder = new_builder_with_objects(object_ids);
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 );
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;
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);
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);
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);
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") );
239 g_settings_get(theapp->prefs_settings, "resource-path", "ms", &filename);
241 gtk_file_chooser_set_filename(blorb_chooser, filename);
244 g_settings_get(theapp->prefs_settings, "css-file", "ms", &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);
251 // gtk_file_chooser_set_filename(css_chooser, filename);
256 /* Populate the list of available interpreters */
257 GtkListStore *interp_list = GTK_LIST_STORE( load_object(builder, "available_interpreters") );
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),
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)
275 // ChimaraIFInterpreter interp_num = parse_interpreter(plugin);
276 // if(interp_num == CHIMARA_IF_INTERPRETER_NONE)
278 // chimara_if_set_preferred_interpreter(CHIMARA_IF(glk), format_num, interp_num);
280 //g_variant_iter_free(iter);
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)),
292 gtk_builder_connect_signals(builder, self);
293 g_object_unref(builder);
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);
300 /* PUBLIC FUNCTIONS */
302 chimara_prefs_new(void)
304 return GTK_WIDGET(g_object_new(CHIMARA_TYPE_PREFS,
305 "type", GTK_WINDOW_TOPLEVEL,
309 /* GLADE CALLBACKS */
312 style_tree_select_callback(GtkTreeSelection *selection)
314 GtkTreeIter child, parent;
315 gchar *child_name, *parent_name;
318 if( !gtk_tree_selection_get_selected(selection, &model, &child) )
321 gtk_tree_model_get(model, &child, 0, &child_name, -1);
323 if( !gtk_tree_model_iter_parent(model, &parent, &child) )
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);
330 // current_tag = chimara_glk_get_tag(glk, CHIMARA_GLK_TEXT_GRID, child_name);
334 on_toggle_left(GtkToggleButton *button, ChimaraGlk *glk) {
335 /* No nothing if the button is deactivated */
336 if( !gtk_toggle_button_get_active(button) )
338 g_object_set(current_tag, "justification", GTK_JUSTIFY_LEFT, "justification-set", TRUE, NULL);
339 chimara_glk_update_style(glk);
343 on_toggle_center(GtkToggleButton *button, ChimaraGlk *glk) {
344 if( !gtk_toggle_button_get_active(button) )
346 g_object_set(current_tag, "justification", GTK_JUSTIFY_CENTER, "justification-set", TRUE, NULL);
347 chimara_glk_update_style(glk);
351 on_toggle_right(GtkToggleButton *button, ChimaraGlk *glk) {
352 if( !gtk_toggle_button_get_active(button) )
354 g_object_set(current_tag, "justification", GTK_JUSTIFY_RIGHT, "justification-set", TRUE, NULL);
355 chimara_glk_update_style(glk);
359 on_toggle_justify(GtkToggleButton *button, ChimaraGlk *glk) {
360 if( !gtk_toggle_button_get_active(button) )
362 g_object_set(current_tag, "justification", GTK_JUSTIFY_FILL, "justification-set", TRUE, NULL);
363 chimara_glk_update_style(glk);
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);
371 g_object_set(current_tag, "weight", PANGO_WEIGHT_NORMAL, "weight-set", TRUE, NULL);
373 chimara_glk_update_style(glk);
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);
381 g_object_set(current_tag, "style", PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
383 chimara_glk_update_style(glk);
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);
391 g_object_set(current_tag, "underline", PANGO_UNDERLINE_NONE, "underline-set", TRUE, NULL);
393 chimara_glk_update_style(glk);
397 on_foreground_color_set(GtkColorButton *button, ChimaraGlk *glk)
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);
406 on_background_color_set(GtkColorButton *button, ChimaraGlk *glk)
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);
415 on_font_set(GtkFontButton *button, ChimaraGlk *glk)
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);
424 on_css_filechooser_file_set(GtkFileChooserButton *button, ChimaraGlk *glk)
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);
433 // g_settings_set(theapp->prefs_settings, "css-file", "ms", filename);
439 on_resource_file_set(GtkFileChooserButton *button, ChimaraGlk *glk)
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);
448 on_interpreter_cell_changed(GtkCellRendererCombo *combo, char *path_string, GtkTreeIter *new_iter, ChimaraGlk *glk)
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);
458 //chimara_if_set_preferred_interpreter(CHIMARA_IF(glk), format, interpreter);
460 /* Display the new setting in the list */
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),
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)));
478 //g_settings_set(theapp->prefs_settings, "preferred-interpreters", "a{ss}", builder);
479 //g_variant_builder_unref(builder);