Updated Spanish translation
[anjuta-git-plugin.git] / plugins / macro / macro-dialog.c
blob70d37a7a7447e602291d5158b43a26dc3cba46e3
1 /*
2 * macro_dialog.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 "macro-dialog.h"
19 #include "macro-edit.h"
20 #include "macro-actions.h"
21 #include <libanjuta/interfaces/ianjuta-editor.h>
23 enum
25 OK,
26 CANCEL
29 static gboolean
30 on_macro_dialog_key_press_event(GtkWidget *widget, GdkEventKey *event,
31 MacroPlugin * plugin)
33 if (event->keyval == GDK_Escape)
35 gtk_widget_hide (GTK_WIDGET (plugin->macro_dialog));
36 return TRUE;
38 return FALSE;
41 static void
42 on_ok_clicked (MacroPlugin * plugin)
44 MacroDialog *dialog = MACRO_DIALOG (plugin->macro_dialog);
45 GtkTreeSelection *selection = gtk_tree_view_get_selection
46 (GTK_TREE_VIEW (dialog->macro_tree));
48 GtkTreeModel* model = macro_db_get_model(dialog->macro_db);
49 GtkTreeIter iter;
50 gchar* text;
51 gint offset = 0;
53 g_return_if_fail (plugin != NULL);
54 g_return_if_fail (model != NULL);
56 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
57 return;
58 text = macro_db_get_macro(plugin, dialog->macro_db, &iter, &offset);
59 if (text)
61 if (plugin->current_editor != NULL)
63 gint i;
64 IAnjutaIterable *pos;
65 pos = ianjuta_editor_get_position (IANJUTA_EDITOR(plugin->current_editor),
66 NULL);
67 ianjuta_editor_insert (IANJUTA_EDITOR(plugin->current_editor),
68 pos, text, -1, NULL);
69 for (i = 0; i < offset; i++)
70 ianjuta_iterable_next (pos, NULL);
71 ianjuta_editor_goto_position (IANJUTA_EDITOR(plugin->current_editor),
72 pos, NULL);
73 g_object_unref (pos);
75 g_free(text);
76 gtk_widget_hide (plugin->macro_dialog);
80 static void
81 on_cancel_clicked (MacroPlugin * plugin)
83 g_return_if_fail (plugin != NULL);
84 gtk_widget_hide (GTK_WIDGET (plugin->macro_dialog));
87 static void
88 on_dialog_response (GtkWidget * dialog, gint response, MacroPlugin * plugin)
90 g_return_if_fail (plugin != NULL);
91 switch (response)
93 case OK:
94 on_ok_clicked (plugin);
95 break;
96 case CANCEL:
97 on_cancel_clicked (plugin);
98 break;
99 default:
100 break;
104 static void
105 on_macro_selection_changed (GtkTreeSelection * selection,
106 MacroDialog * dialog)
108 g_return_if_fail (selection != NULL);
109 g_return_if_fail (dialog != NULL);
110 GtkTreeIter iter;
111 GtkTreeModel *model = macro_db_get_model (dialog->macro_db);
112 GtkWidget *edit;
113 GtkWidget *remove;
114 GtkTextBuffer *text_buffer =
115 gtk_text_view_get_buffer (GTK_TEXT_VIEW
116 (dialog->preview_text));
118 edit = glade_xml_get_widget (dialog->gxml, "macro_edit");
119 remove = glade_xml_get_widget (dialog->gxml, "macro_remove");
121 if (gtk_tree_selection_get_selected (selection, &model, &iter))
123 gchar *name;
124 gchar *category;
125 gchar shortcut;
126 gchar *text;
127 gchar *details;
128 gchar *details_utf8;
129 gboolean is_category;
130 gboolean predefined;
131 gint offset;
133 gtk_tree_model_get (model, &iter,
134 MACRO_NAME, &name,
135 MACRO_CATEGORY, &category,
136 MACRO_SHORTCUT, &shortcut,
137 MACRO_IS_CATEGORY, &is_category,
138 MACRO_PREDEFINED, &predefined, -1);
139 if (!is_category)
141 details = g_strdup_printf ("Name:\t %s\n"
142 "Category:\t %s\nShortcut:\t %c\n",
143 name, category, shortcut);
144 /* Keep pango happy */
145 details_utf8 = g_utf8_normalize (details, -1,
146 G_NORMALIZE_DEFAULT_COMPOSE);
148 gtk_label_set_text (GTK_LABEL (dialog->details_label),
149 details_utf8);
150 text = macro_db_get_macro(dialog->plugin, dialog->macro_db, &iter, &offset);
151 if (text != NULL)
153 gtk_text_buffer_set_text (text_buffer, text, -1);
155 gtk_widget_set_sensitive (edit, !predefined);
156 gtk_widget_set_sensitive (remove, !predefined);
157 g_free (text);
158 return;
162 gtk_label_set_text (GTK_LABEL (dialog->details_label), "");
163 gtk_text_buffer_set_text (text_buffer, "", -1);
164 gtk_widget_set_sensitive (edit, FALSE);
165 gtk_widget_set_sensitive (remove, FALSE);
168 static void
169 on_add_clicked (GtkButton * add, MacroDialog * dialog)
171 g_return_if_fail (dialog != NULL);
172 MacroEdit *edit =
173 MACRO_EDIT (macro_edit_new (MACRO_ADD, dialog->macro_db));
174 gtk_window_set_modal (GTK_WINDOW (edit), TRUE);
175 gtk_widget_show (GTK_WIDGET (edit));
178 static void
179 on_remove_clicked (GtkButton * remove, MacroDialog * dialog)
181 GtkTreeSelection *selection = gtk_tree_view_get_selection
182 (GTK_TREE_VIEW (dialog->macro_tree));
183 GtkTreeModel *model = macro_db_get_model (dialog->macro_db);
184 GtkTreeIter iter;
186 if (gtk_tree_selection_get_selected (selection, &model, &iter))
188 gboolean is_category;
189 gboolean predefined;
190 gtk_tree_model_get (model, &iter,
191 MACRO_PREDEFINED, &predefined,
192 MACRO_IS_CATEGORY, &is_category, -1);
193 if (is_category || predefined)
194 return;
195 else
197 macro_db_remove (dialog->macro_db, &iter);
202 static void
203 on_edit_clicked (GtkButton * ok, MacroDialog * dialog)
205 GtkTreeSelection *select = gtk_tree_view_get_selection
206 (GTK_TREE_VIEW (dialog->macro_tree));
207 MacroEdit *edit;
209 g_return_if_fail (dialog != NULL);
211 edit = MACRO_EDIT (macro_edit_new (MACRO_EDIT, dialog->macro_db));
212 if (edit == NULL)
213 return;
214 macro_edit_fill (edit, select);
215 gtk_window_set_modal (GTK_WINDOW (edit), TRUE);
216 gtk_widget_show (GTK_WIDGET (edit));
219 static gpointer parent_class;
221 static void
222 macro_dialog_dispose (GObject * object)
224 MacroDialog *dialog = MACRO_DIALOG (object);
225 if (dialog->gxml)
227 g_object_unref (dialog->gxml);
228 dialog->gxml = NULL;
230 if (dialog->macro_db)
232 g_object_unref (dialog->macro_db);
233 dialog->macro_db = NULL;
235 G_OBJECT_CLASS (parent_class)->dispose (object);
238 static void
239 macro_dialog_finalize (GObject *object)
241 G_OBJECT_CLASS (parent_class)->finalize (object);
244 static void
245 macro_dialog_class_init (MacroDialogClass * klass)
247 GObjectClass *object_class = G_OBJECT_CLASS (klass);
248 parent_class = g_type_class_peek_parent (klass);
249 object_class->dispose = macro_dialog_dispose;
250 object_class->finalize = macro_dialog_finalize;
253 static void
254 macro_dialog_init (MacroDialog * dialog)
256 GtkTreeSelection *select;
257 dialog->gxml = glade_xml_new (GLADE_FILE, "macro_dialog_table", NULL);
258 GtkWidget *table =
259 glade_xml_get_widget (dialog->gxml, "macro_dialog_table");
261 g_return_if_fail (dialog != NULL);
263 gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), table);
264 gtk_dialog_add_buttons (GTK_DIALOG (dialog), "Insert", OK,
265 GTK_STOCK_CANCEL, CANCEL, NULL);
266 gtk_window_set_default_size (GTK_WINDOW (dialog), 400, 300);
267 gtk_window_set_title (GTK_WINDOW (dialog), _("Insert macro"));
269 dialog->macro_tree =
270 glade_xml_get_widget (dialog->gxml, "macro_tree");
271 dialog->preview_text =
272 glade_xml_get_widget (dialog->gxml, "macro_preview");
273 dialog->details_label =
274 glade_xml_get_widget (dialog->gxml, "macro_details");
276 glade_xml_signal_connect_data (dialog->gxml, "on_edit_clicked",
277 G_CALLBACK (on_edit_clicked), dialog);
278 glade_xml_signal_connect_data (dialog->gxml, "on_add_clicked",
279 G_CALLBACK (on_add_clicked), dialog);
280 glade_xml_signal_connect_data (dialog->gxml, "on_remove_clicked",
281 G_CALLBACK (on_remove_clicked),
282 dialog);
284 select = gtk_tree_view_get_selection (GTK_TREE_VIEW
285 (dialog->macro_tree));
286 gtk_tree_selection_set_mode (select, GTK_SELECTION_SINGLE);
287 g_signal_connect (G_OBJECT (select), "changed",
288 G_CALLBACK (on_macro_selection_changed), dialog);
291 GType
292 macro_dialog_get_type (void)
294 static GType macro_dialog_type = 0;
295 if (!macro_dialog_type)
297 static const GTypeInfo md_info = {
298 sizeof (MacroDialogClass),
299 NULL, /* base_init */
300 NULL, /* base_finalize */
301 (GClassInitFunc) macro_dialog_class_init,
302 NULL, /* class_finalize */
303 NULL, /* class_data */
304 sizeof (MacroDialog),
306 (GInstanceInitFunc) macro_dialog_init,
308 macro_dialog_type =
309 g_type_register_static (GTK_TYPE_DIALOG,
310 "MacroDialog", &md_info, 0);
312 return macro_dialog_type;
315 GtkWidget *
316 macro_dialog_new (MacroPlugin * plugin)
318 g_return_val_if_fail (plugin != NULL, NULL);
319 GtkCellRenderer *renderer;
320 GtkTreeViewColumn *column;
321 MacroDialog *dialog =
322 MACRO_DIALOG (g_object_new (macro_dialog_get_type (), NULL));
324 g_signal_connect (G_OBJECT (dialog), "response",
325 G_CALLBACK (on_dialog_response), plugin);
326 g_signal_connect(G_OBJECT(dialog), "key-press-event",
327 G_CALLBACK(on_macro_dialog_key_press_event), plugin);
329 plugin->macro_dialog = GTK_WIDGET (dialog);
330 dialog->plugin = plugin;
331 dialog->macro_db = plugin->macro_db;
332 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->macro_tree),
333 macro_db_get_model (dialog->macro_db));
334 renderer = gtk_cell_renderer_text_new ();
335 column = gtk_tree_view_column_new_with_attributes ("Macro name",
336 renderer,
337 "text", MACRO_NAME,
338 NULL);
339 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->macro_tree),
340 column);
342 return GTK_WIDGET (dialog);