]> git.saurik.com Git - wxWidgets.git/blob - samples/minimal/minimal.cpp
patch fix for a constant definition under OS/2 VA V4.0
[wxWidgets.git] / samples / minimal / minimal.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: minimal.cpp
3 // Purpose: Minimal wxWindows sample
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 // #pragma implementation "minimal.cpp"
22 // #pragma interface "minimal.cpp"
23 #endif
24
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
27
28 #ifdef __BORLANDC__
29 #pragma hdrstop
30 #endif
31
32 // for all others, include the necessary headers (this file is usually all you
33 // need because it includes almost all "standard" wxWindows headers)
34 #ifndef WX_PRECOMP
35 #include "wx/wx.h"
36 #endif
37
38 #include "wx/dynarray.h"
39
40 WX_DEFINE_ARRAY(wxWindow *, wxArrayLboxes);
41
42 // ----------------------------------------------------------------------------
43 // resources
44 // ----------------------------------------------------------------------------
45 // the application icon
46 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__)
47 #include "mondrian.xpm"
48 #endif
49
50 // ----------------------------------------------------------------------------
51 // private classes
52 // ----------------------------------------------------------------------------
53
54 // Define a new application type, each program should derive a class from wxApp
55 class MyApp : public wxApp
56 {
57 public:
58 // override base class virtuals
59 // ----------------------------
60
61 // this one is called on application startup and is a good place for the app
62 // initialization (doing it here and not in the ctor allows to have an error
63 // return: if OnInit() returns false, the application terminates)
64 virtual bool OnInit();
65 };
66
67 // Define a new frame type: this is going to be our main frame
68 class MyFrame : public wxFrame
69 {
70 public:
71 // ctor(s)
72 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
73
74 // event handlers (these functions should _not_ be virtual)
75 void OnQuit(wxCommandEvent& event);
76 void OnAbout(wxCommandEvent& event);
77 void OnCreateLbox(wxCommandEvent& event);
78 void OnDeleteLbox(wxCommandEvent& event);
79
80 private:
81 wxArrayLboxes m_lboxes;
82 wxCoord m_pos;
83
84 // any class wishing to process wxWindows events must use this macro
85 DECLARE_EVENT_TABLE()
86 };
87
88 // ----------------------------------------------------------------------------
89 // constants
90 // ----------------------------------------------------------------------------
91
92 // IDs for the controls and the menu commands
93 enum
94 {
95 // menu items
96 Minimal_Quit = 1,
97 Minimal_About,
98 Minimal_CreateLbox,
99 Minimal_DeleteLbox,
100 };
101
102 static const size_t NUM_LBOXES = 10;
103 static const wxCoord POS_STEP = 5;
104
105 // ----------------------------------------------------------------------------
106 // event tables and other macros for wxWindows
107 // ----------------------------------------------------------------------------
108
109 // the event tables connect the wxWindows events with the functions (event
110 // handlers) which process them. It can be also done at run-time, but for the
111 // simple menu events like this the static method is much simpler.
112 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
113 EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
114 EVT_MENU(Minimal_About, MyFrame::OnAbout)
115 EVT_MENU(Minimal_CreateLbox, MyFrame::OnCreateLbox)
116 EVT_MENU(Minimal_DeleteLbox, MyFrame::OnDeleteLbox)
117 END_EVENT_TABLE()
118
119 // Create a new application object: this macro will allow wxWindows to create
120 // the application object during program execution (it's better than using a
121 // static object for many reasons) and also declares the accessor function
122 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
123 // not wxApp)
124 IMPLEMENT_APP(MyApp)
125
126 // ============================================================================
127 // implementation
128 // ============================================================================
129
130 // ----------------------------------------------------------------------------
131 // the application class
132 // ----------------------------------------------------------------------------
133
134 // 'Main program' equivalent: the program execution "starts" here
135 bool MyApp::OnInit()
136 {
137 // create the main application window
138 MyFrame *frame = new MyFrame("Minimal wxWindows App",
139 wxPoint(50, 50), wxSize(450, 340));
140
141 // and show it (the frames, unlike simple controls, are not shown when
142 // created initially)
143 frame->Show(TRUE);
144
145 // success: wxApp::OnRun() will be called which will enter the main message
146 // loop and the application will run. If we returned FALSE here, the
147 // application would exit immediately.
148 return TRUE;
149 }
150
151 // ----------------------------------------------------------------------------
152 // main frame
153 // ----------------------------------------------------------------------------
154
155 // frame constructor
156 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
157 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
158 {
159 m_pos = 0;
160
161 #ifdef __WXMAC__
162 // we need this in order to allow the about menu relocation, since ABOUT is
163 // not the default id of the about menu
164 wxApp::s_macAboutMenuItemId = Minimal_About;
165 #endif
166
167 // set the frame icon
168 SetIcon(wxICON(mondrian));
169
170 #if wxUSE_MENUS
171 // create a menu bar
172 wxMenu *menuFile = new wxMenu("", wxMENU_TEAROFF);
173
174 // the "About" item should be in the help menu
175 wxMenu *helpMenu = new wxMenu;
176 helpMenu->Append(Minimal_About, "&About...\tCtrl-A", "Show about dialog");
177 helpMenu->Append(Minimal_CreateLbox, "&Create 10 listboxes\tCtrl-C");
178 helpMenu->Append(Minimal_DeleteLbox, "&Delete 10 listboxes\tCtrl-D");
179
180 menuFile->Append(Minimal_Quit, "E&xit\tAlt-X", "Quit this program");
181
182 // now append the freshly created menu to the menu bar...
183 wxMenuBar *menuBar = new wxMenuBar();
184 menuBar->Append(menuFile, "&File");
185 menuBar->Append(helpMenu, "&Help");
186
187 // ... and attach this menu bar to the frame
188 SetMenuBar(menuBar);
189 #endif // wxUSE_MENUS
190
191 #if wxUSE_STATUSBAR
192 // create a status bar just for fun (by default with 1 pane only)
193 CreateStatusBar(2);
194 SetStatusText("Welcome to wxWindows!");
195 #endif // wxUSE_STATUSBAR
196 }
197
198
199 // event handlers
200
201 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
202 {
203 // TRUE is to force the frame to close
204 Close(TRUE);
205 }
206
207 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
208 {
209 wxString msg;
210 msg.Printf( _T("This is the about dialog of minimal sample.\n")
211 _T("Welcome to %s"), wxVERSION_STRING);
212
213 wxMessageBox(msg, "About Minimal", wxOK | wxICON_INFORMATION, this);
214 }
215
216 void MyFrame::OnCreateLbox(wxCommandEvent& WXUNUSED(event))
217 {
218 for ( size_t n = 0; n < NUM_LBOXES; n++ )
219 {
220 m_lboxes.Add(new wxListBox(this, -1, wxPoint(m_pos, m_pos)));
221 m_pos += POS_STEP;
222 }
223 }
224
225 void MyFrame::OnDeleteLbox(wxCommandEvent& WXUNUSED(event))
226 {
227 size_t count = m_lboxes.GetCount();
228 if ( count < NUM_LBOXES )
229 return;
230
231 for ( size_t n = 0; n < NUM_LBOXES; n++ )
232 {
233 delete m_lboxes[--count];
234 m_lboxes.RemoveAt(count);
235
236 m_pos -= POS_STEP;
237 }
238 }
239