document-manager: Fixes on find in files
[anjuta.git] / plugins / sourceview / plugin.c
blobc2fa184db6cf7cbcb3643c1ce923a8450ef22c64
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * plugin.c
4 * Copyright (C) Johannes Schmid 2005 <jhs@gnome.org>
6 * plugin.c is free software.
8 * You may redistribute it and/or modify it under the terms of the
9 * GNU General Public License, as published by the Free Software
10 * Foundation; either version 2 of the License, or (at your option)
11 * any later version.
13 * plugin.c is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with plugin.c. If not, write to:
20 * The Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301, USA.
25 #include <config.h>
26 #include <libanjuta/anjuta-shell.h>
27 #include <libanjuta/anjuta-debug.h>
28 #include <libanjuta/anjuta-preferences.h>
29 #include <libanjuta/anjuta-status.h>
30 #include <libanjuta/interfaces/ianjuta-document-manager.h>
31 #include <libanjuta/interfaces/ianjuta-editor-factory.h>
32 #include <libanjuta/interfaces/ianjuta-editor.h>
33 #include <libanjuta/interfaces/ianjuta-preferences.h>
34 #include <gtksourceview/gtksourceview.h>
35 #include <gtksourceview/gtksourcestyleschememanager.h>
37 #include "plugin.h"
38 #include "sourceview.h"
39 #include "sourceview-prefs.h"
40 #include "sourceview-private.h"
42 #define PREF_SCHEMA "org.gnome.anjuta.plugins.sourceview"
43 #define PREFS_GLADE PACKAGE_DATA_DIR"/glade/anjuta-editor-sourceview.ui"
44 #define ICON_FILE "anjuta-editor-sourceview-plugin-48.png"
45 #define UI_FILE PACKAGE_DATA_DIR"/ui/anjuta-sourceview.xml"
47 #define COMBO_STYLES "combo_styles"
48 #define SOURCEVIEW_STYLE "style"
50 #define FONT_USE_THEME_BUTTON "preferences_toggle:bool:1:0:font-use-theme"
51 #define FONT_BUTTON "preferences_font:font:Monospace 12:0:font"
53 static gpointer parent_class;
55 static GtkBuilder* builder = NULL;
57 static void
58 on_editor_linenos_activate (GtkAction *action, gpointer user_data)
60 gboolean state;
61 SourceviewPlugin* plugin = ANJUTA_PLUGIN_SOURCEVIEW (user_data);
63 state = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
64 g_settings_set_boolean (plugin->settings,
65 VIEW_LINENUMBERS, state);
68 static void
69 on_editor_markers_activate (GtkAction *action, gpointer user_data)
71 gboolean state;
72 SourceviewPlugin* plugin = ANJUTA_PLUGIN_SOURCEVIEW (user_data);
74 state = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
75 g_settings_set_boolean (plugin->settings,
76 VIEW_MARKS, state);
79 static void
80 on_editor_whitespaces_activate (GtkAction *action, gpointer user_data)
82 gboolean state;
83 SourceviewPlugin* plugin = ANJUTA_PLUGIN_SOURCEVIEW (user_data);
85 state = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
86 g_settings_set_boolean (plugin->settings,
87 VIEW_WHITE_SPACES, state);
90 static void
91 on_editor_eolchars_activate (GtkAction *action, gpointer user_data)
93 gboolean state;
94 SourceviewPlugin* plugin = ANJUTA_PLUGIN_SOURCEVIEW (user_data);
96 state = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
97 g_settings_set_boolean (plugin->settings,
98 VIEW_EOL, state);
101 static void
102 on_editor_linewrap_activate (GtkAction *action, gpointer user_data)
104 gboolean state;
105 SourceviewPlugin* plugin = ANJUTA_PLUGIN_SOURCEVIEW (user_data);
107 state = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
108 g_settings_set_boolean (plugin->settings,
109 VIEW_LINE_WRAP, state);
112 static GtkToggleActionEntry actions_view[] = {
113 { "ActionViewEditorLinenumbers", NULL, N_("_Line Number Margin"), NULL,
114 N_("Show/Hide line numbers"),
115 G_CALLBACK (on_editor_linenos_activate), FALSE},
116 { "ActionViewEditorMarkers", NULL, N_("_Marker Margin"), NULL,
117 N_("Show/Hide marker margin"),
118 G_CALLBACK (on_editor_markers_activate), FALSE},
119 { "ActionViewEditorSpaces", NULL, N_("_White Space"), NULL,
120 N_("Show/Hide white spaces"),
121 G_CALLBACK (on_editor_whitespaces_activate), FALSE},
122 { "ActionViewEditorEOL", NULL, N_("_Line End Characters"), NULL,
123 N_("Show/Hide line end characters"),
124 G_CALLBACK (on_editor_eolchars_activate), FALSE},
125 { "ActionViewEditorWrapping", NULL, N_("Line _Wrapping"), NULL,
126 N_("Enable/disable line wrapping"),
127 G_CALLBACK (on_editor_linewrap_activate), FALSE}
130 static void
131 ui_states_init (SourceviewPlugin* plugin, AnjutaUI *ui)
133 static const gchar *prefs[] = {
134 VIEW_LINENUMBERS,
135 VIEW_MARKS,
136 VIEW_WHITE_SPACES,
137 VIEW_EOL,
138 VIEW_LINE_WRAP
140 gint i;
142 for (i = 0; i < sizeof (prefs)/sizeof(const gchar *); i++)
144 GtkAction *action;
145 gboolean state;
147 state = g_settings_get_boolean (plugin->settings, prefs[i]);
148 action = anjuta_ui_get_action (ui, "ActionGroupEditorView",
149 actions_view[i].name);
150 g_object_set (G_OBJECT (action), "sensitive", TRUE, "visible", TRUE, NULL);
151 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), state);
156 static void
157 on_font_check_toggled(GtkToggleButton* button, GtkBuilder* builder)
159 GtkWidget* font_button;
160 font_button = GTK_WIDGET (gtk_builder_get_object (builder, FONT_BUTTON));
161 gtk_widget_set_sensitive(font_button, !gtk_toggle_button_get_active(button));
164 static gboolean
165 sourceview_plugin_activate (AnjutaPlugin *obj)
167 SourceviewPlugin* plugin = ANJUTA_PLUGIN_SOURCEVIEW (obj);
168 AnjutaUI *ui;
170 DEBUG_PRINT ("%s", "SourceviewPlugin: Activating SourceviewPlugin plugin ...");
172 /* Add menu entries */
173 ui = anjuta_shell_get_ui (ANJUTA_PLUGIN (plugin)->shell, NULL);
174 plugin->group = anjuta_ui_add_toggle_action_group_entries (ui, "ActionGroupEditorView",
175 _("Editor view settings"),
176 actions_view,
177 G_N_ELEMENTS (actions_view),
178 GETTEXT_PACKAGE, TRUE, plugin);
179 ui_states_init (plugin, ui);
180 plugin->uiid = anjuta_ui_merge (ui, UI_FILE);
182 return TRUE;
185 static gboolean
186 sourceview_plugin_deactivate (AnjutaPlugin *obj)
188 SourceviewPlugin* plugin = ANJUTA_PLUGIN_SOURCEVIEW (obj);
189 AnjutaUI *ui;
191 DEBUG_PRINT ("%s", "SourceviewPlugin: Dectivating SourceviewPlugin plugin ...");
193 ui = anjuta_shell_get_ui (ANJUTA_PLUGIN (plugin)->shell, NULL);
194 anjuta_ui_unmerge (ui, plugin->uiid);
195 if (plugin->group != NULL)
197 anjuta_ui_remove_action_group (ui, plugin->group);
198 plugin->group = NULL;
201 return TRUE;
204 static void
205 sourceview_plugin_finalize (GObject *obj)
207 /* Finalization codes here */
208 G_OBJECT_CLASS (parent_class)->finalize (obj);
211 static void
212 sourceview_plugin_dispose (GObject *obj)
214 SourceviewPlugin* plugin = ANJUTA_PLUGIN_SOURCEVIEW (obj);
216 g_object_unref (plugin->settings);
218 G_OBJECT_CLASS (parent_class)->dispose (obj);
221 static void
222 sourceview_plugin_instance_init (GObject *obj)
224 SourceviewPlugin* plugin = ANJUTA_PLUGIN_SOURCEVIEW (obj);
225 plugin->settings = g_settings_new (PREF_SCHEMA);
226 plugin->group = NULL;
227 plugin->uiid = 0;
230 static void
231 sourceview_plugin_class_init (GObjectClass *klass)
233 AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
235 parent_class = g_type_class_peek_parent (klass);
237 plugin_class->activate = sourceview_plugin_activate;
238 plugin_class->deactivate = sourceview_plugin_deactivate;
239 klass->finalize = sourceview_plugin_finalize;
240 klass->dispose = sourceview_plugin_dispose;
243 static IAnjutaEditor*
244 ieditor_factory_new_editor(IAnjutaEditorFactory* factory,
245 GFile* file,
246 const gchar* filename,
247 GError** error)
249 AnjutaPlugin* plugin = ANJUTA_PLUGIN(factory);
250 SourceviewPlugin* splugin = ANJUTA_PLUGIN_SOURCEVIEW (plugin);
251 gchar* current_style = g_settings_get_string (splugin->settings, SOURCEVIEW_STYLE);
252 GtkSourceStyleSchemeManager* manager = gtk_source_style_scheme_manager_get_default();
253 Sourceview* sv;
254 sv = sourceview_new(file, filename, plugin);
255 if (current_style)
256 gtk_source_buffer_set_style_scheme (GTK_SOURCE_BUFFER (sv->priv->document),
257 gtk_source_style_scheme_manager_get_scheme (manager,
258 current_style));
259 g_free (current_style);
260 return IANJUTA_EDITOR (sv);
263 static void
264 ieditor_factory_iface_init (IAnjutaEditorFactoryIface *iface)
266 iface->new_editor = ieditor_factory_new_editor;
269 enum
271 COLUMN_NAME = 0,
272 COLUMN_DESC,
273 COLUMN_ID
276 static GtkTreeModel*
277 create_style_model (GSettings* settings, GtkTreeIter** current)
279 GtkListStore* model = gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING,
280 G_TYPE_STRING);
281 GtkSourceStyleSchemeManager* manager = gtk_source_style_scheme_manager_get_default();
282 const gchar* const *styles = gtk_source_style_scheme_manager_get_scheme_ids (manager);
283 const gchar* const *style;
284 gchar* current_style = g_settings_get_string (settings, SOURCEVIEW_STYLE);
285 *current = NULL;
286 for (style = styles; *style != NULL; style++)
288 GtkTreeIter iter;
289 GtkSourceStyleScheme* scheme =
290 gtk_source_style_scheme_manager_get_scheme (manager, *style);
291 const gchar* id = gtk_source_style_scheme_get_id (scheme);
292 gtk_list_store_append (model, &iter);
293 gtk_list_store_set (model, &iter,
294 COLUMN_NAME, gtk_source_style_scheme_get_name (scheme),
295 COLUMN_DESC, gtk_source_style_scheme_get_description (scheme),
296 COLUMN_ID, id, -1);
297 if (current_style && g_str_equal (id, current_style))
299 *current = gtk_tree_iter_copy (&iter);
302 g_free (current_style);
303 return GTK_TREE_MODEL (model);
306 static void
307 on_style_changed (GtkComboBox* combo, SourceviewPlugin* plugin)
309 GtkTreeIter iter;
310 gchar* id;
311 GtkSourceStyleSchemeManager* manager = gtk_source_style_scheme_manager_get_default();
312 GtkSourceStyleScheme* scheme;
313 IAnjutaDocumentManager* docman;
314 AnjutaShell* shell = ANJUTA_PLUGIN (plugin)->shell;
315 gtk_combo_box_get_active_iter (combo, &iter);
316 gtk_tree_model_get (gtk_combo_box_get_model(combo), &iter,
317 COLUMN_ID, &id, -1);
318 scheme = gtk_source_style_scheme_manager_get_scheme (manager, id);
320 g_settings_set_string (plugin->settings,
321 SOURCEVIEW_STYLE,
322 id);
323 g_free (id);
326 docman = anjuta_shell_get_interface (shell,
327 IAnjutaDocumentManager, NULL);
328 if (docman)
330 GList* editors = ianjuta_document_manager_get_doc_widgets (docman, NULL);
331 GList* node;
332 for (node = editors; node != NULL; node = g_list_next (node))
334 IAnjutaDocument* editor = IANJUTA_DOCUMENT (node->data);
335 if (ANJUTA_IS_SOURCEVIEW (editor))
337 Sourceview* sv = ANJUTA_SOURCEVIEW (editor);
338 gtk_source_buffer_set_style_scheme (GTK_SOURCE_BUFFER (sv->priv->document),
339 scheme);
345 static void
346 ipreferences_merge(IAnjutaPreferences* ipref, AnjutaPreferences* prefs, GError** e)
348 /* Add preferences */
349 SourceviewPlugin* plugin = ANJUTA_PLUGIN_SOURCEVIEW (ipref);
350 GtkCellRenderer* renderer_name = gtk_cell_renderer_text_new ();
351 GtkCellRenderer* renderer_desc = gtk_cell_renderer_text_new ();
352 GtkTreeIter* iter = NULL;
353 GError* error = NULL;
354 builder = gtk_builder_new ();
355 if (!gtk_builder_add_from_file(builder, PREFS_GLADE, &error))
357 DEBUG_PRINT ("Could load sourceview preferences: %s", error->message);
358 g_error_free (error);
359 return;
361 anjuta_preferences_add_from_builder (prefs,
362 builder,
363 plugin->settings,
364 "Editor",
365 _("GtkSourceView Editor"),
366 ICON_FILE);
368 plugin->check_font = GTK_WIDGET (gtk_builder_get_object (builder,
369 FONT_USE_THEME_BUTTON));
370 g_signal_connect(G_OBJECT(plugin->check_font), "toggled",
371 G_CALLBACK(on_font_check_toggled), builder);
372 on_font_check_toggled (GTK_TOGGLE_BUTTON (plugin->check_font), builder);
374 /* Init styles combo */
375 plugin->combo_styles = GTK_WIDGET (gtk_builder_get_object (builder, COMBO_STYLES));
376 gtk_combo_box_set_model (GTK_COMBO_BOX (plugin->combo_styles),
377 create_style_model(plugin->settings, &iter));
378 g_signal_connect (plugin->combo_styles, "changed", G_CALLBACK (on_style_changed), plugin);
380 gtk_cell_layout_clear (GTK_CELL_LAYOUT(plugin->combo_styles));
381 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT(plugin->combo_styles), renderer_name, TRUE);
382 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT(plugin->combo_styles), renderer_desc, FALSE);
383 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT(plugin->combo_styles), renderer_name,
384 "text", COLUMN_NAME);
385 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT(plugin->combo_styles), renderer_desc,
386 "text", COLUMN_DESC);
387 g_object_set (renderer_desc,
388 "style", PANGO_STYLE_ITALIC, NULL);
389 if (iter)
391 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (plugin->combo_styles),
392 iter);
393 gtk_tree_iter_free (iter);
397 static void
398 ipreferences_unmerge(IAnjutaPreferences* ipref, AnjutaPreferences* prefs, GError** e)
400 SourceviewPlugin* plugin = ANJUTA_PLUGIN_SOURCEVIEW (ipref);
401 g_signal_handlers_disconnect_by_func(G_OBJECT(plugin->check_font),
402 G_CALLBACK(on_font_check_toggled), builder);
403 g_signal_handlers_disconnect_by_func(G_OBJECT(plugin->combo_styles),
404 G_CALLBACK(on_style_changed), builder);
406 anjuta_preferences_remove_page(prefs, _("GtkSourceView Editor"));
407 g_object_unref(builder);
408 builder = NULL;
411 static void
412 ipreferences_iface_init(IAnjutaPreferencesIface* iface)
414 iface->merge = ipreferences_merge;
415 iface->unmerge = ipreferences_unmerge;
418 ANJUTA_PLUGIN_BEGIN (SourceviewPlugin, sourceview_plugin);
419 ANJUTA_TYPE_ADD_INTERFACE(ieditor_factory, IANJUTA_TYPE_EDITOR_FACTORY);
420 ANJUTA_TYPE_ADD_INTERFACE(ipreferences, IANJUTA_TYPE_PREFERENCES);
421 ANJUTA_PLUGIN_END;
423 ANJUTA_SIMPLE_PLUGIN (SourceviewPlugin, sourceview_plugin);