1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
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 *---------------------------------------------------------------------------*/
34 /*---------------------------------------------------------------------------*/
36 /* Widget and signal name found in glade file
37 *---------------------------------------------------------------------------*/
39 #define TOOL_LIST "list_dialog"
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 #define LIST_RESPONSE_SIGNAL "on_list_dialog_response"
47 #define TOOL_ADD_SIGNAL "on_tool_add"
48 #define TOOL_ACTIVATED_SIGNAL "on_tool_activated"
49 #define TOOL_EDIT_SIGNAL "on_tool_edit"
50 #define TOOL_DELETE_SIGNAL "on_tool_delete"
51 #define TOOL_UP_SIGNAL "on_tool_up"
52 #define TOOL_DOWN_SIGNAL "on_tool_down"
54 /* column of the list view */
56 ATP_TOOLS_ENABLED_COLUMN
,
57 ATP_TOOLS_NAME_COLUMN
,
58 ATP_TOOLS_DATA_COLUMN
,
62 /* Useful function used by GUI code
63 *---------------------------------------------------------------------------*/
66 get_current_tool (const ATPToolDialog
*this)
69 GtkTreeSelection
*sel
;
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);
85 update_sensitivity (const ATPToolDialog
*this)
88 gboolean tool_selected
;
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
);
107 get_current_writable_tool (ATPToolDialog
*this)
111 tool
= get_current_tool(this);
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
);
125 *---------------------------------------------------------------------------*/
128 on_tool_add (GtkButton
*button
, gpointer user_data
)
130 ATPToolDialog
*this = (ATPToolDialog
*)user_data
;
134 tool
= get_current_tool (this);
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
);
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
);
152 on_tool_edit (GtkButton
*button
, gpointer user_data
)
154 ATPToolDialog
*this = (ATPToolDialog
*)user_data
;
158 /* Warning button could be NULL (called from on_row_activated) */
160 tool
= get_current_writable_tool (this);
163 ted
= atp_tool_editor_new (tool
, &this->tedl
, this);
164 atp_tool_editor_show (ted
);
169 on_tool_delete (GtkButton
*button
, gpointer user_data
)
171 ATPToolDialog
*this = (ATPToolDialog
*)user_data
;
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
), _("Are you sure you want to delete the '%s' tool?"), atp_user_tool_get_name(tool
)))
179 atp_user_tool_free (tool
);
180 atp_tool_dialog_refresh (this, NULL
);
186 on_tool_up (GtkButton
*button
, gpointer user_data
)
188 ATPToolDialog
*this = (ATPToolDialog
*)user_data
;
192 tool
= get_current_writable_tool (this);
195 prev
= atp_user_tool_previous (tool
);
198 if (atp_user_tool_get_storage (prev
) <= ATP_TSTORE_GLOBAL
)
200 /* global tool are read only so create a writeable one */
201 prev
= atp_user_tool_clone_new (prev
, ATP_TSTORE_LOCAL
);
203 atp_user_tool_move_after (prev
, tool
);
204 atp_tool_dialog_refresh (this, atp_user_tool_get_name (tool
));
210 on_tool_down (GtkButton
*button
, gpointer user_data
)
212 ATPToolDialog
*this = (ATPToolDialog
*)user_data
;
216 tool
= get_current_writable_tool (this);
219 next
= atp_user_tool_next (tool
);
222 atp_user_tool_move_after (tool
, next
);
223 atp_tool_dialog_refresh (this, atp_user_tool_get_name (tool
));
229 on_tool_enable (GtkCellRendererToggle
*cell_renderer
, const gchar
*path
, gpointer user_data
)
231 ATPToolDialog
*this = (ATPToolDialog
*)user_data
;
236 model
= gtk_tree_view_get_model (this->view
);
237 if (gtk_tree_model_get_iter_from_string (model
, &iter
, path
))
239 gtk_tree_model_get (model
, &iter
, ATP_TOOLS_DATA_COLUMN
, &tool
, -1);
240 atp_user_tool_set_flag (tool
, ATP_TOOL_ENABLE
| ATP_TOGGLE
);
242 gtk_list_store_set (GTK_LIST_STORE(model
), &iter
, ATP_TOOLS_ENABLED_COLUMN
,
243 atp_user_tool_get_flag (tool
, ATP_TOOL_ENABLE
), -1);
248 on_tool_activated (GtkTreeView
*treeview
, GtkTreePath
*path
, GtkTreeViewColumn
*col
, gpointer user_data
)
250 on_tool_edit (NULL
, user_data
);
254 on_tool_selection_changed (GtkTreeSelection
*selection
, gpointer user_data
)
256 ATPToolDialog
*this = (ATPToolDialog
*)user_data
;
258 update_sensitivity (this);
262 on_list_response (GtkDialog
*dialog
, gint res
, gpointer user_data
)
264 ATPToolDialog
*this = (ATPToolDialog
*)user_data
;
266 /* Just save tool list */
267 atp_anjuta_tools_save(this->plugin
);
270 atp_tool_dialog_close (this);
274 *---------------------------------------------------------------------------*/
277 atp_tool_dialog_refresh (const ATPToolDialog
*this, const gchar
* select
)
281 GtkTreeSelection
*selection
;
283 /* Disable changed signal to avoid changing buttons sensitivity */
284 selection
= gtk_tree_view_get_selection (this->view
);
285 g_signal_handler_block (selection
, this->changed_sig
);
287 /* Regenerate the tool list */
288 model
= gtk_tree_view_get_model (this->view
);
289 gtk_list_store_clear (GTK_LIST_STORE(model
));
290 tool
= atp_tool_list_first (atp_plugin_get_tool_list (this->plugin
));
295 gtk_list_store_append (GTK_LIST_STORE(model
), &iter
);
296 gtk_list_store_set (GTK_LIST_STORE(model
), &iter
,
297 ATP_TOOLS_ENABLED_COLUMN
,
298 atp_user_tool_get_flag (tool
, ATP_TOOL_ENABLE
),
299 ATP_TOOLS_NAME_COLUMN
,
300 atp_user_tool_get_name (tool
),
301 ATP_TOOLS_DATA_COLUMN
, tool
,
303 if ((select
!= NULL
) && (strcmp(select
, atp_user_tool_get_name (tool
)) == 0))
305 gtk_tree_selection_select_iter (selection
, &iter
);
307 tool
= atp_user_tool_next (tool
);
311 /* Regenerate the tool menu */
312 atp_tool_list_activate (atp_plugin_get_tool_list (this->plugin
));
314 /* Restore changed signal */
315 g_signal_handler_unblock (selection
, this->changed_sig
);
316 update_sensitivity(this);
319 /* Start the tool lister and editor */
322 atp_tool_dialog_show (ATPToolDialog
* this)
326 GtkCellRenderer
*renderer
;
327 GtkTreeViewColumn
*column
;
328 GtkTreeSelection
*selection
;
330 if (this->dialog
!= NULL
)
332 /* dialog is already displayed */
333 gtk_window_present (GTK_WINDOW (this->dialog
));
337 /* Load glade file */
338 if (NULL
== (xml
= glade_xml_new(GLADE_FILE
, TOOL_LIST
, NULL
)))
340 anjuta_util_dialog_error(atp_plugin_get_app_window (this->plugin
), _("Unable to build user interface for tool list"));
343 this->dialog
= GTK_DIALOG(glade_xml_get_widget(xml
, TOOL_LIST
));
344 gtk_widget_show (GTK_WIDGET(this->dialog
));
345 gtk_window_set_transient_for (GTK_WINDOW (this->dialog
), atp_plugin_get_app_window (this->plugin
));
347 /* Create tree view */
348 this->view
= GTK_TREE_VIEW (glade_xml_get_widget(xml
, TOOL_TREEVIEW
));
349 model
= GTK_TREE_MODEL (gtk_list_store_new (ATP_N_TOOLS_COLUMNS
,
350 G_TYPE_BOOLEAN
, G_TYPE_STRING
, G_TYPE_POINTER
));
351 gtk_tree_view_set_model (this->view
, model
);
353 renderer
= gtk_cell_renderer_toggle_new ();
354 g_signal_connect (G_OBJECT (renderer
), "toggled", G_CALLBACK (on_tool_enable
), this);
355 column
= gtk_tree_view_column_new_with_attributes ("", renderer
,
356 "active", ATP_TOOLS_ENABLED_COLUMN
, NULL
);
357 gtk_tree_view_append_column (this->view
, column
);
359 renderer
= gtk_cell_renderer_text_new ();
360 column
= gtk_tree_view_column_new_with_attributes (_("Tool"), renderer
,
361 "text", ATP_TOOLS_NAME_COLUMN
, NULL
);
362 gtk_tree_view_append_column (this->view
, column
);
363 g_object_unref (model
);
365 /* Get all buttons */
366 this->edit_bt
= glade_xml_get_widget(xml
, TOOL_EDIT_BUTTON
);
367 this->delete_bt
= glade_xml_get_widget(xml
, TOOL_DELETE_BUTTON
);
368 this->up_bt
= glade_xml_get_widget(xml
, TOOL_UP_BUTTON
);
369 this->down_bt
= glade_xml_get_widget(xml
, TOOL_DOWN_BUTTON
);
371 /* Connect all signals */
372 glade_xml_signal_connect_data (xml
, LIST_RESPONSE_SIGNAL
, GTK_SIGNAL_FUNC (on_list_response
), this);
373 glade_xml_signal_connect_data (xml
, TOOL_ADD_SIGNAL
, GTK_SIGNAL_FUNC (on_tool_add
), this);
374 glade_xml_signal_connect_data (xml
, TOOL_ACTIVATED_SIGNAL
, GTK_SIGNAL_FUNC (on_tool_activated
), this);
375 glade_xml_signal_connect_data (xml
, TOOL_EDIT_SIGNAL
, GTK_SIGNAL_FUNC (on_tool_edit
), this);
376 glade_xml_signal_connect_data (xml
, TOOL_DELETE_SIGNAL
, GTK_SIGNAL_FUNC (on_tool_delete
), this);
377 glade_xml_signal_connect_data (xml
, TOOL_UP_SIGNAL
, GTK_SIGNAL_FUNC (on_tool_up
), this);
378 glade_xml_signal_connect_data (xml
, TOOL_DOWN_SIGNAL
, GTK_SIGNAL_FUNC (on_tool_down
), this);
379 selection
= gtk_tree_view_get_selection (this->view
);
380 this->changed_sig
= g_signal_connect (G_OBJECT (selection
), "changed", G_CALLBACK (on_tool_selection_changed
), this);
382 g_object_unref (xml
);
384 atp_tool_dialog_refresh (this, NULL
);
390 atp_tool_dialog_close (ATPToolDialog
*this)
392 atp_tool_editor_list_destroy (&this->tedl
);
396 gtk_widget_destroy (GTK_WIDGET(this->dialog
));
402 *---------------------------------------------------------------------------*/
405 atp_tool_dialog_get_window (const ATPToolDialog
*this)
407 return GTK_WINDOW (this->dialog
);
411 atp_tool_dialog_get_variable (const ATPToolDialog
*this)
413 return atp_plugin_get_variable (this->plugin
);
416 /* Creation and Destruction
417 *---------------------------------------------------------------------------*/
420 atp_tool_dialog_construct (ATPToolDialog
*this, ATPPlugin
*plugin
)
422 this->plugin
= plugin
;
424 atp_tool_editor_list_construct (&this->tedl
);
428 atp_tool_dialog_destroy (ATPToolDialog
*this)
430 atp_tool_dialog_close (this);