1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 Copyright (C) Dragos Dena 2010
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301 USA
25 #include "snippets-import-export.h"
26 #include <libanjuta/interfaces/ianjuta-snippets-manager.h>
27 #include <libanjuta/interfaces/ianjuta-document-manager.h>
28 #include <libanjuta/interfaces/ianjuta-preferences.h>
29 #include <libanjuta/interfaces/ianjuta-editor-language.h>
31 #include <libanjuta/anjuta-shell.h>
32 #include <libanjuta/anjuta-debug.h>
33 #include <libanjuta/anjuta-utils.h>
35 #define ICON_FILE "anjuta-snippets-manager.png"
36 #define PREFERENCES_UI PACKAGE_DATA_DIR"/glade/snippets-manager-preferences.ui"
37 #define SNIPPETS_MANAGER_PREFERENCES_ROOT "snippets_preferences_root"
38 #define MENU_UI PACKAGE_DATA_DIR"/ui/snippets-manager-ui.xml"
40 #define GLOBAL_VAR_NEW_NAME "new_global_var_name"
41 #define GLOBAL_VAR_NEW_VALUE "new_global_var_value"
43 #define PREF_SCHEMA "org.gnome.anjuta.snippets"
45 static gpointer parent_class
;
47 /* Menu callbacks and actions */
49 static void on_menu_trigger_insert_snippet (GtkAction
*action
,
50 SnippetsManagerPlugin
*plugin
);
51 static void on_menu_autocomplete_insert_snippet (GtkAction
*action
,
52 SnippetsManagerPlugin
*plugin
);
53 static void on_menu_import_snippets (GtkAction
*action
,
54 SnippetsManagerPlugin
*plugin
);
55 static void on_menu_export_snippets (GtkAction
*action
,
56 SnippetsManagerPlugin
*plugin
);
58 static GtkActionEntry actions_snippets
[] = {
60 "ActionMenuEditSnippetsManager",
67 "ActionEditTriggerInsert",
69 /* Translator: Appears in Edit->Snippets menu. It is used mainly for providing a
70 shortcut for the trigger-key based insertion of snippets. It's called like this
71 because you type the trigger-key in the editor, followed by Trigger Insert and
72 the snippet gets inserted. */
73 N_("_Trigger insert"),
75 N_("Insert a snippet using the trigger-key"),
76 G_CALLBACK (on_menu_trigger_insert_snippet
)},
78 "ActionEditAutoCompleteInsert",
80 /* Translator: In a similar matter, it also appears in Edit->Snippets. It's another method
81 for inserting snippets. In this case, you call Auto complete insert, start
82 typing in the editor a string, the database is searched for that string and the
83 most relevant snippets are returned in a pop-up like the auto-complete one
84 (they are showed by their names). After selecting one of them, the snippet gets
86 N_("_Auto complete insert"),
88 N_("Insert a snippet using auto-completion"),
89 G_CALLBACK (on_menu_autocomplete_insert_snippet
)},
91 "ActionEditImportSnippets",
93 N_("_Import snippets …"),
95 N_("Import snippets to the database"),
96 G_CALLBACK (on_menu_import_snippets
)},
98 "ActionEditExportSnippets",
100 N_("_Export snippets …"),
102 N_("Export snippets from the database"),
103 G_CALLBACK (on_menu_export_snippets
)}
106 typedef struct _GlobalVariablesUpdateData
108 SnippetsDB
*snippets_db
;
109 GtkTreeView
*global_vars_view
;
110 } GlobalVariablesUpdateData
;
113 snippet_insert (SnippetsManagerPlugin
* plugin
,
114 const gchar
*trigger
,
115 gboolean editing_session
)
117 AnjutaSnippet
*requested_snippet
= NULL
;
118 SnippetsManagerPlugin
*snippets_manager_plugin
= NULL
;
121 g_return_val_if_fail (ANJUTA_IS_PLUGIN_SNIPPETS_MANAGER (plugin
),
123 snippets_manager_plugin
= ANJUTA_PLUGIN_SNIPPETS_MANAGER (plugin
);
125 /* Get the snippet from the database and check if it's not found */
126 requested_snippet
= snippets_db_get_snippet (snippets_manager_plugin
->snippets_db
,
129 g_return_val_if_fail (ANJUTA_IS_SNIPPET (requested_snippet
), FALSE
);
131 /* Get the default content of the snippet */
132 snippets_interaction_insert_snippet (snippets_manager_plugin
->snippets_interaction
,
133 snippets_manager_plugin
->snippets_db
,
141 on_menu_trigger_insert_snippet (GtkAction
*action
,
142 SnippetsManagerPlugin
*plugin
)
145 g_return_if_fail (ANJUTA_IS_PLUGIN_SNIPPETS_MANAGER (plugin
));
146 g_return_if_fail (ANJUTA_IS_SNIPPETS_DB (plugin
->snippets_db
));
147 g_return_if_fail (ANJUTA_IS_SNIPPETS_INTERACTION (plugin
->snippets_interaction
));
149 snippets_interaction_trigger_insert_request (plugin
->snippets_interaction
,
150 plugin
->snippets_db
);
155 on_menu_autocomplete_insert_snippet (GtkAction
*action
,
156 SnippetsManagerPlugin
*plugin
)
159 g_return_if_fail (ANJUTA_IS_PLUGIN_SNIPPETS_MANAGER (plugin
));
160 g_return_if_fail (ANJUTA_IS_SNIPPETS_PROVIDER (plugin
->snippets_provider
));
162 snippets_provider_request (plugin
->snippets_provider
);
166 on_menu_import_snippets (GtkAction
*action
,
167 SnippetsManagerPlugin
*plugin
)
169 AnjutaPlugin
*p
= NULL
;
172 g_return_if_fail (ANJUTA_IS_PLUGIN_SNIPPETS_MANAGER (plugin
));
174 p
= ANJUTA_PLUGIN (plugin
);
175 snippets_manager_import_snippets (plugin
->snippets_db
, p
->shell
);
179 on_menu_export_snippets (GtkAction
*action
,
180 SnippetsManagerPlugin
*plugin
)
182 AnjutaPlugin
*p
= NULL
;
185 g_return_if_fail (ANJUTA_IS_PLUGIN_SNIPPETS_MANAGER (plugin
));
187 p
= ANJUTA_PLUGIN (plugin
);
188 snippets_manager_export_snippets (plugin
->snippets_db
, p
->shell
);
192 on_added_current_document (AnjutaPlugin
*plugin
,
197 GObject
*cur_editor
= NULL
;
198 SnippetsManagerPlugin
*snippets_manager_plugin
= NULL
;
201 g_return_if_fail (ANJUTA_IS_PLUGIN_SNIPPETS_MANAGER (plugin
));
202 snippets_manager_plugin
= ANJUTA_PLUGIN_SNIPPETS_MANAGER (plugin
);
204 /* Get the current document and test if it's an IAnjutaEditor */
205 cur_editor
= g_value_get_object (value
);
206 if (IANJUTA_IS_EDITOR (cur_editor
))
207 snippets_interaction_set_editor (snippets_manager_plugin
->snippets_interaction
,
208 IANJUTA_EDITOR (cur_editor
));
210 snippets_interaction_set_editor (snippets_manager_plugin
->snippets_interaction
,
213 /* Refilter the snippets shown in the browser */
214 snippets_browser_refilter_snippets_view (snippets_manager_plugin
->snippets_browser
);
216 /* Load the provider if needed */
217 if (IANJUTA_IS_EDITOR_ASSIST (cur_editor
))
218 snippets_provider_load (snippets_manager_plugin
->snippets_provider
,
219 IANJUTA_EDITOR_ASSIST (cur_editor
));
224 on_removed_current_document (AnjutaPlugin
*plugin
,
228 SnippetsManagerPlugin
*snippets_manager_plugin
= NULL
;
231 g_return_if_fail (ANJUTA_IS_PLUGIN_SNIPPETS_MANAGER (plugin
));
232 snippets_manager_plugin
= ANJUTA_PLUGIN_SNIPPETS_MANAGER (plugin
);
234 /* Unload the provider */
235 snippets_provider_unload (snippets_manager_plugin
->snippets_provider
);
237 snippets_interaction_set_editor (snippets_manager_plugin
->snippets_interaction
,
242 on_snippets_browser_maximize_request (SnippetsBrowser
*snippets_browser
,
245 SnippetsManagerPlugin
*snippets_manager_plugin
= NULL
;
248 g_return_if_fail (ANJUTA_IS_PLUGIN_SNIPPETS_MANAGER (user_data
));
249 snippets_manager_plugin
= ANJUTA_PLUGIN_SNIPPETS_MANAGER (user_data
);
251 if (snippets_manager_plugin
->browser_maximized
)
254 anjuta_shell_maximize_widget (ANJUTA_PLUGIN (snippets_manager_plugin
)->shell
,
255 "snippets_browser", NULL
);
256 snippets_browser_show_editor (snippets_browser
);
258 snippets_manager_plugin
->browser_maximized
= TRUE
;
262 on_snippets_browser_unmaximize_request (SnippetsBrowser
*snippets_browser
,
265 SnippetsManagerPlugin
*snippets_manager_plugin
= NULL
;
268 g_return_if_fail (ANJUTA_IS_PLUGIN_SNIPPETS_MANAGER (user_data
));
269 snippets_manager_plugin
= ANJUTA_PLUGIN_SNIPPETS_MANAGER (user_data
);
271 if (!snippets_manager_plugin
->browser_maximized
)
274 anjuta_shell_unmaximize (ANJUTA_PLUGIN (snippets_manager_plugin
)->shell
,
276 snippets_browser_hide_editor (snippets_browser
);
278 snippets_manager_plugin
->browser_maximized
= FALSE
;
282 snippets_manager_activate (AnjutaPlugin
* plugin
)
284 SnippetsManagerPlugin
*snippets_manager_plugin
= ANJUTA_PLUGIN_SNIPPETS_MANAGER (plugin
);
285 AnjutaUI
*anjuta_ui
= NULL
;
288 g_return_val_if_fail (ANJUTA_IS_PLUGIN_SNIPPETS_MANAGER (snippets_manager_plugin
),
291 /* Link the AnjutaShell to the SnippetsDB and load the SnippetsDB*/
292 snippets_manager_plugin
->snippets_db
->anjuta_shell
= plugin
->shell
;
293 snippets_db_load (snippets_manager_plugin
->snippets_db
);
295 /* Link the AnjutaShell to the SnippetsProvider and load if necessary */
296 snippets_manager_plugin
->snippets_provider
->anjuta_shell
= plugin
->shell
;
298 /* Load the SnippetsBrowser with the snippets in the SnippetsDB */
299 snippets_manager_plugin
->snippets_browser
->anjuta_shell
= plugin
->shell
;
300 snippets_browser_load (snippets_manager_plugin
->snippets_browser
,
301 snippets_manager_plugin
->snippets_db
,
302 snippets_manager_plugin
->snippets_interaction
);
303 gtk_widget_show_all (snippets_manager_plugin
->snippets_browser
);
304 anjuta_shell_add_widget_custom (plugin
->shell
,
305 GTK_WIDGET (snippets_manager_plugin
->snippets_browser
),
309 snippets_browser_get_grip (snippets_manager_plugin
->snippets_browser
),
310 ANJUTA_SHELL_PLACEMENT_LEFT
,
312 snippets_manager_plugin
->browser_maximized
= FALSE
;
314 /* Initialize the Interaction Interpreter */
315 snippets_interaction_start (snippets_manager_plugin
->snippets_interaction
,
318 /* Add a watch for the current document */
319 snippets_manager_plugin
->cur_editor_watch_id
=
320 anjuta_plugin_add_watch (plugin
,
321 IANJUTA_DOCUMENT_MANAGER_CURRENT_DOCUMENT
,
322 on_added_current_document
,
323 on_removed_current_document
,
326 /* Merge the Menu UI */
327 anjuta_ui
= anjuta_shell_get_ui (plugin
->shell
, FALSE
);
329 snippets_manager_plugin
->action_group
=
330 anjuta_ui_add_action_group_entries (anjuta_ui
,
331 "ActionGroupSnippetsManager",
332 _("Snippets Manager actions"),
334 G_N_ELEMENTS (actions_snippets
),
337 snippets_manager_plugin
);
339 snippets_manager_plugin
->uiid
= anjuta_ui_merge (anjuta_ui
, MENU_UI
);
341 DEBUG_PRINT ("%s", "SnippetsManager: Activating SnippetsManager plugin …");
347 snippets_manager_deactivate (AnjutaPlugin
*plugin
)
349 SnippetsManagerPlugin
*snippets_manager_plugin
= NULL
;
350 AnjutaUI
*anjuta_ui
= NULL
;
353 g_return_val_if_fail (ANJUTA_IS_PLUGIN_SNIPPETS_MANAGER (plugin
), FALSE
);
354 snippets_manager_plugin
= ANJUTA_PLUGIN_SNIPPETS_MANAGER (plugin
);
356 DEBUG_PRINT ("%s", "SnippetsManager: Deactivating SnippetsManager plugin …");
358 anjuta_plugin_remove_watch (plugin
,
359 snippets_manager_plugin
->cur_editor_watch_id
,
362 /* Remove the Menu UI */
363 anjuta_ui
= anjuta_shell_get_ui (plugin
->shell
, NULL
);
364 anjuta_ui_unmerge (anjuta_ui
, snippets_manager_plugin
->uiid
);
365 anjuta_ui_remove_action_group (anjuta_ui
, snippets_manager_plugin
->action_group
);
367 /* Unload the SnippetsBrowser */
368 if (snippets_manager_plugin
->browser_maximized
)
369 on_snippets_browser_unmaximize_request (snippets_manager_plugin
->snippets_browser
,
370 snippets_manager_plugin
);
371 snippets_browser_unload (snippets_manager_plugin
->snippets_browser
);
372 g_object_ref (snippets_manager_plugin
->snippets_browser
);
373 anjuta_shell_remove_widget (plugin
->shell
,
374 GTK_WIDGET (snippets_manager_plugin
->snippets_browser
),
377 /* Destroy the SnippetsDB */
378 snippets_db_close (snippets_manager_plugin
->snippets_db
);
380 /* Destroy the Interaction Interpreter */
381 snippets_interaction_destroy (snippets_manager_plugin
->snippets_interaction
);
383 /* Unload the SnippetsProvider */
384 snippets_provider_unload (snippets_manager_plugin
->snippets_provider
);
390 snippets_manager_finalize (GObject
* obj
)
393 G_OBJECT_CLASS (parent_class
)->finalize (obj
);
397 snippets_manager_dispose (GObject
* obj
)
399 SnippetsManagerPlugin
*snippets_manager
= ANJUTA_PLUGIN_SNIPPETS_MANAGER (obj
);
402 g_return_if_fail (ANJUTA_IS_PLUGIN_SNIPPETS_MANAGER (snippets_manager
));
404 if (ANJUTA_IS_SNIPPETS_DB (snippets_manager
->snippets_db
))
405 g_object_unref (snippets_manager
->snippets_db
);
407 if (ANJUTA_IS_SNIPPETS_INTERACTION (snippets_manager
->snippets_interaction
))
408 g_object_unref (snippets_manager
->snippets_interaction
);
410 if (ANJUTA_IS_SNIPPETS_BROWSER (snippets_manager
->snippets_browser
))
411 g_object_unref (snippets_manager
->snippets_browser
);
413 if (ANJUTA_IS_SNIPPETS_PROVIDER (snippets_manager
->snippets_provider
))
414 g_object_unref (snippets_manager
->snippets_provider
);
416 g_object_unref (snippets_manager
->settings
);
418 G_OBJECT_CLASS (parent_class
)->dispose (obj
);
422 snippets_manager_plugin_instance_init (GObject
* obj
)
424 SnippetsManagerPlugin
*snippets_manager
= ANJUTA_PLUGIN_SNIPPETS_MANAGER (obj
);
426 snippets_manager
->overwrite_on_conflict
= FALSE
;
427 snippets_manager
->show_only_document_language_snippets
= FALSE
;
429 snippets_manager
->cur_editor_watch_id
= -1;
431 snippets_manager
->action_group
= NULL
;
432 snippets_manager
->uiid
= -1;
434 snippets_manager
->settings
= g_settings_new (PREF_SCHEMA
);
436 snippets_manager
->snippets_db
= snippets_db_new ();
437 snippets_manager
->snippets_interaction
= snippets_interaction_new ();
438 snippets_manager
->snippets_browser
= snippets_browser_new ();
439 snippets_manager
->snippets_provider
=
440 snippets_provider_new (snippets_manager
->snippets_db
,
441 snippets_manager
->snippets_interaction
);
443 g_signal_connect (G_OBJECT (snippets_manager
->snippets_browser
),
445 G_CALLBACK (on_snippets_browser_maximize_request
),
447 g_signal_connect (G_OBJECT (snippets_manager
->snippets_browser
),
448 "unmaximize-request",
449 G_CALLBACK (on_snippets_browser_unmaximize_request
),
455 snippets_manager_plugin_class_init (GObjectClass
* klass
)
457 AnjutaPluginClass
*plugin_class
= ANJUTA_PLUGIN_CLASS (klass
);
459 parent_class
= g_type_class_peek_parent (klass
);
461 plugin_class
->activate
= snippets_manager_activate
;
462 plugin_class
->deactivate
= snippets_manager_deactivate
;
463 klass
->dispose
= snippets_manager_dispose
;
464 klass
->finalize
= snippets_manager_finalize
;
469 /* IAnjutaSnippetsManager interface */
472 isnippets_manager_iface_insert (IAnjutaSnippetsManager
* snippets_manager
,
474 gboolean editing_session
,
477 SnippetsManagerPlugin
* plugin
= ANJUTA_PLUGIN_SNIPPETS_MANAGER (snippets_manager
);
478 snippet_insert (plugin
, key
, editing_session
);
483 isnippets_manager_iface_init (IAnjutaSnippetsManagerIface
*iface
)
485 iface
->insert
= isnippets_manager_iface_insert
;
490 /* IAnjutaPreferences interface */
493 on_global_vars_name_changed (GtkCellRendererText
*cell
,
498 GtkTreeModel
*global_vars_model
= NULL
;
499 SnippetsDB
*snippets_db
= NULL
;
500 GtkTreePath
*path
= NULL
;
505 g_return_if_fail (ANJUTA_IS_SNIPPETS_DB (user_data
));
506 snippets_db
= ANJUTA_SNIPPETS_DB (user_data
);
507 global_vars_model
= snippets_db_get_global_vars_model (snippets_db
);
508 g_return_if_fail (GTK_IS_TREE_MODEL (global_vars_model
));
511 path
= gtk_tree_path_new_from_string (path_string
);
512 gtk_tree_model_get_iter (global_vars_model
, &iter
, path
);
514 /* Get the current type and change it */
515 gtk_tree_model_get (global_vars_model
, &iter
,
516 GLOBAL_VARS_MODEL_COL_NAME
, &name
,
518 snippets_db_set_global_variable_name (snippets_db
,
523 /* Save the global variables */
524 snippets_db_save_global_vars (snippets_db
);
528 global_vars_view_name_data_func (GtkTreeViewColumn
*col
,
529 GtkCellRenderer
*cell
,
530 GtkTreeModel
*global_vars_model
,
535 gboolean is_internal
= FALSE
;
538 g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (cell
));
541 gtk_tree_model_get (global_vars_model
, iter
,
542 GLOBAL_VARS_MODEL_COL_NAME
, &name
,
545 /* Check if it's internal */
546 gtk_tree_model_get (global_vars_model
, iter
,
547 GLOBAL_VARS_MODEL_COL_IS_INTERNAL
, &is_internal
,
552 temp
= g_strconcat ("<b>", name
, "</b> <i>(Internal)</i>", NULL
);
555 g_object_set (cell
, "sensitive", FALSE
, NULL
);
556 g_object_set (cell
, "editable", FALSE
, NULL
);
561 temp
= g_strconcat ("<b>", name
, "</b>", NULL
);
564 g_object_set (cell
, "sensitive", TRUE
, NULL
);
565 g_object_set (cell
, "editable", TRUE
, NULL
);
568 g_object_set (cell
, "markup", name
, NULL
);
573 on_global_vars_type_toggled (GtkCellRendererToggle
*cell
,
577 GtkTreeModel
*global_vars_model
= NULL
;
578 SnippetsDB
*snippets_db
= NULL
;
579 GtkTreePath
*path
= NULL
;
581 gboolean is_command
= FALSE
;
585 g_return_if_fail (ANJUTA_IS_SNIPPETS_DB (user_data
));
586 snippets_db
= ANJUTA_SNIPPETS_DB (user_data
);
587 global_vars_model
= snippets_db_get_global_vars_model (snippets_db
);
588 g_return_if_fail (GTK_IS_TREE_MODEL (global_vars_model
));
591 path
= gtk_tree_path_new_from_string (path_string
);
592 gtk_tree_model_get_iter (global_vars_model
, &iter
, path
);
594 /* Get the current type and change it */
595 gtk_tree_model_get (global_vars_model
, &iter
,
596 GLOBAL_VARS_MODEL_COL_IS_COMMAND
, &is_command
,
597 GLOBAL_VARS_MODEL_COL_NAME
, &name
,
599 snippets_db_set_global_variable_type (snippets_db
,
601 (is_command
) ? FALSE
: TRUE
);
603 /* Save the global variables */
604 snippets_db_save_global_vars (snippets_db
);
611 global_vars_view_type_data_func (GtkTreeViewColumn
*col
,
612 GtkCellRenderer
*cell
,
613 GtkTreeModel
*global_vars_model
,
617 gboolean is_command
= FALSE
, is_internal
= TRUE
;
620 g_return_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (cell
));
623 /* Check if it's internal */
624 gtk_tree_model_get (global_vars_model
, iter
,
625 GLOBAL_VARS_MODEL_COL_IS_INTERNAL
, &is_internal
,
629 g_object_set (cell
, "sensitive", FALSE
, NULL
);
630 gtk_cell_renderer_toggle_set_activatable (GTK_CELL_RENDERER_TOGGLE (cell
), FALSE
);
631 gtk_cell_renderer_toggle_set_active (GTK_CELL_RENDERER_TOGGLE (cell
), FALSE
);
635 gtk_tree_model_get (global_vars_model
, iter
,
636 GLOBAL_VARS_MODEL_COL_IS_COMMAND
, &is_command
,
638 g_object_set (cell
, "sensitive", TRUE
, NULL
);
639 gtk_cell_renderer_toggle_set_activatable (GTK_CELL_RENDERER_TOGGLE (cell
), TRUE
);
640 gtk_cell_renderer_toggle_set_active (GTK_CELL_RENDERER_TOGGLE (cell
), is_command
);
645 on_global_vars_text_changed (GtkCellRendererText
*cell
,
650 GtkTreeModel
*global_vars_model
= NULL
;
651 SnippetsDB
*snippets_db
= NULL
;
652 GtkTreePath
*path
= NULL
;
657 g_return_if_fail (ANJUTA_IS_SNIPPETS_DB (user_data
));
658 snippets_db
= ANJUTA_SNIPPETS_DB (user_data
);
659 global_vars_model
= snippets_db_get_global_vars_model (snippets_db
);
660 g_return_if_fail (GTK_IS_TREE_MODEL (global_vars_model
));
663 path
= gtk_tree_path_new_from_string (path_string
);
664 gtk_tree_model_get_iter (global_vars_model
, &iter
, path
);
666 /* Get the current type and change it */
667 gtk_tree_model_get (global_vars_model
, &iter
,
668 GLOBAL_VARS_MODEL_COL_NAME
, &name
,
670 snippets_db_set_global_variable_value (snippets_db
,
675 /* Save the global variables */
676 snippets_db_save_global_vars (snippets_db
);
680 global_vars_view_text_data_func (GtkTreeViewColumn
*col
,
681 GtkCellRenderer
*cell
,
682 GtkTreeModel
*global_vars_model
,
686 gchar
*name
= NULL
, *text
= NULL
;
687 SnippetsDB
*snippets_db
= NULL
;
688 gboolean is_internal
= FALSE
;
691 g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (cell
));
692 g_return_if_fail (ANJUTA_IS_SNIPPETS_DB (user_data
));
693 snippets_db
= ANJUTA_SNIPPETS_DB (user_data
);
696 gtk_tree_model_get (global_vars_model
, iter
,
697 GLOBAL_VARS_MODEL_COL_NAME
, &name
,
698 GLOBAL_VARS_MODEL_COL_IS_INTERNAL
, &is_internal
,
703 g_object_set (cell
, "editable", FALSE
, NULL
);
707 g_object_set (cell
, "editable", TRUE
, NULL
);
710 text
= snippets_db_get_global_variable_text (snippets_db
, name
);
712 g_object_set (cell
, "text", text
, NULL
);
718 global_vars_view_value_data_func (GtkTreeViewColumn
*col
,
719 GtkCellRenderer
*cell
,
720 GtkTreeModel
*global_vars_model
,
724 gchar
*name
= NULL
, *value
= NULL
;
725 SnippetsDB
*snippets_db
= NULL
;
728 g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (cell
));
729 g_return_if_fail (ANJUTA_IS_SNIPPETS_DB (user_data
));
730 snippets_db
= ANJUTA_SNIPPETS_DB (user_data
);
733 gtk_tree_model_get (global_vars_model
, iter
,
734 GLOBAL_VARS_MODEL_COL_NAME
, &name
,
737 value
= snippets_db_get_global_variable (snippets_db
, name
);
739 g_object_set (cell
, "text", value
, NULL
);
744 set_up_global_variables_view (SnippetsManagerPlugin
*snippets_manager_plugin
,
745 GtkTreeView
*global_vars_view
)
747 GtkCellRenderer
*cell
= NULL
;
748 GtkTreeViewColumn
*col
= NULL
;
749 GtkTreeModel
*global_vars_model
= NULL
;
752 global_vars_model
= snippets_db_get_global_vars_model (snippets_manager_plugin
->snippets_db
);
753 g_return_if_fail (GTK_IS_TREE_MODEL (global_vars_model
));
754 g_return_if_fail (GTK_IS_TREE_VIEW (global_vars_view
));
756 /* Set up the model */
757 gtk_tree_view_set_model (global_vars_view
,
760 /* Set up the name cell */
761 cell
= gtk_cell_renderer_text_new ();
762 col
= gtk_tree_view_column_new ();
763 gtk_tree_view_column_set_title (col
, _("Name"));
764 gtk_tree_view_column_set_sizing (col
, GTK_TREE_VIEW_COLUMN_AUTOSIZE
);
765 gtk_tree_view_column_set_resizable (col
, TRUE
);
766 gtk_tree_view_column_pack_start (col
, cell
, FALSE
);
767 gtk_tree_view_column_set_cell_data_func (col
, cell
,
768 global_vars_view_name_data_func
,
770 gtk_tree_view_append_column (global_vars_view
, col
);
771 g_signal_connect (G_OBJECT (cell
),
773 G_CALLBACK (on_global_vars_name_changed
),
774 snippets_manager_plugin
->snippets_db
);
776 /* Set up the type cell */
777 cell
= gtk_cell_renderer_toggle_new ();
778 col
= gtk_tree_view_column_new ();
779 gtk_tree_view_column_set_title (col
, _("Command?"));
780 gtk_tree_view_column_set_sizing (col
, GTK_TREE_VIEW_COLUMN_AUTOSIZE
);
781 gtk_tree_view_column_set_resizable (col
, TRUE
);
782 gtk_tree_view_column_pack_start (col
, cell
, FALSE
);
783 gtk_tree_view_column_set_cell_data_func (col
, cell
,
784 global_vars_view_type_data_func
,
786 gtk_tree_view_append_column (global_vars_view
, col
);
787 g_signal_connect (G_OBJECT (cell
),
789 G_CALLBACK (on_global_vars_type_toggled
),
790 snippets_manager_plugin
->snippets_db
);
792 /* Set up the text cell */
793 cell
= gtk_cell_renderer_text_new ();
794 col
= gtk_tree_view_column_new ();
795 gtk_tree_view_column_set_title (col
, _("Variable text"));
796 gtk_tree_view_column_set_sizing (col
, GTK_TREE_VIEW_COLUMN_AUTOSIZE
);
797 gtk_tree_view_column_set_resizable (col
, TRUE
);
798 gtk_tree_view_column_pack_start (col
, cell
, FALSE
);
799 gtk_tree_view_column_set_cell_data_func (col
, cell
,
800 global_vars_view_text_data_func
,
801 snippets_manager_plugin
->snippets_db
,
803 gtk_tree_view_append_column (global_vars_view
, col
);
804 g_signal_connect (G_OBJECT (cell
),
806 G_CALLBACK (on_global_vars_text_changed
),
807 snippets_manager_plugin
->snippets_db
);
809 /* Set up the instant value cell */
810 cell
= gtk_cell_renderer_text_new ();
811 g_object_set (cell
, "editable", FALSE
, NULL
);
812 col
= gtk_tree_view_column_new ();
813 gtk_tree_view_column_set_title (col
, _("Instant value"));
814 gtk_tree_view_column_set_sizing (col
, GTK_TREE_VIEW_COLUMN_AUTOSIZE
);
815 gtk_tree_view_column_set_resizable (col
, TRUE
);
816 gtk_tree_view_column_pack_start (col
, cell
, FALSE
);
817 gtk_tree_view_column_set_cell_data_func (col
, cell
,
818 global_vars_view_value_data_func
,
819 snippets_manager_plugin
->snippets_db
,
821 gtk_tree_view_append_column (global_vars_view
, col
);
826 on_add_variable_b_clicked (GtkButton
*button
,
829 GlobalVariablesUpdateData
*update_data
= (GlobalVariablesUpdateData
*)user_data
;
830 GtkTreeView
*global_vars_view
= NULL
;
831 GtkTreeModel
*global_vars_model
= NULL
;
832 SnippetsDB
*snippets_db
= NULL
;
834 gboolean iter_has_next
= TRUE
;
838 g_return_if_fail (ANJUTA_IS_SNIPPETS_DB (update_data
->snippets_db
));
839 g_return_if_fail (GTK_IS_TREE_VIEW (update_data
->global_vars_view
));
840 snippets_db
= ANJUTA_SNIPPETS_DB (update_data
->snippets_db
);
841 global_vars_view
= GTK_TREE_VIEW (update_data
->global_vars_view
);
842 global_vars_model
= snippets_db_get_global_vars_model (snippets_db
);
844 /* Insert it into the SnippetsDB */
845 snippets_db_add_global_variable (snippets_db
,
847 GLOBAL_VAR_NEW_VALUE
,
850 /* Get to the new inserted variable */
851 iter_has_next
= gtk_tree_model_get_iter_first (global_vars_model
, &iter
);
852 while (iter_has_next
)
854 gtk_tree_model_get (global_vars_model
, &iter
,
855 GLOBAL_VARS_MODEL_COL_NAME
, &name
,
857 if (!g_strcmp0 (name
, GLOBAL_VAR_NEW_NAME
))
859 GtkTreePath
*path
= gtk_tree_model_get_path (global_vars_model
, &iter
);
861 gtk_tree_view_set_cursor (global_vars_view
,
863 gtk_tree_view_get_column (global_vars_view
, 0),
866 gtk_tree_path_free (path
);
872 iter_has_next
= gtk_tree_model_iter_next (global_vars_model
, &iter
);
875 /* Save the global variables */
876 snippets_db_save_global_vars (snippets_db
);
880 on_delete_variable_b_clicked (GtkButton
*button
,
883 GlobalVariablesUpdateData
*update_data
= (GlobalVariablesUpdateData
*)user_data
;
884 GtkTreeView
*global_vars_view
= NULL
;
885 GtkTreeModel
*global_vars_model
= NULL
;
886 SnippetsDB
*snippets_db
= NULL
;
887 GtkTreeSelection
*global_vars_selection
= NULL
;
889 gboolean has_selection
= FALSE
;
893 g_return_if_fail (ANJUTA_IS_SNIPPETS_DB (update_data
->snippets_db
));
894 g_return_if_fail (GTK_IS_TREE_VIEW (update_data
->global_vars_view
));
895 snippets_db
= ANJUTA_SNIPPETS_DB (update_data
->snippets_db
);
896 global_vars_view
= GTK_TREE_VIEW (update_data
->global_vars_view
);
897 global_vars_model
= snippets_db_get_global_vars_model (snippets_db
);
898 global_vars_selection
= gtk_tree_view_get_selection (global_vars_view
);
900 /* Get the selected iter */
901 has_selection
= gtk_tree_selection_get_selected (global_vars_selection
,
905 /* If there is a selection delete the selected item */
908 gtk_tree_model_get (global_vars_model
, &iter
,
909 GLOBAL_VARS_MODEL_COL_NAME
, &name
,
911 snippets_db_remove_global_variable (snippets_db
, name
);
916 /* Save the global variables */
917 snippets_db_save_global_vars (snippets_db
);
921 ipreferences_merge (IAnjutaPreferences
* ipref
,
922 AnjutaPreferences
* prefs
,
925 GError
* error
= NULL
;
926 GtkBuilder
* bxml
= gtk_builder_new ();
927 GtkTreeView
*global_vars_view
= NULL
;
928 GtkButton
*add_variable_b
= NULL
, *delete_variable_b
= NULL
;
929 SnippetsManagerPlugin
*snippets_manager_plugin
= NULL
;
930 GlobalVariablesUpdateData
*global_vars_update_data
= NULL
;
933 snippets_manager_plugin
= ANJUTA_PLUGIN_SNIPPETS_MANAGER (ipref
);
934 g_return_if_fail (ANJUTA_IS_PLUGIN_SNIPPETS_MANAGER (snippets_manager_plugin
));
936 if (!gtk_builder_add_from_file (bxml
, PREFERENCES_UI
, &error
))
938 g_warning ("Couldn't load preferences ui file: %s", error
->message
);
939 g_error_free (error
);
941 anjuta_preferences_add_from_builder (prefs
, bxml
, snippets_manager_plugin
->settings
,
942 SNIPPETS_MANAGER_PREFERENCES_ROOT
, _("Code Snippets"),
945 /* Get the Gtk objects */
946 global_vars_view
= GTK_TREE_VIEW (gtk_builder_get_object (bxml
, "global_vars_view"));
947 add_variable_b
= GTK_BUTTON (gtk_builder_get_object (bxml
, "add_var_button"));
948 delete_variable_b
= GTK_BUTTON (gtk_builder_get_object (bxml
, "delete_var_button"));
949 g_return_if_fail (GTK_IS_TREE_VIEW (global_vars_view
));
950 g_return_if_fail (GTK_IS_BUTTON (add_variable_b
));
951 g_return_if_fail (GTK_IS_BUTTON (delete_variable_b
));
953 /* Set up the Global Variables GtkTreeView */
954 set_up_global_variables_view (snippets_manager_plugin
, global_vars_view
);
956 /* Connect the addition/deletion buttons */
957 global_vars_update_data
= g_malloc (sizeof (GlobalVariablesUpdateData
));
958 global_vars_update_data
->snippets_db
= snippets_manager_plugin
->snippets_db
;
959 global_vars_update_data
->global_vars_view
= global_vars_view
;
961 g_signal_connect (G_OBJECT (add_variable_b
),
963 G_CALLBACK (on_add_variable_b_clicked
),
964 global_vars_update_data
);
966 g_signal_connect (G_OBJECT (delete_variable_b
),
968 G_CALLBACK (on_delete_variable_b_clicked
),
969 global_vars_update_data
);
971 g_object_unref (bxml
);
975 ipreferences_unmerge (IAnjutaPreferences
* ipref
,
976 AnjutaPreferences
* prefs
,
979 anjuta_preferences_remove_page (prefs
, _("Code Snippets"));
983 ipreferences_iface_init (IAnjutaPreferencesIface
* iface
)
985 iface
->merge
= ipreferences_merge
;
986 iface
->unmerge
= ipreferences_unmerge
;
990 ANJUTA_PLUGIN_BEGIN (SnippetsManagerPlugin
, snippets_manager_plugin
);
991 ANJUTA_PLUGIN_ADD_INTERFACE (isnippets_manager
, IANJUTA_TYPE_SNIPPETS_MANAGER
);
992 ANJUTA_PLUGIN_ADD_INTERFACE (ipreferences
, IANJUTA_TYPE_PREFERENCES
);
995 ANJUTA_SIMPLE_PLUGIN (SnippetsManagerPlugin
, snippets_manager_plugin
);