Updated Spanish translation
[anjuta-git-plugin.git] / plugins / debug-manager / watch.c
blob2be6f4404f7d9178cf1656f7961fa513b27a8384
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * watch.c Copyright (C) 2000 Kh. Naba Kumar Singh
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA */
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
22 #include "watch.h"
24 #include "debug_tree.h"
25 #include "plugin.h"
27 /*#define DEBUG*/
28 #include <libanjuta/anjuta-debug.h>
29 #include <libanjuta/interfaces/ianjuta-editor-selection.h>
30 #include <libanjuta/interfaces/ianjuta-editor.h>
31 #include <libanjuta/interfaces/ianjuta-document-manager.h>
33 /* Type
34 *---------------------------------------------------------------------------*/
36 struct _ExprWatch
38 AnjutaPlugin *plugin;
40 GtkWidget *scrolledwindow;
41 DebugTree *debug_tree;
42 IAnjutaDebugger *debugger;
44 /* Menu action */
45 GtkActionGroup *action_group;
46 GtkActionGroup *toggle_group;
49 struct _InspectDialog
51 DebugTree *tree;
52 GtkWidget *treeview;
53 GtkWidget *dialog;
56 typedef struct _InspectDialog InspectDialog;
58 /* Widget and signal name found in glade file
59 *---------------------------------------------------------------------------*/
61 #define ADD_WATCH_DIALOG "add_watch_dialog"
62 #define CHANGE_WATCH_DIALOG "change_watch_dialog"
63 #define INSPECT_EVALUATE_DIALOG "watch_dialog"
64 #define NAME_ENTRY "name_entry"
65 #define VALUE_ENTRY "value_entry"
66 #define VALUE_TREE "value_treeview"
67 #define AUTO_UPDATE_CHECK "auto_update_check"
69 /* Private functions
70 *---------------------------------------------------------------------------*/
72 #if 0
73 static void
74 on_entry_updated (const gchar *value, gpointer user_data, GError *err)
76 GtkWidget *entry = GTK_WIDGET (user_data);
78 gtk_entry_set_text (GTK_ENTRY (entry), value);
79 gtk_widget_unref (entry);
81 #endif
83 static void
84 debug_tree_inspect_evaluate_dialog (ExprWatch * ew, const gchar* expression)
86 GladeXML *gxml;
87 gint reply;
88 gchar *new_expr;
89 // const gchar *value;
90 InspectDialog dlg;
91 IAnjutaDebuggerVariable var = {NULL, NULL, NULL, NULL, FALSE, -1};
93 gxml = glade_xml_new (GLADE_FILE, INSPECT_EVALUATE_DIALOG, NULL);
94 dlg.dialog = glade_xml_get_widget (gxml, INSPECT_EVALUATE_DIALOG);
95 gtk_window_set_transient_for (GTK_WINDOW (dlg.dialog),
96 NULL);
97 dlg.treeview = glade_xml_get_widget (gxml, VALUE_TREE);
98 g_object_unref (gxml);
100 /* Create debug tree */
101 dlg.tree = debug_tree_new_with_view (ANJUTA_PLUGIN (ew->plugin), GTK_TREE_VIEW (dlg.treeview));
102 if (ew->debugger)
103 debug_tree_connect (dlg.tree, ew->debugger);
104 if (expression != NULL)
106 var.expression = (gchar *)expression;
107 debug_tree_add_watch (dlg.tree, &var, FALSE);
109 else
111 debug_tree_add_dummy (dlg.tree, NULL);
114 for(;;)
116 reply = gtk_dialog_run (GTK_DIALOG (dlg.dialog));
117 switch (reply)
119 case GTK_RESPONSE_OK:
120 /* Add in watch window */
121 new_expr = debug_tree_get_first (dlg.tree);
123 if (new_expr != NULL)
125 var.expression = new_expr;
126 debug_tree_add_watch (ew->debug_tree, &var, FALSE);
127 g_free (new_expr);
129 break;
130 default:
131 break;
133 break;
135 debug_tree_free (dlg.tree);
136 gtk_widget_destroy (dlg.dialog);
139 static void
140 debug_tree_add_watch_dialog (ExprWatch *ew, const gchar* expression)
142 GladeXML *gxml;
143 GtkWidget *dialog;
144 GtkWidget *name_entry;
145 GtkWidget *auto_update_check;
146 gint reply;
147 IAnjutaDebuggerVariable var = {NULL, NULL, NULL, NULL, FALSE, -1};
149 gxml = glade_xml_new (GLADE_FILE, ADD_WATCH_DIALOG, NULL);
150 dialog = glade_xml_get_widget (gxml, ADD_WATCH_DIALOG);
151 gtk_window_set_transient_for (GTK_WINDOW (dialog),
152 NULL);
153 auto_update_check = glade_xml_get_widget (gxml, AUTO_UPDATE_CHECK);
154 name_entry = glade_xml_get_widget (gxml, NAME_ENTRY);
155 g_object_unref (gxml);
157 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (auto_update_check), TRUE);
158 gtk_entry_set_text (GTK_ENTRY (name_entry), expression == NULL ? "" : expression);
160 reply = gtk_dialog_run (GTK_DIALOG (dialog));
161 if (reply == GTK_RESPONSE_OK)
163 var.expression = (gchar *)gtk_entry_get_text (GTK_ENTRY (name_entry));
164 debug_tree_add_watch (ew->debug_tree, &var,
165 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (auto_update_check)));
167 gtk_widget_destroy (dialog);
170 static void
171 debug_tree_change_watch_dialog (ExprWatch *ew, GtkTreeIter* iter)
173 #if 0
174 GladeXML *gxml;
175 GtkWidget *dialog;
176 GtkWidget *name_entry;
177 GtkWidget *value_entry;
178 gint reply;
179 TrimmableItem *item = NULL;
180 GtkTreeModel* model = NULL;
182 model = gtk_tree_view_get_model(d_tree->view);
183 gtk_tree_model_get (model, iter, ITEM_COLUMN, &item, -1);
185 gxml = glade_xml_new (GLADE_FILE, CHANGE_WATCH_DIALOG, NULL);
186 dialog = glade_xml_get_widget (gxml, CHANGE_WATCH_DIALOG);
187 gtk_window_set_transient_for (GTK_WINDOW (dialog),
188 NULL);
189 name_entry = glade_xml_get_widget (gxml, NAME_ENTRY);
190 value_entry = glade_xml_get_widget (gxml, VALUE_ENTRY);
191 g_object_unref (gxml);
193 gtk_widget_grab_focus (value_entry);
194 gtk_entry_set_text (GTK_ENTRY (name_entry), &item->name[1]);
195 gtk_entry_set_text (GTK_ENTRY (value_entry), item->value);
197 reply = gtk_dialog_run (GTK_DIALOG (dialog));
198 if (reply == GTK_RESPONSE_APPLY)
200 debug_tree_evaluate (d_tree, iter, gtk_entry_get_text (GTK_ENTRY (value_entry)));
202 gtk_widget_destroy (dialog);
203 #endif
206 /* Menu call backs
207 *---------------------------------------------------------------------------*/
209 static void
210 on_debug_tree_inspect (GtkAction *action, gpointer user_data)
212 ExprWatch * ew = (ExprWatch *)user_data;
213 GObject *obj;
214 IAnjutaDocumentManager *docman = NULL;
215 IAnjutaEditor *te = NULL;
216 gchar *expression = NULL;
218 /* Get current editor and line */
219 obj = anjuta_shell_get_object (ANJUTA_PLUGIN (ew->plugin)->shell,
220 "IAnjutaDocumentManager", NULL /* TODO */);
221 docman = IANJUTA_DOCUMENT_MANAGER (obj);
222 if (docman != NULL)
224 te = ianjuta_document_manager_get_current_editor (docman, NULL);
225 expression = ianjuta_editor_selection_get (IANJUTA_EDITOR_SELECTION (te), NULL);
226 if (expression == NULL)
228 expression = ianjuta_editor_get_current_word (IANJUTA_EDITOR (te), NULL);
232 debug_tree_inspect_evaluate_dialog (ew, expression);
233 g_free (expression);
236 static void
237 on_debug_tree_add_watch (GtkAction *action, gpointer user_data)
239 ExprWatch * ew = (ExprWatch *)user_data;
241 debug_tree_add_watch_dialog (ew, NULL);
244 static void
245 on_debug_tree_remove_watch (GtkAction *action, gpointer user_data)
247 ExprWatch * ew = (ExprWatch *)user_data;
248 GtkTreeIter iter;
250 if (debug_tree_get_current (ew->debug_tree, &iter))
252 debug_tree_remove (ew->debug_tree, &iter);
256 static void
257 on_debug_tree_update_watch (GtkAction *action, gpointer user_data)
259 ExprWatch * ew = (ExprWatch *)user_data;
260 GtkTreeIter iter;
262 if (debug_tree_get_current (ew->debug_tree, &iter))
264 debug_tree_update (ew->debug_tree, &iter, TRUE);
268 static void
269 on_debug_tree_auto_update_watch (GtkAction *action, gpointer user_data)
271 ExprWatch * ew = (ExprWatch *)user_data;
272 GtkTreeIter iter;
274 if (debug_tree_get_current (ew->debug_tree, &iter))
276 gboolean state;
278 state = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
279 debug_tree_set_auto_update (ew->debug_tree, &iter, state);
283 static void
284 on_debug_tree_edit_watch (GtkAction *action, gpointer user_data)
286 ExprWatch * ew = (ExprWatch *)user_data;
287 GtkTreeIter iter;
289 if (debug_tree_get_current (ew->debug_tree, &iter))
291 debug_tree_change_watch_dialog (ew, &iter);
295 static void
296 on_debug_tree_update_all_watch (GtkAction *action, gpointer user_data)
298 ExprWatch * ew = (ExprWatch *)user_data;
300 debug_tree_update_all (ew->debug_tree);
303 static void
304 on_debug_tree_remove_all_watch (GtkAction *action, gpointer user_data)
306 ExprWatch * ew = (ExprWatch *)user_data;
308 debug_tree_remove_all (ew->debug_tree);
311 static gboolean
312 on_debug_tree_button_press (GtkWidget *widget, GdkEventButton *bevent, gpointer user_data)
314 ExprWatch * ew = (ExprWatch *)user_data;
316 if (bevent->button == 3)
318 GtkAction *action;
319 AnjutaUI *ui;
320 GtkTreeIter iter;
321 GtkWidget *middle_click_menu;
323 ui = anjuta_shell_get_ui (ew->plugin->shell, NULL);
324 action = anjuta_ui_get_action (ui, "ActionGroupWatchToggle", "ActionDmaAutoUpdateWatch");
325 if (debug_tree_get_current (ew->debug_tree, &iter))
327 gtk_action_set_sensitive (GTK_ACTION (action), TRUE);
328 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), debug_tree_get_auto_update (ew->debug_tree, &iter));
330 else
332 gtk_action_set_sensitive (GTK_ACTION (action), FALSE);
335 action = anjuta_ui_get_action (ui, "ActionGroupWatch", "ActionDmaEditWatch");
336 gtk_action_set_sensitive (GTK_ACTION (action), FALSE); // FIXME: Not implemented
338 middle_click_menu = gtk_ui_manager_get_widget (GTK_UI_MANAGER (ui), "/PopupWatch");
339 g_return_val_if_fail (middle_click_menu != NULL, FALSE);
340 gtk_menu_popup (GTK_MENU (middle_click_menu), NULL, NULL, NULL, NULL,
341 bevent->button, bevent->time);
344 return FALSE;
347 /* Actions table
348 *---------------------------------------------------------------------------*/
350 static GtkActionEntry actions_watch[] = {
352 "ActionDmaInspect", /* Action name */
353 GTK_STOCK_DIALOG_INFO, /* Stock icon, if any */
354 N_("Ins_pect/Evaluate..."), /* Display label */
355 NULL, /* short-cut */
356 N_("Inspect or evaluate an expression or variable"), /* Tooltip */
357 G_CALLBACK (on_debug_tree_inspect) /* action callback */
360 "ActionDmaAddWatch",
361 NULL,
362 N_("Add Watch..."),
363 NULL,
364 NULL,
365 G_CALLBACK (on_debug_tree_add_watch)
368 "ActionDmaRemoveWatch",
369 NULL,
370 N_("Remove Watch"),
371 NULL,
372 NULL,
373 G_CALLBACK (on_debug_tree_remove_watch)
376 "ActionDmaUpdateWatch",
377 NULL,
378 N_("Update Watch"),
379 NULL,
380 NULL,
381 G_CALLBACK (on_debug_tree_update_watch)
384 "ActionDmaEditWatch",
385 NULL,
386 N_("Change Value"),
387 NULL,
388 NULL,
389 G_CALLBACK (on_debug_tree_edit_watch)
392 "ActionDmaUpdateAllWatch",
393 NULL,
394 N_("Update all"),
395 NULL,
396 NULL,
397 G_CALLBACK (on_debug_tree_update_all_watch)
400 "ActionDmaRemoveAllWatch",
401 NULL,
402 N_("Remove all"),
403 NULL,
404 NULL,
405 G_CALLBACK (on_debug_tree_remove_all_watch)
409 static GtkToggleActionEntry toggle_watch[] = {
411 "ActionDmaAutoUpdateWatch", /* Action name */
412 NULL, /* Stock icon, if any */
413 N_("Automatic update"), /* Display label */
414 NULL, /* short-cut */
415 NULL, /* Tooltip */
416 G_CALLBACK (on_debug_tree_auto_update_watch), /* action callback */
417 FALSE /* Initial state */
421 static void
422 create_expr_watch_gui (ExprWatch * ew)
424 AnjutaUI *ui;
426 ew->debug_tree = debug_tree_new (ew->plugin);
427 ew->scrolledwindow = gtk_scrolled_window_new (NULL, NULL);
428 gtk_widget_show (ew->scrolledwindow);
429 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (ew->scrolledwindow),
430 GTK_POLICY_AUTOMATIC,
431 GTK_POLICY_AUTOMATIC);
432 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (ew->scrolledwindow),
433 GTK_SHADOW_IN);
434 gtk_container_add (GTK_CONTAINER (ew->scrolledwindow), debug_tree_get_tree_widget (ew->debug_tree));
436 ui = anjuta_shell_get_ui (ew->plugin->shell, NULL);
437 ew->action_group =
438 anjuta_ui_add_action_group_entries (ui, "ActionGroupWatch",
439 _("Watch operations"),
440 actions_watch,
441 G_N_ELEMENTS (actions_watch),
442 GETTEXT_PACKAGE, TRUE, ew);
443 ew->toggle_group =
444 anjuta_ui_add_toggle_action_group_entries (ui, "ActionGroupWatchToggle",
445 _("Watch operations"),
446 toggle_watch,
447 G_N_ELEMENTS (toggle_watch),
448 GETTEXT_PACKAGE, TRUE, ew);
449 g_signal_connect (debug_tree_get_tree_widget (ew->debug_tree), "button-press-event", G_CALLBACK (on_debug_tree_button_press), ew);
451 gtk_widget_show_all (ew->scrolledwindow);
454 /* Public function
455 *---------------------------------------------------------------------------*/
457 void
458 expr_watch_cmd_queqe (ExprWatch *ew)
460 debug_tree_update_all (ew->debug_tree);
463 void
464 expr_watch_connect (ExprWatch *ew, IAnjutaDebugger *debugger)
466 ew->debugger = debugger;
467 debug_tree_connect (ew->debug_tree, debugger);
470 /* Callback for saving session
471 *---------------------------------------------------------------------------*/
473 static void
474 on_session_save (AnjutaShell *shell, AnjutaSessionPhase phase, AnjutaSession *session, ExprWatch *ew)
476 GList *list;
478 if (phase != ANJUTA_SESSION_PHASE_NORMAL)
479 return;
481 list = debug_tree_get_full_watch_list (ew->debug_tree);
482 if (list != NULL)
483 anjuta_session_set_string_list (session, "Debugger", "Watch", list);
484 g_list_foreach (list, (GFunc)g_free, NULL);
485 g_list_free (list);
488 static void
489 on_session_load (AnjutaShell *shell, AnjutaSessionPhase phase, AnjutaSession *session, ExprWatch *ew)
491 GList *list;
493 if (phase != ANJUTA_SESSION_PHASE_NORMAL)
494 return;
496 /* debug_tree_remove_all (ew->debug_tree); */
497 list = anjuta_session_get_string_list (session, "Debugger", "Watch");
498 if (list != NULL)
499 debug_tree_add_full_watch_list (ew->debug_tree, list);
502 /* Constructor & Destructor
503 *---------------------------------------------------------------------------*/
505 ExprWatch *
506 expr_watch_new (AnjutaPlugin *plugin, IAnjutaDebugger *debugger)
508 ExprWatch *ew;
510 ew = g_new0 (ExprWatch, 1);
511 ew->plugin = plugin;
512 create_expr_watch_gui (ew);
513 g_object_ref (debugger);
514 ew->debugger = debugger;
516 /* Connect to Load and Save event */
517 g_signal_connect (plugin->shell, "save-session",
518 G_CALLBACK (on_session_save), ew);
519 g_signal_connect (plugin->shell, "load-session",
520 G_CALLBACK (on_session_load), ew);
522 /* Add watch window */
523 anjuta_shell_add_widget (plugin->shell,
524 ew->scrolledwindow,
525 "AnjutaDebuggerWatch", _("Watches"),
526 "gdb-watch-icon", ANJUTA_SHELL_PLACEMENT_BOTTOM,
527 NULL);
530 return ew;
533 void
534 expr_watch_destroy (ExprWatch * ew)
536 AnjutaUI *ui;
538 g_return_if_fail (ew != NULL);
540 /* Disconnect from Load and Save event */
541 g_signal_handlers_disconnect_by_func (ew->plugin->shell, G_CALLBACK (on_session_save), ew);
542 g_signal_handlers_disconnect_by_func (ew->plugin->shell, G_CALLBACK (on_session_load), ew);
544 ui = anjuta_shell_get_ui (ew->plugin->shell, NULL);
545 anjuta_ui_remove_action_group (ui, ew->action_group);
546 anjuta_ui_remove_action_group (ui, ew->toggle_group);
548 debug_tree_free (ew->debug_tree);
549 gtk_widget_destroy (ew->scrolledwindow);
550 g_free (ew);