]> git.saurik.com Git - wxWidgets.git/blob - samples/minimal/minimal.cpp
ffad284b3b23f0b40cd74bcbdf3640971d45506e
[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 // ----------------------------------------------------------------------------
44 // private classes
45 // ----------------------------------------------------------------------------
46
47 // Define a new application type, each program should derive a class from wxApp
48 class MyApp : public wxApp
49 {
50 public:
51 // override base class virtuals
52 // ----------------------------
53
54 // this one is called on application startup and is a good place for the app
55 // initialization (doing it here and not in the ctor allows to have an error
56 // return: if OnInit() returns false, the application terminates)
57 virtual bool OnInit();
58 };
59
60 // Define a new frame type: this is going to be our main frame
61 class MyFrame : public wxFrame
62 {
63 public:
64 // ctor(s)
65 MyFrame(const wxString& title);
66
67 // event handlers (these functions should _not_ be virtual)
68 void OnQuit(wxCommandEvent& event);
69 void OnAbout(wxCommandEvent& event);
70
71 void OnPaint(wxPaintEvent&)
72 {
73 wxPaintDC dc(this);
74
75 wxBitmap bmp(100, 100, 1);
76 {
77 wxMemoryDC memdc(bmp);
78 memdc.SetBackground(*wxWHITE);
79 memdc.SetTextForeground(*wxBLACK);
80 memdc.Clear();
81 memdc.DrawText(_T("Hello wx!"), 10, 10);
82 }
83
84 wxBitmap bmp2 = bmp;
85 {
86 wxMemoryDC memdc(bmp2);
87 memdc.SetBackground(*wxWHITE);
88 memdc.SetTextForeground(*wxRED);
89 memdc.Clear();
90 memdc.DrawText(_T("Goodbye!"), 10, 10);
91 }
92
93 dc.DrawBitmap(bmp, 10, 10);
94 dc.DrawBitmap(bmp2, 120, 10);
95 }
96
97 private:
98 // any class wishing to process wxWidgets events must use this macro
99 DECLARE_EVENT_TABLE()
100 };
101
102 // ----------------------------------------------------------------------------
103 // constants
104 // ----------------------------------------------------------------------------
105
106 // IDs for the controls and the menu commands
107 enum
108 {
109 // menu items
110 Minimal_Quit = wxID_EXIT,
111
112 // it is important for the id corresponding to the "About" command to have
113 // this standard value as otherwise it won't be handled properly under Mac
114 // (where it is special and put into the "Apple" menu)
115 Minimal_About = wxID_ABOUT
116 };
117
118 // ----------------------------------------------------------------------------
119 // event tables and other macros for wxWidgets
120 // ----------------------------------------------------------------------------
121
122 // the event tables connect the wxWidgets events with the functions (event
123 // handlers) which process them. It can be also done at run-time, but for the
124 // simple menu events like this the static method is much simpler.
125 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
126 EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
127 EVT_MENU(Minimal_About, MyFrame::OnAbout)
128 END_EVENT_TABLE()
129
130 // Create a new application object: this macro will allow wxWidgets to create
131 // the application object during program execution (it's better than using a
132 // static object for many reasons) and also implements the accessor function
133 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
134 // not wxApp)
135 IMPLEMENT_APP(MyApp)
136
137 // ============================================================================
138 // implementation
139 // ============================================================================
140
141 // ----------------------------------------------------------------------------
142 // the application class
143 // ----------------------------------------------------------------------------
144
145 // 'Main program' equivalent: the program execution "starts" here
146 bool MyApp::OnInit()
147 {
148 // call the base class initialization method, currently it only parses a
149 // few common command-line options but it could be do more in the future
150 if ( !wxApp::OnInit() )
151 return false;
152
153 // create the main application window
154 MyFrame *frame = new MyFrame(_T("Minimal wxWidgets App"));
155
156 // and show it (the frames, unlike simple controls, are not shown when
157 // created initially)
158 frame->Show(true);
159
160 // success: wxApp::OnRun() will be called which will enter the main message
161 // loop and the application will run. If we returned false here, the
162 // application would exit immediately.
163 return true;
164 }
165
166 // ----------------------------------------------------------------------------
167 // main frame
168 // ----------------------------------------------------------------------------
169
170 // frame constructor
171 MyFrame::MyFrame(const wxString& title)
172 : wxFrame(NULL, wxID_ANY, title)
173 {
174 Connect(wxEVT_PAINT, wxPaintEventHandler(MyFrame::OnPaint));
175 // set the frame icon
176 SetIcon(wxICON(sample));
177
178 #if wxUSE_MENUS
179 // create a menu bar
180 wxMenu *fileMenu = new wxMenu;
181
182 // the "About" item should be in the help menu
183 wxMenu *helpMenu = new wxMenu;
184 helpMenu->Append(Minimal_About, _T("&About...\tF1"), _T("Show about dialog"));
185
186 fileMenu->Append(Minimal_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
187
188 // now append the freshly created menu to the menu bar...
189 wxMenuBar *menuBar = new wxMenuBar();
190 menuBar->Append(fileMenu, _T("&File"));
191 menuBar->Append(helpMenu, _T("&Help"));
192
193 // ... and attach this menu bar to the frame
194 SetMenuBar(menuBar);
195 #endif // wxUSE_MENUS
196
197 wxTextCtrl *m_textctrl = new wxTextCtrl(this, -1, _T(""), wxPoint(100,
198 100), wxSize(100, 100), wxTE_MULTILINE);
199 wxTextAttr t_style ( *wxRED );
200 m_textctrl->SetDefaultStyle(t_style);
201 m_textctrl->Clear();
202 m_textctrl->WriteText(_T("Must be red"));
203
204 #if wxUSE_STATUSBAR
205 // create a status bar just for fun (by default with 1 pane only)
206 CreateStatusBar(2);
207 SetStatusText(_T("Welcome to wxWidgets!"));
208 #endif // wxUSE_STATUSBAR
209 }
210
211
212 // event handlers
213
214 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
215 {
216 // true is to force the frame to close
217 Close(true);
218 }
219
220 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
221 {
222 wxMessageBox(wxString::Format(
223 _T("Welcome to %s!\n")
224 _T("\n")
225 _T("This is the minimal wxWidgets sample\n")
226 _T("running under %s."),
227 wxVERSION_STRING,
228 wxGetOsDescription().c_str()
229 ),
230 _T("About wxWidgets minimal sample"),
231 wxOK | wxICON_INFORMATION,
232 this);
233 }