Updated Spanish translation
[anjuta-git-plugin.git] / plugins / sourceview / sourceview-prefs.c
blobc739075e944ffbb3430f0e424b9cfbf5156bebce
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Library General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 #include "sourceview-prefs.h"
18 #include "sourceview-private.h"
19 #include <gtksourceview/gtksourceview.h>
21 #include <libanjuta/anjuta-debug.h>
23 static AnjutaPreferences* prefs = NULL;
25 #define REGISTER_NOTIFY(key, func) \
26 notify_id = anjuta_preferences_notify_add (sv->priv->prefs, \
27 key, func, sv, NULL); \
28 sv->priv->gconf_notify_ids = g_list_prepend (sv->priv->gconf_notify_ids, \
29 GUINT_TO_POINTER(notify_id));
30 /* Editor preferences */
31 #define HIGHLIGHT_SYNTAX "sourceview.syntax.highlight"
32 #define HIGHLIGHT_CURRENT_LINE "sourceview.currentline.highlight"
33 #define USE_TABS "use.tabs"
34 #define HIGHLIGHT_BRACKETS "sourceview.brackets.highlight"
35 #define TAB_SIZE "tabsize"
36 #define INDENT_SIZE "indent.size"
38 #define VIEW_LINENUMBERS "sourceview.linenumbers.visible"
39 #define VIEW_MARKS "sourceview.marks.visible"
40 #define VIEW_RIGHTMARGIN "sourceview.rightmargin.visible"
41 #define RIGHTMARGIN_POSITION "sourceview.rightmargin.position"
43 #define COLOR_THEME "sourceview.color.use_theme"
44 #define COLOR_TEXT "sourceview.color.text"
45 #define COLOR_BACKGROUND "sourceview.color.background"
46 #define COLOR_SELECTED_TEXT "sourceview.color.selected_text"
47 #define COLOR_SELECTION "sourceview.color.selection"
49 #define FONT_THEME "sourceview.font.use_theme"
50 #define FONT "sourceview.font"
51 #define DESKTOP_FIXED_FONT "/desktop/gnome/interface/monospace_font_name"
53 static int
54 get_int(GConfEntry* entry)
56 GConfValue* value = gconf_entry_get_value(entry);
57 return gconf_value_get_int(value);
60 static gboolean
61 get_bool(GConfEntry* entry)
63 GConfValue* value = gconf_entry_get_value(entry);
64 /* Usually we would use get_bool but anjuta_preferences saves bool as int
65 #409408 */
66 if (value->type == GCONF_VALUE_BOOL)
67 return gconf_value_get_bool (value);
68 else
69 return gconf_value_get_int(value);
72 static void
73 on_gconf_notify_disable_hilite (GConfClient *gclient, guint cnxn_id,
74 GConfEntry *entry, gpointer user_data)
76 Sourceview *sv;
77 gboolean highlight = get_bool(entry);
78 sv = ANJUTA_SOURCEVIEW(user_data);
80 gtk_source_buffer_set_highlight_syntax(GTK_SOURCE_BUFFER(sv->priv->document),
81 highlight);
85 static void
86 on_gconf_notify_highlight_current_line (GConfClient *gclient, guint cnxn_id,
87 GConfEntry *entry, gpointer user_data)
89 Sourceview *sv;
90 gboolean highlight_current_line = get_bool(entry);
91 sv = ANJUTA_SOURCEVIEW(user_data);
93 gtk_source_view_set_highlight_current_line(GTK_SOURCE_VIEW(sv->priv->view),
94 highlight_current_line);
97 static void
98 on_gconf_notify_tab_size (GConfClient *gclient, guint cnxn_id,
99 GConfEntry *entry, gpointer user_data)
101 Sourceview *sv;
102 gint tab_size = get_int(entry);
104 sv = ANJUTA_SOURCEVIEW(user_data);
106 g_return_if_fail(GTK_IS_SOURCE_VIEW(sv->priv->view));
108 gtk_source_view_set_tab_width(GTK_SOURCE_VIEW(sv->priv->view), tab_size);
111 static void
112 on_gconf_notify_indent_size (GConfClient *gclient, guint cnxn_id,
113 GConfEntry *entry, gpointer user_data)
115 Sourceview *sv;
116 gint indent_width = get_int(entry);
118 sv = ANJUTA_SOURCEVIEW(user_data);
120 g_return_if_fail(GTK_IS_SOURCE_VIEW(sv->priv->view));
122 gtk_source_view_set_indent_width(GTK_SOURCE_VIEW(sv->priv->view), indent_width);
125 static void
126 on_gconf_notify_use_tab_for_indentation (GConfClient *gclient, guint cnxn_id,
127 GConfEntry *entry, gpointer user_data)
129 Sourceview *sv;
130 gboolean use_tabs = get_bool(entry);
131 sv = ANJUTA_SOURCEVIEW(user_data);
133 gtk_source_view_set_insert_spaces_instead_of_tabs(GTK_SOURCE_VIEW(sv->priv->view),
134 !use_tabs);
137 static void
138 on_gconf_notify_braces_check (GConfClient *gclient, guint cnxn_id,
139 GConfEntry *entry, gpointer user_data)
141 Sourceview *sv;
142 gboolean braces_check = get_bool(entry);
143 sv = ANJUTA_SOURCEVIEW(user_data);
145 gtk_source_buffer_set_highlight_matching_brackets(GTK_SOURCE_BUFFER(sv->priv->document),
146 braces_check);
149 static void
150 on_gconf_notify_view_marks (GConfClient *gclient, guint cnxn_id,
151 GConfEntry *entry, gpointer user_data)
153 Sourceview *sv;
154 gboolean show_markers = get_bool(entry);
155 sv = ANJUTA_SOURCEVIEW(user_data);
157 gtk_source_view_set_show_line_marks(GTK_SOURCE_VIEW(sv->priv->view),
158 show_markers);
162 static void
163 on_gconf_notify_view_linenums (GConfClient *gclient, guint cnxn_id,
164 GConfEntry *entry, gpointer user_data)
166 Sourceview *sv;
167 gboolean show_lineno = get_bool(entry);
168 sv = ANJUTA_SOURCEVIEW(user_data);
170 gtk_source_view_set_show_line_numbers(GTK_SOURCE_VIEW(sv->priv->view),
171 show_lineno);
175 static void
176 on_gconf_notify_view_right_margin (GConfClient *gclient, guint cnxn_id,
177 GConfEntry *entry, gpointer user_data)
179 Sourceview *sv;
180 gboolean show_margin = get_bool(entry);
181 sv = ANJUTA_SOURCEVIEW(user_data);
183 gtk_source_view_set_show_right_margin(GTK_SOURCE_VIEW(sv->priv->view),
184 show_margin);
188 static void
189 on_gconf_notify_right_margin_position (GConfClient *gclient, guint cnxn_id,
190 GConfEntry *entry, gpointer user_data)
192 Sourceview *sv;
193 gboolean pos = get_bool(entry);
194 sv = ANJUTA_SOURCEVIEW(user_data);
196 gtk_source_view_set_right_margin_position(GTK_SOURCE_VIEW(sv->priv->view),
197 pos);
201 static void
202 on_gconf_notify_color (GConfClient *gclient, guint cnxn_id,
203 GConfEntry *entry, gpointer user_data)
205 Sourceview *sv;
206 GdkColor *text, *background, *selected_text, *selection;
207 AnjutaPreferences* prefs = sourceview_get_prefs();
208 sv = ANJUTA_SOURCEVIEW(user_data);
210 if (!anjuta_preferences_get_int (prefs, COLOR_THEME))
212 text = anjuta_util_convert_color(prefs, COLOR_TEXT);
213 background = anjuta_util_convert_color(prefs, COLOR_BACKGROUND);
214 selected_text = anjuta_util_convert_color(prefs, COLOR_SELECTED_TEXT);
215 selection = anjuta_util_convert_color(prefs, COLOR_SELECTION);
216 anjuta_view_set_colors(sv->priv->view, FALSE, background, text, selection, selected_text);
220 static void
221 on_gconf_notify_color_theme (GConfClient *gclient, guint cnxn_id,
222 GConfEntry *entry, gpointer user_data)
224 Sourceview *sv;
225 gboolean use_theme = get_bool(entry);
226 sv = ANJUTA_SOURCEVIEW(user_data);
228 if (use_theme)
230 anjuta_view_set_colors(sv->priv->view, TRUE, NULL, NULL, NULL, NULL);
232 else
233 on_gconf_notify_color(NULL, 0, NULL, sv);
236 static void
237 on_gconf_notify_font (GConfClient *gclient, guint cnxn_id,
238 GConfEntry *entry, gpointer user_data)
240 Sourceview *sv;
241 gchar* font;
242 AnjutaPreferences* prefs = sourceview_get_prefs();
243 sv = ANJUTA_SOURCEVIEW(user_data);
245 font = anjuta_preferences_get(prefs, FONT);
247 anjuta_view_set_font(sv->priv->view, FALSE, font);
248 g_free (font);
251 static void
252 on_gconf_notify_font_theme (GConfClient *gclient, guint cnxn_id,
253 GConfEntry *entry, gpointer user_data)
255 Sourceview *sv;
256 gboolean use_theme = get_bool(entry);
257 sv = ANJUTA_SOURCEVIEW(user_data);
259 if (use_theme)
261 gchar *desktop_fixed_font;
262 desktop_fixed_font =
263 gconf_client_get_string (gclient, DESKTOP_FIXED_FONT, NULL);
264 if (desktop_fixed_font)
265 anjuta_view_set_font(sv->priv->view, FALSE, desktop_fixed_font);
266 else
267 anjuta_view_set_font(sv->priv->view, TRUE, NULL);
268 g_free (desktop_fixed_font);
270 else
271 on_gconf_notify_font(NULL, 0, NULL, sv);
274 static void
275 init_colors_and_fonts(Sourceview* sv)
277 gboolean font_theme;
278 gboolean color_theme;
280 font_theme = anjuta_preferences_get_int(prefs, FONT_THEME);
281 color_theme = anjuta_preferences_get_int(prefs, COLOR_THEME);
283 if (!font_theme)
285 on_gconf_notify_font(NULL, 0, NULL, sv);
287 else
289 GConfClient *gclient;
290 gchar *desktop_fixed_font;
292 gclient = gconf_client_get_default ();
293 desktop_fixed_font =
294 gconf_client_get_string (gclient, DESKTOP_FIXED_FONT, NULL);
295 if (desktop_fixed_font)
296 anjuta_view_set_font(sv->priv->view, FALSE, desktop_fixed_font);
297 else
298 anjuta_view_set_font(sv->priv->view, TRUE, NULL);
299 g_free (desktop_fixed_font);
300 g_object_unref (gclient);
303 if (!color_theme)
305 on_gconf_notify_color(NULL, 0, NULL, sv);
309 static int
310 get_key(Sourceview* sv, const gchar* key)
312 return anjuta_preferences_get_int (sv->priv->prefs, key);
315 void
316 sourceview_prefs_init(Sourceview* sv)
318 guint notify_id;
320 prefs = sv->priv->prefs;
322 /* Init */
323 gtk_source_buffer_set_highlight_syntax(GTK_SOURCE_BUFFER(sv->priv->document), get_key(sv, HIGHLIGHT_SYNTAX));
324 gtk_source_view_set_highlight_current_line(GTK_SOURCE_VIEW(sv->priv->view),
325 get_key(sv, HIGHLIGHT_CURRENT_LINE));
326 gtk_source_view_set_tab_width(GTK_SOURCE_VIEW(sv->priv->view), get_key(sv, TAB_SIZE));
327 gtk_source_view_set_indent_width(GTK_SOURCE_VIEW(sv->priv->view), get_key(sv, INDENT_SIZE));
328 gtk_source_view_set_insert_spaces_instead_of_tabs(GTK_SOURCE_VIEW(sv->priv->view),
329 !get_key(sv, USE_TABS));
330 gtk_source_buffer_set_highlight_matching_brackets(GTK_SOURCE_BUFFER(sv->priv->document),
331 get_key(sv, HIGHLIGHT_BRACKETS));
332 gtk_source_view_set_show_line_marks(GTK_SOURCE_VIEW(sv->priv->view),
333 get_key(sv, VIEW_MARKS));
334 gtk_source_view_set_show_line_numbers(GTK_SOURCE_VIEW(sv->priv->view),
335 get_key(sv, VIEW_LINENUMBERS));
336 gtk_source_view_set_show_right_margin(GTK_SOURCE_VIEW(sv->priv->view),
337 get_key(sv, VIEW_RIGHTMARGIN));
338 gtk_source_view_set_right_margin_position(GTK_SOURCE_VIEW(sv->priv->view),
339 get_key(sv, RIGHTMARGIN_POSITION));
342 init_colors_and_fonts(sv);
344 /* Register gconf notifications */
345 REGISTER_NOTIFY (TAB_SIZE, on_gconf_notify_tab_size);
346 REGISTER_NOTIFY (TAB_SIZE, on_gconf_notify_indent_size);
347 REGISTER_NOTIFY (USE_TABS, on_gconf_notify_use_tab_for_indentation);
348 REGISTER_NOTIFY (HIGHLIGHT_SYNTAX, on_gconf_notify_disable_hilite);
349 REGISTER_NOTIFY (HIGHLIGHT_CURRENT_LINE, on_gconf_notify_highlight_current_line);
350 REGISTER_NOTIFY (HIGHLIGHT_BRACKETS, on_gconf_notify_braces_check);
351 REGISTER_NOTIFY (VIEW_MARKS, on_gconf_notify_view_marks);
352 REGISTER_NOTIFY (VIEW_LINENUMBERS, on_gconf_notify_view_linenums);
353 REGISTER_NOTIFY (VIEW_RIGHTMARGIN, on_gconf_notify_view_right_margin);
354 REGISTER_NOTIFY (RIGHTMARGIN_POSITION, on_gconf_notify_right_margin_position);
355 REGISTER_NOTIFY (COLOR_THEME, on_gconf_notify_color_theme);
356 REGISTER_NOTIFY (COLOR_TEXT, on_gconf_notify_color);
357 REGISTER_NOTIFY (COLOR_BACKGROUND, on_gconf_notify_color);
358 REGISTER_NOTIFY (COLOR_SELECTED_TEXT, on_gconf_notify_color);
359 REGISTER_NOTIFY (COLOR_SELECTION, on_gconf_notify_color);
360 REGISTER_NOTIFY (FONT_THEME, on_gconf_notify_font_theme);
361 REGISTER_NOTIFY (FONT, on_gconf_notify_font);
365 void sourceview_prefs_destroy(Sourceview* sv)
367 AnjutaPreferences* prefs = sv->priv->prefs;
368 GList* id;
369 DEBUG_PRINT("Destroying preferences");
370 for (id = sv->priv->gconf_notify_ids; id != NULL; id = id->next)
372 anjuta_preferences_notify_remove(prefs,GPOINTER_TO_UINT(id->data));
374 g_list_free(sv->priv->gconf_notify_ids);
378 AnjutaPreferences*
379 sourceview_get_prefs()
381 return prefs;