]>
git.saurik.com Git - wxWidgets.git/blob - samples/controls/controls.cpp
385a294f95e3fd44df84bb272e6f6eac1392fa47
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
);
60 void OnSetFont( wxCommandEvent
&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, 500, 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;
144 const ID_CHOICE
= 120;
145 const ID_CHOICE_SEL_NUM
= 121;
146 const ID_CHOICE_SEL_STR
= 122;
147 const ID_CHOICE_CLEAR
= 123;
148 const ID_CHOICE_APPEND
= 124;
149 const ID_CHOICE_DELETE
= 125;
150 const ID_CHOICE_FONT
= 126;
152 const ID_COMBO
= 140;
153 const ID_COMBO_SEL_NUM
= 141;
154 const ID_COMBO_SEL_STR
= 142;
155 const ID_COMBO_CLEAR
= 143;
156 const ID_COMBO_APPEND
= 144;
157 const ID_COMBO_DELETE
= 145;
158 const ID_COMBO_FONT
= 146;
162 const ID_RADIOBOX
= 160;
163 const ID_RADIOBOX_SEL_NUM
= 161;
164 const ID_RADIOBOX_SEL_STR
= 162;
165 const ID_RADIOBOX_FONT
= 163;
167 const ID_SET_FONT
= 170;
169 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
170 EVT_SIZE ( MyPanel::OnSize
)
171 EVT_LISTBOX (ID_LISTBOX
, MyPanel::OnListBox
)
172 EVT_BUTTON (ID_LISTBOX_SEL_NUM
, MyPanel::OnListBoxButtons
)
173 EVT_BUTTON (ID_LISTBOX_SEL_STR
, MyPanel::OnListBoxButtons
)
174 EVT_BUTTON (ID_LISTBOX_CLEAR
, MyPanel::OnListBoxButtons
)
175 EVT_BUTTON (ID_LISTBOX_APPEND
, MyPanel::OnListBoxButtons
)
176 EVT_BUTTON (ID_LISTBOX_DELETE
, MyPanel::OnListBoxButtons
)
177 EVT_BUTTON (ID_LISTBOX_FONT
, MyPanel::OnListBoxButtons
)
178 EVT_CHOICE (ID_CHOICE
, MyPanel::OnChoice
)
179 EVT_BUTTON (ID_CHOICE_SEL_NUM
, MyPanel::OnChoiceButtons
)
180 EVT_BUTTON (ID_CHOICE_SEL_STR
, MyPanel::OnChoiceButtons
)
181 EVT_BUTTON (ID_CHOICE_CLEAR
, MyPanel::OnChoiceButtons
)
182 EVT_BUTTON (ID_CHOICE_APPEND
, MyPanel::OnChoiceButtons
)
183 EVT_BUTTON (ID_CHOICE_DELETE
, MyPanel::OnChoiceButtons
)
184 EVT_BUTTON (ID_CHOICE_FONT
, MyPanel::OnChoiceButtons
)
185 EVT_CHOICE (ID_COMBO
, MyPanel::OnCombo
)
186 EVT_BUTTON (ID_COMBO_SEL_NUM
, MyPanel::OnComboButtons
)
187 EVT_BUTTON (ID_COMBO_SEL_STR
, MyPanel::OnComboButtons
)
188 EVT_BUTTON (ID_COMBO_CLEAR
, MyPanel::OnComboButtons
)
189 EVT_BUTTON (ID_COMBO_APPEND
, MyPanel::OnComboButtons
)
190 EVT_BUTTON (ID_COMBO_DELETE
, MyPanel::OnComboButtons
)
191 EVT_BUTTON (ID_COMBO_FONT
, MyPanel::OnComboButtons
)
192 EVT_RADIOBOX (ID_RADIOBOX
, MyPanel::OnRadio
)
193 EVT_BUTTON (ID_RADIOBOX_SEL_NUM
, MyPanel::OnRadioButtons
)
194 EVT_BUTTON (ID_RADIOBOX_SEL_STR
, MyPanel::OnRadioButtons
)
195 EVT_BUTTON (ID_RADIOBOX_FONT
, MyPanel::OnRadioButtons
)
196 EVT_BUTTON (ID_SET_FONT
, MyPanel::OnSetFont
)
199 MyPanel::MyPanel( wxFrame
*frame
, int x
, int y
, int w
, int h
) :
200 wxPanel( frame
, -1, wxPoint(x
, y
), wxSize(w
, h
) )
202 m_text
= new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(0,50), wxSize(100,50), wxTE_MULTILINE
);
204 m_notebook
= new wxNotebook( this, ID_NOTEBOOK
, wxPoint(0,0), wxSize(200,150) );
214 // image ids and names
217 Image_List
, Image_Choice
, Image_Combo
, Image_Text
, Image_Radio
, Image_Max
220 const char *aIconNames
[] =
222 "list.xpm", "choice.xpm", "combo.xpm", "text.xpm", "radio.xpm"
225 wxASSERT( WXSIZEOF(aIconNames
) == Image_Max
); // keep in sync
227 // fill the image list
229 wxString strIconDir
= "icons/";
231 wxString strIconDir
= "../icons/";
234 wxImageList
*imagelist
= new wxImageList(32, 32);
235 for ( size_t n
= 0; n
< Image_Max
; n
++ )
237 imagelist
->Add(wxBitmap(strIconDir
+ aIconNames
[n
]));
240 m_notebook
->SetImageList(imagelist
);
242 wxPanel
*panel
= new wxPanel(m_notebook
);
243 m_listbox
= new wxListBox( panel
, ID_LISTBOX
, wxPoint(10,10), wxSize(120,70), 4, choices
);
244 (void)new wxButton( panel
, ID_LISTBOX_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(100,30) );
245 (void)new wxButton( panel
, ID_LISTBOX_SEL_STR
, "Select 'This'", wxPoint(300,30), wxSize(100,30) );
246 (void)new wxButton( panel
, ID_LISTBOX_CLEAR
, "Clear", wxPoint(180,80), wxSize(100,30) );
247 (void)new wxButton( panel
, ID_LISTBOX_APPEND
, "Append 'Hi!'", wxPoint(300,80), wxSize(100,30) );
248 (void)new wxButton( panel
, ID_LISTBOX_DELETE
, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
249 (void)new wxButton( panel
, ID_LISTBOX_FONT
, "Set Italic font", wxPoint(180,180), wxSize(140,30) );
250 m_notebook
->AddPage(panel
, "wxList", FALSE
, Image_List
);
252 panel
= new wxPanel(m_notebook
);
253 m_choice
= new wxChoice( panel
, ID_CHOICE
, wxPoint(10,10), wxSize(120,-1), 4, choices
);
254 (void)new wxButton( panel
, ID_CHOICE_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(100,30) );
255 (void)new wxButton( panel
, ID_CHOICE_SEL_STR
, "Select 'This'", wxPoint(300,30), wxSize(100,30) );
256 (void)new wxButton( panel
, ID_CHOICE_CLEAR
, "Clear", wxPoint(180,80), wxSize(100,30) );
257 (void)new wxButton( panel
, ID_CHOICE_APPEND
, "Append 'Hi!'", wxPoint(300,80), wxSize(100,30) );
258 (void)new wxButton( panel
, ID_CHOICE_DELETE
, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
259 (void)new wxButton( panel
, ID_CHOICE_FONT
, "Set Italic font", wxPoint(180,180), wxSize(140,30) );
260 m_notebook
->AddPage(panel
, "wxChoice", FALSE
, Image_Choice
);
262 panel
= new wxPanel(m_notebook
);
263 m_combo
= new wxComboBox( panel
, ID_COMBO
, "This", wxPoint(10,10), wxSize(170,-1), 4, choices
);
264 (void)new wxButton( panel
, ID_COMBO_SEL_NUM
, "Select #2", wxPoint(180,30), wxSize(100,30) );
265 (void)new wxButton( panel
, ID_COMBO_SEL_STR
, "Select 'This'", wxPoint(300,30), wxSize(100,30) );
266 (void)new wxButton( panel
, ID_COMBO_CLEAR
, "Clear", wxPoint(180,80), wxSize(100,30) );
267 (void)new wxButton( panel
, ID_COMBO_APPEND
, "Append 'Hi!'", wxPoint(300,80), wxSize(100,30) );
268 (void)new wxButton( panel
, ID_COMBO_DELETE
, "Delete selected item", wxPoint(180,130), wxSize(140,30) );
269 (void)new wxButton( panel
, ID_COMBO_FONT
, "Set Italic font", wxPoint(180,180), wxSize(140,30) );
270 m_notebook
->AddPage(panel
, "wxComboBox", FALSE
, Image_Combo
);
272 wxTextCtrl
*text
= new wxTextCtrl( m_notebook
, ID_TEXT
, "Write text here.", wxPoint(10,10), wxSize(120,100), wxTE_MULTILINE
);
273 m_notebook
->AddPage(text
, "wxTextCtrl" , FALSE
, Image_Text
);
275 panel
= new wxPanel(m_notebook
);
276 m_radio
= new wxRadioBox( panel
, ID_RADIOBOX
, "This", wxPoint(10,10), wxSize(-1,-1), 4, choices
);
277 (void)new wxButton( panel
, ID_RADIOBOX_SEL_NUM
, "Select #2", wxPoint(200,30), wxSize(100,30) );
278 (void)new wxButton( panel
, ID_RADIOBOX_SEL_STR
, "Select 'This'", wxPoint(200,80), wxSize(100,30) );
279 (void)new wxButton( panel
, ID_RADIOBOX_FONT
, "Set Italic font", wxPoint(200,130), wxSize(160,30) );
280 m_fontButton
= new wxButton( panel
, ID_SET_FONT
, "Set more Italic font", wxPoint(200,180), wxSize(160,30) );
281 m_notebook
->AddPage(panel
, "wxRadioBox", FALSE
, Image_Radio
);
284 void MyPanel::OnSize( wxSizeEvent
& WXUNUSED(event
) )
288 GetClientSize( &x
, &y
);
290 if (m_notebook
) m_notebook
->SetSize( 2, 2, x
-4, y
*2/3-4 );
291 if (m_text
) m_text
->SetSize( 2, y
*2/3+2, x
-4, y
/3-4 );
294 void MyPanel::OnListBox( wxCommandEvent
&event
)
296 m_text
->WriteText( "ListBox selection string is: " );
297 m_text
->WriteText( event
.GetString() );
298 m_text
->WriteText( "\n" );
301 void MyPanel::OnListBoxButtons( wxCommandEvent
&event
)
303 switch (event
.GetId())
305 case ID_LISTBOX_SEL_NUM
:
307 m_listbox
->SetSelection( 2 );
310 case ID_LISTBOX_SEL_STR
:
312 m_listbox
->SetStringSelection( "This" );
315 case ID_LISTBOX_CLEAR
:
320 case ID_LISTBOX_APPEND
:
322 m_listbox
->Append( "Hi!" );
325 case ID_LISTBOX_DELETE
:
327 int idx
= m_listbox
->GetSelection();
328 m_listbox
->Delete( idx
);
331 case ID_LISTBOX_FONT
:
333 m_listbox
->SetFont( *wxITALIC_FONT
);
339 void MyPanel::OnChoice( wxCommandEvent
&event
)
341 m_text
->WriteText( "Choice selection string is: " );
342 m_text
->WriteText( event
.GetString() );
343 m_text
->WriteText( "\n" );
346 void MyPanel::OnChoiceButtons( wxCommandEvent
&event
)
348 switch (event
.GetId())
350 case ID_CHOICE_SEL_NUM
:
352 m_choice
->SetSelection( 2 );
355 case ID_CHOICE_SEL_STR
:
357 m_choice
->SetStringSelection( "This" );
360 case ID_CHOICE_CLEAR
:
365 case ID_CHOICE_APPEND
:
367 m_choice
->Append( "Hi!" );
370 case ID_CHOICE_DELETE
:
372 int idx
= m_choice
->GetSelection();
373 m_choice
->Delete( idx
);
378 m_choice
->SetFont( *wxITALIC_FONT
);
384 void MyPanel::OnCombo( wxCommandEvent
&event
)
386 m_text
->WriteText( "ComboBox selection string is: " );
387 m_text
->WriteText( event
.GetString() );
388 m_text
->WriteText( "\n" );
391 void MyPanel::OnComboButtons( wxCommandEvent
&event
)
393 switch (event
.GetId())
395 case ID_COMBO_SEL_NUM
:
397 m_combo
->SetSelection( 2 );
400 case ID_COMBO_SEL_STR
:
402 m_combo
->SetStringSelection( "This" );
410 case ID_COMBO_APPEND
:
412 m_combo
->Append( "Hi!" );
415 case ID_COMBO_DELETE
:
417 int idx
= m_combo
->GetSelection();
418 m_combo
->Delete( idx
);
423 m_combo
->SetFont( *wxITALIC_FONT
);
429 void MyPanel::OnRadio( wxCommandEvent
&event
)
431 m_text
->WriteText( "RadioBox selection string is: " );
432 m_text
->WriteText( event
.GetString() );
433 m_text
->WriteText( "\n" );
436 void MyPanel::OnRadioButtons( wxCommandEvent
&event
)
438 switch (event
.GetId())
440 case ID_RADIOBOX_SEL_NUM
:
442 m_radio
->SetSelection( 2 );
445 case ID_RADIOBOX_SEL_STR
:
447 m_radio
->SetStringSelection( "This" );
450 case ID_RADIOBOX_FONT
:
452 m_radio
->SetFont( *wxITALIC_FONT
);
458 void MyPanel::OnSetFont( wxCommandEvent
&WXUNUSED(event
) )
460 m_fontButton
->SetFont( *wxITALIC_FONT
);
461 m_text
->SetFont( *wxITALIC_FONT
);
466 delete m_notebook
->GetImageList();
469 //----------------------------------------------------------------------
471 //----------------------------------------------------------------------
473 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
474 EVT_MENU(MINIMAL_QUIT
, MyFrame::OnQuit
)
475 EVT_MENU(MINIMAL_ABOUT
, MyFrame::OnAbout
)
478 MyFrame::MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
):
479 wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
481 (void)new MyPanel( this, 10, 10, 300, 100 );
484 void MyFrame::OnQuit (wxCommandEvent
& WXUNUSED(event
) )
489 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
491 wxMessageDialog
dialog(this, "This is a control sample", "About Controls", wxOK
);