The Battle for Wesnoth  1.19.5+dev
widget.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
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"
20 #include "sdl/rect.hpp"
21 
22 #include <string>
23 
24 namespace gui {
25 
26 // TODO: making widgets TLDs is horrible. Please move everything to GUI2.
28 {
29 public:
30  const 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  /** Indicate that the widget should be redrawn. */
52  void queue_redraw();
53  /** Indicate that a specific region of the screen should be redrawn.
54  * This is in absolute drawing coordinates, and is not clipped. */
55  void queue_redraw(const rect&);
56 
57  // Note: all that needs the dirty handling is the editor palette.
58  void set_dirty(bool dirty=true);
59  bool dirty() const;
60  const std::string& id() const;
61  void set_id(const std::string& id);
62 
63  void set_tooltip_string(const std::string& str);
64 
65  virtual void process_tooltip_string(int mousex, int mousey) override;
66 
67 protected:
68  widget(const bool auto_join=true);
69  virtual ~widget();
70 
71 public:
72  /* draw_manager interface */
73 
74  /** Called by draw_manager to validate layout. */
75  virtual void layout() override;
76  /** Called by draw_manager when it believes a redraw is necessary. */
77  virtual bool expose(const rect& region) override;
78  /** The current draw location of the display, on the screen. */
79  virtual rect screen_location() override { return location(); }
80 
81 private:
82  // This could be made public again, but GUI1 widgets are deprecated.
83  // It's better to replace with GUI2 systems than to improve this.
84  void draw();
85 
86 protected:
87  virtual void draw_contents() {}
88  virtual void update_location(const SDL_Rect&) {};
89 
90  virtual void handle_event(const SDL_Event&) override {};
91  bool focus_; // Should user input be ignored?
92 
93  bool mouse_locked() const;
94 
95  void aquire_mouse_lock();
96  void free_mouse_lock();
97 
98 private:
100 
102  bool enabled_;
103  bool clip_;
105 
106  std::string tooltip_text_;
107  std::string id_;
108 
110  static bool mouse_lock_;
111 
112  friend class dialog;
113 };
114 
115 }
A top-level drawable item (TLD), such as a window.
void set_width(int w)
Definition: widget.cpp:98
widget(const bool auto_join=true)
Definition: widget.cpp:33
virtual void enable(bool new_val=true)
Definition: widget.cpp:167
bool enabled_
Definition: widget.hpp:102
static bool mouse_lock_
Definition: widget.hpp:110
void set_dirty(bool dirty=true)
Definition: widget.cpp:180
bool dirty() const
Definition: widget.cpp:193
virtual void layout() override
Called by draw_manager to validate layout.
Definition: widget.cpp:88
std::string tooltip_text_
Definition: widget.hpp:106
void set_height(int h)
Definition: widget.cpp:103
virtual void update_location(const SDL_Rect &)
Definition: widget.hpp:88
virtual void set_location(const SDL_Rect &rect)
Definition: widget.cpp:69
void set_id(const std::string &id)
Definition: widget.cpp:203
void draw()
Definition: widget.cpp:230
virtual void handle_event(const SDL_Event &) override
Definition: widget.hpp:90
bool focus(const SDL_Event *event)
Definition: widget.cpp:136
virtual void draw_contents()
Definition: widget.hpp:87
const std::string & id() const
Definition: widget.cpp:198
virtual bool expose(const rect &region) override
Called by draw_manager when it believes a redraw is necessary.
Definition: widget.cpp:220
void set_measurements(int w, int h)
Definition: widget.cpp:108
enum gui::widget::@9 state_
void set_clip_rect(const SDL_Rect &rect)
Definition: widget.cpp:154
const rect & location() const
Definition: widget.cpp:123
int width() const
Definition: widget.cpp:113
virtual void hide(bool value=true)
Definition: widget.cpp:141
void queue_redraw()
Indicate that the widget should be redrawn.
Definition: widget.cpp:215
friend class dialog
Definition: widget.hpp:112
bool enabled() const
Definition: widget.cpp:175
bool clip_
Definition: widget.hpp:103
virtual ~widget()
Definition: widget.cpp:40
int height() const
Definition: widget.cpp:118
void set_tooltip_string(const std::string &str)
Definition: widget.cpp:246
virtual rect screen_location() override
The current draw location of the display, on the screen.
Definition: widget.hpp:79
void aquire_mouse_lock()
Definition: widget.cpp:48
void set_focus(bool focus)
Definition: widget.cpp:128
rect clip_rect_
Definition: widget.hpp:104
bool hidden() const
Definition: widget.cpp:161
rect rect_
Definition: widget.hpp:99
bool mouse_locked() const
Definition: widget.cpp:64
std::string id_
Definition: widget.hpp:107
bool mouse_lock_local_
Definition: widget.hpp:109
bool focus_
Definition: widget.hpp:90
virtual void process_tooltip_string(int mousex, int mousey) override
Definition: widget.cpp:251
void free_mouse_lock()
Definition: widget.cpp:55
int w
General purpose widgets.
Contains the SDL_Rect helper code.
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:47
#define h