]> git.saurik.com Git - wxWidgets.git/blob - samples/controls/controls.cpp
ac2be534d512a9a02ea570fe40fa1c90574e810c
[wxWidgets.git] / samples / controls / controls.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: controls.cpp
3 // Purpose: Controls wxWindows sample
4 // Author: Robert Roebling
5 // Modified by:
6 // RCS-ID: $Id$
7 // Copyright: (c) Robert Roebling, Julian Smart
8 // Licence: wxWindows license
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifdef __GNUG__
12 #pragma implementation "controls.h"
13 #endif
14
15 // For compilers that support precompilation, includes "wx/wx.h".
16 #include "wx/wxprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #ifndef WX_PRECOMP
23 #include "wx/wx.h"
24 #endif
25
26 #include "wx/notebook.h"
27 #include "wx/imaglist.h"
28
29 #if defined(__WXGTK__) || defined(__WXMOTIF__)
30 #include "mondrian.xpm"
31 #include "icons/choice.xpm"
32 #include "icons/combo.xpm"
33 #include "icons/list.xpm"
34 #include "icons/radio.xpm"
35 #include "icons/text.xpm"
36 #include "icons/stattext.xpm"
37 #endif
38
39 //----------------------------------------------------------------------
40 // class definitions
41 //----------------------------------------------------------------------
42
43 class MyApp: public wxApp
44 {
45 public:
46 bool OnInit(void);
47 };
48
49 class MyPanel: public wxPanel
50 {
51 public:
52
53 MyPanel(wxFrame *frame, int x, int y, int w, int h);
54 virtual ~MyPanel();
55
56 void OnSize( wxSizeEvent& event );
57 void OnListBox( wxCommandEvent &event );
58 void OnListBoxButtons( wxCommandEvent &event );
59 void OnChoice( wxCommandEvent &event );
60 void OnChoiceButtons( wxCommandEvent &event );
61 void OnCombo( wxCommandEvent &event );
62 void OnComboButtons( wxCommandEvent &event );
63 void OnRadio( wxCommandEvent &event );
64 void OnRadioButtons( wxCommandEvent &event );
65 void OnSetFont( wxCommandEvent &event );
66 void OnPageChanged( wxNotebookEvent &event );
67
68 wxListBox *m_listbox;
69 wxChoice *m_choice;
70 wxComboBox *m_combo;
71 wxRadioBox *m_radio;
72 wxButton *m_fontButton;
73
74 wxTextCtrl *m_text;
75 wxNotebook *m_notebook;
76
77 DECLARE_EVENT_TABLE()
78 };
79
80 class MyFrame: public wxFrame
81 {
82 public:
83
84 MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h);
85
86 public:
87
88 void OnQuit(wxCommandEvent& event);
89 void OnAbout(wxCommandEvent& event);
90 bool OnClose(void) { return TRUE; }
91
92 DECLARE_EVENT_TABLE()
93 };
94
95 //----------------------------------------------------------------------
96 // main()
97 //----------------------------------------------------------------------
98
99 IMPLEMENT_APP (MyApp)
100
101 //----------------------------------------------------------------------
102 // MyApp
103 //----------------------------------------------------------------------
104
105 const int MINIMAL_QUIT = 100;
106 const int MINIMAL_TEXT = 101;
107 const int MINIMAL_ABOUT = 102;
108
109 bool MyApp::OnInit(void)
110 {
111 // Create the main frame window
112 MyFrame *frame = new MyFrame((wxFrame *) NULL, (char *) "Controls wxWindows App", 50, 50, 530, 420 );
113
114 // Give it an icon
115 #ifdef __WXMSW__
116 frame->SetIcon(wxIcon("mondrian"));
117 #else
118 frame->SetIcon(wxIcon( mondrian_xpm ));
119 #endif
120
121 wxMenu *file_menu = new wxMenu;
122
123 file_menu->Append(MINIMAL_ABOUT, "&About");
124 file_menu->Append(MINIMAL_QUIT, "E&xit");
125 wxMenuBar *menu_bar = new wxMenuBar;
126 menu_bar->Append(file_menu, "&File");
127 frame->SetMenuBar(menu_bar);
128
129 frame->Show(TRUE);
130
131 SetTopWindow(frame);
132
133 return TRUE;
134 }
135
136 //----------------------------------------------------------------------
137 // MyPanel
138 //----------------------------------------------------------------------
139
140 const ID_NOTEBOOK = 1000;
141
142 const ID_LISTBOX = 130;
143 const ID_LISTBOX_SEL_NUM = 131;
144 const ID_LISTBOX_SEL_STR = 132;
145 const ID_LISTBOX_CLEAR = 133;
146 const ID_LISTBOX_APPEND = 134;
147 const ID_LISTBOX_DELETE = 135;
148 const ID_LISTBOX_FONT = 136;
149 const ID_LISTBOX_ENABLE = 137;
150
151 const ID_CHOICE = 120;
152 const ID_CHOICE_SEL_NUM = 121;
153 const ID_CHOICE_SEL_STR = 122;
154 const ID_CHOICE_CLEAR = 123;
155 const ID_CHOICE_APPEND = 124;
156 const ID_CHOICE_DELETE = 125;
157 const ID_CHOICE_FONT = 126;
158 const ID_CHOICE_ENABLE = 127;
159
160 const ID_COMBO = 140;
161 const ID_COMBO_SEL_NUM = 141;
162 const ID_COMBO_SEL_STR = 142;
163 const ID_COMBO_CLEAR = 143;
164 const ID_COMBO_APPEND = 144;
165 const ID_COMBO_DELETE = 145;
166 const ID_COMBO_FONT = 146;
167 const ID_COMBO_ENABLE = 147;
168
169 const ID_TEXT = 150;
170
171 const ID_RADIOBOX = 160;
172 const ID_RADIOBOX_SEL_NUM = 161;
173 const ID_RADIOBOX_SEL_STR = 162;
174 const ID_RADIOBOX_FONT = 163;
175 const ID_RADIOBOX_ENABLE = 164;
176
177 const ID_SET_FONT = 170;
178
179 BEGIN_EVENT_TABLE(MyPanel, wxPanel)
180 EVT_SIZE ( MyPanel::OnSize)
181 EVT_NOTEBOOK_PAGE_CHANGED(ID_NOTEBOOK, MyPanel::OnPageChanged)
182 EVT_LISTBOX (ID_LISTBOX, MyPanel::OnListBox)
183 EVT_BUTTON (ID_LISTBOX_SEL_NUM, MyPanel::OnListBoxButtons)
184 EVT_BUTTON (ID_LISTBOX_SEL_STR, MyPanel::OnListBoxButtons)
185 EVT_BUTTON (ID_LISTBOX_CLEAR, MyPanel::OnListBoxButtons)
186 EVT_BUTTON (ID_LISTBOX_APPEND, MyPanel::OnListBoxButtons)
187 EVT_BUTTON (ID_LISTBOX_DELETE, MyPanel::OnListBoxButtons)
188 EVT_BUTTON (ID_LISTBOX_FONT, MyPanel::OnListBoxButtons)
189 EVT_CHECKBOX (ID_LISTBOX_ENABLE, MyPanel::OnListBoxButtons)
190 EVT_CHOICE (ID_CHOICE, MyPanel::OnChoice)
191 EVT_BUTTON (ID_CHOICE_SEL_NUM, MyPanel::OnChoiceButtons)
192 EVT_BUTTON (ID_CHOICE_SEL_STR, MyPanel::OnChoiceButtons)
193 EVT_BUTTON (ID_CHOICE_CLEAR, MyPanel::OnChoiceButtons)
194 EVT_BUTTON (ID_CHOICE_APPEND, MyPanel::OnChoiceButtons)
195 EVT_BUTTON (ID_CHOICE_DELETE, MyPanel::OnChoiceButtons)
196 EVT_BUTTON (ID_CHOICE_FONT, MyPanel::OnChoiceButtons)
197 EVT_CHECKBOX (ID_CHOICE_ENABLE, MyPanel::OnChoiceButtons)
198 EVT_CHOICE (ID_COMBO, MyPanel::OnCombo)
199 EVT_BUTTON (ID_COMBO_SEL_NUM, MyPanel::OnComboButtons)
200 EVT_BUTTON (ID_COMBO_SEL_STR, MyPanel::OnComboButtons)
201 EVT_BUTTON (ID_COMBO_CLEAR, MyPanel::OnComboButtons)
202 EVT_BUTTON (ID_COMBO_APPEND, MyPanel::OnComboButtons)
203 EVT_BUTTON (ID_COMBO_DELETE, MyPanel::OnComboButtons)
204 EVT_BUTTON (ID_COMBO_FONT, MyPanel::OnComboButtons)
205 EVT_CHECKBOX (ID_COMBO_ENABLE, MyPanel::OnComboButtons)
206 EVT_RADIOBOX (ID_RADIOBOX, MyPanel::OnRadio)
207 EVT_BUTTON (ID_RADIOBOX_SEL_NUM, MyPanel::OnRadioButtons)
208 EVT_BUTTON (ID_RADIOBOX_SEL_STR, MyPanel::OnRadioButtons)
209 EVT_BUTTON (ID_RADIOBOX_FONT, MyPanel::OnRadioButtons)
210 EVT_CHECKBOX (ID_RADIOBOX_ENABLE, MyPanel::OnRadioButtons)
211 EVT_BUTTON (ID_SET_FONT, MyPanel::OnSetFont)
212 END_EVENT_TABLE()
213
214 MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h ) :
215 wxPanel( frame, -1, wxPoint(x, y), wxSize(w, h) )
216 {
217 SetBackgroundColour("cadet blue");
218
219 m_text = new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(0,50), wxSize(100,50), wxTE_MULTILINE );
220 m_text->SetBackgroundColour("wheat");
221
222 m_notebook = new wxNotebook( this, ID_NOTEBOOK, wxPoint(0,0), wxSize(200,150) );
223
224 wxString choices[] =
225 {
226 "This",
227 "is one of my",
228 "really",
229 "wonderful",
230 "examples.",
231 };
232
233 // image ids and names
234 enum
235 {
236 Image_List, Image_Choice, Image_Combo, Image_Text, Image_Radio, Image_Static, Image_Max
237 };
238
239 // fill the image list
240 #ifdef __WXMSW__
241 const char *aIconNames[] =
242 {
243 "list.xpm", "choice.xpm", "combo.xpm", "text.xpm", "radio.xpm", "stattext.xpm"
244 };
245
246 wxASSERT( WXSIZEOF(aIconNames) == Image_Max ); // keep in sync
247
248 wxString strIconDir = "icons/";
249
250 wxImageList *imagelist = new wxImageList(32, 32);
251 for ( size_t n = 0; n < Image_Max; n++ )
252 {
253 imagelist->Add(wxBitmap(strIconDir + aIconNames[n]));
254 }
255 #else
256 wxImageList *imagelist = new wxImageList(32, 32);
257
258 imagelist-> Add( wxBitmap( list_xpm ));
259 imagelist-> Add( wxBitmap( choice_xpm ));
260 imagelist-> Add( wxBitmap( combo_xpm ));
261 imagelist-> Add( wxBitmap( text_xpm ));
262 imagelist-> Add( wxBitmap( radio_xpm ));
263 imagelist-> Add( wxBitmap( stattext_xpm ));
264 #endif
265
266 wxButton *button = (wxButton*)NULL;
267
268 m_notebook->SetImageList(imagelist);
269 m_notebook->SetBackgroundColour("cadet blue");
270
271 wxPanel *panel = new wxPanel(m_notebook);
272 panel->SetBackgroundColour("cadet blue");
273 m_listbox = new wxListBox( panel, ID_LISTBOX, wxPoint(10,10), wxSize(120,70), 5, choices );
274 m_listbox->SetBackgroundColour("wheat");
275 (void)new wxButton( panel, ID_LISTBOX_SEL_NUM, "Select #2", wxPoint(180,30), wxSize(140,30) );
276 (void)new wxButton( panel, ID_LISTBOX_SEL_STR, "Select 'This'", wxPoint(340,30), wxSize(140,30) );
277 (void)new wxButton( panel, ID_LISTBOX_CLEAR, "Clear", wxPoint(180,80), wxSize(140,30) );
278 (void)new wxButton( panel, ID_LISTBOX_APPEND, "Append 'Hi!'", wxPoint(340,80), wxSize(140,30) );
279 (void)new wxButton( panel, ID_LISTBOX_DELETE, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
280 button = new wxButton( panel, ID_LISTBOX_FONT, "Set Italic font", wxPoint(340,130), wxSize(140,30) );
281 button->SetForegroundColour( "red" );
282 (void)new wxCheckBox( panel, ID_LISTBOX_ENABLE, "Disable", wxPoint(20,130), wxSize(140,30) );
283 m_notebook->AddPage(panel, "wxList", FALSE, Image_List);
284
285 panel = new wxPanel(m_notebook);
286 panel->SetBackgroundColour("cadet blue");
287 m_choice = new wxChoice( panel, ID_CHOICE, wxPoint(10,10), wxSize(120,-1), 5, choices );
288 m_choice->SetBackgroundColour("wheat");
289 (void)new wxButton( panel, ID_CHOICE_SEL_NUM, "Select #2", wxPoint(180,30), wxSize(140,30) );
290 (void)new wxButton( panel, ID_CHOICE_SEL_STR, "Select 'This'", wxPoint(340,30), wxSize(140,30) );
291 (void)new wxButton( panel, ID_CHOICE_CLEAR, "Clear", wxPoint(180,80), wxSize(140,30) );
292 (void)new wxButton( panel, ID_CHOICE_APPEND, "Append 'Hi!'", wxPoint(340,80), wxSize(140,30) );
293 (void)new wxButton( panel, ID_CHOICE_DELETE, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
294 (void)new wxButton( panel, ID_CHOICE_FONT, "Set Italic font", wxPoint(340,130), wxSize(140,30) );
295 (void)new wxCheckBox( panel, ID_CHOICE_ENABLE, "Disable", wxPoint(20,130), wxSize(140,30) );
296 m_notebook->AddPage(panel, "wxChoice", FALSE, Image_Choice);
297
298 panel = new wxPanel(m_notebook);
299 panel->SetBackgroundColour("cadet blue");
300 m_combo = new wxComboBox( panel, ID_COMBO, "This", wxPoint(10,10), wxSize(120,-1), 5, choices );
301 m_combo->SetBackgroundColour("wheat");
302 (void)new wxButton( panel, ID_COMBO_SEL_NUM, "Select #2", wxPoint(180,30), wxSize(140,30) );
303 (void)new wxButton( panel, ID_COMBO_SEL_STR, "Select 'This'", wxPoint(340,30), wxSize(140,30) );
304 (void)new wxButton( panel, ID_COMBO_CLEAR, "Clear", wxPoint(180,80), wxSize(140,30) );
305 (void)new wxButton( panel, ID_COMBO_APPEND, "Append 'Hi!'", wxPoint(340,80), wxSize(140,30) );
306 (void)new wxButton( panel, ID_COMBO_DELETE, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
307 (void)new wxButton( panel, ID_COMBO_FONT, "Set Italic font", wxPoint(340,130), wxSize(140,30) );
308 (void)new wxCheckBox( panel, ID_COMBO_ENABLE, "Disable", wxPoint(20,130), wxSize(140,30) );
309 m_notebook->AddPage(panel, "wxComboBox", FALSE, Image_Combo);
310
311 panel = new wxPanel(m_notebook);
312 panel->SetBackgroundColour("cadet blue");
313 wxTextCtrl *tc = new wxTextCtrl( panel, ID_TEXT, "Write text here.", wxPoint(10,10), wxSize(350,28));
314 tc->SetBackgroundColour("wheat");
315 tc = new wxTextCtrl( panel, ID_TEXT, "And here.", wxPoint(10,50), wxSize(350,160), wxTE_MULTILINE );
316 tc->SetBackgroundColour("wheat");
317 m_notebook->AddPage(panel, "wxTextCtrl" , FALSE, Image_Text);
318
319 panel = new wxPanel(m_notebook);
320 panel->SetBackgroundColour("cadet blue");
321 m_radio = new wxRadioBox( panel, ID_RADIOBOX, "This", wxPoint(10,10), wxSize(-1,-1), 5, choices );
322 m_radio->SetBackgroundColour("wheat");
323 (void)new wxButton( panel, ID_RADIOBOX_SEL_NUM, "Select #2", wxPoint(180,30), wxSize(140,30) );
324 (void)new wxButton( panel, ID_RADIOBOX_SEL_STR, "Select 'This'", wxPoint(180,80), wxSize(140,30) );
325 (void)new wxButton( panel, ID_RADIOBOX_FONT, "Set Italic font", wxPoint(180,130), wxSize(140,30) );
326 (void)new wxCheckBox( panel, ID_RADIOBOX_ENABLE, "Disable", wxPoint(20,130), wxSize(140,30) );
327 m_fontButton = new wxButton( panel, ID_SET_FONT, "Set more Italic font", wxPoint(340,30), wxSize(160,30) );
328 m_notebook->AddPage(panel, "wxRadioBox", FALSE, Image_Radio);
329
330 panel = new wxPanel(m_notebook);
331 panel->SetBackgroundColour("cadet blue");
332 (void)new wxStaticBox( panel, -1, "StaticBox", wxPoint(10,10), wxSize(160,130) );
333 m_notebook->AddPage(panel, "wxStaticBox", FALSE, Image_Static);
334 }
335
336 void MyPanel::OnSize( wxSizeEvent& WXUNUSED(event) )
337 {
338 int x = 0;
339 int y = 0;
340 GetClientSize( &x, &y );
341
342 if (m_notebook) m_notebook->SetSize( 2, 2, x-4, y*2/3-4 );
343 if (m_text) m_text->SetSize( 2, y*2/3+2, x-4, y/3-4 );
344 }
345
346 void MyPanel::OnPageChanged( wxNotebookEvent &event )
347 {
348 *m_text << "Notebook selection is " << event.GetSelection() << "\n";
349 }
350
351 void MyPanel::OnListBox( wxCommandEvent &event )
352 {
353 m_text->WriteText( "ListBox selection string is: " );
354 m_text->WriteText( event.GetString() );
355 m_text->WriteText( "\n" );
356 }
357
358 void MyPanel::OnListBoxButtons( wxCommandEvent &event )
359 {
360 switch (event.GetId())
361 {
362 case ID_LISTBOX_ENABLE:
363 {
364 m_listbox->Enable( !((bool)event.GetInt()) );
365 break;
366 }
367 case ID_LISTBOX_SEL_NUM:
368 {
369 m_listbox->SetSelection( 2 );
370 break;
371 }
372 case ID_LISTBOX_SEL_STR:
373 {
374 m_listbox->SetStringSelection( "This" );
375 break;
376 }
377 case ID_LISTBOX_CLEAR:
378 {
379 m_listbox->Clear();
380 break;
381 }
382 case ID_LISTBOX_APPEND:
383 {
384 m_listbox->Append( "Hi!" );
385 break;
386 }
387 case ID_LISTBOX_DELETE:
388 {
389 int idx = m_listbox->GetSelection();
390 m_listbox->Delete( idx );
391 break;
392 }
393 case ID_LISTBOX_FONT:
394 {
395 m_listbox->SetFont( *wxITALIC_FONT );
396 break;
397 }
398 }
399 }
400
401 void MyPanel::OnChoice( wxCommandEvent &event )
402 {
403 m_text->WriteText( "Choice selection string is: " );
404 m_text->WriteText( event.GetString() );
405 m_text->WriteText( "\n" );
406 }
407
408 void MyPanel::OnChoiceButtons( wxCommandEvent &event )
409 {
410 switch (event.GetId())
411 {
412 case ID_CHOICE_ENABLE:
413 {
414 m_choice->Enable( !((bool)event.GetInt()) );
415 break;
416 }
417 case ID_CHOICE_SEL_NUM:
418 {
419 m_choice->SetSelection( 2 );
420 break;
421 }
422 case ID_CHOICE_SEL_STR:
423 {
424 m_choice->SetStringSelection( "This" );
425 break;
426 }
427 case ID_CHOICE_CLEAR:
428 {
429 m_choice->Clear();
430 break;
431 }
432 case ID_CHOICE_APPEND:
433 {
434 m_choice->Append( "Hi!" );
435 break;
436 }
437 case ID_CHOICE_DELETE:
438 {
439 int idx = m_choice->GetSelection();
440 m_choice->Delete( idx );
441 break;
442 }
443 case ID_CHOICE_FONT:
444 {
445 m_choice->SetFont( *wxITALIC_FONT );
446 break;
447 }
448 }
449 }
450
451 void MyPanel::OnCombo( wxCommandEvent &event )
452 {
453 m_text->WriteText( "ComboBox selection string is: " );
454 m_text->WriteText( event.GetString() );
455 m_text->WriteText( "\n" );
456 }
457
458 void MyPanel::OnComboButtons( wxCommandEvent &event )
459 {
460 switch (event.GetId())
461 {
462 case ID_COMBO_ENABLE:
463 {
464 m_combo->Enable( !((bool)event.GetInt()) );
465 break;
466 }
467 case ID_COMBO_SEL_NUM:
468 {
469 m_combo->SetSelection( 2 );
470 break;
471 }
472 case ID_COMBO_SEL_STR:
473 {
474 m_combo->SetStringSelection( "This" );
475 break;
476 }
477 case ID_COMBO_CLEAR:
478 {
479 m_combo->Clear();
480 break;
481 }
482 case ID_COMBO_APPEND:
483 {
484 m_combo->Append( "Hi!" );
485 break;
486 }
487 case ID_COMBO_DELETE:
488 {
489 int idx = m_combo->GetSelection();
490 m_combo->Delete( idx );
491 break;
492 }
493 case ID_COMBO_FONT:
494 {
495 m_combo->SetFont( *wxITALIC_FONT );
496 break;
497 }
498 }
499 }
500
501 void MyPanel::OnRadio( wxCommandEvent &event )
502 {
503 m_text->WriteText( "RadioBox selection string is: " );
504 m_text->WriteText( event.GetString() );
505 m_text->WriteText( "\n" );
506 }
507
508 void MyPanel::OnRadioButtons( wxCommandEvent &event )
509 {
510 switch (event.GetId())
511 {
512 case ID_RADIOBOX_ENABLE:
513 {
514 m_radio->Enable( !((bool)event.GetInt()) );
515 break;
516 }
517 case ID_RADIOBOX_SEL_NUM:
518 {
519 m_radio->SetSelection( 2 );
520 break;
521 }
522 case ID_RADIOBOX_SEL_STR:
523 {
524 m_radio->SetStringSelection( "This" );
525 break;
526 }
527 case ID_RADIOBOX_FONT:
528 {
529 m_radio->SetFont( *wxITALIC_FONT );
530 break;
531 }
532 }
533 }
534
535 void MyPanel::OnSetFont( wxCommandEvent &WXUNUSED(event) )
536 {
537 m_fontButton->SetFont( *wxITALIC_FONT );
538 m_text->SetFont( *wxITALIC_FONT );
539 }
540
541 MyPanel::~MyPanel()
542 {
543 delete m_notebook->GetImageList();
544 }
545
546 //----------------------------------------------------------------------
547 // MyFrame
548 //----------------------------------------------------------------------
549
550 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
551 EVT_MENU(MINIMAL_QUIT, MyFrame::OnQuit)
552 EVT_MENU(MINIMAL_ABOUT, MyFrame::OnAbout)
553 END_EVENT_TABLE()
554
555 MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h):
556 wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
557 {
558 (void)new MyPanel( this, 10, 10, 300, 100 );
559 }
560
561 void MyFrame::OnQuit (wxCommandEvent& WXUNUSED(event) )
562 {
563 Close(TRUE);
564 }
565
566 void MyFrame::OnAbout( wxCommandEvent& WXUNUSED(event) )
567 {
568 wxMessageDialog dialog(this, "This is a control sample", "About Controls", wxOK );
569 dialog.ShowModal();
570 }