]>
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"
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
);
49 void OnSize( wxSizeEvent
& event
);
50 void OnListBox( wxCommandEvent
&event
);
51 void OnListBoxButtons( wxCommandEvent
&event
);
52 void OnChoice( wxCommandEvent
&event
);
53 void OnChoiceButtons( wxCommandEvent
&event
);
54 void OnCombo( wxCommandEvent
&event
);
55 void OnComboButtons( wxCommandEvent
&event
);
56 void OnRadio( wxCommandEvent
&event
);
57 void OnRadioButtons( wxCommandEvent
&event
);
65 wxNotebook
*m_notebook
;
70 class MyFrame
: public wxFrame
74 MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
);
78 void OnQuit(wxCommandEvent
& event
);
79 void OnAbout(wxCommandEvent
& event
);
80 bool OnClose(void) { return TRUE
; }
85 //----------------------------------------------------------------------
87 //----------------------------------------------------------------------
91 //----------------------------------------------------------------------
93 //----------------------------------------------------------------------
95 const MINIMAL_QUIT
= 100;
96 const MINIMAL_TEXT
= 101;
97 const MINIMAL_ABOUT
= 102;
99 bool MyApp::OnInit(void)
101 // Create the main frame window
102 MyFrame
*frame
= new MyFrame(NULL
, "Controls wxWindows App", 50, 50, 500, 420 );
106 frame
->SetIcon(wxIcon("mondrian"));
108 frame
->SetIcon(wxIcon( mondrian_xpm
));
111 wxMenu
*file_menu
= new wxMenu
;
113 file_menu
->Append(MINIMAL_ABOUT
, "&About");
114 file_menu
->Append(MINIMAL_QUIT
, "E&xit");
115 wxMenuBar
*menu_bar
= new wxMenuBar
;
116 menu_bar
->Append(file_menu
, "&File");
117 frame
->SetMenuBar(menu_bar
);
126 //----------------------------------------------------------------------
128 //----------------------------------------------------------------------
130 class MyTextCtrl
: public wxTextCtrl
133 MyTextCtrl( wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
134 const wxPoint
&pos
, const wxSize
&size
, int style
);
136 void OnRightButton(wxCommandEvent
&) { }
138 DECLARE_EVENT_TABLE()
141 BEGIN_EVENT_TABLE(MyTextCtrl
, wxTextCtrl
)
142 EVT_RIGHT_DOWN(MyTextCtrl::OnRightButton
)
145 MyTextCtrl::MyTextCtrl( wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
146 const wxPoint
&pos
, const wxSize
&size
, int style
) :
147 wxTextCtrl( parent
, id
, value
, pos
, size
, style
)
151 //----------------------------------------------------------------------
153 //----------------------------------------------------------------------
155 const ID_NOTEBOOK
= 1000;
157 const ID_LISTBOX
= 130;
158 const ID_LISTBOX_SEL_NUM
= 131;
159 const ID_LISTBOX_SEL_STR
= 132;
160 const ID_LISTBOX_CLEAR
= 133;
161 const ID_LISTBOX_APPEND
= 134;
163 const ID_CHOICE
= 120;
164 const ID_CHOICE_SEL_NUM
= 121;
165 const ID_CHOICE_SEL_STR
= 122;
166 const ID_CHOICE_CLEAR
= 123;
167 const ID_CHOICE_APPEND
= 124;
169 const ID_COMBO
= 140;
170 const ID_COMBO_SEL_NUM
= 141;
171 const ID_COMBO_SEL_STR
= 142;
172 const ID_COMBO_CLEAR
= 143;
173 const ID_COMBO_APPEND
= 144;
177 const ID_RADIOBOX
= 160;
178 const ID_RADIOBOX_SEL_NUM
= 161;
179 const ID_RADIOBOX_SEL_STR
= 162;
181 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
182 EVT_SIZE ( MyPanel::OnSize
)
183 EVT_LISTBOX (ID_LISTBOX
, MyPanel::OnListBox
)
184 EVT_BUTTON (ID_LISTBOX_SEL_NUM
, MyPanel::OnListBoxButtons
)
185 EVT_BUTTON (ID_LISTBOX_SEL_STR
, MyPanel::OnListBoxButtons
)
186 EVT_BUTTON (ID_LISTBOX_CLEAR
, MyPanel::OnListBoxButtons
)
187 EVT_BUTTON (ID_LISTBOX_APPEND
, MyPanel::OnListBoxButtons
)
188 EVT_CHOICE (ID_CHOICE
, MyPanel::OnChoice
)
189 EVT_BUTTON (ID_CHOICE_SEL_NUM
, MyPanel::OnChoiceButtons
)
190 EVT_BUTTON (ID_CHOICE_SEL_STR
, MyPanel::OnChoiceButtons
)
191 EVT_BUTTON (ID_CHOICE_CLEAR
, MyPanel::OnChoiceButtons
)
192 EVT_BUTTON (ID_CHOICE_APPEND
, MyPanel::OnChoiceButtons
)
193 EVT_CHOICE (ID_COMBO
, MyPanel::OnCombo
)
194 EVT_BUTTON (ID_COMBO_SEL_NUM
, MyPanel::OnComboButtons
)
195 EVT_BUTTON (ID_COMBO_SEL_STR
, MyPanel::OnComboButtons
)
196 EVT_BUTTON (ID_COMBO_CLEAR
, MyPanel::OnComboButtons
)
197 EVT_BUTTON (ID_COMBO_APPEND
, MyPanel::OnComboButtons
)
198 EVT_RADIOBOX (ID_RADIOBOX
, MyPanel::OnRadio
)
199 EVT_BUTTON (ID_RADIOBOX_SEL_NUM
, MyPanel::OnRadioButtons
)
200 EVT_BUTTON (ID_RADIOBOX_SEL_STR
, MyPanel::OnRadioButtons
)
203 MyPanel::MyPanel( wxFrame
*frame
, int x
, int y
, int w
, int h
) :
204 wxPanel( frame
, -1, wxPoint(x
, y
), wxSize(w
, h
) )
206 m_text
= new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(0,50), wxSize(100,50), wxTE_MULTILINE
);
208 m_notebook
= new wxNotebook( this, ID_NOTEBOOK
, wxPoint(0,0), wxSize(200,150) );
218 wxPanel
*panel
= new wxPanel(m_notebook
);
219 m_listbox
= new wxListBox( panel
, ID_LISTBOX
, wxPoint(10,10), wxSize(120,70), 4, choices
);
220 (void)new wxButton( panel
, ID_LISTBOX_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(100,30) );
221 (void)new wxButton( panel
, ID_LISTBOX_SEL_STR
, "Select 'This'", wxPoint(300,30), wxSize(100,30) );
222 (void)new wxButton( panel
, ID_LISTBOX_CLEAR
, "Clear", wxPoint(180,80), wxSize(100,30) );
223 (void)new wxButton( panel
, ID_LISTBOX_APPEND
, "Append 'Hi!'", wxPoint(300,80), wxSize(100,30) );
224 m_notebook
->AddPage(panel
, "wxList");
226 panel
= new wxPanel(m_notebook
);
227 m_choice
= new wxChoice( panel
, ID_CHOICE
, wxPoint(10,10), wxSize(120,-1), 4, choices
);
228 (void)new wxButton( panel
, ID_CHOICE_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(100,30) );
229 (void)new wxButton( panel
, ID_CHOICE_SEL_STR
, "Select 'This'", wxPoint(300,30), wxSize(100,30) );
230 (void)new wxButton( panel
, ID_CHOICE_CLEAR
, "Clear", wxPoint(180,80), wxSize(100,30) );
231 (void)new wxButton( panel
, ID_CHOICE_APPEND
, "Append 'Hi!'", wxPoint(300,80), wxSize(100,30) );
232 m_notebook
->AddPage(panel
, "wxChoice");
234 panel
= new wxPanel(m_notebook
);
235 m_combo
= new wxComboBox( panel
, ID_COMBO
, "This", wxPoint(10,10), wxSize(170,-1), 4, choices
);
236 (void)new wxButton( panel
, ID_COMBO_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(100,30) );
237 (void)new wxButton( panel
, ID_COMBO_SEL_STR
, "Select 'This'", wxPoint(300,30), wxSize(100,30) );
238 (void)new wxButton( panel
, ID_COMBO_CLEAR
, "Clear", wxPoint(180,80), wxSize(100,30) );
239 (void)new wxButton( panel
, ID_COMBO_APPEND
, "Append 'Hi!'", wxPoint(300,80), wxSize(100,30) );
240 m_notebook
->AddPage(panel
, "wxComboBox");
242 wxTextCtrl
*text
= new wxTextCtrl( m_notebook
, ID_TEXT
, "Write text here.", wxPoint(10,10), wxSize(120,100), wxTE_MULTILINE
);
243 m_notebook
->AddPage( text
, "wxTextCtrl" );
245 panel
= new wxPanel(m_notebook
);
246 m_radio
= new wxRadioBox( panel
, ID_RADIOBOX
, "This", wxPoint(10,10), wxSize(-1,-1), 4, choices
);
247 (void)new wxButton( panel
, ID_RADIOBOX_SEL_NUM
, "Select #2", wxPoint(200,30), wxSize(100,30) );
248 (void)new wxButton( panel
, ID_RADIOBOX_SEL_STR
, "Select 'This'", wxPoint(200,80), wxSize(100,30) );
249 m_notebook
->AddPage(panel
, "wxRadioBox");
252 void MyPanel::OnSize( wxSizeEvent
& WXUNUSED(event
) )
256 GetClientSize( &x
, &y
);
258 if (m_notebook
) m_notebook
->SetSize( 2, 2, x
-4, y
/2-4 );
259 if (m_text
) m_text
->SetSize( 2, y
/2+2, x
-4, y
/2-4 );
262 void MyPanel::OnListBox( wxCommandEvent
&event
)
264 m_text
->WriteText( "ListBox selection string is: " );
265 m_text
->WriteText( event
.GetString() );
266 m_text
->WriteText( "\n" );
269 void MyPanel::OnListBoxButtons( wxCommandEvent
&event
)
271 switch (event
.GetId())
273 case ID_LISTBOX_SEL_NUM
:
275 m_listbox
->SetSelection( 2 );
278 case ID_LISTBOX_SEL_STR
:
280 m_listbox
->SetStringSelection( "This" );
283 case ID_LISTBOX_CLEAR
:
288 case ID_LISTBOX_APPEND
:
290 m_listbox
->Append( "Hi!" );
296 void MyPanel::OnChoice( wxCommandEvent
&event
)
298 m_text
->WriteText( "Choice selection string is: " );
299 m_text
->WriteText( event
.GetString() );
300 m_text
->WriteText( "\n" );
303 void MyPanel::OnChoiceButtons( wxCommandEvent
&event
)
305 switch (event
.GetId())
307 case ID_CHOICE_SEL_NUM
:
309 m_choice
->SetSelection( 2 );
312 case ID_CHOICE_SEL_STR
:
314 m_choice
->SetStringSelection( "This" );
317 case ID_CHOICE_CLEAR
:
322 case ID_CHOICE_APPEND
:
324 m_choice
->Append( "Hi!" );
330 void MyPanel::OnCombo( wxCommandEvent
&event
)
332 m_text
->WriteText( "ComboBox selection string is: " );
333 m_text
->WriteText( event
.GetString() );
334 m_text
->WriteText( "\n" );
337 void MyPanel::OnComboButtons( wxCommandEvent
&event
)
339 switch (event
.GetId())
341 case ID_COMBO_SEL_NUM
:
343 m_combo
->SetSelection( 2 );
346 case ID_COMBO_SEL_STR
:
348 m_combo
->SetStringSelection( "This" );
356 case ID_COMBO_APPEND
:
358 m_combo
->Append( "Hi!" );
364 void MyPanel::OnRadio( wxCommandEvent
&event
)
366 m_text
->WriteText( "RadioBox selection string is: " );
367 m_text
->WriteText( event
.GetString() );
368 m_text
->WriteText( "\n" );
371 void MyPanel::OnRadioButtons( wxCommandEvent
&event
)
373 switch (event
.GetId())
375 case ID_RADIOBOX_SEL_NUM
:
377 m_radio
->SetSelection( 2 );
380 case ID_RADIOBOX_SEL_STR
:
382 m_radio
->SetStringSelection( "This" );
388 //----------------------------------------------------------------------
390 //----------------------------------------------------------------------
392 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
393 EVT_MENU(MINIMAL_QUIT
, MyFrame::OnQuit
)
394 EVT_MENU(MINIMAL_ABOUT
, MyFrame::OnAbout
)
397 MyFrame::MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
):
398 wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
400 (void)new MyPanel( this, 10, 10, 300, 100 );
403 void MyFrame::OnQuit (wxCommandEvent
& WXUNUSED(event
) )
408 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
410 wxMessageDialog
dialog(this, "This is a control sample", "About Controls", wxOK
);