]>
git.saurik.com Git - wxWidgets.git/blob - samples/controls/controls.cpp
fa791e095fe73cee7f16215978e05b656a673dbc
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Controls wxWindows sample
4 // Author: Robert Roebling
7 // Copyright: (c) Robert Roebling, Julian Smart
8 // Licence: wxWindows license
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "controls.h"
15 // For compilers that support precompilation, includes "wx/wx.h".
16 #include "wx/wxprec.h"
26 #include "wx/notebook.h"
27 #include "wx/imaglist.h"
29 #if defined(__WXGTK__) || defined(__WXMOTIF__)
30 #include "mondrian.xpm"
31 #include "icons/choice.xpm"
32 #include "icons/combo.xpm"
33 #include "icons/list.xpm"
34 #include "icons/radio.xpm"
35 #include "icons/text.xpm"
36 #include "icons/gauge.xpm"
39 //----------------------------------------------------------------------
41 //----------------------------------------------------------------------
43 class MyApp
: public wxApp
49 class MyPanel
: public wxPanel
53 MyPanel(wxFrame
*frame
, int x
, int y
, int w
, int h
);
56 void OnSize( wxSizeEvent
& event
);
57 void OnListBox( wxCommandEvent
&event
);
58 void OnListBoxButtons( wxCommandEvent
&event
);
59 void OnChoice( wxCommandEvent
&event
);
60 void OnChoiceButtons( wxCommandEvent
&event
);
61 void OnCombo( wxCommandEvent
&event
);
62 void OnComboButtons( wxCommandEvent
&event
);
63 void OnRadio( wxCommandEvent
&event
);
64 void OnRadioButtons( wxCommandEvent
&event
);
65 void OnSetFont( wxCommandEvent
&event
);
66 void OnPageChanged( wxNotebookEvent
&event
);
67 void OnSliderUpdate( wxCommandEvent
&event
);
75 wxButton
*m_fontButton
;
78 wxNotebook
*m_notebook
;
83 class MyFrame
: public wxFrame
87 MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
);
91 void OnQuit(wxCommandEvent
& event
);
92 void OnAbout(wxCommandEvent
& event
);
93 bool OnClose(void) { return TRUE
; }
98 //----------------------------------------------------------------------
100 //----------------------------------------------------------------------
102 IMPLEMENT_APP (MyApp
)
104 //----------------------------------------------------------------------
106 //----------------------------------------------------------------------
108 const int MINIMAL_QUIT
= 100;
109 const int MINIMAL_TEXT
= 101;
110 const int MINIMAL_ABOUT
= 102;
112 bool MyApp::OnInit(void)
114 // Create the main frame window
115 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, (char *) "Controls wxWindows App", 50, 50, 530, 420 );
119 frame
->SetIcon(wxIcon("mondrian"));
121 frame
->SetIcon(wxIcon( mondrian_xpm
));
124 wxMenu
*file_menu
= new wxMenu
;
126 file_menu
->Append(MINIMAL_ABOUT
, "&About");
127 file_menu
->Append(MINIMAL_QUIT
, "E&xit");
128 wxMenuBar
*menu_bar
= new wxMenuBar
;
129 menu_bar
->Append(file_menu
, "&File");
130 frame
->SetMenuBar(menu_bar
);
139 //----------------------------------------------------------------------
141 //----------------------------------------------------------------------
143 const ID_NOTEBOOK
= 1000;
145 const ID_LISTBOX
= 130;
146 const ID_LISTBOX_SEL_NUM
= 131;
147 const ID_LISTBOX_SEL_STR
= 132;
148 const ID_LISTBOX_CLEAR
= 133;
149 const ID_LISTBOX_APPEND
= 134;
150 const ID_LISTBOX_DELETE
= 135;
151 const ID_LISTBOX_FONT
= 136;
152 const ID_LISTBOX_ENABLE
= 137;
154 const ID_CHOICE
= 120;
155 const ID_CHOICE_SEL_NUM
= 121;
156 const ID_CHOICE_SEL_STR
= 122;
157 const ID_CHOICE_CLEAR
= 123;
158 const ID_CHOICE_APPEND
= 124;
159 const ID_CHOICE_DELETE
= 125;
160 const ID_CHOICE_FONT
= 126;
161 const ID_CHOICE_ENABLE
= 127;
163 const ID_COMBO
= 140;
164 const ID_COMBO_SEL_NUM
= 141;
165 const ID_COMBO_SEL_STR
= 142;
166 const ID_COMBO_CLEAR
= 143;
167 const ID_COMBO_APPEND
= 144;
168 const ID_COMBO_DELETE
= 145;
169 const ID_COMBO_FONT
= 146;
170 const ID_COMBO_ENABLE
= 147;
174 const ID_RADIOBOX
= 160;
175 const ID_RADIOBOX_SEL_NUM
= 161;
176 const ID_RADIOBOX_SEL_STR
= 162;
177 const ID_RADIOBOX_FONT
= 163;
178 const ID_RADIOBOX_ENABLE
= 164;
180 const ID_SET_FONT
= 170;
182 const ID_GAUGE
= 180;
183 const ID_SLIDER
= 181;
186 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
187 EVT_SIZE ( MyPanel::OnSize
)
188 EVT_NOTEBOOK_PAGE_CHANGED(ID_NOTEBOOK
, MyPanel::OnPageChanged
)
189 EVT_LISTBOX (ID_LISTBOX
, MyPanel::OnListBox
)
190 EVT_BUTTON (ID_LISTBOX_SEL_NUM
, MyPanel::OnListBoxButtons
)
191 EVT_BUTTON (ID_LISTBOX_SEL_STR
, MyPanel::OnListBoxButtons
)
192 EVT_BUTTON (ID_LISTBOX_CLEAR
, MyPanel::OnListBoxButtons
)
193 EVT_BUTTON (ID_LISTBOX_APPEND
, MyPanel::OnListBoxButtons
)
194 EVT_BUTTON (ID_LISTBOX_DELETE
, MyPanel::OnListBoxButtons
)
195 EVT_BUTTON (ID_LISTBOX_FONT
, MyPanel::OnListBoxButtons
)
196 EVT_CHECKBOX (ID_LISTBOX_ENABLE
, MyPanel::OnListBoxButtons
)
197 EVT_CHOICE (ID_CHOICE
, MyPanel::OnChoice
)
198 EVT_BUTTON (ID_CHOICE_SEL_NUM
, MyPanel::OnChoiceButtons
)
199 EVT_BUTTON (ID_CHOICE_SEL_STR
, MyPanel::OnChoiceButtons
)
200 EVT_BUTTON (ID_CHOICE_CLEAR
, MyPanel::OnChoiceButtons
)
201 EVT_BUTTON (ID_CHOICE_APPEND
, MyPanel::OnChoiceButtons
)
202 EVT_BUTTON (ID_CHOICE_DELETE
, MyPanel::OnChoiceButtons
)
203 EVT_BUTTON (ID_CHOICE_FONT
, MyPanel::OnChoiceButtons
)
204 EVT_CHECKBOX (ID_CHOICE_ENABLE
, MyPanel::OnChoiceButtons
)
205 EVT_CHOICE (ID_COMBO
, MyPanel::OnCombo
)
206 EVT_BUTTON (ID_COMBO_SEL_NUM
, MyPanel::OnComboButtons
)
207 EVT_BUTTON (ID_COMBO_SEL_STR
, MyPanel::OnComboButtons
)
208 EVT_BUTTON (ID_COMBO_CLEAR
, MyPanel::OnComboButtons
)
209 EVT_BUTTON (ID_COMBO_APPEND
, MyPanel::OnComboButtons
)
210 EVT_BUTTON (ID_COMBO_DELETE
, MyPanel::OnComboButtons
)
211 EVT_BUTTON (ID_COMBO_FONT
, MyPanel::OnComboButtons
)
212 EVT_CHECKBOX (ID_COMBO_ENABLE
, MyPanel::OnComboButtons
)
213 EVT_RADIOBOX (ID_RADIOBOX
, MyPanel::OnRadio
)
214 EVT_BUTTON (ID_RADIOBOX_SEL_NUM
, MyPanel::OnRadioButtons
)
215 EVT_BUTTON (ID_RADIOBOX_SEL_STR
, MyPanel::OnRadioButtons
)
216 EVT_BUTTON (ID_RADIOBOX_FONT
, MyPanel::OnRadioButtons
)
217 EVT_CHECKBOX (ID_RADIOBOX_ENABLE
, MyPanel::OnRadioButtons
)
218 EVT_BUTTON (ID_SET_FONT
, MyPanel::OnSetFont
)
219 EVT_SLIDER (ID_SLIDER
, MyPanel::OnSliderUpdate
)
222 MyPanel::MyPanel( wxFrame
*frame
, int x
, int y
, int w
, int h
) :
223 wxPanel( frame
, -1, wxPoint(x
, y
), wxSize(w
, h
) )
225 SetBackgroundColour("cadet blue");
227 m_text
= new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(0,50), wxSize(100,50), wxTE_MULTILINE
);
228 m_text
->SetBackgroundColour("wheat");
230 m_notebook
= new wxNotebook( this, ID_NOTEBOOK
, wxPoint(0,0), wxSize(200,150) );
241 // image ids and names
244 Image_List
, Image_Choice
, Image_Combo
, Image_Text
, Image_Radio
, Image_Gauge
, Image_Max
247 // fill the image list
249 const char *aIconNames
[] =
251 "list.xpm", "choice.xpm", "combo.xpm", "text.xpm", "radio.xpm", "gauge.txt"
254 wxASSERT( WXSIZEOF(aIconNames
) == Image_Max
); // keep in sync
256 wxString strIconDir
= "icons/";
258 wxImageList
*imagelist
= new wxImageList(32, 32);
259 for ( size_t n
= 0; n
< Image_Max
; n
++ )
261 imagelist
->Add(wxBitmap(strIconDir
+ aIconNames
[n
]));
264 wxImageList
*imagelist
= new wxImageList(32, 32);
266 imagelist
-> Add( wxBitmap( list_xpm
));
267 imagelist
-> Add( wxBitmap( choice_xpm
));
268 imagelist
-> Add( wxBitmap( combo_xpm
));
269 imagelist
-> Add( wxBitmap( text_xpm
));
270 imagelist
-> Add( wxBitmap( radio_xpm
));
271 imagelist
-> Add( wxBitmap( gauge_xpm
));
274 wxButton
*button
= (wxButton
*)NULL
;
276 m_notebook
->SetImageList(imagelist
);
277 m_notebook
->SetBackgroundColour("cadet blue");
279 wxPanel
*panel
= (wxPanel
*) NULL
;
280 panel
= new wxPanel(m_notebook
);
281 panel
->SetBackgroundColour("cadet blue");
282 panel
->SetForegroundColour("blue");
283 m_listbox
= new wxListBox( panel
, ID_LISTBOX
, wxPoint(10,10), wxSize(120,70), 5, choices
);
284 m_listbox
->SetBackgroundColour("wheat");
285 (void)new wxButton( panel
, ID_LISTBOX_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(140,30) );
286 (void)new wxButton( panel
, ID_LISTBOX_SEL_STR
, "Select 'This'", wxPoint(340,30), wxSize(140,30) );
287 (void)new wxButton( panel
, ID_LISTBOX_CLEAR
, "Clear", wxPoint(180,80), wxSize(140,30) );
288 (void)new wxButton( panel
, ID_LISTBOX_APPEND
, "Append 'Hi!'", wxPoint(340,80), wxSize(140,30) );
289 (void)new wxButton( panel
, ID_LISTBOX_DELETE
, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
290 button
= new wxButton( panel
, ID_LISTBOX_FONT
, "Set Italic font", wxPoint(340,130), wxSize(140,30) );
291 button
->SetForegroundColour( "red" );
292 (void)new wxCheckBox( panel
, ID_LISTBOX_ENABLE
, "Disable", wxPoint(20,130), wxSize(140,30) );
293 m_notebook
->AddPage(panel
, "wxList", FALSE
, Image_List
);
295 panel
= new wxPanel(m_notebook
);
296 panel
->SetBackgroundColour("cadet blue");
297 panel
->SetForegroundColour("blue");
298 m_choice
= new wxChoice( panel
, ID_CHOICE
, wxPoint(10,10), wxSize(120,-1), 5, choices
);
299 m_choice
->SetBackgroundColour("wheat");
300 (void)new wxButton( panel
, ID_CHOICE_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(140,30) );
301 (void)new wxButton( panel
, ID_CHOICE_SEL_STR
, "Select 'This'", wxPoint(340,30), wxSize(140,30) );
302 (void)new wxButton( panel
, ID_CHOICE_CLEAR
, "Clear", wxPoint(180,80), wxSize(140,30) );
303 (void)new wxButton( panel
, ID_CHOICE_APPEND
, "Append 'Hi!'", wxPoint(340,80), wxSize(140,30) );
304 (void)new wxButton( panel
, ID_CHOICE_DELETE
, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
305 (void)new wxButton( panel
, ID_CHOICE_FONT
, "Set Italic font", wxPoint(340,130), wxSize(140,30) );
306 (void)new wxCheckBox( panel
, ID_CHOICE_ENABLE
, "Disable", wxPoint(20,130), wxSize(140,30) );
307 m_notebook
->AddPage(panel
, "wxChoice", FALSE
, Image_Choice
);
309 panel
= new wxPanel(m_notebook
);
310 panel
->SetBackgroundColour("cadet blue");
311 panel
->SetForegroundColour("blue");
312 m_combo
= new wxComboBox( panel
, ID_COMBO
, "This", wxPoint(10,10), wxSize(120,-1), 5, choices
);
313 m_combo
->SetBackgroundColour("wheat");
314 (void)new wxButton( panel
, ID_COMBO_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(140,30) );
315 (void)new wxButton( panel
, ID_COMBO_SEL_STR
, "Select 'This'", wxPoint(340,30), wxSize(140,30) );
316 (void)new wxButton( panel
, ID_COMBO_CLEAR
, "Clear", wxPoint(180,80), wxSize(140,30) );
317 (void)new wxButton( panel
, ID_COMBO_APPEND
, "Append 'Hi!'", wxPoint(340,80), wxSize(140,30) );
318 (void)new wxButton( panel
, ID_COMBO_DELETE
, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
319 (void)new wxButton( panel
, ID_COMBO_FONT
, "Set Italic font", wxPoint(340,130), wxSize(140,30) );
320 (void)new wxCheckBox( panel
, ID_COMBO_ENABLE
, "Disable", wxPoint(20,130), wxSize(140,30) );
321 m_notebook
->AddPage(panel
, "wxComboBox", FALSE
, Image_Combo
);
323 panel
= new wxPanel(m_notebook
);
324 panel
->SetBackgroundColour("cadet blue");
325 panel
->SetForegroundColour("blue");
326 wxTextCtrl
*tc
= new wxTextCtrl( panel
, ID_TEXT
, "Write text here.", wxPoint(10,10), wxSize(350,28));
327 tc
->SetBackgroundColour("wheat");
328 tc
= new wxTextCtrl( panel
, ID_TEXT
, "And here.", wxPoint(10,50), wxSize(350,160), wxTE_MULTILINE
);
329 tc
->SetBackgroundColour("wheat");
330 m_notebook
->AddPage(panel
, "wxTextCtrl" , FALSE
, Image_Text
);
332 wxString choices2
[] =
338 panel
= new wxPanel(m_notebook
);
339 panel
->SetBackgroundColour("cadet blue");
340 panel
->SetForegroundColour("blue");
341 m_radio
= new wxRadioBox( panel
, ID_RADIOBOX
, "That", wxPoint(10,160), wxSize(-1,-1), 2, choices2
, 1, wxRA_HORIZONTAL
);
342 m_radio
->SetBackgroundColour("wheat");
343 m_radio
= new wxRadioBox( panel
, ID_RADIOBOX
, "This", wxPoint(10,10), wxSize(-1,-1), 5, choices
, 1, wxRA_VERTICAL
);
344 m_radio
->SetBackgroundColour("wheat");
345 (void)new wxButton( panel
, ID_RADIOBOX_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(140,30) );
346 (void)new wxButton( panel
, ID_RADIOBOX_SEL_STR
, "Select 'This'", wxPoint(180,80), wxSize(140,30) );
347 (void)new wxButton( panel
, ID_RADIOBOX_FONT
, "Set Italic font", wxPoint(180,130), wxSize(140,30) );
348 (void)new wxCheckBox( panel
, ID_RADIOBOX_ENABLE
, "Disable", wxPoint(340,130), wxSize(140,30) );
349 m_fontButton
= new wxButton( panel
, ID_SET_FONT
, "Set more Italic font", wxPoint(340,30), wxSize(160,30) );
350 m_notebook
->AddPage(panel
, "wxRadioBox", FALSE
, Image_Radio
);
352 panel
= new wxPanel(m_notebook
);
353 panel
->SetBackgroundColour("cadet blue");
354 panel
->SetForegroundColour("blue");
355 (void)new wxStaticBox( panel
, -1, "wxGauge and wxSlider", wxPoint(10,10), wxSize(180,130) );
356 m_gauge
= new wxGauge( panel
, -1, 200, wxPoint(18,50), wxSize(155,-1) );
357 m_gauge
->SetBackgroundColour("wheat");
358 m_slider
= new wxSlider( panel
, ID_SLIDER
, 0, 0, 200, wxPoint(18,90), wxSize(155,-1) );
359 m_slider
->SetBackgroundColour("wheat");
360 (void)new wxStaticBox( panel
, -1, "Explanation", wxPoint(200,10), wxSize(290,130) );
361 (void)new wxStaticText( panel
, -1,
362 "In order see the gauge (aka progress bar)\n"
363 "control do something you have to drag the\n"
364 "handle of the slider to the right.\n"
366 "This is also supposed to demonstrate how\n"
367 "to use static controls.\n",
369 m_notebook
->AddPage(panel
, "wxGauge", FALSE
, Image_Gauge
);
372 void MyPanel::OnSize( wxSizeEvent
& WXUNUSED(event
) )
376 GetClientSize( &x
, &y
);
378 if (m_notebook
) m_notebook
->SetSize( 2, 2, x
-4, y
*2/3-4 );
379 if (m_text
) m_text
->SetSize( 2, y
*2/3+2, x
-4, y
/3-4 );
382 void MyPanel::OnPageChanged( wxNotebookEvent
&event
)
384 *m_text
<< "Notebook selection is " << event
.GetSelection() << "\n";
387 void MyPanel::OnListBox( wxCommandEvent
&event
)
389 m_text
->WriteText( "ListBox selection string is: " );
390 m_text
->WriteText( event
.GetString() );
391 m_text
->WriteText( "\n" );
394 void MyPanel::OnListBoxButtons( wxCommandEvent
&event
)
396 switch (event
.GetId())
398 case ID_LISTBOX_ENABLE
:
400 m_listbox
->Enable( !((bool)event
.GetInt()) );
403 case ID_LISTBOX_SEL_NUM
:
405 m_listbox
->SetSelection( 2 );
408 case ID_LISTBOX_SEL_STR
:
410 m_listbox
->SetStringSelection( "This" );
413 case ID_LISTBOX_CLEAR
:
418 case ID_LISTBOX_APPEND
:
420 m_listbox
->Append( "Hi!" );
423 case ID_LISTBOX_DELETE
:
425 int idx
= m_listbox
->GetSelection();
426 m_listbox
->Delete( idx
);
429 case ID_LISTBOX_FONT
:
431 m_listbox
->SetFont( *wxITALIC_FONT
);
437 void MyPanel::OnChoice( wxCommandEvent
&event
)
439 m_text
->WriteText( "Choice selection string is: " );
440 m_text
->WriteText( event
.GetString() );
441 m_text
->WriteText( "\n" );
444 void MyPanel::OnChoiceButtons( wxCommandEvent
&event
)
446 switch (event
.GetId())
448 case ID_CHOICE_ENABLE
:
450 m_choice
->Enable( !((bool)event
.GetInt()) );
453 case ID_CHOICE_SEL_NUM
:
455 m_choice
->SetSelection( 2 );
458 case ID_CHOICE_SEL_STR
:
460 m_choice
->SetStringSelection( "This" );
463 case ID_CHOICE_CLEAR
:
468 case ID_CHOICE_APPEND
:
470 m_choice
->Append( "Hi!" );
473 case ID_CHOICE_DELETE
:
475 int idx
= m_choice
->GetSelection();
476 m_choice
->Delete( idx
);
481 m_choice
->SetFont( *wxITALIC_FONT
);
487 void MyPanel::OnCombo( wxCommandEvent
&event
)
489 m_text
->WriteText( "ComboBox selection string is: " );
490 m_text
->WriteText( event
.GetString() );
491 m_text
->WriteText( "\n" );
494 void MyPanel::OnComboButtons( wxCommandEvent
&event
)
496 switch (event
.GetId())
498 case ID_COMBO_ENABLE
:
500 m_combo
->Enable( !((bool)event
.GetInt()) );
503 case ID_COMBO_SEL_NUM
:
505 m_combo
->SetSelection( 2 );
508 case ID_COMBO_SEL_STR
:
510 m_combo
->SetStringSelection( "This" );
518 case ID_COMBO_APPEND
:
520 m_combo
->Append( "Hi!" );
523 case ID_COMBO_DELETE
:
525 int idx
= m_combo
->GetSelection();
526 m_combo
->Delete( idx
);
531 m_combo
->SetFont( *wxITALIC_FONT
);
537 void MyPanel::OnRadio( wxCommandEvent
&event
)
539 m_text
->WriteText( "RadioBox selection string is: " );
540 m_text
->WriteText( event
.GetString() );
541 m_text
->WriteText( "\n" );
544 void MyPanel::OnRadioButtons( wxCommandEvent
&event
)
546 switch (event
.GetId())
548 case ID_RADIOBOX_ENABLE
:
550 m_radio
->Enable( !((bool)event
.GetInt()) );
553 case ID_RADIOBOX_SEL_NUM
:
555 m_radio
->SetSelection( 2 );
558 case ID_RADIOBOX_SEL_STR
:
560 m_radio
->SetStringSelection( "This" );
563 case ID_RADIOBOX_FONT
:
565 m_radio
->SetFont( *wxITALIC_FONT
);
571 void MyPanel::OnSetFont( wxCommandEvent
&WXUNUSED(event
) )
573 m_fontButton
->SetFont( *wxITALIC_FONT
);
574 m_text
->SetFont( *wxITALIC_FONT
);
577 void MyPanel::OnSliderUpdate( wxCommandEvent
&WXUNUSED(event
) )
579 m_gauge
->SetValue( m_slider
->GetValue() );
584 delete m_notebook
->GetImageList();
587 //----------------------------------------------------------------------
589 //----------------------------------------------------------------------
591 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
592 EVT_MENU(MINIMAL_QUIT
, MyFrame::OnQuit
)
593 EVT_MENU(MINIMAL_ABOUT
, MyFrame::OnAbout
)
596 MyFrame::MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
):
597 wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
599 (void)new MyPanel( this, 10, 10, 300, 100 );
602 void MyFrame::OnQuit (wxCommandEvent
& WXUNUSED(event
) )
607 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
609 wxMessageDialog
dialog(this, "This is a control sample", "About Controls", wxOK
);