Debian Codegraph @

ui/UIGadget.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
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 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/ui/UIGadget.cpp $
21 * $Revision: 28 $
22 * $Date: 10/21/99 3:31p $
23 * $Author: Kevin $
24 *
25 * Gadget code
26 *
27 * $Log: /DescentIII/Main/ui/UIGadget.cpp $
28 *
29 * 28 10/21/99 3:31p Kevin
30 * mac merge & fix for slider
31 *
32 * 27 4/14/99 1:52a Jeff
33 * fixed case mismatched #includes
34 *
35 * 26 3/23/99 9:02p Samir
36 * always null hotkey when creating gadget!
37 *
38 * 25 3/01/99 7:29p Samir
39 * fixed an issue with slaves getting freed after their master was
40 * destroyed for sliders.
41 *
42 * 24 2/22/99 8:13p Samir
43 * added slave gadget system that only uses mouse input from window, but
44 * defers keys to its master.
45 *
46 * 23 2/21/99 6:36p Samir
47 * focusing changes and key input changes to ui.,
48 *
49 * 22 2/17/99 8:32p Samir
50 * fixed bugs in remove gadget, added callbacks for attaching and
51 * detaching gadgets.
52 *
53 * 21 2/16/99 11:58a Samir
54 * small fixes and added gadget notification.
55 *
56 * 20 10/19/98 12:18p Samir
57 * made tabbing between edit controls possible.
58 *
59 * 19 10/13/98 8:23p Samir
60 * communication between gadget and window and edit system to work
61 * together.
62 *
63 * 18 8/26/98 12:25p Samir
64 * fixed the whole editbox, keyboard access mess.
65 *
66 * 17 8/25/98 7:08p Samir
67 * select how keys are sent to a gadget.
68 *
69 * 16 6/22/98 7:30p Samir
70 * set focus on gadget when calling LockFocus.
71 *
72 * 15 6/05/98 5:35p Samir
73 * massive improvement in UI keyboard interface.
74 *
75 * 14 5/25/98 8:18p Samir
76 * Enter keys are passed to gadgets too.
77 *
78 * 13 4/24/98 2:43a Samir
79 * added IsLocked function.
80 *
81 * 12 3/24/98 4:26p Samir
82 * fixed minor bug in TrapKey.
83 *
84 * 11 3/10/98 7:24p Samir
85 * Save original width and height.
86 *
87 * 10 3/09/98 2:57p Samir
88 * Remove gadget from window called from gadget destroy.
89 *
90 * 9 2/15/98 7:07p Samir
91 * Revamped way keys like up and down are handled in window system.
92 *
93 * 8 2/13/98 6:35p Samir
94 * Fixed some gadget creation/destruction inconsistancies
95 *
96 * 7 1/30/98 7:05p Samir
97 * Each gadget now has their own coordinate system.
98 *
99 * 6 1/23/98 5:34p Samir
100 * Allow for nonpersistant UIItems.
101 *
102 * 5 1/18/98 4:22p Samir
103 * Implemented new UIItem system.
104 *
105 * 4 1/13/98 4:27p Samir
106 * Keyboard timing delay stored UIGadget. Moved from UIEdit.
107 *
108 * 3 1/05/98 4:36p Samir
109 * Moved centering and fittext control flags to UIGadget class.
110 *
111 * 2 1/02/98 12:43p Samir
112 * Gadget processing functional.
113 *
114 * 1 12/30/97 4:36p Samir
115 * Initial revision
116 *
117 * $NoKeywords: $
118 */
119
120 #include <cstring>
121
122 #include "UIlib.h"
123 #include "Macros.h"
124
125 int UIGadget::m_LastKey = 0;
126 int UIGadget::m_LastKeyCount = 0;
127 float UIGadget::m_LastKeyTime = 0.0f;
128
129 UIGadget::UIGadget() : UIObject() {
130 m_ID = 0;
131 m_Wnd = NULL;
132 m_Prev = m_PrevSlave = NULL;
133 m_Next = m_NextSlave = NULL;
134 m_MasterGadget = NULL;
135 m_SlaveGadgets = NULL;
136 m_CurrentSlave = NULL;
137 m_infocus = false;
138 }
139
140 UIGadget::~UIGadget() {}
141
142 // ALL Gadgets must have a parent.
143 void UIGadget::Create(UIWindow *wnd, int id, int x, int y, int w, int h, int flags) {
144 ASSERT(!IsCreated());
145 ASSERT(wnd);
146
147 m_ID = id;
148 m_Wnd = wnd;
149
150 m_Disabled = false;
151 m_Flags = flags;
152 m_infocus = false;
153
154 // add to interface's list.
155 UIObject::Create(x, y, w, h);
156
157 // save original width and height
158 m_SavedW = m_W;
159 m_SavedH = m_H;
160 m_Hotkey = 0;
161
162 // handle formatting issues.
163 OnFormat();
164 m_MasterGadget = NULL;
165 m_SlaveGadgets = NULL;
166 m_CurrentSlave = NULL;
167 m_NextSlave = m_PrevSlave = NULL;
168 m_Datum = 0;
169
170 m_Wnd->AddGadget(this);
171 }
172
173 void UIGadget::Destroy() {
174 // slaves are freed or destroyed by now.
175 m_SlaveGadgets = NULL;
176
177 // remove gadget from windw list.
178 if (m_Wnd) {
179 m_Wnd->RemoveGadget(this);
180 }
181 OnDestroy();
182 UIObject::Destroy();
183 }
184
185 // sets hotkey for gadget
186 void UIGadget::SetHotkey(int key) { m_Hotkey = key; }
187
188 void UIGadget::SetFlag(int flag) { m_Flags |= flag; }
189
190 void UIGadget::ClearFlag(int flag) { m_Flags &= (~flag); }
191
192 // locks input on this gadget
193 void UIGadget::LockFocus() {
194 // m_Wnd->SetFocusOnGadget(this);
195 m_Wnd->LockFocusOnGadget(this);
196 }
197
198 void UIGadget::UnlockFocus() { m_Wnd->UnlockFocusOnGadget(); }
199
200 // called before drawing usually. sets x,y,w,h optionally.
201 void UIGadget::OnFormat() {
202 if (m_Flags & UIF_CENTER)
203 m_X = m_Wnd->W() / 2 - m_W / 2;
204 }
205
206 // interprets input on this gadget. this gadget is in focus.
207 void UIGadget::Process(bool do_keys, bool do_mouse, bool do_user) {
208 // do mouse processing on this gadget if and only if no slave processing was done.
209 if (UI_input.b1_status == UIMSEBTN_PRESSED && do_mouse) {
210 OnMouseBtnDown(UILMSEBTN);
211 UI_input.b1_last_status = UIMSEBTN_PRESSED; // DAJ use to latch the UI mouse button
212 } else if (UI_input.b1_status == UIMSEBTN_RELEASED && do_mouse) {
213 OnMouseBtnUp(UILMSEBTN);
214 UI_input.b1_last_status = UIMSEBTN_RELEASED; // DAJ use to latch the UI mouse button
215 } else if (UI_input.b1_status == UIMSEBTN_CLICKED && do_mouse) {
216 OnMouseBtnDown(UILMSEBTN);
217 OnMouseBtnUp(UILMSEBTN);
218 } else if (do_keys) {
219 // keyboard processing.
220 switch (UI_input.key) {
221 case KEY_ENTER:
222 if ((UI_input.key_status == UIKEY_PRESSED) || (UI_input.key_status == UIKEY_CLICKED)) {
223 OnSelect();
224 }
225 break;
226 }
227
228 if (UI_input.key_status == UIKEY_PRESSED) {
229 if (TrapKey(UI_input.key))
230 OnKeyDown(UI_input.key);
231 } else if (UI_input.key_status == UIKEY_RELEASED) {
232 UntrapKey(UI_input.key);
233 OnKeyUp(UI_input.key);
234 } else if (UI_input.key_status == UIKEY_CLICKED) {
235 if (TrapKey(UI_input.key))
236 OnKeyDown(UI_input.key);
237 UntrapKey(UI_input.key);
238 OnKeyUp(UI_input.key);
239 }
240 }
241
242 // run gadget's user process
243 if (do_user) {
244 OnUserProcess();
245 }
246 }
247
248 // when gadget is selected, this function will AT LEAST be called.
249 // typically this function is called by a child class when we want
250 // to tell the caller that this gadget was selected for something.
251 void UIGadget::OnSelect() {
252 if (m_MasterGadget && CHECK_FLAG(m_Flags, UIF_NOTIFYMASTERSEL)) {
253 m_MasterGadget->OnNotifySelect(this);
254 } else {
255 UI_output.id = m_ID;
256 UI_output.gadget = this;
257 }
258 }
259
260 void UIGadget::AttachSlaveGadget(UIGadget *gadget) {
261 // attach to list.
262 ASSERT(gadget);
263 ASSERT(!m_MasterGadget); // slaves can't own each other (sorry, been reading Uncle Tom's Cabin in class)
264
265 gadget->m_Flags |= UIF_SLAVE;
266 gadget->m_MasterGadget = this;
267 if (m_SlaveGadgets) {
268 m_SlaveGadgets->m_NextSlave = gadget;
269 }
270 gadget->m_PrevSlave = m_SlaveGadgets;
271 gadget->m_NextSlave = NULL;
272 m_SlaveGadgets = gadget;
273 }
274
275 void UIGadget::DetachSlaveGadget(UIGadget *gadget) {
276 if (!gadget) {
277 return;
278 }
279
280 ASSERT(gadget->m_MasterGadget == this);
281 ASSERT(!m_MasterGadget); // slaves can't own each other (sorry, been reading Uncle Tom's Cabin in class)
282
283 if (gadget == m_SlaveGadgets) {
284 m_SlaveGadgets = gadget->m_PrevSlave;
285 }
286 if (gadget == m_CurrentSlave) {
287 m_CurrentSlave->LostFocus();
288 m_CurrentSlave = gadget->m_PrevSlave ? gadget->m_PrevSlave : gadget->m_NextSlave;
289 }
290 if (gadget->m_PrevSlave) {
291 gadget->m_PrevSlave->m_NextSlave = gadget->m_NextSlave;
292 }
293 if (gadget->m_NextSlave) {
294 gadget->m_NextSlave->m_PrevSlave = gadget->m_PrevSlave;
295 }
296
297 gadget->m_MasterGadget = NULL;
298 gadget->m_Flags &= (~UIF_SLAVE);
299 }
300
301 ///////////////////////////////////////////////////////////////////////////////
302 // PRIVATE FUNCTIONS
303
304 // called by window system
305 void UIGadget::LostFocus() {
306 this->OnLostFocus(); // should invoke gadget's OnLostFocus function
307 if (m_CurrentSlave) {
308 ASSERT(m_SlaveGadgets);
309 m_CurrentSlave->LostFocus();
310 m_CurrentSlave = NULL;
311 }
312 m_infocus = false;
313 }
314
315 // called by window system
316 void UIGadget::GainFocus() {
317 // m_LastKey = 0;
318 // m_LastKeyTime = 0.0f;
319
320 m_infocus = true;
321 m_CurrentSlave = NULL;
322 this->OnGainFocus(); // should invoke gadget's OnGainFocus function
323 }
324
325 void UIGadget::CheckFocusOnSlaves(int mx, int my) {
326 UIGadget *slave = m_SlaveGadgets;
327
328 if (CHECK_FLAG(m_Flags, UIF_SLAVE))
329 return;
330
331 while (slave) {
332 if (PT_IN_GADGET(m_Wnd, slave, mx, my)) {
333 if (m_CurrentSlave) {
334 m_CurrentSlave->LostFocus();
335 }
336 m_CurrentSlave = slave;
337 m_CurrentSlave->GainFocus();
338 break;
339 }
340 slave = slave->m_PrevSlave;
341 }
342
343 if (!slave) {
344 if (m_CurrentSlave) {
345 m_CurrentSlave->LostFocus();
346 m_CurrentSlave = NULL;
347 }
348 }
349 }
350
351 // used to manage keypresses so repeating isn't quick.
352 bool UIGadget::TrapKey(int key) {
353 float time;
354 time = timer_GetTime();
355
356 // take care of multiple keypresses and key down repeats. handle timing
357 // so it feels 'natural'
358 if (m_LastKey && m_LastKey == key) {
359 if (m_LastKeyCount == 1) {
360 if ((time - m_LastKeyTime) < KEYDOWN_FIRST_DELAY)
361 return false;
362 } else if ((time - m_LastKeyTime) < KEYDOWN_REPEAT_DELAY) {
363 return false;
364 }
365 m_LastKeyCount++;
366 } else {
367 m_LastKeyCount = 1;
368 }
369
370 m_LastKey = key;
371 m_LastKeyTime = time;
372
373 return true;
374 }
375
376 void UIGadget::UntrapKey(int key) {
377 // if the key previously pressed went up, then
378 // turn off key repeat management.
379 if (key == m_LastKey) {
380 m_LastKeyCount = 0;
381 m_LastKey = 0;
382 }
383 }
384
385 // is this gadget's focus locked?
386 bool UIGadget::IsLocked() const {
387 if (m_Wnd->m_LockedGadget == this)
388 return true;
389 else
390 return false;
391 }
392
393 UIGadget *UIGadget::GetPrevUsableGadget() {
394 UIGadget *gadget = this;
395 UIGadget *next_gadget = NULL;
396
397 while (gadget->m_Prev) {
398 gadget = gadget->m_Prev;
399 if (!gadget->IsDisabled()) {
400 next_gadget = gadget;
401 break;
402 }
403 }
404
405 return next_gadget;
406 }
407
408 UIGadget *UIGadget::GetNextUsableGadget() {
409 UIGadget *gadget = this;
410 UIGadget *next_gadget = NULL;
411
412 while (gadget->m_Next) {
413 gadget = gadget->m_Next;
414 if (!gadget->IsDisabled()) {
415 next_gadget = gadget;
416 break;
417 }
418 }
419
420 return next_gadget;
421 }