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