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