Debian Codegraph @

ui/UIButton.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/UIButton.cpp $
21 * $Revision: 29 $
22 * $Date: 5/02/99 7:18p $
23 * $Author: Samir $
24 *
25 * Button code.
26 *
27 * $Log: /DescentIII/Main/ui/UIButton.cpp $
28 *
29 * 29 5/02/99 7:18p Samir
30 * a little better error checking with mouse input. also with fix from
31 * grtext.cpp, fixed phony button problem.
32 *
33 * 28 5/02/99 6:09p Samir
34 * unlock focus for buttons when mouse goes up regardless of state.
35 *
36 * 27 5/02/99 2:08a Samir
37 * added Enable and Disable and only accept locking of button if mouse is
38 * inside the button.
39 *
40 * 26 5/01/99 1:14a Samir
41 * fixed mouse sequencing probs with selections being canceled out by
42 * UISystem.
43 *
44 * 25 4/17/99 5:59a Jeff
45 * Linux wants m_ButtonFont defined
46 *
47 * 24 4/14/99 1:52a Jeff
48 * fixed case mismatched #includes
49 *
50 * 23 3/09/99 12:21p Samir
51 * only allow checking of boxes if mouse is in checkbox at all times, not
52 * when clicked on once.
53 *
54 * 22 2/21/99 6:36p Samir
55 * focusing changes and key input changes to ui.,
56 *
57 * 21 2/16/99 11:58a Samir
58 * format buttons to fit their state.
59 *
60 * 20 12/08/98 3:18p Samir
61 * checking a check box calls OnSelect
62 *
63 * 19 9/30/98 4:33p Samir
64 * disabled gadgets display dim text.
65 *
66 * 18 9/23/98 3:00p Samir
67 * took out unnecessary code.
68 *
69 * 17 5/22/98 5:02p Jeff
70 * Added a flag for radio buttons so the button part won't be drawn (just
71 * the text)
72 *
73 * 16 5/22/98 1:31p Jeff
74 * changed text saturation for radio button
75 *
76 * 15 5/01/98 6:23p Samir
77 * darkened buttons a bit.
78 *
79 * 14 4/24/98 2:42a Samir
80 * added UICheckBox
81 *
82 * 13 3/10/98 12:50p Samir
83 * Added highlight to radio button.
84 *
85 * 12 3/05/98 6:40p Samir
86 * Implemented radio buttons.
87 *
88 * 11 2/10/98 4:55p Samir
89 * Added radio buttons.
90 *
91 * 10 1/30/98 7:05p Samir
92 * Each gadget now has their own coordinate system.
93 *
94 * 9 1/23/98 5:34p Samir
95 * Allow for nonpersistant UIItems.
96 *
97 * 8 1/18/98 4:22p Samir
98 * Implemented new UIItem system.
99 *
100 * 7 1/13/98 4:29p Samir
101 * Added gadget buttons.
102 *
103 * 6 1/08/98 12:18p Samir
104 * Buttons draw text color based off of UITextItem color.
105 *
106 * 5 1/05/98 4:36p Samir
107 * Moved centering and fittext control flags to UIGadget class.
108 *
109 * 4 1/05/98 3:48p Jason
110 * added SetAllText function for buttons
111 *
112 * 3 1/05/98 3:18p Samir
113 * Added some button flags.
114 *
115 * 2 1/02/98 12:43p Samir
116 * New UIButton interface.
117 *
118 * 1 12/30/97 4:36p Samir
119 * Initial revision
120 *
121 * $NoKeywords: $
122 */
123
124 #include <cstring>
125
126 #include "UIlib.h"
127
128 #if defined(POSIX)
129 int UIButton::m_ButtonFont;
130 #endif
131
132 // tUIButton
133 // This is a simple 2 state button (push down or push up)
134 // Buttons have properties of gadgets, plus bitmap information per state.
135
136 UIButton::UIButton() {
137 m_Items[UI_BTS_DISABLED] = NULL;
138 m_Items[UI_BTS_INACTIVE] = NULL;
139 m_Items[UI_BTS_HILITE] = NULL;
140 m_Items[UI_BTS_ACTIVATED] = NULL;
141 }
142
143 UIButton::~UIButton() {}
144
145 // if text = NULL, no text printed out.
146 void UIButton::Create(UIWindow *parent, int id, UIItem *item, int x, int y, int w, int h, int flags) {
147 if (item) {
148 m_Items[UI_BTS_DISABLED] = item->CopyUIItem();
149 m_Items[UI_BTS_INACTIVE] = item->CopyUIItem();
150 m_Items[UI_BTS_HILITE] = item->CopyUIItem();
151 m_Items[UI_BTS_ACTIVATED] = item->CopyUIItem();
152 }
153
154 SetColor(UI_BTS_DISABLED, GR_DARKGRAY);
155 SetColor(UI_BTS_INACTIVE, GR_DARKGRAY);
156 SetColor(UI_BTS_HILITE, GR_RGB(128, 128, 128));
157 SetColor(UI_BTS_ACTIVATED, GR_RGB(128, 128, 128));
158
159 m_State = UI_BTS_INACTIVE; // current state of button.
160
161 UIGadget::Create(parent, id, x, y, w, h, flags);
162 }
163
164 // sets the background bitmap for a specified button state
165 void UIButton::SetStateItem(int state, UIItem *item) {
166 ASSERT(state < UI_BTS_NUM);
167 if (m_Items[state]) {
168 delete m_Items[state];
169 m_Items[state] = NULL;
170 }
171 if (item)
172 m_Items[state] = item->CopyUIItem();
173 }
174
175 // sets the background color for a specified button state
176 void UIButton::SetColor(int state, ddgr_color col) {
177 ASSERT(state < UI_BTS_NUM);
178
179 m_Colors[state] = col;
180 }
181
182 // sets text for all states.
183 void UIButton::SetAllStates(UIItem *item) {
184 for (int i = 0; i < UI_BTS_NUM; i++)
185 SetStateItem(i, item);
186 }
187
188 // Inheritable operations
189
190 // override: behavior when gadget is destroyed.
191 void UIButton::OnDestroy() {
192 UIGadget::OnDestroy();
193
194 SetStateItem(UI_BTS_DISABLED, NULL);
195 SetStateItem(UI_BTS_INACTIVE, NULL);
196 SetStateItem(UI_BTS_HILITE, NULL);
197 SetStateItem(UI_BTS_ACTIVATED, NULL);
198 }
199
200 // behavior when gadget loses input focus.
201 void UIButton::OnLostFocus() {
202 if (m_State == UI_BTS_HILITE)
203 m_State = UI_BTS_INACTIVE;
204 }
205
206 // behavior when gadget gains input focus.
207 void UIButton::OnGainFocus() {
208 if (m_State != UI_BTS_ACTIVATED)
209 m_State = UI_BTS_HILITE;
210 }
211
212 // override: called when resized or before drawing.
213 void UIButton::OnFormat() {
214 if (m_Flags & UIF_FIT) {
215 m_W = m_Items[m_State]->width() + 20;
216 m_H = m_Items[m_State]->height() + 8;
217 }
218
219 UIGadget::OnFormat();
220 }
221
222 // behavior when gadget is being drawn.
223 void UIButton::OnDraw() {
224 // do state based drawing
225 // determine colors here.
226 if (IsDisabled())
227 m_State = UI_BTS_DISABLED;
228
229 switch (m_State) {
230 case UI_BTS_ACTIVATED:
231 case UI_BTS_HILITE:
232 ui_DrawLTBox(GR_BLACK, GR_LIGHTGRAY, 0, 0, m_W, m_H);
233 break;
234
235 case UI_BTS_INACTIVE:
236 ui_DrawLTBox(GR_LIGHTGRAY, GR_BLACK, 0, 0, m_W, m_H);
237 break;
238
239 case UI_BTS_DISABLED:
240 ui_DrawLTBox(GR_DARKGRAY, GR_DARKGRAY, 0, 0, m_W, m_H);
241 break;
242
243 default:
244 Int3();
245 }
246
247 ui_DrawRect(m_Colors[m_State], 1, 1, m_W - 1, m_H - 1);
248
249 if (m_Items[m_State]) {
250 int x = (m_W / 2 - m_Items[m_State]->width() / 2);
251 int y = (m_H / 2 - m_Items[m_State]->height() / 2);
252 m_Items[m_State]->draw(x, y, IsDisabled() ? uiDrawFaded : uiDrawNormal);
253 }
254 }
255
256 // behavior when key is pressed.
257 void UIButton::OnKeyDown(int key) {}
258
259 // behavior when key is released.
260 void UIButton::OnKeyUp(int key) {}
261
262 // behavior when mouse button is pressed.
263 void UIButton::OnMouseBtnDown(int btn) {
264 if (btn == UILMSEBTN) {
265 if (m_State != UI_BTS_ACTIVATED && PT_IN_GADGET(m_Wnd, this, UI_input.mx, UI_input.my)) {
266 m_State = UI_BTS_ACTIVATED;
267 LOCK_FOCUS(this);
268 // mprintf(0, "Mouse down, locking gadget.\n");
269 } else if (m_State == UI_BTS_ACTIVATED && !PT_IN_GADGET(m_Wnd, this, UI_input.mx, UI_input.my)) {
270 m_State = UI_BTS_HILITE;
271 }
272 }
273 }
274
275 // behavior when mouse button is released.
276 void UIButton::OnMouseBtnUp(int btn) {
277 if (btn == UILMSEBTN) {
278 if (m_State == UI_BTS_ACTIVATED) {
279 if (PT_IN_GADGET(UIGadget::m_Wnd, this, UI_input.mx, UI_input.my)) {
280 OnSelect();
281 }
282 m_State = UI_BTS_HILITE;
283 }
284 // mprintf(0, "Mouse up, unlocking gadget.\n");
285 UNLOCK_FOCUS(this);
286 }
287 }
288
289 // this adds to the gadget equivalent
290 void UIButton::Disable() {
291 UIGadget::Disable();
292 m_State = UI_BTS_DISABLED;
293 }
294
295 // this adds to the gadget equivalent
296 void UIButton::Enable() {
297 UIGadget::Enable();
298
299 if (HasFocus()) {
300 m_State = UI_BTS_HILITE;
301 } else {
302 m_State = UI_BTS_INACTIVE;
303 }
304 }
305
306 // UIRadioButton
307 // This is a simple 2 state button (push down or push up)
308 // Buttons have properties of gadgets, plus bitmap information per state.
309
310 UIRadioButton::UIRadioButton() { m_PrevRB = m_NextRB = NULL; }
311
312 void UIRadioButton::Create(UIWindow *parent, UIRadioButton *prev_rb, int id, UIItem *title, int x, int y, int w, int h,
313 int flags) {
314 UIButton::Create(parent, id, title, x, y, w, h, flags);
315
316 // create link in radio button chain.
317 m_PrevRB = prev_rb;
318 m_NextRB = NULL;
319 if (m_PrevRB) {
320 m_NextRB = m_PrevRB->m_NextRB;
321 if (m_NextRB) {
322 m_NextRB->m_PrevRB = this;
323 }
324 m_PrevRB->m_NextRB = this;
325 }
326
327 if (!m_PrevRB) {
328 UIGadget::SetFlag(UIF_GROUP_START);
329 } else {
330 m_PrevRB->ClearFlag(UIF_GROUP_END);
331 }
332 if (!m_NextRB) {
333 UIGadget::SetFlag(UIF_GROUP_END);
334 }
335
336 // THIS PROBLEM IS PROBABLY DUE TO THE CALLER'S FAULT!
337 ASSERT(m_PrevRB != this);
338 }
339
340 // turns on button, turns off others in group.
341 void UIRadioButton::Activate() { ActivateButton(); }
342
343 // behavior when gadget is being drawn.
344 void UIRadioButton::OnDraw() {
345 char btnstr[3];
346 int iterations = 0;
347
348 if (IsDisabled())
349 m_State = UI_BTS_DISABLED;
350
351 switch (m_State) {
352 case UI_BTS_HILITE:
353 iterations = 1;
354 case UI_BTS_INACTIVE:
355 case UI_BTS_DISABLED:
356 if (m_Flags & UIRB_NOBUTTON)
357 strcpy(btnstr, " ");
358 else
359 snprintf(btnstr, sizeof(btnstr), "%c ", UI_RADIO_OFF_CHAR);
360 break;
361 case UI_BTS_ACTIVATED:
362 iterations = 1;
363 if (m_Flags & UIRB_NOBUTTON)
364 strcpy(btnstr, " ");
365 else
366 snprintf(btnstr, sizeof(btnstr), "%c ", UI_RADIO_ON_CHAR);
367 break;
368
369 default:
370 btnstr[0] = 0;
371 }
372
373 UITextItem btn(btnstr, GetStateItem(m_State)->get_color(), GetStateItem(m_State)->get_alpha());
374
375 btn.draw(0, 0, IsDisabled() ? uiDrawFaded : uiDrawNormal);
376 GetStateItem(m_State)->draw(ui_GetTextWidth(btnstr), 0, IsDisabled() ? uiDrawFaded : uiDrawNormal);
377
378 while (iterations--) {
379 btn.draw(0, 0, uiDrawAlphaSaturate);
380 GetStateItem(m_State)->draw(ui_GetTextWidth(btnstr), 0, uiDrawAlphaSaturate);
381 }
382 }
383
384 // override: called when resized or before drawing.
385 void UIRadioButton::OnFormat() {
386 if (m_Flags & UIF_FIT) {
387 char btnstr[3];
388 if (m_Flags & UIRB_NOBUTTON)
389 strcpy(btnstr, " ");
390 else
391 snprintf(btnstr, sizeof(btnstr), "%c ", UI_RADIO_OFF_CHAR);
392
393 UITextItem btn(btnstr, GetStateItem(m_State)->get_color(), GetStateItem(m_State)->get_alpha());
394
395 m_W = GetStateItem(m_State)->width() + btn.width();
396 m_H = GetStateItem(m_State)->height() + 8;
397 }
398
399 UIGadget::OnFormat();
400 }
401
402 // behavior when gadget loses input focus.
403 void UIRadioButton::OnLostFocus() {}
404
405 // behavior when gadget gains input focus.
406 void UIRadioButton::OnGainFocus() {}
407
408 // behavior when key is pressed.
409 void UIRadioButton::OnKeyDown(int key) {
410 if (key == KEY_SPACEBAR || key == KEY_ENTER)
411 ActivateButton();
412 }
413
414 // behavior when key is released.
415 void UIRadioButton::OnKeyUp(int key) {}
416
417 // behavior when mouse button is pressed.
418 void UIRadioButton::OnMouseBtnDown(int btn) {
419 if (btn == UILMSEBTN && !this->IsLocked()) {
420 if (PT_IN_GADGET(m_Wnd, this, UI_input.mx, UI_input.my)) {
421 ActivateButton();
422 LOCK_FOCUS(this);
423 }
424 }
425 }
426
427 // behavior when mouse button is released.
428 void UIRadioButton::OnMouseBtnUp(int btn) {
429 if (btn == UILMSEBTN) {
430 UNLOCK_FOCUS(this);
431 }
432 }
433
434 // activates this button, deactivating all others.
435 void UIRadioButton::ActivateButton() {
436 // okay, start through list
437 UIRadioButton *radiolist = this;
438
439 while (radiolist->m_PrevRB)
440 radiolist = radiolist->m_PrevRB;
441
442 // deactivate all buttons until we hit the current button, then activate it.
443 while (radiolist) {
444 if (radiolist == this) {
445 radiolist->m_State = UI_BTS_ACTIVATED;
446 OnSelect();
447 } else {
448 radiolist->m_State = UI_BTS_INACTIVE;
449 }
450
451 radiolist = radiolist->m_NextRB;
452 }
453 }
454
455 //////////////////////////////////////////////////////////////////////////////
456 // UICheckBox
457 // This is a simple 2 state button (push down or push up)
458 // Buttons have properties of gadgets, plus bitmap information per state.
459
460 UICheckBox::UICheckBox() {}
461
462 void UICheckBox::Create(UIWindow *parent, int id, UIItem *title, int x, int y, int w, int h, int flags) {
463 UIButton::Create(parent, id, title, x, y, w, h, flags);
464 SetCheck(false);
465 }
466
467 // turns on button, turns off others in group.
468 void UICheckBox::SetCheck(bool state) {
469 if (state) {
470 m_State = UI_BTS_ACTIVATED;
471 } else {
472 m_State = UI_BTS_INACTIVE;
473 }
474 }
475
476 // behavior when gadget is being drawn.
477 void UICheckBox::OnDraw() {
478 char btnstr[3];
479 int iterations = 1;
480
481 ui_DrawSetTextType(UI_TEXTTYPE_SATURATE);
482
483 if (IsDisabled())
484 m_State = UI_BTS_DISABLED;
485
486 switch (m_State) {
487 case UI_BTS_HILITE:
488 iterations = 2;
489 case UI_BTS_INACTIVE:
490 case UI_BTS_DISABLED:
491 snprintf(btnstr, sizeof(btnstr), "%c ", UI_CHECKBOX_OFF_CHAR);
492 break;
493 case UI_BTS_ACTIVATED:
494 iterations = 2;
495 snprintf(btnstr, sizeof(btnstr), "%c ", UI_CHECKBOX_ON_CHAR);
496 break;
497
498 default:
499 btnstr[0] = 0;
500 }
501
502 UITextItem btn(btnstr, GetStateItem(m_State)->get_color(), GetStateItem(m_State)->get_alpha());
503
504 while (iterations--) {
505 btn.draw(0, 0);
506 GetStateItem(m_State)->draw(ui_GetTextWidth(btnstr), 0, IsDisabled() ? uiDrawFaded : uiDrawNormal);
507 }
508
509 ui_DrawSetTextType(0);
510 }
511
512 // override: called when resized or before drawing.
513 void UICheckBox::OnFormat() {
514 if (m_Flags & UIF_FIT) {
515 char btnstr[3];
516 snprintf(btnstr, sizeof(btnstr), "%c ", UI_CHECKBOX_OFF_CHAR);
517
518 UITextItem btn(btnstr, GetStateItem(m_State)->get_color(), GetStateItem(m_State)->get_alpha());
519
520 m_W = GetStateItem(m_State)->width() + btn.width();
521 m_H = GetStateItem(m_State)->height() + 8;
522 }
523
524 UIGadget::OnFormat();
525 }
526
527 // behavior when gadget loses input focus.
528 void UICheckBox::OnLostFocus() {}
529
530 // behavior when gadget gains input focus.
531 void UICheckBox::OnGainFocus() {}
532
533 // behavior when key is pressed.
534 void UICheckBox::OnKeyDown(int key) {
535 if (key == KEY_SPACEBAR || key == KEY_ENTER) {
536 SetCheck((m_State == UI_BTS_ACTIVATED) ? false : true);
537 OnSelect();
538 }
539 }
540
541 // behavior when key is released.
542 void UICheckBox::OnKeyUp(int key) {}
543
544 // behavior when mouse button is pressed.
545 void UICheckBox::OnMouseBtnDown(int btn) {
546 if (btn == UILMSEBTN && !this->IsLocked()) {
547 if (PT_IN_GADGET(m_Wnd, this, UI_input.mx, UI_input.my)) {
548 SetCheck((m_State == UI_BTS_ACTIVATED) ? false : true);
549 OnSelect();
550 LOCK_FOCUS(this);
551 }
552 }
553 }
554
555 // behavior when mouse button is released.
556 void UICheckBox::OnMouseBtnUp(int btn) {
557 if (btn == UILMSEBTN) {
558 UNLOCK_FOCUS(this);
559 }
560 }