]>
git.saurik.com Git - wxWidgets.git/blob - samples/controls/controls.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Controls wxWindows sample
4 // Author: Robert Roebling
7 // Copyright: (c) Robert Roebling, Julian Smart and Markus Holzem
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"
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"
38 //----------------------------------------------------------------------
40 //----------------------------------------------------------------------
42 class MyApp
: public wxApp
48 class MyPanel
: public wxPanel
52 MyPanel(wxFrame
*frame
, int x
, int y
, int w
, int h
);
55 void OnSize( wxSizeEvent
& event
);
56 void OnListBox( wxCommandEvent
&event
);
57 void OnListBoxButtons( wxCommandEvent
&event
);
58 void OnChoice( wxCommandEvent
&event
);
59 void OnChoiceButtons( wxCommandEvent
&event
);
60 void OnCombo( wxCommandEvent
&event
);
61 void OnComboButtons( wxCommandEvent
&event
);
62 void OnRadio( wxCommandEvent
&event
);
63 void OnRadioButtons( wxCommandEvent
&event
);
64 void OnSetFont( wxCommandEvent
&event
);
65 void OnPageChanged( wxNotebookEvent
&event
);
71 wxButton
*m_fontButton
;
74 wxNotebook
*m_notebook
;
79 class MyFrame
: public wxFrame
83 MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
);
87 void OnQuit(wxCommandEvent
& event
);
88 void OnAbout(wxCommandEvent
& event
);
89 bool OnClose(void) { return TRUE
; }
94 //----------------------------------------------------------------------
96 //----------------------------------------------------------------------
100 //----------------------------------------------------------------------
102 //----------------------------------------------------------------------
104 const int MINIMAL_QUIT
= 100;
105 const int MINIMAL_TEXT
= 101;
106 const int MINIMAL_ABOUT
= 102;
108 bool MyApp::OnInit(void)
110 // Create the main frame window
111 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, (char *) "Controls wxWindows App", 50, 50, 530, 420 );
115 frame
->SetIcon(wxIcon("mondrian"));
117 frame
->SetIcon(wxIcon( mondrian_xpm
));
120 wxMenu
*file_menu
= new wxMenu
;
122 file_menu
->Append(MINIMAL_ABOUT
, "&About");
123 file_menu
->Append(MINIMAL_QUIT
, "E&xit");
124 wxMenuBar
*menu_bar
= new wxMenuBar
;
125 menu_bar
->Append(file_menu
, "&File");
126 frame
->SetMenuBar(menu_bar
);
135 //----------------------------------------------------------------------
137 //----------------------------------------------------------------------
139 const ID_NOTEBOOK
= 1000;
141 const ID_LISTBOX
= 130;
142 const ID_LISTBOX_SEL_NUM
= 131;
143 const ID_LISTBOX_SEL_STR
= 132;
144 const ID_LISTBOX_CLEAR
= 133;
145 const ID_LISTBOX_APPEND
= 134;
146 const ID_LISTBOX_DELETE
= 135;
147 const ID_LISTBOX_FONT
= 136;
148 const ID_LISTBOX_ENABLE
= 137;
150 const ID_CHOICE
= 120;
151 const ID_CHOICE_SEL_NUM
= 121;
152 const ID_CHOICE_SEL_STR
= 122;
153 const ID_CHOICE_CLEAR
= 123;
154 const ID_CHOICE_APPEND
= 124;
155 const ID_CHOICE_DELETE
= 125;
156 const ID_CHOICE_FONT
= 126;
157 const ID_CHOICE_ENABLE
= 127;
159 const ID_COMBO
= 140;
160 const ID_COMBO_SEL_NUM
= 141;
161 const ID_COMBO_SEL_STR
= 142;
162 const ID_COMBO_CLEAR
= 143;
163 const ID_COMBO_APPEND
= 144;
164 const ID_COMBO_DELETE
= 145;
165 const ID_COMBO_FONT
= 146;
166 const ID_COMBO_ENABLE
= 147;
170 const ID_RADIOBOX
= 160;
171 const ID_RADIOBOX_SEL_NUM
= 161;
172 const ID_RADIOBOX_SEL_STR
= 162;
173 const ID_RADIOBOX_FONT
= 163;
174 const ID_RADIOBOX_ENABLE
= 164;
176 const ID_SET_FONT
= 170;
178 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
179 EVT_SIZE ( MyPanel::OnSize
)
180 EVT_NOTEBOOK_PAGE_CHANGED(ID_NOTEBOOK
, MyPanel::OnPageChanged
)
181 EVT_LISTBOX (ID_LISTBOX
, MyPanel::OnListBox
)
182 EVT_BUTTON (ID_LISTBOX_SEL_NUM
, MyPanel::OnListBoxButtons
)
183 EVT_BUTTON (ID_LISTBOX_SEL_STR
, MyPanel::OnListBoxButtons
)
184 EVT_BUTTON (ID_LISTBOX_CLEAR
, MyPanel::OnListBoxButtons
)
185 EVT_BUTTON (ID_LISTBOX_APPEND
, MyPanel::OnListBoxButtons
)
186 EVT_BUTTON (ID_LISTBOX_DELETE
, MyPanel::OnListBoxButtons
)
187 EVT_BUTTON (ID_LISTBOX_FONT
, MyPanel::OnListBoxButtons
)
188 EVT_CHECKBOX (ID_LISTBOX_ENABLE
, MyPanel::OnListBoxButtons
)
189 EVT_CHOICE (ID_CHOICE
, MyPanel::OnChoice
)
190 EVT_BUTTON (ID_CHOICE_SEL_NUM
, MyPanel::OnChoiceButtons
)
191 EVT_BUTTON (ID_CHOICE_SEL_STR
, MyPanel::OnChoiceButtons
)
192 EVT_BUTTON (ID_CHOICE_CLEAR
, MyPanel::OnChoiceButtons
)
193 EVT_BUTTON (ID_CHOICE_APPEND
, MyPanel::OnChoiceButtons
)
194 EVT_BUTTON (ID_CHOICE_DELETE
, MyPanel::OnChoiceButtons
)
195 EVT_BUTTON (ID_CHOICE_FONT
, MyPanel::OnChoiceButtons
)
196 EVT_CHECKBOX (ID_CHOICE_ENABLE
, MyPanel::OnChoiceButtons
)
197 EVT_CHOICE (ID_COMBO
, MyPanel::OnCombo
)
198 EVT_BUTTON (ID_COMBO_SEL_NUM
, MyPanel::OnComboButtons
)
199 EVT_BUTTON (ID_COMBO_SEL_STR
, MyPanel::OnComboButtons
)
200 EVT_BUTTON (ID_COMBO_CLEAR
, MyPanel::OnComboButtons
)
201 EVT_BUTTON (ID_COMBO_APPEND
, MyPanel::OnComboButtons
)
202 EVT_BUTTON (ID_COMBO_DELETE
, MyPanel::OnComboButtons
)
203 EVT_BUTTON (ID_COMBO_FONT
, MyPanel::OnComboButtons
)
204 EVT_CHECKBOX (ID_COMBO_ENABLE
, MyPanel::OnComboButtons
)
205 EVT_RADIOBOX (ID_RADIOBOX
, MyPanel::OnRadio
)
206 EVT_BUTTON (ID_RADIOBOX_SEL_NUM
, MyPanel::OnRadioButtons
)
207 EVT_BUTTON (ID_RADIOBOX_SEL_STR
, MyPanel::OnRadioButtons
)
208 EVT_BUTTON (ID_RADIOBOX_FONT
, MyPanel::OnRadioButtons
)
209 EVT_CHECKBOX (ID_RADIOBOX_ENABLE
, MyPanel::OnRadioButtons
)
210 EVT_BUTTON (ID_SET_FONT
, MyPanel::OnSetFont
)
213 MyPanel::MyPanel( wxFrame
*frame
, int x
, int y
, int w
, int h
) :
214 wxPanel( frame
, -1, wxPoint(x
, y
), wxSize(w
, h
) )
216 m_text
= new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(0,50), wxSize(100,50), wxTE_MULTILINE
);
217 m_text
->SetBackgroundColour("yellow");
219 m_notebook
= new wxNotebook( this, ID_NOTEBOOK
, wxPoint(0,0), wxSize(200,150) );
229 // image ids and names
232 Image_List
, Image_Choice
, Image_Combo
, Image_Text
, Image_Radio
, Image_Max
235 // fill the image list
237 const char *aIconNames
[] =
239 "list.xpm", "choice.xpm", "combo.xpm", "text.xpm", "radio.xpm"
242 wxASSERT( WXSIZEOF(aIconNames
) == Image_Max
); // keep in sync
244 wxString strIconDir
= "icons/";
246 wxImageList
*imagelist
= new wxImageList(32, 32);
247 for ( size_t n
= 0; n
< Image_Max
; n
++ )
249 imagelist
->Add(wxBitmap(strIconDir
+ aIconNames
[n
]));
252 wxImageList
*imagelist
= new wxImageList(32, 32);
254 imagelist
-> Add( wxBitmap( list_xpm
));
255 imagelist
-> Add( wxBitmap( choice_xpm
));
256 imagelist
-> Add( wxBitmap( combo_xpm
));
257 imagelist
-> Add( wxBitmap( text_xpm
));
258 imagelist
-> Add( wxBitmap( radio_xpm
));
261 m_notebook
->SetImageList(imagelist
);
263 wxPanel
*panel
= new wxPanel(m_notebook
);
264 m_listbox
= new wxListBox( panel
, ID_LISTBOX
, wxPoint(10,10), wxSize(120,70), 4, choices
);
265 m_listbox
->SetBackgroundColour("red");
266 (void)new wxButton( panel
, ID_LISTBOX_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(140,30) );
267 (void)new wxButton( panel
, ID_LISTBOX_SEL_STR
, "Select 'This'", wxPoint(340,30), wxSize(140,30) );
268 wxButton
*btn
= new wxButton( panel
, ID_LISTBOX_CLEAR
, "Clear", wxPoint(180,80), wxSize(140,30) );
269 btn
->SetBackgroundColour("green");
270 (void)new wxButton( panel
, ID_LISTBOX_APPEND
, "Append 'Hi!'", wxPoint(340,80), wxSize(140,30) );
271 (void)new wxButton( panel
, ID_LISTBOX_DELETE
, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
272 (void)new wxButton( panel
, ID_LISTBOX_FONT
, "Set Italic font", wxPoint(340,130), wxSize(140,30) );
273 (void)new wxCheckBox( panel
, ID_LISTBOX_ENABLE
, "Disable", wxPoint(20,130), wxSize(140,30) );
274 m_notebook
->AddPage(panel
, "wxList", FALSE
, Image_List
);
276 panel
= new wxPanel(m_notebook
);
277 m_choice
= new wxChoice( panel
, ID_CHOICE
, wxPoint(10,10), wxSize(120,-1), 4, choices
);
278 (void)new wxButton( panel
, ID_CHOICE_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(140,30) );
279 (void)new wxButton( panel
, ID_CHOICE_SEL_STR
, "Select 'This'", wxPoint(340,30), wxSize(140,30) );
280 (void)new wxButton( panel
, ID_CHOICE_CLEAR
, "Clear", wxPoint(180,80), wxSize(140,30) );
281 (void)new wxButton( panel
, ID_CHOICE_APPEND
, "Append 'Hi!'", wxPoint(340,80), wxSize(140,30) );
282 (void)new wxButton( panel
, ID_CHOICE_DELETE
, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
283 (void)new wxButton( panel
, ID_CHOICE_FONT
, "Set Italic font", wxPoint(340,130), wxSize(140,30) );
284 (void)new wxCheckBox( panel
, ID_CHOICE_ENABLE
, "Disable", wxPoint(20,130), wxSize(140,30) );
285 m_notebook
->AddPage(panel
, "wxChoice", FALSE
, Image_Choice
);
287 panel
= new wxPanel(m_notebook
);
288 m_combo
= new wxComboBox( panel
, ID_COMBO
, "This", wxPoint(10,10), wxSize(120,-1), 4, choices
);
289 (void)new wxButton( panel
, ID_COMBO_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(140,30) );
290 (void)new wxButton( panel
, ID_COMBO_SEL_STR
, "Select 'This'", wxPoint(340,30), wxSize(140,30) );
291 (void)new wxButton( panel
, ID_COMBO_CLEAR
, "Clear", wxPoint(180,80), wxSize(140,30) );
292 (void)new wxButton( panel
, ID_COMBO_APPEND
, "Append 'Hi!'", wxPoint(340,80), wxSize(140,30) );
293 (void)new wxButton( panel
, ID_COMBO_DELETE
, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
294 (void)new wxButton( panel
, ID_COMBO_FONT
, "Set Italic font", wxPoint(340,130), wxSize(140,30) );
295 (void)new wxCheckBox( panel
, ID_COMBO_ENABLE
, "Disable", wxPoint(20,130), wxSize(140,30) );
296 m_notebook
->AddPage(panel
, "wxComboBox", FALSE
, Image_Combo
);
298 wxTextCtrl
*text
= new wxTextCtrl( m_notebook
, ID_TEXT
, "Write text here.", wxPoint(10,10), wxSize(120,100), wxTE_MULTILINE
);
299 m_notebook
->AddPage(text
, "wxTextCtrl" , FALSE
, Image_Text
);
301 panel
= new wxPanel(m_notebook
);
302 m_radio
= new wxRadioBox( panel
, ID_RADIOBOX
, "This", wxPoint(10,10), wxSize(-1,-1), 4, choices
);
303 (void)new wxButton( panel
, ID_RADIOBOX_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(140,30) );
304 (void)new wxButton( panel
, ID_RADIOBOX_SEL_STR
, "Select 'This'", wxPoint(180,80), wxSize(140,30) );
305 (void)new wxButton( panel
, ID_RADIOBOX_FONT
, "Set Italic font", wxPoint(180,130), wxSize(140,30) );
306 (void)new wxCheckBox( panel
, ID_RADIOBOX_ENABLE
, "Disable", wxPoint(20,130), wxSize(140,30) );
307 m_fontButton
= new wxButton( panel
, ID_SET_FONT
, "Set more Italic font", wxPoint(340,30), wxSize(160,30) );
308 m_notebook
->AddPage(panel
, "wxRadioBox", FALSE
, Image_Radio
);
311 void MyPanel::OnSize( wxSizeEvent
& WXUNUSED(event
) )
315 GetClientSize( &x
, &y
);
317 if (m_notebook
) m_notebook
->SetSize( 2, 2, x
-4, y
*2/3-4 );
318 if (m_text
) m_text
->SetSize( 2, y
*2/3+2, x
-4, y
/3-4 );
321 void MyPanel::OnPageChanged( wxNotebookEvent
&event
)
323 *m_text
<< "Notebook selection is " << event
.GetSelection() << "\n";
326 void MyPanel::OnListBox( wxCommandEvent
&event
)
328 m_text
->WriteText( "ListBox selection string is: " );
329 m_text
->WriteText( event
.GetString() );
330 m_text
->WriteText( "\n" );
333 void MyPanel::OnListBoxButtons( wxCommandEvent
&event
)
335 switch (event
.GetId())
337 case ID_LISTBOX_ENABLE
:
339 m_listbox
->Enable( !((bool)event
.GetInt()) );
342 case ID_LISTBOX_SEL_NUM
:
344 m_listbox
->SetSelection( 2 );
347 case ID_LISTBOX_SEL_STR
:
349 m_listbox
->SetStringSelection( "This" );
352 case ID_LISTBOX_CLEAR
:
357 case ID_LISTBOX_APPEND
:
359 m_listbox
->Append( "Hi!" );
362 case ID_LISTBOX_DELETE
:
364 int idx
= m_listbox
->GetSelection();
365 m_listbox
->Delete( idx
);
368 case ID_LISTBOX_FONT
:
370 m_listbox
->SetFont( *wxITALIC_FONT
);
376 void MyPanel::OnChoice( wxCommandEvent
&event
)
378 m_text
->WriteText( "Choice selection string is: " );
379 m_text
->WriteText( event
.GetString() );
380 m_text
->WriteText( "\n" );
383 void MyPanel::OnChoiceButtons( wxCommandEvent
&event
)
385 switch (event
.GetId())
387 case ID_CHOICE_ENABLE
:
389 m_choice
->Enable( !((bool)event
.GetInt()) );
392 case ID_CHOICE_SEL_NUM
:
394 m_choice
->SetSelection( 2 );
397 case ID_CHOICE_SEL_STR
:
399 m_choice
->SetStringSelection( "This" );
402 case ID_CHOICE_CLEAR
:
407 case ID_CHOICE_APPEND
:
409 m_choice
->Append( "Hi!" );
412 case ID_CHOICE_DELETE
:
414 int idx
= m_choice
->GetSelection();
415 m_choice
->Delete( idx
);
420 m_choice
->SetFont( *wxITALIC_FONT
);
426 void MyPanel::OnCombo( wxCommandEvent
&event
)
428 m_text
->WriteText( "ComboBox selection string is: " );
429 m_text
->WriteText( event
.GetString() );
430 m_text
->WriteText( "\n" );
433 void MyPanel::OnComboButtons( wxCommandEvent
&event
)
435 switch (event
.GetId())
437 case ID_COMBO_ENABLE
:
439 m_combo
->Enable( !((bool)event
.GetInt()) );
442 case ID_COMBO_SEL_NUM
:
444 m_combo
->SetSelection( 2 );
447 case ID_COMBO_SEL_STR
:
449 m_combo
->SetStringSelection( "This" );
457 case ID_COMBO_APPEND
:
459 m_combo
->Append( "Hi!" );
462 case ID_COMBO_DELETE
:
464 int idx
= m_combo
->GetSelection();
465 m_combo
->Delete( idx
);
470 m_combo
->SetFont( *wxITALIC_FONT
);
476 void MyPanel::OnRadio( wxCommandEvent
&event
)
478 m_text
->WriteText( "RadioBox selection string is: " );
479 m_text
->WriteText( event
.GetString() );
480 m_text
->WriteText( "\n" );
483 void MyPanel::OnRadioButtons( wxCommandEvent
&event
)
485 switch (event
.GetId())
487 case ID_RADIOBOX_ENABLE
:
489 m_radio
->Enable( !((bool)event
.GetInt()) );
492 case ID_RADIOBOX_SEL_NUM
:
494 m_radio
->SetSelection( 2 );
497 case ID_RADIOBOX_SEL_STR
:
499 m_radio
->SetStringSelection( "This" );
502 case ID_RADIOBOX_FONT
:
504 m_radio
->SetFont( *wxITALIC_FONT
);
510 void MyPanel::OnSetFont( wxCommandEvent
&WXUNUSED(event
) )
512 m_fontButton
->SetFont( *wxITALIC_FONT
);
513 m_text
->SetFont( *wxITALIC_FONT
);
518 delete m_notebook
->GetImageList();
521 //----------------------------------------------------------------------
523 //----------------------------------------------------------------------
525 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
526 EVT_MENU(MINIMAL_QUIT
, MyFrame::OnQuit
)
527 EVT_MENU(MINIMAL_ABOUT
, MyFrame::OnAbout
)
530 MyFrame::MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
):
531 wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
533 (void)new MyPanel( this, 10, 10, 300, 100 );
536 void MyFrame::OnQuit (wxCommandEvent
& WXUNUSED(event
) )
541 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
543 wxMessageDialog
dialog(this, "This is a control sample", "About Controls", wxOK
);