The Battle for Wesnoth  1.17.12+dev
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2022
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_help_string(const std::string& str);
65  void set_tooltip_string(const std::string& str);
67  virtual void process_help_string(int mousex, int mousey) override;
68  virtual void process_tooltip_string(int mousex, int mousey) override;
70 protected:
71  widget(const bool auto_join=true);
72  virtual ~widget();
74 public:
75  /* draw_manager interface */
77  /** Called by draw_manager to validate layout. */
78  virtual void layout() override;
79  /** Called by draw_manager when it believes a redraw is necessary. */
80  virtual bool expose(const rect& region) override;
81  /** The current draw location of the display, on the screen. */
82  virtual rect screen_location() override { return location(); }
84 private:
85  // This could be made public again, but GUI1 widgets are deprecated.
86  // It's better to replace with GUI2 systems than to improve this.
87  void draw();
89 protected:
90  virtual void draw_contents() {}
91  virtual void update_location(const SDL_Rect&) {};
93  virtual void handle_event(const SDL_Event&) override {};
94  bool focus_; // Should user input be ignored?
96  bool mouse_locked() const;
98  void aquire_mouse_lock();
99  void free_mouse_lock();
101 private:
102  rect rect_;
105  bool enabled_;
106  bool clip_;
109  std::string help_text_;
110  std::string tooltip_text_;
111  bool has_help_;
112  std::string id_;
115  static bool mouse_lock_;
117  friend class dialog;
118 };
120 }
bool enabled() const
Definition: widget.cpp:176
virtual void draw_contents()
Definition: widget.hpp:90
void aquire_mouse_lock()
Definition: widget.cpp:49
virtual void enable(bool new_val=true)
Definition: widget.cpp:168
bool hidden() const
Definition: widget.cpp:162
void queue_redraw()
Indicate that the widget should be redrawn.
Definition: widget.cpp:216
void set_clip_rect(const SDL_Rect &rect)
Definition: widget.cpp:155
bool has_help_
Definition: widget.hpp:111
General purpose widgets.
enum gui::widget::@10 state_
void set_width(int w)
Definition: widget.cpp:99
void set_focus(bool focus)
Definition: widget.cpp:129
#define h
rect clip_rect_
Definition: widget.hpp:107
bool dirty() const
Definition: widget.cpp:194
widget(const bool auto_join=true)
Definition: widget.cpp:34
static bool mouse_lock_
Definition: widget.hpp:115
virtual void layout() override
Called by draw_manager to validate layout.
Definition: widget.cpp:89
void set_measurements(int w, int h)
Definition: widget.cpp:109
virtual rect screen_location() override
The current draw location of the display, on the screen.
Definition: widget.hpp:82
void set_dirty(bool dirty=true)
Definition: widget.cpp:181
bool focus(const SDL_Event *event)
Definition: widget.cpp:137
virtual void hide(bool value=true)
Definition: widget.cpp:142
void free_mouse_lock()
Definition: widget.cpp:56
const rect & location() const
Definition: widget.cpp:124
virtual void process_tooltip_string(int mousex, int mousey) override
Definition: widget.cpp:271
A top-level drawable item (TLD), such as a window.
virtual void process_help_string(int mousex, int mousey) override
Definition: widget.cpp:257
virtual void set_location(const SDL_Rect &rect)
Definition: widget.cpp:70
bool mouse_lock_local_
Definition: widget.hpp:114
void set_help_string(const std::string &str)
Definition: widget.cpp:247
virtual bool expose(const rect &region) override
Called by draw_manager when it believes a redraw is necessary.
Definition: widget.cpp:221
friend class dialog
Definition: widget.hpp:117
const std::string & id() const
Definition: widget.cpp:199
virtual void handle_event(const SDL_Event &) override
Definition: widget.hpp:93
std::string id_
Definition: widget.hpp:112
bool focus_
Definition: widget.hpp:93
std::string help_text_
Definition: widget.hpp:109
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:46
rect rect_
Definition: widget.hpp:102
virtual void update_location(const SDL_Rect &)
Definition: widget.hpp:91
int width() const
Definition: widget.cpp:114
void draw()
Definition: widget.cpp:231
int w
int height() const
Definition: widget.cpp:119
bool enabled_
Definition: widget.hpp:105
bool mouse_locked() const
Definition: widget.cpp:65
void set_tooltip_string(const std::string &str)
Definition: widget.cpp:252
void set_id(const std::string &id)
Definition: widget.cpp:204
Contains the SDL_Rect helper code.
bool clip_
Definition: widget.hpp:106
virtual ~widget()
Definition: widget.cpp:41
void set_height(int h)
Definition: widget.cpp:104
std::string tooltip_text_
Definition: widget.hpp:110