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