The Battle for Wesnoth  1.15.0-dev
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 https://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/widgets/button.hpp"
22 #include "gui/widgets/settings.hpp"
23 #include "gui/widgets/window.hpp"
24 #include "formula/debugger.hpp"
25 #include "font/pango/escape.hpp"
26 
27 #include "utils/functional.hpp"
28 
29 namespace gui2
30 {
31 namespace dialogs
32 {
33 
34 /*WIKI
35  * @page = GUIWindowDefinitionWML
36  * @order = 2_formula_debugger
37  *
38  * == Formula debugger ==
39  *
40  * This shows the debugger for the formulas.
41  *
42  * @begin{table}{dialog_widgets}
43  *
44  * stack & & styled_widget & m &
45  * A stack. $
46  *
47  * execution & & styled_widget & m &
48  * Execution trace label. $
49  *
50  * state & & styled_widget & m &
51  * The state. $
52  *
53  * step & & button & m &
54  * Button to step into the execution. $
55  *
56  * stepout & & button & m &
57  * Button to step out of the execution. $
58  *
59  * next & & button & m &
60  * Button to execute the next statement. $
61  *
62  * continue & & button & m &
63  * Button to continue the execution. $
64  *
65  * @end{table}
66  */
67 
68 REGISTER_DIALOG(formula_debugger)
69 
70 void formula_debugger::pre_show(window& window)
71 {
72  // stack label
73  scroll_label* stack_label
74  = find_widget<scroll_label>(&window, "stack", false, true);
75 
76  std::stringstream stack_text;
77  std::string indent = " ";
78  int c = 0;
79  for(const auto & i : fdb_.get_call_stack())
80  {
81  for(int d = 0; d < c; ++d) {
82  stack_text << indent;
83  }
84  stack_text << "#<span color=\"green\">" << i.counter()
85  << "</span>: \"<span color=\"green\">" << font::escape_text(i.name())
86  << "</span>\": (" << font::escape_text(i.str()) << ") " << std::endl;
87  ++c;
88  }
89 
90  stack_label->set_use_markup(true);
91  stack_label->set_label(stack_text.str());
93  window.keyboard_capture(stack_label);
94 
95  // execution trace label
96  scroll_label* execution_label
97  = find_widget<scroll_label>(&window, "execution", false, true);
98 
99  std::stringstream execution_text;
100  for(const auto & i : fdb_.get_execution_trace())
101  {
102  for(int d = 0; d < i.level(); ++d) {
103  execution_text << indent;
104  }
105  if(!i.evaluated()) {
106  execution_text << "#<span color=\"green\">" << i.counter()
107  << "</span>: \"<span color=\"green\">" << font::escape_text(i.name())
108  << "</span>\": (" << font::escape_text(i.str()) << ") " << std::endl;
109  } else {
110  execution_text << "#<span color=\"yellow\">" << i.counter()
111  << "</span>: \"<span color=\"yellow\">" << font::escape_text(i.name())
112  << "</span>\": (" << font::escape_text(i.str()) << ") = "
113  << "<span color=\"orange\">"
114  << font::escape_text(i.value().to_debug_string())
115  << "</span>" << std::endl;
116  }
117  }
118 
119  execution_label->set_use_markup(true);
120  execution_label->set_label(execution_text.str());
122  // state
123  std::string state_str;
124  bool is_end = false;
125  if(!fdb_.get_current_breakpoint()) {
126  state_str = "";
127  } else {
128  state_str = fdb_.get_current_breakpoint()->name();
129  if(state_str == "End") {
130  is_end = true;
131  }
132  }
133 
134  find_widget<styled_widget>(&window, "state", false).set_label(state_str);
135 
136  // callbacks
137  button& step_button = find_widget<button>(&window, "step", false);
139  step_button,
141  this,
142  std::ref(window)));
143 
144  button& stepout_button = find_widget<button>(&window, "stepout", false);
146  stepout_button,
148  this,
149  std::ref(window)));
150 
151  button& next_button = find_widget<button>(&window, "next", false);
153  next_button,
155  this,
156  std::ref(window)));
157 
158  button& continue_button = find_widget<button>(&window, "continue", false);
160  continue_button,
162  this,
163  std::ref(window)));
164 
165  if(is_end) {
166  step_button.set_active(false);
167  stepout_button.set_active(false);
168  next_button.set_active(false);
169  continue_button.set_active(false);
170  }
171 }
172 
174 {
176  window.set_retval(retval::OK);
177 }
178 
180 {
182  window.set_retval(retval::OK);
183 }
184 
186 {
188  window.set_retval(retval::OK);
189 }
190 
192 {
194  window.set_retval(retval::OK);
195 }
196 
197 } // namespace dialogs
198 } // namespace gui2
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:233
Label showing a text.
static int indent
Definition: log.cpp:44
Various uncategorised dialogs.
std::string escape_text(const std::string &text)
Escapes the pango markup characters in a text.
Definition: escape.hpp:32
std::size_t i
Definition: function.cpp:933
void scroll_vertical_scrollbar(const scrollbar_base::scroll_mode scroll)
Scrolls the vertical scrollbar.
void callback_step_button(window &window)
void set_retval(const int retval, const bool close_window=true)
Sets there return value of the window.
Definition: window.hpp:364
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
Dialog was closed with the OK button.
Definition: retval.hpp:34
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:63
void add_breakpoint_step_into()
Definition: debugger.cpp:368
void callback_stepout_button(window &window)