]>
git.saurik.com Git - wxWidgets.git/blob - samples/controls/controls.cpp
ac2be534d512a9a02ea570fe40fa1c90574e810c
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/stattext.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
);
72 wxButton
*m_fontButton
;
75 wxNotebook
*m_notebook
;
80 class MyFrame
: public wxFrame
84 MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
);
88 void OnQuit(wxCommandEvent
& event
);
89 void OnAbout(wxCommandEvent
& event
);
90 bool OnClose(void) { return TRUE
; }
95 //----------------------------------------------------------------------
97 //----------------------------------------------------------------------
101 //----------------------------------------------------------------------
103 //----------------------------------------------------------------------
105 const int MINIMAL_QUIT
= 100;
106 const int MINIMAL_TEXT
= 101;
107 const int MINIMAL_ABOUT
= 102;
109 bool MyApp::OnInit(void)
111 // Create the main frame window
112 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, (char *) "Controls wxWindows App", 50, 50, 530, 420 );
116 frame
->SetIcon(wxIcon("mondrian"));
118 frame
->SetIcon(wxIcon( mondrian_xpm
));
121 wxMenu
*file_menu
= new wxMenu
;
123 file_menu
->Append(MINIMAL_ABOUT
, "&About");
124 file_menu
->Append(MINIMAL_QUIT
, "E&xit");
125 wxMenuBar
*menu_bar
= new wxMenuBar
;
126 menu_bar
->Append(file_menu
, "&File");
127 frame
->SetMenuBar(menu_bar
);
136 //----------------------------------------------------------------------
138 //----------------------------------------------------------------------
140 const ID_NOTEBOOK
= 1000;
142 const ID_LISTBOX
= 130;
143 const ID_LISTBOX_SEL_NUM
= 131;
144 const ID_LISTBOX_SEL_STR
= 132;
145 const ID_LISTBOX_CLEAR
= 133;
146 const ID_LISTBOX_APPEND
= 134;
147 const ID_LISTBOX_DELETE
= 135;
148 const ID_LISTBOX_FONT
= 136;
149 const ID_LISTBOX_ENABLE
= 137;
151 const ID_CHOICE
= 120;
152 const ID_CHOICE_SEL_NUM
= 121;
153 const ID_CHOICE_SEL_STR
= 122;
154 const ID_CHOICE_CLEAR
= 123;
155 const ID_CHOICE_APPEND
= 124;
156 const ID_CHOICE_DELETE
= 125;
157 const ID_CHOICE_FONT
= 126;
158 const ID_CHOICE_ENABLE
= 127;
160 const ID_COMBO
= 140;
161 const ID_COMBO_SEL_NUM
= 141;
162 const ID_COMBO_SEL_STR
= 142;
163 const ID_COMBO_CLEAR
= 143;
164 const ID_COMBO_APPEND
= 144;
165 const ID_COMBO_DELETE
= 145;
166 const ID_COMBO_FONT
= 146;
167 const ID_COMBO_ENABLE
= 147;
171 const ID_RADIOBOX
= 160;
172 const ID_RADIOBOX_SEL_NUM
= 161;
173 const ID_RADIOBOX_SEL_STR
= 162;
174 const ID_RADIOBOX_FONT
= 163;
175 const ID_RADIOBOX_ENABLE
= 164;
177 const ID_SET_FONT
= 170;
179 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
180 EVT_SIZE ( MyPanel::OnSize
)
181 EVT_NOTEBOOK_PAGE_CHANGED(ID_NOTEBOOK
, MyPanel::OnPageChanged
)
182 EVT_LISTBOX (ID_LISTBOX
, MyPanel::OnListBox
)
183 EVT_BUTTON (ID_LISTBOX_SEL_NUM
, MyPanel::OnListBoxButtons
)
184 EVT_BUTTON (ID_LISTBOX_SEL_STR
, MyPanel::OnListBoxButtons
)
185 EVT_BUTTON (ID_LISTBOX_CLEAR
, MyPanel::OnListBoxButtons
)
186 EVT_BUTTON (ID_LISTBOX_APPEND
, MyPanel::OnListBoxButtons
)
187 EVT_BUTTON (ID_LISTBOX_DELETE
, MyPanel::OnListBoxButtons
)
188 EVT_BUTTON (ID_LISTBOX_FONT
, MyPanel::OnListBoxButtons
)
189 EVT_CHECKBOX (ID_LISTBOX_ENABLE
, MyPanel::OnListBoxButtons
)
190 EVT_CHOICE (ID_CHOICE
, MyPanel::OnChoice
)
191 EVT_BUTTON (ID_CHOICE_SEL_NUM
, MyPanel::OnChoiceButtons
)
192 EVT_BUTTON (ID_CHOICE_SEL_STR
, MyPanel::OnChoiceButtons
)
193 EVT_BUTTON (ID_CHOICE_CLEAR
, MyPanel::OnChoiceButtons
)
194 EVT_BUTTON (ID_CHOICE_APPEND
, MyPanel::OnChoiceButtons
)
195 EVT_BUTTON (ID_CHOICE_DELETE
, MyPanel::OnChoiceButtons
)
196 EVT_BUTTON (ID_CHOICE_FONT
, MyPanel::OnChoiceButtons
)
197 EVT_CHECKBOX (ID_CHOICE_ENABLE
, MyPanel::OnChoiceButtons
)
198 EVT_CHOICE (ID_COMBO
, MyPanel::OnCombo
)
199 EVT_BUTTON (ID_COMBO_SEL_NUM
, MyPanel::OnComboButtons
)
200 EVT_BUTTON (ID_COMBO_SEL_STR
, MyPanel::OnComboButtons
)
201 EVT_BUTTON (ID_COMBO_CLEAR
, MyPanel::OnComboButtons
)
202 EVT_BUTTON (ID_COMBO_APPEND
, MyPanel::OnComboButtons
)
203 EVT_BUTTON (ID_COMBO_DELETE
, MyPanel::OnComboButtons
)
204 EVT_BUTTON (ID_COMBO_FONT
, MyPanel::OnComboButtons
)
205 EVT_CHECKBOX (ID_COMBO_ENABLE
, MyPanel::OnComboButtons
)
206 EVT_RADIOBOX (ID_RADIOBOX
, MyPanel::OnRadio
)
207 EVT_BUTTON (ID_RADIOBOX_SEL_NUM
, MyPanel::OnRadioButtons
)
208 EVT_BUTTON (ID_RADIOBOX_SEL_STR
, MyPanel::OnRadioButtons
)
209 EVT_BUTTON (ID_RADIOBOX_FONT
, MyPanel::OnRadioButtons
)
210 EVT_CHECKBOX (ID_RADIOBOX_ENABLE
, MyPanel::OnRadioButtons
)
211 EVT_BUTTON (ID_SET_FONT
, MyPanel::OnSetFont
)
214 MyPanel::MyPanel( wxFrame
*frame
, int x
, int y
, int w
, int h
) :
215 wxPanel( frame
, -1, wxPoint(x
, y
), wxSize(w
, h
) )
217 SetBackgroundColour("cadet blue");
219 m_text
= new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(0,50), wxSize(100,50), wxTE_MULTILINE
);
220 m_text
->SetBackgroundColour("wheat");
222 m_notebook
= new wxNotebook( this, ID_NOTEBOOK
, wxPoint(0,0), wxSize(200,150) );
233 // image ids and names
236 Image_List
, Image_Choice
, Image_Combo
, Image_Text
, Image_Radio
, Image_Static
, Image_Max
239 // fill the image list
241 const char *aIconNames
[] =
243 "list.xpm", "choice.xpm", "combo.xpm", "text.xpm", "radio.xpm", "stattext.xpm"
246 wxASSERT( WXSIZEOF(aIconNames
) == Image_Max
); // keep in sync
248 wxString strIconDir
= "icons/";
250 wxImageList
*imagelist
= new wxImageList(32, 32);
251 for ( size_t n
= 0; n
< Image_Max
; n
++ )
253 imagelist
->Add(wxBitmap(strIconDir
+ aIconNames
[n
]));
256 wxImageList
*imagelist
= new wxImageList(32, 32);
258 imagelist
-> Add( wxBitmap( list_xpm
));
259 imagelist
-> Add( wxBitmap( choice_xpm
));
260 imagelist
-> Add( wxBitmap( combo_xpm
));
261 imagelist
-> Add( wxBitmap( text_xpm
));
262 imagelist
-> Add( wxBitmap( radio_xpm
));
263 imagelist
-> Add( wxBitmap( stattext_xpm
));
266 wxButton
*button
= (wxButton
*)NULL
;
268 m_notebook
->SetImageList(imagelist
);
269 m_notebook
->SetBackgroundColour("cadet blue");
271 wxPanel
*panel
= new wxPanel(m_notebook
);
272 panel
->SetBackgroundColour("cadet blue");
273 m_listbox
= new wxListBox( panel
, ID_LISTBOX
, wxPoint(10,10), wxSize(120,70), 5, choices
);
274 m_listbox
->SetBackgroundColour("wheat");
275 (void)new wxButton( panel
, ID_LISTBOX_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(140,30) );
276 (void)new wxButton( panel
, ID_LISTBOX_SEL_STR
, "Select 'This'", wxPoint(340,30), wxSize(140,30) );
277 (void)new wxButton( panel
, ID_LISTBOX_CLEAR
, "Clear", wxPoint(180,80), wxSize(140,30) );
278 (void)new wxButton( panel
, ID_LISTBOX_APPEND
, "Append 'Hi!'", wxPoint(340,80), wxSize(140,30) );
279 (void)new wxButton( panel
, ID_LISTBOX_DELETE
, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
280 button
= new wxButton( panel
, ID_LISTBOX_FONT
, "Set Italic font", wxPoint(340,130), wxSize(140,30) );
281 button
->SetForegroundColour( "red" );
282 (void)new wxCheckBox( panel
, ID_LISTBOX_ENABLE
, "Disable", wxPoint(20,130), wxSize(140,30) );
283 m_notebook
->AddPage(panel
, "wxList", FALSE
, Image_List
);
285 panel
= new wxPanel(m_notebook
);
286 panel
->SetBackgroundColour("cadet blue");
287 m_choice
= new wxChoice( panel
, ID_CHOICE
, wxPoint(10,10), wxSize(120,-1), 5, choices
);
288 m_choice
->SetBackgroundColour("wheat");
289 (void)new wxButton( panel
, ID_CHOICE_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(140,30) );
290 (void)new wxButton( panel
, ID_CHOICE_SEL_STR
, "Select 'This'", wxPoint(340,30), wxSize(140,30) );
291 (void)new wxButton( panel
, ID_CHOICE_CLEAR
, "Clear", wxPoint(180,80), wxSize(140,30) );
292 (void)new wxButton( panel
, ID_CHOICE_APPEND
, "Append 'Hi!'", wxPoint(340,80), wxSize(140,30) );
293 (void)new wxButton( panel
, ID_CHOICE_DELETE
, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
294 (void)new wxButton( panel
, ID_CHOICE_FONT
, "Set Italic font", wxPoint(340,130), wxSize(140,30) );
295 (void)new wxCheckBox( panel
, ID_CHOICE_ENABLE
, "Disable", wxPoint(20,130), wxSize(140,30) );
296 m_notebook
->AddPage(panel
, "wxChoice", FALSE
, Image_Choice
);
298 panel
= new wxPanel(m_notebook
);
299 panel
->SetBackgroundColour("cadet blue");
300 m_combo
= new wxComboBox( panel
, ID_COMBO
, "This", wxPoint(10,10), wxSize(120,-1), 5, choices
);
301 m_combo
->SetBackgroundColour("wheat");
302 (void)new wxButton( panel
, ID_COMBO_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(140,30) );
303 (void)new wxButton( panel
, ID_COMBO_SEL_STR
, "Select 'This'", wxPoint(340,30), wxSize(140,30) );
304 (void)new wxButton( panel
, ID_COMBO_CLEAR
, "Clear", wxPoint(180,80), wxSize(140,30) );
305 (void)new wxButton( panel
, ID_COMBO_APPEND
, "Append 'Hi!'", wxPoint(340,80), wxSize(140,30) );
306 (void)new wxButton( panel
, ID_COMBO_DELETE
, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
307 (void)new wxButton( panel
, ID_COMBO_FONT
, "Set Italic font", wxPoint(340,130), wxSize(140,30) );
308 (void)new wxCheckBox( panel
, ID_COMBO_ENABLE
, "Disable", wxPoint(20,130), wxSize(140,30) );
309 m_notebook
->AddPage(panel
, "wxComboBox", FALSE
, Image_Combo
);
311 panel
= new wxPanel(m_notebook
);
312 panel
->SetBackgroundColour("cadet blue");
313 wxTextCtrl
*tc
= new wxTextCtrl( panel
, ID_TEXT
, "Write text here.", wxPoint(10,10), wxSize(350,28));
314 tc
->SetBackgroundColour("wheat");
315 tc
= new wxTextCtrl( panel
, ID_TEXT
, "And here.", wxPoint(10,50), wxSize(350,160), wxTE_MULTILINE
);
316 tc
->SetBackgroundColour("wheat");
317 m_notebook
->AddPage(panel
, "wxTextCtrl" , FALSE
, Image_Text
);
319 panel
= new wxPanel(m_notebook
);
320 panel
->SetBackgroundColour("cadet blue");
321 m_radio
= new wxRadioBox( panel
, ID_RADIOBOX
, "This", wxPoint(10,10), wxSize(-1,-1), 5, choices
);
322 m_radio
->SetBackgroundColour("wheat");
323 (void)new wxButton( panel
, ID_RADIOBOX_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(140,30) );
324 (void)new wxButton( panel
, ID_RADIOBOX_SEL_STR
, "Select 'This'", wxPoint(180,80), wxSize(140,30) );
325 (void)new wxButton( panel
, ID_RADIOBOX_FONT
, "Set Italic font", wxPoint(180,130), wxSize(140,30) );
326 (void)new wxCheckBox( panel
, ID_RADIOBOX_ENABLE
, "Disable", wxPoint(20,130), wxSize(140,30) );
327 m_fontButton
= new wxButton( panel
, ID_SET_FONT
, "Set more Italic font", wxPoint(340,30), wxSize(160,30) );
328 m_notebook
->AddPage(panel
, "wxRadioBox", FALSE
, Image_Radio
);
330 panel
= new wxPanel(m_notebook
);
331 panel
->SetBackgroundColour("cadet blue");
332 (void)new wxStaticBox( panel
, -1, "StaticBox", wxPoint(10,10), wxSize(160,130) );
333 m_notebook
->AddPage(panel
, "wxStaticBox", FALSE
, Image_Static
);
336 void MyPanel::OnSize( wxSizeEvent
& WXUNUSED(event
) )
340 GetClientSize( &x
, &y
);
342 if (m_notebook
) m_notebook
->SetSize( 2, 2, x
-4, y
*2/3-4 );
343 if (m_text
) m_text
->SetSize( 2, y
*2/3+2, x
-4, y
/3-4 );
346 void MyPanel::OnPageChanged( wxNotebookEvent
&event
)
348 *m_text
<< "Notebook selection is " << event
.GetSelection() << "\n";
351 void MyPanel::OnListBox( wxCommandEvent
&event
)
353 m_text
->WriteText( "ListBox selection string is: " );
354 m_text
->WriteText( event
.GetString() );
355 m_text
->WriteText( "\n" );
358 void MyPanel::OnListBoxButtons( wxCommandEvent
&event
)
360 switch (event
.GetId())
362 case ID_LISTBOX_ENABLE
:
364 m_listbox
->Enable( !((bool)event
.GetInt()) );
367 case ID_LISTBOX_SEL_NUM
:
369 m_listbox
->SetSelection( 2 );
372 case ID_LISTBOX_SEL_STR
:
374 m_listbox
->SetStringSelection( "This" );
377 case ID_LISTBOX_CLEAR
:
382 case ID_LISTBOX_APPEND
:
384 m_listbox
->Append( "Hi!" );
387 case ID_LISTBOX_DELETE
:
389 int idx
= m_listbox
->GetSelection();
390 m_listbox
->Delete( idx
);
393 case ID_LISTBOX_FONT
:
395 m_listbox
->SetFont( *wxITALIC_FONT
);
401 void MyPanel::OnChoice( wxCommandEvent
&event
)
403 m_text
->WriteText( "Choice selection string is: " );
404 m_text
->WriteText( event
.GetString() );
405 m_text
->WriteText( "\n" );
408 void MyPanel::OnChoiceButtons( wxCommandEvent
&event
)
410 switch (event
.GetId())
412 case ID_CHOICE_ENABLE
:
414 m_choice
->Enable( !((bool)event
.GetInt()) );
417 case ID_CHOICE_SEL_NUM
:
419 m_choice
->SetSelection( 2 );
422 case ID_CHOICE_SEL_STR
:
424 m_choice
->SetStringSelection( "This" );
427 case ID_CHOICE_CLEAR
:
432 case ID_CHOICE_APPEND
:
434 m_choice
->Append( "Hi!" );
437 case ID_CHOICE_DELETE
:
439 int idx
= m_choice
->GetSelection();
440 m_choice
->Delete( idx
);
445 m_choice
->SetFont( *wxITALIC_FONT
);
451 void MyPanel::OnCombo( wxCommandEvent
&event
)
453 m_text
->WriteText( "ComboBox selection string is: " );
454 m_text
->WriteText( event
.GetString() );
455 m_text
->WriteText( "\n" );
458 void MyPanel::OnComboButtons( wxCommandEvent
&event
)
460 switch (event
.GetId())
462 case ID_COMBO_ENABLE
:
464 m_combo
->Enable( !((bool)event
.GetInt()) );
467 case ID_COMBO_SEL_NUM
:
469 m_combo
->SetSelection( 2 );
472 case ID_COMBO_SEL_STR
:
474 m_combo
->SetStringSelection( "This" );
482 case ID_COMBO_APPEND
:
484 m_combo
->Append( "Hi!" );
487 case ID_COMBO_DELETE
:
489 int idx
= m_combo
->GetSelection();
490 m_combo
->Delete( idx
);
495 m_combo
->SetFont( *wxITALIC_FONT
);
501 void MyPanel::OnRadio( wxCommandEvent
&event
)
503 m_text
->WriteText( "RadioBox selection string is: " );
504 m_text
->WriteText( event
.GetString() );
505 m_text
->WriteText( "\n" );
508 void MyPanel::OnRadioButtons( wxCommandEvent
&event
)
510 switch (event
.GetId())
512 case ID_RADIOBOX_ENABLE
:
514 m_radio
->Enable( !((bool)event
.GetInt()) );
517 case ID_RADIOBOX_SEL_NUM
:
519 m_radio
->SetSelection( 2 );
522 case ID_RADIOBOX_SEL_STR
:
524 m_radio
->SetStringSelection( "This" );
527 case ID_RADIOBOX_FONT
:
529 m_radio
->SetFont( *wxITALIC_FONT
);
535 void MyPanel::OnSetFont( wxCommandEvent
&WXUNUSED(event
) )
537 m_fontButton
->SetFont( *wxITALIC_FONT
);
538 m_text
->SetFont( *wxITALIC_FONT
);
543 delete m_notebook
->GetImageList();
546 //----------------------------------------------------------------------
548 //----------------------------------------------------------------------
550 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
551 EVT_MENU(MINIMAL_QUIT
, MyFrame::OnQuit
)
552 EVT_MENU(MINIMAL_ABOUT
, MyFrame::OnAbout
)
555 MyFrame::MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
):
556 wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
558 (void)new MyPanel( this, 10, 10, 300, 100 );
561 void MyFrame::OnQuit (wxCommandEvent
& WXUNUSED(event
) )
566 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
568 wxMessageDialog
dialog(this, "This is a control sample", "About Controls", wxOK
);