The Battle for Wesnoth  1.17.5+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"
19 #include "sdl/surface.hpp"
21 #include <string>
23 class CVideo;
25 namespace gui {
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);
37  int width() const;
38  int height() const;
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);
44  virtual void hide(bool value = true);
45  bool hidden() const;
46  virtual void enable(bool new_val = true);
47  bool enabled() const;
49  void set_clip_rect(const SDL_Rect& rect);
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);
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);
66  void set_help_string(const std::string& str);
67  void set_tooltip_string(const std::string& str);
69  virtual void process_help_string(int mousex, int mousey);
70  virtual void process_tooltip_string(int mousex, int mousey);
72 protected:
73  widget(CVideo& video, const bool auto_join=true);
74  virtual ~widget();
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();
84  CVideo& video() const { return *video_; }
86 public:
87  virtual void draw();
88 protected:
89  virtual void draw_contents() {}
90  virtual void update_location(const SDL_Rect& rect);
92  const SDL_Rect* clip_rect() const;
93  virtual sdl_handler_vector member_handlers() { return sdl_handler::handler_members(); }
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?
99  bool mouse_locked() const;
101  void aquire_mouse_lock();
102  void free_mouse_lock();
103 private:
104  void volatile_draw();
105  void volatile_undraw();
107  void hide_override(bool value = true);
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?
116  bool enabled_;
117  bool clip_;
118  SDL_Rect clip_rect_;
120  bool volatile_;
122  std::string help_text_;
123  std::string tooltip_text_;
125  std::string id_;
128  static bool mouse_lock_;
130  friend class dialog;
131 };
133 }
void bg_cancel()
Definition: widget.cpp:68
bool enabled() const
Definition: widget.cpp:203
virtual void draw_contents()
Definition: widget.hpp:89
bool hidden_override_
Definition: widget.hpp:115
void aquire_mouse_lock()
Definition: widget.cpp:47
std::vector< events::sdl_handler * > sdl_handler_vector
Definition: events.hpp:193
virtual void enable(bool new_val=true)
Definition: widget.cpp:195
void bg_update()
Definition: widget.cpp:235
bool hidden() const
Definition: widget.cpp:189
Definition: video.hpp:39
void set_clip_rect(const SDL_Rect &rect)
Definition: widget.cpp:182
General purpose widgets.
enum gui::widget::@10 state_
void set_width(int w)
Definition: widget.cpp:110
void set_focus(bool focus)
Definition: widget.cpp:140
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:218
static bool mouse_lock_
Definition: widget.hpp:128
void rect(const SDL_Rect &rect)
Draw a rectangle.
Definition: draw.cpp:117
void set_measurements(int w, int h)
Definition: widget.cpp:120
virtual void process_help_string(int mousex, int mousey)
Definition: widget.cpp:313
widget(CVideo &video, const bool auto_join=true)
Definition: widget.cpp:34
void set_dirty(bool dirty=true)
Definition: widget.cpp:208
std::vector< surface_restorer > restorer_
Definition: widget.hpp:110
virtual void handle_event(const SDL_Event &)
Definition: widget.cpp:334
virtual void process_tooltip_string(int mousex, int mousey)
Definition: widget.cpp:326
bool focus(const SDL_Event *event)
Definition: widget.cpp:148
virtual void hide(bool value=true)
Definition: widget.cpp:153
const SDL_Rect & location() const
Definition: widget.cpp:135
CVideo * video_
Definition: widget.hpp:109
void volatile_undraw()
Definition: widget.cpp:296
void free_mouse_lock()
Definition: widget.cpp:54
void volatile_draw()
Definition: widget.cpp:287
void bg_register(const SDL_Rect &rect)
Definition: widget.cpp:100
virtual void set_location(const SDL_Rect &rect)
Definition: widget.cpp:76
bool mouse_lock_local_
Definition: widget.hpp:127
void set_help_string(const std::string &str)
Definition: widget.cpp:303
friend class dialog
Definition: widget.hpp:130
const std::string & id() const
Definition: widget.cpp:223
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:125
virtual void draw()
Definition: widget.cpp:270
bool needs_restore_
Definition: widget.hpp:112
int w
int height() const
Definition: widget.cpp:130
bool enabled_
Definition: widget.hpp:116
void bg_restore() const
Definition: widget.cpp:242
bool mouse_locked() const
Definition: widget.cpp:63
void set_tooltip_string(const std::string &str)
Definition: widget.cpp:308
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:228
bool clip_
Definition: widget.hpp:117
void set_volatile(bool val=true)
Definition: widget.cpp:263
const SDL_Rect * clip_rect() const
Definition: widget.cpp:95
int help_string_
Definition: widget.hpp:124
virtual void update_location(const SDL_Rect &rect)
Definition: widget.cpp:90
virtual ~widget()
Definition: widget.cpp:41
void set_height(int h)
Definition: widget.cpp:115
void hide_override(bool value=true)
Definition: widget.cpp:168
virtual void handle_window_event(const SDL_Event &event)
Definition: widget.cpp:341
std::string tooltip_text_
Definition: widget.hpp:123