]> git.saurik.com Git - wxWidgets.git/blob - samples/minimal/minimal.cpp
don't try to refresh the menu bar before showing it, fixes crash on startup with...
[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 #ifdef __GNUG__
21 // #pragma implementation "minimal.cpp"
22 // #pragma interface "minimal.cpp"
23 #endif
24
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
27
28 #ifdef __BORLANDC__
29 #pragma hdrstop
30 #endif
31
32 // for all others, include the necessary headers (this file is usually all you
33 // need because it includes almost all "standard" wxWindows headers)
34 #ifndef WX_PRECOMP
35 #include "wx/wx.h"
36 #endif
37
38 // ----------------------------------------------------------------------------
39 // resources
40 // ----------------------------------------------------------------------------
41 // the application icon
42 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__)
43 #include "mondrian.xpm"
44 #endif
45
46 // ----------------------------------------------------------------------------
47 // private classes
48 // ----------------------------------------------------------------------------
49
50 // Define a new application type, each program should derive a class from wxApp
51 class MyApp : public wxApp
52 {
53 public:
54 // override base class virtuals
55 // ----------------------------
56
57 // this one is called on application startup and is a good place for the app
58 // initialization (doing it here and not in the ctor allows to have an error
59 // return: if OnInit() returns false, the application terminates)
60 virtual bool OnInit();
61 };
62
63 // Define a new frame type: this is going to be our main frame
64 class MyFrame : public wxFrame
65 {
66 public:
67 // ctor(s)
68 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
69
70 // event handlers (these functions should _not_ be virtual)
71 void OnQuit(wxCommandEvent& event);
72 void OnAbout(wxCommandEvent& event);
73
74 private:
75 // any class wishing to process wxWindows events must use this macro
76 DECLARE_EVENT_TABLE()
77 };
78
79 // ----------------------------------------------------------------------------
80 // constants
81 // ----------------------------------------------------------------------------
82
83 // IDs for the controls and the menu commands
84 enum
85 {
86 // menu items
87 Minimal_Quit = 1,
88 Minimal_About
89 };
90
91 // ----------------------------------------------------------------------------
92 // event tables and other macros for wxWindows
93 // ----------------------------------------------------------------------------
94
95 // the event tables connect the wxWindows events with the functions (event
96 // handlers) which process them. It can be also done at run-time, but for the
97 // simple menu events like this the static method is much simpler.
98 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
99 EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
100 EVT_MENU(Minimal_About, MyFrame::OnAbout)
101 END_EVENT_TABLE()
102
103 // Create a new application object: this macro will allow wxWindows to create
104 // the application object during program execution (it's better than using a
105 // static object for many reasons) and also declares the accessor function
106 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
107 // not wxApp)
108 IMPLEMENT_APP(MyApp)
109
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("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)
142 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
143 {
144 #ifdef __WXMAC__
145 // we need this in order to allow the about menu relocation, since ABOUT is
146 // not the default id of the about menu
147 wxApp::s_macAboutMenuItemId = Minimal_About;
148 #endif
149
150 // set the frame icon
151 SetIcon(wxICON(mondrian));
152
153 #if wxUSE_MENUS
154 // create a menu bar
155 wxMenu *menuFile = new wxMenu("", wxMENU_TEAROFF);
156
157 // the "About" item should be in the help menu
158 wxMenu *helpMenu = new wxMenu;
159 helpMenu->Append(Minimal_About, "&About...\tCtrl-A", "Show about dialog");
160
161 menuFile->Append(Minimal_Quit, "E&xit\tAlt-X", "Quit this program");
162
163 // now append the freshly created menu to the menu bar...
164 wxMenuBar *menuBar = new wxMenuBar();
165 menuBar->Append(menuFile, "&File");
166 menuBar->Append(helpMenu, "&Help");
167
168 // ... and attach this menu bar to the frame
169 SetMenuBar(menuBar);
170 #endif // wxUSE_MENUS
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"), wxVERSION_STRING);
193
194 wxMessageBox(msg, "About Minimal", wxOK | wxICON_INFORMATION, this);
195 }