]>
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 "minimal.cpp"
13 #pragma interface "minimal.cpp"
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
27 #include "wx/notebook.h"
28 #include "wx/imaglist.h"
31 #include "mondrian.xpm"
34 //----------------------------------------------------------------------
36 //----------------------------------------------------------------------
38 class MyApp
: public wxApp
44 class MyPanel
: public wxPanel
48 MyPanel(wxFrame
*frame
, int x
, int y
, int w
, int h
);
51 void OnSize( wxSizeEvent
& event
);
52 void OnListBox( wxCommandEvent
&event
);
53 void OnListBoxButtons( wxCommandEvent
&event
);
54 void OnChoice( wxCommandEvent
&event
);
55 void OnChoiceButtons( wxCommandEvent
&event
);
56 void OnCombo( wxCommandEvent
&event
);
57 void OnComboButtons( wxCommandEvent
&event
);
58 void OnRadio( wxCommandEvent
&event
);
59 void OnRadioButtons( wxCommandEvent
&event
);
67 wxNotebook
*m_notebook
;
72 class MyFrame
: public wxFrame
76 MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
);
80 void OnQuit(wxCommandEvent
& event
);
81 void OnAbout(wxCommandEvent
& event
);
82 bool OnClose(void) { return TRUE
; }
87 //----------------------------------------------------------------------
89 //----------------------------------------------------------------------
93 //----------------------------------------------------------------------
95 //----------------------------------------------------------------------
97 const int MINIMAL_QUIT
= 100;
98 const int MINIMAL_TEXT
= 101;
99 const int MINIMAL_ABOUT
= 102;
101 bool MyApp::OnInit(void)
103 // Create the main frame window
104 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, (char *) "Controls wxWindows App", 50, 50, 500, 420 );
108 frame
->SetIcon(wxIcon("mondrian"));
110 frame
->SetIcon(wxIcon( mondrian_xpm
));
113 wxMenu
*file_menu
= new wxMenu
;
115 file_menu
->Append(MINIMAL_ABOUT
, "&About");
116 file_menu
->Append(MINIMAL_QUIT
, "E&xit");
117 wxMenuBar
*menu_bar
= new wxMenuBar
;
118 menu_bar
->Append(file_menu
, "&File");
119 frame
->SetMenuBar(menu_bar
);
128 //----------------------------------------------------------------------
130 //----------------------------------------------------------------------
132 const ID_NOTEBOOK
= 1000;
134 const ID_LISTBOX
= 130;
135 const ID_LISTBOX_SEL_NUM
= 131;
136 const ID_LISTBOX_SEL_STR
= 132;
137 const ID_LISTBOX_CLEAR
= 133;
138 const ID_LISTBOX_APPEND
= 134;
139 const ID_LISTBOX_DELETE
= 135;
141 const ID_CHOICE
= 120;
142 const ID_CHOICE_SEL_NUM
= 121;
143 const ID_CHOICE_SEL_STR
= 122;
144 const ID_CHOICE_CLEAR
= 123;
145 const ID_CHOICE_APPEND
= 124;
146 const ID_CHOICE_DELETE
= 125;
148 const ID_COMBO
= 140;
149 const ID_COMBO_SEL_NUM
= 141;
150 const ID_COMBO_SEL_STR
= 142;
151 const ID_COMBO_CLEAR
= 143;
152 const ID_COMBO_APPEND
= 144;
153 const ID_COMBO_DELETE
= 145;
157 const ID_RADIOBOX
= 160;
158 const ID_RADIOBOX_SEL_NUM
= 161;
159 const ID_RADIOBOX_SEL_STR
= 162;
161 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
162 EVT_SIZE ( MyPanel::OnSize
)
163 EVT_LISTBOX (ID_LISTBOX
, MyPanel::OnListBox
)
164 EVT_BUTTON (ID_LISTBOX_SEL_NUM
, MyPanel::OnListBoxButtons
)
165 EVT_BUTTON (ID_LISTBOX_SEL_STR
, MyPanel::OnListBoxButtons
)
166 EVT_BUTTON (ID_LISTBOX_CLEAR
, MyPanel::OnListBoxButtons
)
167 EVT_BUTTON (ID_LISTBOX_APPEND
, MyPanel::OnListBoxButtons
)
168 EVT_BUTTON (ID_LISTBOX_DELETE
, MyPanel::OnListBoxButtons
)
169 EVT_CHOICE (ID_CHOICE
, MyPanel::OnChoice
)
170 EVT_BUTTON (ID_CHOICE_SEL_NUM
, MyPanel::OnChoiceButtons
)
171 EVT_BUTTON (ID_CHOICE_SEL_STR
, MyPanel::OnChoiceButtons
)
172 EVT_BUTTON (ID_CHOICE_CLEAR
, MyPanel::OnChoiceButtons
)
173 EVT_BUTTON (ID_CHOICE_APPEND
, MyPanel::OnChoiceButtons
)
174 EVT_BUTTON (ID_CHOICE_DELETE
, MyPanel::OnChoiceButtons
)
175 EVT_CHOICE (ID_COMBO
, MyPanel::OnCombo
)
176 EVT_BUTTON (ID_COMBO_SEL_NUM
, MyPanel::OnComboButtons
)
177 EVT_BUTTON (ID_COMBO_SEL_STR
, MyPanel::OnComboButtons
)
178 EVT_BUTTON (ID_COMBO_CLEAR
, MyPanel::OnComboButtons
)
179 EVT_BUTTON (ID_COMBO_APPEND
, MyPanel::OnComboButtons
)
180 EVT_BUTTON (ID_COMBO_DELETE
, MyPanel::OnComboButtons
)
181 EVT_RADIOBOX (ID_RADIOBOX
, MyPanel::OnRadio
)
182 EVT_BUTTON (ID_RADIOBOX_SEL_NUM
, MyPanel::OnRadioButtons
)
183 EVT_BUTTON (ID_RADIOBOX_SEL_STR
, MyPanel::OnRadioButtons
)
186 MyPanel::MyPanel( wxFrame
*frame
, int x
, int y
, int w
, int h
) :
187 wxPanel( frame
, -1, wxPoint(x
, y
), wxSize(w
, h
) )
189 m_text
= new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(0,50), wxSize(100,50), wxTE_MULTILINE
);
191 m_notebook
= new wxNotebook( this, ID_NOTEBOOK
, wxPoint(0,0), wxSize(200,150) );
201 // image ids and names
204 Image_List
, Image_Choice
, Image_Combo
, Image_Text
, Image_Radio
, Image_Max
207 const char *aIconNames
[] =
209 "list.xpm", "choice.xpm", "combo.xpm", "text.xpm", "radio.xpm"
212 wxASSERT( WXSIZEOF(aIconNames
) == Image_Max
); // keep in sync
214 // fill the image list
215 wxString strIconDir
= "icons/";
216 wxImageList
*imagelist
= new wxImageList(32, 32);
217 for ( size_t n
= 0; n
< Image_Max
; n
++ ) {
218 imagelist
->Add(wxBitmap(strIconDir
+ aIconNames
[n
]));
221 m_notebook
->SetImageList(imagelist
);
223 wxPanel
*panel
= new wxPanel(m_notebook
);
224 m_listbox
= new wxListBox( panel
, ID_LISTBOX
, wxPoint(10,10), wxSize(120,70), 4, choices
);
225 (void)new wxButton( panel
, ID_LISTBOX_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(100,30) );
226 (void)new wxButton( panel
, ID_LISTBOX_SEL_STR
, "Select 'This'", wxPoint(300,30), wxSize(100,30) );
227 (void)new wxButton( panel
, ID_LISTBOX_CLEAR
, "Clear", wxPoint(180,80), wxSize(100,30) );
228 (void)new wxButton( panel
, ID_LISTBOX_APPEND
, "Append 'Hi!'", wxPoint(300,80), wxSize(100,30) );
229 (void)new wxButton( panel
, ID_LISTBOX_DELETE
, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
230 m_notebook
->AddPage(panel
, "wxList", FALSE
, Image_List
);
232 panel
= new wxPanel(m_notebook
);
233 m_choice
= new wxChoice( panel
, ID_CHOICE
, wxPoint(10,10), wxSize(120,-1), 4, choices
);
234 (void)new wxButton( panel
, ID_CHOICE_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(100,30) );
235 (void)new wxButton( panel
, ID_CHOICE_SEL_STR
, "Select 'This'", wxPoint(300,30), wxSize(100,30) );
236 (void)new wxButton( panel
, ID_CHOICE_CLEAR
, "Clear", wxPoint(180,80), wxSize(100,30) );
237 (void)new wxButton( panel
, ID_CHOICE_APPEND
, "Append 'Hi!'", wxPoint(300,80), wxSize(100,30) );
238 (void)new wxButton( panel
, ID_CHOICE_DELETE
, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
239 m_notebook
->AddPage(panel
, "wxChoice", FALSE
, Image_Choice
);
241 panel
= new wxPanel(m_notebook
);
242 m_combo
= new wxComboBox( panel
, ID_COMBO
, "This", wxPoint(10,10), wxSize(170,-1), 4, choices
);
243 (void)new wxButton( panel
, ID_COMBO_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(100,30) );
244 (void)new wxButton( panel
, ID_COMBO_SEL_STR
, "Select 'This'", wxPoint(300,30), wxSize(100,30) );
245 (void)new wxButton( panel
, ID_COMBO_CLEAR
, "Clear", wxPoint(180,80), wxSize(100,30) );
246 (void)new wxButton( panel
, ID_COMBO_APPEND
, "Append 'Hi!'", wxPoint(300,80), wxSize(100,30) );
247 (void)new wxButton( panel
, ID_COMBO_DELETE
, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
248 m_notebook
->AddPage(panel
, "wxComboBox", FALSE
, Image_Combo
);
250 wxTextCtrl
*text
= new wxTextCtrl( m_notebook
, ID_TEXT
, "Write text here.", wxPoint(10,10), wxSize(120,100), wxTE_MULTILINE
);
251 m_notebook
->AddPage(text
, "wxTextCtrl" , FALSE
, Image_Text
);
253 panel
= new wxPanel(m_notebook
);
254 m_radio
= new wxRadioBox( panel
, ID_RADIOBOX
, "This", wxPoint(10,10), wxSize(-1,-1), 4, choices
);
255 (void)new wxButton( panel
, ID_RADIOBOX_SEL_NUM
, "Select #2", wxPoint(200,30), wxSize(100,30) );
256 (void)new wxButton( panel
, ID_RADIOBOX_SEL_STR
, "Select 'This'", wxPoint(200,80), wxSize(100,30) );
257 m_notebook
->AddPage(panel
, "wxRadioBox", FALSE
, Image_Radio
);
260 void MyPanel::OnSize( wxSizeEvent
& WXUNUSED(event
) )
264 GetClientSize( &x
, &y
);
266 if (m_notebook
) m_notebook
->SetSize( 2, 2, x
-4, y
*2/3-4 );
267 if (m_text
) m_text
->SetSize( 2, y
*2/3+2, x
-4, y
/3-4 );
270 void MyPanel::OnListBox( wxCommandEvent
&event
)
272 m_text
->WriteText( "ListBox selection string is: " );
273 m_text
->WriteText( event
.GetString() );
274 m_text
->WriteText( "\n" );
277 void MyPanel::OnListBoxButtons( wxCommandEvent
&event
)
279 switch (event
.GetId())
281 case ID_LISTBOX_SEL_NUM
:
283 m_listbox
->SetSelection( 2 );
286 case ID_LISTBOX_SEL_STR
:
288 m_listbox
->SetStringSelection( "This" );
291 case ID_LISTBOX_CLEAR
:
296 case ID_LISTBOX_APPEND
:
298 m_listbox
->Append( "Hi!" );
301 case ID_LISTBOX_DELETE
:
303 int idx
= m_listbox
->GetSelection();
304 m_listbox
->Delete( idx
);
310 void MyPanel::OnChoice( wxCommandEvent
&event
)
312 m_text
->WriteText( "Choice selection string is: " );
313 m_text
->WriteText( event
.GetString() );
314 m_text
->WriteText( "\n" );
317 void MyPanel::OnChoiceButtons( wxCommandEvent
&event
)
319 switch (event
.GetId())
321 case ID_CHOICE_SEL_NUM
:
323 m_choice
->SetSelection( 2 );
326 case ID_CHOICE_SEL_STR
:
328 m_choice
->SetStringSelection( "This" );
331 case ID_CHOICE_CLEAR
:
336 case ID_CHOICE_APPEND
:
338 m_choice
->Append( "Hi!" );
341 case ID_CHOICE_DELETE
:
343 int idx
= m_choice
->GetSelection();
344 m_choice
->Delete( idx
);
350 void MyPanel::OnCombo( wxCommandEvent
&event
)
352 m_text
->WriteText( "ComboBox selection string is: " );
353 m_text
->WriteText( event
.GetString() );
354 m_text
->WriteText( "\n" );
357 void MyPanel::OnComboButtons( wxCommandEvent
&event
)
359 switch (event
.GetId())
361 case ID_COMBO_SEL_NUM
:
363 m_combo
->SetSelection( 2 );
366 case ID_COMBO_SEL_STR
:
368 m_combo
->SetStringSelection( "This" );
376 case ID_COMBO_APPEND
:
378 m_combo
->Append( "Hi!" );
381 case ID_COMBO_DELETE
:
383 int idx
= m_combo
->GetSelection();
384 m_combo
->Delete( idx
);
390 void MyPanel::OnRadio( wxCommandEvent
&event
)
392 m_text
->WriteText( "RadioBox selection string is: " );
393 m_text
->WriteText( event
.GetString() );
394 m_text
->WriteText( "\n" );
397 void MyPanel::OnRadioButtons( wxCommandEvent
&event
)
399 switch (event
.GetId())
401 case ID_RADIOBOX_SEL_NUM
:
403 m_radio
->SetSelection( 2 );
406 case ID_RADIOBOX_SEL_STR
:
408 m_radio
->SetStringSelection( "This" );
416 delete m_notebook
->GetImageList();
419 //----------------------------------------------------------------------
421 //----------------------------------------------------------------------
423 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
424 EVT_MENU(MINIMAL_QUIT
, MyFrame::OnQuit
)
425 EVT_MENU(MINIMAL_ABOUT
, MyFrame::OnAbout
)
428 MyFrame::MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
):
429 wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
431 (void)new MyPanel( this, 10, 10, 300, 100 );
434 void MyFrame::OnQuit (wxCommandEvent
& WXUNUSED(event
) )
439 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
441 wxMessageDialog
dialog(this, "This is a control sample", "About Controls", wxOK
);