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>
30 on_macro_dialog_key_press_event(GtkWidget
*widget
, GdkEventKey
*event
,
33 if (event
->keyval
== GDK_Escape
)
35 gtk_widget_hide (GTK_WIDGET (plugin
->macro_dialog
));
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
);
53 g_return_if_fail (plugin
!= NULL
);
54 g_return_if_fail (model
!= NULL
);
56 if (!gtk_tree_selection_get_selected(selection
, &model
, &iter
))
58 text
= macro_db_get_macro(plugin
, dialog
->macro_db
, &iter
, &offset
);
61 if (plugin
->current_editor
!= NULL
)
65 pos
= ianjuta_editor_get_position (IANJUTA_EDITOR(plugin
->current_editor
),
67 ianjuta_editor_insert (IANJUTA_EDITOR(plugin
->current_editor
),
69 for (i
= 0; i
< offset
; i
++)
70 ianjuta_iterable_next (pos
, NULL
);
71 ianjuta_editor_goto_position (IANJUTA_EDITOR(plugin
->current_editor
),
76 gtk_widget_hide (plugin
->macro_dialog
);
81 on_cancel_clicked (MacroPlugin
* plugin
)
83 g_return_if_fail (plugin
!= NULL
);
84 gtk_widget_hide (GTK_WIDGET (plugin
->macro_dialog
));
88 on_dialog_response (GtkWidget
* dialog
, gint response
, MacroPlugin
* plugin
)
90 g_return_if_fail (plugin
!= NULL
);
94 on_ok_clicked (plugin
);
97 on_cancel_clicked (plugin
);
105 on_macro_selection_changed (GtkTreeSelection
* selection
,
106 MacroDialog
* dialog
)
108 g_return_if_fail (selection
!= NULL
);
109 g_return_if_fail (dialog
!= NULL
);
111 GtkTreeModel
*model
= macro_db_get_model (dialog
->macro_db
);
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
))
129 gboolean is_category
;
133 gtk_tree_model_get (model
, &iter
,
135 MACRO_CATEGORY
, &category
,
136 MACRO_SHORTCUT
, &shortcut
,
137 MACRO_IS_CATEGORY
, &is_category
,
138 MACRO_PREDEFINED
, &predefined
, -1);
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
),
150 text
= macro_db_get_macro(dialog
->plugin
, dialog
->macro_db
, &iter
, &offset
);
153 gtk_text_buffer_set_text (text_buffer
, text
, -1);
155 gtk_widget_set_sensitive (edit
, !predefined
);
156 gtk_widget_set_sensitive (remove
, !predefined
);
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
);
169 on_add_clicked (GtkButton
* add
, MacroDialog
* dialog
)
171 g_return_if_fail (dialog
!= NULL
);
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
));
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
);
186 if (gtk_tree_selection_get_selected (selection
, &model
, &iter
))
188 gboolean is_category
;
190 gtk_tree_model_get (model
, &iter
,
191 MACRO_PREDEFINED
, &predefined
,
192 MACRO_IS_CATEGORY
, &is_category
, -1);
193 if (is_category
|| predefined
)
197 macro_db_remove (dialog
->macro_db
, &iter
);
203 on_edit_clicked (GtkButton
* ok
, MacroDialog
* dialog
)
205 GtkTreeSelection
*select
= gtk_tree_view_get_selection
206 (GTK_TREE_VIEW (dialog
->macro_tree
));
209 g_return_if_fail (dialog
!= NULL
);
211 edit
= MACRO_EDIT (macro_edit_new (MACRO_EDIT
, dialog
->macro_db
));
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
;
222 macro_dialog_dispose (GObject
* object
)
224 MacroDialog
*dialog
= MACRO_DIALOG (object
);
227 g_object_unref (dialog
->gxml
);
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
);
239 macro_dialog_finalize (GObject
*object
)
241 G_OBJECT_CLASS (parent_class
)->finalize (object
);
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
;
254 macro_dialog_init (MacroDialog
* dialog
)
256 GtkTreeSelection
*select
;
257 dialog
->gxml
= glade_xml_new (GLADE_FILE
, "macro_dialog_table", NULL
);
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"));
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
),
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
);
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
,
309 g_type_register_static (GTK_TYPE_DIALOG
,
310 "MacroDialog", &md_info
, 0);
312 return macro_dialog_type
;
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",
339 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog
->macro_tree
),
342 return GTK_WIDGET (dialog
);