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