1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        minimal.cpp
3 // Purpose:     Minimal wxWidgets sample
4 // Author:      Julian Smart
5 // Modified by:
6 // Created:     04/01/98
7 // Copyright:   (c) Julian Smart
8 // Licence:     wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10 
11 // ============================================================================
12 // declarations
13 // ============================================================================
14 
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18 
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
21 
22 
23 // for all others, include the necessary headers (this file is usually all you
24 // need because it includes almost all "standard" wxWidgets headers)
25 #ifndef WX_PRECOMP
26     #include "wx/wx.h"
27 #endif
28 
29 // ----------------------------------------------------------------------------
30 // resources
31 // ----------------------------------------------------------------------------
32 
33 
34 // ----------------------------------------------------------------------------
35 // private classes
36 // ----------------------------------------------------------------------------
37 
38 // Define a new application type, each program should derive a class from wxApp
39 class MyApp : public wxApp
40 {
41 public:
42     // override base class virtuals
43     // ----------------------------
44 
45     // this one is called on application startup and is a good place for the app
46     // initialization (doing it here and not in the ctor allows to have an error
47     // return: if OnInit() returns false, the application terminates)
48     virtual bool OnInit();
49 };
50 
51 // Define a new frame type: this is going to be our main frame
52 class MyFrame : public wxFrame
53 {
54 public:
55     // ctor(s)
56     MyFrame(const wxString& title);
57 
58     // event handlers (these functions should _not_ be virtual)
59     void OnQuit(wxCommandEvent& event);
60     void OnAbout(wxCommandEvent& event);
61 
62 private:
63     // any class wishing to process wxWindows events must use this macro
64     wxDECLARE_EVENT_TABLE();
65 };
66 
67 // ----------------------------------------------------------------------------
68 // constants
69 // ----------------------------------------------------------------------------
70 
71 // IDs for the controls and the menu commands
72 enum
73 {
74     // menu items
75     Minimal_Quit = wxID_EXIT,
76 
77     // it is important for the id corresponding to the "About" command to have
78     // this standard value as otherwise it won't be handled properly under Mac
79     // (where it is special and put into the "Apple" menu)
80     Minimal_About = wxID_ABOUT
81 };
82 
83 // ----------------------------------------------------------------------------
84 // event tables and other macros for wxWindows
85 // ----------------------------------------------------------------------------
86 
87 // the event tables connect the wxWindows events with the functions (event
88 // handlers) which process them. It can be also done at run-time, but for the
89 // simple menu events like this the static method is much simpler.
90 wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
91     EVT_MENU(Minimal_Quit,  MyFrame::OnQuit)
92     EVT_MENU(Minimal_About, MyFrame::OnAbout)
93 wxEND_EVENT_TABLE()
94 
95 // Create a new application object: this macro will allow wxWidgets to create
96 // the application object during program execution (it's better than using a
97 // static object for many reasons) and also implements the accessor function
98 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
99 // not wxApp)
100 wxIMPLEMENT_APP(MyApp);
101 
102 // ============================================================================
103 // implementation
104 // ============================================================================
105 
106 // ----------------------------------------------------------------------------
107 // the application class
108 // ----------------------------------------------------------------------------
109 
110 // 'Main program' equivalent: the program execution "starts" here
OnInit()111 bool MyApp::OnInit()
112 {
113     // create the main application window
114     MyFrame *frame = new MyFrame(wxT("Minimal wxWidgets App"));
115 
116     // and show it (the frames, unlike simple controls, are not shown when
117     // created initially)
118     frame->Show(true);
119 
120     // success: wxApp::OnRun() will be called which will enter the main message
121     // loop and the application will run. If we returned false here, the
122     // application would exit immediately.
123     return true;
124 }
125 
126 // ----------------------------------------------------------------------------
127 // main frame
128 // ----------------------------------------------------------------------------
129 
130 // frame constructor
MyFrame(const wxString & title)131 MyFrame::MyFrame(const wxString& title)
132        : wxFrame(NULL, wxID_ANY, title)
133 {
134     // set the frame icon
135 
136 #if wxUSE_MENUS
137     // create a menu bar
138     wxMenu *menuFile = new wxMenu;
139 
140     // the "About" item should be in the help menu
141     wxMenu *helpMenu = new wxMenu;
142     helpMenu->Append(Minimal_About, wxT("&About\tF1"), wxT("Show about dialog"));
143 
144     menuFile->Append(Minimal_Quit, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
145 
146     // now append the freshly created menu to the menu bar...
147     wxMenuBar *menuBar = new wxMenuBar();
148     menuBar->Append(menuFile, wxT("&File"));
149     menuBar->Append(helpMenu, wxT("&Help"));
150 
151     // ... and attach this menu bar to the frame
152     SetMenuBar(menuBar);
153 #endif // wxUSE_MENUS
154 
155 #if wxUSE_STATUSBAR
156     // create a status bar just for fun (by default with 1 pane only)
157     CreateStatusBar(2);
158     SetStatusText(wxT("Welcome to wxWidgets!"));
159 #endif // wxUSE_STATUSBAR
160 }
161 
162 
163 // event handlers
164 
OnQuit(wxCommandEvent & WXUNUSED (event))165 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
166 {
167     // true is to force the frame to close
168     Close(true);
169 }
170 
OnAbout(wxCommandEvent & WXUNUSED (event))171 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
172 {
173     wxString msg;
174     msg.Printf( wxT("This is the About dialog of the minimal sample.\n")
175                 wxT("Welcome to %s"), wxVERSION_STRING);
176 
177     wxMessageBox(msg, wxT("About Minimal"), wxOK | wxICON_INFORMATION, this);
178 }
179