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