]> git.mxchange.org Git - flightgear.git/blob - src/GUI/puList.cxx
Clear chat messages when an aircraft becomes inactive in the property tree.
[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 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);
18 }
19
20
21 /**
22  * Static function: handle list entry clicks.
23  */
24 static void
25 handle_list_entry (puObject * listbox)
26 {
27     puListBox * box = (puListBox *)listbox->getUserData();
28     box->invokeCallback();
29 }
30
31
32 /**
33  * Static function: handle arrow clicks.
34  */
35 static void
36 handle_arrow (puObject * arrow)
37 {
38     puSlider * slider = (puSlider *)arrow->getUserData();
39     puListBox * list_box = (puListBox *)slider->getUserData();
40     puList * list = (puList *)list_box->getUserData();
41
42     int step;
43     switch (((puArrowButton *)arrow)->getArrowType()) {
44     case PUARROW_DOWN:
45         step = 1;
46         break;
47     case PUARROW_UP:
48         step = -1;
49         break;
50     default:
51         step = 0;
52         break;
53     }
54
55     int index = list->getTopItem();
56     list->setTopItem(index + step);
57     slider->setValue(1.0f - float(list->getTopItem()) / (list->getNumItems() - list->getNumVisible()));
58 }
59
60 puList::puList (int x, int y, int w, int h, int sl_width)
61     : puGroup(x, y),
62       _sw(sl_width)
63 {
64     type |= PUCLASS_LIST;
65     init(w, h, 1);
66 }
67
68 puList::puList (int x, int y, int w, int h, char ** contents, int sl_width)
69     : puGroup(x, y),
70       _sw(sl_width)
71 {
72     type |= PUCLASS_LIST;
73     init(w, h, 1);
74     newList(contents);
75 }
76
77 puList::~puList ()
78 {
79 }
80
81 void
82 puList::newList (char ** contents)
83 {
84     _list_box->newList(contents);
85     _contents = contents;
86
87     // new size calculation to consider slider visibility
88     setSize(_width, _height);
89 }
90
91 void
92 puList::setTopItem( int top )
93 {
94     int visible = _list_box->getNumVisible();
95     int num = _list_box->getNumItems();
96     if ( top < 0 || num <= visible )
97         top = 0 ;
98     else if ( num > 0 && top > num-visible )
99         top = num-visible;
100
101     _list_box->setTopItem(top);
102     top = _list_box->getTopItem();
103     // read clamped value back in, and only set slider if it doesn't match the new
104     // index to avoid jumps
105     int slider_index = int((1.0f - _slider->getFloatValue()) * (getNumItems() - getNumVisible()));
106     if (slider_index != top)
107         _slider->setValue(1.0f - float(getTopItem()) / (getNumItems() - getNumVisible()));
108 }
109
110 char *
111 puList::getListStringValue ()
112 {
113     int i = _list_box->getIntegerValue();
114     return i < 0 ? 0 : _contents[i];
115 }
116
117 int
118 puList::getListIntegerValue()
119 {
120   return _list_box->getIntegerValue();
121 }
122
123 void
124 puList::setColourScheme (float r, float g, float b, float a)
125 {
126     puObject::setColourScheme(r, g, b, a);
127     _list_box->setColourScheme(r, g, b, a);
128 }
129
130 void
131 puList::setColour (int which, float r, float g, float b, float a)
132 {
133     puObject::setColour(which, r, g, b, a);
134     _list_box->setColour(which, r, g, b, a);
135 }
136
137 void
138 puList::setSize (int w, int h)
139 {
140     _width = w;
141     _height = h;
142     puObject::setSize(w, h);
143     if (_frame)
144         _frame->setSize(w, h);
145
146     int total = _list_box->getNumItems();
147     int visible = _list_box->getNumVisible();
148
149     if (total > visible)
150     {
151         if (!_slider->isVisible())
152         {
153             _slider->setValue(1.0f);
154             _slider->reveal();
155             _up_arrow->reveal();
156             _down_arrow->reveal();
157         }
158         _list_box->setSize(w-_sw, h);
159
160         _slider->setPosition(w-_sw, _sw);
161         _slider->setSize(_sw, h-2*_sw);
162         _slider->setSliderFraction(float(visible) / total);
163
164         _down_arrow->setPosition(w-_sw, 0);
165         _up_arrow->setPosition(w-_sw, h-_sw);
166
167     }
168     else
169     {
170         if (_slider->isVisible())
171         {
172             _slider->hide();
173             _up_arrow->hide();
174             _down_arrow->hide();
175         }
176         _list_box->setSize(w, h);
177     }
178 }
179
180 void
181 puList::init (int w, int h, short transparent)
182 {
183     if ( transparent )
184         _frame = NULL ;
185     else
186         _frame = new puFrame(0, 0, w, h);
187
188     _list_box = new puListBox(0, 0, w-_sw, h);
189     _list_box->setStyle(-PUSTYLE_SMALL_SHADED);
190     _list_box->setUserData(this);
191     _list_box->setCallback(handle_list_entry);
192     _list_box->setValue(0);
193
194     _slider = new puSlider(w-_sw, _sw, h-2*_sw, true, _sw);
195     _slider->setValue(1.0f);
196     _slider->setUserData(_list_box);
197     _slider->setCallback(handle_slider);
198     _slider->setCBMode(PUSLIDER_ALWAYS);
199
200     _down_arrow = new puArrowButton(w-_sw, 0, w, _sw, PUARROW_DOWN) ;
201     _down_arrow->setUserData(_slider);
202     _down_arrow->setCallback(handle_arrow);
203
204     _up_arrow = new puArrowButton(w-_sw, h-_sw, w, h, PUARROW_UP);
205     _up_arrow->setUserData(_slider);
206     _up_arrow->setCallback(handle_arrow);
207
208     setSize(w, h);
209     close();
210 }
211
212 // end of puList.cxx