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/window.hpp"
23 #include "formula/debugger.hpp"
24 #include "font/pango/escape.hpp"
25 
26 #include "utils/functional.hpp"
27 
28 namespace gui2
29 {
30 namespace dialogs
31 {
32 
33 /*WIKI
34  * @page = GUIWindowDefinitionWML
35  * @order = 2_formula_debugger
36  *
37  * == Formula debugger ==
38  *
39  * This shows the debugger for the formulas.
40  *
41  * @begin{table}{dialog_widgets}
42  *
43  * stack & & styled_widget & m &
44  * A stack. $
45  *
46  * execution & & styled_widget & m &
47  * Execution trace label. $
48  *
49  * state & & styled_widget & m &
50  * The state. $
51  *
52  * step & & button & m &
53  * Button to step into the execution. $
54  *
55  * stepout & & button & m &
56  * Button to step out of the execution. $
57  *
58  * next & & button & m &
59  * Button to execute the next statement. $
60  *
61  * continue & & button & m &
62  * Button to continue the execution. $
63  *
64  * @end{table}
65  */
66 
67 REGISTER_DIALOG(formula_debugger)
68 
69 void formula_debugger::pre_show(window& window)
70 {
71  // stack label
72  scroll_label* stack_label
73  = find_widget<scroll_label>(&window, "stack", false, true);
74 
75  std::stringstream stack_text;
76  std::string indent = " ";
77  int c = 0;
78  for(const auto & i : fdb_.get_call_stack())
79  {
80  for(int d = 0; d < c; ++d) {
81  stack_text << indent;
82  }
83  stack_text << "#<span color=\"green\">" << i.counter()
84  << "</span>: \"<span color=\"green\">" << font::escape_text(i.name())
85  << "</span>\": (" << font::escape_text(i.str()) << ") " << std::endl;
86  ++c;
87  }
88 
89  stack_label->set_use_markup(true);
90  stack_label->set_label(stack_text.str());
92  window.keyboard_capture(stack_label);
93 
94  // execution trace label
95  scroll_label* execution_label
96  = find_widget<scroll_label>(&window, "execution", false, true);
97 
98  std::stringstream execution_text;
99  for(const auto & i : fdb_.get_execution_trace())
100  {
101  for(int d = 0; d < i.level(); ++d) {
102  execution_text << indent;
103  }
104  if(!i.evaluated()) {
105  execution_text << "#<span color=\"green\">" << i.counter()
106  << "</span>: \"<span color=\"green\">" << font::escape_text(i.name())
107  << "</span>\": (" << font::escape_text(i.str()) << ") " << std::endl;
108  } else {
109  execution_text << "#<span color=\"yellow\">" << i.counter()
110  << "</span>: \"<span color=\"yellow\">" << font::escape_text(i.name())
111  << "</span>\": (" << font::escape_text(i.str()) << ") = "
112  << "<span color=\"orange\">"
113  << font::escape_text(i.value().to_debug_string())
114  << "</span>" << std::endl;
115  }
116  }
117 
118  execution_label->set_use_markup(true);
119  execution_label->set_label(execution_text.str());
121  // state
122  std::string state_str;
123  bool is_end = false;
124  if(!fdb_.get_current_breakpoint()) {
125  state_str = "";
126  } else {
127  state_str = fdb_.get_current_breakpoint()->name();
128  if(state_str == "End") {
129  is_end = true;
130  }
131  }
132 
133  find_widget<styled_widget>(&window, "state", false).set_label(state_str);
134 
135  // callbacks
136  button& step_button = find_widget<button>(&window, "step", false);
138  step_button,
140  this,
141  std::ref(window)));
142 
143  button& stepout_button = find_widget<button>(&window, "stepout", false);
145  stepout_button,
147  this,
148  std::ref(window)));
149 
150  button& next_button = find_widget<button>(&window, "next", false);
152  next_button,
154  this,
155  std::ref(window)));
156 
157  button& continue_button = find_widget<button>(&window, "continue", false);
159  continue_button,
161  this,
162  std::ref(window)));
163 
164  if(is_end) {
165  step_button.set_active(false);
166  stepout_button.set_active(false);
167  next_button.set_active(false);
168  continue_button.set_active(false);
169  }
170 }
171 
173 {
175  window.set_retval(retval::OK);
176 }
177 
179 {
181  window.set_retval(retval::OK);
182 }
183 
185 {
187  window.set_retval(retval::OK);
188 }
189 
191 {
193  window.set_retval(retval::OK);
194 }
195 
196 } // namespace dialogs
197 } // 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:373
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)
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:163
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:345
void add_breakpoint_continue_to_end()
Definition: debugger.cpp:359
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:62
void add_breakpoint_step_into()
Definition: debugger.cpp:366
void callback_stepout_button(window &window)