The Battle for Wesnoth  1.17.23+dev
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2023
3  by David White <>
4  Part of the Battle for Wesnoth Project
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,
13  See the COPYING file for more details.
14 */
16 #pragma once
18 #include "events.hpp"
20 #include "sdl/rect.hpp"
21 #include "sdl/surface.hpp"
23 #include <string>
25 namespace gui {
27 // TODO: making widgets TLDs is horrible. Please move everything to GUI2.
29 {
30 public:
31  const rect& location() const;
32  virtual void set_location(const SDL_Rect& rect);
33  void set_location(int x, int y);
34  void set_width(int w);
35  void set_height(int h);
36  void set_measurements(int w, int h);
38  int width() const;
39  int height() const;
41  //focus() may gain the focus if the currently focused handler doesn't require this event
42  bool focus(const SDL_Event* event);
43  void set_focus(bool focus);
45  virtual void hide(bool value = true);
46  bool hidden() const;
47  virtual void enable(bool new_val = true);
48  bool enabled() const;
50  void set_clip_rect(const SDL_Rect& rect);
52  /** Indicate that the widget should be redrawn. */
53  void queue_redraw();
54  /** Indicate that a specific region of the screen should be redrawn.
55  * This is in absolute drawing coordinates, and is not clipped. */
56  void queue_redraw(const rect&);
58  // Note: all that needs the dirty handling is the editor palette.
59  void set_dirty(bool dirty=true);
60  bool dirty() const;
61  const std::string& id() const;
62  void set_id(const std::string& id);
64  void set_tooltip_string(const std::string& str);
66  virtual void process_tooltip_string(int mousex, int mousey) override;
68 protected:
69  widget(const bool auto_join=true);
70  virtual ~widget();
72 public:
73  /* draw_manager interface */
75  /** Called by draw_manager to validate layout. */
76  virtual void layout() override;
77  /** Called by draw_manager when it believes a redraw is necessary. */
78  virtual bool expose(const rect& region) override;
79  /** The current draw location of the display, on the screen. */
80  virtual rect screen_location() override { return location(); }
82 private:
83  // This could be made public again, but GUI1 widgets are deprecated.
84  // It's better to replace with GUI2 systems than to improve this.
85  void draw();
87 protected:
88  virtual void draw_contents() {}
89  virtual void update_location(const SDL_Rect&) {};
91  virtual void handle_event(const SDL_Event&) override {};
92  bool focus_; // Should user input be ignored?
94  bool mouse_locked() const;
96  void aquire_mouse_lock();
97  void free_mouse_lock();
99 private:
103  bool enabled_;
104  bool clip_;
107  std::string tooltip_text_;
108  std::string id_;
111  static bool mouse_lock_;
113  friend class dialog;
114 };
116 }
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:103
static bool mouse_lock_
Definition: widget.hpp:111
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:107
void set_height(int h)
Definition: widget.cpp:103
virtual void update_location(const SDL_Rect &)
Definition: widget.hpp:89
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:91
bool focus(const SDL_Event *event)
Definition: widget.cpp:136
virtual void draw_contents()
Definition: widget.hpp:88
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
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
enum gui::widget::@10 state_
void queue_redraw()
Indicate that the widget should be redrawn.
Definition: widget.cpp:215
friend class dialog
Definition: widget.hpp:113
bool enabled() const
Definition: widget.cpp:175
bool clip_
Definition: widget.hpp:104
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:80
void aquire_mouse_lock()
Definition: widget.cpp:48
void set_focus(bool focus)
Definition: widget.cpp:128
rect clip_rect_
Definition: widget.hpp:105
bool hidden() const
Definition: widget.cpp:161
rect rect_
Definition: widget.hpp:100
bool mouse_locked() const
Definition: widget.cpp:64
std::string id_
Definition: widget.hpp:108
bool mouse_lock_local_
Definition: widget.hpp:110
bool focus_
Definition: widget.hpp:91
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