The Battle for Wesnoth  1.13.4+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 - 2016 by Yurii Chernyi <[email protected]>
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 
27 #include "utils/functional.hpp"
28 
29 namespace gui2
30 {
31 
32 /*WIKI
33  * @page = GUIWindowDefinitionWML
34  * @order = 2_formula_debugger
35  *
36  * == Formula debugger ==
37  *
38  * This shows the debugger for the formulas.
39  *
40  * @begin{table}{dialog_widgets}
41  *
42  * stack & & control & m &
43  * A stack. $
44  *
45  * execution & & control & m &
46  * Execution trace label. $
47  *
48  * state & & control & m &
49  * The state. $
50  *
51  * step & & button & m &
52  * Button to step into the execution. $
53  *
54  * stepout & & button & m &
55  * Button to step out of the execution. $
56  *
57  * next & & button & m &
58  * Button to execute the next statement. $
59  *
60  * continue & & button & m &
61  * Button to continue the execution. $
62  *
63  * @end{table}
64  */
65 
66 REGISTER_DIALOG(formula_debugger)
67 
68 void tformula_debugger::pre_show(twindow& window)
69 {
70  // stack label
71  tscroll_label* stack_label
72  = find_widget<tscroll_label>(&window, "stack", false, true);
73 
74  std::stringstream stack_text;
75  std::string indent = " ";
76  int c = 0;
77  for(const auto & i : fdb_.get_call_stack())
78  {
79  for(int d = 0; d < c; ++d) {
80  stack_text << indent;
81  }
82  stack_text << "#<span color=\"green\">" << i.counter()
83  << "</span>: \"<span color=\"green\">" << font::escape_text(i.name())
84  << "</span>\": (" << font::escape_text(i.str()) << ") " << std::endl;
85  ++c;
86  }
87 
88  stack_label->set_use_markup(true);
89  stack_label->set_label(stack_text.str());
91  window.keyboard_capture(stack_label);
92 
93  // execution trace label
94  tscroll_label* execution_label
95  = find_widget<tscroll_label>(&window, "execution", false, true);
96 
97  std::stringstream execution_text;
98  for(const auto & i : fdb_.get_execution_trace())
99  {
100  for(int d = 0; d < i.level(); ++d) {
101  execution_text << indent;
102  }
103  if(!i.evaluated()) {
104  execution_text << "#<span color=\"green\">" << i.counter()
105  << "</span>: \"<span color=\"green\">" << font::escape_text(i.name())
106  << "</span>\": (" << font::escape_text(i.str()) << ") " << std::endl;
107  } else {
108  execution_text << "#<span color=\"yellow\">" << i.counter()
109  << "</span>: \"<span color=\"yellow\">" << font::escape_text(i.name())
110  << "</span>\": (" << font::escape_text(i.str()) << ") = "
111  << "<span color=\"orange\">"
112  << font::escape_text(i.value().to_debug_string(nullptr, false))
113  << "</span>" << std::endl;
114  }
115  }
116 
117  execution_label->set_use_markup(true);
118  execution_label->set_label(execution_text.str());
120  // state
121  std::string state_str;
122  bool is_end = false;
123  if(!fdb_.get_current_breakpoint()) {
124  state_str = "";
125  } else {
126  state_str = fdb_.get_current_breakpoint()->name();
127  if(state_str == "End") {
128  is_end = true;
129  }
130  }
131 
132  find_widget<tcontrol>(&window, "state", false).set_label(state_str);
133 
134  // callbacks
135  tbutton& step_button = find_widget<tbutton>(&window, "step", false);
137  step_button,
139  this,
140  std::ref(window)));
141 
142  tbutton& stepout_button = find_widget<tbutton>(&window, "stepout", false);
144  stepout_button,
146  this,
147  std::ref(window)));
148 
149  tbutton& next_button = find_widget<tbutton>(&window, "next", false);
151  next_button,
153  this,
154  std::ref(window)));
155 
156  tbutton& continue_button = find_widget<tbutton>(&window, "continue", false);
158  continue_button,
160  this,
161  std::ref(window)));
162 
163  if(is_end) {
164  step_button.set_active(false);
165  stepout_button.set_active(false);
166  next_button.set_active(false);
167  continue_button.set_active(false);
168  }
169 }
170 
172 {
174  window.set_retval(twindow::OK);
175 }
176 
178 {
180  window.set_retval(twindow::OK);
181 }
182 
184 {
186  window.set_retval(twindow::OK);
187 }
188 
190 {
192  window.set_retval(twindow::OK);
193 }
194 
195 } // end of namespace gui2
virtual void set_active(const bool active) override
See tcontrol::set_active.
Definition: button.cpp:58
GLvoid **typedef void(GLAPIENTRY *PFNGLGETVERTEXATTRIBDVPROC)(GLuint
Definition: glew.h:1806
const GLfloat * c
Definition: glew.h:12741
Formula AI debugger.
This file contains the window object, this object is a top level container which has the event manage...
REGISTER_DIALOG(label_settings)
void scroll_vertical_scrollbar(const tscrollbar_::tscroll scroll)
Scrolls the vertical scrollbar.
Go to the end position.
Definition: scrollbar.hpp:61
void connect_signal_mouse_left_click(tdispatcher &dispatcher, const tsignal_function &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.hpp:710
virtual void set_use_markup(bool use_markup) override
See tcontrol::set_use_markup.
#define d
game_logic::formula_debugger & fdb_
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
Simple push button.
Definition: button.hpp:32
virtual void set_label(const t_string &label) override
See tcontrol::set_label.
Dialog is closed with ok button.
Definition: window.hpp:125
This file contains the settings handling of the widget library.
void callback_continue_button(twindow &window)
void set_retval(const int retval, const bool close_window=true)
Sets there return value of the window.
Definition: window.hpp:422
void callback_next_button(twindow &window)
Label showing a text.
static int indent
Definition: log.cpp:45
std::string escape_text(const std::string &text)
Escapes the markup characters in a text.
Definition: text.cpp:75
size_t i
Definition: function.cpp:1057
GLenum GLint ref
Definition: glew.h:1813
#define c
Definition: glew.h:12743
static void set_label(twindow &window, const std::string &id, const std::string &label)
Definition: unit_attack.cpp:89
void callback_step_button(twindow &window)
void callback_stepout_button(twindow &window)
GLsizei const GLcharARB ** string
Definition: glew.h:4503