The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
distributor.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 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 #ifndef GUI_WIDGETS_AUXILIARY_EVENT_DISTRIBUTOR_HPP_INCLUDED
16 #define GUI_WIDGETS_AUXILIARY_EVENT_DISTRIBUTOR_HPP_INCLUDED
17 
18 /**
19  * @file
20  * Contains the event distributor.
21  *
22  * The event distributor exists of several classes which are combined in one
23  * templated tdistributor class. The classes are closely tight together.
24  *
25  * All classes have direct access to each others members since they should act
26  * as one. (Since the buttons are a templated subclass it's not possible to use
27  * private subclasses.)
28  *
29  * The tmouse_motion class handles the mouse motion and holds the owner of us
30  * since all classes virtually inherit us.
31  *
32  * The tmouse_button classes are templated classes per mouse button, the
33  * template parameters are used to make the difference between the mouse
34  * buttons. Although it's easily possible to add more mouse buttons in the
35  * code several places only expect a left, middle and right button.
36  *
37  * tdistributor is the main class to be used in the user code. This class
38  * contains the handling of the keyboard as well.
39  */
40 
43 #include "gui/core/point.hpp"
45 #include "video.hpp"
46 
47 #include <string>
48 #include <vector>
49 
50 namespace gui2
51 {
52 
53 class twidget;
54 
55 namespace event
56 {
57 
58 /***** ***** ***** ***** tmouse_motion ***** ***** ***** ***** *****/
59 
61 {
62 public:
63  tmouse_motion(twidget& owner, const tdispatcher::tposition queue_position);
64 
66 
67  /**
68  * Captures the mouse input.
69  *
70  * When capturing the widget that has the mouse focus_ does the capturing.
71  *
72  * @param capture Set or release the capturing.
73  */
74  void capture_mouse( // twidget* widget);
75  const bool capture = true);
76 
77 protected:
78  /** The widget that currently has the mouse focus_. */
80 
81  /** Did the current widget capture the focus_? */
83 
84  /** The widget that owns us. */
86 
87  /** The timer for the hover event. */
88  size_t hover_timer_;
89 
90  /** The widget which should get the hover event. */
92 
93  /** The anchor point of the hover event. */
95 
96  /**
97  * Has the hover been shown for the widget?
98  *
99  * A widget won't get a second hover event after the tooltip has been
100  * triggered. Only after (shortly) entering another widget it will be shown
101  * again for this widget.
102  */
104 
105  /**
106  * Starts the hover timer.
107  *
108  * @param widget The widget that wants the tooltip.
109  * @param coordinate The anchor coordinate.
110  */
111  void start_hover_timer(twidget* widget, const tpoint& coordinate);
112 
113  /** Stops the current hover timer. */
114  void stop_hover_timer();
115 
116  /**
117  * Called when the mouse enters a widget.
118  *
119  * @param mouse_over The widget that should receive the event.
120  */
121  void mouse_enter(twidget* mouse_over);
122 
123  /** Called when the mouse leaves the current widget. */
124  void mouse_leave();
125 
126 private:
127  /**
128  * Called when the mouse moves over a widget.
129  *
130  * @param mouse_over The widget that should receive the event.
131  * @param coordinate The current screen coordinate of the mouse.
132  */
133  void mouse_motion(twidget* mouse_over, const tpoint& coordinate);
134 
135  /** Called when the mouse wants the widget to show its tooltip. */
136  void show_tooltip();
137 
140  bool& handled,
141  const tpoint& coordinate);
142 
144  bool& handled,
145  const tpoint& coordinate);
146 
148  bool& handled,
149  const tpoint& coordinate);
150 };
151 
152 /***** ***** ***** ***** tmouse_button ***** ***** ***** ***** *****/
153 
154 template <tevent sdl_button_down,
155  tevent sdl_button_up,
156  tevent button_down,
157  tevent button_up,
158  tevent button_click,
159  tevent button_double_click>
160 class tmouse_button : public virtual tmouse_motion
161 {
162 public:
164  twidget& owner,
165  const tdispatcher::tposition queue_position);
166 
167  /**
168  * Initializes the state of the button.
169  *
170  * @param is_down The initial state of the button, if true down
171  * else initialized as up.
172  */
173  void initialize_state(const bool is_down);
174 
175 protected:
176  /** The time of the last click used for double clicking. */
178 
179  /** The widget the last click was on, used for double clicking. */
181 
182  /**
183  * If the mouse isn't captured we need to verify the up is on the same
184  * widget as the down so we send a proper click, also needed to send the
185  * up to the right widget.
186  */
188 
189 private:
190  /** used for debug messages. */
192 
193  /** Is the button down? */
194  bool is_down_;
195 
198  bool& handled,
199  const tpoint& coordinate);
200 
203  bool& handled,
204  const tpoint& coordinate);
205 
206 
207  void mouse_button_click(twidget* widget);
208 };
209 
210 /***** ***** ***** ***** tdistributor ***** ***** ***** ***** *****/
211 
218 
225 
232 
233 
234 /** The event handler class for the widget library. */
235 class tdistributor : public tmouse_button_left,
236  public tmouse_button_middle,
237  public tmouse_button_right
238 {
239 public:
240  tdistributor(twidget& owner, const tdispatcher::tposition queue_position);
241 
242  ~tdistributor();
243 
244  /**
245  * Initializes the state of the keyboard and mouse.
246  *
247  * Needed after initialization and reactivation.
248  */
249  void initialize_state();
250 
251  /**
252  * Captures the keyboard input.
253  *
254  * @param widget The widget which should capture the keyboard.
255  * Sending nullptr releases the capturing.
256  */
257  void keyboard_capture(twidget* widget);
258 
259  /**
260  * Adds the widget to the keyboard chain.
261  *
262  * @param widget The widget to add to the chain. The widget
263  * should be valid widget, which hasn't been
264  * added to the chain yet.
265  */
266  void keyboard_add_to_chain(twidget* widget);
267 
268  /**
269  * Remove the widget from the keyboard chain.
270  *
271  * @param widget The widget to be removed from the chain.
272  */
273  void keyboard_remove_from_chain(twidget* widget);
274 
275 private:
277  {
278  public:
279  virtual void handle_event(const SDL_Event& ) {}
280  virtual void handle_window_event(const SDL_Event& ) {}
281  layer() : video2::draw_layering(false) { }
282  };
283 
284  // make sure the appropriate things happens when we close.
286 
287 #if 0
288  bool hover_pending_; /**< Is there a hover event pending? */
289  unsigned hover_id_; /**< Id of the pending hover event. */
290  SDL_Rect hover_box_; /**< The area the mouse can move in,
291  * moving outside invalidates the
292  * pending hover event.
293  */
294 
295  bool had_hover_; /**< A widget only gets one hover event
296  * per enter cycle.
297  */
298 
299  /** The widget of the currently active tooltip. */
300  twidget* tooltip_;
301 
302  /** The widget of the currently active help popup. */
303  twidget* help_popup_;
304 #endif
305  /** The widget that holds the keyboard focus_. */
307 
308  /**
309  * Fall back keyboard focus_ items.
310  *
311  * When the focused widget didn't handle the keyboard event (or no handler
312  * for the keyboard focus_) it is send all widgets in this vector. The order
313  * is from rbegin() to rend(). If the keyboard_focus_ is in the vector it
314  * won't get the event twice. The first item added to the vector should be
315  * the window, so it will be the last handler and can dispatch the hotkeys
316  * registered.
317  */
318  std::vector<twidget*> keyboard_focus_chain_;
319 
320  /**
321  * Set of functions that handle certain events and sends them to the proper
322  * widget. These functions are called by the SDL event handling functions.
323  */
324 
325  void signal_handler_sdl_key_down(const SDLKey key,
326  const SDLMod modifier,
327  const utf8::string& unicode);
328 
330 };
331 
332 } // namespace event
333 
334 } // namespace gui2
335 
336 #endif
tmouse_button< SDL_RIGHT_BUTTON_DOWN, SDL_RIGHT_BUTTON_UP, RIGHT_BUTTON_DOWN, RIGHT_BUTTON_UP, RIGHT_BUTTON_CLICK, RIGHT_BUTTON_DOUBLE_CLICK > tmouse_button_right
twidget & owner_
The widget that owns us.
Definition: distributor.hpp:85
Uint32 last_click_stamp_
The time of the last click used for double clicking.
void stop_hover_timer()
Stops the current hover timer.
void capture_mouse(const bool capture=true)
Captures the mouse input.
draw_layering(const bool auto_join=true)
Definition: video.cpp:75
bool mouse_captured_
Did the current widget capture the focus_?
Definition: distributor.hpp:82
See LEFT_BUTTON_DOUBLE_CLICK.
Definition: handler.hpp:116
#define SDLMod
Definition: compat.hpp:30
The event handler class for the widget library.
void initialize_state(const bool is_down)
Initializes the state of the button.
virtual void handle_event(const SDL_Event &)
A SDL middle mouse button up event.
Definition: handler.hpp:96
A left mouse button double click event for a widget.
Definition: handler.hpp:89
void start_hover_timer(twidget *widget, const tpoint &coordinate)
Starts the hover timer.
tmouse_button(const std::string &name_, twidget &owner, const tdispatcher::tposition queue_position)
void show_tooltip()
Called when the mouse wants the widget to show its tooltip.
See LEFT_BUTTON_UP.
Definition: handler.hpp:100
void signal_handler_sdl_wheel(const event::tevent event, bool &handled, const tpoint &coordinate)
tmouse_button< SDL_MIDDLE_BUTTON_DOWN, SDL_MIDDLE_BUTTON_UP, MIDDLE_BUTTON_DOWN, MIDDLE_BUTTON_UP, MIDDLE_BUTTON_CLICK, MIDDLE_BUTTON_DOUBLE_CLICK > tmouse_button_middle
const std::string name_
used for debug messages.
Base class for event handling.
Definition: dispatcher.hpp:122
A left mouse button down event for a widget.
Definition: handler.hpp:76
-file util.hpp
#define SDLKey
Definition: compat.hpp:29
void keyboard_add_to_chain(twidget *widget)
Adds the widget to the keyboard chain.
void signal_handler_sdl_key_down(const SDLKey key, const SDLMod modifier, const utf8::string &unicode)
Set of functions that handle certain events and sends them to the proper widget.
void signal_handler_sdl_button_up(const event::tevent event, bool &handled, const tpoint &coordinate)
See LEFT_BUTTON_UP.
Definition: handler.hpp:112
See LEFT_BUTTON_DOWN.
Definition: handler.hpp:110
A left mouse button click event for a widget.
Definition: handler.hpp:84
std::vector< twidget * > keyboard_focus_chain_
Fall back keyboard focus_ items.
void signal_handler_show_helptip(const event::tevent event, bool &handled, const tpoint &coordinate)
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
void initialize_state()
Initializes the state of the keyboard and mouse.
See LEFT_BUTTON_DOUBLE_CLICK.
Definition: handler.hpp:104
twidget * focus_
If the mouse isn't captured we need to verify the up is on the same widget as the down so we send a p...
See LEFT_BUTTON_DOWN.
Definition: handler.hpp:98
twidget * last_clicked_widget_
The widget the last click was on, used for double clicking.
twidget * mouse_focus_
The widget that currently has the mouse focus_.
Definition: distributor.hpp:79
A SDL middle mouse button down event.
Definition: handler.hpp:94
tpoint hover_position_
The anchor point of the hover event.
Definition: distributor.hpp:94
virtual void handle_window_event(const SDL_Event &)
size_t hover_timer_
The timer for the hover event.
Definition: distributor.hpp:88
tposition
The position where to add a new callback in the signal handler.
Definition: dispatcher.hpp:249
A SDL left mouse button up event.
Definition: handler.hpp:74
void signal_handler_sdl_mouse_motion(const event::tevent event, bool &handled, const tpoint &coordinate)
tevent
The event send to the dispatcher.
Definition: handler.hpp:54
void keyboard_capture(twidget *widget)
Captures the keyboard input.
Definition: video.cpp:71
A left mouse button up event for a widget.
Definition: handler.hpp:80
See LEFT_BUTTON_CLICK.
Definition: handler.hpp:114
bool hover_shown_
Has the hover been shown for the widget?
void mouse_enter(twidget *mouse_over)
Called when the mouse enters a widget.
A SDL left mouse button down event.
Definition: handler.hpp:72
Holds a 2D point.
Definition: point.hpp:24
void keyboard_remove_from_chain(twidget *widget)
Remove the widget from the keyboard chain.
void signal_handler_sdl_button_down(const event::tevent event, bool &handled, const tpoint &coordinate)
void mouse_button_click(twidget *widget)
A SDL right mouse button down event.
Definition: handler.hpp:106
void mouse_leave()
Called when the mouse leaves the current widget.
twidget * hover_widget_
The widget which should get the hover event.
Definition: distributor.hpp:91
twidget * keyboard_focus_
The widget that holds the keyboard focus_.
See LEFT_BUTTON_CLICK.
Definition: handler.hpp:102
cl_event event
Definition: glew.h:3070
A SDL right mouse button up event.
Definition: handler.hpp:108
Base class for all widgets.
Definition: widget.hpp:49
tmouse_button< SDL_LEFT_BUTTON_DOWN, SDL_LEFT_BUTTON_UP, LEFT_BUTTON_DOWN, LEFT_BUTTON_UP, LEFT_BUTTON_CLICK, LEFT_BUTTON_DOUBLE_CLICK > tmouse_button_left
tdistributor(twidget &owner, const tdispatcher::tposition queue_position)
void signal_handler_notify_removal(tdispatcher &widget, const tevent event)
void mouse_motion(twidget *mouse_over, const tpoint &coordinate)
Called when the mouse moves over a widget.
GLsizei const GLcharARB ** string
Definition: glew.h:4503
bool is_down_
Is the button down?
std::string string
tmouse_motion(twidget &owner, const tdispatcher::tposition queue_position)