]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: minimal.cpp | |
be5a51fb | 3 | // Purpose: Minimal wxWidgets sample |
c801d85f KB |
4 | // Author: Julian Smart |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
e2a6f233 JS |
8 | // Copyright: (c) Julian Smart |
9 | // Licence: wxWindows licence | |
c801d85f KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
9fdf3c38 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
0731c594 | 19 | |
c801d85f KB |
20 | // For compilers that support precompilation, includes "wx/wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
9fdf3c38 | 24 | #pragma hdrstop |
c801d85f KB |
25 | #endif |
26 | ||
9fdf3c38 | 27 | // for all others, include the necessary headers (this file is usually all you |
be5a51fb | 28 | // need because it includes almost all "standard" wxWidgets headers) |
c801d85f | 29 | #ifndef WX_PRECOMP |
9fdf3c38 | 30 | #include "wx/wx.h" |
c801d85f KB |
31 | #endif |
32 | ||
9fdf3c38 | 33 | // ---------------------------------------------------------------------------- |
f6bcfd97 | 34 | // resources |
9fdf3c38 | 35 | // ---------------------------------------------------------------------------- |
bcbf2881 | 36 | |
be708d3f VZ |
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" | |
d355d3fe RR |
41 | #endif |
42 | ||
9fdf3c38 VZ |
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(); | |
c801d85f KB |
58 | }; |
59 | ||
9fdf3c38 VZ |
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) | |
0bff3f69 | 65 | MyFrame(const wxString& title); |
9fdf3c38 VZ |
66 | |
67 | // event handlers (these functions should _not_ be virtual) | |
c801d85f KB |
68 | void OnQuit(wxCommandEvent& event); |
69 | void OnAbout(wxCommandEvent& event); | |
c50f1fb9 | 70 | |
9fdf3c38 | 71 | private: |
be5a51fb | 72 | // any class wishing to process wxWidgets events must use this macro |
9fdf3c38 | 73 | DECLARE_EVENT_TABLE() |
c801d85f KB |
74 | }; |
75 | ||
9fdf3c38 VZ |
76 | // ---------------------------------------------------------------------------- |
77 | // constants | |
78 | // ---------------------------------------------------------------------------- | |
c801d85f | 79 | |
9fdf3c38 VZ |
80 | // IDs for the controls and the menu commands |
81 | enum | |
82 | { | |
83 | // menu items | |
1896f99c | 84 | Minimal_Quit = wxID_EXIT, |
9fdf3c38 | 85 | |
bcbf2881 VZ |
86 | // it is important for the id corresponding to the "About" command to have |
87 | // this standard value as otherwise it won't be handled properly under Mac | |
88 | // (where it is special and put into the "Apple" menu) | |
89 | Minimal_About = wxID_ABOUT | |
90 | }; | |
82c9f85c | 91 | |
9fdf3c38 | 92 | // ---------------------------------------------------------------------------- |
be5a51fb | 93 | // event tables and other macros for wxWidgets |
9fdf3c38 VZ |
94 | // ---------------------------------------------------------------------------- |
95 | ||
be5a51fb | 96 | // the event tables connect the wxWidgets events with the functions (event |
9fdf3c38 VZ |
97 | // handlers) which process them. It can be also done at run-time, but for the |
98 | // simple menu events like this the static method is much simpler. | |
c801d85f | 99 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
9fdf3c38 VZ |
100 | EVT_MENU(Minimal_Quit, MyFrame::OnQuit) |
101 | EVT_MENU(Minimal_About, MyFrame::OnAbout) | |
c801d85f KB |
102 | END_EVENT_TABLE() |
103 | ||
be5a51fb | 104 | // Create a new application object: this macro will allow wxWidgets to create |
9fdf3c38 | 105 | // the application object during program execution (it's better than using a |
dab73021 | 106 | // static object for many reasons) and also implements the accessor function |
9fdf3c38 VZ |
107 | // wxGetApp() which will return the reference of the right type (i.e. MyApp and |
108 | // not wxApp) | |
109 | IMPLEMENT_APP(MyApp) | |
110 | ||
111 | // ============================================================================ | |
112 | // implementation | |
113 | // ============================================================================ | |
c801d85f | 114 | |
9fdf3c38 VZ |
115 | // ---------------------------------------------------------------------------- |
116 | // the application class | |
117 | // ---------------------------------------------------------------------------- | |
118 | ||
f6bcfd97 | 119 | // 'Main program' equivalent: the program execution "starts" here |
9fdf3c38 VZ |
120 | bool MyApp::OnInit() |
121 | { | |
01ca9e8e | 122 | // create the main application window |
be5a51fb | 123 | MyFrame *frame = new MyFrame(_T("Minimal wxWidgets App")); |
9fdf3c38 | 124 | |
01ca9e8e VZ |
125 | // and show it (the frames, unlike simple controls, are not shown when |
126 | // created initially) | |
5c0ccabf | 127 | frame->Show(true); |
9fdf3c38 VZ |
128 | |
129 | // success: wxApp::OnRun() will be called which will enter the main message | |
5c0ccabf | 130 | // loop and the application will run. If we returned false here, the |
9fdf3c38 | 131 | // application would exit immediately. |
5c0ccabf | 132 | return true; |
9fdf3c38 VZ |
133 | } |
134 | ||
135 | // ---------------------------------------------------------------------------- | |
136 | // main frame | |
137 | // ---------------------------------------------------------------------------- | |
138 | ||
139 | // frame constructor | |
0bff3f69 | 140 | MyFrame::MyFrame(const wxString& title) |
5c0ccabf | 141 | : wxFrame(NULL, wxID_ANY, title) |
c801d85f | 142 | { |
9fdf3c38 | 143 | // set the frame icon |
be708d3f | 144 | SetIcon(wxICON(sample)); |
c801d85f | 145 | |
3379ed37 | 146 | #if wxUSE_MENUS |
d6d26e04 | 147 | // create a menu bar |
2ef60269 | 148 | wxMenu *fileMenu = new wxMenu; |
c801d85f | 149 | |
d6d26e04 RR |
150 | // the "About" item should be in the help menu |
151 | wxMenu *helpMenu = new wxMenu; | |
bcbf2881 | 152 | helpMenu->Append(Minimal_About, _T("&About...\tF1"), _T("Show about dialog")); |
c801d85f | 153 | |
2ef60269 | 154 | fileMenu->Append(Minimal_Quit, _T("E&xit\tAlt-X"), _T("Quit this program")); |
01ca9e8e | 155 | |
d6d26e04 RR |
156 | // now append the freshly created menu to the menu bar... |
157 | wxMenuBar *menuBar = new wxMenuBar(); | |
2ef60269 | 158 | menuBar->Append(fileMenu, _T("&File")); |
bcbf2881 | 159 | menuBar->Append(helpMenu, _T("&Help")); |
c801d85f | 160 | |
9fdf3c38 | 161 | // ... and attach this menu bar to the frame |
d6d26e04 | 162 | SetMenuBar(menuBar); |
3379ed37 | 163 | #endif // wxUSE_MENUS |
c801d85f | 164 | |
6503f634 | 165 | #if wxUSE_STATUSBAR |
9fdf3c38 | 166 | // create a status bar just for fun (by default with 1 pane only) |
47d67540 | 167 | CreateStatusBar(2); |
be5a51fb | 168 | SetStatusText(_T("Welcome to wxWidgets!")); |
c50f1fb9 | 169 | #endif // wxUSE_STATUSBAR |
c801d85f KB |
170 | } |
171 | ||
c801d85f | 172 | |
9fdf3c38 VZ |
173 | // event handlers |
174 | ||
175 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
c801d85f | 176 | { |
5c0ccabf DS |
177 | // true is to force the frame to close |
178 | Close(true); | |
c801d85f KB |
179 | } |
180 | ||
9fdf3c38 | 181 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
c801d85f | 182 | { |
6e86d784 VZ |
183 | wxMessageBox(wxString::Format( |
184 | _T("Welcome to %s!\n") | |
185 | _T("\n") | |
186 | _T("This is the minimal wxWidgets sample\n") | |
187 | _T("running under %s."), | |
188 | wxVERSION_STRING, | |
189 | wxGetOsDescription().c_str() | |
190 | ), | |
191 | _T("About wxWidgets minimal sample"), | |
192 | wxOK | wxICON_INFORMATION, | |
193 | this); | |
82c9f85c | 194 | } |