]>
git.saurik.com Git - wxWidgets.git/blob - samples/controls/controls.cpp
8ce29591c82c7137f1b9650fb29f0927449a98fb
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
);
74 wxButton
*m_fontButton
;
77 wxNotebook
*m_notebook
;
82 class MyFrame
: public wxFrame
86 MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
);
90 void OnQuit(wxCommandEvent
& event
);
91 void OnAbout(wxCommandEvent
& event
);
92 bool OnClose(void) { return TRUE
; }
97 //----------------------------------------------------------------------
99 //----------------------------------------------------------------------
101 IMPLEMENT_APP (MyApp
)
103 //----------------------------------------------------------------------
105 //----------------------------------------------------------------------
107 const int MINIMAL_QUIT
= 100;
108 const int MINIMAL_TEXT
= 101;
109 const int MINIMAL_ABOUT
= 102;
111 bool MyApp::OnInit(void)
113 // Create the main frame window
114 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, (char *) "Controls wxWindows App", 50, 50, 530, 420 );
118 frame
->SetIcon(wxIcon("mondrian"));
120 frame
->SetIcon(wxIcon( mondrian_xpm
));
123 wxMenu
*file_menu
= new wxMenu
;
125 file_menu
->Append(MINIMAL_ABOUT
, "&About");
126 file_menu
->Append(MINIMAL_QUIT
, "E&xit");
127 wxMenuBar
*menu_bar
= new wxMenuBar
;
128 menu_bar
->Append(file_menu
, "&File");
129 frame
->SetMenuBar(menu_bar
);
138 //----------------------------------------------------------------------
140 //----------------------------------------------------------------------
142 const ID_NOTEBOOK
= 1000;
144 const ID_LISTBOX
= 130;
145 const ID_LISTBOX_SEL_NUM
= 131;
146 const ID_LISTBOX_SEL_STR
= 132;
147 const ID_LISTBOX_CLEAR
= 133;
148 const ID_LISTBOX_APPEND
= 134;
149 const ID_LISTBOX_DELETE
= 135;
150 const ID_LISTBOX_FONT
= 136;
151 const ID_LISTBOX_ENABLE
= 137;
153 const ID_CHOICE
= 120;
154 const ID_CHOICE_SEL_NUM
= 121;
155 const ID_CHOICE_SEL_STR
= 122;
156 const ID_CHOICE_CLEAR
= 123;
157 const ID_CHOICE_APPEND
= 124;
158 const ID_CHOICE_DELETE
= 125;
159 const ID_CHOICE_FONT
= 126;
160 const ID_CHOICE_ENABLE
= 127;
162 const ID_COMBO
= 140;
163 const ID_COMBO_SEL_NUM
= 141;
164 const ID_COMBO_SEL_STR
= 142;
165 const ID_COMBO_CLEAR
= 143;
166 const ID_COMBO_APPEND
= 144;
167 const ID_COMBO_DELETE
= 145;
168 const ID_COMBO_FONT
= 146;
169 const ID_COMBO_ENABLE
= 147;
173 const ID_RADIOBOX
= 160;
174 const ID_RADIOBOX_SEL_NUM
= 161;
175 const ID_RADIOBOX_SEL_STR
= 162;
176 const ID_RADIOBOX_FONT
= 163;
177 const ID_RADIOBOX_ENABLE
= 164;
179 const ID_SET_FONT
= 170;
181 const ID_GAUGE
= 180;
182 const ID_SLIDER
= 181;
185 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
186 EVT_SIZE ( MyPanel::OnSize
)
187 EVT_NOTEBOOK_PAGE_CHANGED(ID_NOTEBOOK
, MyPanel::OnPageChanged
)
188 EVT_LISTBOX (ID_LISTBOX
, MyPanel::OnListBox
)
189 EVT_BUTTON (ID_LISTBOX_SEL_NUM
, MyPanel::OnListBoxButtons
)
190 EVT_BUTTON (ID_LISTBOX_SEL_STR
, MyPanel::OnListBoxButtons
)
191 EVT_BUTTON (ID_LISTBOX_CLEAR
, MyPanel::OnListBoxButtons
)
192 EVT_BUTTON (ID_LISTBOX_APPEND
, MyPanel::OnListBoxButtons
)
193 EVT_BUTTON (ID_LISTBOX_DELETE
, MyPanel::OnListBoxButtons
)
194 EVT_BUTTON (ID_LISTBOX_FONT
, MyPanel::OnListBoxButtons
)
195 EVT_CHECKBOX (ID_LISTBOX_ENABLE
, MyPanel::OnListBoxButtons
)
196 EVT_CHOICE (ID_CHOICE
, MyPanel::OnChoice
)
197 EVT_BUTTON (ID_CHOICE_SEL_NUM
, MyPanel::OnChoiceButtons
)
198 EVT_BUTTON (ID_CHOICE_SEL_STR
, MyPanel::OnChoiceButtons
)
199 EVT_BUTTON (ID_CHOICE_CLEAR
, MyPanel::OnChoiceButtons
)
200 EVT_BUTTON (ID_CHOICE_APPEND
, MyPanel::OnChoiceButtons
)
201 EVT_BUTTON (ID_CHOICE_DELETE
, MyPanel::OnChoiceButtons
)
202 EVT_BUTTON (ID_CHOICE_FONT
, MyPanel::OnChoiceButtons
)
203 EVT_CHECKBOX (ID_CHOICE_ENABLE
, MyPanel::OnChoiceButtons
)
204 EVT_CHOICE (ID_COMBO
, MyPanel::OnCombo
)
205 EVT_BUTTON (ID_COMBO_SEL_NUM
, MyPanel::OnComboButtons
)
206 EVT_BUTTON (ID_COMBO_SEL_STR
, MyPanel::OnComboButtons
)
207 EVT_BUTTON (ID_COMBO_CLEAR
, MyPanel::OnComboButtons
)
208 EVT_BUTTON (ID_COMBO_APPEND
, MyPanel::OnComboButtons
)
209 EVT_BUTTON (ID_COMBO_DELETE
, MyPanel::OnComboButtons
)
210 EVT_BUTTON (ID_COMBO_FONT
, MyPanel::OnComboButtons
)
211 EVT_CHECKBOX (ID_COMBO_ENABLE
, MyPanel::OnComboButtons
)
212 EVT_RADIOBOX (ID_RADIOBOX
, MyPanel::OnRadio
)
213 EVT_BUTTON (ID_RADIOBOX_SEL_NUM
, MyPanel::OnRadioButtons
)
214 EVT_BUTTON (ID_RADIOBOX_SEL_STR
, MyPanel::OnRadioButtons
)
215 EVT_BUTTON (ID_RADIOBOX_FONT
, MyPanel::OnRadioButtons
)
216 EVT_CHECKBOX (ID_RADIOBOX_ENABLE
, MyPanel::OnRadioButtons
)
217 EVT_BUTTON (ID_SET_FONT
, MyPanel::OnSetFont
)
220 MyPanel::MyPanel( wxFrame
*frame
, int x
, int y
, int w
, int h
) :
221 wxPanel( frame
, -1, wxPoint(x
, y
), wxSize(w
, h
) )
223 // SetBackgroundColour("cadet blue");
225 m_text
= new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(0,50), wxSize(100,50), wxTE_MULTILINE
);
226 m_text
->SetBackgroundColour("wheat");
228 m_notebook
= new wxNotebook( this, ID_NOTEBOOK
, wxPoint(0,0), wxSize(200,150) );
239 // image ids and names
242 Image_List
, Image_Choice
, Image_Combo
, Image_Text
, Image_Radio
, Image_Gauge
, Image_Max
245 // fill the image list
247 const char *aIconNames
[] =
249 "list.xpm", "choice.xpm", "combo.xpm", "text.xpm", "radio.xpm", "gauge.txt"
252 wxASSERT( WXSIZEOF(aIconNames
) == Image_Max
); // keep in sync
254 wxString strIconDir
= "icons/";
256 wxImageList
*imagelist
= new wxImageList(32, 32);
257 for ( size_t n
= 0; n
< Image_Max
; n
++ )
259 imagelist
->Add(wxBitmap(strIconDir
+ aIconNames
[n
]));
262 wxImageList
*imagelist
= new wxImageList(32, 32);
264 imagelist
-> Add( wxBitmap( list_xpm
));
265 imagelist
-> Add( wxBitmap( choice_xpm
));
266 imagelist
-> Add( wxBitmap( combo_xpm
));
267 imagelist
-> Add( wxBitmap( text_xpm
));
268 imagelist
-> Add( wxBitmap( radio_xpm
));
269 imagelist
-> Add( wxBitmap( gauge_xpm
));
272 wxButton
*button
= (wxButton
*)NULL
;
274 m_notebook
->SetImageList(imagelist
);
275 m_notebook
->SetBackgroundColour("cadet blue");
277 wxPanel
*panel
= (wxPanel
*) NULL
;
278 panel
= new wxPanel(m_notebook
);
279 panel
->SetBackgroundColour("cadet blue");
280 panel
->SetForegroundColour("blue");
281 m_listbox
= new wxListBox( panel
, ID_LISTBOX
, wxPoint(10,10), wxSize(120,70), 5, choices
);
282 m_listbox
->SetBackgroundColour("wheat");
283 (void)new wxButton( panel
, ID_LISTBOX_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(140,30) );
284 (void)new wxButton( panel
, ID_LISTBOX_SEL_STR
, "Select 'This'", wxPoint(340,30), wxSize(140,30) );
285 (void)new wxButton( panel
, ID_LISTBOX_CLEAR
, "Clear", wxPoint(180,80), wxSize(140,30) );
286 (void)new wxButton( panel
, ID_LISTBOX_APPEND
, "Append 'Hi!'", wxPoint(340,80), wxSize(140,30) );
287 (void)new wxButton( panel
, ID_LISTBOX_DELETE
, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
288 button
= new wxButton( panel
, ID_LISTBOX_FONT
, "Set Italic font", wxPoint(340,130), wxSize(140,30) );
289 button
->SetForegroundColour( "red" );
290 (void)new wxCheckBox( panel
, ID_LISTBOX_ENABLE
, "Disable", wxPoint(20,130), wxSize(140,30) );
291 m_notebook
->AddPage(panel
, "wxList", FALSE
, Image_List
);
293 panel
= new wxPanel(m_notebook
);
294 // panel->SetBackgroundColour("cadet blue");
295 // panel->SetForegroundColour("blue");
296 m_choice
= new wxChoice( panel
, ID_CHOICE
, wxPoint(10,10), wxSize(120,-1), 5, choices
);
297 m_choice
->SetBackgroundColour("wheat");
298 (void)new wxButton( panel
, ID_CHOICE_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(140,30) );
299 (void)new wxButton( panel
, ID_CHOICE_SEL_STR
, "Select 'This'", wxPoint(340,30), wxSize(140,30) );
300 (void)new wxButton( panel
, ID_CHOICE_CLEAR
, "Clear", wxPoint(180,80), wxSize(140,30) );
301 (void)new wxButton( panel
, ID_CHOICE_APPEND
, "Append 'Hi!'", wxPoint(340,80), wxSize(140,30) );
302 (void)new wxButton( panel
, ID_CHOICE_DELETE
, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
303 (void)new wxButton( panel
, ID_CHOICE_FONT
, "Set Italic font", wxPoint(340,130), wxSize(140,30) );
304 (void)new wxCheckBox( panel
, ID_CHOICE_ENABLE
, "Disable", wxPoint(20,130), wxSize(140,30) );
305 m_notebook
->AddPage(panel
, "wxChoice", FALSE
, Image_Choice
);
307 panel
= new wxPanel(m_notebook
);
308 // panel->SetBackgroundColour("cadet blue");
309 // panel->SetForegroundColour("blue");
310 m_combo
= new wxComboBox( panel
, ID_COMBO
, "This", wxPoint(10,10), wxSize(120,-1), 5, choices
);
311 // m_combo->SetBackgroundColour("wheat");
312 (void)new wxButton( panel
, ID_COMBO_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(140,30) );
313 (void)new wxButton( panel
, ID_COMBO_SEL_STR
, "Select 'This'", wxPoint(340,30), wxSize(140,30) );
314 (void)new wxButton( panel
, ID_COMBO_CLEAR
, "Clear", wxPoint(180,80), wxSize(140,30) );
315 (void)new wxButton( panel
, ID_COMBO_APPEND
, "Append 'Hi!'", wxPoint(340,80), wxSize(140,30) );
316 (void)new wxButton( panel
, ID_COMBO_DELETE
, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
317 (void)new wxButton( panel
, ID_COMBO_FONT
, "Set Italic font", wxPoint(340,130), wxSize(140,30) );
318 (void)new wxCheckBox( panel
, ID_COMBO_ENABLE
, "Disable", wxPoint(20,130), wxSize(140,30) );
319 m_notebook
->AddPage(panel
, "wxComboBox", FALSE
, Image_Combo
);
321 panel
= new wxPanel(m_notebook
);
322 // panel->SetBackgroundColour("cadet blue");
323 // panel->SetForegroundColour("blue");
324 wxTextCtrl
*tc
= new wxTextCtrl( panel
, ID_TEXT
, "Write text here.", wxPoint(10,10), wxSize(350,28));
325 // tc->SetBackgroundColour("wheat");
326 tc
= new wxTextCtrl( panel
, ID_TEXT
, "And here.", wxPoint(10,50), wxSize(350,160), wxTE_MULTILINE
);
327 // tc->SetBackgroundColour("wheat");
328 m_notebook
->AddPage(panel
, "wxTextCtrl" , FALSE
, Image_Text
);
330 wxString choices2
[] =
336 panel
= new wxPanel(m_notebook
);
337 // panel->SetBackgroundColour("cadet blue");
338 // panel->SetForegroundColour("blue");
339 m_radio
= new wxRadioBox( panel
, ID_RADIOBOX
, "That", wxPoint(10,160), wxSize(-1,-1), 2, choices2
, 1, wxRA_HORIZONTAL
);
340 // m_radio->SetBackgroundColour("wheat");
341 m_radio
= new wxRadioBox( panel
, ID_RADIOBOX
, "This", wxPoint(10,10), wxSize(-1,-1), 5, choices
, 1, wxRA_VERTICAL
);
342 // m_radio->SetBackgroundColour("wheat");
343 (void)new wxButton( panel
, ID_RADIOBOX_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(140,30) );
344 (void)new wxButton( panel
, ID_RADIOBOX_SEL_STR
, "Select 'This'", wxPoint(180,80), wxSize(140,30) );
345 (void)new wxButton( panel
, ID_RADIOBOX_FONT
, "Set Italic font", wxPoint(180,130), wxSize(140,30) );
346 (void)new wxCheckBox( panel
, ID_RADIOBOX_ENABLE
, "Disable", wxPoint(340,130), wxSize(140,30) );
347 m_fontButton
= new wxButton( panel
, ID_SET_FONT
, "Set more Italic font", wxPoint(340,30), wxSize(160,30) );
348 m_notebook
->AddPage(panel
, "wxRadioBox", FALSE
, Image_Radio
);
350 panel
= new wxPanel(m_notebook
);
351 // panel->SetBackgroundColour("cadet blue");
352 // panel->SetForegroundColour("blue");
353 (void)new wxStaticBox( panel
, -1, "wxGauge and wxSlider", wxPoint(10,10), wxSize(180,130) );
354 m_gauge
= new wxGauge( panel
, -1, 200, wxPoint(18,50), wxSize(155,-1) );
355 // m_gauge->SetBackgroundColour("wheat");
356 m_slider
= new wxSlider( panel
, -1, 0, 0, 200, wxPoint(18,90), wxSize(155,-1) );
357 // m_slider->SetBackgroundColour("wheat");
358 m_notebook
->AddPage(panel
, "wxGauge", FALSE
, Image_Gauge
);
361 void MyPanel::OnSize( wxSizeEvent
& WXUNUSED(event
) )
365 GetClientSize( &x
, &y
);
367 if (m_notebook
) m_notebook
->SetSize( 2, 2, x
-4, y
*2/3-4 );
368 if (m_text
) m_text
->SetSize( 2, y
*2/3+2, x
-4, y
/3-4 );
371 void MyPanel::OnPageChanged( wxNotebookEvent
&event
)
373 *m_text
<< "Notebook selection is " << event
.GetSelection() << "\n";
376 void MyPanel::OnListBox( wxCommandEvent
&event
)
378 m_text
->WriteText( "ListBox selection string is: " );
379 m_text
->WriteText( event
.GetString() );
380 m_text
->WriteText( "\n" );
383 void MyPanel::OnListBoxButtons( wxCommandEvent
&event
)
385 switch (event
.GetId())
387 case ID_LISTBOX_ENABLE
:
389 m_listbox
->Enable( !((bool)event
.GetInt()) );
392 case ID_LISTBOX_SEL_NUM
:
394 m_listbox
->SetSelection( 2 );
397 case ID_LISTBOX_SEL_STR
:
399 m_listbox
->SetStringSelection( "This" );
402 case ID_LISTBOX_CLEAR
:
407 case ID_LISTBOX_APPEND
:
409 m_listbox
->Append( "Hi!" );
412 case ID_LISTBOX_DELETE
:
414 int idx
= m_listbox
->GetSelection();
415 m_listbox
->Delete( idx
);
418 case ID_LISTBOX_FONT
:
420 m_listbox
->SetFont( *wxITALIC_FONT
);
426 void MyPanel::OnChoice( wxCommandEvent
&event
)
428 m_text
->WriteText( "Choice selection string is: " );
429 m_text
->WriteText( event
.GetString() );
430 m_text
->WriteText( "\n" );
433 void MyPanel::OnChoiceButtons( wxCommandEvent
&event
)
435 switch (event
.GetId())
437 case ID_CHOICE_ENABLE
:
439 m_choice
->Enable( !((bool)event
.GetInt()) );
442 case ID_CHOICE_SEL_NUM
:
444 m_choice
->SetSelection( 2 );
447 case ID_CHOICE_SEL_STR
:
449 m_choice
->SetStringSelection( "This" );
452 case ID_CHOICE_CLEAR
:
457 case ID_CHOICE_APPEND
:
459 m_choice
->Append( "Hi!" );
462 case ID_CHOICE_DELETE
:
464 int idx
= m_choice
->GetSelection();
465 m_choice
->Delete( idx
);
470 m_choice
->SetFont( *wxITALIC_FONT
);
476 void MyPanel::OnCombo( wxCommandEvent
&event
)
478 m_text
->WriteText( "ComboBox selection string is: " );
479 m_text
->WriteText( event
.GetString() );
480 m_text
->WriteText( "\n" );
483 void MyPanel::OnComboButtons( wxCommandEvent
&event
)
485 switch (event
.GetId())
487 case ID_COMBO_ENABLE
:
489 m_combo
->Enable( !((bool)event
.GetInt()) );
492 case ID_COMBO_SEL_NUM
:
494 m_combo
->SetSelection( 2 );
497 case ID_COMBO_SEL_STR
:
499 m_combo
->SetStringSelection( "This" );
507 case ID_COMBO_APPEND
:
509 m_combo
->Append( "Hi!" );
512 case ID_COMBO_DELETE
:
514 int idx
= m_combo
->GetSelection();
515 m_combo
->Delete( idx
);
520 m_combo
->SetFont( *wxITALIC_FONT
);
526 void MyPanel::OnRadio( wxCommandEvent
&event
)
528 m_text
->WriteText( "RadioBox selection string is: " );
529 m_text
->WriteText( event
.GetString() );
530 m_text
->WriteText( "\n" );
533 void MyPanel::OnRadioButtons( wxCommandEvent
&event
)
535 switch (event
.GetId())
537 case ID_RADIOBOX_ENABLE
:
539 m_radio
->Enable( !((bool)event
.GetInt()) );
542 case ID_RADIOBOX_SEL_NUM
:
544 m_radio
->SetSelection( 2 );
547 case ID_RADIOBOX_SEL_STR
:
549 m_radio
->SetStringSelection( "This" );
552 case ID_RADIOBOX_FONT
:
554 m_radio
->SetFont( *wxITALIC_FONT
);
560 void MyPanel::OnSetFont( wxCommandEvent
&WXUNUSED(event
) )
562 m_fontButton
->SetFont( *wxITALIC_FONT
);
563 m_text
->SetFont( *wxITALIC_FONT
);
568 delete m_notebook
->GetImageList();
571 //----------------------------------------------------------------------
573 //----------------------------------------------------------------------
575 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
576 EVT_MENU(MINIMAL_QUIT
, MyFrame::OnQuit
)
577 EVT_MENU(MINIMAL_ABOUT
, MyFrame::OnAbout
)
580 MyFrame::MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
):
581 wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
583 (void)new MyPanel( this, 10, 10, 300, 100 );
586 void MyFrame::OnQuit (wxCommandEvent
& WXUNUSED(event
) )
591 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
593 wxMessageDialog
dialog(this, "This is a control sample", "About Controls", wxOK
);