VZ: I have reformatted it to correspond to our style conventions (tab stop =
[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 and Markus Holzem
9 // Licence: wxWindows license
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 #ifdef __WXGTK__
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
73 private:
74 // any class wishing to process wxWindows events must use this macro
75 DECLARE_EVENT_TABLE()
76 };
77
78 // ----------------------------------------------------------------------------
79 // constants
80 // ----------------------------------------------------------------------------
81
82 // IDs for the controls and the menu commands
83 enum
84 {
85 // menu items
86 Minimal_Quit = 1,
87 Minimal_About,
88
89 // controls start here (the numbers are, of course, arbitrary)
90 Minimal_Text = 1000,
91 };
92
93 // ----------------------------------------------------------------------------
94 // event tables and other macros for wxWindows
95 // ----------------------------------------------------------------------------
96
97 // the event tables connect the wxWindows events with the functions (event
98 // handlers) which process them. It can be also done at run-time, but for the
99 // simple menu events like this the static method is much simpler.
100 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
101 EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
102 EVT_MENU(Minimal_About, MyFrame::OnAbout)
103 END_EVENT_TABLE()
104
105 // Create a new application object: this macro will allow wxWindows to create
106 // the application object during program execution (it's better than using a
107 // static object for many reasons) and also declares the accessor function
108 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
109 // not wxApp)
110 IMPLEMENT_APP(MyApp)
111
112 // ============================================================================
113 // implementation
114 // ============================================================================
115
116 // ----------------------------------------------------------------------------
117 // the application class
118 // ----------------------------------------------------------------------------
119
120 // `Main program' equivalent: the program execution "starts" here
121 bool MyApp::OnInit()
122 {
123 // Create the main application window
124 MyFrame *frame = new MyFrame("Minimal wxWindows App",
125 wxPoint(50, 50), wxSize(450, 340));
126
127 // Show it and tell the application that it's our main window
128 // @@@ what does it do exactly, in fact? is it necessary here?
129 frame->Show(TRUE);
130 SetTopWindow(frame);
131
132 // success: wxApp::OnRun() will be called which will enter the main message
133 // loop and the application will run. If we returned FALSE here, the
134 // application would exit immediately.
135 return TRUE;
136 }
137
138 // ----------------------------------------------------------------------------
139 // main frame
140 // ----------------------------------------------------------------------------
141
142 // frame constructor
143 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
144 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
145 {
146 // set the frame icon
147 SetIcon(wxICON(mondrian));
148
149 // create a menu bar
150 wxMenu *menuFile = new wxMenu;
151
152 menuFile->Append(Minimal_About, "&About...");
153 menuFile->AppendSeparator();
154 menuFile->Append(Minimal_Quit, "E&xit");
155
156 // now append the freshly created menu to the menu bar...
157 wxMenuBar *menuBar = new wxMenuBar;
158 menuBar->Append(menuFile, "&File");
159
160 // ... and attach this menu bar to the frame
161 SetMenuBar(menuBar);
162
163 // create a status bar just for fun (by default with 1 pane only)
164 CreateStatusBar();
165 SetStatusText("Welcome to wxWindows!");
166
167 // now create some controls
168
169 // a panel first - if there were several controls, it would allow us to
170 // navigate between them from the keyboard
171 wxPanel *panel = new wxPanel(this, -1, wxPoint(0, 0), wxSize(400, 200));
172
173 // and a static control whose parent is the panel
174 (void)new wxStaticText(panel, -1, "Hello, world!", wxPoint(10, 10));
175 }
176
177
178 // event handlers
179
180 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
181 {
182 // TRUE is to force the frame to close
183 Close(TRUE);
184 }
185
186 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
187 {
188 wxMessageBox("This is a minimal sample\nA second line in the message box",
189 "About Minimal", wxOK | wxICON_INFORMATION, this);
190 }