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