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