The Battle for Wesnoth  1.17.17+dev
formula_debugger.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2023
3  by Yurii Chernyi <terraninfo@terraninfo.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
19 
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 <functional>
29 
30 namespace gui2::dialogs
31 {
32 
33 REGISTER_DIALOG(formula_debugger)
34 
35 void formula_debugger::pre_show(window& window)
36 {
37  // stack label
38  scroll_label* stack_label
39  = find_widget<scroll_label>(&window, "stack", false, true);
40 
41  std::stringstream stack_text;
42  std::string indent = " ";
43  int c = 0;
44  for(const auto & i : fdb_.get_call_stack())
45  {
46  for(int d = 0; d < c; ++d) {
47  stack_text << indent;
48  }
49  stack_text << "#<span color=\"#00ff00\">" << i.counter()
50  << "</span>: \"<span color=\"#00ff00\">" << font::escape_text(i.name())
51  << "</span>\": (" << font::escape_text(i.str()) << ") " << std::endl;
52  ++c;
53  }
54 
55  stack_label->set_use_markup(true);
56  stack_label->set_label(stack_text.str());
58  window.keyboard_capture(stack_label);
59 
60  // execution trace label
61  scroll_label* execution_label
62  = find_widget<scroll_label>(&window, "execution", false, true);
63 
64  std::stringstream execution_text;
65  for(const auto & i : fdb_.get_execution_trace())
66  {
67  for(int d = 0; d < i.level(); ++d) {
68  execution_text << indent;
69  }
70  if(!i.evaluated()) {
71  execution_text << "#<span color=\"#00ff00\">" << i.counter()
72  << "</span>: \"<span color=\"#00ff00\">" << font::escape_text(i.name())
73  << "</span>\": (" << font::escape_text(i.str()) << ") " << std::endl;
74  } else {
75  execution_text << "#<span color=\"#ffff00\">" << i.counter()
76  << "</span>: \"<span color=\"#ffff00\">" << font::escape_text(i.name())
77  << "</span>\": (" << font::escape_text(i.str()) << ") = "
78  << "<span color=\"#ffa500\">"
79  << font::escape_text(i.value().to_debug_string())
80  << "</span>" << std::endl;
81  }
82  }
83 
84  execution_label->set_use_markup(true);
85  execution_label->set_label(execution_text.str());
87  // state
88  std::string state_str;
89  bool is_end = false;
90  if(!fdb_.get_current_breakpoint()) {
91  state_str = "";
92  } else {
93  state_str = fdb_.get_current_breakpoint()->name();
94  if(state_str == "End") {
95  is_end = true;
96  }
97  }
98 
99  find_widget<styled_widget>(&window, "state", false).set_label(state_str);
100 
101  // callbacks
102  button& step_button = find_widget<button>(&window, "step", false);
104  step_button,
105  std::bind(&formula_debugger::callback_step_button, this));
106 
107  button& stepout_button = find_widget<button>(&window, "stepout", false);
109  stepout_button,
111 
112  button& next_button = find_widget<button>(&window, "next", false);
114  next_button,
115  std::bind(&formula_debugger::callback_next_button, this));
116 
117  button& continue_button = find_widget<button>(&window, "continue", false);
119  continue_button,
121 
122  if(is_end) {
123  step_button.set_active(false);
124  stepout_button.set_active(false);
125  next_button.set_active(false);
126  continue_button.set_active(false);
127  }
128 }
129 
131 {
134 }
135 
137 {
140 }
141 
143 {
146 }
147 
149 {
152 }
153 
154 } // namespace dialogs
Simple push button.
Definition: button.hpp:37
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: button.cpp:65
This shows the debugger for the formulas.
wfl::formula_debugger & fdb_
Label showing a text.
virtual void set_label(const t_string &label) override
See styled_widget::set_label.
virtual void set_use_markup(bool use_markup) override
See styled_widget::set_use_markup.
@ END
Go to the end position.
Definition: scrollbar.hpp:61
void scroll_vertical_scrollbar(const scrollbar_base::scroll_mode scroll)
Scrolls the vertical scrollbar.
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:67
void set_retval(const int retval, const bool close_window=true)
Sets there return value of the window.
Definition: window.hpp:358
void keyboard_capture(widget *widget)
Definition: window.cpp:1130
void add_breakpoint_step_into()
Definition: debugger.cpp:368
void add_breakpoint_continue_to_end()
Definition: debugger.cpp:361
void add_breakpoint_step_out()
Definition: debugger.cpp:375
Formula AI debugger.
std::size_t i
Definition: function.cpp:968
This file contains the window object, this object is a top level container which has the event manage...
static int indent
Definition: log.cpp:55
#define REGISTER_DIALOG(window_id)
Wrapper for REGISTER_DIALOG2.
std::string escape_text(const std::string &text)
Escapes the pango markup characters in a text.
Definition: escape.hpp:33
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:179
@ OK
Dialog was closed with the OK button.
Definition: retval.hpp:35
This file contains the settings handling of the widget library.
mock_char c
#define d