2 * Copyright (C) 2008, 2009, 2010, 2011 Philip Chimento and Marijn van Vliet.
5 * Chimara is free software copyrighted by Philip Chimento and Marijn van Vliet.
7 * Redistribution and use in source and binary forms, with or without
8 * 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 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.
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.
34 #include <glib-object.h>
35 #include <glib/gi18n.h>
37 #include <libchimara/chimara-glk.h>
38 #include <libchimara/chimara-if.h>
41 #include "preferences.h"
44 typedef struct _ChimaraPrefsPrivate {
46 } ChimaraPrefsPrivate;
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)
51 G_DEFINE_TYPE(ChimaraPrefs, chimara_prefs, GTK_TYPE_DIALOG);
53 static GtkTextTag *current_tag;
54 static GtkListStore *preferred_list;
56 static void style_tree_select_callback(GtkTreeSelection *selection);
59 chimara_prefs_finalize(GObject *self)
62 G_OBJECT_CLASS(chimara_prefs_parent_class)->finalize(self);
66 chimara_prefs_class_init(ChimaraPrefsClass *klass)
68 /* Override methods of parent classes */
69 GObjectClass *object_class = G_OBJECT_CLASS(klass);
70 object_class->finalize = chimara_prefs_finalize;
73 g_type_class_add_private(klass, sizeof(ChimaraPrefsPrivate));
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)
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;
96 static const char *format_strings[CHIMARA_IF_NUM_FORMATS] = {
97 "z5", "z6", "z8", "zblorb", "glulx", "gblorb"
100 static const char *format_to_string(ChimaraIFFormat format)
102 if(format >= 0 && format < CHIMARA_IF_NUM_FORMATS)
103 return format_strings[format];
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"),
113 N_("Glulx Blorb file")
117 format_to_display_string(ChimaraIFFormat format)
119 if(format >= 0 && format < CHIMARA_IF_NUM_FORMATS)
120 return gettext(format_display_strings[format]);
124 static ChimaraIFInterpreter
125 parse_interpreter(const char *interp)
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;
138 static const char *interpreter_strings[CHIMARA_IF_NUM_INTERPRETERS] = {
139 "frotz", "nitfol", "glulxe", "git"
143 interpreter_to_string(ChimaraIFInterpreter interp)
145 if(interp >= 0 && interp < CHIMARA_IF_NUM_INTERPRETERS)
146 return interpreter_strings[interp];
150 static const char *interpreter_display_strings[CHIMARA_IF_NUM_INTERPRETERS] = {
158 interpreter_to_display_string(ChimaraIFInterpreter interp)
160 if(interp >= 0 && interp < CHIMARA_IF_NUM_INTERPRETERS)
161 return gettext(interpreter_display_strings[interp]);
165 /* Create the preferences dialog. */
167 chimara_prefs_init(ChimaraPrefs *self)
169 ChimaraApp *theapp = chimara_app_get();
171 /* Set parent properties */
173 "title", _("Chimara Preferences"),
174 "window-position", GTK_WIN_POS_CENTER,
175 "type-hint", GDK_WINDOW_TYPE_HINT_DIALOG,
178 gtk_dialog_add_buttons(GTK_DIALOG(self),
179 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
182 /* Build user interface */
183 char *object_ids[] = {
185 "available_interpreters",
190 GtkBuilder *builder = new_builder_with_objects(object_ids);
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 );
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;
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);
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);
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);
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") );
227 g_settings_get(theapp->prefs_settings, "resource-path", "ms", &filename);
229 gtk_file_chooser_set_filename(blorb_chooser, filename);
232 g_settings_get(theapp->prefs_settings, "css-file", "ms", &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);
239 // gtk_file_chooser_set_filename(css_chooser, filename);
244 /* Populate the list of available interpreters */
245 GtkListStore *interp_list = GTK_LIST_STORE( load_object(builder, "available_interpreters") );
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),
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)
263 // ChimaraIFInterpreter interp_num = parse_interpreter(plugin);
264 // if(interp_num == CHIMARA_IF_INTERPRETER_NONE)
266 // chimara_if_set_preferred_interpreter(CHIMARA_IF(glk), format_num, interp_num);
268 //g_variant_iter_free(iter);
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)),
280 gtk_builder_connect_signals(builder, self);
281 g_object_unref(builder);
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);
288 /* PUBLIC FUNCTIONS */
290 chimara_prefs_new(void)
292 return GTK_WIDGET(g_object_new(CHIMARA_TYPE_PREFS,
293 "type", GTK_WINDOW_TOPLEVEL,
297 /* GLADE CALLBACKS */
300 style_tree_select_callback(GtkTreeSelection *selection)
302 GtkTreeIter child, parent;
303 gchar *child_name, *parent_name;
306 if( !gtk_tree_selection_get_selected(selection, &model, &child) )
309 gtk_tree_model_get(model, &child, 0, &child_name, -1);
311 if( !gtk_tree_model_iter_parent(model, &parent, &child) )
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);
318 // current_tag = chimara_glk_get_tag(glk, CHIMARA_GLK_TEXT_GRID, child_name);
322 on_toggle_left(GtkToggleButton *button, ChimaraGlk *glk) {
323 /* No nothing if the button is deactivated */
324 if( !gtk_toggle_button_get_active(button) )
326 g_object_set(current_tag, "justification", GTK_JUSTIFY_LEFT, "justification-set", TRUE, NULL);
327 chimara_glk_update_style(glk);
331 on_toggle_center(GtkToggleButton *button, ChimaraGlk *glk) {
332 if( !gtk_toggle_button_get_active(button) )
334 g_object_set(current_tag, "justification", GTK_JUSTIFY_CENTER, "justification-set", TRUE, NULL);
335 chimara_glk_update_style(glk);
339 on_toggle_right(GtkToggleButton *button, ChimaraGlk *glk) {
340 if( !gtk_toggle_button_get_active(button) )
342 g_object_set(current_tag, "justification", GTK_JUSTIFY_RIGHT, "justification-set", TRUE, NULL);
343 chimara_glk_update_style(glk);
347 on_toggle_justify(GtkToggleButton *button, ChimaraGlk *glk) {
348 if( !gtk_toggle_button_get_active(button) )
350 g_object_set(current_tag, "justification", GTK_JUSTIFY_FILL, "justification-set", TRUE, NULL);
351 chimara_glk_update_style(glk);
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);
359 g_object_set(current_tag, "weight", PANGO_WEIGHT_NORMAL, "weight-set", TRUE, NULL);
361 chimara_glk_update_style(glk);
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);
369 g_object_set(current_tag, "style", PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
371 chimara_glk_update_style(glk);
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);
379 g_object_set(current_tag, "underline", PANGO_UNDERLINE_NONE, "underline-set", TRUE, NULL);
381 chimara_glk_update_style(glk);
385 on_foreground_color_set(GtkColorButton *button, ChimaraGlk *glk)
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);
394 on_background_color_set(GtkColorButton *button, ChimaraGlk *glk)
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);
403 on_font_set(GtkFontButton *button, ChimaraGlk *glk)
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);
412 on_css_filechooser_file_set(GtkFileChooserButton *button, ChimaraGlk *glk)
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);
421 // g_settings_set(theapp->prefs_settings, "css-file", "ms", filename);
427 on_resource_file_set(GtkFileChooserButton *button, ChimaraGlk *glk)
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);
436 on_interpreter_cell_changed(GtkCellRendererCombo *combo, char *path_string, GtkTreeIter *new_iter, ChimaraGlk *glk)
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);
446 //chimara_if_set_preferred_interpreter(CHIMARA_IF(glk), format, interpreter);
448 /* Display the new setting in the list */
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),
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)));
466 //g_settings_set(theapp->prefs_settings, "preferred-interpreters", "a{ss}", builder);
467 //g_variant_builder_unref(builder);