TORCS  1.3.10
The Open Racing Car Simulator
Loading...
Searching...
No Matches
guiscrollist.cpp
Go to the documentation of this file.
1/***************************************************************************
2
3 file : guiscrollist.cpp
4 created : Mon Aug 23 19:22:49 CEST 1999
5 copyright : (C) 1999-2026 by Eric EspiƩ, Bernhard Wymann
6 email : berniw@bluewin.ch
7
8 ***************************************************************************/
9
10/***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************/
18
23
24#include <stdlib.h>
25#include <stdio.h>
26#ifdef WIN32
27#include <windows.h>
28#endif
29#include <tgfclient.h>
30#include "gui.h"
31#include "guifont.h"
32#include <portability.h>
33
34void
36{
37}
38
39static void
41{
42 tGfuiObject *object;
43 tGfuiScrollList *scrollist;
44
45 object = (tGfuiObject *) sinfo->userData;
46 //object = gfuiGetObject(GfuiScreen, (long)(sinfo->userData));
47
48 if(object == NULL) {
49 return;
50 }
51
52 if(object->widget != GFUI_SCROLLIST) {
53 return;
54 }
55
56 scrollist = &(object->u.scrollist);
57
58 scrollist->firstVisible = sinfo->pos;
59
60 if((scrollist->selectedElt < scrollist->firstVisible) ||
61 (scrollist->selectedElt > scrollist->firstVisible + scrollist->nbVisible)) {
62 scrollist->selectedElt = -1;
63 }
64}
65
66void
68{
69 tGfuiScrollList *scrollist;
70
71 scrollist = &(object->u.scrollist);
72
73 scrollist->selectedElt++;
74
75 if(scrollist->selectedElt == scrollist->nbElts) {
76 scrollist->selectedElt = scrollist->nbElts - 1;
77 return;
78 }
79
80 gfuiRunAction(scrollist->onSelectAction);
81
82 if(scrollist->selectedElt == scrollist->firstVisible + scrollist->nbVisible) {
83 /* Scroll down */
84 if(scrollist->firstVisible + scrollist->nbVisible < scrollist->nbElts) {
85 scrollist->firstVisible++;
86
87 if(scrollist->scrollBar) {
88 GfuiScrollBarPosSet(GfuiScreen, scrollist->scrollBar, 0, MAX(scrollist->nbElts - scrollist->nbVisible, 0),
89 scrollist->nbVisible, scrollist->firstVisible);
90 }
91 }
92 }
93}
94
95void
97{
98 tGfuiScrollList *scrollist;
99
100 scrollist = &(object->u.scrollist);
101
102 scrollist->selectedElt--;
103
104 if(scrollist->selectedElt < 0) {
105 scrollist->selectedElt = 0;
106 return;
107 }
108
109 gfuiRunAction(scrollist->onSelectAction);
110
111 if(scrollist->selectedElt < scrollist->firstVisible) {
112 /* Scroll down */
113 if(scrollist->firstVisible > 0) {
114 scrollist->firstVisible--;
115
116 if(scrollist->scrollBar) {
117 GfuiScrollBarPosSet(GfuiScreen, scrollist->scrollBar, 0, MAX(scrollist->nbElts - scrollist->nbVisible, 0),
118 scrollist->nbVisible, scrollist->firstVisible);
119 }
120 }
121 }
122}
123
124
142int
143GfuiScrollListCreateAction(void *scr, int font, int x, int y, int align, int width, int height,
144 int scrollBarPos, const tGfuiAction &onSelect)
145{
146 tGfuiScrollList *scrollist;
147 tGfuiObject *object;
148 tGfuiScreen *screen = (tGfuiScreen *)scr;
149
150 object = (tGfuiObject *)calloc(1, sizeof(tGfuiObject));
151 object->widget = GFUI_SCROLLIST;
152 object->focusMode = GFUI_FOCUS_MOUSE_MOVE;
153 object->id = screen->curId++;
154 object->visible = 1;
155
156 object->xmin = x;
157 object->xmax = x + width;
158 object->ymin = y;
159 object->ymax = y + height;
160
161 scrollist = &(object->u.scrollist);
162 scrollist->fgColor[0] = &(GfuiColor[GFUI_FGSCROLLIST][0]);
163 scrollist->bgColor[0] = &(GfuiColor[GFUI_BGSCROLLIST][0]);
164 scrollist->fgSelectColor[0] = &(GfuiColor[GFUI_FGSELSCROLLIST][0]);
165 scrollist->bgSelectColor[0] = &(GfuiColor[GFUI_BGSELSCROLLIST][0]);
166 scrollist->font = gfuiFont[font];
167 scrollist->nbVisible = height / (scrollist->font->getDescender() + scrollist->font->getHeight());
168 scrollist->selectedElt = -1;
170
171 switch(scrollBarPos) {
172 case GFUI_SB_NONE:
173 break;
174 case GFUI_SB_RIGHT:
175 scrollist->scrollBar = GfuiScrollBarCreate(scr, x + width, y, GFUI_ALIGN_HL_VB, height, GFUI_VERT_SCROLLBAR,
176 0, 10, 10, 10, (void *)(object), gfuiScroll);
177 break;
178 case GFUI_SB_LEFT:
180 0, 10, 10, 10, (void *)(object), gfuiScroll);
181 break;
182 }
183
184 gfuiAddObject(screen, object);
185 return object->id;
186}
187
188int
189GfuiScrollListCreate(void *scr, int font, int x, int y, int align, int width, int height,
190 int scrollBarPos, void *userDataOnSelect, tfuiCallback onSelect)
191{
192 return GfuiScrollListCreateAction(scr, font, x, y, align, width, height, scrollBarPos,
193 gfuiMakeAction(userDataOnSelect, onSelect));
194}
195
196static void
198{
199 tGfuiListElement *cur;
200 int i;
201
202 if(scrollist->elts == NULL) {
203 scrollist->elts = elt;
204 elt->next = elt;
205 elt->prev = elt;
206 } else {
207 cur = scrollist->elts;
208 i = 0;
209
210 do {
211 if(i == index) {
212 break;
213 }
214
215 cur = cur->next;
216 i++;
217 } while(cur != scrollist->elts);
218
219 elt->next = cur->next;
220 cur->next = elt;
221 elt->prev = cur;
222 elt->next->prev = elt;
223
224 if((cur == scrollist->elts) && (index != 0)) {
225 scrollist->elts = elt;
226 }
227 }
228}
229
230static tGfuiListElement *
232{
233 tGfuiListElement *cur;
234 int i;
235
236 if(scrollist->elts == NULL) {
237 return (tGfuiListElement *)NULL;
238 }
239
240 cur = scrollist->elts;
241 i = 0;
242
243 do {
244 cur = cur->next;
245
246 if(i == index) {
247 break;
248 }
249
250 i++;
251 } while(cur != scrollist->elts);
252
253 cur->next->prev = cur->prev;
254 cur->prev->next = cur->next;
255
256 if(cur == scrollist->elts) {
257 if(cur->next == cur) {
258 scrollist->elts = (tGfuiListElement *)NULL;
259 } else {
260 scrollist->elts = cur->prev;
261 }
262 }
263
264 return cur;
265}
266
276char *
277GfuiScrollListGetSelectedElement(void *scr, int Id, void **userData)
278{
279 tGfuiObject *object;
280 tGfuiScrollList *scrollist;
281 tGfuiListElement *elt;
282 char *name;
283 int i;
284
285
286 object = gfuiGetObject(scr, Id);
287
288 if(object == NULL) {
289 return (char *)NULL;
290 }
291
292 if(object->widget != GFUI_SCROLLIST) {
293 return (char *)NULL;
294 }
295
296 scrollist = &(object->u.scrollist);
297
298 if(scrollist->selectedElt == -1) {
299 return (char *)NULL;
300 }
301
302 if(scrollist->elts == NULL) {
303 return (char *)NULL;
304 }
305
306 elt = scrollist->elts;
307 i = 0;
308
309 do {
310 elt = elt->next;
311
312 if(i == scrollist->selectedElt) {
313 break;
314 }
315
316 i++;
317 } while(elt != scrollist->elts);
318
319 name = elt->name;
320 *userData = elt->userData;
321
322 return name;
323}
324
335char *
336GfuiScrollListGetElement(void *scr, int Id, int index, void **userData)
337{
338 tGfuiObject *object;
339 tGfuiScrollList *scrollist;
340 tGfuiListElement *elt;
341 char *name;
342 int i;
343
344 object = gfuiGetObject(scr, Id);
345
346 if(object == NULL) {
347 return (char *)NULL;
348 }
349
350 if(object->widget != GFUI_SCROLLIST) {
351 return (char *)NULL;
352 }
353
354 scrollist = &(object->u.scrollist);
355
356 if((index < 0) || (index > scrollist->nbElts - 1)) {
357 return (char *)NULL;
358 }
359
360 if(scrollist->elts == NULL) {
361 return (char *)NULL;
362 }
363
364 elt = scrollist->elts;
365 i = 0;
366
367 do {
368 elt = elt->next;
369
370 if(i == index) {
371 break;
372 }
373
374 i++;
375 } while(elt != scrollist->elts);
376
377 name = elt->name;
378 *userData = elt->userData;
379
380 return name;
381}
382
393char *
394GfuiScrollListExtractSelectedElement(void *scr, int Id, void **userData)
395{
396 tGfuiObject *object;
397 tGfuiScrollList *scrollist;
398 tGfuiListElement *elt;
399 char *name;
400
401 object = gfuiGetObject(scr, Id);
402
403 if(object == NULL) {
404 return (char *)NULL;
405 }
406
407 if(object->widget != GFUI_SCROLLIST) {
408 return (char *)NULL;
409 }
410
411 scrollist = &(object->u.scrollist);
412
413 if(scrollist->selectedElt == -1) {
414 return (char *)NULL;
415 }
416
417 elt = gfuiScrollListRemElt(scrollist, scrollist->selectedElt);
418
419 scrollist->nbElts--;
420
421 if(scrollist->selectedElt > scrollist->nbElts - 1) {
422 scrollist->selectedElt--;
423 }
424
425 name = elt->name;
426 *userData = elt->userData;
427 free(elt);
428
429 return name;
430}
431
443char *
444GfuiScrollListExtractElement(void *scr, int Id, int index, void **userData)
445{
446 tGfuiObject *object;
447 tGfuiScrollList *scrollist;
448 tGfuiListElement *elt;
449 char *name;
450
451 object = gfuiGetObject(scr, Id);
452
453 if(object == NULL) {
454 return (char *)NULL;
455 }
456
457 if(object->widget != GFUI_SCROLLIST) {
458 return (char *)NULL;
459 }
460
461 scrollist = &(object->u.scrollist);
462
463 if((index < 0) || (index > scrollist->nbElts - 1)) {
464 return (char *)NULL;
465 }
466
467 elt = gfuiScrollListRemElt(scrollist, index);
468
469 scrollist->nbElts--;
470
471 if(scrollist->selectedElt > scrollist->nbElts - 1) {
472 scrollist->selectedElt--;
473 }
474
475 name = elt->name;
476 *userData = elt->userData;
477 free(elt);
478
479 return name;
480}
481
482
495int
496GfuiScrollListInsertElement(void *scr, int Id, char *element, int index, void *userData)
497{
498 tGfuiObject *object;
499 tGfuiScrollList *scrollist;
500 tGfuiListElement *elt;
501
502 object = gfuiGetObject(scr, Id);
503
504 if(object == NULL) {
505 return -1;
506 }
507
508 if(object->widget != GFUI_SCROLLIST) {
509 return -1;
510 }
511
512 scrollist = &(object->u.scrollist);
513
514 elt = (tGfuiListElement *)calloc(1, sizeof(tGfuiListElement));
515 elt->name = element;
516 elt->label = elt->name; /* TODO LENGTH !!!!!*/
517 elt->userData = userData;
518 elt->index = index;
519
520 gfuiScrollListInsElt(scrollist, elt, index);
521
522 scrollist->nbElts++;
523
524 if(scrollist->scrollBar) {
525 GfuiScrollBarPosSet(scr, scrollist->scrollBar, 0, MAX(scrollist->nbElts - scrollist->nbVisible, 0),
526 scrollist->nbVisible, scrollist->firstVisible);
527 }
528
529 return 0;
530}
531
532
533void
535{
536 tGfuiScrollList *scrollist;
537 tGfuiListElement *elt;
538 float *fgColor;
539 float *bgColor;
540 const int BUFSIZE = 256;
541 char buf[BUFSIZE];
542 int w, h, x, y;
543 int index;
544
545 scrollist = &(obj->u.scrollist);
546
547 fgColor = scrollist->fgColor[0];
548 bgColor = scrollist->bgColor[0];
549
550 if(bgColor[3] != 0.0) {
551 glBegin(GL_QUADS);
552 glColor4fv(bgColor);
553 glVertex2i(obj->xmin, obj->ymin);
554 glVertex2i(obj->xmin, obj->ymax);
555 glVertex2i(obj->xmax, obj->ymax);
556 glVertex2i(obj->xmax, obj->ymin);
557 glEnd();
558 }
559
560 glBegin(GL_LINE_STRIP);
561 glColor4fv(fgColor);
562 glVertex2i(obj->xmin, obj->ymin);
563 glVertex2i(obj->xmin, obj->ymax);
564 glVertex2i(obj->xmax, obj->ymax);
565 glVertex2i(obj->xmax, obj->ymin);
566 glVertex2i(obj->xmin, obj->ymin);
567 glEnd();
568
569
570 h = scrollist->font->getDescender() + scrollist->font->getHeight();
571 x = obj->xmin;
572 y = obj->ymax;
573 index = 0;
574 elt = scrollist->elts;
575
576 if(elt != NULL) {
577 if(scrollist->nbElts < 100) {
578 snprintf(buf, BUFSIZE, " 00 ");
579 } else {
580 snprintf(buf, BUFSIZE, " 000 ");
581 }
582
583 w = scrollist->font->getWidth((const char *)buf);
584
585 do {
586 elt = elt->next;
587
588 if(index < scrollist->firstVisible) {
589 index++;
590 continue;
591 }
592
593 if(index == scrollist->selectedElt) {
594 glColor4fv(scrollist->fgSelectColor[0]);
595 } else {
596 glColor4fv(scrollist->fgColor[0]);
597 }
598
599 index++;
600
601 if(index > (scrollist->firstVisible + scrollist->nbVisible)) {
602 break;
603 }
604
605 y -= h;
606 snprintf(buf, BUFSIZE, " %d", index);
607 gfuiPrintString(x, y, scrollist->font, buf);
608 gfuiPrintString(x + w, y, scrollist->font, elt->label);
609 } while(elt != scrollist->elts);
610 }
611
612
613}
614
615void
617{
618 tGfuiObject *curObject;
619
620 curObject = GfuiScreen->objects;
621
622 if(curObject != NULL) {
623 do {
624 curObject = curObject->next;
625
626 if(curObject->widget == GFUI_SCROLLIST) {
627 curObject->u.scrollist.selectedElt = -1;
628 }
629 } while(curObject != GfuiScreen->objects);
630 }
631
632}
633
634
635void
637{
638 tGfuiObject *object;
639 tGfuiScrollList *scrollist;
640 int relY;
641
642 if(mouse == 0) {
643 /* button down */
645 object = GfuiScreen->hasFocus;
646 scrollist = &(object->u.scrollist);
647 relY = object->ymax - GfuiMouse.Y;
648 relY = scrollist->firstVisible + relY / (scrollist->font->getDescender() + scrollist->font->getHeight()) + 1;
649
650 if(relY > scrollist->nbElts) {
651 scrollist->selectedElt = -1;
652 return;
653 }
654
655 scrollist->selectedElt = relY - 1;
656
657 gfuiRunAction(scrollist->onSelectAction);
658 }
659}
660
669int
670GfuiScrollListMoveSelectedElement(void *scr, int Id, int delta)
671{
672 tGfuiObject *object;
673 tGfuiScrollList *scrollist;
674 int newPos;
675 tGfuiListElement *elt;
676
677 object = gfuiGetObject(scr, Id);
678
679 if(object == NULL) {
680 return -1;
681 }
682
683 if(object->widget != GFUI_SCROLLIST) {
684 return -1;
685 }
686
687 scrollist = &(object->u.scrollist);
688
689 if(scrollist->selectedElt == -1) {
690 return -1;
691 }
692
693 newPos = scrollist->selectedElt + delta;
694
695 if((newPos < 0) || (newPos > scrollist->nbElts - 1)) {
696 return -1;
697 }
698
699 elt = gfuiScrollListRemElt(scrollist, scrollist->selectedElt);
700
701 gfuiScrollListInsElt(scrollist, elt, newPos);
702
703 scrollist->selectedElt = newPos;
704
705 if(scrollist->selectedElt == scrollist->firstVisible + scrollist->nbVisible) {
706 /* Scroll down */
707 if(scrollist->firstVisible + scrollist->nbVisible < scrollist->nbElts) {
708 scrollist->firstVisible++;
709
710 if(scrollist->scrollBar) {
711 GfuiScrollBarPosSet(GfuiScreen, scrollist->scrollBar, 0, MAX(scrollist->nbElts - scrollist->nbVisible, 0),
712 scrollist->nbVisible, scrollist->firstVisible);
713 }
714 }
715 } else if(scrollist->selectedElt < scrollist->firstVisible) {
716 /* Scroll down */
717 if(scrollist->firstVisible > 0) {
718 scrollist->firstVisible--;
719
720 if(scrollist->scrollBar) {
721 GfuiScrollBarPosSet(GfuiScreen, scrollist->scrollBar, 0, MAX(scrollist->nbElts - scrollist->nbVisible, 0),
722 scrollist->nbVisible, scrollist->firstVisible);
723 }
724 }
725 }
726
727 return 0;
728}
729
730
731void
733{
734 tGfuiScrollList *scrollist;
735 tGfuiListElement *elt;
736
737 scrollist = &(curObject->u.scrollist);
739
740 while((elt = gfuiScrollListRemElt(scrollist, 0)) != NULL) {
741 free(elt);
742 }
743
744 free(curObject);
745}
static Vector y[4]
Definition Convex.cpp:56
int getDescender() const
Definition guifont.cpp:272
int getWidth(const char *text)
Definition guifont.cpp:222
int getHeight() const
Definition guifont.cpp:261
static void onSelect(void *)
int GfuiScrollBarCreate(void *scr, int x, int y, int align, int width, int orientation, int min, int max, int len, int start, void *userData, tfuiSBCallback onScroll)
Create a new scroll bar.
int GfuiScrollListCreateAction(void *scr, int font, int x, int y, int align, int width, int height, int scrollBarPos, const tGfuiAction &onSelect)
Create a new scroll list.
int GfuiScrollListInsertElement(void *scr, int Id, char *element, int index, void *userData)
Insert an element in a scroll list.
int GfuiScrollListCreate(void *scr, int font, int x, int y, int align, int width, int height, int scrollBarPos, void *userDataOnSelect, tfuiCallback onSelect)
Create a scrolling list using a callback with caller-supplied data.
char * GfuiScrollListExtractElement(void *scr, int Id, int index, void **userData)
Extract the specified element from the scroll list.
char * GfuiScrollListGetSelectedElement(void *scr, int Id, void **userData)
Get the selected element from the scroll list.
char * GfuiScrollListExtractSelectedElement(void *scr, int Id, void **userData)
Extract the selected element from the scroll list (removed).
char * GfuiScrollListGetElement(void *scr, int Id, int index, void **userData)
Get the specified element from the scroll list.
void GfuiScrollBarPosSet(void *scr, int id, int min, int max, int len, int start)
Set new values for position.
int GfuiScrollListMoveSelectedElement(void *scr, int Id, int delta)
Move the selected element within the scroll list.
tGfuiScreen * GfuiScreen
Definition gui.cpp:32
void gfuiReleaseAction(tGfuiAction *action)
Definition gui.cpp:79
tGfuiAction gfuiMakeAction(void *userData, tfuiCallback callback)
Definition gui.cpp:48
float GfuiColor[GFUI_COLORNB][4]
Definition gui.cpp:38
tGfuiAction * gfuiCreateAction(const tGfuiAction &action)
Definition gui.cpp:60
void gfuiRunAction(const tGfuiAction *action)
Definition gui.cpp:70
tMouseInfo GfuiMouse
Definition gui.cpp:34
#define GFUI_BGSCROLLIST
Definition gui.h:43
#define GFUI_FGSELSCROLLIST
Definition gui.h:46
void gfuiPrintString(int x, int y, GfuiFontClass *font, const char *string)
Definition guiobject.cpp:32
void gfuiAddObject(tGfuiScreen *screen, tGfuiObject *object)
struct GfuiObject tGfuiObject
#define GFUI_FGSCROLLIST
Definition gui.h:44
struct GfuiListElement tGfuiListElement
tGfuiObject * gfuiGetObject(void *scr, int id)
#define GFUI_FOCUS_MOUSE_MOVE
Definition gui.h:129
#define GFUI_BGSELSCROLLIST
Definition gui.h:45
GfuiFontClass * gfuiFont[FONT_NB]
Definition guifont.cpp:43
static void gfuiScroll(tScrollBarInfo *sinfo)
void gfuiScrollListNextElt(tGfuiObject *object)
void gfuiScrollListDeselectAll(void)
void gfuiScrollListPrevElt(tGfuiObject *object)
void gfuiScrollListAction(int mouse)
void gfuiReleaseScrollist(tGfuiObject *curObject)
void gfuiDrawScrollist(tGfuiObject *obj)
static tGfuiListElement * gfuiScrollListRemElt(tGfuiScrollList *scrollist, int index)
static void gfuiScrollListInsElt(tGfuiScrollList *scrollist, tGfuiListElement *elt, int index)
void gfuiScrListInit(void)
char * name
Definition gui.h:134
void * userData
Definition gui.h:136
char * label
Definition gui.h:135
struct GfuiListElement * prev
Definition gui.h:140
struct GfuiListElement * next
Definition gui.h:139
tGfuiScrollList scrollist
Definition gui.h:206
int ymin
Definition gui.h:199
int xmin
Definition gui.h:199
int xmax
Definition gui.h:200
int ymax
Definition gui.h:200
union GfuiObject::@107216344126174123112226307372375007210201317144 u
int widget
Definition gui.h:193
struct GfuiObject * next
Definition gui.h:211
void * userData
Associated user data.
Definition tgfclient.h:104
int pos
Current scroll bar position.
Definition tgfclient.h:103
int curId
Definition gui.h:238
float * bgColor[3]
Definition gui.h:146
int firstVisible
Definition gui.h:153
tGfuiAction * onSelectAction
Definition gui.h:157
tGfuiListElement * elts
Definition gui.h:151
GfuiFontClass * font
Definition gui.h:150
float * fgSelectColor[3]
Definition gui.h:149
int scrollBar
Definition gui.h:156
float * fgColor[3]
Definition gui.h:147
int nbVisible
Definition gui.h:154
int selectedElt
Definition gui.h:155
float * bgSelectColor[3]
Definition gui.h:148
#define MAX(x, y)
Definition tgf.h:52
The Gaming Framework API (client part).
void(* tfuiCallback)(void *)
Definition tgfclient.h:108
struct ScrollBarInfo tScrollBarInfo
Scroll bar call-back information.
#define GFUI_SB_NONE
Definition tgfclient.h:90
#define GFUI_SCROLLIST
Definition tgfclient.h:66
#define GFUI_ALIGN_HL_VB
Definition tgfclient.h:71
std::function< void()> tGfuiAction
Definition tgfclient.h:107
#define GFUI_ALIGN_HR_VB
Definition tgfclient.h:77
#define GFUI_SB_LEFT
Definition tgfclient.h:92
#define GFUI_VERT_SCROLLBAR
Definition tgfclient.h:98
#define GFUI_SB_RIGHT
Definition tgfclient.h:91