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