]> git.saurik.com Git - wxWidgets.git/blame - samples/controls/controls.cpp
optimizations: more functions made inline, added Alloc()/Shrink() function for
[wxWidgets.git] / samples / controls / controls.cpp
CommitLineData
1c005ff7
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: minimal.cpp
3// Purpose: Controls wxWindows sample
4// Author: Robert Roebling
5// Modified by:
6// RCS-ID: $Id$
7// Copyright: (c) Robert Roebling, Julian Smart and Markus Holzem
8// Licence: wxWindows license
9/////////////////////////////////////////////////////////////////////////////
10
11#ifdef __GNUG__
12#pragma implementation "minimal.cpp"
13#pragma interface "minimal.cpp"
14#endif
15
16// For compilers that support precompilation, includes "wx/wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20#pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
24#include "wx/wx.h"
25#endif
26
53b28675 27#include "wx/notebook.h"
1c005ff7
RR
28
29//----------------------------------------------------------------------
30// class definitions
31//----------------------------------------------------------------------
32
33class MyApp: public wxApp
34{
35 public:
36 bool OnInit(void);
37};
38
39class MyPanel: public wxPanel
40{
41 public:
42
43 MyPanel(wxFrame *frame, int x, int y, int w, int h);
44
45 void OnSize( wxSizeEvent& event );
46 void OnListBox( wxCommandEvent &event );
47 void OnListBoxButtons( wxCommandEvent &event );
48
49 wxListBox *m_listbox;
53010e52
RR
50 wxChoice *m_choice;
51 wxComboBox *m_combo;
1c005ff7
RR
52
53 wxTextCtrl *m_text;
53b28675 54 wxNotebook *m_notebook;
1c005ff7
RR
55
56 DECLARE_EVENT_TABLE()
57};
58
59class MyFrame: public wxFrame
60{
61 public:
62
63 MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h);
64
65 public:
66
67 void OnQuit(wxCommandEvent& event);
68 void OnAbout(wxCommandEvent& event);
69 bool OnClose(void) { return TRUE; }
70
71 DECLARE_EVENT_TABLE()
72};
73
74//----------------------------------------------------------------------
75// main()
76//----------------------------------------------------------------------
77
78IMPLEMENT_APP (MyApp)
79
80//----------------------------------------------------------------------
81// MyApp
82//----------------------------------------------------------------------
83
84const MINIMAL_QUIT = 100;
85const MINIMAL_TEXT = 101;
86const MINIMAL_ABOUT = 102;
87
88bool MyApp::OnInit(void)
89{
90 // Create the main frame window
91 MyFrame *frame = new MyFrame(NULL, "Controls wxWindows App", 50, 50, 500, 420 );
92
93 // Give it an icon
2049ba38 94#ifdef __WXMSW__
1c005ff7
RR
95 frame->SetIcon(wxIcon("mondrian"));
96#endif
97#ifdef __X__
98 frame->SetIcon(wxIcon("aiai.xbm"));
99#endif
100
101 wxMenu *file_menu = new wxMenu;
102
103 file_menu->Append(MINIMAL_ABOUT, "&About");
104 file_menu->Append(MINIMAL_QUIT, "E&xit");
105 wxMenuBar *menu_bar = new wxMenuBar;
106 menu_bar->Append(file_menu, "&File");
107 frame->SetMenuBar(menu_bar);
108
109 frame->Show(TRUE);
110
111 SetTopWindow(frame);
112
113 return TRUE;
114}
115
116//----------------------------------------------------------------------
117// MyPanel
118//----------------------------------------------------------------------
119
53b28675 120const ID_NOTEBOOK = 1000;
1c005ff7
RR
121
122const ID_LISTBOX = 130;
123const ID_LISTBOX_SEL_NUM = 131;
124const ID_LISTBOX_SEL_STR = 132;
125const ID_LISTBOX_CLEAR = 133;
126const ID_LISTBOX_APPEND = 134;
127
53010e52
RR
128const ID_CHOICE = 120;
129const ID_CHOICE_SEL_NUM = 121;
130const ID_CHOICE_SEL_STR = 122;
131const ID_CHOICE_CLEAR = 123;
132const ID_CHOICE_APPEND = 124;
133
134const ID_COMBO = 140;
135
219f895a
RR
136const ID_TEXT = 150;
137
1c005ff7
RR
138BEGIN_EVENT_TABLE(MyPanel, wxPanel)
139 EVT_SIZE ( MyPanel::OnSize)
140 EVT_LISTBOX (ID_LISTBOX, MyPanel::OnListBox)
141 EVT_BUTTON (ID_LISTBOX_SEL_NUM, MyPanel::OnListBoxButtons)
142 EVT_BUTTON (ID_LISTBOX_SEL_STR, MyPanel::OnListBoxButtons)
143 EVT_BUTTON (ID_LISTBOX_CLEAR, MyPanel::OnListBoxButtons)
144 EVT_BUTTON (ID_LISTBOX_APPEND, MyPanel::OnListBoxButtons)
145END_EVENT_TABLE()
146
147MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h ) :
148 wxPanel( frame, -1, wxPoint(x, y), wxSize(w, h) )
149{
150 m_text = new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(0,50), wxSize(100,50), wxTE_MULTILINE );
151
53b28675 152 m_notebook = new wxNotebook( this, ID_NOTEBOOK, wxPoint(0,0), wxSize(200,150) );
1c005ff7 153
53010e52 154 wxString choices[] =
1c005ff7
RR
155 {
156 "This",
157 "is",
158 "a",
53010e52
RR
159 "wonderfull example.",
160 "Or",
161 "what",
162 "do",
163 "you",
164 "think?"
1c005ff7
RR
165 };
166
4bf58c62 167 wxPanel *panel = new wxPanel(m_notebook);
53010e52
RR
168 m_listbox = new wxListBox( panel, ID_LISTBOX, wxPoint(10,10), wxSize(120,70), 9, choices );
169 (void)new wxButton( panel, ID_LISTBOX_SEL_NUM, "Select #2", wxPoint(180,30), wxSize(100,30) );
170 (void)new wxButton( panel, ID_LISTBOX_SEL_STR, "Select 'This'", wxPoint(300,30), wxSize(100,30) );
171 (void)new wxButton( panel, ID_LISTBOX_CLEAR, "Clear", wxPoint(180,80), wxSize(100,30) );
172 (void)new wxButton( panel, ID_LISTBOX_APPEND, "Append 'Hi!'", wxPoint(300,80), wxSize(100,30) );
4bf58c62 173 m_notebook->AddPage(panel, "wxList");
53010e52 174
4bf58c62 175 panel = new wxPanel(m_notebook);
53010e52 176 m_choice = new wxChoice( panel, ID_CHOICE, wxPoint(10,10), wxSize(120,-1), 9, choices );
4bf58c62 177 m_notebook->AddPage(panel, "wxChoice");
53010e52 178
4bf58c62 179 panel = new wxPanel(m_notebook);
53010e52 180 m_combo = new wxComboBox( panel, ID_COMBO, "This", wxPoint(10,10), wxSize(120,-1), 9, choices );
4bf58c62 181 m_notebook->AddPage(panel, "wxComboBox");
219f895a 182
33d0b396 183 wxTextCtrl *text = new wxTextCtrl( m_notebook, ID_TEXT, "Write text here.", wxPoint(10,10), wxSize(120,100), wxTE_MULTILINE );
219f895a 184 m_notebook->AddPage( text, "wxTextCtrl" );
1c005ff7
RR
185}
186
187void MyPanel::OnSize( wxSizeEvent& WXUNUSED(event) )
188{
189 int x = 0;
190 int y = 0;
191 GetClientSize( &x, &y );
192
53b28675 193 if (m_notebook) m_notebook->SetSize( 2, 2, x-4, y/2-4 );
1c005ff7
RR
194 if (m_text) m_text->SetSize( 2, y/2+2, x-4, y/2-4 );
195}
196
197void MyPanel::OnListBox( wxCommandEvent &event )
198{
199 m_text->WriteText( "ListBox Event:\n");
200 m_text->WriteText( "ListBox selection string is: " );
201 m_text->WriteText( event.GetString() );
202 m_text->WriteText( "\n" );
203}
204
205void MyPanel::OnListBoxButtons( wxCommandEvent &WXUNUSED(event) )
206{
447daba1
JS
207 if (m_notebook->GetPageCount() > 1)
208 m_notebook->DeletePage( 1 );
1c005ff7
RR
209}
210
211//----------------------------------------------------------------------
212// MyFrame
213//----------------------------------------------------------------------
214
215BEGIN_EVENT_TABLE(MyFrame, wxFrame)
216 EVT_MENU(MINIMAL_QUIT, MyFrame::OnQuit)
217 EVT_MENU(MINIMAL_ABOUT, MyFrame::OnAbout)
218END_EVENT_TABLE()
219
220MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h):
221 wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
222{
223 (void*) new MyPanel( this, 10, 10, 300, 100 );
224}
225
226void MyFrame::OnQuit (wxCommandEvent& WXUNUSED(event) )
227{
228 Close(TRUE);
229}
230
231void MyFrame::OnAbout( wxCommandEvent& WXUNUSED(event) )
232{
233 wxMessageDialog dialog(this, "This is a control sample", "About Controls", wxOK );
234 dialog.ShowModal();
235}
236
237