1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
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)
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.
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>
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
;
58 on_editor_linenos_activate (GtkAction
*action
, gpointer user_data
)
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
);
69 on_editor_markers_activate (GtkAction
*action
, gpointer user_data
)
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
,
80 on_editor_whitespaces_activate (GtkAction
*action
, gpointer user_data
)
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
);
91 on_editor_eolchars_activate (GtkAction
*action
, gpointer user_data
)
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
,
102 on_editor_linewrap_activate (GtkAction
*action
, gpointer user_data
)
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
}
131 ui_states_init (SourceviewPlugin
* plugin
, AnjutaUI
*ui
)
133 static const gchar
*prefs
[] = {
142 for (i
= 0; i
< sizeof (prefs
)/sizeof(const gchar
*); i
++)
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
);
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
));
165 sourceview_plugin_activate (AnjutaPlugin
*obj
)
167 SourceviewPlugin
* plugin
= ANJUTA_PLUGIN_SOURCEVIEW (obj
);
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"),
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
);
186 sourceview_plugin_deactivate (AnjutaPlugin
*obj
)
188 SourceviewPlugin
* plugin
= ANJUTA_PLUGIN_SOURCEVIEW (obj
);
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
;
205 sourceview_plugin_finalize (GObject
*obj
)
207 /* Finalization codes here */
208 G_OBJECT_CLASS (parent_class
)->finalize (obj
);
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
);
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
;
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
,
246 const gchar
* filename
,
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();
254 sv
= sourceview_new(file
, filename
, plugin
);
256 gtk_source_buffer_set_style_scheme (GTK_SOURCE_BUFFER (sv
->priv
->document
),
257 gtk_source_style_scheme_manager_get_scheme (manager
,
259 g_free (current_style
);
260 return IANJUTA_EDITOR (sv
);
264 ieditor_factory_iface_init (IAnjutaEditorFactoryIface
*iface
)
266 iface
->new_editor
= ieditor_factory_new_editor
;
277 create_style_model (GSettings
* settings
, GtkTreeIter
** current
)
279 GtkListStore
* model
= gtk_list_store_new (3, G_TYPE_STRING
, 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
);
286 for (style
= styles
; *style
!= NULL
; style
++)
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
),
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
);
307 on_style_changed (GtkComboBox
* combo
, SourceviewPlugin
* plugin
)
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
,
318 scheme
= gtk_source_style_scheme_manager_get_scheme (manager
, id
);
320 g_settings_set_string (plugin
->settings
,
326 docman
= anjuta_shell_get_interface (shell
,
327 IAnjutaDocumentManager
, NULL
);
330 GList
* editors
= ianjuta_document_manager_get_doc_widgets (docman
, NULL
);
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
),
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
);
361 anjuta_preferences_add_from_builder (prefs
,
365 _("GtkSourceView Editor"),
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
);
391 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (plugin
->combo_styles
),
393 gtk_tree_iter_free (iter
);
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
);
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
);
423 ANJUTA_SIMPLE_PLUGIN (SourceviewPlugin
, sourceview_plugin
);