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.
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 "sourceview-provider.h"
20 #include <gtksourceview/gtksource.h>
21 #include <gtksourceview/completion-providers/words/gtksourcecompletionwords.h>
23 #include <libanjuta/anjuta-debug.h>
24 #include <libanjuta/interfaces/ianjuta-editor.h>
26 #define REGISTER_NOTIFY(settings, key, func) \
27 g_signal_connect_object (settings, "changed::" key, G_CALLBACK(func), sv, 0);
29 #define ANJUTA_PREF_SCHEMA_PREFIX "org.gnome.anjuta."
30 #define PREF_SCHEMA "org.gnome.anjuta.plugins.sourceview"
31 #define MSGMAN_PREF_SCHEMA "org.gnome.anjuta.plugins.message-manager"
33 /* Editor preferences */
34 #define HIGHLIGHT_SYNTAX "syntax-highlight"
35 #define HIGHLIGHT_CURRENT_LINE "currentline-highlight"
36 #define HIGHLIGHT_BRACKETS "brackets-highlight"
37 #define INDENT_SIZE "indent-size"
38 #define AUTOCOMPLETION "autocomplete"
40 #define VIEW_RIGHTMARGIN "rightmargin-visible"
41 #define RIGHTMARGIN_POSITION "rightmargin-position"
43 #define MSGMAN_COLOR_ERROR "color-error"
44 #define MSGMAN_COLOR_WARNING "color-warning"
45 #define MSGMAN_COLOR_IMPORTANT "color-important"
47 #define FONT_THEME "font-use-theme"
49 #define FONT_SCHEMA "org.gnome.desktop.interface"
50 #define GNOME_MONOSPACE_FONT "monospace-font-name"
53 on_notify_view_spaces (GSettings
* settings
,
58 sv
= ANJUTA_SOURCEVIEW(user_data
);
59 GtkSourceDrawSpacesFlags flags
=
60 gtk_source_view_get_draw_spaces (GTK_SOURCE_VIEW (sv
->priv
->view
));
62 if (g_settings_get_boolean (settings
, key
))
63 flags
|= (GTK_SOURCE_DRAW_SPACES_SPACE
| GTK_SOURCE_DRAW_SPACES_TAB
);
65 flags
&= ~(GTK_SOURCE_DRAW_SPACES_SPACE
| GTK_SOURCE_DRAW_SPACES_TAB
);
67 gtk_source_view_set_draw_spaces (GTK_SOURCE_VIEW(sv
->priv
->view
),
72 on_notify_view_eol (GSettings
* settings
,
77 sv
= ANJUTA_SOURCEVIEW(user_data
);
78 GtkSourceDrawSpacesFlags flags
=
79 gtk_source_view_get_draw_spaces (GTK_SOURCE_VIEW (sv
->priv
->view
));
81 if (g_settings_get_boolean (settings
, key
))
82 flags
|= GTK_SOURCE_DRAW_SPACES_NEWLINE
;
84 flags
&= ~GTK_SOURCE_DRAW_SPACES_NEWLINE
;
86 gtk_source_view_set_draw_spaces (GTK_SOURCE_VIEW(sv
->priv
->view
),
91 on_notify_line_wrap (GSettings
* settings
,
96 sv
= ANJUTA_SOURCEVIEW(user_data
);
98 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (sv
->priv
->view
),
99 g_settings_get_boolean (settings
, key
) ? GTK_WRAP_WORD
: GTK_WRAP_NONE
);
103 on_notify_use_tab_for_indentation (GSettings
* settings
,
108 sv
= ANJUTA_SOURCEVIEW(user_data
);
110 gtk_source_view_set_insert_spaces_instead_of_tabs(GTK_SOURCE_VIEW(sv
->priv
->view
),
111 !g_settings_get_boolean (settings
, key
));
115 on_notify_autocompletion (GSettings
* settings
,
120 sv
= ANJUTA_SOURCEVIEW(user_data
);
121 GtkSourceCompletion
* completion
= gtk_source_view_get_completion(GTK_SOURCE_VIEW(sv
->priv
->view
));
123 if (g_settings_get_boolean (settings
, key
))
125 DEBUG_PRINT ("Register word completion provider");
126 GtkSourceCompletionWords
*prov_words
;
127 prov_words
= gtk_source_completion_words_new (NULL
, NULL
);
129 gtk_source_completion_words_register (prov_words
,
130 gtk_text_view_get_buffer (GTK_TEXT_VIEW (sv
->priv
->view
)));
132 gtk_source_completion_add_provider (completion
,
133 GTK_SOURCE_COMPLETION_PROVIDER (prov_words
),
139 for (node
= gtk_source_completion_get_providers(completion
); node
!= NULL
; node
= g_list_next (node
))
141 if (GTK_SOURCE_IS_COMPLETION_WORDS(node
->data
))
143 DEBUG_PRINT ("Unregister word completion provider");
144 gtk_source_completion_words_unregister (GTK_SOURCE_COMPLETION_WORDS(node
->data
),
145 gtk_text_view_get_buffer (GTK_TEXT_VIEW (sv
->priv
->view
)));
146 gtk_source_completion_remove_provider(completion
, GTK_SOURCE_COMPLETION_PROVIDER(node
->data
), NULL
);
154 on_notify_font (GSettings
* settings
,
159 sv
= ANJUTA_SOURCEVIEW(user_data
);
160 gchar
* font
= g_settings_get_string (settings
, key
);
162 anjuta_view_set_font(sv
->priv
->view
, FALSE
,
168 on_notify_font_theme (GSettings
* settings
,
173 sv
= ANJUTA_SOURCEVIEW(user_data
);
175 if (g_settings_get_boolean (settings
, key
))
177 GSettings
* font_settings
= g_settings_new (FONT_SCHEMA
);
178 gchar
* desktop_font
= g_settings_get_string (font_settings
,
179 GNOME_MONOSPACE_FONT
);
182 anjuta_view_set_font(sv
->priv
->view
, FALSE
, desktop_font
);
184 anjuta_view_set_font(sv
->priv
->view
, TRUE
, NULL
);
185 g_free (desktop_font
);
186 g_object_unref (font_settings
);
190 gchar
* font
= g_settings_get_string (settings
, FONT
);
191 anjuta_view_set_font(sv
->priv
->view
, FALSE
, font
);
196 /* Preferences notifications */
198 on_notify_indic_colors (GSettings
* settings
,
203 g_settings_get_string (settings
,
205 char* warning_color
=
206 g_settings_get_string (settings
,
207 MSGMAN_COLOR_WARNING
);
208 char* important_color
=
209 g_settings_get_string (settings
,
210 MSGMAN_COLOR_IMPORTANT
);
211 Sourceview
* sv
= ANJUTA_SOURCEVIEW (user_data
);
213 g_object_set (sv
->priv
->warning_indic
, "foreground", warning_color
, NULL
);
214 g_object_set (sv
->priv
->critical_indic
, "foreground", error_color
, NULL
);
215 g_object_set (sv
->priv
->important_indic
, "background", important_color
, NULL
);
217 g_free (error_color
);
218 g_free (warning_color
);
219 g_free (important_color
);
223 init_fonts(Sourceview
* sv
)
225 on_notify_font_theme (sv
->priv
->settings
, FONT_THEME
, sv
);
229 sourceview_prefs_init(Sourceview
* sv
)
231 GtkSourceDrawSpacesFlags flags
= 0;
232 /* We create a new GSettings object here because if we used the one from
233 * the editor might be destroyed while the plugin is still alive
235 sv
->priv
->settings
= g_settings_new (PREF_SCHEMA
);
236 sv
->priv
->msgman_settings
= g_settings_new (MSGMAN_PREF_SCHEMA
);
237 sv
->priv
->editor_settings
= g_settings_new (ANJUTA_PREF_SCHEMA_PREFIX IANJUTA_EDITOR_PREF_SCHEMA
);
239 /* Bind simple options to GSettings */
240 g_settings_bind (sv
->priv
->settings
, HIGHLIGHT_SYNTAX
,
241 sv
->priv
->document
, "highlight-syntax",
242 G_SETTINGS_BIND_GET
);
243 g_settings_bind (sv
->priv
->settings
, HIGHLIGHT_CURRENT_LINE
,
244 sv
->priv
->view
, "highlight-current-line",
245 G_SETTINGS_BIND_GET
);
246 g_settings_bind (sv
->priv
->editor_settings
, IANJUTA_EDITOR_TAB_WIDTH_KEY
,
247 sv
->priv
->view
, "tab-width",
248 G_SETTINGS_BIND_GET
);
249 g_settings_bind (sv
->priv
->editor_settings
, IANJUTA_EDITOR_INDENT_WIDTH_KEY
,
250 sv
->priv
->view
, "indent-width",
251 G_SETTINGS_BIND_GET
);
252 g_settings_bind (sv
->priv
->settings
, HIGHLIGHT_BRACKETS
,
253 sv
->priv
->document
, "highlight-matching-brackets",
254 G_SETTINGS_BIND_GET
);
256 g_settings_bind (sv
->priv
->settings
, VIEW_MARKS
,
257 sv
->priv
->view
, "show-line-marks",
258 G_SETTINGS_BIND_GET
);
260 g_settings_bind (sv
->priv
->settings
, RIGHTMARGIN_POSITION
,
261 sv
->priv
->view
, "right-margin-position",
262 G_SETTINGS_BIND_GET
);
264 g_settings_bind (sv
->priv
->settings
, VIEW_RIGHTMARGIN
,
265 sv
->priv
->view
, "show-right-margin",
266 G_SETTINGS_BIND_GET
);
268 g_settings_bind (sv
->priv
->settings
, VIEW_LINENUMBERS
,
269 sv
->priv
->view
, "show-line-numbers",
270 G_SETTINGS_BIND_GET
);
272 /* Init non-simple options */
273 gtk_source_view_set_indent_width(GTK_SOURCE_VIEW(sv
->priv
->view
),
274 g_settings_get_int (sv
->priv
->editor_settings
, IANJUTA_EDITOR_INDENT_WIDTH_KEY
));
275 gtk_source_view_set_tab_width(GTK_SOURCE_VIEW(sv
->priv
->view
),
276 g_settings_get_int (sv
->priv
->editor_settings
, IANJUTA_EDITOR_TAB_WIDTH_KEY
));
277 gtk_source_view_set_insert_spaces_instead_of_tabs(GTK_SOURCE_VIEW(sv
->priv
->view
),
278 !g_settings_get_boolean (sv
->priv
->editor_settings
, IANJUTA_EDITOR_USE_TABS_KEY
));
280 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (sv
->priv
->view
),
281 g_settings_get_boolean (sv
->priv
->settings
, VIEW_EOL
) ? GTK_WRAP_WORD
: GTK_WRAP_NONE
);
284 if (g_settings_get_boolean (sv
->priv
->settings
, VIEW_WHITE_SPACES
))
285 flags
|= (GTK_SOURCE_DRAW_SPACES_SPACE
| GTK_SOURCE_DRAW_SPACES_TAB
);
286 if (g_settings_get_boolean (sv
->priv
->settings
, VIEW_EOL
))
287 flags
|= GTK_SOURCE_DRAW_SPACES_NEWLINE
;
289 gtk_source_view_set_draw_spaces (GTK_SOURCE_VIEW (sv
->priv
->view
),
294 on_notify_autocompletion(sv
->priv
->settings
, AUTOCOMPLETION
, sv
);
296 /* Register notifications */
297 REGISTER_NOTIFY (sv
->priv
->editor_settings
, IANJUTA_EDITOR_USE_TABS_KEY
, on_notify_use_tab_for_indentation
);
299 REGISTER_NOTIFY (sv
->priv
->settings
, AUTOCOMPLETION
, on_notify_autocompletion
);
300 REGISTER_NOTIFY (sv
->priv
->settings
, VIEW_WHITE_SPACES
, on_notify_view_spaces
);
301 REGISTER_NOTIFY (sv
->priv
->settings
, VIEW_EOL
, on_notify_view_eol
);
302 REGISTER_NOTIFY (sv
->priv
->settings
, VIEW_LINE_WRAP
, on_notify_line_wrap
);
303 REGISTER_NOTIFY (sv
->priv
->settings
, FONT_THEME
, on_notify_font_theme
);
304 REGISTER_NOTIFY (sv
->priv
->settings
, FONT
, on_notify_font
);
306 g_signal_connect (sv
->priv
->msgman_settings
, "changed::" MSGMAN_COLOR_ERROR
,
307 G_CALLBACK (on_notify_indic_colors
), sv
);
308 g_signal_connect (sv
->priv
->msgman_settings
, "changed::" MSGMAN_COLOR_WARNING
,
309 G_CALLBACK (on_notify_indic_colors
), sv
);
310 g_signal_connect (sv
->priv
->msgman_settings
, "changed::" MSGMAN_COLOR_IMPORTANT
,
311 G_CALLBACK (on_notify_indic_colors
), sv
);
314 void sourceview_prefs_destroy(Sourceview
* sv
)
316 g_clear_object (&sv
->priv
->settings
);
317 g_clear_object (&sv
->priv
->msgman_settings
);
318 g_clear_object (&sv
->priv
->editor_settings
);