]> git.mxchange.org Git - flightgear.git/blob - src/GUI/puList.cxx
make puList actually useful: call back on list entry selection.
[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 list entry clicks.
22  */
23 static void
24 handle_list_entry (puObject * listbox)
25 {
26     puListBox * box = (puListBox *)listbox->getUserData();
27     box->invokeCallback();
28 }
29
30
31 /**
32  * Static function: handle arrow clicks.
33  */
34 static void
35 handle_arrow (puObject * arrow)
36 {
37     puSlider * slider = (puSlider *)arrow->getUserData();
38     puListBox * list_box = (puListBox *)slider->getUserData();
39
40     int step;
41     switch (((puArrowButton *)arrow)->getArrowType()) {
42     case PUARROW_DOWN:
43         step = 1;
44         break;
45     case PUARROW_UP:
46         step = -1;
47         break;
48     default:
49         step = 0;
50         break;
51     }
52
53     int index = list_box->getTopItem();
54     index += step;
55     if (index < 0)
56         index = 0;
57     else if (index >= list_box->getNumItems())
58         index = list_box->getNumItems() - 1;
59     list_box->setTopItem(index);
60
61     slider->setValue(1.0f - float(index)/list_box->getNumItems());
62 }
63
64 puList::puList (int x, int y, int w, int h)
65     : puGroup(x, y)
66 {
67     type |= PUCLASS_LIST;
68     init(w, h);
69 }
70
71 puList::puList (int x, int y, int w, int h, char ** contents)
72     : puGroup(x, y)
73 {
74     type |= PUCLASS_LIST;
75     init(w, h);
76     newList(contents);
77 }
78
79 puList::~puList ()
80 {
81 }
82
83 void
84 puList::newList (char ** contents)
85 {
86     _list_box->newList(contents);
87     _contents = contents;
88 }
89
90 char *
91 puList::getListStringValue ()
92 {
93     int i = _list_box->getIntegerValue();
94     return i < 0 ? 0 : _contents[i];
95 }
96
97 int
98 puList::getListIntegerValue()
99 {
100   return _list_box->getIntegerValue();
101 }
102
103 void
104 puList::init (int w, int h)
105 {
106     _frame = new puFrame(0, 0, w, h);
107
108     _list_box = new puListBox(0, 0, w-30, h);
109     _list_box->setStyle(-PUSTYLE_SMALL_SHADED);
110     _list_box->setUserData(this);
111     _list_box->setCallback(handle_list_entry);
112     _list_box->setValue(0);
113
114     _slider = new puSlider(w-30, 30, h-60, true);
115     _slider->setValue(1.0f);
116     _slider->setUserData(_list_box);
117     _slider->setCallback(handle_slider);
118     _slider->setCBMode(PUSLIDER_ALWAYS);
119
120     _down_arrow = new puArrowButton(w-30, 0, w, 30, PUARROW_DOWN) ;
121     _down_arrow->setUserData(_slider);
122     _down_arrow->setCallback(handle_arrow);
123
124     _up_arrow = new puArrowButton(w-30, h-30, w, h, PUARROW_UP);
125     _up_arrow->setUserData(_slider);
126     _up_arrow->setCallback(handle_arrow);
127
128     close();
129 }
130
131 void
132 puList::setColourScheme (float r, float g, float b, float a)
133 {
134     puObject::setColourScheme(r, g, b, a);
135     _list_box->setColourScheme(r, g, b, a);
136 }
137
138 void
139 puList::setColour (int which, float r, float g, float b, float a)
140 {
141     puObject::setColour(which, r, g, b, a);
142     _list_box->setColour(which, r, g, b, a);
143 }
144
145 // end of puList.cxx