]> git.saurik.com Git - wxWidgets.git/blob - samples/controls/controls.cpp
1. corrected client data deletion in wxListBox (no more memory leaks in controls
[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
8 // Licence: wxWindows license
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifdef __GNUG__
12 #pragma implementation "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 #if !defined( __WXMSW__ ) || defined( __WIN95__ )
27 #include "wx/spinbutt.h"
28 #endif
29 #include "wx/notebook.h"
30 #include "wx/imaglist.h"
31
32 #if wxUSE_TOOLTIPS
33 #include "wx/tooltip.h"
34 #endif
35
36 #if defined(__WXGTK__) || defined(__WXMOTIF__)
37 #define USE_XPM
38 #endif
39
40 #ifdef USE_XPM
41 #include "mondrian.xpm"
42 #include "icons/choice.xpm"
43 #include "icons/combo.xpm"
44 #include "icons/list.xpm"
45 #include "icons/radio.xpm"
46 #include "icons/text.xpm"
47 #include "icons/gauge.xpm"
48 #endif
49
50 #ifdef __WIN16__
51 // Win16 doesn't have them
52 #undef wxUSE_SPINBUTTON
53 #define wxUSE_SPINBUTTON 0
54 #else
55 #if !defined(wxUSE_SPINBUTTON)
56 #define wxUSE_SPINBUTTON 1
57 #endif
58 #endif // __WIN16__
59
60 #include "wx/progdlg.h"
61
62 // VZ: this is a temp. hack, will remove soon
63 #define wxUSE_SPINCTRL 1
64
65 #if wxUSE_SPINCTRL
66 #include "wx/spinctrl.h"
67 #endif // wxUSE_SPINCTRL
68
69 //----------------------------------------------------------------------
70 // class definitions
71 //----------------------------------------------------------------------
72
73 class MyApp: public wxApp
74 {
75 public:
76 bool OnInit();
77 };
78
79 class MyPanel: public wxPanel
80 {
81 public:
82 MyPanel(wxFrame *frame, int x, int y, int w, int h);
83 virtual ~MyPanel();
84
85 void OnSize( wxSizeEvent& event );
86 void OnListBox( wxCommandEvent &event );
87 void OnListBoxDoubleClick( wxCommandEvent &event );
88 void OnListBoxButtons( wxCommandEvent &event );
89 void OnChoice( wxCommandEvent &event );
90 void OnChoiceButtons( wxCommandEvent &event );
91 void OnCombo( wxCommandEvent &event );
92 void OnComboButtons( wxCommandEvent &event );
93 void OnRadio( wxCommandEvent &event );
94 void OnRadioButtons( wxCommandEvent &event );
95 void OnSetFont( wxCommandEvent &event );
96 void OnPageChanged( wxNotebookEvent &event );
97 void OnPageChanging( wxNotebookEvent &event );
98 void OnSliderUpdate( wxCommandEvent &event );
99 void OnUpdateLabel( wxCommandEvent &event );
100 #if wxUSE_SPINBUTTON
101 void OnSpinUp( wxSpinEvent &event );
102 void OnSpinDown( wxSpinEvent &event );
103 void OnSpinUpdate( wxSpinEvent &event );
104 void OnUpdateShowProgress( wxUpdateUIEvent& event );
105 void OnShowProgress( wxCommandEvent &event );
106 #endif // wxUSE_SPINBUTTON
107
108 wxListBox *m_listbox,
109 *m_listboxSorted;
110 wxChoice *m_choice,
111 *m_choiceSorted;
112 wxComboBox *m_combo;
113 wxRadioBox *m_radio;
114 wxGauge *m_gauge;
115 wxSlider *m_slider;
116 wxButton *m_fontButton;
117 wxButton *m_lbSelectNum;
118 wxButton *m_lbSelectThis;
119 #if wxUSE_SPINBUTTON
120 wxSpinButton *m_spinbutton;
121 wxButton *m_btnProgress;
122 #endif // wxUSE_SPINBUTTON
123
124 #if wxUSE_SPINCTRL
125 wxSpinCtrl *m_spinctrl;
126 #endif // wxUSE_SPINCTRL
127
128 wxTextCtrl *m_spintext;
129 wxCheckBox *m_checkbox;
130
131 wxTextCtrl *m_text;
132 wxNotebook *m_notebook;
133
134 wxStaticText *m_label;
135
136 private:
137 DECLARE_EVENT_TABLE()
138 };
139
140 class MyFrame: public wxFrame
141 {
142 public:
143 MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h);
144
145 void OnQuit(wxCommandEvent& event);
146 void OnAbout(wxCommandEvent& event);
147 #if wxUSE_TOOLTIPS
148 void OnSetTooltipDelay(wxCommandEvent& event);
149 void OnToggleTooltips(wxCommandEvent& event);
150 #endif // wxUSE_TOOLTIPS
151 void OnIdle( wxIdleEvent& event );
152 void OnSize( wxSizeEvent& event );
153
154 private:
155 DECLARE_EVENT_TABLE()
156 };
157
158 //----------------------------------------------------------------------
159 // other
160 //----------------------------------------------------------------------
161
162 static void SetControlClientData(const char *name,
163 wxControlWithItems *control);
164
165 IMPLEMENT_APP(MyApp)
166
167 //----------------------------------------------------------------------
168 // MyApp
169 //----------------------------------------------------------------------
170
171 enum
172 {
173 MINIMAL_QUIT = 100,
174 MINIMAL_TEXT,
175 MINIMAL_ABOUT,
176
177 // tooltip menu
178 MINIMAL_SET_TOOLTIP_DELAY = 200,
179 MINIMAL_ENABLE_TOOLTIPS
180 };
181
182 bool MyApp::OnInit()
183 {
184 // Create the main frame window
185 MyFrame *frame = new MyFrame((wxFrame *) NULL,
186 "Controls wxWindows App",
187 50, 50, 530, 420);
188
189 // Give it an icon
190 // The wxICON() macros loads an icon from a resource under Windows
191 // and uses an #included XPM image under GTK+ and Motif
192
193 frame->SetIcon( wxICON(mondrian) );
194
195 wxMenu *file_menu = new wxMenu("", wxMENU_TEAROFF );
196 file_menu->Append(MINIMAL_ABOUT, "&About\tF1");
197 file_menu->Append(MINIMAL_QUIT, "E&xit\tAlt-X", "Quit controls sample");
198
199 wxMenuBar *menu_bar = new wxMenuBar;
200 menu_bar->Append(file_menu, "&File");
201
202 #if wxUSE_TOOLTIPS
203 wxMenu *tooltip_menu = new wxMenu;
204 tooltip_menu->Append(MINIMAL_SET_TOOLTIP_DELAY, "Set &delay\tCtrl-D");
205 tooltip_menu->AppendSeparator();
206 tooltip_menu->Append(MINIMAL_ENABLE_TOOLTIPS, "&Toggle tooltips\tCtrl-T",
207 "enable/disable tooltips", TRUE);
208 tooltip_menu->Check(MINIMAL_ENABLE_TOOLTIPS, TRUE);
209 menu_bar->Append(tooltip_menu, "&Tooltips");
210 #endif // wxUSE_TOOLTIPS
211
212 frame->SetMenuBar(menu_bar);
213
214 frame->Show(TRUE);
215 frame->SetCursor(wxCursor(wxCURSOR_HAND));
216
217 SetTopWindow(frame);
218
219 return TRUE;
220 }
221
222 //----------------------------------------------------------------------
223 // MyPanel
224 //----------------------------------------------------------------------
225
226 const int ID_NOTEBOOK = 1000;
227
228 const int ID_LISTBOX = 130;
229 const int ID_LISTBOX_SEL_NUM = 131;
230 const int ID_LISTBOX_SEL_STR = 132;
231 const int ID_LISTBOX_CLEAR = 133;
232 const int ID_LISTBOX_APPEND = 134;
233 const int ID_LISTBOX_DELETE = 135;
234 const int ID_LISTBOX_FONT = 136;
235 const int ID_LISTBOX_ENABLE = 137;
236 const int ID_LISTBOX_SORTED = 138;
237
238 const int ID_CHOICE = 120;
239 const int ID_CHOICE_SEL_NUM = 121;
240 const int ID_CHOICE_SEL_STR = 122;
241 const int ID_CHOICE_CLEAR = 123;
242 const int ID_CHOICE_APPEND = 124;
243 const int ID_CHOICE_DELETE = 125;
244 const int ID_CHOICE_FONT = 126;
245 const int ID_CHOICE_ENABLE = 127;
246 const int ID_CHOICE_SORTED = 128;
247
248 const int ID_COMBO = 140;
249 const int ID_COMBO_SEL_NUM = 141;
250 const int ID_COMBO_SEL_STR = 142;
251 const int ID_COMBO_CLEAR = 143;
252 const int ID_COMBO_APPEND = 144;
253 const int ID_COMBO_DELETE = 145;
254 const int ID_COMBO_FONT = 146;
255 const int ID_COMBO_ENABLE = 147;
256
257 const int ID_RADIOBOX = 160;
258 const int ID_RADIOBOX_SEL_NUM = 161;
259 const int ID_RADIOBOX_SEL_STR = 162;
260 const int ID_RADIOBOX_FONT = 163;
261 const int ID_RADIOBOX_ENABLE = 164;
262
263 const int ID_RADIOBUTTON_1 = 166;
264 const int ID_RADIOBUTTON_2 = 167;
265
266 const int ID_SET_FONT = 170;
267
268 const int ID_GAUGE = 180;
269 const int ID_SLIDER = 181;
270
271 const int ID_SPIN = 182;
272 const int ID_BTNPROGRESS = 183;
273 const int ID_BUTTON_LABEL = 184;
274
275 BEGIN_EVENT_TABLE(MyPanel, wxPanel)
276 EVT_SIZE ( MyPanel::OnSize)
277 EVT_NOTEBOOK_PAGE_CHANGING(ID_NOTEBOOK, MyPanel::OnPageChanging)
278 EVT_NOTEBOOK_PAGE_CHANGED(ID_NOTEBOOK, MyPanel::OnPageChanged)
279 EVT_LISTBOX (ID_LISTBOX, MyPanel::OnListBox)
280 EVT_LISTBOX (ID_LISTBOX_SORTED, MyPanel::OnListBox)
281 EVT_LISTBOX_DCLICK(ID_LISTBOX, MyPanel::OnListBoxDoubleClick)
282 EVT_BUTTON (ID_LISTBOX_SEL_NUM, MyPanel::OnListBoxButtons)
283 EVT_BUTTON (ID_LISTBOX_SEL_STR, MyPanel::OnListBoxButtons)
284 EVT_BUTTON (ID_LISTBOX_CLEAR, MyPanel::OnListBoxButtons)
285 EVT_BUTTON (ID_LISTBOX_APPEND, MyPanel::OnListBoxButtons)
286 EVT_BUTTON (ID_LISTBOX_DELETE, MyPanel::OnListBoxButtons)
287 EVT_BUTTON (ID_LISTBOX_FONT, MyPanel::OnListBoxButtons)
288 EVT_CHECKBOX (ID_LISTBOX_ENABLE, MyPanel::OnListBoxButtons)
289 EVT_CHOICE (ID_CHOICE, MyPanel::OnChoice)
290 EVT_CHOICE (ID_CHOICE_SORTED, MyPanel::OnChoice)
291 EVT_BUTTON (ID_CHOICE_SEL_NUM, MyPanel::OnChoiceButtons)
292 EVT_BUTTON (ID_CHOICE_SEL_STR, MyPanel::OnChoiceButtons)
293 EVT_BUTTON (ID_CHOICE_CLEAR, MyPanel::OnChoiceButtons)
294 EVT_BUTTON (ID_CHOICE_APPEND, MyPanel::OnChoiceButtons)
295 EVT_BUTTON (ID_CHOICE_DELETE, MyPanel::OnChoiceButtons)
296 EVT_BUTTON (ID_CHOICE_FONT, MyPanel::OnChoiceButtons)
297 EVT_CHECKBOX (ID_CHOICE_ENABLE, MyPanel::OnChoiceButtons)
298 EVT_COMBOBOX (ID_COMBO, MyPanel::OnCombo)
299 EVT_BUTTON (ID_COMBO_SEL_NUM, MyPanel::OnComboButtons)
300 EVT_BUTTON (ID_COMBO_SEL_STR, MyPanel::OnComboButtons)
301 EVT_BUTTON (ID_COMBO_CLEAR, MyPanel::OnComboButtons)
302 EVT_BUTTON (ID_COMBO_APPEND, MyPanel::OnComboButtons)
303 EVT_BUTTON (ID_COMBO_DELETE, MyPanel::OnComboButtons)
304 EVT_BUTTON (ID_COMBO_FONT, MyPanel::OnComboButtons)
305 EVT_CHECKBOX (ID_COMBO_ENABLE, MyPanel::OnComboButtons)
306 EVT_RADIOBOX (ID_RADIOBOX, MyPanel::OnRadio)
307 EVT_BUTTON (ID_RADIOBOX_SEL_NUM, MyPanel::OnRadioButtons)
308 EVT_BUTTON (ID_RADIOBOX_SEL_STR, MyPanel::OnRadioButtons)
309 EVT_BUTTON (ID_RADIOBOX_FONT, MyPanel::OnRadioButtons)
310 EVT_CHECKBOX (ID_RADIOBOX_ENABLE, MyPanel::OnRadioButtons)
311 EVT_BUTTON (ID_SET_FONT, MyPanel::OnSetFont)
312 EVT_SLIDER (ID_SLIDER, MyPanel::OnSliderUpdate)
313 #if wxUSE_SPINBUTTON
314 EVT_SPIN (ID_SPIN, MyPanel::OnSpinUpdate)
315 EVT_SPIN_UP (ID_SPIN, MyPanel::OnSpinUp)
316 EVT_SPIN_DOWN (ID_SPIN, MyPanel::OnSpinDown)
317 EVT_UPDATE_UI (ID_BTNPROGRESS, MyPanel::OnUpdateShowProgress)
318 EVT_BUTTON (ID_BTNPROGRESS, MyPanel::OnShowProgress)
319 #endif
320 EVT_BUTTON (ID_BUTTON_LABEL, MyPanel::OnUpdateLabel)
321 END_EVENT_TABLE()
322
323 MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h )
324 : wxPanel( frame, -1, wxPoint(x, y), wxSize(w, h) ),
325 m_text(NULL), m_notebook(NULL)
326 {
327 // SetBackgroundColour("cadet blue");
328
329 m_text = new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(0,50), wxSize(100,50), wxTE_MULTILINE );
330 // m_text->SetBackgroundColour("wheat");
331
332 delete wxLog::SetActiveTarget(new wxLogStderr);
333
334 m_notebook = new wxNotebook( this, ID_NOTEBOOK, wxPoint(0,0), wxSize(200,150) );
335
336 wxString choices[] =
337 {
338 "This",
339 "is one of my",
340 "really",
341 "wonderful",
342 "examples."
343 };
344
345 #ifdef USE_XPM
346 // image ids
347 enum
348 {
349 Image_List, Image_Choice, Image_Combo, Image_Text, Image_Radio, Image_Gauge, Image_Max
350 };
351
352 // fill the image list
353 wxImageList *imagelist = new wxImageList(32, 32);
354
355 imagelist-> Add( wxBitmap( list_xpm ));
356 imagelist-> Add( wxBitmap( choice_xpm ));
357 imagelist-> Add( wxBitmap( combo_xpm ));
358 imagelist-> Add( wxBitmap( text_xpm ));
359 imagelist-> Add( wxBitmap( radio_xpm ));
360 imagelist-> Add( wxBitmap( gauge_xpm ));
361 m_notebook->SetImageList(imagelist);
362 #elif defined(__WXMSW__)
363 // load images from resources
364 enum
365 {
366 Image_List, Image_Choice, Image_Combo, Image_Text, Image_Radio, Image_Gauge, Image_Max
367 };
368 wxImageList *imagelist = new wxImageList(16, 16, FALSE, Image_Max);
369
370 static const char *s_iconNames[Image_Max] =
371 {
372 "list", "choice", "combo", "text", "radio", "gauge"
373 };
374
375 for ( size_t n = 0; n < Image_Max; n++ )
376 {
377 wxBitmap bmp(s_iconNames[n]);
378 if ( !bmp.Ok() || (imagelist->Add(bmp) == -1) )
379 {
380 wxLogWarning("Couldn't load the image '%s' for the notebook page %d.",
381 s_iconNames[n], n);
382 }
383 }
384
385 m_notebook->SetImageList(imagelist);
386 #else
387
388 // No images for now
389 #define Image_List -1
390 #define Image_Choice -1
391 #define Image_Combo -1
392 #define Image_Text -1
393 #define Image_Radio -1
394 #define Image_Gauge -1
395 #define Image_Max -1
396
397 #endif
398
399 wxPanel *panel = new wxPanel(m_notebook);
400 m_listbox = new wxListBox( panel, ID_LISTBOX,
401 wxPoint(10,10), wxSize(120,70),
402 5, choices, wxLB_ALWAYS_SB );
403 m_listboxSorted = new wxListBox( panel, ID_LISTBOX_SORTED,
404 wxPoint(10,90), wxSize(120,70),
405 5, choices, wxLB_SORT );
406
407 SetControlClientData("listbox", m_listbox);
408 SetControlClientData("listbox", m_listboxSorted);
409
410 m_listbox->SetCursor(*wxCROSS_CURSOR);
411 #if wxUSE_TOOLTIPS
412 m_listbox->SetToolTip( "This is a list box" );
413 #endif // wxUSE_TOOLTIPS
414
415 m_lbSelectNum = new wxButton( panel, ID_LISTBOX_SEL_NUM, "Select #2", wxPoint(180,30), wxSize(140,30) );
416 m_lbSelectThis = new wxButton( panel, ID_LISTBOX_SEL_STR, "Select 'This'", wxPoint(340,30), wxSize(140,30) );
417 (void)new wxButton( panel, ID_LISTBOX_CLEAR, "Clear", wxPoint(180,80), wxSize(140,30) );
418 (void)new wxButton( panel, ID_LISTBOX_APPEND, "Append 'Hi!'", wxPoint(340,80), wxSize(140,30) );
419 (void)new wxButton( panel, ID_LISTBOX_DELETE, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
420 wxButton *button = new wxButton( panel, ID_LISTBOX_FONT, "Set &Italic font", wxPoint(340,130), wxSize(140,30) );
421 #if wxUSE_TOOLTIPS
422 button->SetToolTip( "Press here to set italic font" );
423 #endif // wxUSE_TOOLTIPS
424
425 m_checkbox = new wxCheckBox( panel, ID_LISTBOX_ENABLE, "&Disable", wxPoint(20,170) );
426 m_checkbox->SetValue(FALSE);
427 #if wxUSE_TOOLTIPS
428 m_checkbox->SetToolTip( "Click here to disable the listbox" );
429 #endif // wxUSE_TOOLTIPS
430 m_notebook->AddPage(panel, "wxListBox", TRUE, Image_List);
431
432 panel = new wxPanel(m_notebook);
433 m_choice = new wxChoice( panel, ID_CHOICE, wxPoint(10,10), wxSize(120,-1), 5, choices );
434 m_choiceSorted = new wxChoice( panel, ID_CHOICE_SORTED, wxPoint(10,70), wxSize(120,-1),
435 5, choices, wxCB_SORT );
436
437 SetControlClientData("choice", m_choice);
438 SetControlClientData("choice", m_choiceSorted);
439
440 m_choice->SetSelection(2);
441 m_choice->SetBackgroundColour( "red" );
442 (void)new wxButton( panel, ID_CHOICE_SEL_NUM, "Select #2", wxPoint(180,30), wxSize(140,30) );
443 (void)new wxButton( panel, ID_CHOICE_SEL_STR, "Select 'This'", wxPoint(340,30), wxSize(140,30) );
444 (void)new wxButton( panel, ID_CHOICE_CLEAR, "Clear", wxPoint(180,80), wxSize(140,30) );
445 (void)new wxButton( panel, ID_CHOICE_APPEND, "Append 'Hi!'", wxPoint(340,80), wxSize(140,30) );
446 (void)new wxButton( panel, ID_CHOICE_DELETE, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
447 (void)new wxButton( panel, ID_CHOICE_FONT, "Set Italic font", wxPoint(340,130), wxSize(140,30) );
448 (void)new wxCheckBox( panel, ID_CHOICE_ENABLE, "Disable", wxPoint(20,130), wxSize(140,30) );
449
450 m_notebook->AddPage(panel, "wxChoice", FALSE, Image_Choice);
451
452 panel = new wxPanel(m_notebook);
453 m_combo = new wxComboBox( panel, ID_COMBO, "This", wxPoint(10,10), wxSize(120,-1), 5, choices, wxCB_READONLY );
454 (void)new wxButton( panel, ID_COMBO_SEL_NUM, "Select #2", wxPoint(180,30), wxSize(140,30) );
455 (void)new wxButton( panel, ID_COMBO_SEL_STR, "Select 'This'", wxPoint(340,30), wxSize(140,30) );
456 (void)new wxButton( panel, ID_COMBO_CLEAR, "Clear", wxPoint(180,80), wxSize(140,30) );
457 (void)new wxButton( panel, ID_COMBO_APPEND, "Append 'Hi!'", wxPoint(340,80), wxSize(140,30) );
458 (void)new wxButton( panel, ID_COMBO_DELETE, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
459 (void)new wxButton( panel, ID_COMBO_FONT, "Set Italic font", wxPoint(340,130), wxSize(140,30) );
460 (void)new wxCheckBox( panel, ID_COMBO_ENABLE, "Disable", wxPoint(20,130), wxSize(140,30) );
461 m_notebook->AddPage(panel, "wxComboBox", FALSE, Image_Combo);
462
463 wxString choices2[] =
464 {
465 "First", "Second",
466 /* "Third",
467 "Fourth", "Fifth", "Sixth",
468 "Seventh", "Eighth", "Nineth", "Tenth" */
469 };
470
471 panel = new wxPanel(m_notebook);
472 (void)new wxRadioBox( panel, ID_RADIOBOX, "That", wxPoint(10,160), wxSize(-1,-1), WXSIZEOF(choices2), choices2, 1, wxRA_SPECIFY_ROWS );
473 m_radio = new wxRadioBox( panel, ID_RADIOBOX, "This", wxPoint(10,10), wxSize(-1,-1), WXSIZEOF(choices), choices, 1, wxRA_SPECIFY_COLS );
474 (void)new wxButton( panel, ID_RADIOBOX_SEL_NUM, "Select #2", wxPoint(180,30), wxSize(140,30) );
475 (void)new wxButton( panel, ID_RADIOBOX_SEL_STR, "Select 'This'", wxPoint(180,80), wxSize(140,30) );
476 m_fontButton = new wxButton( panel, ID_SET_FONT, "Set more Italic font", wxPoint(340,30), wxSize(140,30) );
477 (void)new wxButton( panel, ID_RADIOBOX_FONT, "Set Italic font", wxPoint(340,80), wxSize(140,30) );
478 (void)new wxCheckBox( panel, ID_RADIOBOX_ENABLE, "Disable", wxPoint(340,130), wxSize(140,30) );
479 wxRadioButton *rb = new wxRadioButton( panel, ID_RADIOBUTTON_1, "Radiobutton1", wxPoint(210,170), wxSize(110,30), wxRB_GROUP );
480 rb->SetValue( FALSE );
481 (void)new wxRadioButton( panel, ID_RADIOBUTTON_2, "Radiobutton2", wxPoint(340,170), wxSize(110,30) );
482 m_notebook->AddPage(panel, "wxRadioBox", FALSE, Image_Radio);
483
484 panel = new wxPanel(m_notebook);
485 (void)new wxStaticBox( panel, -1, "wxGauge and wxSlider", wxPoint(10,10), wxSize(200,130) );
486 m_gauge = new wxGauge( panel, -1, 200, wxPoint(18,50), wxSize(155, 30) );
487 m_slider = new wxSlider( panel, ID_SLIDER, 0, 0, 200, wxPoint(18,90), wxSize(155,-1), wxSL_LABELS );
488 (void)new wxStaticBox( panel, -1, "Explanation", wxPoint(220,10), wxSize(270,130) );
489 #ifdef __WXMOTIF__
490 // No wrapping text in wxStaticText yet :-(
491 (void)new wxStaticText( panel, -1,
492 "Drag the slider!",
493 wxPoint(228,30),
494 wxSize(240, -1)
495 );
496 #else
497 (void)new wxStaticText( panel, -1,
498 "In order see the gauge (aka progress bar)\n"
499 "control do something you have to drag the\n"
500 "handle of the slider to the right.\n"
501 "\n"
502 "This is also supposed to demonstrate how\n"
503 "to use static controls.\n",
504 wxPoint(228,25),
505 wxSize(240, 110)
506 );
507 #endif
508 int initialSpinValue = -5;
509 wxString s;
510 s << initialSpinValue;
511 m_spintext = new wxTextCtrl( panel, -1, s, wxPoint(20,160), wxSize(80,-1) );
512 #if wxUSE_SPINBUTTON
513 m_spinbutton = new wxSpinButton( panel, ID_SPIN, wxPoint(103,160), wxSize(80, -1) );
514 m_spinbutton->SetRange(-10,30);
515 m_spinbutton->SetValue(initialSpinValue);
516
517 m_btnProgress = new wxButton( panel, ID_BTNPROGRESS, "Show progress dialog",
518 wxPoint(300, 160) );
519 #endif // wxUSE_SPINBUTTON
520
521 #if wxUSE_SPINCTRL
522 m_spinctrl = new wxSpinCtrl( panel, -1, wxPoint(200, 160), wxSize(80, -1) );
523 m_spinctrl->SetRange(10,30);
524 m_spinctrl->SetValue(15);
525 #endif // wxUSE_SPINCTRL
526
527 m_notebook->AddPage(panel, "wxGauge", FALSE, Image_Gauge);
528
529 panel = new wxPanel(m_notebook);
530
531 #ifndef __WXMOTIF__ // wxStaticBitmap not working under Motif yet. MB
532 wxIcon icon = wxTheApp->GetStdIcon(wxICON_INFORMATION);
533 wxStaticBitmap *bmpStatic = new wxStaticBitmap(panel, -1, icon, wxPoint(10, 10));
534
535 bmpStatic = new wxStaticBitmap(panel, -1, wxNullIcon, wxPoint(50, 10));
536 bmpStatic->SetIcon(wxTheApp->GetStdIcon(wxICON_QUESTION));
537 #endif // !Motif
538
539 wxBitmap bitmap( 100, 100 );
540 wxMemoryDC dc;
541 dc.SelectObject( bitmap );
542 dc.SetPen(*wxGREEN_PEN);
543 dc.DrawEllipse(5, 5, 90, 90);
544 dc.DrawText("Bitmap", 20, 20);
545 dc.SelectObject( wxNullBitmap );
546
547 (void)new wxBitmapButton(panel, -1, bitmap, wxPoint(100, 20));
548 (void)new wxButton(panel, ID_BUTTON_LABEL, "Toggle label", wxPoint(250, 20));
549 m_label = new wxStaticText(panel, -1, "Label with some long text",
550 wxPoint(250, 60), wxDefaultSize,
551 wxALIGN_RIGHT | wxST_NO_AUTORESIZE);
552
553 m_notebook->AddPage(panel, "wxBitmapXXX");
554
555 // --------------- TEST CODE ----------------------
556
557 // layout constraints
558
559 panel = new wxPanel(m_notebook);
560 panel->SetAutoLayout( true );
561
562 wxLayoutConstraints *c;
563 c = new wxLayoutConstraints;
564 c->top.SameAs( panel, wxTop, 10 );
565 c->height.AsIs( );
566 c->left.SameAs( panel, wxLeft, 10 );
567 c->width.PercentOf( panel, wxWidth, 40 );
568
569 wxButton *pMyButton = new wxButton(panel, -1, "Test Button" );
570 pMyButton->SetConstraints( c );
571
572 c = new wxLayoutConstraints;
573 c->top.SameAs( panel, wxTop, 10 );
574 c->bottom.SameAs( panel, wxBottom, 10 );
575 c->right.SameAs( panel, wxRight, 10 );
576 c->width.PercentOf( panel, wxWidth, 40 );
577
578 wxButton *pMyButton2 = new wxButton(panel, -1, "Test Button 2" );
579 pMyButton2->SetConstraints( c );
580
581 m_notebook->AddPage(panel, "wxLayoutConstraint");
582
583 // sizer
584
585 panel = new wxPanel(m_notebook);
586 panel->SetAutoLayout( true );
587
588 wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
589
590 sizer->Add( new wxButton(panel, -1, "Test Button" ), 3, wxALL, 10 );
591 sizer->Add( 20,20, 1 );
592 sizer->Add( new wxButton(panel, -1, "Test Button 2" ), 3, wxGROW|wxALL, 10 );
593
594 panel->SetSizer( sizer );
595
596 m_notebook->AddPage(panel, "wxSizer");
597
598 // --------------- TEST CODE ----------------------
599
600 }
601
602 void MyPanel::OnSize( wxSizeEvent& WXUNUSED(event) )
603 {
604 int x = 0;
605 int y = 0;
606 GetClientSize( &x, &y );
607
608 if (m_notebook) m_notebook->SetSize( 2, 2, x-4, y*2/3-4 );
609 if (m_text) m_text->SetSize( 2, y*2/3+2, x-4, y/3-4 );
610 }
611
612 void MyPanel::OnPageChanging( wxNotebookEvent &event )
613 {
614 int selOld = event.GetOldSelection();
615 if ( selOld == 2 )
616 {
617 if ( wxMessageBox("This demonstrates how a program may prevent the\n"
618 "page change from taking place - if you select\n"
619 "[No] the current page will stay the third one\n",
620 "Control sample",
621 wxICON_QUESTION | wxYES_NO) != wxYES )
622 {
623 event.Veto();
624
625 return;
626 }
627 }
628
629 *m_text << "Notebook selection is being changed from " << selOld << "\n";
630 }
631
632 void MyPanel::OnPageChanged( wxNotebookEvent &event )
633 {
634 *m_text << "Notebook selection is " << event.GetSelection() << "\n";
635 }
636
637 void MyPanel::OnListBox( wxCommandEvent &event )
638 {
639 wxListBox *listbox = event.GetId() == ID_LISTBOX ? m_listbox
640 : m_listboxSorted;
641
642 m_text->AppendText( "ListBox event selection string is: '" );
643 m_text->AppendText( event.GetString() );
644 m_text->AppendText( "'\n" );
645 m_text->AppendText( "ListBox control selection string is: '" );
646 m_text->AppendText( listbox->GetStringSelection() );
647 m_text->AppendText( "'\n" );
648
649 wxStringClientData *obj = ((wxStringClientData *)event.GetClientObject());
650 m_text->AppendText( "ListBox event client data string is: '" );
651 m_text->AppendText( obj ? obj->GetData() : wxString("none"));
652 m_text->AppendText( "'\n" );
653 m_text->AppendText( "ListBox control client data string is: '" );
654 obj = (wxStringClientData *)listbox->GetClientObject(listbox->GetSelection());
655 m_text->AppendText( obj ? obj->GetData() : wxString("none"));
656 m_text->AppendText( "'\n" );
657 }
658
659 void MyPanel::OnListBoxDoubleClick( wxCommandEvent &event )
660 {
661 m_text->AppendText( "ListBox double click string is: " );
662 m_text->AppendText( event.GetString() );
663 m_text->AppendText( "\n" );
664 }
665
666 void MyPanel::OnListBoxButtons( wxCommandEvent &event )
667 {
668 switch (event.GetId())
669 {
670 case ID_LISTBOX_ENABLE:
671 {
672 m_text->AppendText("Checkbox clicked.\n");
673 wxCheckBox *cb = (wxCheckBox*)event.GetEventObject();
674 #if wxUSE_TOOLTIPS
675 if (event.GetInt())
676 cb->SetToolTip( "Click to enable listbox" );
677 else
678 cb->SetToolTip( "Click to disable listbox" );
679 #endif // wxUSE_TOOLTIPS
680 m_listbox->Enable( event.GetInt() == 0 );
681 m_listboxSorted->Enable( event.GetInt() == 0 );
682 break;
683 }
684 case ID_LISTBOX_SEL_NUM:
685 {
686 m_listbox->SetSelection( 2 );
687 m_listboxSorted->SetSelection( 2 );
688 m_lbSelectThis->WarpPointer( 40, 14 );
689 break;
690 }
691 case ID_LISTBOX_SEL_STR:
692 {
693 m_listbox->SetStringSelection( "This" );
694 m_listboxSorted->SetStringSelection( "This" );
695 m_lbSelectNum->WarpPointer( 40, 14 );
696 break;
697 }
698 case ID_LISTBOX_CLEAR:
699 {
700 m_listbox->Clear();
701 m_listboxSorted->Clear();
702 break;
703 }
704 case ID_LISTBOX_APPEND:
705 {
706 m_listbox->Append( "Hi!" );
707 m_listboxSorted->Append( "Hi!" );
708 break;
709 }
710 case ID_LISTBOX_DELETE:
711 {
712 int idx;
713 idx = m_listbox->GetSelection();
714 m_listbox->Delete( idx );
715 idx = m_listboxSorted->GetSelection();
716 m_listboxSorted->Delete( idx );
717 break;
718 }
719 case ID_LISTBOX_FONT:
720 {
721 m_listbox->SetFont( *wxITALIC_FONT );
722 m_listboxSorted->SetFont( *wxITALIC_FONT );
723 m_checkbox->SetFont( *wxITALIC_FONT );
724 break;
725 }
726 }
727 }
728
729 void MyPanel::OnChoice( wxCommandEvent &event )
730 {
731 wxChoice *choice = event.GetId() == ID_CHOICE ? m_choice
732 : m_choiceSorted;
733
734 m_text->AppendText( "Choice event selection string is: '" );
735 m_text->AppendText( event.GetString() );
736 m_text->AppendText( "'\n" );
737 m_text->AppendText( "Choice control selection string is: '" );
738 m_text->AppendText( choice->GetStringSelection() );
739 m_text->AppendText( "'\n" );
740
741 wxStringClientData *obj = ((wxStringClientData *)event.GetClientObject());
742 m_text->AppendText( "Choice event client data string is: '" );
743 m_text->AppendText( obj ? obj->GetData() : wxString("none"));
744 m_text->AppendText( "'\n" );
745 m_text->AppendText( "Choice control client data string is: '" );
746 obj = (wxStringClientData *)choice->GetClientObject(choice->GetSelection());
747 m_text->AppendText( obj ? obj->GetData() : wxString("none"));
748 m_text->AppendText( "'\n" );
749 }
750
751 void MyPanel::OnChoiceButtons( wxCommandEvent &event )
752 {
753 switch (event.GetId())
754 {
755 case ID_CHOICE_ENABLE:
756 {
757 m_choice->Enable( event.GetInt() == 0 );
758 m_choiceSorted->Enable( event.GetInt() == 0 );
759 break;
760 }
761 case ID_CHOICE_SEL_NUM:
762 {
763 m_choice->SetSelection( 2 );
764 m_choiceSorted->SetSelection( 2 );
765 break;
766 }
767 case ID_CHOICE_SEL_STR:
768 {
769 m_choice->SetStringSelection( "This" );
770 m_choiceSorted->SetStringSelection( "This" );
771 break;
772 }
773 case ID_CHOICE_CLEAR:
774 {
775 m_choice->Clear();
776 m_choiceSorted->Clear();
777 break;
778 }
779 case ID_CHOICE_APPEND:
780 {
781 m_choice->Append( "Hi!" );
782 m_choiceSorted->Append( "Hi!" );
783 break;
784 }
785 case ID_CHOICE_DELETE:
786 {
787 int idx = m_choice->GetSelection();
788 m_choice->Delete( idx );
789 idx = m_choiceSorted->GetSelection();
790 m_choiceSorted->Delete( idx );
791 break;
792 }
793 case ID_CHOICE_FONT:
794 {
795 m_choice->SetFont( *wxITALIC_FONT );
796 m_choiceSorted->SetFont( *wxITALIC_FONT );
797 break;
798 }
799 }
800 }
801
802 void MyPanel::OnCombo( wxCommandEvent &event )
803 {
804 m_text->AppendText( "ComboBox event selection string is: " );
805 m_text->AppendText( event.GetString() );
806 m_text->AppendText( "\n" );
807 m_text->AppendText( "ComboBox control selection string is: " );
808 m_text->AppendText( m_combo->GetStringSelection() );
809 m_text->AppendText( "\n" );
810 }
811
812 void MyPanel::OnComboButtons( wxCommandEvent &event )
813 {
814 switch (event.GetId())
815 {
816 case ID_COMBO_ENABLE:
817 {
818 m_combo->Enable( event.GetInt() == 0 );
819 break;
820 }
821 case ID_COMBO_SEL_NUM:
822 {
823 m_combo->SetSelection( 2 );
824 break;
825 }
826 case ID_COMBO_SEL_STR:
827 {
828 m_combo->SetStringSelection( "This" );
829 break;
830 }
831 case ID_COMBO_CLEAR:
832 {
833 m_combo->Clear();
834 break;
835 }
836 case ID_COMBO_APPEND:
837 {
838 m_combo->Append( "Hi!" );
839 break;
840 }
841 case ID_COMBO_DELETE:
842 {
843 int idx = m_combo->GetSelection();
844 m_combo->Delete( idx );
845 break;
846 }
847 case ID_COMBO_FONT:
848 {
849 m_combo->SetFont( *wxITALIC_FONT );
850 break;
851 }
852 }
853 }
854
855 void MyPanel::OnRadio( wxCommandEvent &event )
856 {
857 m_text->AppendText( "RadioBox selection string is: " );
858 m_text->AppendText( event.GetString() );
859 m_text->AppendText( "\n" );
860 }
861
862 void MyPanel::OnRadioButtons( wxCommandEvent &event )
863 {
864 switch (event.GetId())
865 {
866 case ID_RADIOBOX_ENABLE:
867 {
868 m_radio->Enable( event.GetInt() == 0 );
869 break;
870 }
871 case ID_RADIOBOX_SEL_NUM:
872 {
873 m_radio->SetSelection( 2 );
874 break;
875 }
876 case ID_RADIOBOX_SEL_STR:
877 {
878 m_radio->SetStringSelection( "This" );
879 break;
880 }
881 case ID_RADIOBOX_FONT:
882 {
883 m_radio->SetFont( *wxITALIC_FONT );
884 break;
885 }
886 }
887 }
888
889 void MyPanel::OnSetFont( wxCommandEvent &WXUNUSED(event) )
890 {
891 m_fontButton->SetFont( *wxITALIC_FONT );
892 m_text->SetFont( *wxITALIC_FONT );
893 }
894
895 void MyPanel::OnUpdateLabel( wxCommandEvent &WXUNUSED(event) )
896 {
897 static bool s_long = TRUE;
898
899 s_long = !s_long;
900 m_label->SetLabel(s_long ? "very very very long text" : "shorter text");
901 }
902
903 void MyPanel::OnSliderUpdate( wxCommandEvent &WXUNUSED(event) )
904 {
905 m_gauge->SetValue( m_slider->GetValue() );
906 }
907
908 #if wxUSE_SPINBUTTON
909 void MyPanel::OnSpinUp( wxSpinEvent &event )
910 {
911 wxString value;
912 value.Printf( _T("Spin control up: current = %d\n"),
913 m_spinbutton->GetValue());
914
915 if ( m_spinbutton->GetValue() > 17 )
916 {
917 value += _T("Preventing the spin button from going above 17.\n");
918
919 event.Veto();
920 }
921
922 m_text->AppendText(value);
923 }
924
925 void MyPanel::OnSpinDown( wxSpinEvent &event )
926 {
927 wxString value;
928 value.Printf( _T("Spin control down: current = %d\n"),
929 m_spinbutton->GetValue());
930
931 if ( m_spinbutton->GetValue() < -17 )
932 {
933 value += _T("Preventing the spin button from going below -17.\n");
934
935 event.Veto();
936 }
937
938 m_text->AppendText(value);
939 }
940
941 void MyPanel::OnSpinUpdate( wxSpinEvent &event )
942 {
943 wxString value;
944 value.Printf( _T("%d"), event.GetPosition() );
945 m_spintext->SetValue( value );
946
947 value.Printf( _T("Spin control range: (%d, %d), current = %d\n"),
948 m_spinbutton->GetMin(), m_spinbutton->GetMax(),
949 m_spinbutton->GetValue());
950
951 m_text->AppendText(value);
952 }
953
954 void MyPanel::OnUpdateShowProgress( wxUpdateUIEvent& event )
955 {
956 event.Enable( m_spinbutton->GetValue() > 0 );
957 }
958
959 void MyPanel::OnShowProgress( wxCommandEvent& WXUNUSED(event) )
960 {
961 int max = m_spinbutton->GetValue();
962 wxProgressDialog dialog("Progress dialog example",
963 "An informative message",
964 max, // range
965 this, // parent
966 wxPD_CAN_ABORT |
967 wxPD_APP_MODAL |
968 wxPD_ELAPSED_TIME |
969 wxPD_ESTIMATED_TIME |
970 wxPD_REMAINING_TIME);
971
972
973 bool cont = TRUE;
974 for ( int i = 0; i < max && cont; i++ )
975 {
976 wxSleep(1);
977 if ( i == max - 1 )
978 {
979 cont = dialog.Update(i, "That's all, folks!");
980 }
981 else if ( i == max / 2 )
982 {
983 cont = dialog.Update(i, "Only a half left!");
984 }
985 else
986 {
987 cont = dialog.Update(i);
988 }
989 }
990
991 if ( !cont )
992 {
993 *m_text << "Progress dialog aborted!\n";
994 }
995 else
996 {
997 *m_text << "Countdown from " << max << " finished.\n";
998 }
999 }
1000
1001 #endif // wxUSE_SPINBUTTON
1002
1003 MyPanel::~MyPanel()
1004 {
1005 delete m_notebook->GetImageList();
1006 }
1007
1008 //----------------------------------------------------------------------
1009 // MyFrame
1010 //----------------------------------------------------------------------
1011
1012 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
1013 EVT_MENU(MINIMAL_QUIT, MyFrame::OnQuit)
1014 EVT_MENU(MINIMAL_ABOUT, MyFrame::OnAbout)
1015 #if wxUSE_TOOLTIPS
1016 EVT_MENU(MINIMAL_SET_TOOLTIP_DELAY, MyFrame::OnSetTooltipDelay)
1017 EVT_MENU(MINIMAL_ENABLE_TOOLTIPS, MyFrame::OnToggleTooltips)
1018 #endif // wxUSE_TOOLTIPS
1019 EVT_SIZE(MyFrame::OnSize)
1020 EVT_IDLE(MyFrame::OnIdle)
1021 END_EVENT_TABLE()
1022
1023 MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h)
1024 : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
1025 {
1026 CreateStatusBar(2);
1027
1028 (void)new MyPanel( this, 10, 10, 300, 100 );
1029 }
1030
1031 void MyFrame::OnQuit (wxCommandEvent& WXUNUSED(event) )
1032 {
1033 Close(TRUE);
1034 }
1035
1036 void MyFrame::OnAbout( wxCommandEvent& WXUNUSED(event) )
1037 {
1038 wxBeginBusyCursor();
1039
1040 wxMessageDialog dialog(this, "This is a control sample", "About Controls", wxOK );
1041 dialog.ShowModal();
1042
1043 wxEndBusyCursor();
1044 }
1045
1046 #if wxUSE_TOOLTIPS
1047 void MyFrame::OnSetTooltipDelay(wxCommandEvent& event)
1048 {
1049 static long s_delay = 5000;
1050
1051 wxString delay;
1052 delay.Printf( _T("%ld"), s_delay);
1053
1054 delay = wxGetTextFromUser("Enter delay (in milliseconds)",
1055 "Set tooltip delay",
1056 delay,
1057 this);
1058 if ( !delay )
1059 return; // cancelled
1060
1061 wxSscanf(delay, _T("%ld"), &s_delay);
1062
1063 wxToolTip::SetDelay(s_delay);
1064
1065 wxLogStatus(this, _T("Tooltip delay set to %ld milliseconds"), s_delay);
1066 }
1067
1068 void MyFrame::OnToggleTooltips(wxCommandEvent& event)
1069 {
1070 static bool s_enabled = TRUE;
1071
1072 s_enabled = !s_enabled;
1073
1074 wxToolTip::Enable(s_enabled);
1075
1076 wxLogStatus(this, _T("Tooltips %sabled"), s_enabled ? _T("en") : _T("dis") );
1077 }
1078 #endif // tooltips
1079
1080 void MyFrame::OnSize( wxSizeEvent& event )
1081 {
1082 wxString msg;
1083 msg.Printf( _("%dx%d"), event.GetSize().x, event.GetSize().y);
1084 SetStatusText(msg, 1);
1085
1086 event.Skip();
1087 }
1088
1089 void MyFrame::OnIdle( wxIdleEvent& WXUNUSED(event) )
1090 {
1091 // track the window which has the focus in the status bar
1092 static wxWindow *s_windowFocus = (wxWindow *)NULL;
1093 wxWindow *focus = wxWindow::FindFocus();
1094 if ( focus && (focus != s_windowFocus) )
1095 {
1096 s_windowFocus = focus;
1097
1098 wxString msg;
1099 msg.Printf(
1100 #ifdef __WXMSW__
1101 _T("Focus: wxWindow = %p, HWND = %08x"),
1102 #else
1103 _T("Focus: wxWindow = %p"),
1104 #endif
1105 s_windowFocus
1106 #ifdef __WXMSW__
1107 , s_windowFocus->GetHWND()
1108 #endif
1109 );
1110
1111 SetStatusText(msg);
1112 }
1113 }
1114
1115 static void SetControlClientData(const char *name,
1116 wxControlWithItems *control)
1117 {
1118 size_t count = control->GetCount();
1119 for ( size_t n = 0; n < count; n++ )
1120 {
1121 wxString s;
1122 s.Printf("%s client data for '%s'",
1123 name, control->GetString(n).c_str());
1124
1125 control->SetClientObject(n, new wxStringClientData(s));
1126 }
1127 }