The Battle for Wesnoth  1.19.0-dev
formula_debugger.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2024
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/window.hpp"
24 #include "formula/debugger.hpp"
25 #include "font/pango/escape.hpp"
26 
27 #include <functional>
28 
29 namespace gui2::dialogs
30 {
31 
32 REGISTER_DIALOG(formula_debugger)
33 
34 void formula_debugger::pre_show(window& window)
35 {
36  // stack label
37  scroll_label* stack_label
38  = find_widget<scroll_label>(&window, "stack", false, true);
39 
40  std::stringstream stack_text;
41  std::string indent = " ";
42  int c = 0;
43  for(const auto & i : fdb_.get_call_stack())
44  {
45  for(int d = 0; d < c; ++d) {
46  stack_text << indent;
47  }
48  stack_text << "#<span color=\"#00ff00\">" << i.counter()
49  << "</span>: \"<span color=\"#00ff00\">" << font::escape_text(i.name())
50  << "</span>\": (" << font::escape_text(i.str()) << ") " << std::endl;
51  ++c;
52  }
53 
54  stack_label->set_use_markup(true);
55  stack_label->set_label(stack_text.str());
57  window.keyboard_capture(stack_label);
58 
59  // execution trace label
60  scroll_label* execution_label
61  = find_widget<scroll_label>(&window, "execution", false, true);
62 
63  std::stringstream execution_text;
64  for(const auto & i : fdb_.get_execution_trace())
65  {
66  for(int d = 0; d < i.level(); ++d) {
67  execution_text << indent;
68  }
69  if(!i.evaluated()) {
70  execution_text << "#<span color=\"#00ff00\">" << i.counter()
71  << "</span>: \"<span color=\"#00ff00\">" << font::escape_text(i.name())
72  << "</span>\": (" << font::escape_text(i.str()) << ") " << std::endl;
73  } else {
74  execution_text << "#<span color=\"#ffff00\">" << i.counter()
75  << "</span>: \"<span color=\"#ffff00\">" << font::escape_text(i.name())
76  << "</span>\": (" << font::escape_text(i.str()) << ") = "
77  << "<span color=\"#ffa500\">"
78  << font::escape_text(i.value().to_debug_string())
79  << "</span>" << std::endl;
80  }
81  }
82 
83  execution_label->set_use_markup(true);
84  execution_label->set_label(execution_text.str());
86  // state
87  std::string state_str;
88  bool is_end = false;
89  if(!fdb_.get_current_breakpoint()) {
90  state_str = "";
91  } else {
92  state_str = fdb_.get_current_breakpoint()->name();
93  if(state_str == "End") {
94  is_end = true;
95  }
96  }
97 
98  find_widget<styled_widget>(&window, "state", false).set_label(state_str);
99 
100  // callbacks
101  button& step_button = find_widget<button>(&window, "step", false);
103  step_button,
104  std::bind(&formula_debugger::callback_step_button, this));
105 
106  button& stepout_button = find_widget<button>(&window, "stepout", false);
108  stepout_button,
110 
111  button& next_button = find_widget<button>(&window, "next", false);
113  next_button,
114  std::bind(&formula_debugger::callback_next_button, this));
115 
116  button& continue_button = find_widget<button>(&window, "continue", false);
118  continue_button,
120 
121  if(is_end) {
122  step_button.set_active(false);
123  stepout_button.set_active(false);
124  next_button.set_active(false);
125  continue_button.set_active(false);
126  }
127 }
128 
130 {
133 }
134 
136 {
139 }
140 
142 {
145 }
146 
148 {
151 }
152 
153 } // namespace dialogs
Simple push button.
Definition: button.hpp:36
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: button.cpp:63
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:58
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:63
void set_retval(const int retval, const bool close_window=true)
Sets there return value of the window.
Definition: window.hpp:399
void keyboard_capture(widget *widget)
Definition: window.cpp:1215
void add_breakpoint_step_into()
Definition: debugger.cpp:367
void add_breakpoint_continue_to_end()
Definition: debugger.cpp:360
void add_breakpoint_step_out()
Definition: debugger.cpp:374
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:57
std::string escape_text(const std::string &text)
Escapes the pango markup characters in a text.
Definition: escape.hpp:33
REGISTER_DIALOG(tod_new_schedule)
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:177
@ OK
Dialog was closed with the OK button.
Definition: retval.hpp:35
mock_char c
#define d