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