]> git.mxchange.org Git - flightgear.git/blob - src/GUI/puList.cxx
puList.cxx: _list_box->getIntegerValue() returns -1 as long as no list
[flightgear.git] / src / GUI / puList.cxx
1 // puList.cxx - implementation of a scrolling list box.
2
3 #include "puList.hxx"
4
5
6 /**
7  * Static function: handle slider movements.
8  */
9 static void
10 handle_slider (puObject * slider)
11 {
12     puListBox * box = (puListBox *)slider->getUserData();
13     int index = int(box->getNumItems() * (1.0 - slider->getFloatValue()));
14     if (index >= box->getNumItems())
15         index = box->getNumItems() - 1;
16     box->setTopItem(index);
17 }
18
19
20 /**
21  * Static function: handle arrow clicks.
22  */
23 static void
24 handle_arrow (puObject * arrow)
25 {
26     puSlider * slider = (puSlider *)arrow->getUserData();
27     puListBox * list_box = (puListBox *)slider->getUserData();
28
29     int step;
30     switch (((puArrowButton *)arrow)->getArrowType()) {
31     case PUARROW_DOWN:
32         step = 1;
33         break;
34     case PUARROW_UP:
35         step = -1;
36         break;
37     default:
38         step = 0;
39         break;
40     }
41
42     int index = list_box->getTopItem();
43     index += step;
44     if (index < 0)
45         index = 0;
46     else if (index >= list_box->getNumItems())
47         index = list_box->getNumItems() - 1;
48     list_box->setTopItem(index);
49
50     slider->setValue(1.0f - float(index)/list_box->getNumItems());
51 }
52
53 puList::puList (int x, int y, int w, int h)
54     : puGroup(x, y)
55 {
56     type |= PUCLASS_LIST;
57     init(w, h);
58 }
59
60 puList::puList (int x, int y, int w, int h, char ** contents)
61     : puGroup(x, y)
62 {
63     type |= PUCLASS_LIST;
64     init(w, h);
65     newList(contents);
66 }
67
68 puList::~puList ()
69 {
70 }
71
72 void
73 puList::newList (char ** contents)
74 {
75     _list_box->newList(contents);
76     _contents = contents;
77 }
78
79 char *
80 puList::getListStringValue ()
81 {
82     int i = _list_box->getIntegerValue();
83     return i < 0 ? 0 : _contents[i];
84 }
85
86 int
87 puList::getListIntegerValue()
88 {
89   return _list_box->getIntegerValue();
90 }
91
92 void
93 puList::init (int w, int h)
94 {
95     _frame = new puFrame(0, 0, w, h);
96
97     _list_box = new puListBox(0, 0, w-30, h);
98     _list_box->setStyle(-PUSTYLE_SMALL_SHADED);
99     _list_box->setUserData(this);
100     _list_box->setValue(0);
101
102     _slider = new puSlider(w-30, 30, h-60, true);
103     _slider->setValue(1.0f);
104     _slider->setUserData(_list_box);
105     _slider->setCallback(handle_slider);
106     _slider->setCBMode(PUSLIDER_ALWAYS);
107
108     _down_arrow = new puArrowButton(w-30, 0, w, 30, PUARROW_DOWN) ;
109     _down_arrow->setUserData(_slider);
110     _down_arrow->setCallback(handle_arrow);
111
112     _up_arrow = new puArrowButton(w-30, h-30, w, h, PUARROW_UP);
113     _up_arrow->setUserData(_slider);
114     _up_arrow->setCallback(handle_arrow);
115
116     close();
117 }
118
119 // end of puList.cxx