1 // puList.cxx - implementation of a scrolling list box.
7 * Static function: handle slider movements.
10 handle_slider (puObject * slider)
12 puListBox * box = (puListBox *)slider->getUserData();
13 int total = box->getNumItems();
14 int visible = box->getNumVisible();
15 // negative numbers are OK -- setTopItem() clamps anyway
16 int index = int((total - visible) * (1.0 - slider->getFloatValue()));
17 box->setTopItem(index);
22 * Static function: handle list entry clicks.
25 handle_list_entry (puObject * listbox)
27 puListBox * box = (puListBox *)listbox->getUserData();
28 box->invokeCallback();
33 * Static function: handle arrow clicks.
36 handle_arrow (puObject * arrow)
38 puSlider * slider = (puSlider *)arrow->getUserData();
39 puListBox * list_box = (puListBox *)slider->getUserData();
42 switch (((puArrowButton *)arrow)->getArrowType()) {
54 int total = list_box->getNumItems();
55 int visible = list_box->getNumVisible();
56 int index = list_box->getTopItem();
57 list_box->setTopItem(index + step);
58 // read back to get setTopItem()'s clamping
59 index = list_box->getTopItem();
60 // negative numbers can't happen, as the buttons aren't visible then
61 slider->setValue(1.0f - float(index)/(total-visible));
64 puList::puList (int x, int y, int w, int h, int sl_width)
72 puList::puList (int x, int y, int w, int h, char ** contents, int sl_width)
86 puList::newList (char ** contents)
88 _list_box->newList(contents);
91 // new size calculation to consider slider visibility
92 setSize(_width, _height);
96 puList::getListStringValue ()
98 int i = _list_box->getIntegerValue();
99 return i < 0 ? 0 : _contents[i];
103 puList::getListIntegerValue()
105 return _list_box->getIntegerValue();
109 puList::init (int w, int h, short transparent)
114 _frame = new puFrame(0, 0, w, h);
116 _list_box = new puListBox(0, 0, w-_sw, h);
117 _list_box->setStyle(-PUSTYLE_SMALL_SHADED);
118 _list_box->setUserData(this);
119 _list_box->setCallback(handle_list_entry);
120 _list_box->setValue(0);
122 _slider = new puSlider(w-_sw, _sw, h-2*_sw, true, _sw);
123 _slider->setValue(1.0f);
124 _slider->setUserData(_list_box);
125 _slider->setCallback(handle_slider);
126 _slider->setCBMode(PUSLIDER_ALWAYS);
128 _down_arrow = new puArrowButton(w-_sw, 0, w, _sw, PUARROW_DOWN) ;
129 _down_arrow->setUserData(_slider);
130 _down_arrow->setCallback(handle_arrow);
132 _up_arrow = new puArrowButton(w-_sw, h-_sw, w, h, PUARROW_UP);
133 _up_arrow->setUserData(_slider);
134 _up_arrow->setCallback(handle_arrow);
141 puList::setColourScheme (float r, float g, float b, float a)
143 puObject::setColourScheme(r, g, b, a);
144 _list_box->setColourScheme(r, g, b, a);
148 puList::setColour (int which, float r, float g, float b, float a)
150 puObject::setColour(which, r, g, b, a);
151 _list_box->setColour(which, r, g, b, a);
155 puList::setTopItem( int top )
157 int visible = _list_box->getNumVisible();
158 int num = _list_box->getNumItems();
159 if ( top < 0 || num <= visible )
161 else if ( num > 0 && top > num-visible )
164 _list_box->setTopItem(top) ;
168 puList::setSize (int w, int h)
172 puObject::setSize(w, h);
174 _frame->setSize(w, h);
176 int total = _list_box->getNumItems();
177 int visible = _list_box->getNumVisible();
181 if (!_slider->isVisible())
183 _slider->setValue(1.0f);
186 _down_arrow->reveal();
188 _list_box->setSize(w-_sw, h);
190 _slider->setPosition(w-_sw, _sw);
191 _slider->setSize(_sw, h-2*_sw);
192 _slider->setSliderFraction(float(visible) / total);
194 _down_arrow->setPosition(w-_sw, 0);
195 _up_arrow->setPosition(w-_sw, h-_sw);
200 if (_slider->isVisible())
206 _list_box->setSize(w, h);