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