made it compile with wxUniv (which doesn't have tooltips)
[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/tglbtn.h"
30 #include "wx/notebook.h"
31 #include "wx/imaglist.h"
32 #include "wx/artprov.h"
33
34 #if wxUSE_TOOLTIPS
35 #include "wx/tooltip.h"
36 #endif
37
38 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
39 #define USE_XPM
40 #endif
41
42 #ifdef USE_XPM
43 #include "mondrian.xpm"
44 #include "icons/choice.xpm"
45 #include "icons/combo.xpm"
46 #include "icons/list.xpm"
47 #include "icons/radio.xpm"
48 #include "icons/text.xpm"
49 #include "icons/gauge.xpm"
50 #endif
51
52 #ifdef __WIN16__
53 // Win16 doesn't have them
54 #ifdef wxUSE_SPINBTN
55 #undef wxUSE_SPINBTN
56 #endif
57 #define wxUSE_SPINBTN 0
58 #else
59 #ifndef wxUSE_SPINBTN
60 #define wxUSE_SPINBTN 1
61 #endif
62 #endif // __WIN16__
63
64 #include "wx/progdlg.h"
65
66 #if wxUSE_SPINCTRL
67 #include "wx/spinctrl.h"
68 #endif // wxUSE_SPINCTRL
69
70 //----------------------------------------------------------------------
71 // class definitions
72 //----------------------------------------------------------------------
73
74 class MyApp: public wxApp
75 {
76 public:
77 bool OnInit();
78 };
79
80 class MyPanel: public wxPanel
81 {
82 public:
83 MyPanel(wxFrame *frame, int x, int y, int w, int h);
84 virtual ~MyPanel();
85
86 void OnSize( wxSizeEvent& event );
87 void OnListBox( wxCommandEvent &event );
88 void OnListBoxDoubleClick( wxCommandEvent &event );
89 void OnListBoxButtons( wxCommandEvent &event );
90 #if wxUSE_CHOICE
91 void OnChoice( wxCommandEvent &event );
92 void OnChoiceButtons( wxCommandEvent &event );
93 #endif
94 void OnCombo( wxCommandEvent &event );
95 void OnComboTextChanged( wxCommandEvent &event );
96 void OnComboTextEnter( wxCommandEvent &event );
97 void OnComboButtons( wxCommandEvent &event );
98 void OnRadio( wxCommandEvent &event );
99 void OnRadioButtons( wxCommandEvent &event );
100 void OnSetFont( wxCommandEvent &event );
101 void OnPageChanged( wxNotebookEvent &event );
102 void OnPageChanging( wxNotebookEvent &event );
103 void OnSliderUpdate( wxCommandEvent &event );
104 void OnUpdateLabel( wxCommandEvent &event );
105 #if wxUSE_SPINBTN
106 void OnSpinUp( wxSpinEvent &event );
107 void OnSpinDown( wxSpinEvent &event );
108 void OnSpinUpdate( wxSpinEvent &event );
109 void OnUpdateShowProgress( wxUpdateUIEvent& event );
110 void OnShowProgress( wxCommandEvent &event );
111 #endif // wxUSE_SPINBTN
112
113 #if wxUSE_SPINCTRL
114 void OnSpinCtrl(wxSpinEvent& event);
115 void OnSpinCtrlUp(wxSpinEvent& event);
116 void OnSpinCtrlDown(wxSpinEvent& event);
117 void OnSpinCtrlText(wxCommandEvent& event);
118 #endif // wxUSE_SPINCTRL
119
120 void OnEnableAll(wxCommandEvent& event);
121 void OnChangeColour(wxCommandEvent& event);
122 void OnTestButton(wxCommandEvent& event);
123 void OnBmpButton(wxCommandEvent& event);
124
125 wxListBox *m_listbox,
126 *m_listboxSorted;
127 #if wxUSE_CHOICE
128 wxChoice *m_choice,
129 *m_choiceSorted;
130 #endif
131 wxComboBox *m_combo;
132 wxRadioBox *m_radio;
133 wxGauge *m_gauge,
134 *m_gaugeVert;
135 wxSlider *m_slider;
136 wxButton *m_fontButton;
137 wxButton *m_lbSelectNum;
138 wxButton *m_lbSelectThis;
139 #if wxUSE_SPINBTN
140 wxSpinButton *m_spinbutton;
141 wxButton *m_btnProgress;
142 #endif // wxUSE_SPINBTN
143
144 #if wxUSE_SPINCTRL
145 wxSpinCtrl *m_spinctrl;
146 #endif // wxUSE_SPINCTRL
147
148 wxTextCtrl *m_spintext;
149 wxCheckBox *m_checkbox;
150
151 wxTextCtrl *m_text;
152 wxNotebook *m_notebook;
153
154 wxStaticText *m_label;
155
156 private:
157 wxLog *m_logTargetOld;
158
159 DECLARE_EVENT_TABLE()
160 };
161
162 class MyFrame: public wxFrame
163 {
164 public:
165 MyFrame(const char *title, int x, int y);
166
167 void OnQuit(wxCommandEvent& event);
168 void OnAbout(wxCommandEvent& event);
169 void OnClearLog(wxCommandEvent& event);
170
171 #if wxUSE_TOOLTIPS
172 void OnSetTooltipDelay(wxCommandEvent& event);
173 void OnToggleTooltips(wxCommandEvent& event);
174 #endif // wxUSE_TOOLTIPS
175
176 void OnEnableAll(wxCommandEvent& event);
177
178 void OnIdle( wxIdleEvent& event );
179 void OnSize( wxSizeEvent& event );
180 void OnMove( wxMoveEvent& event );
181
182 MyPanel *GetPanel() const { return m_panel; }
183
184 private:
185 void UpdateStatusBar(const wxPoint& pos, const wxSize& size)
186 {
187 if ( m_frameStatusBar )
188 {
189 wxString msg;
190 wxSize sizeAll = GetSize(),
191 sizeCl = GetClientSize();
192 msg.Printf(_("pos=(%d, %d), size=%dx%d or %dx%d (client=%dx%d)"),
193 pos.x, pos.y,
194 size.x, size.y,
195 sizeAll.x, sizeAll.y,
196 sizeCl.x, sizeCl.y);
197 SetStatusText(msg, 1);
198 }
199 }
200
201 MyPanel *m_panel;
202
203 DECLARE_EVENT_TABLE()
204 };
205
206 // a button which intercepts double clicks (for testing...)
207 class MyButton : public wxButton
208 {
209 public:
210 MyButton(wxWindow *parent,
211 wxWindowID id,
212 const wxString& label = wxEmptyString,
213 const wxPoint& pos = wxDefaultPosition,
214 const wxSize& size = wxDefaultSize)
215 : wxButton(parent, id, label, pos, size)
216 {
217 }
218
219 void OnDClick(wxMouseEvent& event)
220 {
221 wxLogMessage(_T("MyButton::OnDClick"));
222
223 event.Skip();
224 }
225
226 private:
227 DECLARE_EVENT_TABLE()
228 };
229
230 // a combo which intercepts chars (to test Windows behaviour)
231 class MyComboBox : public wxComboBox
232 {
233 public:
234 MyComboBox(wxWindow *parent, wxWindowID id,
235 const wxString& value = wxEmptyString,
236 const wxPoint& pos = wxDefaultPosition,
237 const wxSize& size = wxDefaultSize,
238 int n = 0, const wxString choices[] = NULL,
239 long style = 0,
240 const wxValidator& validator = wxDefaultValidator,
241 const wxString& name = wxComboBoxNameStr)
242 : wxComboBox(parent, id, value, pos, size, n, choices, style,
243 validator, name) { }
244
245 protected:
246 void OnChar(wxKeyEvent& event);
247 void OnKeyDown(wxKeyEvent& event);
248 void OnKeyUp(wxKeyEvent& event);
249 void OnFocusGot(wxFocusEvent& event)
250 {
251 wxLogMessage(_T("MyComboBox::OnFocusGot"));
252
253 event.Skip();
254 }
255
256 private:
257 DECLARE_EVENT_TABLE()
258 };
259
260 // a radiobox which handles focus set/kill (for testing)
261 class MyRadioBox : public wxRadioBox
262 {
263 public:
264 MyRadioBox(wxWindow *parent,
265 wxWindowID id,
266 const wxString& title = wxEmptyString,
267 const wxPoint& pos = wxDefaultPosition,
268 const wxSize& size = wxDefaultSize,
269 int n = 0, const wxString choices[] = NULL,
270 int majorDim = 1,
271 long style = wxRA_HORIZONTAL,
272 const wxValidator& validator = wxDefaultValidator,
273 const wxString& name = wxRadioBoxNameStr)
274 : wxRadioBox(parent, id, title, pos, size, n, choices, majorDim,
275 style, validator, name) { SetForegroundColour(*wxRED); }
276
277 protected:
278 void OnFocusGot(wxFocusEvent& event)
279 {
280 wxLogMessage(_T("MyRadioBox::OnFocusGot"));
281
282 event.Skip();
283 }
284
285 void OnFocusLost(wxFocusEvent& event)
286 {
287 wxLogMessage(_T("MyRadioBox::OnFocusLost"));
288
289 event.Skip();
290 }
291
292 private:
293 DECLARE_EVENT_TABLE()
294 };
295
296 //----------------------------------------------------------------------
297 // other
298 //----------------------------------------------------------------------
299
300 static void SetListboxClientData(const wxChar *name, wxListBox *control);
301 static void SetChoiceClientData(const wxChar *name, wxChoice *control);
302
303 IMPLEMENT_APP(MyApp)
304
305 //----------------------------------------------------------------------
306 // MyApp
307 //----------------------------------------------------------------------
308
309 enum
310 {
311 CONTROLS_QUIT = 100,
312 CONTROLS_TEXT,
313 CONTROLS_ABOUT,
314 CONTROLS_CLEAR_LOG,
315
316 // tooltip menu
317 CONTROLS_SET_TOOLTIP_DELAY = 200,
318 CONTROLS_ENABLE_TOOLTIPS,
319
320 // panel menu
321 CONTROLS_ENABLE_ALL
322 };
323
324 bool MyApp::OnInit()
325 {
326 // use standard command line handling:
327 if ( !wxApp::OnInit() )
328 return FALSE;
329
330 // parse the cmd line
331 int x = 50,
332 y = 50;
333 if ( argc == 3 )
334 {
335 wxSscanf(wxString(argv[1]), wxT("%d"), &x);
336 wxSscanf(wxString(argv[2]), wxT("%d"), &y);
337 }
338
339 // Create the main frame window
340 MyFrame *frame = new MyFrame("Controls wxWindows App", x, y);
341 frame->Show(TRUE);
342
343 return TRUE;
344 }
345
346 //----------------------------------------------------------------------
347 // MyPanel
348 //----------------------------------------------------------------------
349
350 const int ID_NOTEBOOK = 1000;
351
352 const int ID_LISTBOX = 130;
353 const int ID_LISTBOX_SEL_NUM = 131;
354 const int ID_LISTBOX_SEL_STR = 132;
355 const int ID_LISTBOX_CLEAR = 133;
356 const int ID_LISTBOX_APPEND = 134;
357 const int ID_LISTBOX_DELETE = 135;
358 const int ID_LISTBOX_FONT = 136;
359 const int ID_LISTBOX_ENABLE = 137;
360 const int ID_LISTBOX_SORTED = 138;
361
362 const int ID_CHOICE = 120;
363 const int ID_CHOICE_SEL_NUM = 121;
364 const int ID_CHOICE_SEL_STR = 122;
365 const int ID_CHOICE_CLEAR = 123;
366 const int ID_CHOICE_APPEND = 124;
367 const int ID_CHOICE_DELETE = 125;
368 const int ID_CHOICE_FONT = 126;
369 const int ID_CHOICE_ENABLE = 127;
370 const int ID_CHOICE_SORTED = 128;
371
372 const int ID_COMBO = 140;
373 const int ID_COMBO_SEL_NUM = 141;
374 const int ID_COMBO_SEL_STR = 142;
375 const int ID_COMBO_CLEAR = 143;
376 const int ID_COMBO_APPEND = 144;
377 const int ID_COMBO_DELETE = 145;
378 const int ID_COMBO_FONT = 146;
379 const int ID_COMBO_ENABLE = 147;
380
381 const int ID_RADIOBOX = 160;
382 const int ID_RADIOBOX_SEL_NUM = 161;
383 const int ID_RADIOBOX_SEL_STR = 162;
384 const int ID_RADIOBOX_FONT = 163;
385 const int ID_RADIOBOX_ENABLE = 164;
386
387 const int ID_RADIOBUTTON_1 = 166;
388 const int ID_RADIOBUTTON_2 = 167;
389
390 const int ID_SET_FONT = 170;
391
392 const int ID_GAUGE = 180;
393 const int ID_SLIDER = 181;
394
395 const int ID_SPIN = 182;
396 const int ID_BTNPROGRESS = 183;
397 const int ID_BUTTON_LABEL = 184;
398 const int ID_SPINCTRL = 185;
399
400 const int ID_BUTTON_TEST1 = 190;
401 const int ID_BUTTON_TEST2 = 191;
402 const int ID_BITMAP_BTN = 192;
403
404 const int ID_CHANGE_COLOUR = 200;
405
406 BEGIN_EVENT_TABLE(MyPanel, wxPanel)
407 EVT_SIZE ( MyPanel::OnSize)
408 EVT_NOTEBOOK_PAGE_CHANGING(ID_NOTEBOOK, MyPanel::OnPageChanging)
409 EVT_NOTEBOOK_PAGE_CHANGED(ID_NOTEBOOK, MyPanel::OnPageChanged)
410 EVT_LISTBOX (ID_LISTBOX, MyPanel::OnListBox)
411 EVT_LISTBOX (ID_LISTBOX_SORTED, MyPanel::OnListBox)
412 EVT_LISTBOX_DCLICK(ID_LISTBOX, MyPanel::OnListBoxDoubleClick)
413 EVT_BUTTON (ID_LISTBOX_SEL_NUM, MyPanel::OnListBoxButtons)
414 EVT_BUTTON (ID_LISTBOX_SEL_STR, MyPanel::OnListBoxButtons)
415 EVT_BUTTON (ID_LISTBOX_CLEAR, MyPanel::OnListBoxButtons)
416 EVT_BUTTON (ID_LISTBOX_APPEND, MyPanel::OnListBoxButtons)
417 EVT_BUTTON (ID_LISTBOX_DELETE, MyPanel::OnListBoxButtons)
418 EVT_BUTTON (ID_LISTBOX_FONT, MyPanel::OnListBoxButtons)
419 EVT_CHECKBOX (ID_LISTBOX_ENABLE, MyPanel::OnListBoxButtons)
420 #if wxUSE_CHOICE
421 EVT_CHOICE (ID_CHOICE, MyPanel::OnChoice)
422 EVT_CHOICE (ID_CHOICE_SORTED, MyPanel::OnChoice)
423 EVT_BUTTON (ID_CHOICE_SEL_NUM, MyPanel::OnChoiceButtons)
424 EVT_BUTTON (ID_CHOICE_SEL_STR, MyPanel::OnChoiceButtons)
425 EVT_BUTTON (ID_CHOICE_CLEAR, MyPanel::OnChoiceButtons)
426 EVT_BUTTON (ID_CHOICE_APPEND, MyPanel::OnChoiceButtons)
427 EVT_BUTTON (ID_CHOICE_DELETE, MyPanel::OnChoiceButtons)
428 EVT_BUTTON (ID_CHOICE_FONT, MyPanel::OnChoiceButtons)
429 EVT_CHECKBOX (ID_CHOICE_ENABLE, MyPanel::OnChoiceButtons)
430 #endif
431 EVT_COMBOBOX (ID_COMBO, MyPanel::OnCombo)
432 EVT_TEXT (ID_COMBO, MyPanel::OnComboTextChanged)
433 EVT_TEXT_ENTER(ID_COMBO, MyPanel::OnComboTextEnter)
434 EVT_BUTTON (ID_COMBO_SEL_NUM, MyPanel::OnComboButtons)
435 EVT_BUTTON (ID_COMBO_SEL_STR, MyPanel::OnComboButtons)
436 EVT_BUTTON (ID_COMBO_CLEAR, MyPanel::OnComboButtons)
437 EVT_BUTTON (ID_COMBO_APPEND, MyPanel::OnComboButtons)
438 EVT_BUTTON (ID_COMBO_DELETE, MyPanel::OnComboButtons)
439 EVT_BUTTON (ID_COMBO_FONT, MyPanel::OnComboButtons)
440 EVT_CHECKBOX (ID_COMBO_ENABLE, MyPanel::OnComboButtons)
441 EVT_RADIOBOX (ID_RADIOBOX, MyPanel::OnRadio)
442 EVT_BUTTON (ID_RADIOBOX_SEL_NUM, MyPanel::OnRadioButtons)
443 EVT_BUTTON (ID_RADIOBOX_SEL_STR, MyPanel::OnRadioButtons)
444 EVT_BUTTON (ID_RADIOBOX_FONT, MyPanel::OnRadioButtons)
445 EVT_CHECKBOX (ID_RADIOBOX_ENABLE, MyPanel::OnRadioButtons)
446 EVT_BUTTON (ID_SET_FONT, MyPanel::OnSetFont)
447 EVT_SLIDER (ID_SLIDER, MyPanel::OnSliderUpdate)
448 #if wxUSE_SPINBTN
449 EVT_SPIN (ID_SPIN, MyPanel::OnSpinUpdate)
450 EVT_SPIN_UP (ID_SPIN, MyPanel::OnSpinUp)
451 EVT_SPIN_DOWN (ID_SPIN, MyPanel::OnSpinDown)
452 EVT_UPDATE_UI (ID_BTNPROGRESS, MyPanel::OnUpdateShowProgress)
453 EVT_BUTTON (ID_BTNPROGRESS, MyPanel::OnShowProgress)
454 #endif // wxUSE_SPINBTN
455 #if wxUSE_SPINCTRL
456 EVT_SPINCTRL (ID_SPINCTRL, MyPanel::OnSpinCtrl)
457 EVT_SPIN_UP (ID_SPINCTRL, MyPanel::OnSpinCtrlUp)
458 EVT_SPIN_DOWN (ID_SPINCTRL, MyPanel::OnSpinCtrlDown)
459 EVT_TEXT (ID_SPINCTRL, MyPanel::OnSpinCtrlText)
460 #endif // wxUSE_SPINCTRL
461 #if wxUSE_TOGGLEBTN
462 EVT_TOGGLEBUTTON(ID_BUTTON_LABEL, MyPanel::OnUpdateLabel)
463 #endif // wxUSE_TOGGLEBTN
464 EVT_CHECKBOX (ID_CHANGE_COLOUR, MyPanel::OnChangeColour)
465 EVT_BUTTON (ID_BUTTON_TEST1, MyPanel::OnTestButton)
466 EVT_BUTTON (ID_BUTTON_TEST2, MyPanel::OnTestButton)
467 EVT_BUTTON (ID_BITMAP_BTN, MyPanel::OnBmpButton)
468 END_EVENT_TABLE()
469
470 BEGIN_EVENT_TABLE(MyButton, wxButton)
471 EVT_LEFT_DCLICK(MyButton::OnDClick)
472 END_EVENT_TABLE()
473
474 BEGIN_EVENT_TABLE(MyComboBox, wxComboBox)
475 EVT_CHAR(MyComboBox::OnChar)
476 EVT_KEY_DOWN(MyComboBox::OnKeyDown)
477 EVT_KEY_UP(MyComboBox::OnKeyUp)
478
479 EVT_SET_FOCUS(MyComboBox::OnFocusGot)
480 END_EVENT_TABLE()
481
482 BEGIN_EVENT_TABLE(MyRadioBox, wxRadioBox)
483 EVT_SET_FOCUS(MyRadioBox::OnFocusGot)
484 EVT_KILL_FOCUS(MyRadioBox::OnFocusLost)
485 END_EVENT_TABLE()
486
487 // ============================================================================
488 // implementation
489 // ============================================================================
490
491 MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h )
492 : wxPanel( frame, -1, wxPoint(x, y), wxSize(w, h) )
493 {
494 m_listbox = NULL;
495 m_listboxSorted = NULL;
496 #if wxUSE_CHOICE
497 m_choice = NULL;
498 m_choiceSorted = NULL;
499 #endif
500 m_combo = NULL;
501 m_radio = NULL;
502 m_gauge = NULL;
503 m_gaugeVert = NULL;
504 m_slider = NULL;
505 m_fontButton = NULL;
506 m_lbSelectNum = NULL;
507 m_lbSelectThis = NULL;
508 #if wxUSE_SPINBTN
509 m_spinbutton = NULL;
510 m_btnProgress = NULL;
511 #endif // wxUSE_SPINBTN
512 #if wxUSE_SPINCTRL
513 m_spinctrl = NULL;
514 #endif // wxUSE_SPINCTRL
515 m_spintext = NULL;
516 m_checkbox = NULL;
517 m_text = NULL;
518 m_notebook = NULL;
519 m_label = NULL;
520
521 m_text = new wxTextCtrl(this, -1, "This is the log window.\n",
522 wxPoint(0, 250), wxSize(100, 50), wxTE_MULTILINE);
523 m_text->SetBackgroundColour(wxT("wheat"));
524
525 if ( 0 )
526 wxLog::AddTraceMask(_T("focus"));
527 m_logTargetOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_text));
528
529 m_notebook = new wxNotebook(this, ID_NOTEBOOK);
530
531 wxString choices[] =
532 {
533 "This",
534 "is one of my",
535 "really",
536 "wonderful",
537 "examples."
538 };
539
540 #ifdef USE_XPM
541 // image ids
542 enum
543 {
544 Image_List, Image_Choice, Image_Combo, Image_Text, Image_Radio, Image_Gauge, Image_Max
545 };
546
547 // fill the image list
548 wxImageList *imagelist = new wxImageList(32, 32);
549
550 imagelist-> Add( wxBitmap( list_xpm ));
551 imagelist-> Add( wxBitmap( choice_xpm ));
552 imagelist-> Add( wxBitmap( combo_xpm ));
553 imagelist-> Add( wxBitmap( text_xpm ));
554 imagelist-> Add( wxBitmap( radio_xpm ));
555 imagelist-> Add( wxBitmap( gauge_xpm ));
556 m_notebook->SetImageList(imagelist);
557 #elif defined(__WXMSW__)
558 // load images from resources
559 enum
560 {
561 Image_List, Image_Choice, Image_Combo, Image_Text, Image_Radio, Image_Gauge, Image_Max
562 };
563 wxImageList *imagelist = new wxImageList(16, 16, FALSE, Image_Max);
564
565 static const char *s_iconNames[Image_Max] =
566 {
567 "list", "choice", "combo", "text", "radio", "gauge"
568 };
569
570 for ( size_t n = 0; n < Image_Max; n++ )
571 {
572 wxBitmap bmp(s_iconNames[n]);
573 if ( !bmp.Ok() || (imagelist->Add(bmp) == -1) )
574 {
575 wxLogWarning(wxT("Couldn't load the image '%s' for the notebook page %d."),
576 s_iconNames[n], n);
577 }
578 }
579
580 m_notebook->SetImageList(imagelist);
581 #else
582
583 // No images for now
584 #define Image_List -1
585 #define Image_Choice -1
586 #define Image_Combo -1
587 #define Image_Text -1
588 #define Image_Radio -1
589 #define Image_Gauge -1
590 #define Image_Max -1
591
592 #endif
593
594 wxPanel *panel = new wxPanel(m_notebook);
595 m_listbox = new wxListBox( panel, ID_LISTBOX,
596 wxPoint(10,10), wxSize(120,70),
597 5, choices, wxLB_ALWAYS_SB );
598 m_listboxSorted = new wxListBox( panel, ID_LISTBOX_SORTED,
599 wxPoint(10,90), wxSize(120,70),
600 5, choices, wxLB_SORT );
601
602 SetListboxClientData(wxT("listbox"), m_listbox);
603 SetListboxClientData(wxT("listbox"), m_listboxSorted);
604
605 m_listbox->SetCursor(*wxCROSS_CURSOR);
606 #if wxUSE_TOOLTIPS
607 m_listbox->SetToolTip( "This is a list box" );
608 #endif // wxUSE_TOOLTIPS
609
610 m_lbSelectNum = new wxButton( panel, ID_LISTBOX_SEL_NUM, "Select #&2", wxPoint(180,30), wxSize(140,30) );
611 m_lbSelectThis = new wxButton( panel, ID_LISTBOX_SEL_STR, "&Select 'This'", wxPoint(340,30), wxSize(140,30) );
612 (void)new wxButton( panel, ID_LISTBOX_CLEAR, "&Clear", wxPoint(180,80), wxSize(140,30) );
613 (void)new MyButton( panel, ID_LISTBOX_APPEND, "&Append 'Hi!'", wxPoint(340,80), wxSize(140,30) );
614 (void)new wxButton( panel, ID_LISTBOX_DELETE, "D&elete selected item", wxPoint(180,130), wxSize(140,30) );
615 wxButton *button = new MyButton( panel, ID_LISTBOX_FONT, "Set &Italic font", wxPoint(340,130), wxSize(140,30) );
616
617 button->SetDefault();
618
619 button->SetForegroundColour(*wxBLUE);
620
621 #if wxUSE_TOOLTIPS
622 button->SetToolTip( "Press here to set italic font" );
623 #endif // wxUSE_TOOLTIPS
624
625 m_checkbox = new wxCheckBox( panel, ID_LISTBOX_ENABLE, "&Disable", wxPoint(20,170) );
626 m_checkbox->SetValue(FALSE);
627 #if wxUSE_TOOLTIPS
628 m_checkbox->SetToolTip( "Click here to disable the listbox" );
629 #endif // wxUSE_TOOLTIPS
630 (void)new wxCheckBox( panel, ID_CHANGE_COLOUR, "&Toggle colour",
631 wxPoint(110,170) );
632 panel->SetCursor(wxCursor(wxCURSOR_HAND));
633 m_notebook->AddPage(panel, "wxListBox", TRUE, Image_List);
634
635 #if wxUSE_CHOICE
636 panel = new wxPanel(m_notebook);
637 m_choice = new wxChoice( panel, ID_CHOICE, wxPoint(10,10), wxSize(120,-1), 5, choices );
638 m_choiceSorted = new wxChoice( panel, ID_CHOICE_SORTED, wxPoint(10,70), wxSize(120,-1),
639 5, choices, wxCB_SORT );
640
641 SetChoiceClientData(wxT("choice"), m_choice);
642 SetChoiceClientData(wxT("choice"), m_choiceSorted);
643
644 m_choice->SetSelection(2);
645 m_choice->SetBackgroundColour( wxT("red") );
646 (void)new wxButton( panel, ID_CHOICE_SEL_NUM, "Select #&2", wxPoint(180,30), wxSize(140,30) );
647 (void)new wxButton( panel, ID_CHOICE_SEL_STR, "&Select 'This'", wxPoint(340,30), wxSize(140,30) );
648 (void)new wxButton( panel, ID_CHOICE_CLEAR, "&Clear", wxPoint(180,80), wxSize(140,30) );
649 (void)new wxButton( panel, ID_CHOICE_APPEND, "&Append 'Hi!'", wxPoint(340,80), wxSize(140,30) );
650 (void)new wxButton( panel, ID_CHOICE_DELETE, "D&elete selected item", wxPoint(180,130), wxSize(140,30) );
651 (void)new wxButton( panel, ID_CHOICE_FONT, "Set &Italic font", wxPoint(340,130), wxSize(140,30) );
652 (void)new wxCheckBox( panel, ID_CHOICE_ENABLE, "&Disable", wxPoint(20,130), wxSize(140,30) );
653
654 m_notebook->AddPage(panel, "wxChoice", FALSE, Image_Choice);
655 #endif // wxUSE_CHOICE
656
657 panel = new wxPanel(m_notebook);
658 (void)new wxStaticBox( panel, -1, "&Box around combobox",
659 wxPoint(5, 5), wxSize(150, 100));
660 m_combo = new MyComboBox( panel, ID_COMBO, "This",
661 wxPoint(20,25), wxSize(120, -1),
662 5, choices,
663 /* wxCB_READONLY | */ wxPROCESS_ENTER);
664
665 (void)new wxButton( panel, ID_COMBO_SEL_NUM, "Select #&2", wxPoint(180,30), wxSize(140,30) );
666 (void)new wxButton( panel, ID_COMBO_SEL_STR, "&Select 'This'", wxPoint(340,30), wxSize(140,30) );
667 (void)new wxButton( panel, ID_COMBO_CLEAR, "&Clear", wxPoint(180,80), wxSize(140,30) );
668 (void)new wxButton( panel, ID_COMBO_APPEND, "&Append 'Hi!'", wxPoint(340,80), wxSize(140,30) );
669 (void)new wxButton( panel, ID_COMBO_DELETE, "D&elete selected item", wxPoint(180,130), wxSize(140,30) );
670 (void)new wxButton( panel, ID_COMBO_FONT, "Set &Italic font", wxPoint(340,130), wxSize(140,30) );
671 (void)new wxCheckBox( panel, ID_COMBO_ENABLE, "&Disable", wxPoint(20,130), wxSize(140,30) );
672 m_notebook->AddPage(panel, "wxComboBox", FALSE, Image_Combo);
673
674 wxString choices2[] =
675 {
676 "First", "Second",
677 /* "Third",
678 "Fourth", "Fifth", "Sixth",
679 "Seventh", "Eighth", "Nineth", "Tenth" */
680 };
681
682 panel = new wxPanel(m_notebook);
683 (void)new MyRadioBox( panel, ID_RADIOBOX, "&That", wxPoint(10,160), wxSize(-1,-1), WXSIZEOF(choices2), choices2, 1, wxRA_SPECIFY_ROWS );
684 m_radio = new wxRadioBox( panel, ID_RADIOBOX, "T&his", wxPoint(10,10), wxSize(-1,-1), WXSIZEOF(choices), choices, 1, wxRA_SPECIFY_COLS );
685 m_radio->SetForegroundColour(*wxRED);
686
687 #if wxUSE_TOOLTIPS
688 m_combo->SetToolTip("This is a natural\ncombobox - can you believe me?");
689 m_radio->SetToolTip("Ever seen a radiobox?");
690 #endif // wxUSE_TOOLTIPS
691
692 (void)new wxButton( panel, ID_RADIOBOX_SEL_NUM, "Select #&2", wxPoint(180,30), wxSize(140,30) );
693 (void)new wxButton( panel, ID_RADIOBOX_SEL_STR, "&Select 'This'", wxPoint(180,80), wxSize(140,30) );
694 m_fontButton = new wxButton( panel, ID_SET_FONT, "Set &more Italic font", wxPoint(340,30), wxSize(140,30) );
695 (void)new wxButton( panel, ID_RADIOBOX_FONT, "Set &Italic font", wxPoint(340,80), wxSize(140,30) );
696 (void)new wxCheckBox( panel, ID_RADIOBOX_ENABLE, "&Disable", wxPoint(340,130), wxDefaultSize );
697 wxRadioButton *rb = new wxRadioButton( panel, ID_RADIOBUTTON_1, "Radiobutton1", wxPoint(210,170), wxDefaultSize, wxRB_GROUP );
698 rb->SetValue( FALSE );
699 (void)new wxRadioButton( panel, ID_RADIOBUTTON_2, "&Radiobutton2", wxPoint(340,170), wxDefaultSize );
700 m_notebook->AddPage(panel, "wxRadioBox", FALSE, Image_Radio);
701
702 panel = new wxPanel(m_notebook);
703 (void)new wxStaticBox( panel, -1, "&wxGauge and wxSlider", wxPoint(10,10), wxSize(222,130) );
704 m_gauge = new wxGauge( panel, -1, 200, wxPoint(18,50), wxSize(155, 30), wxGA_HORIZONTAL|wxNO_BORDER );
705 m_gauge->SetBackgroundColour(*wxGREEN);
706 m_gauge->SetForegroundColour(*wxRED);
707 m_gaugeVert = new wxGauge( panel, -1, 100,
708 wxPoint(195,35), wxSize(30, 90),
709 wxGA_VERTICAL | wxGA_SMOOTH | wxNO_BORDER );
710 m_slider = new wxSlider( panel, ID_SLIDER, 0, 0, 200, wxPoint(18,90), wxSize(155,-1),
711 wxSL_AUTOTICKS | wxSL_LABELS );
712 m_slider->SetTickFreq(40, 0);
713 #if wxUSE_TOOLTIPS
714 m_slider->SetToolTip(_T("This is a sliding slider"));
715 #endif // wxUSE_TOOLTIPS
716
717 (void)new wxStaticBox( panel, -1, "&Explanation",
718 wxPoint(230,10), wxSize(270,130),
719 wxALIGN_CENTER );
720
721 #ifdef __WXMOTIF__
722 // No wrapping text in wxStaticText yet :-(
723 (void)new wxStaticText( panel, -1,
724 "Drag the slider!",
725 wxPoint(250,30),
726 wxSize(240, -1)
727 );
728 #else
729 (void)new wxStaticText( panel, -1,
730 "In order see the gauge (aka progress bar)\n"
731 "control do something you have to drag the\n"
732 "handle of the slider to the right.\n"
733 "\n"
734 "This is also supposed to demonstrate how\n"
735 "to use static controls.\n",
736 wxPoint(250,25),
737 wxSize(240, 110)
738 );
739 #endif
740 int initialSpinValue = -5;
741 wxString s;
742 s << initialSpinValue;
743 m_spintext = new wxTextCtrl( panel, -1, s, wxPoint(20,160), wxSize(80,-1) );
744 #if wxUSE_SPINBTN
745 m_spinbutton = new wxSpinButton( panel, ID_SPIN, wxPoint(103,160), wxSize(80, -1) );
746 m_spinbutton->SetRange(-40,30);
747 m_spinbutton->SetValue(initialSpinValue);
748
749 m_btnProgress = new wxButton( panel, ID_BTNPROGRESS, "&Show progress dialog",
750 wxPoint(300, 160) );
751 #endif // wxUSE_SPINBTN
752
753 #if wxUSE_SPINCTRL
754 m_spinctrl = new wxSpinCtrl( panel, ID_SPINCTRL, "", wxPoint(200, 160), wxSize(80, -1) );
755 m_spinctrl->SetRange(10,30);
756 m_spinctrl->SetValue(15);
757 #endif // wxUSE_SPINCTRL
758
759 m_notebook->AddPage(panel, "wxGauge", FALSE, Image_Gauge);
760
761 panel = new wxPanel(m_notebook);
762
763 #if !defined(__WXMOTIF__) && !defined(__WIN16__) // wxStaticBitmap not working under Motif yet; and icons not allowed under WIN16.
764 wxIcon icon = wxArtProvider::GetIcon(wxART_INFORMATION);
765 (void) new wxStaticBitmap( panel, -1, icon, wxPoint(10, 10) );
766
767 // VZ: don't leak memory
768 // bmpStatic = new wxStaticBitmap(panel, -1, wxNullIcon, wxPoint(50, 10));
769 // bmpStatic->SetIcon(wxArtProvider::GetIcon(wxART_QUESTION));
770 #endif // !Motif
771
772 wxBitmap bitmap( 100, 100 );
773 wxMemoryDC dc;
774 dc.SelectObject( bitmap );
775 dc.SetPen(*wxGREEN_PEN);
776 dc.Clear();
777 dc.DrawEllipse(5, 5, 90, 90);
778 dc.DrawText("Bitmap", 30, 40);
779 dc.SelectObject( wxNullBitmap );
780
781 (void)new wxBitmapButton(panel, ID_BITMAP_BTN, bitmap, wxPoint(100, 20));
782
783 #ifdef __WXMSW__
784 // test for masked bitmap display
785 bitmap = wxBitmap("test2.bmp", wxBITMAP_TYPE_BMP);
786 if (bitmap.Ok())
787 {
788 bitmap.SetMask(new wxMask(bitmap, *wxBLUE));
789
790 (void)new wxStaticBitmap /* wxBitmapButton */ (panel, -1, bitmap, wxPoint(300, 120));
791 }
792 #endif
793
794 wxBitmap bmp1(wxArtProvider::GetBitmap(wxART_INFORMATION)),
795 bmp2(wxArtProvider::GetBitmap(wxART_WARNING)),
796 bmp3(wxArtProvider::GetBitmap(wxART_QUESTION));
797 wxBitmapButton *bmpBtn = new wxBitmapButton
798 (
799 panel, -1,
800 bmp1,
801 wxPoint(30, 70)
802 );
803
804 bmpBtn->SetBitmapSelected(bmp2);
805 bmpBtn->SetBitmapFocus(bmp3);
806
807 #if wxUSE_TOGGLEBTN
808 (void)new wxToggleButton(panel, ID_BUTTON_LABEL,
809 "&Toggle label", wxPoint(250, 20));
810 #endif // wxUSE_TOGGLEBTN
811
812 m_label = new wxStaticText(panel, -1, "Label with some long text",
813 wxPoint(250, 60), wxDefaultSize,
814 wxALIGN_RIGHT /*| wxST_NO_AUTORESIZE*/);
815 m_label->SetForegroundColour( *wxBLUE );
816
817 m_notebook->AddPage(panel, "wxBitmapXXX");
818
819 // layout constraints
820 #if wxUSE_CONSTRAINTS
821 wxLayoutConstraints *c;
822
823 panel = new wxPanel(m_notebook);
824 panel->SetAutoLayout( TRUE );
825
826 c = new wxLayoutConstraints;
827 c->top.SameAs( panel, wxTop, 10 );
828 c->height.AsIs( );
829 c->left.SameAs( panel, wxLeft, 10 );
830 c->width.PercentOf( panel, wxWidth, 40 );
831
832 wxButton *pMyButton = new wxButton(panel, ID_BUTTON_TEST1, "Test Button &1" );
833 pMyButton->SetConstraints( c );
834
835 c = new wxLayoutConstraints;
836 c->top.SameAs( panel, wxTop, 10 );
837 c->bottom.SameAs( panel, wxBottom, 10 );
838 c->right.SameAs( panel, wxRight, 10 );
839 c->width.PercentOf( panel, wxWidth, 40 );
840
841 wxButton *pMyButton2 = new wxButton(panel, ID_BUTTON_TEST2, "Test Button &2" );
842 pMyButton2->SetConstraints( c );
843
844 m_notebook->AddPage(panel, "wxLayoutConstraint");
845 #endif
846
847 // sizer
848
849 panel = new wxPanel(m_notebook);
850 panel->SetAutoLayout( TRUE );
851
852 wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
853
854 sizer->Add( new wxButton(panel, -1, "Test Button &1" ), 3, wxALL, 10 );
855 sizer->Add( 20,20, 1 );
856 sizer->Add( new wxButton(panel, -1, "Multiline\nbutton" ), 3, wxGROW|wxALL, 10 );
857
858 panel->SetSizer( sizer );
859
860 m_notebook->AddPage(panel, "wxSizer");
861 }
862
863 void MyPanel::OnSize( wxSizeEvent& WXUNUSED(event) )
864 {
865 int x = 0;
866 int y = 0;
867 GetClientSize( &x, &y );
868
869 if (m_notebook) m_notebook->SetSize( 2, 2, x-4, y*2/3-4 );
870 if (m_text) m_text->SetSize( 2, y*2/3+2, x-4, y/3-4 );
871 }
872
873 void MyPanel::OnPageChanging( wxNotebookEvent &event )
874 {
875 int selOld = event.GetOldSelection();
876 if ( selOld == 2 )
877 {
878 if ( wxMessageBox("This demonstrates how a program may prevent the\n"
879 "page change from taking place - if you select\n"
880 "[No] the current page will stay the third one\n",
881 "Control sample",
882 wxICON_QUESTION | wxYES_NO, this) != wxYES )
883 {
884 event.Veto();
885
886 return;
887 }
888 }
889
890 *m_text << "Notebook selection is being changed from " << selOld
891 << " to " << event.GetSelection()
892 << " (current page from notebook is "
893 << m_notebook->GetSelection() << ")\n";
894 }
895
896 void MyPanel::OnPageChanged( wxNotebookEvent &event )
897 {
898 *m_text << "Notebook selection is now " << event.GetSelection()
899 << " (from notebook: " << m_notebook->GetSelection() << ")\n";
900 }
901
902 void MyPanel::OnTestButton(wxCommandEvent& event)
903 {
904 wxLogMessage(_T("Button %c clicked."),
905 event.GetId() == ID_BUTTON_TEST1 ? _T('1') : _T('2'));
906 }
907
908 void MyPanel::OnBmpButton(wxCommandEvent& event)
909 {
910 wxLogMessage(_T("Bitmap button clicked."));
911 }
912
913 void MyPanel::OnChangeColour(wxCommandEvent& WXUNUSED(event))
914 {
915 static wxColour s_colOld;
916
917 // test panel colour changing and propagation to the subcontrols
918 if ( s_colOld.Ok() )
919 {
920 SetBackgroundColour(s_colOld);
921 s_colOld = wxNullColour;
922
923 m_lbSelectThis->SetForegroundColour(wxT("red"));
924 m_lbSelectThis->SetBackgroundColour(wxT("white"));
925 }
926 else
927 {
928 s_colOld = wxColour(wxT("red"));
929 SetBackgroundColour(wxT("white"));
930
931 m_lbSelectThis->SetForegroundColour(wxT("white"));
932 m_lbSelectThis->SetBackgroundColour(wxT("red"));
933 }
934
935 m_lbSelectThis->Refresh();
936 Refresh();
937 }
938
939 void MyPanel::OnListBox( wxCommandEvent &event )
940 {
941 // GetParent()->Move(100, 100);
942
943 if (event.GetInt() == -1)
944 {
945 m_text->AppendText( "ListBox has no selections anymore\n" );
946 return;
947 }
948
949 wxListBox *listbox = event.GetId() == ID_LISTBOX ? m_listbox
950 : m_listboxSorted;
951
952 m_text->AppendText( "ListBox event selection string is: '" );
953 m_text->AppendText( event.GetString() );
954 m_text->AppendText( "'\n" );
955 m_text->AppendText( "ListBox control selection string is: '" );
956 m_text->AppendText( listbox->GetStringSelection() );
957 m_text->AppendText( "'\n" );
958
959 wxStringClientData *obj = ((wxStringClientData *)event.GetClientObject());
960 m_text->AppendText( "ListBox event client data string is: '" );
961 if (obj) // BC++ doesn't like use of '? .. : .. ' in this context
962 m_text->AppendText( obj->GetData() );
963 else
964 m_text->AppendText( wxString("none") );
965
966 m_text->AppendText( "'\n" );
967 m_text->AppendText( "ListBox control client data string is: '" );
968 obj = (wxStringClientData *)listbox->GetClientObject(listbox->GetSelection());
969 if (obj)
970 m_text->AppendText( obj->GetData() );
971 else
972 m_text->AppendText( wxString("none") );
973 m_text->AppendText( "'\n" );
974 }
975
976 void MyPanel::OnListBoxDoubleClick( wxCommandEvent &event )
977 {
978 m_text->AppendText( "ListBox double click string is: " );
979 m_text->AppendText( event.GetString() );
980 m_text->AppendText( "\n" );
981 }
982
983 void MyPanel::OnListBoxButtons( wxCommandEvent &event )
984 {
985 switch (event.GetId())
986 {
987 case ID_LISTBOX_ENABLE:
988 {
989 m_text->AppendText("Checkbox clicked.\n");
990 #if wxUSE_TOOLTIPS
991 wxCheckBox *cb = (wxCheckBox*)event.GetEventObject();
992 if (event.GetInt())
993 cb->SetToolTip( "Click to enable listbox" );
994 else
995 cb->SetToolTip( "Click to disable listbox" );
996 #endif // wxUSE_TOOLTIPS
997 m_listbox->Enable( event.GetInt() == 0 );
998 m_lbSelectThis->Enable( event.GetInt() == 0 );
999 m_lbSelectNum->Enable( event.GetInt() == 0 );
1000 m_listboxSorted->Enable( event.GetInt() == 0 );
1001 FindWindow(ID_CHANGE_COLOUR)->Enable( event.GetInt() == 0 );
1002 break;
1003 }
1004 case ID_LISTBOX_SEL_NUM:
1005 {
1006 m_listbox->SetSelection( 2 );
1007 m_listboxSorted->SetSelection( 2 );
1008 m_lbSelectThis->WarpPointer( 40, 14 );
1009 break;
1010 }
1011 case ID_LISTBOX_SEL_STR:
1012 {
1013 m_listbox->SetStringSelection( "This" );
1014 m_listboxSorted->SetStringSelection( "This" );
1015 m_lbSelectNum->WarpPointer( 40, 14 );
1016 break;
1017 }
1018 case ID_LISTBOX_CLEAR:
1019 {
1020 m_listbox->Clear();
1021 m_listboxSorted->Clear();
1022 break;
1023 }
1024 case ID_LISTBOX_APPEND:
1025 {
1026 m_listbox->Append( "Hi!" );
1027 m_listboxSorted->Append( "Hi!" );
1028 break;
1029 }
1030 case ID_LISTBOX_DELETE:
1031 {
1032 int idx;
1033 idx = m_listbox->GetSelection();
1034 if ( idx != wxNOT_FOUND )
1035 m_listbox->Delete( idx );
1036 idx = m_listboxSorted->GetSelection();
1037 if ( idx != wxNOT_FOUND )
1038 m_listboxSorted->Delete( idx );
1039 break;
1040 }
1041 case ID_LISTBOX_FONT:
1042 {
1043 m_listbox->SetFont( *wxITALIC_FONT );
1044 m_listboxSorted->SetFont( *wxITALIC_FONT );
1045 m_checkbox->SetFont( *wxITALIC_FONT );
1046 break;
1047 }
1048 }
1049 }
1050
1051 #if wxUSE_CHOICE
1052 void MyPanel::OnChoice( wxCommandEvent &event )
1053 {
1054 wxChoice *choice = event.GetId() == ID_CHOICE ? m_choice
1055 : m_choiceSorted;
1056
1057 m_text->AppendText( "Choice event selection string is: '" );
1058 m_text->AppendText( event.GetString() );
1059 m_text->AppendText( "'\n" );
1060 m_text->AppendText( "Choice control selection string is: '" );
1061 m_text->AppendText( choice->GetStringSelection() );
1062 m_text->AppendText( "'\n" );
1063
1064 wxStringClientData *obj = ((wxStringClientData *)event.GetClientObject());
1065 m_text->AppendText( "Choice event client data string is: '" );
1066
1067 if (obj)
1068 m_text->AppendText( obj->GetData() );
1069 else
1070 m_text->AppendText( wxString("none") );
1071
1072 m_text->AppendText( "'\n" );
1073 m_text->AppendText( "Choice control client data string is: '" );
1074 obj = (wxStringClientData *)choice->GetClientObject(choice->GetSelection());
1075
1076 if (obj)
1077 m_text->AppendText( obj->GetData() );
1078 else
1079 m_text->AppendText( wxString("none") );
1080 m_text->AppendText( "'\n" );
1081 }
1082
1083 void MyPanel::OnChoiceButtons( wxCommandEvent &event )
1084 {
1085 switch (event.GetId())
1086 {
1087 case ID_CHOICE_ENABLE:
1088 {
1089 m_choice->Enable( event.GetInt() == 0 );
1090 m_choiceSorted->Enable( event.GetInt() == 0 );
1091 break;
1092 }
1093 case ID_CHOICE_SEL_NUM:
1094 {
1095 m_choice->SetSelection( 2 );
1096 m_choiceSorted->SetSelection( 2 );
1097 break;
1098 }
1099 case ID_CHOICE_SEL_STR:
1100 {
1101 m_choice->SetStringSelection( "This" );
1102 m_choiceSorted->SetStringSelection( "This" );
1103 break;
1104 }
1105 case ID_CHOICE_CLEAR:
1106 {
1107 m_choice->Clear();
1108 m_choiceSorted->Clear();
1109 break;
1110 }
1111 case ID_CHOICE_APPEND:
1112 {
1113 m_choice->Append( "Hi!" );
1114 m_choiceSorted->Append( "Hi!" );
1115 break;
1116 }
1117 case ID_CHOICE_DELETE:
1118 {
1119 int idx = m_choice->GetSelection();
1120 if ( idx != wxNOT_FOUND )
1121 m_choice->Delete( idx );
1122 idx = m_choiceSorted->GetSelection();
1123 if ( idx != wxNOT_FOUND )
1124 m_choiceSorted->Delete( idx );
1125 break;
1126 }
1127 case ID_CHOICE_FONT:
1128 {
1129 m_choice->SetFont( *wxITALIC_FONT );
1130 m_choiceSorted->SetFont( *wxITALIC_FONT );
1131 break;
1132 }
1133 }
1134 }
1135 #endif // wxUSE_CHOICE
1136
1137 void MyPanel::OnCombo( wxCommandEvent &event )
1138 {
1139 m_text->AppendText( "ComboBox event selection string is: " );
1140 m_text->AppendText( event.GetString() );
1141 m_text->AppendText( "\n" );
1142 m_text->AppendText( "ComboBox control selection string is: " );
1143 m_text->AppendText( m_combo->GetStringSelection() );
1144 m_text->AppendText( "\n" );
1145 }
1146
1147 void MyPanel::OnComboTextChanged(wxCommandEvent& event)
1148 {
1149 wxString str;
1150 str.Printf( wxT("Text in the combobox changed: now is '%s'."),
1151 event.GetString().c_str() );
1152 wxLogMessage( str.c_str() );
1153 }
1154
1155 void MyPanel::OnComboTextEnter(wxCommandEvent& WXUNUSED(event))
1156 {
1157 wxLogMessage(_T("Enter pressed in the combobox: value is '%s'."),
1158 m_combo->GetValue().c_str());
1159 }
1160
1161 void MyPanel::OnComboButtons( wxCommandEvent &event )
1162 {
1163 switch (event.GetId())
1164 {
1165 case ID_COMBO_ENABLE:
1166 {
1167 m_combo->Enable( event.GetInt() == 0 );
1168 break;
1169 }
1170 case ID_COMBO_SEL_NUM:
1171 {
1172 m_combo->SetSelection( 2 );
1173 break;
1174 }
1175 case ID_COMBO_SEL_STR:
1176 {
1177 m_combo->SetStringSelection( "This" );
1178 break;
1179 }
1180 case ID_COMBO_CLEAR:
1181 {
1182 m_combo->Clear();
1183 break;
1184 }
1185 case ID_COMBO_APPEND:
1186 {
1187 m_combo->Append( "Hi!" );
1188 break;
1189 }
1190 case ID_COMBO_DELETE:
1191 {
1192 int idx = m_combo->GetSelection();
1193 m_combo->Delete( idx );
1194 break;
1195 }
1196 case ID_COMBO_FONT:
1197 {
1198 m_combo->SetFont( *wxITALIC_FONT );
1199 break;
1200 }
1201 }
1202 }
1203
1204 void MyPanel::OnRadio( wxCommandEvent &event )
1205 {
1206 m_text->AppendText( "RadioBox selection string is: " );
1207 m_text->AppendText( event.GetString() );
1208 m_text->AppendText( "\n" );
1209 }
1210
1211 void MyPanel::OnRadioButtons( wxCommandEvent &event )
1212 {
1213 switch (event.GetId())
1214 {
1215 case ID_RADIOBOX_ENABLE:
1216 {
1217 m_radio->Enable( event.GetInt() == 0 );
1218 break;
1219 }
1220 case ID_RADIOBOX_SEL_NUM:
1221 {
1222 m_radio->SetSelection( 2 );
1223 break;
1224 }
1225 case ID_RADIOBOX_SEL_STR:
1226 {
1227 m_radio->SetStringSelection( "This" );
1228 break;
1229 }
1230 case ID_RADIOBOX_FONT:
1231 {
1232 m_radio->SetForegroundColour(*wxGREEN);
1233
1234 m_radio->SetFont( *wxITALIC_FONT );
1235 break;
1236 }
1237 }
1238 }
1239
1240 void MyPanel::OnSetFont( wxCommandEvent &WXUNUSED(event) )
1241 {
1242 m_fontButton->SetFont( *wxITALIC_FONT );
1243 m_text->SetFont( *wxITALIC_FONT );
1244 }
1245
1246 void MyPanel::OnUpdateLabel( wxCommandEvent &event )
1247 {
1248 m_label->SetLabel(event.GetInt() ? "Very very very very very long text."
1249 : "Shorter text.");
1250 }
1251
1252 void MyPanel::OnSliderUpdate( wxCommandEvent &WXUNUSED(event) )
1253 {
1254 m_gauge->SetValue( m_slider->GetValue() );
1255 m_gaugeVert->SetValue( m_slider->GetValue() / 2 );
1256 }
1257
1258 #if wxUSE_SPINCTRL
1259
1260 void MyPanel::OnSpinCtrlText(wxCommandEvent& event)
1261 {
1262 if ( m_spinctrl )
1263 {
1264 wxString s;
1265 s.Printf( _T("Spin ctrl text changed: now %d (from event: %s)\n"),
1266 m_spinctrl->GetValue(), event.GetString().c_str() );
1267 m_text->AppendText(s);
1268 }
1269 }
1270
1271 void MyPanel::OnSpinCtrl(wxSpinEvent& event)
1272 {
1273 if ( m_spinctrl )
1274 {
1275 wxString s;
1276 s.Printf( _T("Spin ctrl changed: now %d (from event: %ld)\n"),
1277 m_spinctrl->GetValue(), event.GetInt() );
1278 m_text->AppendText(s);
1279 }
1280 }
1281
1282 void MyPanel::OnSpinCtrlUp(wxSpinEvent& event)
1283 {
1284 if ( m_spinctrl )
1285 {
1286 m_text->AppendText( wxString::Format(
1287 _T("Spin up: %d (from event: %ld)\n"),
1288 m_spinctrl->GetValue(), event.GetInt() ) );
1289 }
1290 }
1291
1292 void MyPanel::OnSpinCtrlDown(wxSpinEvent& event)
1293 {
1294 if ( m_spinctrl )
1295 {
1296 m_text->AppendText( wxString::Format(
1297 _T("Spin down: %d (from event: %ld)\n"),
1298 m_spinctrl->GetValue(), event.GetInt() ) );
1299 }
1300 }
1301
1302 #endif // wxUSE_SPINCTRL
1303
1304 #if wxUSE_SPINBTN
1305 void MyPanel::OnSpinUp( wxSpinEvent &event )
1306 {
1307 wxString value;
1308 value.Printf( _T("Spin control up: current = %d\n"),
1309 m_spinbutton->GetValue());
1310
1311 if ( event.GetPosition() > 17 )
1312 {
1313 value += _T("Preventing the spin button from going above 17.\n");
1314
1315 event.Veto();
1316 }
1317
1318 m_text->AppendText(value);
1319 }
1320
1321 void MyPanel::OnSpinDown( wxSpinEvent &event )
1322 {
1323 wxString value;
1324 value.Printf( _T("Spin control down: current = %d\n"),
1325 m_spinbutton->GetValue());
1326
1327 if ( event.GetPosition() < -17 )
1328 {
1329 value += _T("Preventing the spin button from going below -17.\n");
1330
1331 event.Veto();
1332 }
1333
1334 m_text->AppendText(value);
1335 }
1336
1337 void MyPanel::OnSpinUpdate( wxSpinEvent &event )
1338 {
1339 wxString value;
1340 value.Printf( _T("%d"), event.GetPosition() );
1341 m_spintext->SetValue( value );
1342
1343 value.Printf( _T("Spin control range: (%d, %d), current = %d\n"),
1344 m_spinbutton->GetMin(), m_spinbutton->GetMax(),
1345 m_spinbutton->GetValue());
1346
1347 m_text->AppendText(value);
1348 }
1349
1350 void MyPanel::OnUpdateShowProgress( wxUpdateUIEvent& event )
1351 {
1352 event.Enable( m_spinbutton->GetValue() > 0 );
1353 }
1354
1355 void MyPanel::OnShowProgress( wxCommandEvent& WXUNUSED(event) )
1356 {
1357 int max = m_spinbutton->GetValue();
1358
1359 if ( max <= 0 )
1360 {
1361 wxLogError(_T("You must set positive range!"));
1362 return;
1363 }
1364
1365 wxProgressDialog dialog("Progress dialog example",
1366 "An informative message",
1367 max, // range
1368 this, // parent
1369 wxPD_CAN_ABORT |
1370 wxPD_AUTO_HIDE |
1371 wxPD_APP_MODAL |
1372 wxPD_ELAPSED_TIME |
1373 wxPD_ESTIMATED_TIME |
1374 wxPD_REMAINING_TIME);
1375
1376
1377 bool cont = TRUE;
1378 for ( int i = 0; i <= max && cont; i++ )
1379 {
1380 wxSleep(1);
1381 if ( i == max )
1382 {
1383 cont = dialog.Update(i, "That's all, folks!");
1384 }
1385 else if ( i == max / 2 )
1386 {
1387 cont = dialog.Update(i, "Only a half left (very long message)!");
1388 }
1389 else
1390 {
1391 cont = dialog.Update(i);
1392 }
1393 }
1394
1395 if ( !cont )
1396 {
1397 *m_text << "Progress dialog aborted!\n";
1398 }
1399 else
1400 {
1401 *m_text << "Countdown from " << max << " finished.\n";
1402 }
1403 }
1404
1405 #endif // wxUSE_SPINBTN
1406
1407 MyPanel::~MyPanel()
1408 {
1409 //wxLog::RemoveTraceMask(_T("focus"));
1410 delete wxLog::SetActiveTarget(m_logTargetOld);
1411
1412 delete m_notebook->GetImageList();
1413 }
1414
1415 //----------------------------------------------------------------------
1416 // MyFrame
1417 //----------------------------------------------------------------------
1418
1419 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
1420 EVT_MENU(CONTROLS_QUIT, MyFrame::OnQuit)
1421 EVT_MENU(CONTROLS_ABOUT, MyFrame::OnAbout)
1422 EVT_MENU(CONTROLS_CLEAR_LOG, MyFrame::OnClearLog)
1423 #if wxUSE_TOOLTIPS
1424 EVT_MENU(CONTROLS_SET_TOOLTIP_DELAY, MyFrame::OnSetTooltipDelay)
1425 EVT_MENU(CONTROLS_ENABLE_TOOLTIPS, MyFrame::OnToggleTooltips)
1426 #endif // wxUSE_TOOLTIPS
1427
1428 EVT_MENU(CONTROLS_ENABLE_ALL, MyFrame::OnEnableAll)
1429
1430 EVT_SIZE(MyFrame::OnSize)
1431 EVT_MOVE(MyFrame::OnMove)
1432
1433 EVT_IDLE(MyFrame::OnIdle)
1434 END_EVENT_TABLE()
1435
1436 MyFrame::MyFrame(const char *title, int x, int y)
1437 : wxFrame(NULL, -1, title, wxPoint(x, y), wxSize(500, 430))
1438 {
1439 // Give it an icon
1440 // The wxICON() macros loads an icon from a resource under Windows
1441 // and uses an #included XPM image under GTK+ and Motif
1442
1443 SetIcon( wxICON(mondrian) );
1444
1445 wxMenu *file_menu = new wxMenu;
1446
1447 file_menu->Append(CONTROLS_CLEAR_LOG, "&Clear log\tCtrl-L");
1448 file_menu->AppendSeparator();
1449 file_menu->Append(CONTROLS_ABOUT, "&About\tF1");
1450 file_menu->AppendSeparator();
1451 file_menu->Append(CONTROLS_QUIT, "E&xit\tAlt-X", "Quit controls sample");
1452
1453 wxMenuBar *menu_bar = new wxMenuBar;
1454 menu_bar->Append(file_menu, "&File");
1455
1456 #if wxUSE_TOOLTIPS
1457 wxMenu *tooltip_menu = new wxMenu;
1458 tooltip_menu->Append(CONTROLS_SET_TOOLTIP_DELAY, "Set &delay\tCtrl-D");
1459 tooltip_menu->AppendSeparator();
1460 tooltip_menu->Append(CONTROLS_ENABLE_TOOLTIPS, "&Toggle tooltips\tCtrl-T",
1461 "enable/disable tooltips", TRUE);
1462 tooltip_menu->Check(CONTROLS_ENABLE_TOOLTIPS, TRUE);
1463 menu_bar->Append(tooltip_menu, "&Tooltips");
1464 #endif // wxUSE_TOOLTIPS
1465
1466 wxMenu *panel_menu = new wxMenu;
1467 panel_menu->Append(CONTROLS_ENABLE_ALL, "&Disable all\tCtrl-E",
1468 "Enable/disable all panel controls", TRUE);
1469 menu_bar->Append(panel_menu, "&Panel");
1470
1471 SetMenuBar(menu_bar);
1472
1473 CreateStatusBar(2);
1474
1475 m_panel = new MyPanel( this, 10, 10, 300, 100 );
1476
1477 SetSizeHints( 500, 425 );
1478 }
1479
1480 void MyFrame::OnQuit (wxCommandEvent& WXUNUSED(event) )
1481 {
1482 Close(TRUE);
1483 }
1484
1485 void MyFrame::OnAbout( wxCommandEvent& WXUNUSED(event) )
1486 {
1487 wxBusyCursor bc;
1488
1489 wxMessageDialog dialog(this, "This is a control sample", "About Controls", wxOK );
1490 dialog.ShowModal();
1491 }
1492
1493 void MyFrame::OnClearLog(wxCommandEvent& WXUNUSED(event))
1494 {
1495 m_panel->m_text->Clear();
1496 }
1497
1498 #if wxUSE_TOOLTIPS
1499 void MyFrame::OnSetTooltipDelay(wxCommandEvent& event)
1500 {
1501 static long s_delay = 5000;
1502
1503 wxString delay;
1504 delay.Printf( _T("%ld"), s_delay);
1505
1506 delay = wxGetTextFromUser("Enter delay (in milliseconds)",
1507 "Set tooltip delay",
1508 delay,
1509 this);
1510 if ( !delay )
1511 return; // cancelled
1512
1513 wxSscanf(delay, _T("%ld"), &s_delay);
1514
1515 wxToolTip::SetDelay(s_delay);
1516
1517 wxLogStatus(this, _T("Tooltip delay set to %ld milliseconds"), s_delay);
1518 }
1519
1520 void MyFrame::OnToggleTooltips(wxCommandEvent& event)
1521 {
1522 static bool s_enabled = TRUE;
1523
1524 s_enabled = !s_enabled;
1525
1526 wxToolTip::Enable(s_enabled);
1527
1528 wxLogStatus(this, _T("Tooltips %sabled"), s_enabled ? _T("en") : _T("dis") );
1529 }
1530 #endif // tooltips
1531
1532 void MyFrame::OnEnableAll(wxCommandEvent& WXUNUSED(event))
1533 {
1534 static bool s_enable = TRUE;
1535
1536 s_enable = !s_enable;
1537 m_panel->Enable(s_enable);
1538 }
1539
1540 void MyFrame::OnMove( wxMoveEvent& event )
1541 {
1542 UpdateStatusBar(event.GetPosition(), GetSize());
1543
1544 event.Skip();
1545 }
1546
1547 void MyFrame::OnSize( wxSizeEvent& event )
1548 {
1549 UpdateStatusBar(GetPosition(), event.GetSize());
1550
1551 event.Skip();
1552 }
1553
1554 void MyFrame::OnIdle( wxIdleEvent& WXUNUSED(event) )
1555 {
1556 // track the window which has the focus in the status bar
1557 static wxWindow *s_windowFocus = (wxWindow *)NULL;
1558 wxWindow *focus = wxWindow::FindFocus();
1559 if ( focus && (focus != s_windowFocus) )
1560 {
1561 s_windowFocus = focus;
1562
1563 wxString msg;
1564 msg.Printf(
1565 #ifdef __WXMSW__
1566 _T("Focus: %s, HWND = %08x"),
1567 #else
1568 _T("Focus: %s"),
1569 #endif
1570 s_windowFocus->GetClassInfo()->GetClassName()
1571 #ifdef __WXMSW__
1572 , (unsigned int) s_windowFocus->GetHWND()
1573 #endif
1574 );
1575
1576 SetStatusText(msg);
1577 }
1578 }
1579
1580 void MyComboBox::OnChar(wxKeyEvent& event)
1581 {
1582 wxLogMessage(_T("MyComboBox::OnChar"));
1583
1584 if ( event.KeyCode() == 'w' )
1585 wxLogMessage(_T("MyComboBox: 'w' will be ignored."));
1586 else
1587 event.Skip();
1588 }
1589
1590 void MyComboBox::OnKeyDown(wxKeyEvent& event)
1591 {
1592 wxLogMessage(_T("MyComboBox::OnKeyDown"));
1593
1594 if ( event.KeyCode() == 'w' )
1595 wxLogMessage(_T("MyComboBox: 'w' will be ignored."));
1596 else
1597 event.Skip();
1598 }
1599
1600 void MyComboBox::OnKeyUp(wxKeyEvent& event)
1601 {
1602 wxLogMessage(_T("MyComboBox::OnKeyUp"));
1603
1604 event.Skip();
1605 }
1606
1607 static void SetListboxClientData(const wxChar *name, wxListBox *control)
1608 {
1609 size_t count = control->GetCount();
1610 for ( size_t n = 0; n < count; n++ )
1611 {
1612 wxString s;
1613 s.Printf(wxT("%s client data for '%s'"),
1614 name, control->GetString(n).c_str());
1615
1616 control->SetClientObject(n, new wxStringClientData(s));
1617 }
1618 }
1619
1620 static void SetChoiceClientData(const wxChar *name, wxChoice *control)
1621 {
1622 size_t count = control->GetCount();
1623 for ( size_t n = 0; n < count; n++ )
1624 {
1625 wxString s;
1626 s.Printf(wxT("%s client data for '%s'"),
1627 name, control->GetString(n).c_str());
1628
1629 control->SetClientObject(n, new wxStringClientData(s));
1630 }
1631 }