Updated Spanish translation
[anjuta-git-plugin.git] / plugins / macro / plugin.c
blob80d1dc219b3b9314ebfa5084a5413b46279f11d4
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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><alt>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 if (!IANJUTA_IS_EDITOR(editor))
71 return;
73 MacroPlugin *macro_plugin = ANJUTA_PLUGIN_MACRO (plugin);
74 macro_insert_action =
75 anjuta_ui_get_action (ui, "ActionGroupMacro", "ActionEditMacroInsert");
77 if (editor != NULL)
79 g_object_set (G_OBJECT (macro_insert_action), "sensitive", TRUE, NULL);
80 macro_plugin->current_editor = editor;
82 else
84 g_object_set (G_OBJECT (macro_insert_action), "sensitive", FALSE, NULL);
85 macro_plugin->current_editor = NULL;
90 static void
91 value_removed_current_editor (AnjutaPlugin * plugin,
92 const char *name, gpointer data)
94 MacroPlugin *macro_plugin = ANJUTA_PLUGIN_MACRO (plugin);
96 macro_plugin->current_editor = NULL;
100 static gboolean
101 activate_plugin (AnjutaPlugin * plugin)
103 //AnjutaPreferences *prefs;
104 AnjutaUI *ui;
105 MacroPlugin *macro_plugin;
107 DEBUG_PRINT ("MacroPlugin: Activating Macro plugin...");
109 macro_plugin = ANJUTA_PLUGIN_MACRO (plugin);
110 ui = anjuta_shell_get_ui (plugin->shell, NULL);
112 /* Add all our actions */
113 macro_plugin->action_group =
114 anjuta_ui_add_action_group_entries (ui, "ActionGroupMacro",
115 _("Macro operations"),
116 actions_macro,
117 G_N_ELEMENTS (actions_macro),
118 GETTEXT_PACKAGE, TRUE, plugin);
119 macro_plugin->uiid = anjuta_ui_merge (ui, UI_FILE);
121 macro_plugin->editor_watch_id =
122 anjuta_plugin_add_watch (plugin,
123 "document_manager_current_editor",
124 value_added_current_editor,
125 value_removed_current_editor, NULL);
127 macro_plugin->macro_db = macro_db_new();
129 return TRUE;
132 static gboolean
133 deactivate_plugin (AnjutaPlugin * plugin)
135 AnjutaUI *ui = anjuta_shell_get_ui (plugin->shell, NULL);
137 DEBUG_PRINT ("MacroPlugin: Deactivating Macro plugin...");
139 anjuta_plugin_remove_watch (plugin,
140 ANJUTA_PLUGIN_MACRO (plugin)->editor_watch_id,
141 TRUE);
142 anjuta_ui_unmerge (ui, ANJUTA_PLUGIN_MACRO (plugin)->uiid);
143 anjuta_ui_remove_action_group (ui, ANJUTA_PLUGIN_MACRO (plugin)->action_group);
144 return TRUE;
147 static void
148 finalize (GObject * obj)
150 G_OBJECT_CLASS (parent_class)->finalize (obj);
153 static void
154 dispose (GObject * obj)
156 MacroPlugin *plugin = ANJUTA_PLUGIN_MACRO (obj);
157 if (plugin->macro_dialog != NULL)
158 g_object_unref (plugin->macro_dialog);
159 g_object_unref(plugin->macro_db);
160 G_OBJECT_CLASS (parent_class)->dispose (obj);
163 static void
164 macro_plugin_instance_init (GObject * obj)
166 MacroPlugin *plugin = ANJUTA_PLUGIN_MACRO (obj);
167 plugin->uiid = 0;
168 plugin->current_editor = NULL;
171 static void
172 macro_plugin_class_init (GObjectClass * klass)
174 AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
176 parent_class = g_type_class_peek_parent (klass);
178 plugin_class->activate = activate_plugin;
179 plugin_class->deactivate = deactivate_plugin;
180 klass->dispose = dispose;
181 klass->finalize = finalize;
184 static gboolean
185 match_keyword (MacroPlugin * plugin, GtkTreeIter * iter, const gchar *keyword)
187 gchar *name;
188 gint offset = 0;
190 gtk_tree_model_get(macro_db_get_model(plugin->macro_db), iter,
191 MACRO_NAME, &name, -1);
192 if ( name && strcmp(keyword, name) == 0)
194 gchar* text = macro_db_get_macro(plugin, plugin->macro_db, iter, &offset);
195 if (plugin->current_editor != NULL && text != NULL)
197 gint i;
198 IAnjutaIterable *pos =
199 ianjuta_editor_get_position (IANJUTA_EDITOR(plugin->current_editor),
200 NULL);
201 ianjuta_editor_insert (IANJUTA_EDITOR (plugin->current_editor),
202 pos, text, -1, NULL);
203 for (i = 0; i < offset; i++)
204 ianjuta_iterable_next (pos, NULL);
205 ianjuta_editor_goto_position (IANJUTA_EDITOR(plugin->current_editor),
206 pos, NULL);
207 g_free(text);
208 g_object_unref (pos);
210 return TRUE;
212 return FALSE;
215 /* keyword : macro name */
217 gboolean
218 macro_insert (MacroPlugin * plugin, const gchar *keyword)
220 GtkTreeIter parent;
221 GtkTreeIter cur_cat;
222 GtkTreeModel *model = macro_db_get_model (plugin->macro_db);
224 gtk_tree_model_get_iter_first (model, &parent);
227 if (gtk_tree_model_iter_children (model, &cur_cat, &parent))
231 GtkTreeIter cur_macro;
232 if (gtk_tree_model_iter_children
233 (model, &cur_macro, &cur_cat))
237 gboolean predefined;
238 gtk_tree_model_get (model, &cur_macro,
239 MACRO_PREDEFINED,
240 &predefined, -1);
241 if (predefined)
243 if (match_keyword (plugin, &cur_macro, keyword))
245 return TRUE;
249 while (gtk_tree_model_iter_next
250 (model, &cur_macro));
252 else
254 gboolean is_category;
255 gtk_tree_model_get (model, &cur_cat,
256 MACRO_IS_CATEGORY,
257 &is_category, -1);
258 if (is_category)
259 continue;
261 if (match_keyword (plugin, &cur_cat, keyword))
263 return TRUE;
267 while (gtk_tree_model_iter_next (model, &cur_cat));
270 while (gtk_tree_model_iter_next (model, &parent));
271 return TRUE;
274 static void
275 ianjuta_macro_iface_insert(IAnjutaMacro* macro, const gchar* key, GError** err)
277 MacroPlugin* plugin = ANJUTA_PLUGIN_MACRO (macro);
278 macro_insert(plugin, key);
281 /* Interface */
282 static void
283 ianjuta_macro_iface_init (IAnjutaMacroIface *iface)
285 iface->insert = ianjuta_macro_iface_insert;
288 ANJUTA_PLUGIN_BEGIN (MacroPlugin, macro_plugin);
289 ANJUTA_PLUGIN_ADD_INTERFACE (ianjuta_macro, IANJUTA_TYPE_MACRO);
290 ANJUTA_PLUGIN_END;
292 ANJUTA_SIMPLE_PLUGIN (MacroPlugin, macro_plugin);