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.
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 MacroPlugin
*macro_plugin
= ANJUTA_PLUGIN_MACRO (plugin
);
72 anjuta_ui_get_action (ui
, "ActionGroupMacro", "ActionEditMacroInsert");
76 g_object_set (G_OBJECT (macro_insert_action
), "sensitive", TRUE
, NULL
);
77 macro_plugin
->current_editor
= editor
;
81 g_object_set (G_OBJECT (macro_insert_action
), "sensitive", FALSE
, NULL
);
82 macro_plugin
->current_editor
= NULL
;
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
;
98 activate_plugin (AnjutaPlugin
* plugin
)
100 //AnjutaPreferences *prefs;
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"),
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();
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
,
139 anjuta_ui_unmerge (ui
, ANJUTA_PLUGIN_MACRO (plugin
)->uiid
);
140 anjuta_ui_remove_action_group (ui
, ANJUTA_PLUGIN_MACRO (plugin
)->action_group
);
145 finalize (GObject
* obj
)
147 GNOME_CALL_PARENT (G_OBJECT_CLASS
, finalize
, (G_OBJECT (obj
)));
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
)));
161 macro_plugin_instance_init (GObject
* obj
)
163 MacroPlugin
*plugin
= ANJUTA_PLUGIN_MACRO (obj
);
165 plugin
->current_editor
= NULL
;
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
;
182 match_keyword (MacroPlugin
* plugin
, GtkTreeIter
* iter
, const gchar
*keyword
)
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
),
197 ianjuta_editor_insert (IANJUTA_EDITOR (plugin
->current_editor
),
198 pos
, text
, -1, NULL
);
199 ianjuta_editor_goto_position (IANJUTA_EDITOR(plugin
->current_editor
),
209 /* keyword : macro name */
212 macro_insert (MacroPlugin
* plugin
, const gchar
*keyword
)
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
))
232 gtk_tree_model_get (model
, &cur_macro
,
237 if (match_keyword (plugin
, &cur_macro
, keyword
))
243 while (gtk_tree_model_iter_next
244 (model
, &cur_macro
));
248 gboolean is_category
;
249 gtk_tree_model_get (model
, &cur_cat
,
255 if (match_keyword (plugin
, &cur_cat
, keyword
))
261 while (gtk_tree_model_iter_next (model
, &cur_cat
));
264 while (gtk_tree_model_iter_next (model
, &parent
));
269 ianjuta_macro_iface_insert(IAnjutaMacro
* macro
, const gchar
* key
, GError
** err
)
271 MacroPlugin
* plugin
= ANJUTA_PLUGIN_MACRO (macro
);
272 macro_insert(plugin
, key
);
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
);
286 ANJUTA_SIMPLE_PLUGIN (MacroPlugin
, macro_plugin
);