]>
git.saurik.com Git - wxWidgets.git/blob - samples/controls/controls.cpp
7e784c9590ec8cdbffa67faf48cd30d41ff10e45
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
) << " More text.";
328 tc
->SetBackgroundColour("wheat");
329 tc
= new wxTextCtrl( panel
, ID_TEXT
, "And here.", wxPoint(10,50), wxSize(350,160), wxTE_MULTILINE
);
330 (*tc
) << " More text.";
331 tc
->SetBackgroundColour("wheat");
332 m_notebook
->AddPage(panel
, "wxTextCtrl" , FALSE
, Image_Text
);
334 wxString choices2
[] =
340 panel
= new wxPanel(m_notebook
);
341 panel
->SetBackgroundColour("cadet blue");
342 panel
->SetForegroundColour("blue");
343 m_radio
= new wxRadioBox( panel
, ID_RADIOBOX
, "That", wxPoint(10,160), wxSize(-1,-1), 2, choices2
, 1, wxRA_HORIZONTAL
);
344 m_radio
->SetBackgroundColour("wheat");
345 m_radio
= new wxRadioBox( panel
, ID_RADIOBOX
, "This", wxPoint(10,10), wxSize(-1,-1), 5, choices
, 1, wxRA_VERTICAL
);
346 m_radio
->SetBackgroundColour("wheat");
347 (void)new wxButton( panel
, ID_RADIOBOX_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(140,30) );
348 (void)new wxButton( panel
, ID_RADIOBOX_SEL_STR
, "Select 'This'", wxPoint(180,80), wxSize(140,30) );
349 (void)new wxButton( panel
, ID_RADIOBOX_FONT
, "Set Italic font", wxPoint(180,130), wxSize(140,30) );
350 (void)new wxCheckBox( panel
, ID_RADIOBOX_ENABLE
, "Disable", wxPoint(340,130), wxSize(140,30) );
351 m_fontButton
= new wxButton( panel
, ID_SET_FONT
, "Set more Italic font", wxPoint(340,30), wxSize(160,30) );
352 m_notebook
->AddPage(panel
, "wxRadioBox", FALSE
, Image_Radio
);
354 panel
= new wxPanel(m_notebook
);
355 panel
->SetBackgroundColour("cadet blue");
356 panel
->SetForegroundColour("blue");
357 (void)new wxStaticBox( panel
, -1, "wxGauge and wxSlider", wxPoint(10,10), wxSize(180,130) );
358 m_gauge
= new wxGauge( panel
, -1, 200, wxPoint(18,50), wxSize(155,-1) );
359 m_gauge
->SetBackgroundColour("wheat");
360 m_slider
= new wxSlider( panel
, ID_SLIDER
, 0, 0, 200, wxPoint(18,90), wxSize(155,-1) );
361 m_slider
->SetBackgroundColour("wheat");
362 (void)new wxStaticBox( panel
, -1, "Explanation", wxPoint(200,10), wxSize(290,130) );
363 (void)new wxStaticText( panel
, -1,
364 "In order see the gauge (aka progress bar)\n"
365 "control do something you have to drag the\n"
366 "handle of the slider to the right.\n"
368 "This is also supposed to demonstrate how\n"
369 "to use static controls.\n",
371 m_notebook
->AddPage(panel
, "wxGauge", FALSE
, Image_Gauge
);
374 void MyPanel::OnSize( wxSizeEvent
& WXUNUSED(event
) )
378 GetClientSize( &x
, &y
);
380 if (m_notebook
) m_notebook
->SetSize( 2, 2, x
-4, y
*2/3-4 );
381 if (m_text
) m_text
->SetSize( 2, y
*2/3+2, x
-4, y
/3-4 );
384 void MyPanel::OnPageChanged( wxNotebookEvent
&event
)
386 *m_text
<< "Notebook selection is " << event
.GetSelection() << "\n";
389 void MyPanel::OnListBox( wxCommandEvent
&event
)
391 m_text
->WriteText( "ListBox selection string is: " );
392 m_text
->WriteText( event
.GetString() );
393 m_text
->WriteText( "\n" );
396 void MyPanel::OnListBoxButtons( wxCommandEvent
&event
)
398 switch (event
.GetId())
400 case ID_LISTBOX_ENABLE
:
402 m_listbox
->Enable( !((bool)event
.GetInt()) );
405 case ID_LISTBOX_SEL_NUM
:
407 m_listbox
->SetSelection( 2 );
410 case ID_LISTBOX_SEL_STR
:
412 m_listbox
->SetStringSelection( "This" );
415 case ID_LISTBOX_CLEAR
:
420 case ID_LISTBOX_APPEND
:
422 m_listbox
->Append( "Hi!" );
425 case ID_LISTBOX_DELETE
:
427 int idx
= m_listbox
->GetSelection();
428 m_listbox
->Delete( idx
);
431 case ID_LISTBOX_FONT
:
433 m_listbox
->SetFont( *wxITALIC_FONT
);
439 void MyPanel::OnChoice( wxCommandEvent
&event
)
441 m_text
->WriteText( "Choice selection string is: " );
442 m_text
->WriteText( event
.GetString() );
443 m_text
->WriteText( "\n" );
446 void MyPanel::OnChoiceButtons( wxCommandEvent
&event
)
448 switch (event
.GetId())
450 case ID_CHOICE_ENABLE
:
452 m_choice
->Enable( !((bool)event
.GetInt()) );
455 case ID_CHOICE_SEL_NUM
:
457 m_choice
->SetSelection( 2 );
460 case ID_CHOICE_SEL_STR
:
462 m_choice
->SetStringSelection( "This" );
465 case ID_CHOICE_CLEAR
:
470 case ID_CHOICE_APPEND
:
472 m_choice
->Append( "Hi!" );
475 case ID_CHOICE_DELETE
:
477 int idx
= m_choice
->GetSelection();
478 m_choice
->Delete( idx
);
483 m_choice
->SetFont( *wxITALIC_FONT
);
489 void MyPanel::OnCombo( wxCommandEvent
&event
)
491 m_text
->WriteText( "ComboBox selection string is: " );
492 m_text
->WriteText( event
.GetString() );
493 m_text
->WriteText( "\n" );
496 void MyPanel::OnComboButtons( wxCommandEvent
&event
)
498 switch (event
.GetId())
500 case ID_COMBO_ENABLE
:
502 m_combo
->Enable( !((bool)event
.GetInt()) );
505 case ID_COMBO_SEL_NUM
:
507 m_combo
->SetSelection( 2 );
510 case ID_COMBO_SEL_STR
:
512 m_combo
->SetStringSelection( "This" );
520 case ID_COMBO_APPEND
:
522 m_combo
->Append( "Hi!" );
525 case ID_COMBO_DELETE
:
527 int idx
= m_combo
->GetSelection();
528 m_combo
->Delete( idx
);
533 m_combo
->SetFont( *wxITALIC_FONT
);
539 void MyPanel::OnRadio( wxCommandEvent
&event
)
541 m_text
->WriteText( "RadioBox selection string is: " );
542 m_text
->WriteText( event
.GetString() );
543 m_text
->WriteText( "\n" );
546 void MyPanel::OnRadioButtons( wxCommandEvent
&event
)
548 switch (event
.GetId())
550 case ID_RADIOBOX_ENABLE
:
552 m_radio
->Enable( !((bool)event
.GetInt()) );
555 case ID_RADIOBOX_SEL_NUM
:
557 m_radio
->SetSelection( 2 );
560 case ID_RADIOBOX_SEL_STR
:
562 m_radio
->SetStringSelection( "This" );
565 case ID_RADIOBOX_FONT
:
567 m_radio
->SetFont( *wxITALIC_FONT
);
573 void MyPanel::OnSetFont( wxCommandEvent
&WXUNUSED(event
) )
575 m_fontButton
->SetFont( *wxITALIC_FONT
);
576 m_text
->SetFont( *wxITALIC_FONT
);
579 void MyPanel::OnSliderUpdate( wxCommandEvent
&WXUNUSED(event
) )
581 m_gauge
->SetValue( m_slider
->GetValue() );
586 delete m_notebook
->GetImageList();
589 //----------------------------------------------------------------------
591 //----------------------------------------------------------------------
593 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
594 EVT_MENU(MINIMAL_QUIT
, MyFrame::OnQuit
)
595 EVT_MENU(MINIMAL_ABOUT
, MyFrame::OnAbout
)
598 MyFrame::MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
):
599 wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
601 (void)new MyPanel( this, 10, 10, 300, 100 );
604 void MyFrame::OnQuit (wxCommandEvent
& WXUNUSED(event
) )
609 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
611 wxMessageDialog
dialog(this, "This is a control sample", "About Controls", wxOK
);