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