* configure.in, plugins/glade/plugin.c: Make it work with
[anjuta-git-plugin.git] / plugins / macro / plugin.c
blob379a8d970329e12ea069780c6a228d07c42e49c9
1 /*
2 * plugin.c (c) 2004 Johannes Schmid
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Library General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 #include "plugin.h"
19 #include "macro-actions.h"
20 #include "macro-db.h"
22 #include <libanjuta/anjuta-debug.h>
23 #include <libanjuta/interfaces/ianjuta-macro.h>
25 #define UI_FILE PACKAGE_DATA_DIR"/ui/anjuta-macro.ui"
26 #define ICON_FILE "anjuta-macro.png"
28 static gpointer parent_class;
30 static GtkActionEntry actions_macro[] = {
32 "ActionMenuEditMacro",
33 NULL,
34 N_("Macros"),
35 NULL,
36 NULL,
37 NULL},
39 "ActionEditMacroInsert",
40 NULL,
41 N_("_Insert Macro..."),
42 "<control>i",
43 N_("Insert a macro using a shortcut"),
44 G_CALLBACK (on_menu_insert_macro)},
46 "ActionEditMacroAdd",
47 NULL,
48 N_("_Add Macro..."),
49 "<control>m",
50 N_("Add a macro"),
51 G_CALLBACK (on_menu_add_macro)},
53 "ActionEditMacroManager",
54 NULL,
55 N_("Macros..."),
56 NULL,
57 N_("Add/Edit/Remove macros"),
58 G_CALLBACK (on_menu_manage_macro)}
61 static void
62 value_added_current_editor (AnjutaPlugin * plugin, const char *name,
63 const GValue * value, gpointer data)
65 GObject *editor;
66 GtkAction* macro_insert_action;
67 AnjutaUI* ui = anjuta_shell_get_ui (plugin->shell, NULL);
68 editor = g_value_get_object (value);
70 MacroPlugin *macro_plugin = ANJUTA_PLUGIN_MACRO (plugin);
71 macro_insert_action =
72 anjuta_ui_get_action (ui, "ActionGroupMacro", "ActionEditMacroInsert");
74 if (editor != NULL)
76 g_object_set (G_OBJECT (macro_insert_action), "sensitive", TRUE, NULL);
77 macro_plugin->current_editor = editor;
79 else
81 g_object_set (G_OBJECT (macro_insert_action), "sensitive", FALSE, NULL);
82 macro_plugin->current_editor = NULL;
87 static void
88 value_removed_current_editor (AnjutaPlugin * plugin,
89 const char *name, gpointer data)
91 MacroPlugin *macro_plugin = ANJUTA_PLUGIN_MACRO (plugin);
93 macro_plugin->current_editor = NULL;
97 static gboolean
98 activate_plugin (AnjutaPlugin * plugin)
100 //AnjutaPreferences *prefs;
101 AnjutaUI *ui;
102 MacroPlugin *macro_plugin;
104 DEBUG_PRINT ("MacroPlugin: Activating Macro plugin...");
106 macro_plugin = ANJUTA_PLUGIN_MACRO (plugin);
107 ui = anjuta_shell_get_ui (plugin->shell, NULL);
109 /* Add all our actions */
110 macro_plugin->action_group =
111 anjuta_ui_add_action_group_entries (ui, "ActionGroupMacro",
112 _("Macro operations"),
113 actions_macro,
114 G_N_ELEMENTS (actions_macro),
115 GETTEXT_PACKAGE, TRUE, plugin);
116 macro_plugin->uiid = anjuta_ui_merge (ui, UI_FILE);
118 macro_plugin->editor_watch_id =
119 anjuta_plugin_add_watch (plugin,
120 "document_manager_current_editor",
121 value_added_current_editor,
122 value_removed_current_editor, NULL);
124 macro_plugin->macro_db = macro_db_new();
126 return TRUE;
129 static gboolean
130 deactivate_plugin (AnjutaPlugin * plugin)
132 AnjutaUI *ui = anjuta_shell_get_ui (plugin->shell, NULL);
134 DEBUG_PRINT ("MacroPlugin: Deactivating Macro plugin...");
136 anjuta_plugin_remove_watch (plugin,
137 ANJUTA_PLUGIN_MACRO (plugin)->editor_watch_id,
138 TRUE);
139 anjuta_ui_unmerge (ui, ANJUTA_PLUGIN_MACRO (plugin)->uiid);
140 anjuta_ui_remove_action_group (ui, ANJUTA_PLUGIN_MACRO (plugin)->action_group);
141 return TRUE;
144 static void
145 finalize (GObject * obj)
147 GNOME_CALL_PARENT (G_OBJECT_CLASS, finalize, (G_OBJECT (obj)));
150 static void
151 dispose (GObject * obj)
153 MacroPlugin *plugin = ANJUTA_PLUGIN_MACRO (obj);
154 if (plugin->macro_dialog != NULL)
155 g_object_unref (plugin->macro_dialog);
156 g_object_unref(plugin->macro_db);
157 GNOME_CALL_PARENT (G_OBJECT_CLASS, dispose, (G_OBJECT (obj)));
160 static void
161 macro_plugin_instance_init (GObject * obj)
163 MacroPlugin *plugin = ANJUTA_PLUGIN_MACRO (obj);
164 plugin->uiid = 0;
165 plugin->current_editor = NULL;
168 static void
169 macro_plugin_class_init (GObjectClass * klass)
171 AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
173 parent_class = g_type_class_peek_parent (klass);
175 plugin_class->activate = activate_plugin;
176 plugin_class->deactivate = deactivate_plugin;
177 klass->dispose = dispose;
178 klass->finalize = finalize;
181 static gboolean
182 match_keyword (MacroPlugin * plugin, GtkTreeIter * iter, const gchar *keyword)
184 gchar *name;
185 gint offset = 0;
186 gint pos;
188 gtk_tree_model_get(macro_db_get_model(plugin->macro_db), iter,
189 MACRO_NAME, &name, -1);
190 if ( name && strcmp(keyword, name) == 0)
192 gchar* text = macro_db_get_macro(plugin, plugin->macro_db, iter, &offset);
193 if (plugin->current_editor != NULL && text != NULL)
195 pos = ianjuta_editor_get_position (IANJUTA_EDITOR(plugin->current_editor),
196 NULL);
197 ianjuta_editor_insert (IANJUTA_EDITOR (plugin->current_editor),
198 pos, text, -1, NULL);
199 ianjuta_editor_goto_position (IANJUTA_EDITOR(plugin->current_editor),
200 pos + offset,
201 NULL);
202 g_free(text);
204 return TRUE;
206 return FALSE;
209 /* keyword : macro name */
211 gboolean
212 macro_insert (MacroPlugin * plugin, const gchar *keyword)
214 GtkTreeIter parent;
215 GtkTreeIter cur_cat;
216 GtkTreeModel *model = macro_db_get_model (plugin->macro_db);
218 gtk_tree_model_get_iter_first (model, &parent);
221 if (gtk_tree_model_iter_children (model, &cur_cat, &parent))
225 GtkTreeIter cur_macro;
226 if (gtk_tree_model_iter_children
227 (model, &cur_macro, &cur_cat))
231 gboolean predefined;
232 gtk_tree_model_get (model, &cur_macro,
233 MACRO_PREDEFINED,
234 &predefined, -1);
235 if (predefined)
237 if (match_keyword (plugin, &cur_macro, keyword))
239 return TRUE;
243 while (gtk_tree_model_iter_next
244 (model, &cur_macro));
246 else
248 gboolean is_category;
249 gtk_tree_model_get (model, &cur_cat,
250 MACRO_IS_CATEGORY,
251 &is_category, -1);
252 if (is_category)
253 continue;
255 if (match_keyword (plugin, &cur_cat, keyword))
257 return TRUE;
261 while (gtk_tree_model_iter_next (model, &cur_cat));
264 while (gtk_tree_model_iter_next (model, &parent));
265 return TRUE;
268 static void
269 ianjuta_macro_iface_insert(IAnjutaMacro* macro, const gchar* key, GError** err)
271 MacroPlugin* plugin = ANJUTA_PLUGIN_MACRO (macro);
272 macro_insert(plugin, key);
275 /* Interface */
276 static void
277 ianjuta_macro_iface_init (IAnjutaMacroIface *iface)
279 iface->insert = ianjuta_macro_iface_insert;
282 ANJUTA_PLUGIN_BEGIN (MacroPlugin, macro_plugin);
283 ANJUTA_PLUGIN_ADD_INTERFACE (ianjuta_macro, IANJUTA_TYPE_MACRO);
284 ANJUTA_PLUGIN_END;
286 ANJUTA_SIMPLE_PLUGIN (MacroPlugin, macro_plugin);