]> git.saurik.com Git - wxWidgets.git/blob - samples/controls/controls.cpp
* wxCreateDynamicObject() uses an hashtable now
[wxWidgets.git] / samples / controls / controls.cpp
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
27 #include "wx/notebook.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
49 void OnSize( wxSizeEvent& event );
50 void OnListBox( wxCommandEvent &event );
51 void OnListBoxButtons( wxCommandEvent &event );
52 void OnChoice( wxCommandEvent &event );
53 void OnChoiceButtons( wxCommandEvent &event );
54 void OnCombo( wxCommandEvent &event );
55 void OnComboButtons( wxCommandEvent &event );
56 void OnRadio( wxCommandEvent &event );
57 void OnRadioButtons( wxCommandEvent &event );
58
59 wxListBox *m_listbox;
60 wxChoice *m_choice;
61 wxComboBox *m_combo;
62 wxRadioBox *m_radio;
63
64 wxTextCtrl *m_text;
65 wxNotebook *m_notebook;
66
67 DECLARE_EVENT_TABLE()
68 };
69
70 class MyFrame: public wxFrame
71 {
72 public:
73
74 MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h);
75
76 public:
77
78 void OnQuit(wxCommandEvent& event);
79 void OnAbout(wxCommandEvent& event);
80 bool OnClose(void) { return TRUE; }
81
82 DECLARE_EVENT_TABLE()
83 };
84
85 //----------------------------------------------------------------------
86 // main()
87 //----------------------------------------------------------------------
88
89 IMPLEMENT_APP (MyApp)
90
91 //----------------------------------------------------------------------
92 // MyApp
93 //----------------------------------------------------------------------
94
95 const int MINIMAL_QUIT = 100;
96 const int MINIMAL_TEXT = 101;
97 const int MINIMAL_ABOUT = 102;
98
99 bool MyApp::OnInit(void)
100 {
101 // Create the main frame window
102 MyFrame *frame = new MyFrame((wxFrame *) NULL, (char *) "Controls wxWindows App", 50, 50, 500, 420 );
103
104 // Give it an icon
105 #ifdef __WXMSW__
106 frame->SetIcon(wxIcon("mondrian"));
107 #else
108 frame->SetIcon(wxIcon( mondrian_xpm ));
109 #endif
110
111 wxMenu *file_menu = new wxMenu;
112
113 file_menu->Append(MINIMAL_ABOUT, "&About");
114 file_menu->Append(MINIMAL_QUIT, "E&xit");
115 wxMenuBar *menu_bar = new wxMenuBar;
116 menu_bar->Append(file_menu, "&File");
117 frame->SetMenuBar(menu_bar);
118
119 frame->Show(TRUE);
120
121 SetTopWindow(frame);
122
123 return TRUE;
124 }
125
126 //----------------------------------------------------------------------
127 // MyPanel
128 //----------------------------------------------------------------------
129
130 const ID_NOTEBOOK = 1000;
131
132 const ID_LISTBOX = 130;
133 const ID_LISTBOX_SEL_NUM = 131;
134 const ID_LISTBOX_SEL_STR = 132;
135 const ID_LISTBOX_CLEAR = 133;
136 const ID_LISTBOX_APPEND = 134;
137 const ID_LISTBOX_DELETE = 135;
138
139 const ID_CHOICE = 120;
140 const ID_CHOICE_SEL_NUM = 121;
141 const ID_CHOICE_SEL_STR = 122;
142 const ID_CHOICE_CLEAR = 123;
143 const ID_CHOICE_APPEND = 124;
144 const ID_CHOICE_DELETE = 125;
145
146 const ID_COMBO = 140;
147 const ID_COMBO_SEL_NUM = 141;
148 const ID_COMBO_SEL_STR = 142;
149 const ID_COMBO_CLEAR = 143;
150 const ID_COMBO_APPEND = 144;
151 const ID_COMBO_DELETE = 145;
152
153 const ID_TEXT = 150;
154
155 const ID_RADIOBOX = 160;
156 const ID_RADIOBOX_SEL_NUM = 161;
157 const ID_RADIOBOX_SEL_STR = 162;
158
159 BEGIN_EVENT_TABLE(MyPanel, wxPanel)
160 EVT_SIZE ( MyPanel::OnSize)
161 EVT_LISTBOX (ID_LISTBOX, MyPanel::OnListBox)
162 EVT_BUTTON (ID_LISTBOX_SEL_NUM, MyPanel::OnListBoxButtons)
163 EVT_BUTTON (ID_LISTBOX_SEL_STR, MyPanel::OnListBoxButtons)
164 EVT_BUTTON (ID_LISTBOX_CLEAR, MyPanel::OnListBoxButtons)
165 EVT_BUTTON (ID_LISTBOX_APPEND, MyPanel::OnListBoxButtons)
166 EVT_BUTTON (ID_LISTBOX_DELETE, MyPanel::OnListBoxButtons)
167 EVT_CHOICE (ID_CHOICE, MyPanel::OnChoice)
168 EVT_BUTTON (ID_CHOICE_SEL_NUM, MyPanel::OnChoiceButtons)
169 EVT_BUTTON (ID_CHOICE_SEL_STR, MyPanel::OnChoiceButtons)
170 EVT_BUTTON (ID_CHOICE_CLEAR, MyPanel::OnChoiceButtons)
171 EVT_BUTTON (ID_CHOICE_APPEND, MyPanel::OnChoiceButtons)
172 EVT_BUTTON (ID_CHOICE_DELETE, MyPanel::OnChoiceButtons)
173 EVT_CHOICE (ID_COMBO, MyPanel::OnCombo)
174 EVT_BUTTON (ID_COMBO_SEL_NUM, MyPanel::OnComboButtons)
175 EVT_BUTTON (ID_COMBO_SEL_STR, MyPanel::OnComboButtons)
176 EVT_BUTTON (ID_COMBO_CLEAR, MyPanel::OnComboButtons)
177 EVT_BUTTON (ID_COMBO_APPEND, MyPanel::OnComboButtons)
178 EVT_BUTTON (ID_COMBO_DELETE, MyPanel::OnComboButtons)
179 EVT_RADIOBOX (ID_RADIOBOX, MyPanel::OnRadio)
180 EVT_BUTTON (ID_RADIOBOX_SEL_NUM, MyPanel::OnRadioButtons)
181 EVT_BUTTON (ID_RADIOBOX_SEL_STR, MyPanel::OnRadioButtons)
182 END_EVENT_TABLE()
183
184 MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h ) :
185 wxPanel( frame, -1, wxPoint(x, y), wxSize(w, h) )
186 {
187 m_text = new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(0,50), wxSize(100,50), wxTE_MULTILINE );
188
189 m_notebook = new wxNotebook( this, ID_NOTEBOOK, wxPoint(0,0), wxSize(200,150) );
190
191 wxString choices[] =
192 {
193 "This",
194 "is a",
195 "wonderful",
196 "example.",
197 };
198
199 wxPanel *panel = new wxPanel(m_notebook);
200 m_listbox = new wxListBox( panel, ID_LISTBOX, wxPoint(10,10), wxSize(120,70), 4, choices );
201 (void)new wxButton( panel, ID_LISTBOX_SEL_NUM, "Select #2", wxPoint(180,30), wxSize(100,30) );
202 (void)new wxButton( panel, ID_LISTBOX_SEL_STR, "Select 'This'", wxPoint(300,30), wxSize(100,30) );
203 (void)new wxButton( panel, ID_LISTBOX_CLEAR, "Clear", wxPoint(180,80), wxSize(100,30) );
204 (void)new wxButton( panel, ID_LISTBOX_APPEND, "Append 'Hi!'", wxPoint(300,80), wxSize(100,30) );
205 (void)new wxButton( panel, ID_LISTBOX_DELETE, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
206 m_notebook->AddPage(panel, "wxList");
207
208 panel = new wxPanel(m_notebook);
209 m_choice = new wxChoice( panel, ID_CHOICE, wxPoint(10,10), wxSize(120,-1), 4, choices );
210 (void)new wxButton( panel, ID_CHOICE_SEL_NUM, "Select #2", wxPoint(180,30), wxSize(100,30) );
211 (void)new wxButton( panel, ID_CHOICE_SEL_STR, "Select 'This'", wxPoint(300,30), wxSize(100,30) );
212 (void)new wxButton( panel, ID_CHOICE_CLEAR, "Clear", wxPoint(180,80), wxSize(100,30) );
213 (void)new wxButton( panel, ID_CHOICE_APPEND, "Append 'Hi!'", wxPoint(300,80), wxSize(100,30) );
214 (void)new wxButton( panel, ID_CHOICE_DELETE, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
215 m_notebook->AddPage(panel, "wxChoice");
216
217 panel = new wxPanel(m_notebook);
218 m_combo = new wxComboBox( panel, ID_COMBO, "This", wxPoint(10,10), wxSize(170,-1), 4, choices );
219 (void)new wxButton( panel, ID_COMBO_SEL_NUM, "Select #2", wxPoint(180,30), wxSize(100,30) );
220 (void)new wxButton( panel, ID_COMBO_SEL_STR, "Select 'This'", wxPoint(300,30), wxSize(100,30) );
221 (void)new wxButton( panel, ID_COMBO_CLEAR, "Clear", wxPoint(180,80), wxSize(100,30) );
222 (void)new wxButton( panel, ID_COMBO_APPEND, "Append 'Hi!'", wxPoint(300,80), wxSize(100,30) );
223 (void)new wxButton( panel, ID_COMBO_DELETE, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
224 m_notebook->AddPage(panel, "wxComboBox");
225
226 wxTextCtrl *text = new wxTextCtrl( m_notebook, ID_TEXT, "Write text here.", wxPoint(10,10), wxSize(120,100), wxTE_MULTILINE );
227 m_notebook->AddPage( text, "wxTextCtrl" );
228
229 panel = new wxPanel(m_notebook);
230 m_radio = new wxRadioBox( panel, ID_RADIOBOX, "This", wxPoint(10,10), wxSize(-1,-1), 4, choices );
231 (void)new wxButton( panel, ID_RADIOBOX_SEL_NUM, "Select #2", wxPoint(200,30), wxSize(100,30) );
232 (void)new wxButton( panel, ID_RADIOBOX_SEL_STR, "Select 'This'", wxPoint(200,80), wxSize(100,30) );
233 m_notebook->AddPage(panel, "wxRadioBox");
234 }
235
236 void MyPanel::OnSize( wxSizeEvent& WXUNUSED(event) )
237 {
238 int x = 0;
239 int y = 0;
240 GetClientSize( &x, &y );
241
242 if (m_notebook) m_notebook->SetSize( 2, 2, x-4, y*2/3-4 );
243 if (m_text) m_text->SetSize( 2, y*2/3+2, x-4, y/3-4 );
244 }
245
246 void MyPanel::OnListBox( wxCommandEvent &event )
247 {
248 m_text->WriteText( "ListBox selection string is: " );
249 m_text->WriteText( event.GetString() );
250 m_text->WriteText( "\n" );
251 }
252
253 void MyPanel::OnListBoxButtons( wxCommandEvent &event )
254 {
255 switch (event.GetId())
256 {
257 case ID_LISTBOX_SEL_NUM:
258 {
259 m_listbox->SetSelection( 2 );
260 break;
261 }
262 case ID_LISTBOX_SEL_STR:
263 {
264 m_listbox->SetStringSelection( "This" );
265 break;
266 }
267 case ID_LISTBOX_CLEAR:
268 {
269 m_listbox->Clear();
270 break;
271 }
272 case ID_LISTBOX_APPEND:
273 {
274 m_listbox->Append( "Hi!" );
275 break;
276 }
277 case ID_LISTBOX_DELETE:
278 {
279 int idx = m_listbox->GetSelection();
280 m_listbox->Delete( idx );
281 break;
282 }
283 }
284 }
285
286 void MyPanel::OnChoice( wxCommandEvent &event )
287 {
288 m_text->WriteText( "Choice selection string is: " );
289 m_text->WriteText( event.GetString() );
290 m_text->WriteText( "\n" );
291 }
292
293 void MyPanel::OnChoiceButtons( wxCommandEvent &event )
294 {
295 switch (event.GetId())
296 {
297 case ID_CHOICE_SEL_NUM:
298 {
299 m_choice->SetSelection( 2 );
300 break;
301 }
302 case ID_CHOICE_SEL_STR:
303 {
304 m_choice->SetStringSelection( "This" );
305 break;
306 }
307 case ID_CHOICE_CLEAR:
308 {
309 m_choice->Clear();
310 break;
311 }
312 case ID_CHOICE_APPEND:
313 {
314 m_choice->Append( "Hi!" );
315 break;
316 }
317 case ID_CHOICE_DELETE:
318 {
319 int idx = m_choice->GetSelection();
320 m_choice->Delete( idx );
321 break;
322 }
323 }
324 }
325
326 void MyPanel::OnCombo( wxCommandEvent &event )
327 {
328 m_text->WriteText( "ComboBox selection string is: " );
329 m_text->WriteText( event.GetString() );
330 m_text->WriteText( "\n" );
331 }
332
333 void MyPanel::OnComboButtons( wxCommandEvent &event )
334 {
335 switch (event.GetId())
336 {
337 case ID_COMBO_SEL_NUM:
338 {
339 m_combo->SetSelection( 2 );
340 break;
341 }
342 case ID_COMBO_SEL_STR:
343 {
344 m_combo->SetStringSelection( "This" );
345 break;
346 }
347 case ID_COMBO_CLEAR:
348 {
349 m_combo->Clear();
350 break;
351 }
352 case ID_COMBO_APPEND:
353 {
354 m_combo->Append( "Hi!" );
355 break;
356 }
357 case ID_COMBO_DELETE:
358 {
359 int idx = m_combo->GetSelection();
360 m_combo->Delete( idx );
361 break;
362 }
363 }
364 }
365
366 void MyPanel::OnRadio( wxCommandEvent &event )
367 {
368 m_text->WriteText( "RadioBox selection string is: " );
369 m_text->WriteText( event.GetString() );
370 m_text->WriteText( "\n" );
371 }
372
373 void MyPanel::OnRadioButtons( wxCommandEvent &event )
374 {
375 switch (event.GetId())
376 {
377 case ID_RADIOBOX_SEL_NUM:
378 {
379 m_radio->SetSelection( 2 );
380 break;
381 }
382 case ID_RADIOBOX_SEL_STR:
383 {
384 m_radio->SetStringSelection( "This" );
385 break;
386 }
387 }
388 }
389
390 //----------------------------------------------------------------------
391 // MyFrame
392 //----------------------------------------------------------------------
393
394 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
395 EVT_MENU(MINIMAL_QUIT, MyFrame::OnQuit)
396 EVT_MENU(MINIMAL_ABOUT, MyFrame::OnAbout)
397 END_EVENT_TABLE()
398
399 MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h):
400 wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
401 {
402 (void)new MyPanel( this, 10, 10, 300, 100 );
403 }
404
405 void MyFrame::OnQuit (wxCommandEvent& WXUNUSED(event) )
406 {
407 Close(TRUE);
408 }
409
410 void MyFrame::OnAbout( wxCommandEvent& WXUNUSED(event) )
411 {
412 wxMessageDialog dialog(this, "This is a control sample", "About Controls", wxOK );
413 dialog.ShowModal();
414 }
415
416