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