]>
git.saurik.com Git - wxWidgets.git/blob - samples/controls/controls.cpp
e8f89f65af7e67eeac8437072960f81b5a8d2c2e
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 interface "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"
33 //----------------------------------------------------------------------
35 //----------------------------------------------------------------------
37 class MyApp
: public wxApp
43 class MyPanel
: public wxPanel
47 MyPanel(wxFrame
*frame
, int x
, int y
, int w
, int h
);
50 void OnSize( wxSizeEvent
& event
);
51 void OnListBox( wxCommandEvent
&event
);
52 void OnListBoxButtons( wxCommandEvent
&event
);
53 void OnChoice( wxCommandEvent
&event
);
54 void OnChoiceButtons( wxCommandEvent
&event
);
55 void OnCombo( wxCommandEvent
&event
);
56 void OnComboButtons( wxCommandEvent
&event
);
57 void OnRadio( wxCommandEvent
&event
);
58 void OnRadioButtons( wxCommandEvent
&event
);
59 void OnSetFont( wxCommandEvent
&event
);
60 void OnPageChanged( wxNotebookEvent
&event
);
66 wxButton
*m_fontButton
;
69 wxNotebook
*m_notebook
;
74 class MyFrame
: public wxFrame
78 MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
);
82 void OnQuit(wxCommandEvent
& event
);
83 void OnAbout(wxCommandEvent
& event
);
84 bool OnClose(void) { return TRUE
; }
89 //----------------------------------------------------------------------
91 //----------------------------------------------------------------------
95 //----------------------------------------------------------------------
97 //----------------------------------------------------------------------
99 const int MINIMAL_QUIT
= 100;
100 const int MINIMAL_TEXT
= 101;
101 const int MINIMAL_ABOUT
= 102;
103 bool MyApp::OnInit(void)
105 // Create the main frame window
106 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, (char *) "Controls wxWindows App", 50, 50, 530, 420 );
110 frame
->SetIcon(wxIcon("mondrian"));
112 frame
->SetIcon(wxIcon( mondrian_xpm
));
115 wxMenu
*file_menu
= new wxMenu
;
117 file_menu
->Append(MINIMAL_ABOUT
, "&About");
118 file_menu
->Append(MINIMAL_QUIT
, "E&xit");
119 wxMenuBar
*menu_bar
= new wxMenuBar
;
120 menu_bar
->Append(file_menu
, "&File");
121 frame
->SetMenuBar(menu_bar
);
130 //----------------------------------------------------------------------
132 //----------------------------------------------------------------------
134 const ID_NOTEBOOK
= 1000;
136 const ID_LISTBOX
= 130;
137 const ID_LISTBOX_SEL_NUM
= 131;
138 const ID_LISTBOX_SEL_STR
= 132;
139 const ID_LISTBOX_CLEAR
= 133;
140 const ID_LISTBOX_APPEND
= 134;
141 const ID_LISTBOX_DELETE
= 135;
142 const ID_LISTBOX_FONT
= 136;
143 const ID_LISTBOX_ENABLE
= 137;
145 const ID_CHOICE
= 120;
146 const ID_CHOICE_SEL_NUM
= 121;
147 const ID_CHOICE_SEL_STR
= 122;
148 const ID_CHOICE_CLEAR
= 123;
149 const ID_CHOICE_APPEND
= 124;
150 const ID_CHOICE_DELETE
= 125;
151 const ID_CHOICE_FONT
= 126;
152 const ID_CHOICE_ENABLE
= 127;
154 const ID_COMBO
= 140;
155 const ID_COMBO_SEL_NUM
= 141;
156 const ID_COMBO_SEL_STR
= 142;
157 const ID_COMBO_CLEAR
= 143;
158 const ID_COMBO_APPEND
= 144;
159 const ID_COMBO_DELETE
= 145;
160 const ID_COMBO_FONT
= 146;
161 const ID_COMBO_ENABLE
= 147;
165 const ID_RADIOBOX
= 160;
166 const ID_RADIOBOX_SEL_NUM
= 161;
167 const ID_RADIOBOX_SEL_STR
= 162;
168 const ID_RADIOBOX_FONT
= 163;
169 const ID_RADIOBOX_ENABLE
= 164;
171 const ID_SET_FONT
= 170;
173 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
174 EVT_SIZE ( MyPanel::OnSize
)
175 EVT_NOTEBOOK_PAGE_CHANGED(ID_NOTEBOOK
, MyPanel::OnPageChanged
)
176 EVT_LISTBOX (ID_LISTBOX
, MyPanel::OnListBox
)
177 EVT_BUTTON (ID_LISTBOX_SEL_NUM
, MyPanel::OnListBoxButtons
)
178 EVT_BUTTON (ID_LISTBOX_SEL_STR
, MyPanel::OnListBoxButtons
)
179 EVT_BUTTON (ID_LISTBOX_CLEAR
, MyPanel::OnListBoxButtons
)
180 EVT_BUTTON (ID_LISTBOX_APPEND
, MyPanel::OnListBoxButtons
)
181 EVT_BUTTON (ID_LISTBOX_DELETE
, MyPanel::OnListBoxButtons
)
182 EVT_BUTTON (ID_LISTBOX_FONT
, MyPanel::OnListBoxButtons
)
183 EVT_CHECKBOX (ID_LISTBOX_ENABLE
, MyPanel::OnListBoxButtons
)
184 EVT_CHOICE (ID_CHOICE
, MyPanel::OnChoice
)
185 EVT_BUTTON (ID_CHOICE_SEL_NUM
, MyPanel::OnChoiceButtons
)
186 EVT_BUTTON (ID_CHOICE_SEL_STR
, MyPanel::OnChoiceButtons
)
187 EVT_BUTTON (ID_CHOICE_CLEAR
, MyPanel::OnChoiceButtons
)
188 EVT_BUTTON (ID_CHOICE_APPEND
, MyPanel::OnChoiceButtons
)
189 EVT_BUTTON (ID_CHOICE_DELETE
, MyPanel::OnChoiceButtons
)
190 EVT_BUTTON (ID_CHOICE_FONT
, MyPanel::OnChoiceButtons
)
191 EVT_CHECKBOX (ID_CHOICE_ENABLE
, MyPanel::OnChoiceButtons
)
192 EVT_CHOICE (ID_COMBO
, MyPanel::OnCombo
)
193 EVT_BUTTON (ID_COMBO_SEL_NUM
, MyPanel::OnComboButtons
)
194 EVT_BUTTON (ID_COMBO_SEL_STR
, MyPanel::OnComboButtons
)
195 EVT_BUTTON (ID_COMBO_CLEAR
, MyPanel::OnComboButtons
)
196 EVT_BUTTON (ID_COMBO_APPEND
, MyPanel::OnComboButtons
)
197 EVT_BUTTON (ID_COMBO_DELETE
, MyPanel::OnComboButtons
)
198 EVT_BUTTON (ID_COMBO_FONT
, MyPanel::OnComboButtons
)
199 EVT_CHECKBOX (ID_COMBO_ENABLE
, MyPanel::OnComboButtons
)
200 EVT_RADIOBOX (ID_RADIOBOX
, MyPanel::OnRadio
)
201 EVT_BUTTON (ID_RADIOBOX_SEL_NUM
, MyPanel::OnRadioButtons
)
202 EVT_BUTTON (ID_RADIOBOX_SEL_STR
, MyPanel::OnRadioButtons
)
203 EVT_BUTTON (ID_RADIOBOX_FONT
, MyPanel::OnRadioButtons
)
204 EVT_CHECKBOX (ID_RADIOBOX_ENABLE
, MyPanel::OnRadioButtons
)
205 EVT_BUTTON (ID_SET_FONT
, MyPanel::OnSetFont
)
208 MyPanel::MyPanel( wxFrame
*frame
, int x
, int y
, int w
, int h
) :
209 wxPanel( frame
, -1, wxPoint(x
, y
), wxSize(w
, h
) )
211 m_text
= new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(0,50), wxSize(100,50), wxTE_MULTILINE
);
212 m_text
->SetBackgroundColour("yellow");
214 m_notebook
= new wxNotebook( this, ID_NOTEBOOK
, wxPoint(0,0), wxSize(200,150) );
224 // image ids and names
227 Image_List
, Image_Choice
, Image_Combo
, Image_Text
, Image_Radio
, Image_Max
230 const char *aIconNames
[] =
232 "list.xpm", "choice.xpm", "combo.xpm", "text.xpm", "radio.xpm"
235 wxASSERT( WXSIZEOF(aIconNames
) == Image_Max
); // keep in sync
237 // TODO should find the dir from path to program
238 wxString strIconDir
= "icons/";
240 // fill the image list
241 wxImageList
*imagelist
= new wxImageList(32, 32);
242 for ( size_t n
= 0; n
< Image_Max
; n
++ )
244 imagelist
->Add(wxBitmap(strIconDir
+ aIconNames
[n
]));
247 m_notebook
->SetImageList(imagelist
);
249 wxPanel
*panel
= new wxPanel(m_notebook
);
250 m_listbox
= new wxListBox( panel
, ID_LISTBOX
, wxPoint(10,10), wxSize(120,70), 4, choices
);
251 m_listbox
->SetBackgroundColour("red");
252 (void)new wxButton( panel
, ID_LISTBOX_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(140,30) );
253 (void)new wxButton( panel
, ID_LISTBOX_SEL_STR
, "Select 'This'", wxPoint(340,30), wxSize(140,30) );
254 wxButton
*btn
= new wxButton( panel
, ID_LISTBOX_CLEAR
, "Clear", wxPoint(180,80), wxSize(140,30) );
255 btn
->SetBackgroundColour("green");
256 (void)new wxButton( panel
, ID_LISTBOX_APPEND
, "Append 'Hi!'", wxPoint(340,80), wxSize(140,30) );
257 (void)new wxButton( panel
, ID_LISTBOX_DELETE
, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
258 (void)new wxButton( panel
, ID_LISTBOX_FONT
, "Set Italic font", wxPoint(340,130), wxSize(140,30) );
259 (void)new wxCheckBox( panel
, ID_LISTBOX_ENABLE
, "Disable", wxPoint(20,130), wxSize(140,30) );
260 m_notebook
->AddPage(panel
, "wxList", FALSE
, Image_List
);
262 panel
= new wxPanel(m_notebook
);
263 m_choice
= new wxChoice( panel
, ID_CHOICE
, wxPoint(10,10), wxSize(120,-1), 4, choices
);
264 (void)new wxButton( panel
, ID_CHOICE_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(140,30) );
265 (void)new wxButton( panel
, ID_CHOICE_SEL_STR
, "Select 'This'", wxPoint(340,30), wxSize(140,30) );
266 (void)new wxButton( panel
, ID_CHOICE_CLEAR
, "Clear", wxPoint(180,80), wxSize(140,30) );
267 (void)new wxButton( panel
, ID_CHOICE_APPEND
, "Append 'Hi!'", wxPoint(340,80), wxSize(140,30) );
268 (void)new wxButton( panel
, ID_CHOICE_DELETE
, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
269 (void)new wxButton( panel
, ID_CHOICE_FONT
, "Set Italic font", wxPoint(340,130), wxSize(140,30) );
270 (void)new wxCheckBox( panel
, ID_CHOICE_ENABLE
, "Disable", wxPoint(20,130), wxSize(140,30) );
271 m_notebook
->AddPage(panel
, "wxChoice", FALSE
, Image_Choice
);
273 panel
= new wxPanel(m_notebook
);
274 m_combo
= new wxComboBox( panel
, ID_COMBO
, "This", wxPoint(10,10), wxSize(120,-1), 4, choices
);
275 (void)new wxButton( panel
, ID_COMBO_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(140,30) );
276 (void)new wxButton( panel
, ID_COMBO_SEL_STR
, "Select 'This'", wxPoint(340,30), wxSize(140,30) );
277 (void)new wxButton( panel
, ID_COMBO_CLEAR
, "Clear", wxPoint(180,80), wxSize(140,30) );
278 (void)new wxButton( panel
, ID_COMBO_APPEND
, "Append 'Hi!'", wxPoint(340,80), wxSize(140,30) );
279 (void)new wxButton( panel
, ID_COMBO_DELETE
, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
280 (void)new wxButton( panel
, ID_COMBO_FONT
, "Set Italic font", wxPoint(340,130), wxSize(140,30) );
281 (void)new wxCheckBox( panel
, ID_COMBO_ENABLE
, "Disable", wxPoint(20,130), wxSize(140,30) );
282 m_notebook
->AddPage(panel
, "wxComboBox", FALSE
, Image_Combo
);
284 wxTextCtrl
*text
= new wxTextCtrl( m_notebook
, ID_TEXT
, "Write text here.", wxPoint(10,10), wxSize(120,100), wxTE_MULTILINE
);
285 m_notebook
->AddPage(text
, "wxTextCtrl" , FALSE
, Image_Text
);
287 panel
= new wxPanel(m_notebook
);
288 m_radio
= new wxRadioBox( panel
, ID_RADIOBOX
, "This", wxPoint(10,10), wxSize(-1,-1), 4, choices
);
289 (void)new wxButton( panel
, ID_RADIOBOX_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(140,30) );
290 (void)new wxButton( panel
, ID_RADIOBOX_SEL_STR
, "Select 'This'", wxPoint(180,80), wxSize(140,30) );
291 (void)new wxButton( panel
, ID_RADIOBOX_FONT
, "Set Italic font", wxPoint(180,130), wxSize(140,30) );
292 (void)new wxCheckBox( panel
, ID_RADIOBOX_ENABLE
, "Disable", wxPoint(20,130), wxSize(140,30) );
293 m_fontButton
= new wxButton( panel
, ID_SET_FONT
, "Set more Italic font", wxPoint(340,30), wxSize(160,30) );
294 m_notebook
->AddPage(panel
, "wxRadioBox", FALSE
, Image_Radio
);
297 void MyPanel::OnSize( wxSizeEvent
& WXUNUSED(event
) )
301 GetClientSize( &x
, &y
);
303 if (m_notebook
) m_notebook
->SetSize( 2, 2, x
-4, y
*2/3-4 );
304 if (m_text
) m_text
->SetSize( 2, y
*2/3+2, x
-4, y
/3-4 );
307 void MyPanel::OnPageChanged( wxNotebookEvent
&event
)
309 *m_text
<< "Notebook selection is " << event
.GetSelection() << "\n";
312 void MyPanel::OnListBox( wxCommandEvent
&event
)
314 m_text
->WriteText( "ListBox selection string is: " );
315 m_text
->WriteText( event
.GetString() );
316 m_text
->WriteText( "\n" );
319 void MyPanel::OnListBoxButtons( wxCommandEvent
&event
)
321 switch (event
.GetId())
323 case ID_LISTBOX_ENABLE
:
325 m_listbox
->Enable( !((bool)event
.GetInt()) );
328 case ID_LISTBOX_SEL_NUM
:
330 m_listbox
->SetSelection( 2 );
333 case ID_LISTBOX_SEL_STR
:
335 m_listbox
->SetStringSelection( "This" );
338 case ID_LISTBOX_CLEAR
:
343 case ID_LISTBOX_APPEND
:
345 m_listbox
->Append( "Hi!" );
348 case ID_LISTBOX_DELETE
:
350 int idx
= m_listbox
->GetSelection();
351 m_listbox
->Delete( idx
);
354 case ID_LISTBOX_FONT
:
356 m_listbox
->SetFont( *wxITALIC_FONT
);
362 void MyPanel::OnChoice( wxCommandEvent
&event
)
364 m_text
->WriteText( "Choice selection string is: " );
365 m_text
->WriteText( event
.GetString() );
366 m_text
->WriteText( "\n" );
369 void MyPanel::OnChoiceButtons( wxCommandEvent
&event
)
371 switch (event
.GetId())
373 case ID_CHOICE_ENABLE
:
375 m_choice
->Enable( !((bool)event
.GetInt()) );
378 case ID_CHOICE_SEL_NUM
:
380 m_choice
->SetSelection( 2 );
383 case ID_CHOICE_SEL_STR
:
385 m_choice
->SetStringSelection( "This" );
388 case ID_CHOICE_CLEAR
:
393 case ID_CHOICE_APPEND
:
395 m_choice
->Append( "Hi!" );
398 case ID_CHOICE_DELETE
:
400 int idx
= m_choice
->GetSelection();
401 m_choice
->Delete( idx
);
406 m_choice
->SetFont( *wxITALIC_FONT
);
412 void MyPanel::OnCombo( wxCommandEvent
&event
)
414 m_text
->WriteText( "ComboBox selection string is: " );
415 m_text
->WriteText( event
.GetString() );
416 m_text
->WriteText( "\n" );
419 void MyPanel::OnComboButtons( wxCommandEvent
&event
)
421 switch (event
.GetId())
423 case ID_COMBO_ENABLE
:
425 m_combo
->Enable( !((bool)event
.GetInt()) );
428 case ID_COMBO_SEL_NUM
:
430 m_combo
->SetSelection( 2 );
433 case ID_COMBO_SEL_STR
:
435 m_combo
->SetStringSelection( "This" );
443 case ID_COMBO_APPEND
:
445 m_combo
->Append( "Hi!" );
448 case ID_COMBO_DELETE
:
450 int idx
= m_combo
->GetSelection();
451 m_combo
->Delete( idx
);
456 m_combo
->SetFont( *wxITALIC_FONT
);
462 void MyPanel::OnRadio( wxCommandEvent
&event
)
464 m_text
->WriteText( "RadioBox selection string is: " );
465 m_text
->WriteText( event
.GetString() );
466 m_text
->WriteText( "\n" );
469 void MyPanel::OnRadioButtons( wxCommandEvent
&event
)
471 switch (event
.GetId())
473 case ID_RADIOBOX_ENABLE
:
475 m_radio
->Enable( !((bool)event
.GetInt()) );
478 case ID_RADIOBOX_SEL_NUM
:
480 m_radio
->SetSelection( 2 );
483 case ID_RADIOBOX_SEL_STR
:
485 m_radio
->SetStringSelection( "This" );
488 case ID_RADIOBOX_FONT
:
490 m_radio
->SetFont( *wxITALIC_FONT
);
496 void MyPanel::OnSetFont( wxCommandEvent
&WXUNUSED(event
) )
498 m_fontButton
->SetFont( *wxITALIC_FONT
);
499 m_text
->SetFont( *wxITALIC_FONT
);
504 delete m_notebook
->GetImageList();
507 //----------------------------------------------------------------------
509 //----------------------------------------------------------------------
511 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
512 EVT_MENU(MINIMAL_QUIT
, MyFrame::OnQuit
)
513 EVT_MENU(MINIMAL_ABOUT
, MyFrame::OnAbout
)
516 MyFrame::MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
):
517 wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
519 (void)new MyPanel( this, 10, 10, 300, 100 );
522 void MyFrame::OnQuit (wxCommandEvent
& WXUNUSED(event
) )
527 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
529 wxMessageDialog
dialog(this, "This is a control sample", "About Controls", wxOK
);