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