Initial German translation of the build tutorial
[anjuta.git] / plugins / parser-cxx / plugin.c
blobcadbb1d73ef0e7ede37906976072f736e3e95f35
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 plugin.c
4 Copyright (C) 2000 Naba Kumar
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <config.h>
22 #include <ctype.h>
23 #include <stdlib.h>
24 #include <libanjuta/anjuta-shell.h>
25 #include <libanjuta/anjuta-debug.h>
26 #include <libanjuta/interfaces/ianjuta-editor-assist.h>
27 #include <libanjuta/interfaces/ianjuta-document.h>
28 #include <libanjuta/interfaces/ianjuta-document-manager.h>
29 #include <libanjuta/interfaces/ianjuta-preferences.h>
30 #include <libanjuta/interfaces/ianjuta-language.h>
32 #include "plugin.h"
34 #define PREFS_BUILDER PACKAGE_DATA_DIR"/glade/anjuta-parser-cxx.ui"
35 #define ICON_FILE "anjuta-parser-cxx-plugin.png"
37 /* Preferences keys */
38 #define ANJUTA_PREF_SCHEMA_PREFIX "org.gnome.anjuta."
39 #define PREF_SCHEMA "org.gnome.anjuta.plugins.parser-cxx"
41 static gpointer parent_class;
43 /* Enable/Disable language-support */
44 static void
45 install_support (ParserCxxPlugin *parser_plugin)
47 IAnjutaLanguage* lang_manager =
48 anjuta_shell_get_interface (ANJUTA_PLUGIN (parser_plugin)->shell,
49 IAnjutaLanguage, NULL);
51 if (!lang_manager)
52 return;
54 if (parser_plugin->support_installed)
55 return;
57 parser_plugin->current_language =
58 ianjuta_language_get_name_from_editor (lang_manager,
59 IANJUTA_EDITOR_LANGUAGE (parser_plugin->current_editor), NULL);
61 DEBUG_PRINT("Parser support installed for: %s",
62 parser_plugin->current_language);
64 if (parser_plugin->current_language &&
65 (g_str_equal (parser_plugin->current_language, "C" )
66 || g_str_equal (parser_plugin->current_language, "C++")))
68 ParserCxxAssist *assist;
70 g_assert (parser_plugin->assist == NULL);
72 assist = parser_cxx_assist_new (IANJUTA_EDITOR (parser_plugin->current_editor),
73 anjuta_shell_get_interface (
74 anjuta_plugin_get_shell (ANJUTA_PLUGIN (parser_plugin)),
75 IAnjutaSymbolManager, NULL),
76 parser_plugin->settings);
78 if (!assist)
79 return;
81 parser_plugin->assist = assist;
83 else
84 return;
86 parser_plugin->support_installed = TRUE;
89 static void
90 uninstall_support (ParserCxxPlugin *parser_plugin)
92 if (!parser_plugin->support_installed)
93 return;
95 if (parser_plugin->assist)
97 g_object_unref (parser_plugin->assist);
98 parser_plugin->assist = NULL;
101 parser_plugin->support_installed = FALSE;
104 static void
105 on_editor_language_changed (IAnjutaEditor *editor,
106 const gchar *new_language,
107 ParserCxxPlugin *plugin)
109 uninstall_support (plugin);
110 install_support (plugin);
113 static void
114 on_value_added_current_editor (AnjutaPlugin *plugin, const gchar *name,
115 const GValue *value, gpointer data)
117 ParserCxxPlugin *parser_plugin;
118 IAnjutaDocument* doc = IANJUTA_DOCUMENT(g_value_get_object (value));
119 parser_plugin = ANJUTA_PLUGIN_PARSER_CXX (plugin);
121 if (IANJUTA_IS_EDITOR(doc))
122 parser_plugin->current_editor = G_OBJECT(doc);
123 else
125 parser_plugin->current_editor = NULL;
126 return;
129 if (IANJUTA_IS_EDITOR(parser_plugin->current_editor))
130 install_support (parser_plugin);
132 g_signal_connect (parser_plugin->current_editor, "language-changed",
133 G_CALLBACK (on_editor_language_changed),
134 plugin);
137 static void
138 on_value_removed_current_editor (AnjutaPlugin *plugin, const gchar *name,
139 gpointer data)
141 ParserCxxPlugin *parser_plugin;
142 parser_plugin = ANJUTA_PLUGIN_PARSER_CXX (plugin);
144 if (parser_plugin->current_editor)
145 g_signal_handlers_disconnect_by_func (parser_plugin->current_editor,
146 G_CALLBACK (on_editor_language_changed),
147 plugin);
149 if (IANJUTA_IS_EDITOR(parser_plugin->current_editor))
150 uninstall_support (parser_plugin);
152 parser_plugin->current_editor = NULL;
155 static gboolean
156 parser_cxx_plugin_activate_plugin (AnjutaPlugin *plugin)
158 ParserCxxPlugin *parser_plugin;
160 parser_plugin = ANJUTA_PLUGIN_PARSER_CXX (plugin);
162 DEBUG_PRINT ("%s", "AnjutaParserCxxPlugin: Activating plugin ...");
164 parser_plugin->editor_watch_id =
165 anjuta_plugin_add_watch (plugin,
166 IANJUTA_DOCUMENT_MANAGER_CURRENT_DOCUMENT,
167 on_value_added_current_editor,
168 on_value_removed_current_editor,
169 plugin);
171 return TRUE;
174 static gboolean
175 parser_cxx_plugin_deactivate_plugin (AnjutaPlugin *plugin)
177 ParserCxxPlugin *parser_plugin;
178 parser_plugin = ANJUTA_PLUGIN_PARSER_CXX (plugin);
180 anjuta_plugin_remove_watch (plugin,
181 parser_plugin->editor_watch_id,
182 TRUE);
184 DEBUG_PRINT ("%s", "AnjutaParserCxxPlugin: Deactivated plugin.");
185 return TRUE;
188 static void
189 parser_cxx_plugin_finalize (GObject *obj)
191 /* Finalization codes here */
192 G_OBJECT_CLASS (parent_class)->finalize (obj);
195 static void
196 parser_cxx_plugin_dispose (GObject *obj)
198 ParserCxxPlugin* plugin = ANJUTA_PLUGIN_PARSER_CXX (obj);
200 /* Disposition codes */
201 g_object_unref (plugin->settings);
203 G_OBJECT_CLASS (parent_class)->dispose (obj);
206 static void
207 parser_cxx_plugin_instance_init (GObject *obj)
209 ParserCxxPlugin *plugin = ANJUTA_PLUGIN_PARSER_CXX (obj);
210 plugin->current_editor = NULL;
211 plugin->current_language = NULL;
212 plugin->editor_watch_id = 0;
213 plugin->assist = NULL;
214 plugin->settings = g_settings_new (PREF_SCHEMA);
217 static void
218 parser_cxx_plugin_class_init (GObjectClass *klass)
220 AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
222 parent_class = g_type_class_peek_parent (klass);
224 plugin_class->activate = parser_cxx_plugin_activate_plugin;
225 plugin_class->deactivate = parser_cxx_plugin_deactivate_plugin;
226 klass->finalize = parser_cxx_plugin_finalize;
227 klass->dispose = parser_cxx_plugin_dispose;
230 #define PREF_WIDGET_SPACE "preferences:completion-space-after-func"
231 #define PREF_WIDGET_BRACE "preferences:completion-brace-after-func"
232 #define PREF_WIDGET_CLOSEBRACE "preferences:completion-closebrace-after-func"
233 #define PREF_WIDGET_AUTO "preferences:completion-enable"
235 static void
236 on_autocompletion_toggled (GtkToggleButton* button,
237 ParserCxxPlugin* plugin)
239 GtkWidget* widget;
240 gboolean sensitive = gtk_toggle_button_get_active (button);
242 widget = GTK_WIDGET (gtk_builder_get_object (plugin->bxml, PREF_WIDGET_SPACE));
243 gtk_widget_set_sensitive (widget, sensitive);
244 widget = GTK_WIDGET (gtk_builder_get_object (plugin->bxml, PREF_WIDGET_BRACE));
245 gtk_widget_set_sensitive (widget, sensitive);
246 widget = GTK_WIDGET (gtk_builder_get_object (plugin->bxml, PREF_WIDGET_CLOSEBRACE));
247 gtk_widget_set_sensitive (widget, sensitive);
250 static void
251 ipreferences_merge (IAnjutaPreferences* ipref, AnjutaPreferences* prefs,
252 GError** e)
254 GError* error = NULL;
255 ParserCxxPlugin* plugin = ANJUTA_PLUGIN_PARSER_CXX (ipref);
256 plugin->bxml = gtk_builder_new ();
257 GtkWidget* toggle;
259 /* Add preferences */
260 if (!gtk_builder_add_from_file (plugin->bxml, PREFS_BUILDER, &error))
262 g_warning ("Couldn't load builder file: %s", error->message);
263 g_error_free (error);
265 anjuta_preferences_add_from_builder (prefs,
266 plugin->bxml, plugin->settings,
267 "preferences", _("Auto-complete"),
268 ICON_FILE);
269 toggle = GTK_WIDGET (gtk_builder_get_object (plugin->bxml, PREF_WIDGET_AUTO));
270 g_signal_connect (toggle, "toggled", G_CALLBACK (on_autocompletion_toggled),
271 plugin);
272 on_autocompletion_toggled (GTK_TOGGLE_BUTTON (toggle), plugin);
275 static void
276 ipreferences_unmerge (IAnjutaPreferences* ipref, AnjutaPreferences* prefs,
277 GError** e)
279 ParserCxxPlugin* plugin = ANJUTA_PLUGIN_PARSER_CXX (ipref);
280 anjuta_preferences_remove_page(prefs, _("Auto-complete"));
281 g_object_unref (plugin->bxml);
284 static void
285 ipreferences_iface_init (IAnjutaPreferencesIface* iface)
287 iface->merge = ipreferences_merge;
288 iface->unmerge = ipreferences_unmerge;
291 ANJUTA_PLUGIN_BEGIN (ParserCxxPlugin, parser_cxx_plugin);
292 ANJUTA_PLUGIN_ADD_INTERFACE (ipreferences, IANJUTA_TYPE_PREFERENCES);
293 ANJUTA_PLUGIN_END;
295 ANJUTA_SIMPLE_PLUGIN (ParserCxxPlugin, parser_cxx_plugin);