fix missing includessrc/generic/srchctlg.cpp
[wxWidgets.git] / samples / minimal / minimal.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: minimal.cpp
3 // Purpose: Minimal wxWidgets 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" wxWidgets 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 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"
41 #endif
42
43 //###include "dmalloc.h"
44 // ----------------------------------------------------------------------------
45 // private classes
46 // ----------------------------------------------------------------------------
47
48 // Define a new application type, each program should derive a class from wxApp
49 class MyApp : public wxApp
50 {
51 public:
52 // override base class virtuals
53 // ----------------------------
54
55 // this one is called on application startup and is a good place for the app
56 // initialization (doing it here and not in the ctor allows to have an error
57 // return: if OnInit() returns false, the application terminates)
58 virtual bool OnInit();
59 };
60
61 // Define a new frame type: this is going to be our main frame
62 class MyFrame : public wxFrame
63 {
64 public:
65 // ctor(s)
66 MyFrame(const wxString& title);
67
68 // event handlers (these functions should _not_ be virtual)
69 void OnQuit(wxCommandEvent& event);
70 void OnAbout(wxCommandEvent& event);
71
72 private:
73 // any class wishing to process wxWidgets events must use this macro
74 DECLARE_EVENT_TABLE()
75 };
76
77 // ----------------------------------------------------------------------------
78 // constants
79 // ----------------------------------------------------------------------------
80
81 // IDs for the controls and the menu commands
82 enum
83 {
84 // menu items
85 Minimal_Quit = wxID_EXIT,
86
87 // it is important for the id corresponding to the "About" command to have
88 // this standard value as otherwise it won't be handled properly under Mac
89 // (where it is special and put into the "Apple" menu)
90 Minimal_About = wxID_ABOUT
91 };
92
93 // ----------------------------------------------------------------------------
94 // event tables and other macros for wxWidgets
95 // ----------------------------------------------------------------------------
96
97 // the event tables connect the wxWidgets 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 wxWidgets to create
106 // the application object during program execution (it's better than using a
107 // static object for many reasons) and also implements 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 // call the base class initialization method, currently it only parses a
124 // few common command-line options but it could be do more in the future
125 if ( !wxApp::OnInit() )
126 return false;
127
128 // create the main application window
129 MyFrame *frame = new MyFrame(_T("Minimal wxWidgets App"));
130
131 // and show it (the frames, unlike simple controls, are not shown when
132 // created initially)
133 frame->Show(true);
134
135 // success: wxApp::OnRun() will be called which will enter the main message
136 // loop and the application will run. If we returned false here, the
137 // application would exit immediately.
138 return true;
139 }
140
141 // ----------------------------------------------------------------------------
142 // main frame
143 // ----------------------------------------------------------------------------
144
145 // frame constructor
146 MyFrame::MyFrame(const wxString& title)
147 : wxFrame(NULL, wxID_ANY, title)
148 {
149 // set the frame icon
150 SetIcon(wxICON(sample));
151
152 // char * c = new char [100] ;
153 // strcpy (c, "hello");
154 #if wxUSE_MENUS
155 // create a menu bar
156 wxMenu *fileMenu = new wxMenu;
157
158 // the "About" item should be in the help menu
159 wxMenu *helpMenu = new wxMenu;
160 helpMenu->Append(Minimal_About, "hello", _T("Show about dialog"));
161
162 fileMenu->Append(Minimal_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
163
164 // now append the freshly created menu to the menu bar...
165 wxMenuBar *menuBar = new wxMenuBar();
166 menuBar->Append(fileMenu, _T("&File"));
167 menuBar->Append(helpMenu, _T("&Help"));
168
169 // ... and attach this menu bar to the frame
170 SetMenuBar(menuBar);
171 #endif // wxUSE_MENUS
172
173 #if wxUSE_STATUSBAR
174 // create a status bar just for fun (by default with 1 pane only)
175 CreateStatusBar(2);
176 SetStatusText(_T("Welcome to wxWidgets!"));
177 #endif // wxUSE_STATUSBAR
178 }
179
180
181 // event handlers
182
183 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
184 {
185 // true is to force the frame to close
186 Close(true);
187 }
188
189 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
190 {
191 wxMessageBox(wxString::Format(
192 _T("Welcome to %s!\n")
193 _T("\n")
194 _T("This is the minimal wxWidgets sample\n")
195 _T("running under %s."),
196 wxVERSION_STRING,
197 wxGetOsDescription().c_str()
198 ),
199 _T("About wxWidgets minimal sample"),
200 wxOK | wxICON_INFORMATION,
201 this);
202 }