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