The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
fire_event.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2016 by Mark de Wever <[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 // In this domain since it compares a shared string from this domain.
16 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
18 #include "config_cache.hpp"
19 #include "gui/widgets/grid.hpp"
20 #include "gui/widgets/label.hpp"
21 
22 #include <boost/test/unit_test.hpp>
23 
24 #include "utils/functional.hpp"
25 
26 #include <sstream>
27 
28 
29 static void print(std::stringstream& sstr
30  , const std::string& queue
31  , const std::string& id)
32 {
33  sstr << queue << ':' << id << '\n';
34 }
35 
36 template<gui2::event::tevent E>
38  std::stringstream& sstr
39  , gui2::twidget& widget)
40 {
41  widget.connect_signal<E>(
42  std::bind(print, std::ref(sstr), "pre", widget.id())
44 
45  widget.connect_signal<E>(
46  std::bind(print, std::ref(sstr), "child", widget.id())
48 
49  widget.connect_signal<E>(
50  std::bind(print, std::ref(sstr), "post", widget.id())
52 }
53 
54 static void connect_signals(
55  std::stringstream& sstr
56  , gui2::twidget& widget)
57 {
58  /** @todo Add the rest of the events. */
59  connect_queue<gui2::event::DRAW>(sstr, widget);
60  connect_queue<gui2::event::CLOSE_WINDOW>(sstr, widget);
61  connect_queue<gui2::event::MOUSE_ENTER>(sstr, widget);
62  connect_queue<gui2::event::MOUSE_LEAVE>(sstr, widget);
63  connect_queue<gui2::event::LEFT_BUTTON_DOWN>(sstr, widget);
64  connect_queue<gui2::event::LEFT_BUTTON_UP>(sstr, widget);
65  connect_queue<gui2::event::LEFT_BUTTON_CLICK>(sstr, widget);
66  connect_queue<gui2::event::LEFT_BUTTON_DOUBLE_CLICK>(sstr, widget);
67  connect_queue<gui2::event::MIDDLE_BUTTON_DOWN>(sstr, widget);
68  connect_queue<gui2::event::MIDDLE_BUTTON_UP>(sstr, widget);
69  connect_queue<gui2::event::MIDDLE_BUTTON_CLICK>(sstr, widget);
70  connect_queue<gui2::event::MIDDLE_BUTTON_DOUBLE_CLICK>(sstr, widget);
71  connect_queue<gui2::event::RIGHT_BUTTON_DOWN>(sstr, widget);
72  connect_queue<gui2::event::RIGHT_BUTTON_UP>(sstr, widget);
73  connect_queue<gui2::event::RIGHT_BUTTON_CLICK>(sstr, widget);
74  connect_queue<gui2::event::RIGHT_BUTTON_DOUBLE_CLICK>(sstr, widget);
75 }
76 
78  , gui2::twidget* widget
79  , const std::string& id
80  , const unsigned row
81  , const unsigned column)
82 {
83  BOOST_REQUIRE_NE(widget, static_cast<gui2::twidget*>(nullptr));
84 
85  widget->set_id(id);
86  grid.set_child(widget
87  , row
88  , column
91  , 0);
92 }
93 
95 {
96  return
97  "pre:root\n"
98  "pre:level 1\n"
99  "child:level 2\n"
100  "post:level 1\n"
101  "post:root\n";
102 
103 }
104 
105 /** @todo Add the rest of the events. */
106 static void validate_draw(std::stringstream& sstr)
107 {
108  BOOST_CHECK_EQUAL(sstr.str(), set_event_order());
109 }
110 
111 static void validate_right_button_down(std::stringstream& sstr)
112 {
113  BOOST_CHECK_EQUAL(sstr.str(), set_event_order());
114 }
115 
116 BOOST_AUTO_TEST_CASE(test_fire_event)
117 {
118  /**** Initialize the environment. *****/
120 
121  cache.clear_defines();
122  cache.add_define("EDITOR");
123  cache.add_define("MULTIPLAYER");
124 
125  std::stringstream sstr;
126 
127  /**** Initialize the grid. *****/
128  gui2::tgrid grid(1, 1);
129  grid.set_id("root");
130  connect_signals(sstr, grid);
131 
132  gui2::tgrid *child_grid = new gui2::tgrid(1, 1);
133  add_widget(grid, child_grid, "level 1", 0, 0);
134  connect_signals(sstr, *child_grid);
135 
136  gui2::twidget *child = new gui2::tgrid(1, 1);
137  add_widget(*child_grid, child, "level 2", 0, 0);
138  connect_signals(sstr, *child);
139 
140  /** @todo Add the rest of the events. */
141  sstr.str("");
142  grid.fire(gui2::event::DRAW, *child);
143  validate_draw(sstr);
144 
145  sstr.str("");
146  grid.fire(gui2::event::RIGHT_BUTTON_DOWN, *child);
148 }
149 
static config_cache & instance()
Get reference to the singleton object.
static void validate_right_button_down(std::stringstream &sstr)
Definition: fire_event.cpp:111
static void add_widget(gui2::tgrid &grid, gui2::twidget *widget, const std::string &id, const unsigned row, const unsigned column)
Definition: fire_event.cpp:77
Base container class.
Definition: grid.hpp:29
static const unsigned VERTICAL_GROW_SEND_TO_CLIENT
Definition: grid.hpp:41
static const unsigned HORIZONTAL_GROW_SEND_TO_CLIENT
Definition: grid.hpp:48
boost::enable_if< boost::mpl::has_key< tset_event, boost::mpl::int_< E > > >::type connect_signal(const tsignal_function &signal, const tposition position=back_child)
Connect a signal for callback in tset_event.
Definition: dispatcher.hpp:278
static void connect_signals(std::stringstream &sstr, gui2::twidget &widget)
Definition: fire_event.cpp:54
See LEFT_BUTTON_DOWN.
Definition: handler.hpp:110
static std::string set_event_order()
Definition: fire_event.cpp:94
bool fire(const tevent event, twidget &target)
Fires an event which has no extra parameters.
Definition: dispatcher.cpp:144
Periodic redraw request.
Definition: handler.hpp:55
void set_id(const std::string &id)
Definition: widget.cpp:97
GLenum GLenum GLvoid GLvoid * column
Definition: glew.h:3805
void clear_defines()
Clear stored defines map to default values.
static void print(std::stringstream &sstr, const std::string &queue, const std::string &id)
Definition: fire_event.cpp:29
void connect_queue(std::stringstream &sstr, gui2::twidget &widget)
Definition: fire_event.cpp:37
BOOST_AUTO_TEST_CASE(test_fire_event)
Definition: fire_event.cpp:116
static tcache cache
Definition: minimap.cpp:139
const std::string & id() const
Definition: widget.cpp:109
static void validate_draw(std::stringstream &sstr)
Definition: fire_event.cpp:106
GLenum GLenum GLvoid * row
Definition: glew.h:3805
GLenum GLint ref
Definition: glew.h:1813
Base class for all widgets.
Definition: widget.hpp:49
void set_child(twidget *widget, const unsigned row, const unsigned col, const unsigned flags, const unsigned border_size)
Sets a child in the grid.
Definition: grid.cpp:69
void add_define(const std::string &define)
Add a entry to preproc defines map.
GLsizei const GLcharARB ** string
Definition: glew.h:4503
Singleton class to manage game config file caching.