Load preferred interpreters from preferences
[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 <glib.h>
34 #include <glib/gi18n.h>
35 #include <gtk/gtk.h>
36 #include <libchimara/chimara-glk.h>
37 #include <libchimara/chimara-if.h>
38 #include <config.h>
39 #include "error.h"
40
41 GObject *load_object(const gchar *name);
42 static GtkTextTag *current_tag;
43
44 static void style_tree_select_callback(GtkTreeSelection *selection, ChimaraGlk *glk);
45
46 /* Create the preferences dialog. */
47 void
48 preferences_create(ChimaraGlk *glk)
49 {
50         /* Initialize the tree of style names */
51         GtkTreeStore *style_list = gtk_tree_store_new(1, G_TYPE_STRING);
52         GtkTreeIter buffer, grid, buffer_child, grid_child;
53
54         gtk_tree_store_append(style_list, &buffer, NULL);
55         gtk_tree_store_append(style_list, &grid, NULL);
56         gtk_tree_store_set(style_list, &buffer, 0, "Text buffer", -1);
57         gtk_tree_store_set(style_list, &grid, 0, "Text grid", -1);
58
59         int i;
60     gint num_tags = chimara_glk_get_num_tag_names(glk);
61         const gchar **tag_names = chimara_glk_get_tag_names(glk);
62         for(i=0; i<num_tags; i++) {
63                 gtk_tree_store_append(style_list, &buffer_child, &buffer);
64                 gtk_tree_store_append(style_list, &grid_child, &grid);
65                 gtk_tree_store_set(style_list, &buffer_child, 0, tag_names[i], -1);
66                 gtk_tree_store_set(style_list, &grid_child, 0, tag_names[i], -1);
67         }
68
69         /* Attach the model to the treeview */
70         GtkTreeView *view = GTK_TREE_VIEW( load_object("style-treeview") );
71         gtk_tree_view_set_model(view, GTK_TREE_MODEL(style_list));
72         g_object_unref(style_list);
73
74         /* Set the columns */
75         GtkTreeViewColumn *column = gtk_tree_view_column_new();
76         gtk_tree_view_column_set_title(column, "Style Name");
77         gtk_tree_view_append_column(view, column);
78
79         /* Set the renderers */
80         GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
81         gtk_tree_view_column_pack_start(column, renderer, TRUE);
82         gtk_tree_view_column_add_attribute(column, renderer, "text", 0);
83
84         /* Set selection mode to single select */
85         GtkTreeSelection *selection = gtk_tree_view_get_selection(view);
86         gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
87
88         g_signal_connect(selection, "changed", G_CALLBACK(style_tree_select_callback), glk);
89
90         /* Bind the preferences to the entries in the preferences file */
91         extern GSettings *prefs_settings;
92         GObject *flep = G_OBJECT( load_object("flep") );
93         g_settings_bind(prefs_settings, "flep", flep, "active", G_SETTINGS_BIND_DEFAULT);
94         GtkFileChooser *blorb_chooser = GTK_FILE_CHOOSER( load_object("blorb_file_chooser") );
95         char *filename;
96         g_settings_get(prefs_settings, "resource-path", "ms", &filename);
97         if(filename) {
98                 gtk_file_chooser_set_filename(blorb_chooser, filename);
99                 g_free(filename);
100         }
101
102         /* Initialize the list of preferred interpreters */
103         GtkListStore *interp_list = GTK_LIST_STORE( load_object("interpreters") );
104         GVariantIter *iter;
105         char *format, *plugin;
106         g_settings_get(prefs_settings, "preferred-interpreters", "a{ss}", &iter);
107         while(g_variant_iter_loop(iter, "{ss}", &format, &plugin)) {
108                 GtkTreeIter tree_iter;
109                 gtk_list_store_append(interp_list, &tree_iter);
110                 gtk_list_store_set(interp_list, &tree_iter, 0, format, 1, plugin, -1);
111         }
112         g_variant_iter_free(iter);
113 }
114
115 static void
116 style_tree_select_callback(GtkTreeSelection *selection, ChimaraGlk *glk)
117 {
118         GtkTreeIter child, parent;
119         gchar *child_name, *parent_name;
120         GtkTreeModel *model;
121
122         if( !gtk_tree_selection_get_selected(selection, &model, &child) )
123                 return;
124
125         gtk_tree_model_get(model, &child, 0, &child_name, -1);
126                 
127         if( !gtk_tree_model_iter_parent(model, &parent, &child) )
128                 return;
129
130         gtk_tree_model_get(model, &parent, 0, &parent_name, -1);
131         if( !strcmp(parent_name, "Text buffer") ) 
132                 current_tag = chimara_glk_get_tag(glk, CHIMARA_GLK_TEXT_BUFFER, child_name);
133         else
134                 current_tag = chimara_glk_get_tag(glk, CHIMARA_GLK_TEXT_GRID, child_name);
135 }
136
137 void
138 on_toggle_left(GtkToggleToolButton *button, ChimaraGlk *glk) {
139         /* No nothing if the button is deactivated */
140         if( !gtk_toggle_tool_button_get_active(button) )
141                 return;
142
143         /* Untoggle other alignment options */
144         GtkToggleToolButton *center = GTK_TOGGLE_TOOL_BUTTON(load_object("toolbutton-center"));
145         GtkToggleToolButton *right = GTK_TOGGLE_TOOL_BUTTON(load_object("toolbutton-right"));
146         GtkToggleToolButton *justify = GTK_TOGGLE_TOOL_BUTTON(load_object("toolbutton-justify"));
147         gtk_toggle_tool_button_set_active(center, FALSE);
148         gtk_toggle_tool_button_set_active(right, FALSE);
149         gtk_toggle_tool_button_set_active(justify, FALSE);
150
151         g_object_set(current_tag, "justification", GTK_JUSTIFY_LEFT, "justification-set", TRUE, NULL);
152         chimara_glk_update_style(glk);
153 }
154
155 void
156 on_toggle_center(GtkToggleToolButton *button, ChimaraGlk *glk) {
157         if( !gtk_toggle_tool_button_get_active(button) )
158                 return;
159
160         /* Untoggle other alignment options */
161         GtkToggleToolButton *left = GTK_TOGGLE_TOOL_BUTTON(load_object("toolbutton-left"));
162         GtkToggleToolButton *right = GTK_TOGGLE_TOOL_BUTTON(load_object("toolbutton-right"));
163         GtkToggleToolButton *justify = GTK_TOGGLE_TOOL_BUTTON(load_object("toolbutton-justify"));
164         gtk_toggle_tool_button_set_active(left, FALSE);
165         gtk_toggle_tool_button_set_active(right, FALSE);
166         gtk_toggle_tool_button_set_active(justify, FALSE);
167
168         g_object_set(current_tag, "justification", GTK_JUSTIFY_CENTER, "justification-set", TRUE, NULL);
169         chimara_glk_update_style(glk);
170 }
171
172 void
173 on_toggle_right(GtkToggleToolButton *button, ChimaraGlk *glk) {
174         if( !gtk_toggle_tool_button_get_active(button) )
175                 return;
176
177         /* Untoggle other alignment options */
178         GtkToggleToolButton *left = GTK_TOGGLE_TOOL_BUTTON(load_object("toolbutton-left"));
179         GtkToggleToolButton *center = GTK_TOGGLE_TOOL_BUTTON(load_object("toolbutton-center"));
180         GtkToggleToolButton *justify = GTK_TOGGLE_TOOL_BUTTON(load_object("toolbutton-justify"));
181         gtk_toggle_tool_button_set_active(left, FALSE);
182         gtk_toggle_tool_button_set_active(center, FALSE);
183         gtk_toggle_tool_button_set_active(justify, FALSE);
184
185         g_object_set(current_tag, "justification", GTK_JUSTIFY_RIGHT, "justification-set", TRUE, NULL);
186         chimara_glk_update_style(glk);
187 }
188
189 void
190 on_toggle_justify(GtkToggleToolButton *button, ChimaraGlk *glk) {
191         if( !gtk_toggle_tool_button_get_active(button) )
192                 return;
193
194         /* Untoggle other alignment options */
195         GtkToggleToolButton *left = GTK_TOGGLE_TOOL_BUTTON(load_object("toolbutton-left"));
196         GtkToggleToolButton *center = GTK_TOGGLE_TOOL_BUTTON(load_object("toolbutton-center"));
197         GtkToggleToolButton *right = GTK_TOGGLE_TOOL_BUTTON(load_object("toolbutton-right"));
198         gtk_toggle_tool_button_set_active(left, FALSE);
199         gtk_toggle_tool_button_set_active(center, FALSE);
200         gtk_toggle_tool_button_set_active(right, FALSE);
201
202         g_object_set(current_tag, "justification", GTK_JUSTIFY_FILL, "justification-set", TRUE, NULL);
203         chimara_glk_update_style(glk);
204 }
205
206 void
207 on_toggle_bold(GtkToggleToolButton *button, ChimaraGlk *glk) {
208         if( gtk_toggle_tool_button_get_active(button) )
209                 g_object_set(current_tag, "weight", PANGO_WEIGHT_BOLD, "weight-set", TRUE, NULL);
210         else
211                 g_object_set(current_tag, "weight", PANGO_WEIGHT_NORMAL, "weight-set", TRUE, NULL);
212
213         chimara_glk_update_style(glk);
214 }
215
216 void
217 on_toggle_italic(GtkToggleToolButton *button, ChimaraGlk *glk) {
218         if( gtk_toggle_tool_button_get_active(button) )
219                 g_object_set(current_tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
220         else
221                 g_object_set(current_tag, "style", PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
222
223         chimara_glk_update_style(glk);
224 }
225
226 void
227 on_toggle_underline(GtkToggleToolButton *button, ChimaraGlk *glk) {
228         if( gtk_toggle_tool_button_get_active(button) )
229                 g_object_set(current_tag, "underline", PANGO_UNDERLINE_SINGLE, "underline-set", TRUE, NULL);
230         else
231                 g_object_set(current_tag, "underline", PANGO_UNDERLINE_NONE, "underline-set", TRUE, NULL);
232
233         chimara_glk_update_style(glk);
234 }
235
236 void
237 on_foreground_color_set(GtkColorButton *button, ChimaraGlk *glk)
238 {
239         GdkColor color;
240     gtk_color_button_get_color(button, &color);
241         g_object_set(current_tag, "foreground-gdk", &color, "foreground-set", TRUE, NULL);
242         chimara_glk_update_style(glk);
243 }
244
245 void
246 on_background_color_set(GtkColorButton *button, ChimaraGlk *glk)
247 {
248         GdkColor color;
249     gtk_color_button_get_color(button, &color);
250         g_object_set(current_tag, "background-gdk", &color, "background-set", TRUE, NULL);
251         chimara_glk_update_style(glk);
252 }
253
254 void
255 on_font_set(GtkFontButton *button, ChimaraGlk *glk)
256 {
257         const gchar *font_name = gtk_font_button_get_font_name(button);
258         PangoFontDescription *font_description = pango_font_description_from_string(font_name);
259         g_object_set(current_tag, "font-desc", font_description, NULL);
260         chimara_glk_update_style(glk);
261 }
262
263 void
264 on_resource_file_set(GtkFileChooserButton *button, ChimaraGlk *glk)
265 {
266         extern GSettings *prefs_settings;
267         char *filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(button) );
268         g_settings_set(prefs_settings, "resource-path", "ms", filename);
269         g_free(filename);
270 }