The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
fire_event.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2011 - 2025
3 by Mark de Wever <koraq@xs4all.nl>
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// In this domain since it compares a shared string from this domain.
17#define GETTEXT_DOMAIN "wesnoth-lib"
18
19#include "config_cache.hpp"
20#include "gui/widgets/grid.hpp"
21#include "gui/widgets/label.hpp"
22
23#include <boost/test/unit_test.hpp>
24
25#include <functional>
26
27#include <sstream>
28
29
30static void print(std::stringstream& sstr
31 , const std::string& queue
32 , const std::string& id)
33{
34 sstr << queue << ':' << id << '\n';
35}
36
37template<gui2::event::ui_event E>
39 std::stringstream& sstr
40 , gui2::widget& widget)
41{
42 widget.connect_signal<E>(
43 std::bind(print, std::ref(sstr), "pre", widget.id())
45
46 widget.connect_signal<E>(
47 std::bind(print, std::ref(sstr), "child", widget.id())
49
50 widget.connect_signal<E>(
51 std::bind(print, std::ref(sstr), "post", widget.id())
53}
54
55static void connect_signals(
56 std::stringstream& sstr
57 , gui2::widget& widget)
58{
59 /** @todo Add the rest of the events. */
60 connect_queue<gui2::event::DRAW>(sstr, widget);
61 connect_queue<gui2::event::CLOSE_WINDOW>(sstr, widget);
62 connect_queue<gui2::event::MOUSE_ENTER>(sstr, widget);
63 connect_queue<gui2::event::MOUSE_LEAVE>(sstr, widget);
64 connect_queue<gui2::event::LEFT_BUTTON_DOWN>(sstr, widget);
65 connect_queue<gui2::event::LEFT_BUTTON_UP>(sstr, widget);
66 connect_queue<gui2::event::LEFT_BUTTON_CLICK>(sstr, widget);
67 connect_queue<gui2::event::LEFT_BUTTON_DOUBLE_CLICK>(sstr, widget);
68 connect_queue<gui2::event::MIDDLE_BUTTON_DOWN>(sstr, widget);
69 connect_queue<gui2::event::MIDDLE_BUTTON_UP>(sstr, widget);
70 connect_queue<gui2::event::MIDDLE_BUTTON_CLICK>(sstr, widget);
71 connect_queue<gui2::event::MIDDLE_BUTTON_DOUBLE_CLICK>(sstr, widget);
72 connect_queue<gui2::event::RIGHT_BUTTON_DOWN>(sstr, widget);
73 connect_queue<gui2::event::RIGHT_BUTTON_UP>(sstr, widget);
74 connect_queue<gui2::event::RIGHT_BUTTON_CLICK>(sstr, widget);
75 connect_queue<gui2::event::RIGHT_BUTTON_DOUBLE_CLICK>(sstr, widget);
76}
77
78static void add_widget(gui2::grid& grid
79 , std::unique_ptr<gui2::widget> widget
80 , const std::string& id
81 , const unsigned row
82 , const unsigned column)
83{
84 BOOST_REQUIRE_NE(widget.get(), static_cast<gui2::widget*>(nullptr));
85
86 widget->set_id(id);
87 grid.set_child(std::move(widget)
88 , row
89 , column
92 , 0);
93}
94
95static std::string set_event_order()
96{
97 return
98 "pre:root\n"
99 "pre:level 1\n"
100 "pre:level 2\n"
101 "child:level 2\n"
102 "post:level 2\n"
103 "post:level 1\n"
104 "post:root\n";
105}
106
107/** @todo Add the rest of the events. */
108static void validate_draw(std::stringstream& sstr)
109{
110 BOOST_CHECK_EQUAL(sstr.str(), set_event_order());
111}
112
113static void validate_right_button_down(std::stringstream& sstr)
114{
115 BOOST_CHECK_EQUAL(sstr.str(), set_event_order());
116}
117
118BOOST_AUTO_TEST_CASE(test_fire_event)
119{
120 /**** Initialize the environment. *****/
122
123 cache.clear_defines();
124 cache.add_define("EDITOR");
125 cache.add_define("MULTIPLAYER");
126
127 std::stringstream sstr;
128
129 /**** Initialize the grid. *****/
130 gui2::grid grid(1, 1);
131 grid.set_id("root");
132 connect_signals(sstr, grid);
133
134 auto g1 = std::make_unique<gui2::grid>(1, 1);
135 gui2::grid* g1_ptr = g1.get();
136
137 add_widget(grid, std::move(g1), "level 1", 0, 0);
138 connect_signals(sstr, *g1_ptr);
139
140 auto g2 = std::make_unique<gui2::grid>(1, 1);
141 gui2::grid* g2_ptr = g2.get();
142
143 add_widget(*g1_ptr, std::move(g2), "level 2", 0, 0);
144 connect_signals(sstr, *g2_ptr);
145
146 /** @todo Add the rest of the events. */
147 sstr.str("");
148 grid.fire(gui2::event::DRAW, *g2_ptr);
149 validate_draw(sstr);
150
151 sstr.str("");
154}
Singleton class to manage game config file caching.
static config_cache & instance()
Get reference to the singleton object.
void connect_signal(const F &func, const queue_position position=back_child)
Adds a callback to the appropriate queue based on event type.
bool fire(const ui_event event, widget &target)
Fires an event which has no extra parameters.
Base container class.
Definition grid.hpp:32
static const unsigned HORIZONTAL_GROW_SEND_TO_CLIENT
Definition grid.hpp:56
static const unsigned VERTICAL_GROW_SEND_TO_CLIENT
Definition grid.hpp:49
void set_child(std::unique_ptr< widget > widget, const unsigned row, const unsigned col, const unsigned flags, const unsigned border_size)
Sets a child in the grid.
Definition grid.cpp:71
Base class for all widgets.
Definition widget.hpp:55
void set_id(const std::string &id)
Definition widget.cpp:98
const std::string & id() const
Definition widget.cpp:110
BOOST_AUTO_TEST_CASE(test_fire_event)
static void validate_draw(std::stringstream &sstr)
static void validate_right_button_down(std::stringstream &sstr)
static void print(std::stringstream &sstr, const std::string &queue, const std::string &id)
void connect_queue(std::stringstream &sstr, gui2::widget &widget)
static std::string set_event_order()
static void connect_signals(std::stringstream &sstr, gui2::widget &widget)
static void add_widget(gui2::grid &grid, std::unique_ptr< gui2::widget > widget, const std::string &id, const unsigned row, const unsigned column)
@ RIGHT_BUTTON_DOWN
Definition handler.hpp:128
std::unordered_map< std::string, cache_item > cache
Definition sound.cpp:146