The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
widget.cpp
Go to the documentation of this file.
1 /*
2 
3  Copyright (C) 2003 - 2016 by David White <[email protected]>
4  Part of the Battle for Wesnoth Project http://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 
18 #include "global.hpp"
19 
20 #include "widgets/widget.hpp"
21 #include "video.hpp"
22 #include "sdl/rect.hpp"
23 #include "tooltips.hpp"
24 
25 #include <cassert>
26 
27 namespace {
28  const SDL_Rect EmptyRect = {-1234,-1234,0,0};
29 }
30 
31 namespace gui {
32 
33 bool widget::mouse_lock_ = false;
34 
36  : events::sdl_handler(), focus_(o.focus_), video_(o.video_), restorer_(o.restorer_), rect_(o.rect_),
37  needs_restore_(o.needs_restore_), state_(o.state_), hidden_override_(o.hidden_override_),
38  enabled_(o.enabled_), clip_(o.clip_), clip_rect_(o.clip_rect_), volatile_(o.volatile_),
39  help_text_(o.help_text_), tooltip_text_(o.tooltip_text_), help_string_(o.help_string_), id_(o.id_), mouse_lock_local_(o.mouse_lock_local_)
40 {
41 }
42 
43 widget::widget(CVideo& video, const bool auto_join)
44  : events::sdl_handler(auto_join), focus_(true), video_(&video), rect_(EmptyRect), needs_restore_(false),
45  state_(UNINIT), hidden_override_(false), enabled_(true), clip_(false),
46  clip_rect_(EmptyRect), volatile_(false), help_string_(0), mouse_lock_local_(false)
47 {
48 }
49 
51 {
52  bg_cancel();
54 }
55 
57 {
58  assert(!mouse_lock_);
59  mouse_lock_ = true;
60  mouse_lock_local_ = true;
61 }
62 
64 {
66  {
67  mouse_lock_local_ = false;
68  mouse_lock_ = false;
69  }
70 }
71 
73 {
74  return mouse_lock_ && !mouse_lock_local_;
75 }
76 
78 {
80  i_end = restorer_.end(); i != i_end; ++i)
81  i->cancel();
82  restorer_.clear();
83 }
84 
85 void widget::set_location(SDL_Rect const &rect)
86 {
87  if(rect_.x == rect.x && rect_.y == rect.y && rect_.w == rect.w && rect_.h == rect.h)
88  return;
89  if(state_ == UNINIT && rect.x != -1234 && rect.y != -1234)
90  state_ = DRAWN;
91 
92  bg_restore();
93  bg_cancel();
94  rect_ = rect;
95  set_dirty(true);
96  update_location(rect);
97 }
98 
99 void widget::update_location(SDL_Rect const &rect)
100 {
101  bg_register(rect);
102 }
103 
104 const SDL_Rect* widget::clip_rect() const
105 {
106  return clip_ ? &clip_rect_ : nullptr;
107 }
108 
109 void widget::bg_register(SDL_Rect const &rect)
110 {
111  restorer_.push_back(surface_restorer(&video(), rect));
112 }
113 
114 void widget::set_location(int x, int y)
115 {
117 }
118 
120 {
122 }
123 
125 {
127 }
128 
130 {
132 }
133 
134 int widget::width() const
135 {
136  return rect_.w;
137 }
138 
139 int widget::height() const
140 {
141  return rect_.h;
142 }
143 
144 const SDL_Rect& widget::location() const
145 {
146  return rect_;
147 }
148 
149 void widget::set_focus(bool focus)
150 {
151  if (focus)
152  events::focus_handler(this);
153  focus_ = focus;
154  set_dirty(true);
155 }
156 
157 bool widget::focus(const SDL_Event* event)
158 {
159  return events::has_focus(this, event) && focus_;
160 }
161 
162 void widget::hide(bool value)
163 {
164  if (value) {
165  if ((state_ == DIRTY || state_ == DRAWN) && !hidden_override_)
166  bg_restore();
167  state_ = HIDDEN;
168  } else if (state_ == HIDDEN) {
169  state_ = DRAWN;
170  if (!hidden_override_) {
171  bg_update();
172  set_dirty(true);
173  }
174  }
175 }
176 
178  if (hidden_override_ != value) {
180  if (state_ == DIRTY || state_ == DRAWN) {
181  if (value) {
182  bg_restore();
183  } else {
184  bg_update();
185  set_dirty(true);
186  }
187  }
188  }
189 }
190 
191 void widget::set_clip_rect(const SDL_Rect& rect)
192 {
193  clip_rect_ = rect;
194  clip_ = true;
195  set_dirty(true);
196 }
197 
198 bool widget::hidden() const
199 {
200  return (state_ == HIDDEN || hidden_override_ || state_ == UNINIT
202 }
203 
204 void widget::enable(bool new_val)
205 {
206  if (enabled_ != new_val) {
207  enabled_ = new_val;
208  set_dirty();
209  }
210 }
211 
212 bool widget::enabled() const
213 {
214  return enabled_;
215 }
216 
217 void widget::set_dirty(bool dirty)
218 {
219  if ((dirty && (volatile_ || hidden_override_ || state_ != DRAWN)) || (!dirty && state_ != DIRTY))
220  return;
221 
222  state_ = dirty ? DIRTY : DRAWN;
223  if (!dirty)
224  needs_restore_ = true;
225 }
226 
227 bool widget::dirty() const
228 {
229  return state_ == DIRTY;
230 }
231 
232 const std::string& widget::id() const
233 {
234  return id_;
235 }
236 
238 {
239  if (id_.empty()){
240  id_ = id;
241  }
242 }
243 
245 {
247  i_end = restorer_.end(); i != i_end; ++i)
248  i->update();
249 }
250 
251 void widget::bg_restore() const
252 {
253  clip_rect_setter clipper(video().getSurface(), &clip_rect_, clip_);
254 
255  if (needs_restore_) {
256  for(std::vector< surface_restorer >::const_iterator i = restorer_.begin(),
257  i_end = restorer_.end(); i != i_end; ++i)
258  i->restore();
259  needs_restore_ = false;
260  } else {
261  //this function should be able to be relied upon to update the rectangle,
262  //so do that even if we don't restore
264  }
265 }
266 
267 void widget::bg_restore(SDL_Rect const &rect) const
268 {
269  clip_rect_setter clipper(video().getSurface(), &clip_rect_, clip_);
270 
271  for(std::vector< surface_restorer >::const_iterator i = restorer_.begin(),
272  i_end = restorer_.end(); i != i_end; ++i)
273  i->restore(rect);
274 }
275 
277 {
278  volatile_ = val;
279  if (volatile_ && state_ == DIRTY)
280  state_ = DRAWN;
281 }
282 
284 {
285  if (hidden() || !dirty())
286  return;
287 
288  bg_restore();
289 
290  clip_rect_setter clipper(video().getSurface(), &clip_rect_, clip_);
291 
292  draw_contents();
293 
295  set_dirty(false);
296 }
297 
299 {
300  if (!volatile_ || state_ != DRAWN || hidden_override_)
301  return;
302  state_ = DIRTY;
303  bg_update();
304  draw();
305 }
306 
308 {
309  if (!volatile_)
310  return;
311  bg_restore();
312 }
313 
315 {
316  help_text_ = str;
317 }
318 
320 {
321  tooltip_text_ = str;
322 }
323 
324 void widget::process_help_string(int mousex, int mousey)
325 {
326  if (!hidden() && sdl::point_in_rect(mousex, mousey, rect_)) {
327  if(help_string_ == 0 && help_text_ != "") {
328  //std::cerr << "setting help string to '" << help_text_ << "'\n";
330  }
331  } else if(help_string_ > 0) {
333  help_string_ = 0;
334  }
335 }
336 
337 void widget::process_tooltip_string(int mousex, int mousey)
338 {
339  if (!hidden() && sdl::point_in_rect(mousex, mousey, rect_)) {
340  if (!tooltip_text_.empty())
342  }
343 }
344 
345 void widget::handle_event(SDL_Event const &event) {
346  if (event.type == DRAW_ALL_EVENT) {
347  set_dirty();
348  draw();
349  }
350 }
351 
352 void widget::handle_window_event(SDL_Event const &event) {
353  if (event.type == SDL_WINDOWEVENT) {
354  switch (event.window.event) {
355  case SDL_WINDOWEVENT_RESIZED:
356  case SDL_WINDOWEVENT_RESTORED:
357  case SDL_WINDOWEVENT_SHOWN:
358  case SDL_WINDOWEVENT_EXPOSED:
359  set_dirty();
360  }
361  }
362 }
363 
364 
365 }
void bg_cancel()
Definition: widget.cpp:77
virtual void draw_contents()
Definition: widget.hpp:86
bool hidden_override_
Definition: widget.hpp:112
std::string id_
Definition: formula.cpp:636
void aquire_mouse_lock()
Definition: widget.cpp:56
bool enabled() const
Definition: widget.cpp:212
virtual void enable(bool new_val=true)
Definition: widget.cpp:204
void bg_update()
Definition: widget.cpp:244
bool rects_overlap(const SDL_Rect &rect1, const SDL_Rect &rect2)
Tests whether two rectangles overlap.
Definition: rect.cpp:52
virtual void update_location(SDL_Rect const &rect)
Definition: widget.cpp:99
Definition: video.hpp:58
void set_clip_rect(const SDL_Rect &rect)
Definition: widget.cpp:191
void bg_register(SDL_Rect const &rect)
Definition: widget.cpp:109
CVideo & video() const
Definition: widget.hpp:83
General purpose widgets.
void set_width(int w)
Definition: widget.cpp:119
GLuint const GLfloat * val
Definition: glew.h:2614
void set_focus(bool focus)
Definition: widget.cpp:149
SDL_Rect rect_
Definition: widget.hpp:108
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
-file util.hpp
static bool mouse_lock_
Definition: widget.hpp:125
void set_measurements(int w, int h)
Definition: widget.cpp:129
virtual void process_help_string(int mousex, int mousey)
Definition: widget.cpp:324
bool hidden() const
Definition: widget.cpp:198
void set_dirty(bool dirty=true)
Definition: widget.cpp:217
std::vector< surface_restorer > restorer_
Definition: widget.hpp:107
void focus_handler(const sdl_handler *ptr)
Definition: events.cpp:262
virtual void process_tooltip_string(int mousex, int mousey)
Definition: widget.cpp:337
const SDL_Rect * clip_rect() const
Definition: widget.cpp:104
bool focus(const SDL_Event *event)
Definition: widget.cpp:157
virtual void handle_window_event(SDL_Event const &event)
Definition: widget.cpp:352
virtual void hide(bool value=true)
Definition: widget.cpp:162
GLubyte GLubyte GLubyte GLubyte w
Definition: glew.h:1858
GLsizei const GLfloat * value
Definition: glew.h:1817
bool dirty() const
Definition: widget.cpp:227
void volatile_undraw()
Definition: widget.cpp:307
void free_mouse_lock()
Definition: widget.cpp:63
void volatile_draw()
Definition: widget.cpp:298
bool mouse_lock_local_
Definition: widget.hpp:124
enum gui::widget::@27 state_
bool point_in_rect(int x, int y, const SDL_Rect &rect)
Tests whether a point is inside a rectangle.
Definition: rect.cpp:47
#define DRAW_ALL_EVENT
Definition: events.hpp:29
void set_help_string(const std::string &str)
Definition: widget.cpp:314
std::string id_
Definition: widget.hpp:122
bool focus_
Definition: widget.hpp:94
std::string help_text_
Definition: widget.hpp:119
GLfloat GLfloat GLfloat GLfloat h
Definition: glew.h:5910
size_t i
Definition: function.cpp:1057
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
virtual void draw()
Definition: widget.cpp:283
bool needs_restore_
Definition: widget.hpp:109
bool enabled_
Definition: widget.hpp:113
void set_tooltip_string(const std::string &str)
Definition: widget.cpp:319
Handling of system events.
Definition: manager.hpp:42
bool volatile_
Definition: widget.hpp:117
int set_help_string(const std::string &str)
Definition: video.cpp:625
SDL_Rect create_rect(const int x, const int y, const int w, const int h)
Creates an empty SDL_Rect.
Definition: rect.cpp:28
SDL_Rect clip_rect_
Definition: widget.hpp:115
void set_id(const std::string &id)
Definition: widget.cpp:237
Contains the SDL_Rect helper code.
void bg_restore() const
Definition: widget.cpp:251
bool clip_
Definition: widget.hpp:114
cl_event event
Definition: glew.h:3070
void set_volatile(bool val=true)
Definition: widget.cpp:276
bool has_focus(const sdl_handler *hand, const SDL_Event *event)
Definition: events.cpp:269
bool mouse_locked() const
Definition: widget.cpp:72
virtual void set_location(SDL_Rect const &rect)
Definition: widget.cpp:85
virtual void handle_event(SDL_Event const &)
Definition: widget.cpp:345
int help_string_
Definition: widget.hpp:121
int width() const
Definition: widget.cpp:134
SDL_Rect const & location() const
Definition: widget.cpp:144
widget(widget const &o)
Definition: widget.cpp:35
std::string::const_iterator iterator
Definition: tokenizer.hpp:21
virtual ~widget()
Definition: widget.cpp:50
void set_height(int h)
Definition: widget.cpp:124
void update_rect(const SDL_Rect &)
Definition: dummy_video.cpp:27
int add_tooltip(const SDL_Rect &rect, const std::string &message, const std::string &action, bool use_markup, const surface &foreground)
Definition: tooltips.cpp:180
GLsizei const GLcharARB ** string
Definition: glew.h:4503
void hide_override(bool value=true)
Definition: widget.cpp:177
const std::string & id() const
Definition: widget.cpp:232
std::string tooltip_text_
Definition: widget.hpp:120
void clear_help_string(int handle)
Definition: video.cpp:656
int height() const
Definition: widget.cpp:139