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