]> git.saurik.com Git - wxWidgets.git/blob - samples/minimal/minimal.cpp
Moved the cleanup code to an EVT_WINDOW_DESTROY handler.
[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 // 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" wxWindows 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)
38 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
39 #include "mondrian.xpm"
40 #endif
41
42 // ----------------------------------------------------------------------------
43 // private classes
44 // ----------------------------------------------------------------------------
45
46 // Define a new application type, each program should derive a class from wxApp
47 class MyApp : public wxApp
48 {
49 public:
50 // override base class virtuals
51 // ----------------------------
52
53 // this one is called on application startup and is a good place for the app
54 // initialization (doing it here and not in the ctor allows to have an error
55 // return: if OnInit() returns false, the application terminates)
56 virtual bool OnInit();
57 };
58
59 // Define a new frame type: this is going to be our main frame
60 class MyFrame : public wxFrame
61 {
62 public:
63 // ctor(s)
64 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size,
65 long style = wxDEFAULT_FRAME_STYLE);
66
67 // event handlers (these functions should _not_ be virtual)
68 void OnQuit(wxCommandEvent& event);
69 void OnAbout(wxCommandEvent& event);
70
71 private:
72 // any class wishing to process wxWindows events must use this macro
73 DECLARE_EVENT_TABLE()
74 };
75
76 // ----------------------------------------------------------------------------
77 // constants
78 // ----------------------------------------------------------------------------
79
80 // IDs for the controls and the menu commands
81 enum
82 {
83 // menu items
84 Minimal_Quit = 1,
85
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 };
91
92 // ----------------------------------------------------------------------------
93 // event tables and other macros for wxWindows
94 // ----------------------------------------------------------------------------
95
96 // the event tables connect the wxWindows events with the functions (event
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.
99 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
100 EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
101 EVT_MENU(Minimal_About, MyFrame::OnAbout)
102 END_EVENT_TABLE()
103
104 // Create a new application object: this macro will allow wxWindows to create
105 // the application object during program execution (it's better than using a
106 // static object for many reasons) and also declares the accessor function
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 // ============================================================================
114
115 // ----------------------------------------------------------------------------
116 // the application class
117 // ----------------------------------------------------------------------------
118
119 // 'Main program' equivalent: the program execution "starts" here
120 bool MyApp::OnInit()
121 {
122 // create the main application window
123 MyFrame *frame = new MyFrame(_T("Minimal wxWindows App"),
124 wxPoint(50, 50), wxSize(450, 340));
125
126 // and show it (the frames, unlike simple controls, are not shown when
127 // created initially)
128 frame->Show(TRUE);
129
130 // success: wxApp::OnRun() will be called which will enter the main message
131 // loop and the application will run. If we returned FALSE here, the
132 // application would exit immediately.
133 return TRUE;
134 }
135
136 // ----------------------------------------------------------------------------
137 // main frame
138 // ----------------------------------------------------------------------------
139
140 // frame constructor
141 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
142 : wxFrame(NULL, -1, title, pos, size, style)
143 {
144 // set the frame icon
145 SetIcon(wxICON(mondrian));
146
147 #if wxUSE_MENUS
148 // create a menu bar
149 wxMenu *menuFile = new wxMenu;
150
151 // the "About" item should be in the help menu
152 wxMenu *helpMenu = new wxMenu;
153 helpMenu->Append(Minimal_About, _T("&About...\tF1"), _T("Show about dialog"));
154
155 menuFile->Append(Minimal_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
156
157 // now append the freshly created menu to the menu bar...
158 wxMenuBar *menuBar = new wxMenuBar();
159 menuBar->Append(menuFile, _T("&File"));
160 menuBar->Append(helpMenu, _T("&Help"));
161
162 // ... and attach this menu bar to the frame
163 SetMenuBar(menuBar);
164 #endif // wxUSE_MENUS
165
166 #if wxUSE_STATUSBAR
167 // create a status bar just for fun (by default with 1 pane only)
168 CreateStatusBar(2);
169 SetStatusText(_T("Welcome to wxWindows!"));
170 #endif // wxUSE_STATUSBAR
171 }
172
173
174 // event handlers
175
176 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
177 {
178 // TRUE is to force the frame to close
179 Close(TRUE);
180 }
181
182 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
183 {
184 wxString msg;
185 msg.Printf( _T("This is the About dialog of the minimal sample.\n")
186 _T("Welcome to %s"), wxVERSION_STRING);
187
188 wxMessageBox(msg, _T("About Minimal"), wxOK | wxICON_INFORMATION, this);
189 }