compilation fixes
[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 #ifdef __GNUG__
20 #pragma implementation "minimal.cpp"
21 #pragma interface "minimal.cpp"
22 #endif
23
24 // For compilers that support precompilation, includes "wx/wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 // for all others, include the necessary headers (this file is usually all you
32 // need because it includes almost all "standard" wxWindows headers
33 #ifndef WX_PRECOMP
34 #include "wx/wx.h"
35 #endif
36
37 // ----------------------------------------------------------------------------
38 // ressources
39 // ----------------------------------------------------------------------------
40 // the application icon
41 #if defined(__WXGTK__) || defined(__WXMOTIF__)
42 #include "mondrian.xpm"
43 #endif
44
45 // ----------------------------------------------------------------------------
46 // private classes
47 // ----------------------------------------------------------------------------
48
49 // Define a new application type, each program should derive a class from wxApp
50 class MyApp : public wxApp
51 {
52 public:
53 // override base class virtuals
54 // ----------------------------
55
56 // this one is called on application startup and is a good place for the app
57 // initialization (doing it here and not in the ctor allows to have an error
58 // return: if OnInit() returns false, the application terminates)
59 virtual bool OnInit();
60 };
61
62 // Define a new frame type: this is going to be our main frame
63 class MyFrame : public wxFrame
64 {
65 public:
66 // ctor(s)
67 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
68
69 // event handlers (these functions should _not_ be virtual)
70 void OnQuit(wxCommandEvent& event);
71 void OnAbout(wxCommandEvent& event);
72 void OnTest(wxCommandEvent& event);
73
74 void OnPaint(wxPaintEvent& event);
75
76 private:
77 // any class wishing to process wxWindows events must use this macro
78 DECLARE_EVENT_TABLE()
79 };
80
81 // ----------------------------------------------------------------------------
82 // constants
83 // ----------------------------------------------------------------------------
84
85 // IDs for the controls and the menu commands
86 enum
87 {
88 // menu items
89 Minimal_Quit = 1,
90 Minimal_About,
91 Minimal_Test,
92
93 // controls start here (the numbers are, of course, arbitrary)
94 Minimal_Text = 1000,
95 };
96
97 // ----------------------------------------------------------------------------
98 // event tables and other macros for wxWindows
99 // ----------------------------------------------------------------------------
100
101 // the event tables connect the wxWindows events with the functions (event
102 // handlers) which process them. It can be also done at run-time, but for the
103 // simple menu events like this the static method is much simpler.
104 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
105 EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
106 EVT_MENU(Minimal_About, MyFrame::OnAbout)
107
108 EVT_MENU(Minimal_Test, MyFrame::OnTest)
109
110 EVT_PAINT(MyFrame::OnPaint)
111 END_EVENT_TABLE()
112
113 // Create a new application object: this macro will allow wxWindows to create
114 // the application object during program execution (it's better than using a
115 // static object for many reasons) and also declares the accessor function
116 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
117 // not wxApp)
118 IMPLEMENT_APP(MyApp)
119
120 // ============================================================================
121 // implementation
122 // ============================================================================
123
124 // ----------------------------------------------------------------------------
125 // the application class
126 // ----------------------------------------------------------------------------
127
128 // `Main program' equivalent: the program execution "starts" here
129 bool MyApp::OnInit()
130 {
131 // Create the main application window
132 MyFrame *frame = new MyFrame("Minimal wxWindows App",
133 wxPoint(50, 50), wxSize(450, 340));
134
135 // Show it and tell the application that it's our main window
136 // @@@ what does it do exactly, in fact? is it necessary here?
137 frame->Show(TRUE);
138 SetTopWindow(frame);
139
140 // success: wxApp::OnRun() will be called which will enter the main message
141 // loop and the application will run. If we returned FALSE here, the
142 // application would exit immediately.
143 return TRUE;
144 }
145
146 // ----------------------------------------------------------------------------
147 // main frame
148 // ----------------------------------------------------------------------------
149
150 // frame constructor
151 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
152 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
153 {
154 // set the frame icon
155 SetIcon(wxICON(mondrian));
156
157 // create a menu bar
158 wxMenu *menuFile = new wxMenu("", wxMENU_TEAROFF);
159
160 menuFile->Append(Minimal_About, "&About...\tCtrl-A", "Show about dialog");
161 menuFile->Append(Minimal_Test, "&Test...\tCtrl-T", "Test");
162 menuFile->AppendSeparator();
163 menuFile->Append(Minimal_Quit, "E&xit\tAlt-X", "Quit this program");
164
165 // now append the freshly created menu to the menu bar...
166 wxMenuBar *menuBar = new wxMenuBar();
167 menuBar->Append(menuFile, "&File");
168
169 // ... and attach this menu bar to the frame
170 SetMenuBar(menuBar);
171
172 #if wxUSE_STATUSBAR
173 // create a status bar just for fun (by default with 1 pane only)
174 CreateStatusBar(2);
175 SetStatusText("Welcome to wxWindows!");
176 #endif // wxUSE_STATUSBAR
177 }
178
179
180 // event handlers
181
182 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
183 {
184 // TRUE is to force the frame to close
185 Close(TRUE);
186 }
187
188 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
189 {
190 wxString msg;
191 msg.Printf( _T("This is the about dialog of minimal sample.\n")
192 _T("Welcome to %s")
193 #ifdef wxBETA_NUMBER
194 _T(" (beta %d)!")
195 #endif // wxBETA_NUMBER
196 , wxVERSION_STRING
197 #ifdef wxBETA_NUMBER
198 , wxBETA_NUMBER
199 #endif // wxBETA_NUMBER
200 );
201
202 wxMessageBox(msg, "About Minimal", wxOK | wxICON_INFORMATION, this);
203 }
204
205 struct Foo
206 {
207 Foo(int n_) { n = n_; }
208
209 int n;
210 };
211
212 WX_DECLARE_LIST(Foo, FooList);
213
214 #include <wx/listimpl.cpp>
215
216 WX_DEFINE_LIST(FooList);
217
218 int FooSort(const Foo **item1, const Foo **item2)
219 {
220 return (*item2)->n - (*item1)->n;
221 }
222
223 void ShowList(const FooList& list)
224 {
225 wxString msg, str;
226 msg = "The list elements: (";
227 for ( FooList::Node *node = list.GetFirst(); node; node = node->GetNext() )
228 {
229 if ( !!str )
230 msg += ", ";
231 str.Printf("%d", node->GetData()->n);
232 msg += str;
233 }
234
235 msg += ')';
236
237 wxMessageBox(msg, "List contents", wxOK | wxICON_INFORMATION);
238 }
239
240 void MyFrame::OnTest(wxCommandEvent& event)
241 {
242 FooList list;
243 list.Append(new Foo(12));
244 list.Append(new Foo(3));
245 list.Append(new Foo(1));
246 list.Append(new Foo(7));
247 list.Append(new Foo(4));
248 ShowList(list);
249 list.Sort(FooSort);
250 ShowList(list);
251 }
252
253 void MyFrame::OnPaint(wxPaintEvent& event)
254 {
255 wxPaintDC dc(this);
256
257 wxMemoryDC dcMem;
258 wxSize size(GetClientSize());
259 dcMem.SelectObject(wxBitmap(size.x, size.y, -1));
260
261 dcMem.SetBackground(wxBrush(wxColour(0, 0, 255), wxSOLID));
262 dcMem.SetTextForeground(wxColour(0, 255, 0));
263 dcMem.SetTextBackground(wxColour(0, 0, 0));
264 dcMem.SetBackgroundMode(wxSOLID);
265 dcMem.Clear();
266 dcMem.DrawText("Hello, wxWindows!", 10, 10);
267
268 wxPoint ptOrig(0, 0);
269 dc.Blit(ptOrig, size, &dcMem, ptOrig);
270
271 dcMem.SelectObject(wxNullBitmap);
272 }