The Battle for Wesnoth  1.15.12+dev
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <>
3  Part of the Battle for Wesnoth Project
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,
12  See the COPYING file for more details.
13 */
15 #pragma once
17 #include "events.hpp"
18 #include "sdl/surface.hpp"
20 #include <string>
22 class CVideo;
24 namespace gui {
27 {
28 public:
29  const SDL_Rect& location() const;
30  virtual void set_location(const SDL_Rect& rect);
31  void set_location(int x, int y);
32  void set_width(int w);
33  void set_height(int h);
34  void set_measurements(int w, int h);
36  int width() const;
37  int height() const;
39  //focus() may gain the focus if the currently focused handler doesn't require this event
40  bool focus(const SDL_Event* event);
41  void set_focus(bool focus);
43  virtual void hide(bool value = true);
44  bool hidden() const;
45  virtual void enable(bool new_val = true);
46  bool enabled() const;
48  void set_clip_rect(const SDL_Rect& rect);
50  //Function to set the widget to draw in 'volatile' mode.
51  //When in 'volatile' mode, instead of using the normal
52  //save-background-redraw-when-dirty procedure, redrawing is done
53  //every frame, and then after every frame the area under the widget
54  //is restored to the state it was in before the frame. This is useful
55  //for drawing widgets with alpha components in volatile settings where
56  //the background may change at any time.
57  //(e.g. for putting widgets on top of the game map)
58  void set_volatile(bool val=true);
60  void set_dirty(bool dirty=true);
61  bool dirty() const;
62  const std::string& id() const;
63  void set_id(const std::string& id);
65  void set_help_string(const std::string& str);
66  void set_tooltip_string(const std::string& str);
68  virtual void process_help_string(int mousex, int mousey);
69  virtual void process_tooltip_string(int mousex, int mousey);
71 protected:
72  widget(CVideo& video, const bool auto_join=true);
73  virtual ~widget();
75  // During each relocation, this function should be called to register
76  // the rectangles the widget needs to refresh automatically
77  void bg_register(const SDL_Rect& rect);
78  void bg_restore() const;
79  void bg_restore(const SDL_Rect& rect) const;
80  void bg_update();
81  void bg_cancel();
83  CVideo& video() const { return *video_; }
85 public:
86  virtual void draw();
87 protected:
88  virtual void draw_contents() {}
89  virtual void update_location(const SDL_Rect& rect);
91  const SDL_Rect* clip_rect() const;
92  virtual sdl_handler_vector member_handlers() { return sdl_handler::handler_members(); }
94  virtual void handle_event(const SDL_Event&);
95  virtual void handle_window_event(const SDL_Event& event);
96  bool focus_; // Should user input be ignored?
98  bool mouse_locked() const;
100  void aquire_mouse_lock();
101  void free_mouse_lock();
102 private:
103  void volatile_draw();
104  void volatile_undraw();
106  void hide_override(bool value = true);
109  std::vector< surface_restorer > restorer_;
110  SDL_Rect rect_;
111  mutable bool needs_restore_; // Have we drawn ourselves, so that if moved, we need to restore the background?
115  bool enabled_;
116  bool clip_;
117  SDL_Rect clip_rect_;
119  bool volatile_;
121  std::string help_text_;
122  std::string tooltip_text_;
124  std::string id_;
127  static bool mouse_lock_;
129  friend class dialog;
130 };
132 }
void bg_cancel()
Definition: widget.cpp:67
bool enabled() const
Definition: widget.cpp:202
virtual void draw_contents()
Definition: widget.hpp:88
bool hidden_override_
Definition: widget.hpp:114
void aquire_mouse_lock()
Definition: widget.cpp:46
std::vector< events::sdl_handler * > sdl_handler_vector
Definition: events.hpp:189
virtual void enable(bool new_val=true)
Definition: widget.cpp:194
void bg_update()
Definition: widget.cpp:234
bool hidden() const
Definition: widget.cpp:188
Definition: video.hpp:31
void set_clip_rect(const SDL_Rect &rect)
Definition: widget.cpp:181
General purpose widgets.
enum gui::widget::@30 state_
void set_width(int w)
Definition: widget.cpp:109
void set_focus(bool focus)
Definition: widget.cpp:139
virtual sdl_handler_vector member_handlers()
Definition: widget.hpp:92
SDL_Rect rect_
Definition: widget.hpp:110
#define h
bool dirty() const
Definition: widget.cpp:217
static bool mouse_lock_
Definition: widget.hpp:127
void set_measurements(int w, int h)
Definition: widget.cpp:119
virtual void process_help_string(int mousex, int mousey)
Definition: widget.cpp:309
widget(CVideo &video, const bool auto_join=true)
Definition: widget.cpp:33
void set_dirty(bool dirty=true)
Definition: widget.cpp:207
std::vector< surface_restorer > restorer_
Definition: widget.hpp:109
virtual void handle_event(const SDL_Event &)
Definition: widget.cpp:330
virtual void process_tooltip_string(int mousex, int mousey)
Definition: widget.cpp:322
bool focus(const SDL_Event *event)
Definition: widget.cpp:147
virtual void hide(bool value=true)
Definition: widget.cpp:152
const SDL_Rect & location() const
Definition: widget.cpp:134
CVideo * video_
Definition: widget.hpp:108
void volatile_undraw()
Definition: widget.cpp:292
void free_mouse_lock()
Definition: widget.cpp:53
void volatile_draw()
Definition: widget.cpp:283
void bg_register(const SDL_Rect &rect)
Definition: widget.cpp:99
virtual void set_location(const SDL_Rect &rect)
Definition: widget.cpp:75
bool mouse_lock_local_
Definition: widget.hpp:126
void set_help_string(const std::string &str)
Definition: widget.cpp:299
friend class dialog
Definition: widget.hpp:129
const std::string & id() const
Definition: widget.cpp:222
std::string id_
Definition: widget.hpp:124
bool focus_
Definition: widget.hpp:96
std::string help_text_
Definition: widget.hpp:121
int width() const
Definition: widget.cpp:124
virtual void draw()
Definition: widget.cpp:269
bool needs_restore_
Definition: widget.hpp:111
int w
int height() const
Definition: widget.cpp:129
bool enabled_
Definition: widget.hpp:115
void bg_restore() const
Definition: widget.cpp:241
bool mouse_locked() const
Definition: widget.cpp:62
void set_tooltip_string(const std::string &str)
Definition: widget.cpp:304
CVideo & video() const
Definition: widget.hpp:83
bool volatile_
Definition: widget.hpp:119
SDL_Rect clip_rect_
Definition: widget.hpp:117
void set_id(const std::string &id)
Definition: widget.cpp:227
bool clip_
Definition: widget.hpp:116
void set_volatile(bool val=true)
Definition: widget.cpp:262
const SDL_Rect * clip_rect() const
Definition: widget.cpp:94
int help_string_
Definition: widget.hpp:123
virtual void update_location(const SDL_Rect &rect)
Definition: widget.cpp:89
virtual ~widget()
Definition: widget.cpp:40
void set_height(int h)
Definition: widget.cpp:114
void hide_override(bool value=true)
Definition: widget.cpp:167
virtual void handle_window_event(const SDL_Event &event)
Definition: widget.cpp:337
std::string tooltip_text_
Definition: widget.hpp:122