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