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.
34 #include <glib/gi18n.h>
36 #include <libchimara/chimara-glk.h>
37 #include <libchimara/chimara-if.h>
41 GObject *load_object(const gchar *name);
42 static GtkTextTag *current_tag;
44 static void style_tree_select_callback(GtkTreeSelection *selection, ChimaraGlk *glk);
46 /* Create the preferences dialog. */
48 preferences_create(ChimaraGlk *glk)
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;
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);
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);
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);
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);
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);
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);
88 g_signal_connect(selection, "changed", G_CALLBACK(style_tree_select_callback), glk);
92 style_tree_select_callback(GtkTreeSelection *selection, ChimaraGlk *glk)
94 GtkTreeIter child, parent;
95 gchar *child_name, *parent_name;
98 if( !gtk_tree_selection_get_selected(selection, &model, &child) )
101 gtk_tree_model_get(model, &child, 0, &child_name, -1);
103 if( !gtk_tree_model_iter_parent(model, &parent, &child) )
106 gtk_tree_model_get(model, &parent, 0, &parent_name, -1);
107 if( !strcmp(parent_name, "Text buffer") )
108 current_tag = chimara_glk_get_tag(glk, CHIMARA_GLK_TEXT_BUFFER, child_name);
110 current_tag = chimara_glk_get_tag(glk, CHIMARA_GLK_TEXT_GRID, child_name);
114 on_toggle_left(GtkToggleAction *action, ChimaraGlk *glk) {
115 /* No nothing if the button is deactivated */
116 if( !gtk_toggle_action_get_active(action) )
119 /* Untoggle other alignment options */
120 GtkToggleToolButton *center = GTK_TOGGLE_TOOL_BUTTON(load_object("toolbutton-center"));
121 GtkToggleToolButton *right = GTK_TOGGLE_TOOL_BUTTON(load_object("toolbutton-right"));
122 GtkToggleToolButton *justify = GTK_TOGGLE_TOOL_BUTTON(load_object("toolbutton-justify"));
123 gtk_toggle_tool_button_set_active(center, FALSE);
124 gtk_toggle_tool_button_set_active(right, FALSE);
125 gtk_toggle_tool_button_set_active(justify, FALSE);
127 g_object_set(current_tag, "justification", GTK_JUSTIFY_LEFT, "justification-set", TRUE, NULL);
128 chimara_glk_update_style(glk);
132 on_toggle_center(GtkToggleAction *action, ChimaraGlk *glk) {
133 if( !gtk_toggle_action_get_active(action) )
136 /* Untoggle other alignment options */
137 GtkToggleToolButton *left = GTK_TOGGLE_TOOL_BUTTON(load_object("toolbutton-left"));
138 GtkToggleToolButton *right = GTK_TOGGLE_TOOL_BUTTON(load_object("toolbutton-right"));
139 GtkToggleToolButton *justify = GTK_TOGGLE_TOOL_BUTTON(load_object("toolbutton-justify"));
140 gtk_toggle_tool_button_set_active(left, FALSE);
141 gtk_toggle_tool_button_set_active(right, FALSE);
142 gtk_toggle_tool_button_set_active(justify, FALSE);
144 g_object_set(current_tag, "justification", GTK_JUSTIFY_CENTER, "justification-set", TRUE, NULL);
145 chimara_glk_update_style(glk);
149 on_toggle_right(GtkToggleAction *action, ChimaraGlk *glk) {
150 if( !gtk_toggle_action_get_active(action) )
153 /* Untoggle other alignment options */
154 GtkToggleToolButton *left = GTK_TOGGLE_TOOL_BUTTON(load_object("toolbutton-left"));
155 GtkToggleToolButton *center = GTK_TOGGLE_TOOL_BUTTON(load_object("toolbutton-center"));
156 GtkToggleToolButton *justify = GTK_TOGGLE_TOOL_BUTTON(load_object("toolbutton-justify"));
157 gtk_toggle_tool_button_set_active(left, FALSE);
158 gtk_toggle_tool_button_set_active(center, FALSE);
159 gtk_toggle_tool_button_set_active(justify, FALSE);
161 g_object_set(current_tag, "justification", GTK_JUSTIFY_RIGHT, "justification-set", TRUE, NULL);
162 chimara_glk_update_style(glk);
166 on_toggle_justify(GtkToggleAction *action, ChimaraGlk *glk) {
167 if( !gtk_toggle_action_get_active(action) )
170 /* Untoggle other alignment options */
171 GtkToggleToolButton *left = GTK_TOGGLE_TOOL_BUTTON(load_object("toolbutton-left"));
172 GtkToggleToolButton *center = GTK_TOGGLE_TOOL_BUTTON(load_object("toolbutton-center"));
173 GtkToggleToolButton *right = GTK_TOGGLE_TOOL_BUTTON(load_object("toolbutton-right"));
174 gtk_toggle_tool_button_set_active(left, FALSE);
175 gtk_toggle_tool_button_set_active(center, FALSE);
176 gtk_toggle_tool_button_set_active(right, FALSE);
178 g_object_set(current_tag, "justification", GTK_JUSTIFY_FILL, "justification-set", TRUE, NULL);
179 chimara_glk_update_style(glk);
183 on_toggle_bold(GtkToggleAction *action, ChimaraGlk *glk) {
184 if( gtk_toggle_action_get_active(action) )
185 g_object_set(current_tag, "weight", PANGO_WEIGHT_BOLD, "weight-set", TRUE, NULL);
187 g_object_set(current_tag, "weight", PANGO_WEIGHT_NORMAL, "weight-set", TRUE, NULL);
189 chimara_glk_update_style(glk);
193 on_toggle_italic(GtkToggleAction *action, ChimaraGlk *glk) {
194 if( gtk_toggle_action_get_active(action) )
195 g_object_set(current_tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
197 g_object_set(current_tag, "style", PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
199 chimara_glk_update_style(glk);
203 on_toggle_underline(GtkToggleAction *action, ChimaraGlk *glk) {
204 if( gtk_toggle_action_get_active(action) )
205 g_object_set(current_tag, "underline", PANGO_UNDERLINE_SINGLE, "underline-set", TRUE, NULL);
207 g_object_set(current_tag, "underline", PANGO_UNDERLINE_NONE, "underline-set", TRUE, NULL);
209 chimara_glk_update_style(glk);
213 on_foreground_color_set(GtkColorButton *button, ChimaraGlk *glk)
216 gtk_color_button_get_color(button, &color);
217 g_object_set(current_tag, "foreground-gdk", &color, "foreground-set", TRUE, NULL);
218 chimara_glk_update_style(glk);
222 on_background_color_set(GtkColorButton *button, ChimaraGlk *glk)
225 gtk_color_button_get_color(button, &color);
226 g_object_set(current_tag, "background-gdk", &color, "background-set", TRUE, NULL);
227 chimara_glk_update_style(glk);
231 on_font_set(GtkFontButton *button, ChimaraGlk *glk)
233 const gchar *font_name = gtk_font_button_get_font_name(button);
234 PangoFontDescription *font_description = pango_font_description_from_string(font_name);
235 g_object_set(current_tag, "font-desc", font_description, NULL);
236 chimara_glk_update_style(glk);