2008-04-12 Johannes Schmid <jhs@gnome.org>
[anjuta-git-plugin.git] / plugins / macro / macro-actions.c
blob2f26f6a3de9bfdeeb125e3b3b680adeec6ddd2cf
1 /*
2 * macro-actions.c (c) 2005 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 <libanjuta/interfaces/ianjuta-editor-selection.h>
19 #include "macro-actions.h"
20 #include "macro-db.h"
21 #include "macro-dialog.h"
22 #include "macro-edit.h"
24 static gboolean
25 match_shortcut (MacroPlugin * plugin, GtkTreeIter * iter,
26 gchar key)
28 gchar shortcut;
29 gint offset = 0;
31 gtk_tree_model_get(macro_db_get_model(plugin->macro_db), iter,
32 MACRO_SHORTCUT, &shortcut, -1);
33 if (key == shortcut)
35 gchar* text = macro_db_get_macro(plugin, plugin->macro_db, iter, &offset);
36 if (plugin->current_editor != NULL && text != NULL)
38 gint i;
39 IAnjutaIterable *pos;
40 pos = ianjuta_editor_get_position (IANJUTA_EDITOR(plugin->current_editor),
41 NULL);
42 ianjuta_editor_insert (IANJUTA_EDITOR (plugin->current_editor),
43 pos, text, -1, NULL);
44 for (i = 0; i < offset; i++)
45 ianjuta_iterable_next (pos, NULL);
46 ianjuta_editor_goto_position (IANJUTA_EDITOR(plugin->current_editor),
47 pos, NULL);
48 g_free(text);
49 g_object_unref (pos);
51 return TRUE;
53 return FALSE;
56 /* Shortcut handling */
57 static gboolean
58 on_shortcut_pressed (GtkWidget * window, GdkEventKey * event,
59 MacroPlugin * plugin)
61 gchar key;
62 GtkTreeIter parent;
63 GtkTreeIter cur_cat;
64 GtkTreeModel *model = macro_db_get_model (plugin->macro_db);
65 /* Plase note that this implementation is deprecated but
66 * I could not figure out how to do this with GtkIMContext as
67 * proposed by the gtk docs */
68 #warning FIXME: Deprecated
69 if (event->length)
70 key = event->string[0];
71 else
72 return TRUE;
73 gtk_tree_model_get_iter_first (model, &parent);
76 if (gtk_tree_model_iter_children (model, &cur_cat, &parent))
80 GtkTreeIter cur_macro;
81 if (gtk_tree_model_iter_children
82 (model, &cur_macro, &cur_cat))
86 if (match_shortcut(plugin, &cur_macro, key))
88 gtk_widget_destroy(window);
89 return TRUE;
92 while (gtk_tree_model_iter_next
93 (model, &cur_macro));
95 else
97 gboolean is_category;
98 gtk_tree_model_get (model, &cur_cat,
99 MACRO_IS_CATEGORY,
100 &is_category, -1);
101 if (is_category)
102 continue;
103 if (match_shortcut(plugin, &cur_cat, key))
105 gtk_widget_destroy(window);
106 return TRUE;
110 while (gtk_tree_model_iter_next (model, &cur_cat));
113 while (gtk_tree_model_iter_next (model, &parent));
114 gtk_widget_destroy(window);
115 return TRUE;
118 void
119 on_menu_insert_macro (GtkAction * action, MacroPlugin * plugin)
121 if (plugin->current_editor == NULL)
122 return;
124 GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
125 GtkWidget* entry = gtk_entry_new_with_max_length(1);
126 GtkWidget* label = gtk_label_new_with_mnemonic(_("Press macro shortcut..."));
127 GtkWidget* hbox = gtk_hbox_new (FALSE, 0);
129 gtk_container_set_border_width (GTK_CONTAINER (hbox), 10);
131 gtk_widget_set_size_request(entry, 0, 0);
133 gtk_window_set_title(GTK_WINDOW(window), _("Press shortcut"));
134 gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
135 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
136 gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
138 gtk_container_add(GTK_CONTAINER(window), hbox);
139 gtk_box_pack_start_defaults(GTK_BOX(hbox), label);
140 gtk_box_pack_end_defaults(GTK_BOX(hbox), entry);
141 g_signal_connect (G_OBJECT (window), "key-press-event",
142 G_CALLBACK (on_shortcut_pressed), plugin);
143 gtk_widget_grab_focus (entry);
145 gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);
146 gtk_widget_show_all(window);
149 void on_menu_add_macro (GtkAction * action, MacroPlugin * plugin)
151 MacroEdit* add = MACRO_EDIT(macro_edit_new(MACRO_ADD, plugin->macro_db));
152 gchar* selection = NULL;
153 if (plugin->current_editor != NULL)
155 selection =
156 ianjuta_editor_selection_get (IANJUTA_EDITOR_SELECTION(plugin->current_editor), NULL);
158 if (selection != NULL && strlen(selection))
159 macro_edit_set_macro(add, selection);
160 gtk_widget_show(GTK_WIDGET(add));
163 void on_menu_manage_macro (GtkAction * action, MacroPlugin * plugin)
165 if (plugin->macro_dialog == NULL)
166 plugin->macro_dialog = macro_dialog_new (plugin);
167 gtk_widget_show (plugin->macro_dialog);