ScummVM API documentation
gui_widget.h
1/* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22/* Base class for all widgets -- the GUI operates on this class */
23
24#ifndef NUVIE_GUI_GUI_WIDGET_H
25#define NUVIE_GUI_GUI_WIDGET_H
26
27#include "ultima/nuvie/gui/gui_status.h"
28#include "ultima/nuvie/gui/gui_drag_area.h"
29#include "ultima/nuvie/gui/gui_drag_manager.h"
30#include "ultima/nuvie/screen/screen.h"
31#include "ultima/nuvie/core/events.h"
32
33namespace Ultima {
34namespace Nuvie {
35
36typedef GUI_status(*GUI_CallbackProc)(void *data);
37
38class GUI_Widget : public GUI_DragArea {
39
40protected:
41 /* A generic pointer to user-specified data for the widget.
42 */
43 void *widget_data;
44
45 Screen *screen;
46 /* The display surface for the widget */
48
49 int offset_x, offset_y; /* original offsets to parent */
50
51 /* Flag -- whether or not the widget should be freed */
52 WIDGET_status status;
53
54 /* should we redraw this widget */
55 bool update_display;
56
57 /* the button states for theoretically 3 buttons */
58 int pressed[3];
59
60 bool focused;
61
63 GUI_Widget *parent;
64
65 char *errorptr;
66 char errbuf[BUFSIZ];
67
68 GUI_DragManager *gui_drag_manager;
69
70 // SB-X
71 /* The time of the last mouse click (SDL_GetTicks()). */
72 unsigned int mouseup[3]; /* for 3 buttons */
73 unsigned int mousedown[3]; /* waiting for MouseUp */
74 bool accept_mouseclick[3]; /* which buttons can be [double]clicked */
75 Events::MouseButton delayed_button; /* a MouseClick can be delayed on one button; 0=none */
76 Events::MouseButton held_button; /* a MouseDown can be delayed on one button; 0=none */
77 bool mouse_moved; /* true if mouse moves while button is pressed */
78
79 bool mouse_over; // initialized here; toggled by GUI
80
81public:
82 /* The area covered by the widget */
83 Common::Rect area;
84
85 GUI_Widget(void *data);
86 GUI_Widget(void *data, int x, int y, int w, int h);
87 ~GUI_Widget() override;
88
89 int AddWidget(GUI_Widget *widget);
90
91 /* Mark the widget as visible -- this is the default state */
92 virtual void Show(void);
93
94 /* Mark the widget as hidden; no display, no events */
95 virtual void Hide(void);
96
97 /* Mark the widget as free, so it will be deleted by the GUI */
98 virtual void Delete(void);
99
100 virtual void MoveRelative(int dx, int dy);
101 virtual void Move(int32 new_x, int32 new_y);
102 void MoveRelativeToParent(int dx, int dy);
103 bool has_focus() const {
104 return focused;
105 }
106 void grab_focus();
107 virtual void release_focus();
108 void moveToFront();
109 virtual void PlaceOnScreen(Screen *s, GUI_DragManager *dm, int x, int y);
110
111 virtual WIDGET_status Status(void) const; /* Reports status to GUI */
112
113 /* Set the bounds of the widget.
114 If 'w' or 'h' is -1, that parameter will not be changed.
115 */
116 virtual void SetRect(int x, int y, int w, int h);
117 virtual void SetRect(Common::Rect **bounds);
118
119 /* Return the whole area */
120 virtual Common::Rect GetRect() {
121 return area;
122 }
123
124 /* Return the bounds of the widget */
125 virtual int X() const {
126 return area.left;
127 }
128 virtual int Y() const {
129 return area.top;
130 }
131 virtual int W() const {
132 return area.width();
133 }
134 virtual int H() const {
135 return area.height();
136 }
137
138 /* Check to see if a point intersects the bounds of the widget.
139 */
140 virtual int HitRect(int x, int y);
141 virtual int HitRect(int x, int y, const Common::Rect &rect);
142
143 /* Set the display surface for this widget */
144 virtual void SetDisplay(Screen *s);
145
146 /* Show the widget.
147 If the surface needs to be locked, it will be locked
148 before this call, and unlocked after it returns.
149 */
150 virtual void Display(bool full_redraw = false);
151 void DisplayChildren(bool full_redraw = false);
152
153 /* Redraw the widget and only the widget */
154 virtual void Redraw(void);
155
156 /* should this widget be redrawn */
157 inline bool needs_redraw() const {
158 return update_display;
159 }
160 /* widget has focus or no widget is focused */
161 bool widget_has_focus(); // SB-X
162
163 /* GUI idle function -- run when no events pending */
164 virtual GUI_status Idle(void);
165
166 /* Widget event handlers.
167 These functions should return a status telling the GUI whether
168 or not the event should be passed on to other widgets.
169 These are called by the default HandleEvent function.
170 */
171 virtual GUI_status KeyDown(const Common::KeyState &key);
172 virtual GUI_status KeyUp(Common::KeyState key);
173 virtual GUI_status MouseDown(int x, int y, Events::MouseButton button);
174 virtual GUI_status MouseUp(int x, int y, Events::MouseButton button);
175 virtual GUI_status MouseMotion(int x, int y, uint8 state);
176 virtual GUI_status MouseWheel(sint32 x, sint32 y);
177 // <SB-X>
178 virtual GUI_status MouseEnter(uint8 state);
179 virtual GUI_status MouseLeave(uint8 state);
180 virtual GUI_status MouseClick(int x, int y, Events::MouseButton button);
181 virtual GUI_status MouseDouble(int x, int y, Events::MouseButton button);
182 virtual GUI_status MouseDelayed(int x, int y, Events::MouseButton button);
183 virtual GUI_status MouseHeld(int x, int y, Events::MouseButton button);
184 // </SB-X>
185
186 bool drag_accept_drop(int x, int y, int message, void *data) override;
187 void drag_perform_drop(int x, int y, int message, void *data) override;
188
189 /* Main event handler function.
190 This function gets raw SDL events from the GUI.
191 */
192 virtual GUI_status HandleEvent(const Common::Event *event);
193
194 /* Returns nullptr if everything is okay, or an error message if not */
195 char *Error(void) {
196 return errorptr;
197 }
198
199 /* yields click state: none, pressed, intermediate */
200 inline virtual int ClickState(int button) {
201 return pressed[button - 1];
202 }
203
204 /* set click state from remote */
205 inline virtual void SetClickState(int button, int value) {
206 if ((button > 0) && (button <= 3)) pressed[button - 1] = value;
207 }
208
209protected:
210 /* The constructor, separated out for both access constructors */
211 void Init(void *data, int x, int y, int w, int h);
212
213 void setParent(GUI_Widget *widget);
214
215 /* Useful for getting error feedback */
216 void SetError(char *fmt, ...) {
217 va_list ap;
218
219 va_start(ap, fmt);
220 Common::vsprintf_s(errbuf, fmt, ap);
221 va_end(ap);
222 errorptr = errbuf;
223 }
224
225 // SB-X
226 void set_accept_mouseclick(bool set, int button = 0);
227 void set_mouseup(int set, int button = 0);
228 void set_mousedown(int set, int button = 0);
229 int get_mouseup(int button) const {
230 if (button > 0 && button < 4) return (mouseup[button - 1]);
231 else return 0;
232 }
233 int get_mousedown(int button) const {
234 if (button > 0 && button < 4) return (mousedown[button - 1]);
235 else return 0;
236 }
237 void wait_for_mouseclick(int button) {
238 if (button >= Events::BUTTON_NONE && button < Events::BUTTON_MIDDLE)
239 delayed_button = (Events::MouseButton)button;
240 }
241 void wait_for_mousedown(int button) {
242 if (button >= Events::BUTTON_NONE && button <= Events::BUTTON_MIDDLE)
243 held_button = (Events::MouseButton)button;
244 }
245 virtual GUI_status try_mouse_delayed();
246};
247
248} // End of namespace Nuvie
249} // End of namespace Ultima
250
251#endif
Definition: list.h:44
Definition: managed_surface.h:51
Definition: gui_drag_area.h:30
Definition: gui_drag_manager.h:36
Definition: gui_widget.h:38
Definition: screen.h:41
int vsprintf_s(char *dst, size_t size, const char *format, va_list ap) GCC_PRINTF(3
Definition: events.h:218
Definition: keyboard.h:294
T height() const
Definition: rect.h:218
T left
Definition: rect.h:170
T width() const
Definition: rect.h:217
Definition: rect.h:536