Debian Codegraph @

ui/UIListBox.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/UIListBox.cpp $
21 * $Revision: 42 $
22 * $Date: 4/30/99 6:49p $
23 * $Author: Samir $
24 *
25 * Listbox code
26 *
27 * $Log: /DescentIII/Main/ui/UIListBox.cpp $
28 *
29 * 42 4/30/99 6:49p Samir
30 * fixed bug when scroll down with keyboard in last visible item of
31 * listbox.
32 *
33 * 41 4/14/99 1:53a Jeff
34 * fixed case mismatched #includes
35 *
36 * 40 3/22/99 1:55p Jeff
37 * fixed more crashes
38 *
39 * 39 3/22/99 12:47p Jeff
40 * oops...fixed some listbox crashes
41 *
42 * 38 3/19/99 9:21p Jeff
43 * fixed some listbox sorting issues
44 *
45 * 37 2/24/99 1:14p Jeff
46 * fixed sorting bug when you have 2 identical items
47 *
48 * 36 2/21/99 6:36p Samir
49 * focusing changes and key input changes to ui.,
50 *
51 * 35 2/04/99 12:10p Jeff
52 * fixed SelectItem
53 *
54 * 34 1/27/99 10:26p Jeff
55 * fixed sorting bug, very odd it hasn't cropped up before, only happened
56 * when allocating a new buffer of indices
57 *
58 * 32 10/19/98 6:30p Jeff
59 * changes made for detail variables. Put in preset values. Preset
60 * options. Removed terrain_cast from detail. Put new callbacks in
61 * UIListBox and UISlider
62 *
63 * 31 10/06/98 8:05p Samir
64 * added page up and page down!
65 *
66 * 30 10/06/98 7:30p Samir
67 * scrolling listbox is cleaner.
68 *
69 * 29 9/30/98 4:33p Samir
70 * listbox broke for keyboard for now.
71 *
72 * 28 9/22/98 3:57p Samir
73 * listbox prob fixed again.
74 *
75 * 27 9/10/98 6:32p Samir
76 * changed some visual characteristics of listboxes.
77 *
78 * 26 9/02/98 6:50p Samir
79 * fixed some double clicking issues for listboxes.
80 *
81 * 25 8/31/98 12:37p Samir
82 * SetSelectedIndex modifies visible index as well, and dblclick works
83 * again.
84 *
85 * 24 8/25/98 5:27p Samir
86 * new focusing rules for keyboard.
87 *
88 * 23 8/24/98 3:12p Samir
89 * fixed text clipping
90 *
91 * 22 8/11/98 11:38a Samir
92 * fixed listbox probs yet again. arrows on side this time.
93 *
94 * 21 7/29/98 3:26p Samir
95 * fixed some listbox mouse problems.
96 *
97 * 20 7/28/98 7:13p Samir
98 * reset index when removing all items.
99 *
100 * 19 7/15/98 2:26p Samir
101 * may have fixed something with arrows.
102 *
103 * 18 6/22/98 2:15p Samir
104 * implemented border flag.
105 *
106 * 17 6/05/98 5:36p Samir
107 * cleaner keyboard inteface for listboxes.
108 *
109 * 16 4/29/98 1:04p Jeff
110 * fixed up the UIListBox a bit (color control and scroll buttons)
111 *
112 * 15 4/29/98 11:55a Samir
113 * allow user to change color of selected listbox item.
114 *
115 * 14 4/13/98 7:02p Samir
116 * made some things protected.
117 *
118 * 13 3/16/98 11:43a Brent
119 * (Jeff) fixed UIListBox so it doesn't draw arrows if nothing in the box
120 *
121 * 12 3/13/98 5:19p Jeff
122 * added scroll button code
123 *
124 * 11 3/09/98 5:08p Samir
125 * fixed bug.
126 *
127 * 10 3/09/98 5:05p Samir
128 * Added callback to selection change function.
129 *
130 * 9 3/09/98 3:43p Samir
131 * Single clicks should select an item in a listbox.
132 *
133 * 8 2/24/98 3:05p Samir
134 * don't set listindex when selecting.
135 *
136 * 7 2/15/98 7:07p Samir
137 * Listbox now supports up and down arrow keys.
138 *
139 * 6 2/13/98 6:36p Samir
140 * Double click to select.
141 *
142 * 5 2/10/98 4:56p Samir
143 * Listbox with alphas and a selection bar.
144 *
145 * 4 2/10/98 3:45p Samir
146 * Fixed bug in drawing items over the boundaries of the listbox.
147 *
148 * 3 1/30/98 7:07p Samir
149 * Simple box containing items. Must be handled by a parent.
150 *
151 * 2 1/13/98 4:28p Samir
152 * Fixed typo.
153 *
154 * 1 1/09/98 12:00p Samir
155 * Initial revision
156 *
157 * $NoKeywords: $
158 */
159
160 #include <cstdlib>
161 #include <cstring>
162
163 #include "UIlib.h"
164
165 #define SCROLL_BUTTON_GAP 4
166
167 // Construction and destruction
168
169 UIListBox::UIListBox() {
170 m_NumItems = 0;
171 m_SelectedIndex = 0;
172 m_Index = 0;
173 m_ItemList = NULL;
174 m_Virt2Real = NULL;
175 m_Real2Virt = NULL;
176 m_MouseState = 0;
177 selectchange_fn = NULL;
178 selectchange_id_fn = NULL;
179 m_callbackptr = NULL;
180 m_TextOffX = 4;
181 m_TextOffY = 4;
182 }
183
184 UIListBox::~UIListBox() {}
185
186 void UIListBox::Create(UIWindow *parent, int id, int x, int y, int w, int h, int flags) {
187 m_NumItems = 0;
188 m_ItemList = NULL;
189 m_Virt2Real = NULL;
190 m_Real2Virt = NULL;
191 m_SelectedIndex = 0;
192 m_Index = 0;
193 m_MouseState = 0;
194 m_ClickTime = 0;
195 selectchange_fn = NULL;
196 selectchange_id_fn = NULL;
197 m_callbackptr = NULL;
198 m_ShowDown = false;
199 m_ShowUp = false;
200 m_SelectColor = GR_WHITE;
201 m_HiliteColor = GR_DARKGRAY;
202 m_NumVisibleItems = 0;
203 m_Alpha = 192;
204
205 UIGadget::Create(parent, id, x, y, w, h, flags);
206 m_CX = 1;
207 m_CY = 1;
208 m_CX2 = m_W - 1;
209 m_CY2 = m_H - 1;
210 }
211
212 // settings
213
214 // adds an item to the list, no sorting
215 void UIListBox::AddItem(const UIItem *item) {
216 UIItem **temp;
217 int i;
218 int *tempvirt2real, *tempreal2virt;
219 int real_index = -1;
220
221 if (m_Flags & UILB_NOSORT) {
222 // insert without sort
223
224 if ((m_NumItems % LISTBOX_BUFFER_SIZE) == 0) {
225 // dynamically allocated memory for listbox, expand as needed.
226 temp = new UIItem *[m_NumItems + LISTBOX_BUFFER_SIZE];
227 tempvirt2real = new int[m_NumItems + LISTBOX_BUFFER_SIZE];
228 tempreal2virt = new int[m_NumItems + LISTBOX_BUFFER_SIZE];
229
230 if (m_ItemList) {
231 for (i = 0; i < m_NumItems; i++) {
232 temp[i] = m_ItemList[i];
233 tempvirt2real[i] = m_Virt2Real[i];
234 tempreal2virt[i] = m_Real2Virt[i];
235 }
236
237 delete[] m_ItemList;
238 delete[] m_Virt2Real;
239 delete[] m_Real2Virt;
240 }
241 m_ItemList = temp;
242 m_Virt2Real = tempvirt2real;
243 m_Real2Virt = tempreal2virt;
244 }
245
246 real_index = m_NumItems;
247
248 } else {
249 // only text item listboxes!
250
251 // insert with sorting
252 if ((m_NumItems % LISTBOX_BUFFER_SIZE) == 0) {
253 // dynamically allocated memory for listbox, expand as needed.
254 temp = new UIItem *[m_NumItems + LISTBOX_BUFFER_SIZE];
255 tempvirt2real = new int[m_NumItems + LISTBOX_BUFFER_SIZE];
256 tempreal2virt = new int[m_NumItems + LISTBOX_BUFFER_SIZE];
257 int oidx = 0, cidx = 0;
258 int pidx = 0;
259 real_index = -1;
260 int result;
261
262 if (m_ItemList) {
263 while (oidx <= m_NumItems) {
264 if (oidx == m_NumItems) {
265 if (real_index == -1) {
266 real_index = oidx;
267 } else {
268 temp[cidx] = m_ItemList[pidx];
269 tempreal2virt[cidx] = m_Real2Virt[pidx];
270 }
271 oidx++;
272 continue;
273 }
274
275 if (m_Flags & UILB_CASESENSITIVE)
276 result = strcmp(((UITextItem *)m_ItemList[cidx])->GetBuffer(), ((UITextItem *)item)->GetBuffer());
277 else
278 result = stricmp(((UITextItem *)m_ItemList[cidx])->GetBuffer(), ((UITextItem *)item)->GetBuffer());
279 if (result < 0) {
280 // copy old
281 temp[cidx] = m_ItemList[pidx];
282 tempreal2virt[cidx] = m_Real2Virt[pidx];
283 cidx++;
284 pidx++;
285 } else if (result >= 0) {
286 // see if we need to insert, if not just keep copying
287 if (real_index == -1) {
288 real_index = cidx;
289 cidx++;
290 } else {
291 temp[cidx] = m_ItemList[pidx];
292 tempreal2virt[cidx] = m_Real2Virt[pidx];
293 cidx++;
294 pidx++;
295 }
296 }
297 oidx++;
298 }
299 ASSERT(real_index != -1);
300
301 // now we need to fix m_Virt2Real to represent the new structure,
302 // all values>=real_index must be incremented
303 for (oidx = 0; oidx <= m_NumItems; oidx++) {
304 if (oidx < m_NumItems) {
305 tempvirt2real[oidx] = m_Virt2Real[oidx];
306
307 // if(oidx!=real_index){
308 if (tempvirt2real[oidx] >= real_index)
309 tempvirt2real[oidx]++;
310 }
311 }
312
313 delete[] m_ItemList;
314 delete[] m_Virt2Real;
315 delete[] m_Real2Virt;
316 } else {
317 real_index = 0;
318 }
319
320 m_ItemList = temp;
321 m_Virt2Real = tempvirt2real;
322 m_Real2Virt = tempreal2virt;
323 } else {
324 // find where to insert, without allocating memory
325 int oidx = 0, cidx = 0;
326 real_index = -1;
327 int result;
328
329 int old_r2v;
330 UIItem *old_item = NULL;
331
332 while (oidx <= m_NumItems) {
333
334 if (oidx == m_NumItems) {
335 // we're past the slot of the old buffer
336 if (real_index == -1) {
337 // we haven't set the real index yet, so it has to go here
338 real_index = m_NumItems;
339 } else {
340 // the real index has been set, we're still pulling back
341 m_Real2Virt[cidx] = old_r2v;
342 m_ItemList[cidx] = old_item;
343 }
344 oidx++;
345 continue;
346 } else {
347 if (m_Flags & UILB_CASESENSITIVE)
348 result = strcmp(((UITextItem *)m_ItemList[cidx])->GetBuffer(), ((UITextItem *)item)->GetBuffer());
349 else
350 result = stricmp(((UITextItem *)m_ItemList[cidx])->GetBuffer(), ((UITextItem *)item)->GetBuffer());
351 }
352
353 if (result < 0) {
354 // we haven't gotten to the string yet, keep as they are
355 cidx++;
356 } else if (result >= 0) {
357 // see if we need to insert, if not just keep copying
358 if (real_index == -1) {
359 // we need to insert here
360 real_index = cidx;
361 old_item = m_ItemList[cidx];
362 old_r2v = m_Real2Virt[cidx];
363 cidx++;
364 } else {
365 // old_item and old_r2v should be set!
366 ASSERT(old_item);
367 UIItem *oi;
368 int or2v;
369
370 or2v = m_Real2Virt[cidx];
371 oi = m_ItemList[cidx];
372
373 m_Real2Virt[cidx] = old_r2v;
374 m_ItemList[cidx] = old_item;
375 old_r2v = or2v;
376 old_item = oi;
377
378 cidx++;
379 }
380 }
381
382 // continue on
383 oidx++;
384 }
385
386 ASSERT(real_index != -1);
387
388 // now we need to fix m_Virt2Real to represent the new structure,
389 // all values>=real_index must be incremented
390 cidx = 0;
391 for (oidx = 0; oidx <= m_NumItems; oidx++) {
392 if (oidx != real_index) {
393 if (m_Virt2Real[cidx] >= real_index)
394 m_Virt2Real[cidx]++;
395 cidx++;
396 }
397 }
398 }
399 }
400
401 // copy item passed to list.
402 m_ItemList[real_index] = (UIItem *)item;
403 m_Virt2Real[m_NumItems] = real_index;
404 m_Real2Virt[real_index] = m_NumItems;
405 m_NumItems++;
406
407 if (real_index <= m_SelectedIndex) {
408 // adjust selected index
409 if (m_SelectedIndex < m_NumItems - 1)
410 m_SelectedIndex++;
411 }
412 }
413
414 // removes an item from the list.
415 void UIListBox::RemoveItem(const UIItem *item) {
416 int i, j, found = -1;
417
418 for (i = 0; i < m_NumItems; i++) {
419 if (item == m_ItemList[i]) {
420 found = i;
421 break;
422 }
423 }
424 if (found == -1) {
425 mprintf(0, "UIListBox:: Didn't find item to remove!\n");
426 return;
427 }
428
429 // found is the real index to remove
430 // go through virt2real and subtract one from values of found or higher
431 // go through real2virt and move everything from index found on down, and subtract
432 // 1 from those with value of found or higher
433 int real_index = found; // index to remove
434 int virt_index = m_Real2Virt[found]; // index to remove
435 int real_thresh = virt_index;
436 int virt_thresh = real_index;
437
438 for (j = 0; j < (m_NumItems - 1); j++) {
439 if (j >= real_index) {
440 m_ItemList[j] = m_ItemList[j + 1];
441 m_Real2Virt[j] = m_Real2Virt[j + 1];
442 }
443
444 if (j >= virt_index) {
445 m_Virt2Real[j] = m_Virt2Real[j + 1];
446 }
447
448 if (m_Real2Virt[j] >= real_thresh)
449 m_Real2Virt[j]--;
450
451 if (m_Virt2Real[j] >= virt_thresh)
452 m_Virt2Real[j]--;
453 }
454 m_NumItems--;
455
456 if (real_index <= m_SelectedIndex) {
457 // adjust selected index
458 if (m_SelectedIndex > 0)
459 m_SelectedIndex--;
460 }
461 }
462
463 // empties listbox
464 void UIListBox::RemoveAll() {
465 m_Index = m_SelectedIndex = m_NumItems = 0;
466 if (m_ItemList)
467 delete[] m_ItemList;
468 if (m_Virt2Real)
469 delete[] m_Virt2Real;
470 if (m_Real2Virt)
471 delete[] m_Real2Virt;
472 m_ItemList = NULL;
473 m_Virt2Real = NULL;
474 m_Real2Virt = NULL;
475 }
476
477 // selects the given item in the list.
478 void UIListBox::SelectItem(const UIItem *item) {
479 int i, found = 0;
480
481 for (i = 0; i < m_NumItems; i++) {
482 if (item == m_ItemList[i]) {
483 SetSelectedIndex(m_Real2Virt[i]);
484 found = 1;
485 break;
486 }
487 }
488 if (!found) {
489 mprintf(0, "UIListBox::SelectItem item not found!\n");
490 }
491 }
492
493 // returns the item at listbox index given.
494 UIItem *UIListBox::GetItem(int index) const {
495 if (m_NumItems == 0)
496 return NULL;
497
498 if (index >= 0 && index < m_NumItems) {
499 int real_index = m_Virt2Real[index];
500 ASSERT(real_index >= 0 && real_index < m_NumItems);
501 return m_ItemList[real_index];
502 } else
503 return NULL;
504 }
505
506 // returns the current listbox index
507 int UIListBox::GetListIndex() const {
508 if (m_NumItems == 0)
509 return 0;
510
511 if (m_Index < 0)
512 return m_Index;
513
514 if (!m_Real2Virt)
515 return 0;
516
517 int virt_index = m_Real2Virt[m_Index];
518 ASSERT(virt_index >= 0 && virt_index < m_NumItems);
519
520 return virt_index;
521 }
522
523 // sets the index of a listbox
524 void UIListBox::SetListIndex(int index) {
525 if (m_NumItems == 0)
526 return;
527
528 if (index >= 0 && index < m_NumItems) {
529 int real_index = m_Virt2Real[index];
530 ASSERT(real_index >= 0 && real_index < m_NumItems);
531 m_Index = real_index;
532 }
533 }
534
535 // returns which item index is selected
536 int UIListBox::GetSelectedIndex() const {
537 if (m_NumItems == 0)
538 return 0;
539
540 if (m_SelectedIndex < 0)
541 return m_SelectedIndex;
542
543 if (!m_Real2Virt)
544 return 0;
545
546 int virt_index = m_Real2Virt[m_SelectedIndex];
547 ASSERT(virt_index >= 0 && virt_index < m_NumItems);
548 return virt_index;
549 }
550
551 // sets the selected index.
552 void UIListBox::SetSelectedIndex(int index) {
553 if (m_NumItems == 0)
554 return;
555
556 if (index >= 0 && index < m_NumItems) {
557 int real_index = m_Virt2Real[index];
558 ASSERT(real_index >= 0 && real_index < m_NumItems);
559
560 int i, y = m_TextOffY;
561 m_SelectedIndex = real_index;
562 for (i = 0; i < m_NumItems; i++) {
563 y += m_ItemList[i]->height() + 2;
564 if (y > (m_H - m_TextOffY))
565 break;
566 }
567
568 m_Index = m_SelectedIndex - i;
569 if (m_Index < 0)
570 m_Index = 0;
571
572 if (selectchange_fn)
573 (*selectchange_fn)(index);
574 if (selectchange_id_fn)
575 (*selectchange_id_fn)(GetID(), m_callbackptr);
576 }
577 }
578
579 void UIListBox::SetInternalSelectedIndex(int index) {
580 if (m_NumItems == 0)
581 return;
582
583 if (index >= 0 && index < m_NumItems) {
584 int real_index = m_Virt2Real[index];
585 ASSERT(real_index >= 0 && real_index < m_NumItems);
586 m_SelectedIndex = real_index;
587
588 if (selectchange_fn)
589 (*selectchange_fn)(index);
590 if (selectchange_id_fn)
591 (*selectchange_id_fn)(GetID(), m_callbackptr);
592 }
593 }
594
595 void UIListBox::SetSelectChangeCallback(void (*fn)(int)) { selectchange_fn = fn; }
596
597 void UIListBox::SetSelectChangeCallback(void (*fn)(int, void *), void *ptr) {
598 selectchange_id_fn = fn;
599 m_callbackptr = ptr;
600 }
601
602 // called from outside gadget hierarchy.
603
604 void UIListBox::OnDestroy() {
605 UIGadget::OnDestroy();
606
607 if (m_ItemList)
608 delete[] m_ItemList;
609 if (m_Real2Virt)
610 delete[] m_Real2Virt;
611 if (m_Virt2Real)
612 delete[] m_Virt2Real;
613
614 m_ItemList = NULL;
615 m_Real2Virt = NULL;
616 m_Virt2Real = NULL;
617 }
618
619 // behavior when gadget is being drawn.
620 void UIListBox::OnDraw() {
621 bool use_scroll = !(m_Flags & UILB_NOSCROLL);
622 bool auto_select = ((m_Flags & UILB_AUTOSELECT) > 0);
623 UITextItem uarrow;
624 UITextItem darrow;
625 char arrowbuf[2];
626
627 if (m_Flags & UIF_BORDER)
628 ui_DrawBox(GR_WHITE, 0, 0, m_W, m_H);
629
630 ui_SetTextClip(m_CX, m_CY, m_CX2, m_CY2);
631
632 // draw all items in box that can be visible.
633 int x, y, i;
634 uint8_t alpha;
635
636 x = m_TextOffX;
637 y = m_TextOffY;
638
639 if (m_NumItems) {
640 snprintf(arrowbuf, sizeof(arrowbuf), "%c", UI_UP_ARROW_CHAR);
641 uarrow = UITextItem(arrowbuf, m_ItemList[m_Index]->get_color(), 255);
642 snprintf(arrowbuf, sizeof(arrowbuf), "%c", UI_DOWN_ARROW_CHAR);
643 darrow = UITextItem(arrowbuf, m_ItemList[m_Index]->get_color(), 255);
644
645 // reset these
646 m_ShowUp = m_ShowDown = false;
647
648 if (use_scroll) {
649 m_UpArrowY0 = y;
650 m_UpArrowY1 = y + uarrow.height();
651 if (m_Index > 0)
652 uarrow.draw(m_TextOffX, m_UpArrowY0, IsDisabled() ? uiDrawFaded : uiDrawNormal);
653 m_ShowUp = true;
654 m_ArrowWidth = uarrow.width() + 2;
655 x = x + m_ArrowWidth;
656 } else
657 m_ArrowWidth = 0;
658 }
659
660 alpha = m_Alpha;
661
662 for (i = m_Index; i < m_NumItems; i++) {
663 // check to see if scroll arrow down needs to be drawn.
664 // if (m_SelectedIndex == i) {
665 // ui_DrawSetAlpha((alpha/4)+1);
666 // ui_DrawRect(m_HiliteColor, x,y,m_W-m_TextOffX,y+m_ItemList[i]->height()+2);
667 // }
668
669 m_ItemList[i]->set_alpha((uint8_t)alpha);
670 if (m_SelectedIndex == i) {
671 ddgr_color old_color = m_ItemList[i]->get_color();
672 m_ItemList[i]->set_color(m_SelectColor);
673 m_ItemList[i]->draw(x + 1, y + 1, IsDisabled() ? uiDrawFaded : uiDrawNormal);
674 if (!IsDisabled())
675 m_ItemList[i]->draw(x + 1, y + 1, uiDrawAlphaSaturate);
676 m_ItemList[i]->set_color(old_color);
677 } else {
678 m_ItemList[i]->draw(x + 1, y + 1, IsDisabled() ? uiDrawFaded : uiDrawNormal);
679 }
680
681 if (use_scroll && (i < m_NumItems)) {
682 if ((y + m_ItemList[i]->height()) > (m_H - m_TextOffY - darrow.height() + 1)) {
683 if (i < (m_NumItems - 1)) {
684 m_DownArrowY0 = m_H - m_TextOffY - darrow.height() + 1;
685 m_DownArrowY1 = m_H - m_TextOffY;
686 darrow.draw(m_TextOffX, m_DownArrowY0, IsDisabled() ? uiDrawFaded : uiDrawNormal);
687 m_ShowDown = true;
688 }
689 break;
690 }
691 } else if ((y + m_ItemList[i]->height() + 2) > (m_H - m_TextOffY)) {
692 break;
693 }
694
695 y += m_ItemList[i]->height() + 2;
696 }
697
698 if (m_NumItems) {
699 if (use_scroll && auto_select) {
700 if (m_SelectedIndex < m_Index)
701 UIListBox::SetSelectedIndex(m_Real2Virt[m_Index]);
702 if (m_SelectedIndex > i)
703 UIListBox::SetSelectedIndex(m_Real2Virt[i]);
704 }
705 }
706
707 m_NumVisibleItems = i - m_Index;
708
709 ui_ResetTextClip();
710 }
711
712 // behavior when key is pressed.
713 void UIListBox::OnKeyDown(int key) {
714 UIGadget *gadget = this;
715
716 if (key == KEY_UP) {
717 if (m_SelectedIndex > 0) {
718 if (m_SelectedIndex == m_Index)
719 m_Index--;
720 m_SelectedIndex--;
721
722 UIListBox::SetInternalSelectedIndex((m_Real2Virt) ? m_Real2Virt[m_SelectedIndex] : 0);
723 }
724 } else if (key == KEY_DOWN) {
725 if (m_SelectedIndex < (m_NumItems - 1)) {
726 if (m_SelectedIndex >= (m_Index + m_NumVisibleItems))
727 m_Index++;
728 m_SelectedIndex++;
729 UIListBox::SetInternalSelectedIndex((m_Real2Virt) ? m_Real2Virt[m_SelectedIndex] : 0);
730 }
731 } else if (key == KEY_PAGEDOWN) {
732 if (m_Index < (m_NumItems - m_NumVisibleItems))
733 m_Index = m_Index + m_NumVisibleItems;
734 if (m_Index > (m_NumItems - m_NumVisibleItems))
735 m_Index = m_NumItems - m_NumVisibleItems;
736 m_SelectedIndex = m_Index;
737 UIListBox::SetInternalSelectedIndex((m_Real2Virt) ? m_Real2Virt[m_SelectedIndex] : 0);
738 } else if (key == KEY_PAGEUP) {
739 m_Index = m_Index - m_NumVisibleItems;
740 if (m_Index < 0)
741 m_Index = 0;
742 m_SelectedIndex = m_Index;
743
744 UIListBox::SetInternalSelectedIndex((m_Real2Virt) ? m_Real2Virt[m_SelectedIndex] : 0);
745 }
746 }
747
748 // behavior when key is released.
749 void UIListBox::OnKeyUp(int key) {}
750
751 // behavior when mouse button is pressed.
752 void UIListBox::OnMouseBtnDown(int btn) {
753 // One day, this system needs a complete rewrite, for all this stuff is a HACK!!!!
754 if (btn == UILMSEBTN) {
755 if (m_MouseState == 1 || m_MouseState == 2) { // single click pass or double clicking
756 // handle first slow delay scrolling (like keyboard input)
757 if ((m_ClickTime + KEYDOWN_FIRST_DELAY) > UI_TIME()) {
758 // mprintf(0, "old=%.3f cur=%.3f\n", m_ClickTime, UI_TIME());
759 return;
760 } else
761 m_MouseState = 3;
762 } else if (m_MouseState == 3) {
763 // handle repeat scrolling (note this is if the user has scrolled once already.
764 if ((m_ClickTime + KEYDOWN_REPEAT_DELAY) > UI_TIME()) {
765 // mprintf(0, "old=%.3f cur=%.3f\n", m_ClickTime, UI_TIME());
766 return;
767 }
768 } else if (m_MouseState == 0) {
769 m_MouseState = 1;
770 m_MouseX = SCREEN_TO_GAD_X(this, UI_input.mx);
771 m_MouseY = SCREEN_TO_GAD_Y(this, UI_input.my);
772
773 MseCheckSelect(m_MouseX, m_MouseY);
774
775 if (UI_TIME() < (m_ClickTime + UI_DBLCLICK_DELAY)) {
776 if (abs(m_LastMseX - m_MouseX) < UI_DBLCLICK_MSEDELTA && abs(m_LastMseY - m_MouseY) < UI_DBLCLICK_MSEDELTA) {
777
778 if (MseCheckSelect(m_MouseX, m_MouseY))
779 OnSelect();
780 m_MouseState = 2;
781 }
782 }
783 LOCK_FOCUS(this);
784 }
785
786 m_ClickTime = UI_TIME(); // use for scroll timing, this should be reset on button release
787 // mprintf(0, "new=%.3f\n", m_ClickTime);
788
789 // code here should allow for continuous scrolling on mouse down.
790 // we need to add timing so scrolling isn't do damn quick.
791 m_LastMseX = m_MouseX;
792 m_LastMseY = m_MouseY;
793
794 if (m_ShowUp && m_MouseY >= m_UpArrowY0 && m_MouseY <= m_UpArrowY1) {
795 if (m_MouseX < (m_TextOffX + m_ArrowWidth)) {
796 if (m_Index > 0)
797 m_Index--;
798 }
799 } else if (m_ShowDown && m_MouseY >= m_DownArrowY0 && m_MouseY <= m_DownArrowY1) {
800 if (m_MouseX < (m_TextOffX + m_ArrowWidth)) {
801 if (m_Index < (m_NumItems - m_NumVisibleItems))
802 m_Index++;
803 }
804 }
805 }
806 }
807
808 // behavior when mouse button is released.
809 void UIListBox::OnMouseBtnUp(int btn) {
810 if (btn == UILMSEBTN) {
811 if (m_MouseState == 0)
812 return;
813 if (m_MouseState == 2) // double clicked.
814 m_ClickTime = -2.0f;
815 else if (m_MouseState == 1 || m_MouseState == 3) // single clicked or prolonged single click.
816 m_ClickTime = UI_TIME();
817 m_MouseState = 0;
818 UNLOCK_FOCUS(this);
819 }
820 }
821
822 void UIListBox::OnSelect() { UIGadget::OnSelect(); }
823
824 void UIListBox::SetSelectedColor(ddgr_color col) { m_SelectColor = col; }
825
826 void UIListBox::SetHiliteColor(ddgr_color col) { m_HiliteColor = col; }
827
828 // override: behavior when gadget loses input focus.
829 void UIListBox::OnLostFocus() {
830 m_Alpha = 192;
831
832 UIGadget::OnLostFocus();
833 }
834
835 // override: behavior when gadget gains input focus.
836 void UIListBox::OnGainFocus() {
837 m_Alpha = 255;
838 UIGadget::OnGainFocus();
839 }
840
841 // override: behavior when gadget is processed
842 void UIListBox::OnUserProcess() {
843 // if mouse is outside listbox, unlock the focus on this listbox.
844 // if (this->IsLocked()) {
845 // if (!m_Wnd->HasKeyFocus() && !PT_IN_GADGET(UIGadget::m_Wnd, this, UI_input.mx, UI_input.my)) {
846 // UNLOCK_FOCUS(this);
847 // this->LostFocus();
848 // }
849 // }
850 }
851
852 // check if x and y selection hits a selected item.
853 bool UIListBox::MseCheckSelect(int x, int y) {
854 int i, ty;
855 bool use_scroll = !(m_Flags & UILB_NOSCROLL);
856
857 if (!m_NumItems)
858 return false;
859
860 if (m_ShowUp)
861 ty = m_UpArrowY0;
862 else
863 ty = m_TextOffY;
864
865 for (i = m_Index; i < m_NumItems; i++) {
866 // check if mouse down event occurred within this item.
867 if (y >= ty && y < (ty + m_ItemList[i]->height() + 2)) {
868 if (x > (m_ArrowWidth + m_TextOffX)) {
869 UIListBox::SetInternalSelectedIndex((m_Real2Virt) ? m_Real2Virt[i] : 0);
870 return true;
871 }
872 }
873
874 if (m_ShowDown && (ty + m_ItemList[i]->height()) > m_DownArrowY1)
875 return false;
876 else if ((ty + m_ItemList[i]->height()) > (m_H - m_TextOffY))
877 return false;
878
879 ty += m_ItemList[i]->height() + 2;
880 }
881
882 return false;
883 }