]> git.saurik.com Git - wxWidgets.git/blob - samples/minimal/minimal.cpp
d21c2524f3efb8b6532ea19c5f9a8422716909ef
[wxWidgets.git] / samples / minimal / minimal.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: minimal.cpp
3 // Purpose: Minimal wxWidgets 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 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWidgets headers)
29 #ifndef WX_PRECOMP
30 #include "wx/wx.h"
31 #endif
32
33 // ----------------------------------------------------------------------------
34 // resources
35 // ----------------------------------------------------------------------------
36
37 // the application icon (under Windows and OS/2 it is in resources and even
38 // though we could still include the XPM here it would be unused)
39 #if !defined(__WXMSW__) && !defined(__WXPM__)
40 #include "../sample.xpm"
41 #endif
42
43 // ----------------------------------------------------------------------------
44 // private classes
45 // ----------------------------------------------------------------------------
46
47 // Define a new application type, each program should derive a class from wxApp
48 class MyApp : public wxApp
49 {
50 public:
51 // override base class virtuals
52 // ----------------------------
53
54 // this one is called on application startup and is a good place for the app
55 // initialization (doing it here and not in the ctor allows to have an error
56 // return: if OnInit() returns false, the application terminates)
57 virtual bool OnInit();
58 };
59
60 // Define a new frame type: this is going to be our main frame
61 class MyFrame : public wxFrame
62 {
63 public:
64 // ctor(s)
65 MyFrame(const wxString& title);
66
67 // event handlers (these functions should _not_ be virtual)
68 void OnQuit(wxCommandEvent& event);
69 void OnAbout(wxCommandEvent& event);
70
71 void OnPaint(wxPaintEvent&)
72 {
73 wxPaintDC dc(this);
74
75 wxFont font = GetFont();
76
77 for ( int size = 10; size < 20; size++ )
78 {
79 font.SetPixelSize(wxSize(0, size));
80 dc.SetFont(font);
81 dc.DrawText(wxString::Format("%dpx The quick brown fox jumps over the lazy dog.", size),
82 10, 10 + (size - 10)*20);
83 }
84 }
85
86
87 private:
88 // any class wishing to process wxWidgets events must use this macro
89 DECLARE_EVENT_TABLE()
90 };
91
92 // ----------------------------------------------------------------------------
93 // constants
94 // ----------------------------------------------------------------------------
95
96 // IDs for the controls and the menu commands
97 enum
98 {
99 // menu items
100 Minimal_Quit = wxID_EXIT,
101
102 // it is important for the id corresponding to the "About" command to have
103 // this standard value as otherwise it won't be handled properly under Mac
104 // (where it is special and put into the "Apple" menu)
105 Minimal_About = wxID_ABOUT
106 };
107
108 // ----------------------------------------------------------------------------
109 // event tables and other macros for wxWidgets
110 // ----------------------------------------------------------------------------
111
112 // the event tables connect the wxWidgets events with the functions (event
113 // handlers) which process them. It can be also done at run-time, but for the
114 // simple menu events like this the static method is much simpler.
115 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
116 EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
117 EVT_MENU(Minimal_About, MyFrame::OnAbout)
118 EVT_PAINT(MyFrame::OnPaint)
119 END_EVENT_TABLE()
120
121 // Create a new application object: this macro will allow wxWidgets to create
122 // the application object during program execution (it's better than using a
123 // static object for many reasons) and also implements the accessor function
124 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
125 // not wxApp)
126 IMPLEMENT_APP(MyApp)
127
128 // ============================================================================
129 // implementation
130 // ============================================================================
131
132 // ----------------------------------------------------------------------------
133 // the application class
134 // ----------------------------------------------------------------------------
135
136 // 'Main program' equivalent: the program execution "starts" here
137 bool MyApp::OnInit()
138 {
139 // call the base class initialization method, currently it only parses a
140 // few common command-line options but it could be do more in the future
141 if ( !wxApp::OnInit() )
142 return false;
143
144 // create the main application window
145 MyFrame *frame = new MyFrame("Minimal wxWidgets App");
146
147 // and show it (the frames, unlike simple controls, are not shown when
148 // created initially)
149 frame->Show(true);
150
151 // success: wxApp::OnRun() will be called which will enter the main message
152 // loop and the application will run. If we returned false here, the
153 // application would exit immediately.
154 return true;
155 }
156
157 // ----------------------------------------------------------------------------
158 // main frame
159 // ----------------------------------------------------------------------------
160
161 // frame constructor
162 MyFrame::MyFrame(const wxString& title)
163 : wxFrame(NULL, wxID_ANY, title)
164 {
165 // set the frame icon
166 SetIcon(wxICON(sample));
167
168 #if wxUSE_MENUS
169 // create a menu bar
170 wxMenu *fileMenu = new wxMenu;
171
172 // the "About" item should be in the help menu
173 wxMenu *helpMenu = new wxMenu;
174 helpMenu->Append(Minimal_About, "&About...\tF1", "Show about dialog");
175
176 fileMenu->Append(Minimal_Quit, "E&xit\tAlt-X", "Quit this program");
177
178 // now append the freshly created menu to the menu bar...
179 wxMenuBar *menuBar = new wxMenuBar();
180 menuBar->Append(fileMenu, "&File");
181 menuBar->Append(helpMenu, "&Help");
182
183 // ... and attach this menu bar to the frame
184 SetMenuBar(menuBar);
185 #endif // wxUSE_MENUS
186
187 #if wxUSE_STATUSBAR
188 // create a status bar just for fun (by default with 1 pane only)
189 CreateStatusBar(2);
190 SetStatusText("Welcome to wxWidgets!");
191 #endif // wxUSE_STATUSBAR
192 }
193
194
195 // event handlers
196
197 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
198 {
199 // true is to force the frame to close
200 Close(true);
201 }
202
203 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
204 {
205 wxMessageBox(wxString::Format
206 (
207 "Welcome to %s!\n"
208 "\n"
209 "This is the minimal wxWidgets sample\n"
210 "running under %s.",
211 wxVERSION_STRING,
212 wxGetOsDescription()
213 ),
214 "About wxWidgets minimal sample",
215 wxOK | wxICON_INFORMATION,
216 this);
217 }