Updated Makefile.am files after make -f git.mk
[anjuta.git] / plugins / tools / dialog.c
blob3e0d3748be39662aeda8f13a8b15325f0610b57e
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 dialog.c
4 Copyright (C) 2003 Biswapesh Chattopadhyay
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, Boston, MA 02110-1301 USA
22 * First tool configuratin dialog, presenting all tools in a list.
23 * In this dialog, you can enable, disable, add, remove and order tools.
25 *---------------------------------------------------------------------------*/
27 #include <config.h>
29 #include "dialog.h"
31 #include "editor.h"
32 #include "fileop.h"
34 /*---------------------------------------------------------------------------*/
36 /* Widget and signal name found in gtk builder file
37 *---------------------------------------------------------------------------*/
39 #define TOOL_LIST "list_tools"
40 #define TOOL_TREEVIEW "tools_treeview"
41 #define TOOL_EDIT_BUTTON "edit_bt"
42 #define TOOL_DELETE_BUTTON "delete_bt"
43 #define TOOL_UP_BUTTON "up_bt"
44 #define TOOL_DOWN_BUTTON "down_bt"
46 /* Gtk builder callbacks */
47 void atp_on_tool_add (GtkButton *button, gpointer user_data);
48 void atp_on_tool_activated (GtkTreeView *treeview, GtkTreePath *path, GtkTreeViewColumn *col, gpointer user_data);
49 void atp_on_tool_edit (GtkButton *button, gpointer user_data);
50 void atp_on_tool_delete (GtkButton *button, gpointer user_data);
51 void atp_on_tool_up (GtkButton *button, gpointer user_data);
52 void atp_on_tool_down (GtkButton *button, gpointer user_data);
54 /* column of the list view */
55 enum {
56 ATP_TOOLS_ENABLED_COLUMN,
57 ATP_TOOLS_NAME_COLUMN,
58 ATP_TOOLS_DATA_COLUMN,
59 ATP_N_TOOLS_COLUMNS
62 /* Useful function used by GUI code
63 *---------------------------------------------------------------------------*/
65 static ATPUserTool*
66 get_current_tool (const ATPToolDialog *this)
68 GtkTreeModel *model;
69 GtkTreeSelection *sel;
70 GtkTreeIter iter;
71 ATPUserTool *tool;
73 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (this->view));
74 if (gtk_tree_selection_get_selected (sel, &model, &iter))
76 gtk_tree_model_get (model, &iter, ATP_TOOLS_DATA_COLUMN, &tool, -1);
78 return tool;
81 return NULL;
84 static void
85 update_sensitivity (const ATPToolDialog *this)
87 ATPUserTool *tool;
88 gboolean tool_selected;
89 gboolean tool_up;
90 gboolean tool_down;
91 gboolean tool_deletable;
93 /* Get current selected tool */
94 tool = get_current_tool (this);
95 tool_selected = tool != NULL;
96 tool_up = tool_selected && (atp_user_tool_previous (tool) != NULL);
97 tool_down = tool_selected && (atp_user_tool_next (tool) != NULL);
98 tool_deletable = tool_selected && (atp_user_tool_get_storage (tool) > ATP_TSTORE_GLOBAL);
100 gtk_widget_set_sensitive(this->edit_bt, tool_selected);
101 gtk_widget_set_sensitive(this->delete_bt, tool_deletable);
102 gtk_widget_set_sensitive(this->up_bt, tool_up);
103 gtk_widget_set_sensitive(this->down_bt, tool_down);
106 static ATPUserTool*
107 get_current_writable_tool (ATPToolDialog *this)
109 ATPUserTool *tool;
111 tool = get_current_tool(this);
112 if (tool)
114 if (atp_user_tool_get_storage (tool) <= ATP_TSTORE_GLOBAL)
116 /* global tool are read only so create a writeable one */
117 tool = atp_user_tool_clone_new (tool, ATP_TSTORE_LOCAL);
121 return tool;
124 /* Call backs
125 *---------------------------------------------------------------------------*/
127 void
128 atp_on_tool_add (GtkButton *button, gpointer user_data)
130 ATPToolDialog *this = (ATPToolDialog *)user_data;
131 ATPUserTool *tool;
132 ATPToolEditor *ted;
134 tool = get_current_tool (this);
136 if (tool == NULL)
138 /* Add the new tool at the end of the list */
139 tool = atp_tool_list_append_new (atp_plugin_get_tool_list (this->plugin), NULL, ATP_TSTORE_LOCAL);
141 else
143 /* Add tool after the current one */
144 tool = atp_user_tool_append_new (tool, NULL, ATP_TSTORE_LOCAL);
147 ted = atp_tool_editor_new (tool, &this->tedl, this);
148 atp_tool_editor_show (ted);
151 void
152 atp_on_tool_edit (GtkButton *button, gpointer user_data)
154 ATPToolDialog *this = (ATPToolDialog *)user_data;
155 ATPUserTool *tool;
156 ATPToolEditor *ted;
158 /* Warning button could be NULL (called from on_row_activated) */
160 tool = get_current_writable_tool (this);
161 if (tool != NULL)
163 ted = atp_tool_editor_new (tool, &this->tedl, this);
164 atp_tool_editor_show (ted);
168 void
169 atp_on_tool_delete (GtkButton *button, gpointer user_data)
171 ATPToolDialog *this = (ATPToolDialog *)user_data;
172 ATPUserTool *tool;
174 tool = get_current_tool (this);
175 if ((tool != NULL) && (atp_user_tool_get_storage (tool) > ATP_TSTORE_GLOBAL))
177 if (anjuta_util_dialog_boolean_question (GTK_WINDOW (this->dialog), FALSE,
178 _("Are you sure you want to delete the '%s' tool?"), atp_user_tool_get_name(tool)))
180 atp_user_tool_free (tool);
181 atp_tool_dialog_refresh (this, NULL);
186 void
187 atp_on_tool_up (GtkButton *button, gpointer user_data)
189 ATPToolDialog *this = (ATPToolDialog *)user_data;
190 ATPUserTool *tool;
191 ATPUserTool *prev;
193 tool = get_current_writable_tool (this);
194 if (tool != NULL)
196 prev = atp_user_tool_previous (tool);
197 if (prev != NULL)
199 if (atp_user_tool_get_storage (prev) <= ATP_TSTORE_GLOBAL)
201 /* global tool are read only so create a writeable one */
202 prev = atp_user_tool_clone_new (prev, ATP_TSTORE_LOCAL);
204 atp_user_tool_move_after (prev, tool);
205 atp_tool_dialog_refresh (this, atp_user_tool_get_name (tool));
210 void
211 atp_on_tool_down (GtkButton *button, gpointer user_data)
213 ATPToolDialog *this = (ATPToolDialog *)user_data;
214 ATPUserTool *tool;
215 ATPUserTool *next;
217 tool = get_current_writable_tool (this);
218 if (tool != NULL)
220 next = atp_user_tool_next (tool);
221 if (next != NULL)
223 atp_user_tool_move_after (tool, next);
224 atp_tool_dialog_refresh (this, atp_user_tool_get_name (tool));
229 void
230 atp_on_tool_activated (GtkTreeView *treeview, GtkTreePath *path, GtkTreeViewColumn *col, gpointer user_data)
232 atp_on_tool_edit (NULL, user_data);
235 static void
236 on_tool_enable (GtkCellRendererToggle *cell_renderer, const gchar *path, gpointer user_data)
238 ATPToolDialog *this = (ATPToolDialog *)user_data;
239 GtkTreeModel *model;
240 GtkTreeIter iter;
241 ATPUserTool *tool;
243 model = gtk_tree_view_get_model (this->view);
244 if (gtk_tree_model_get_iter_from_string (model, &iter, path))
246 gtk_tree_model_get (model, &iter, ATP_TOOLS_DATA_COLUMN, &tool, -1);
247 atp_user_tool_set_flag (tool, ATP_TOOL_ENABLE | ATP_TOGGLE);
249 gtk_list_store_set (GTK_LIST_STORE(model), &iter, ATP_TOOLS_ENABLED_COLUMN,
250 atp_user_tool_get_flag (tool, ATP_TOOL_ENABLE), -1);
254 static void
255 on_tool_selection_changed (GtkTreeSelection *selection, gpointer user_data)
257 ATPToolDialog *this = (ATPToolDialog *)user_data;
259 update_sensitivity (this);
262 /* Public functions
263 *---------------------------------------------------------------------------*/
265 void
266 atp_tool_dialog_refresh (const ATPToolDialog *this, const gchar* select)
268 ATPUserTool *tool;
269 GtkTreeModel *model;
270 GtkTreeSelection *selection;
272 /* Disable changed signal to avoid changing buttons sensitivity */
273 selection = gtk_tree_view_get_selection (this->view);
274 g_signal_handler_block (selection, this->changed_sig);
276 /* Regenerate the tool list */
277 model = gtk_tree_view_get_model (this->view);
278 gtk_list_store_clear (GTK_LIST_STORE(model));
279 tool = atp_tool_list_first (atp_plugin_get_tool_list (this->plugin));
280 while (tool)
282 GtkTreeIter iter;
284 gtk_list_store_append (GTK_LIST_STORE(model), &iter);
285 gtk_list_store_set (GTK_LIST_STORE(model), &iter,
286 ATP_TOOLS_ENABLED_COLUMN,
287 atp_user_tool_get_flag (tool, ATP_TOOL_ENABLE),
288 ATP_TOOLS_NAME_COLUMN,
289 atp_user_tool_get_name (tool),
290 ATP_TOOLS_DATA_COLUMN, tool,
291 -1);
292 if ((select != NULL) && (strcmp(select, atp_user_tool_get_name (tool)) == 0))
294 gtk_tree_selection_select_iter (selection, &iter);
296 tool = atp_user_tool_next (tool);
300 /* Regenerate the tool menu */
301 atp_tool_list_activate (atp_plugin_get_tool_list (this->plugin));
303 /* Restore changed signal */
304 g_signal_handler_unblock (selection, this->changed_sig);
305 update_sensitivity(this);
307 /* Just save tool list */
308 atp_anjuta_tools_save(this->plugin);
311 /* Start the tool lister and editor */
313 void
314 atp_tool_dialog_show (ATPToolDialog* this, GtkBuilder *bxml)
316 GtkTreeModel *model;
317 GtkCellRenderer *renderer;
318 GtkTreeViewColumn *column;
319 GtkTreeSelection *selection;
321 /* Get widgets */
322 anjuta_util_builder_get_objects (bxml,
323 TOOL_LIST, &this->dialog,
324 TOOL_TREEVIEW, &this->view,
325 TOOL_EDIT_BUTTON, &this->edit_bt,
326 TOOL_DELETE_BUTTON, &this->delete_bt,
327 TOOL_UP_BUTTON, &this->up_bt,
328 TOOL_DOWN_BUTTON, & this->down_bt,
329 NULL);
330 gtk_window_set_transient_for (GTK_WINDOW (this->dialog), atp_plugin_get_app_window (this->plugin));
332 /* Create tree view */
333 model = GTK_TREE_MODEL (gtk_list_store_new (ATP_N_TOOLS_COLUMNS,
334 G_TYPE_BOOLEAN, G_TYPE_STRING, G_TYPE_POINTER));
335 gtk_tree_view_set_model (this->view, model);
337 renderer = gtk_cell_renderer_toggle_new ();
338 g_signal_connect (G_OBJECT (renderer), "toggled", G_CALLBACK (on_tool_enable), this);
339 column = gtk_tree_view_column_new_with_attributes ("", renderer,
340 "active", ATP_TOOLS_ENABLED_COLUMN, NULL);
341 gtk_tree_view_append_column (this->view, column);
343 renderer = gtk_cell_renderer_text_new ();
344 column = gtk_tree_view_column_new_with_attributes (_("Tool"), renderer,
345 "text", ATP_TOOLS_NAME_COLUMN, NULL);
346 gtk_tree_view_append_column (this->view, column);
347 g_object_unref (model);
349 /* Connect all signals */
350 gtk_builder_connect_signals (bxml, this);
351 selection = gtk_tree_view_get_selection (this->view);
352 this->changed_sig = g_signal_connect (G_OBJECT (selection), "changed", G_CALLBACK (on_tool_selection_changed), this);
354 atp_tool_dialog_refresh (this, NULL);
357 void
358 atp_tool_dialog_close (ATPToolDialog *this)
360 atp_tool_editor_list_destroy (&this->tedl);
362 if (this->dialog)
364 gtk_widget_destroy (GTK_WIDGET(this->dialog));
365 this->dialog = NULL;
369 /* Access functions
370 *---------------------------------------------------------------------------*/
372 GtkWindow*
373 atp_tool_dialog_get_window (const ATPToolDialog *this)
375 return GTK_WINDOW (this->dialog);
378 ATPVariable*
379 atp_tool_dialog_get_variable (const ATPToolDialog *this)
381 return atp_plugin_get_variable (this->plugin);
384 /* Creation and Destruction
385 *---------------------------------------------------------------------------*/
387 void
388 atp_tool_dialog_construct (ATPToolDialog *this, ATPPlugin *plugin)
390 this->plugin = plugin;
391 this->dialog = NULL;
392 atp_tool_editor_list_construct (&this->tedl);
395 void
396 atp_tool_dialog_destroy (ATPToolDialog *this)
398 atp_tool_dialog_close (this);