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.
19 #include "macro-actions.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",
39 "ActionEditMacroInsert",
41 N_("_Insert Macro..."),
43 N_("Insert a macro using a shortcut"),
44 G_CALLBACK (on_menu_insert_macro
)},
51 G_CALLBACK (on_menu_add_macro
)},
53 "ActionEditMacroManager",
57 N_("Add/Edit/Remove macros"),
58 G_CALLBACK (on_menu_manage_macro
)}
62 value_added_current_editor (AnjutaPlugin
* plugin
, const char *name
,
63 const GValue
* value
, gpointer data
)
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
))
73 MacroPlugin
*macro_plugin
= ANJUTA_PLUGIN_MACRO (plugin
);
75 anjuta_ui_get_action (ui
, "ActionGroupMacro", "ActionEditMacroInsert");
79 g_object_set (G_OBJECT (macro_insert_action
), "sensitive", TRUE
, NULL
);
80 macro_plugin
->current_editor
= editor
;
84 g_object_set (G_OBJECT (macro_insert_action
), "sensitive", FALSE
, NULL
);
85 macro_plugin
->current_editor
= NULL
;
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
;
101 activate_plugin (AnjutaPlugin
* plugin
)
103 //AnjutaPreferences *prefs;
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"),
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();
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
,
142 anjuta_ui_unmerge (ui
, ANJUTA_PLUGIN_MACRO (plugin
)->uiid
);
143 anjuta_ui_remove_action_group (ui
, ANJUTA_PLUGIN_MACRO (plugin
)->action_group
);
148 finalize (GObject
* obj
)
150 G_OBJECT_CLASS (parent_class
)->finalize (obj
);
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
);
164 macro_plugin_instance_init (GObject
* obj
)
166 MacroPlugin
*plugin
= ANJUTA_PLUGIN_MACRO (obj
);
168 plugin
->current_editor
= NULL
;
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
;
185 match_keyword (MacroPlugin
* plugin
, GtkTreeIter
* iter
, const gchar
*keyword
)
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
)
198 IAnjutaIterable
*pos
=
199 ianjuta_editor_get_position (IANJUTA_EDITOR(plugin
->current_editor
),
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
),
208 g_object_unref (pos
);
215 /* keyword : macro name */
218 macro_insert (MacroPlugin
* plugin
, const gchar
*keyword
)
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
))
238 gtk_tree_model_get (model
, &cur_macro
,
243 if (match_keyword (plugin
, &cur_macro
, keyword
))
249 while (gtk_tree_model_iter_next
250 (model
, &cur_macro
));
254 gboolean is_category
;
255 gtk_tree_model_get (model
, &cur_cat
,
261 if (match_keyword (plugin
, &cur_cat
, keyword
))
267 while (gtk_tree_model_iter_next (model
, &cur_cat
));
270 while (gtk_tree_model_iter_next (model
, &parent
));
275 ianjuta_macro_iface_insert(IAnjutaMacro
* macro
, const gchar
* key
, GError
** err
)
277 MacroPlugin
* plugin
= ANJUTA_PLUGIN_MACRO (macro
);
278 macro_insert(plugin
, key
);
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
);
292 ANJUTA_SIMPLE_PLUGIN (MacroPlugin
, macro_plugin
);