码迷,mamicode.com
首页 > 其他好文 > 详细

ListBox

时间:2014-12-18 11:34:51      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   ar   io   color   os   sp   for   

  1 //ListBox.cpp
  2 
  3 /*
  4 Copyright 2000-2004 The VCF Project.
  5 Please see License.txt in the top level directory
  6 where you installed the VCF.
  7 */
  8 
  9 
 10 #include "vcf/ApplicationKit/ApplicationKit.h"
 11 #include "vcf/ApplicationKit/ControlsKit.h"
 12 
 13 
 14 using namespace VCF;
 15 
 16 
 17 /**
 18 *This example demonstrates how the different selection styles of a 
 19 *ListBoxControl function. The three styles include single selection,
 20 *when only 1 item can be selected at a time, multiselection, when 
 21 *more than 1 item can be selected at a time, and extended selection.
 22 *Extended selection is similar to multiselection, but can handle Ctrl+MouseDown
 23 *and Shift+MouseDown selection. Experiment. 
 24 */
 25 
 26 class ListBoxWindow : public Window {
 27 public:
 28     ListBoxWindow() {
 29         setCaption( "ListBox" );
 30 
 31         this->setUseColorForBackground( true );
 32     
 33         this->setWidth(600);
 34         this->setHeight(600);
 35         
 36         double labelHeight = 40.0;
 37         double listboxWidth = 160.0;
 38         double listboxHeight = 300.0;
 39         double horizSpace = 20.0;
 40         double vertSpace = 10.0;
 41     
 42         // construct labels
 43         Label* label1 = new Label();
 44         label1->setBounds(horizSpace, 10, listboxWidth, labelHeight);
 45         label1->setWordWrap( true );
 46         label1->setCaption( L"allowsMultiSelect_ = false" );
 47         label1->getFont()->setBold(false);    
 48     
 49         Label* label2 = new Label();
 50         label2->setBounds(listboxWidth + 2*horizSpace, 10, listboxWidth, labelHeight);
 51         label2->setWordWrap( true );
 52         label2->setCaption( L"allowsMultiSelect_ = true\nallowsExtendedSelect_ = false" );    
 53         label2->getFont()->setBold(false);    
 54     
 55         Label* label3 = new Label();
 56         label3->setBounds(2*listboxWidth + 3*horizSpace, 10, listboxWidth, labelHeight);
 57         label3->setWordWrap( true );
 58         label3->setCaption( L"allowsMultiSelect_ = true\nallowsExtendedSelect_ = true" );
 59         label3->getFont()->setBold(false);    
 60 
 61     
 62         //build ListBoxes with a border
 63         ListBoxControl* listBoxSingle = new ListBoxControl();
 64         listBoxSingle->setName( L"listBoxSingle" );
 65         listBoxSingle->setUseColorForBackground( true );
 66 
 67         /**
 68         This sets the selection style to single selection (allowsMultiSelect_ = false).
 69         */
 70         listBoxSingle->setAllowsMultiSelect( false );
 71 
 72         ImageList* imgList = new ImageList(this);
 73         GraphicsResourceBundle* bundle = Application::getRunningInstance()->getResourceBundle();
 74         imgList->setImageHeight( 16 );
 75         imgList->setImageWidth( 16 );
 76         imgList->setTransparentColor( &Color(0.0f, 1.0f, 0.0f) );
 77         Image* img = bundle->getImage("plain.bmp");
 78         imgList->addImage( img );
 79         delete img;
 80 
 81         img = bundle->getImage("ie.bmp");
 82         imgList->addImage( img );
 83         delete img;
 84 
 85         img = bundle->getImage("access.bmp");
 86         imgList->addImage( img );
 87         delete img;
 88 
 89         img = bundle->getImage("excel.bmp");
 90         imgList->addImage( img );
 91         delete img;
 92 
 93         img = bundle->getImage("fp.bmp");
 94         imgList->addImage( img );
 95         delete img;
 96 
 97         img = bundle->getImage("powerpt.bmp");
 98         imgList->addImage( img );
 99         delete img;
100 
101         img = bundle->getImage("word.bmp");
102         imgList->addImage( img );
103         delete img;
104 
105         /**
106         This sets the selection style to single selection. We really
107         don‘t need to set this now, because if allowsMultiSelect_ is false,
108         the setting for allowsExtendedSelect_ has no effect.
109         */
110         listBoxSingle->setAllowsExtendedSelect( false );
111 
112         listBoxSingle->setSmallImageList( imgList );
113 
114         ListBoxControl* listBoxMulti = new ListBoxControl();
115         listBoxMulti->setName( L"listBoxMulti" );
116         listBoxMulti->setUseColorForBackground( true );
117 
118         /**
119         This sets the selection style to multi selection (allowsMultiSelect_ = true).
120         */
121         listBoxMulti->setAllowsMultiSelect( true );
122 
123         /**
124         This sets the selection style to not allow extended selection. It is 
125         important to make this call if you DO NOT want extended selection AND
126         allowsMultiSelect_ = true, because the default setting for allowsExtendedSelect_ is true.
127         This may change in the future.
128         */
129         listBoxMulti->setAllowsExtendedSelect( false );
130 
131 
132         ListBoxControl* listBoxExtended = new ListBoxControl();
133         listBoxExtended->setName( L"listBoxExtended" );
134         listBoxExtended->setUseColorForBackground( true );
135         listBoxExtended->setAllowsMultiSelect( true );
136 
137         /**
138         This sets the selection style to allow extended selection.
139         Technically we do not have to set this here if 
140         allowsMultiSelect_ is true, because the default setting for
141         allowsExtendedSelect_ is true (this might change in the future, as
142         it is a little counter-intuitive).
143         */
144         listBoxExtended->setAllowsExtendedSelect( true );
145 
146         listBoxSingle->setBounds(horizSpace, labelHeight + vertSpace, listboxWidth, listboxHeight);
147         listBoxMulti->setBounds(listboxWidth + 2*horizSpace, labelHeight + vertSpace, listboxWidth, listboxHeight);
148         listBoxExtended->setBounds(2*listboxWidth + 3*horizSpace, labelHeight + vertSpace, listboxWidth, listboxHeight);
149     
150         //add borders to list boxes
151         Basic3DBorder* listBoxSingleBrdr = new Basic3DBorder();
152         listBoxSingleBrdr->setInverted( true );
153         listBoxSingle->setBorder(listBoxSingleBrdr);
154 
155         Basic3DBorder* listBoxMultiBrdr = new Basic3DBorder();
156         listBoxMultiBrdr->setInverted( true );
157         listBoxMulti->setBorder(listBoxMultiBrdr);
158 
159         Basic3DBorder* listBoxExtendedBrdr = new Basic3DBorder();
160         listBoxExtendedBrdr->setInverted( true );
161         listBoxExtended->setBorder(listBoxExtendedBrdr);
162 
163         //add scrollbars to list boxes
164         ScrollbarManager* scrollbarManagerSingle = new ScrollbarManager();
165         addComponent( scrollbarManagerSingle );
166         scrollbarManagerSingle->setHasVerticalScrollbar( true );
167         scrollbarManagerSingle->setHasHorizontalScrollbar( true );        
168         scrollbarManagerSingle->setTarget( listBoxSingle );
169 
170         ScrollbarManager* scrollbarManagerMulti = new ScrollbarManager();
171         addComponent( scrollbarManagerMulti );
172         scrollbarManagerMulti->setHasVerticalScrollbar( true );
173         scrollbarManagerMulti->setHasHorizontalScrollbar( true );
174         scrollbarManagerMulti->setTarget( listBoxMulti );
175 
176         ScrollbarManager* scrollbarManagerExtended = new ScrollbarManager();
177         addComponent( scrollbarManagerExtended );
178         scrollbarManagerExtended->setHasVerticalScrollbar( true );
179         scrollbarManagerExtended->setHasHorizontalScrollbar( true );
180         scrollbarManagerExtended->setTarget( listBoxExtended );
181 
182         //add TextControl to display # of selected items
183         TextControl* textCtrlSingle = new TextControl();
184         textCtrlSingle->setName( L"textCtrlSingle" );
185         TextControl* textCtrlMulti = new TextControl();
186         textCtrlMulti->setName( L"textCtrlMulti" );
187         TextControl* textCtrlExtended = new TextControl();
188         textCtrlExtended->setName( L"textCtrlExtended" );
189 
190         textCtrlSingle->setBounds(horizSpace, labelHeight + 2*vertSpace + listboxHeight, listboxWidth, 20.0);
191         textCtrlMulti->setBounds(2*horizSpace + listboxWidth, labelHeight + 2*vertSpace + listboxHeight, listboxWidth, 20.0);
192         textCtrlExtended->setBounds(3*horizSpace + 2*listboxWidth, labelHeight + 2*vertSpace + listboxHeight, listboxWidth, 20.0);
193 
194         // Populate list boxes with some dummy list items.
195         ListModel* singleModel = listBoxSingle->getListModel();
196         ListModel* multiModel = listBoxMulti->getListModel();
197         ListModel* extendedModel = listBoxExtended->getListModel();
198 
199         for(int j=0; j<60; j++){
200             String indx = StringUtils::toString(j);
201             String capt = L"DefaultListItem " + indx;
202         
203             multiModel->add( capt ); 
204             extendedModel->add( capt );
205 
206             //singleModel->add( capt );
207             ListItem* item = listBoxSingle->addItem( capt, j / 9 );            
208             if ( j%9 == 0 ) {
209                 item->setDisplayState( idsChecked );
210             }
211         }
212 
213         //add buttons for deselecting all selected items
214         CommandButton* btnSingle = new CommandButton();
215         CommandButton* btnMulti = new CommandButton();
216         CommandButton* btnExtended = new CommandButton();
217     
218         btnSingle->setBounds( horizSpace, labelHeight + 3*vertSpace + listboxHeight + 20, listboxWidth, 25.0 );
219         btnMulti->setBounds( 2*horizSpace + listboxWidth, labelHeight + 3*vertSpace + listboxHeight + 20, listboxWidth, 25.0 );
220         btnExtended->setBounds( 3*horizSpace + 2*listboxWidth, labelHeight + 3*vertSpace + listboxHeight + 20, listboxWidth, 25.0 );        
221     
222         btnSingle->setCaption( "Deselect All" );
223         btnMulti->setCaption( "Deselect All" );
224         btnExtended->setCaption( "Deselect All" );
225     
226 
227         //add Controls to MainWindow
228         this->setWidth(3*listboxWidth+4*horizSpace + 8);
229         
230         this->add(label1);
231         this->add(label2);
232         this->add(label3);
233 
234         this->add(listBoxSingle);
235         this->add(listBoxMulti);
236         listBoxExtended->setAnchor( AnchorLeft | AnchorRight );
237         this->add(listBoxExtended);
238 
239         this->add(textCtrlSingle);
240         this->add(textCtrlMulti);
241         textCtrlExtended->setAnchor( AnchorLeft | AnchorRight );
242         this->add(textCtrlExtended);
243 
244         this->add(btnSingle);
245         this->add(btnMulti);
246         btnExtended->setAnchor( AnchorLeft | AnchorRight );
247         this->add(btnExtended);
248     
249         //add SelectionChanged handlers for each listbox.
250         listBoxSingle->ItemSelectionChanged.add( new ClassProcedure1<ItemEvent*,ListBoxWindow>(this, &ListBoxWindow::getListBoxSingleInfo, "ListBoxWindow::getListBoxSingleInfo"));
251         listBoxMulti->ItemSelectionChanged.add( new ClassProcedure1<ItemEvent*,ListBoxWindow>(this, &ListBoxWindow::getListBoxMultiInfo, "ListBoxWindow::getListBoxMultiInfo"));
252         listBoxExtended->ItemSelectionChanged.add( new ClassProcedure1<ItemEvent*,ListBoxWindow>(this, &ListBoxWindow::getListBoxExtendedInfo, "ListBoxWindow::getListBoxExtendedInfo"));
253     
254         //add ButtonClicked handlers for each command button.    
255         btnSingle->ButtonClicked.add( new ClassProcedure1<ButtonEvent*,ListBoxWindow>( this, &ListBoxWindow::onbtnSingleClicked, "ListBoxWindow::onbtnSingleClicked" ) );
256         btnMulti->ButtonClicked.add( new ClassProcedure1<ButtonEvent*,ListBoxWindow>( this, &ListBoxWindow::onbtnMultiClicked, "ListBoxWindow::onbtnMultiClicked" ) );
257         btnExtended->ButtonClicked.add( new ClassProcedure1<ButtonEvent*,ListBoxWindow>( this, &ListBoxWindow::onbtnExtendedClicked, "ListBoxWindow::onbtnExtendedClicked" ) );
258 
259     }
260 
261     virtual ~ListBoxWindow(){};
262 
263 
264     void getListBoxSingleInfo( VCF::ItemEvent* );
265     void getListBoxMultiInfo( VCF::ItemEvent* );
266     void getListBoxExtendedInfo( VCF::ItemEvent* );
267 
268     void getListBoxInfo(VCF::ListBoxControl* LBC, VCF::TextControl* txtCtrl);
269 
270     void onbtnSingleClicked( VCF::ButtonEvent* );
271     void onbtnMultiClicked( VCF::ButtonEvent* );
272     void onbtnExtendedClicked( VCF::ButtonEvent* );
273 
274 
275 };
276 
277 /** This is our main function that calculates the # of selected items in 
278 a list box and displays the # in a corresponding text control. This is called
279 by each of the list box control and button handlers that we added. Those handlers
280 find the correct MyListBox and CommandButton instances and pass pointers to them to
281 this function.
282 */
283 void ListBoxWindow::getListBoxInfo(ListBoxControl* LBC, TextControl* txtCtrl){
284     Enumerator<ListItem*>* myvec = LBC->getSelectedItems();    
285     
286     int sz = 0;    
287     while( myvec->hasMoreElements() ){
288         sz += 1;
289         ListItem* li = myvec->nextElement();
290     }
291     String text = StringUtils::toString( sz ) + " items selected";    
292     txtCtrl->getTextModel()->setText( text );
293 
294 }
295 
296 void ListBoxWindow::onbtnSingleClicked( ButtonEvent*  ){    
297     ListBoxControl* LBC = (ListBoxControl*)this->findComponent( "listBoxSingle" );
298     TextControl* txtCtrl = (TextControl*)this->findComponent( "textCtrlSingle" );
299     LBC->deselectAllItems();    
300     getListBoxInfo(LBC, txtCtrl);
301 }
302 
303 void ListBoxWindow::onbtnMultiClicked( ButtonEvent*  ){    
304     ListBoxControl* LBC = (ListBoxControl*)this->findComponent( "listBoxMulti" );
305     TextControl* txtCtrl = (TextControl*)this->findComponent( "textCtrlMulti" );
306     LBC->deselectAllItems();    
307     getListBoxInfo(LBC, txtCtrl);    
308 }
309 
310 void ListBoxWindow::onbtnExtendedClicked( ButtonEvent*  ){    
311     ListBoxControl* LBC = (ListBoxControl*)this->findComponent( "listBoxExtended" );
312     TextControl* txtCtrl = (TextControl*)this->findComponent( "textCtrlExtended" );
313     LBC->deselectAllItems();    
314     getListBoxInfo(LBC, txtCtrl);
315 }
316 
317 void ListBoxWindow::getListBoxSingleInfo( ItemEvent* )
318 {
319     ListBoxControl* LBC = (ListBoxControl*)this->findComponent( "listBoxSingle" );    
320     TextControl* txtCtrl = (TextControl*)this->findComponent( "textCtrlSingle" );
321     getListBoxInfo(LBC, txtCtrl);    
322 }
323 
324 void ListBoxWindow::getListBoxMultiInfo( ItemEvent* )
325 {
326     ListBoxControl* LBC = (ListBoxControl*)this->findComponent( "listBoxMulti" );    
327     TextControl* txtCtrl = (TextControl*)this->findComponent( "textCtrlMulti" );
328     getListBoxInfo(LBC, txtCtrl);    
329 }
330 
331 void ListBoxWindow::getListBoxExtendedInfo( ItemEvent* )
332 {
333     ListBoxControl* LBC = (ListBoxControl*)this->findComponent( "listBoxExtended" );    
334     TextControl* txtCtrl = (TextControl*)this->findComponent( "textCtrlExtended" );
335     getListBoxInfo(LBC, txtCtrl);
336 }
337 
338 
339 
340 
341 
342 class ListBoxApplication : public Application {
343 public:
344 
345     ListBoxApplication( int argc, char** argv ) : Application(argc, argv) {
346 
347     }
348 
349     virtual bool initRunningApplication(){
350         bool result = Application::initRunningApplication();
351         
352         Window* mainWindow = new ListBoxWindow();
353         setMainWindow(mainWindow);
354         mainWindow->setBounds( &Rect( 50.0, 50.0, 650.0, 500.0 ) );
355         mainWindow->show();
356         
357         return result;
358     }
359 
360 };
361 
362 
363 int main(int argc, char *argv[])
364 {
365     Application* app = new ListBoxApplication( argc, argv );
366 
367     Application::main();
368     
369     return 0;
370 }

 

ListBox

标签:des   style   blog   ar   io   color   os   sp   for   

原文地址:http://www.cnblogs.com/elitiwin/p/4171194.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!