The Battle for Wesnoth  1.13.11+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
formula_debugger.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2018 by Yurii Chernyi <terraninfo@terraninfo.net>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
20 #include "gui/dialogs/helper.hpp"
21 #include "gui/widgets/button.hpp"
23 #include "gui/widgets/settings.hpp"
24 #include "gui/widgets/window.hpp"
25 #include "formula/debugger.hpp"
26 #include "font/pango/escape.hpp"
27 
28 #include "utils/functional.hpp"
29 
30 namespace gui2
31 {
32 namespace dialogs
33 {
34 
35 /*WIKI
36  * @page = GUIWindowDefinitionWML
37  * @order = 2_formula_debugger
38  *
39  * == Formula debugger ==
40  *
41  * This shows the debugger for the formulas.
42  *
43  * @begin{table}{dialog_widgets}
44  *
45  * stack & & styled_widget & m &
46  * A stack. $
47  *
48  * execution & & styled_widget & m &
49  * Execution trace label. $
50  *
51  * state & & styled_widget & m &
52  * The state. $
53  *
54  * step & & button & m &
55  * Button to step into the execution. $
56  *
57  * stepout & & button & m &
58  * Button to step out of the execution. $
59  *
60  * next & & button & m &
61  * Button to execute the next statement. $
62  *
63  * continue & & button & m &
64  * Button to continue the execution. $
65  *
66  * @end{table}
67  */
68 
69 REGISTER_DIALOG(formula_debugger)
70 
71 void formula_debugger::pre_show(window& window)
72 {
73  // stack label
74  scroll_label* stack_label
75  = find_widget<scroll_label>(&window, "stack", false, true);
76 
77  std::stringstream stack_text;
78  std::string indent = " ";
79  int c = 0;
80  for(const auto & i : fdb_.get_call_stack())
81  {
82  for(int d = 0; d < c; ++d) {
83  stack_text << indent;
84  }
85  stack_text << "#<span color=\"green\">" << i.counter()
86  << "</span>: \"<span color=\"green\">" << font::escape_text(i.name())
87  << "</span>\": (" << font::escape_text(i.str()) << ") " << std::endl;
88  ++c;
89  }
90 
91  stack_label->set_use_markup(true);
92  stack_label->set_label(stack_text.str());
94  window.keyboard_capture(stack_label);
95 
96  // execution trace label
97  scroll_label* execution_label
98  = find_widget<scroll_label>(&window, "execution", false, true);
99 
100  std::stringstream execution_text;
101  for(const auto & i : fdb_.get_execution_trace())
102  {
103  for(int d = 0; d < i.level(); ++d) {
104  execution_text << indent;
105  }
106  if(!i.evaluated()) {
107  execution_text << "#<span color=\"green\">" << i.counter()
108  << "</span>: \"<span color=\"green\">" << font::escape_text(i.name())
109  << "</span>\": (" << font::escape_text(i.str()) << ") " << std::endl;
110  } else {
111  execution_text << "#<span color=\"yellow\">" << i.counter()
112  << "</span>: \"<span color=\"yellow\">" << font::escape_text(i.name())
113  << "</span>\": (" << font::escape_text(i.str()) << ") = "
114  << "<span color=\"orange\">"
115  << font::escape_text(i.value().to_debug_string())
116  << "</span>" << std::endl;
117  }
118  }
119 
120  execution_label->set_use_markup(true);
121  execution_label->set_label(execution_text.str());
123  // state
124  std::string state_str;
125  bool is_end = false;
126  if(!fdb_.get_current_breakpoint()) {
127  state_str = "";
128  } else {
129  state_str = fdb_.get_current_breakpoint()->name();
130  if(state_str == "End") {
131  is_end = true;
132  }
133  }
134 
135  find_widget<styled_widget>(&window, "state", false).set_label(state_str);
136 
137  // callbacks
138  button& step_button = find_widget<button>(&window, "step", false);
140  step_button,
142  this,
143  std::ref(window)));
144 
145  button& stepout_button = find_widget<button>(&window, "stepout", false);
147  stepout_button,
149  this,
150  std::ref(window)));
151 
152  button& next_button = find_widget<button>(&window, "next", false);
154  next_button,
156  this,
157  std::ref(window)));
158 
159  button& continue_button = find_widget<button>(&window, "continue", false);
161  continue_button,
163  this,
164  std::ref(window)));
165 
166  if(is_end) {
167  step_button.set_active(false);
168  stepout_button.set_active(false);
169  next_button.set_active(false);
170  continue_button.set_active(false);
171  }
172 }
173 
175 {
177  window.set_retval(window::OK);
178 }
179 
181 {
183  window.set_retval(window::OK);
184 }
185 
187 {
189  window.set_retval(window::OK);
190 }
191 
193 {
195  window.set_retval(window::OK);
196 }
197 
198 } // namespace dialogs
199 } // namespace gui2
Dialog is closed with ok button.
Definition: window.hpp:101
std::vector< char_t > string
virtual void set_use_markup(bool use_markup) override
See styled_widget::set_use_markup.
void add_breakpoint_step_out()
Definition: debugger.cpp:375
Formula AI debugger.
This file contains the window object, this object is a top level container which has the event manage...
void callback_next_button(window &window)
#define d
virtual void set_label(const t_string &label) override
See styled_widget::set_label.
Generic file dialog.
Definition: field-fwd.hpp:22
Go to the end position.
Definition: scrollbar.hpp:60
void callback_continue_button(window &window)
This file contains the settings handling of the widget library.
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal_function &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:218
Label showing a text.
static int indent
Definition: log.cpp:45
Various uncategorised dialogs.
std::string escape_text(const std::string &text)
Escapes the pango markup characters in a text.
Definition: escape.hpp:30
void scroll_vertical_scrollbar(const scrollbar_base::scroll_mode scroll)
Scrolls the vertical scrollbar.
void callback_step_button(window &window)
size_t i
Definition: function.cpp:933
void set_retval(const int retval, const bool close_window=true)
Sets there return value of the window.
Definition: window.hpp:392
void add_breakpoint_continue_to_end()
Definition: debugger.cpp:361
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: button.cpp:62
wfl::formula_debugger & fdb_
Simple push button.
Definition: button.hpp:35
mock_char c
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
void add_breakpoint_step_into()
Definition: debugger.cpp:368
void callback_stepout_button(window &window)