]>
Commit | Line | Data |
---|---|---|
c59e5089 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: render.cpp | |
be5a51fb | 3 | // Purpose: Render wxWidgets sample |
c59e5089 VZ |
4 | // Author: Vadim Zeitlin |
5 | // Modified by: | |
6 | // Created: 04.08.03 | |
7 | // RCS-ID: $Id$ | |
be5a51fb | 8 | // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org> |
c59e5089 VZ |
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 | #ifndef WX_PRECOMP | |
28 | #include "wx/app.h" | |
29 | #include "wx/frame.h" | |
9c743a7c VS |
30 | #include "wx/dc.h" |
31 | #include "wx/dcclient.h" | |
32 | #include "wx/panel.h" | |
33 | #include "wx/menu.h" | |
34 | #include "wx/textdlg.h" | |
35 | #include "wx/log.h" | |
36 | #include "wx/msgdlg.h" | |
c59e5089 VZ |
37 | #endif |
38 | ||
39 | #include "wx/apptrait.h" | |
40 | #include "wx/renderer.h" | |
41 | ||
42 | // ---------------------------------------------------------------------------- | |
43 | // resources | |
44 | // ---------------------------------------------------------------------------- | |
45 | ||
46 | // the application icon (under Windows and OS/2 it is in resources) | |
47 | #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__) | |
48 | #include "../sample.xpm" | |
49 | #endif | |
50 | ||
51 | // ---------------------------------------------------------------------------- | |
52 | // private classes | |
53 | // ---------------------------------------------------------------------------- | |
54 | ||
55 | // A renderer class draws the header buttons in a "special" way | |
56 | class MyRenderer : public wxDelegateRendererNative | |
57 | { | |
58 | public: | |
59 | MyRenderer() : wxDelegateRendererNative(wxRendererNative::GetDefault()) { } | |
60 | ||
61 | virtual void DrawHeaderButton(wxWindow * WXUNUSED(win), | |
62 | wxDC& dc, | |
63 | const wxRect& rect, | |
64 | int WXUNUSED(flags) = 0) | |
65 | { | |
66 | dc.SetBrush(*wxBLUE_BRUSH); | |
67 | dc.SetTextForeground(*wxWHITE); | |
68 | dc.DrawRoundedRectangle(rect, 5); | |
69 | dc.DrawLabel(_T("MyRenderer"), wxNullBitmap, rect, wxALIGN_CENTER); | |
70 | } | |
71 | }; | |
72 | ||
73 | // To use a different renderer from the very beginning we must override | |
74 | // wxAppTraits method creating the renderer (another, simpler, alternative is | |
75 | // to call wxRendererNative::Set() a.s.a.p. which should work in 99% of the | |
76 | // cases, but we show this here just for completeness) | |
77 | class MyTraits : public wxGUIAppTraits | |
78 | { | |
79 | virtual wxRendererNative *CreateRenderer() | |
80 | { | |
be5a51fb | 81 | // it will be deleted on program shutdown by wxWidgets itself |
c59e5089 VZ |
82 | return new MyRenderer; |
83 | } | |
84 | }; | |
85 | ||
86 | // Define a new application type, each program should derive a class from wxApp | |
87 | class MyApp : public wxApp | |
88 | { | |
89 | public: | |
90 | virtual bool OnInit(); | |
91 | ||
92 | // if we want MyTraits to be used we must override CreateTraits() | |
93 | virtual wxAppTraits *CreateTraits() { return new MyTraits; } | |
94 | }; | |
95 | ||
96 | // Define a new frame type: this is going to be our main frame | |
97 | class MyFrame : public wxFrame | |
98 | { | |
99 | public: | |
100 | // ctor(s) | |
101 | MyFrame(); | |
102 | virtual ~MyFrame(); | |
103 | ||
104 | // event handlers (these functions should _not_ be virtual) | |
105 | void OnLoad(wxCommandEvent& event); | |
106 | void OnUnload(wxCommandEvent& event); | |
107 | void OnQuit(wxCommandEvent& event); | |
108 | void OnAbout(wxCommandEvent& event); | |
109 | ||
110 | private: | |
111 | wxPanel *m_panel; | |
112 | ||
be5a51fb | 113 | // any class wishing to process wxWidgets events must use this macro |
c59e5089 VZ |
114 | DECLARE_EVENT_TABLE() |
115 | }; | |
116 | ||
117 | // a very simple class just to have something to draw on | |
118 | class MyPanel : public wxPanel | |
119 | { | |
120 | public: | |
121 | MyPanel(wxWindow *parent) : wxPanel(parent) { } | |
122 | ||
123 | void OnPaint(wxPaintEvent&) | |
124 | { | |
125 | wxPaintDC dc(this); | |
126 | ||
127 | dc.DrawText(_T("Below is the standard header button drawn"), 10, 10); | |
128 | dc.DrawText(_T("using the current renderer:"), 10, 40); | |
129 | ||
130 | wxRendererNative::Get().DrawHeaderButton(this, dc, | |
131 | wxRect(20, 70, 100, 60)); | |
132 | } | |
133 | ||
134 | DECLARE_EVENT_TABLE() | |
135 | }; | |
136 | ||
137 | BEGIN_EVENT_TABLE(MyPanel, wxPanel) | |
138 | EVT_PAINT(MyPanel::OnPaint) | |
139 | END_EVENT_TABLE() | |
140 | ||
141 | // ---------------------------------------------------------------------------- | |
142 | // constants | |
143 | // ---------------------------------------------------------------------------- | |
144 | ||
145 | // IDs for the controls and the menu commands | |
146 | enum | |
147 | { | |
148 | // our menu items | |
149 | Render_Load = 100, | |
150 | Render_Unload, | |
151 | ||
152 | // standard menu items | |
153 | Render_Quit = wxID_EXIT, | |
154 | ||
155 | // it is important for the id corresponding to the "About" command to have | |
156 | // this standard value as otherwise it won't be handled properly under Mac | |
157 | // (where it is special and put into the "Apple" menu) | |
158 | Render_About = wxID_ABOUT | |
159 | }; | |
160 | ||
161 | // ---------------------------------------------------------------------------- | |
be5a51fb | 162 | // event tables and other macros for wxWidgets |
c59e5089 VZ |
163 | // ---------------------------------------------------------------------------- |
164 | ||
be5a51fb | 165 | // the event tables connect the wxWidgets events with the functions (event |
c59e5089 VZ |
166 | // handlers) which process them. It can be also done at run-time, but for the |
167 | // simple menu events like this the static method is much simpler. | |
168 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
169 | EVT_MENU(Render_Load, MyFrame::OnLoad) | |
170 | EVT_MENU(Render_Unload,MyFrame::OnUnload) | |
171 | EVT_MENU(Render_Quit, MyFrame::OnQuit) | |
172 | ||
173 | EVT_MENU(Render_About, MyFrame::OnAbout) | |
174 | END_EVENT_TABLE() | |
175 | ||
be5a51fb | 176 | // Create a new application object: this macro will allow wxWidgets to create |
c59e5089 VZ |
177 | // the application object during program execution (it's better than using a |
178 | // static object for many reasons) and also implements the accessor function | |
179 | // wxGetApp() which will return the reference of the right type (i.e. MyApp and | |
180 | // not wxApp) | |
181 | IMPLEMENT_APP(MyApp) | |
182 | ||
183 | // ============================================================================ | |
184 | // implementation | |
185 | // ============================================================================ | |
186 | ||
187 | // ---------------------------------------------------------------------------- | |
188 | // the application class | |
189 | // ---------------------------------------------------------------------------- | |
190 | ||
191 | // 'Main program' equivalent: the program execution "starts" here | |
192 | bool MyApp::OnInit() | |
193 | { | |
194 | // create the main application window | |
195 | new MyFrame; | |
196 | ||
197 | return true; | |
198 | } | |
199 | ||
200 | // ---------------------------------------------------------------------------- | |
201 | // main frame | |
202 | // ---------------------------------------------------------------------------- | |
203 | ||
204 | // frame constructor | |
205 | MyFrame::MyFrame() | |
206 | : wxFrame(NULL, | |
207 | -1, | |
be5a51fb | 208 | _T("Render wxWidgets Sample"), |
c59e5089 VZ |
209 | wxPoint(50, 50), |
210 | wxSize(450, 340)) | |
211 | { | |
212 | // set the frame icon | |
213 | SetIcon(wxICON(sample)); | |
214 | ||
215 | #if wxUSE_MENUS | |
216 | // create a menu bar | |
217 | wxMenu *menuFile = new wxMenu; | |
218 | menuFile->Append(Render_Load, _T("&Load renderer...\tCtrl-L")); | |
219 | menuFile->Append(Render_Unload, _T("&Unload renderer\tCtrl-U")); | |
220 | menuFile->Append(Render_Quit, _T("E&xit\tCtrl-Q"), _T("Quit this program")); | |
221 | ||
222 | // the "About" item should be in the help menu | |
223 | wxMenu *helpMenu = new wxMenu; | |
224 | helpMenu->Append(Render_About, _T("&About...\tF1"), _T("Show about dialog")); | |
225 | ||
226 | // now append the freshly created menu to the menu bar... | |
227 | wxMenuBar *menuBar = new wxMenuBar(); | |
228 | menuBar->Append(menuFile, _T("&File")); | |
229 | menuBar->Append(helpMenu, _T("&Help")); | |
230 | ||
231 | // ... and attach this menu bar to the frame | |
232 | SetMenuBar(menuBar); | |
233 | #endif // wxUSE_MENUS | |
234 | ||
235 | m_panel = new MyPanel(this); | |
236 | ||
237 | #if wxUSE_STATUSBAR | |
238 | // create a status bar just for fun (by default with 1 pane only) | |
239 | CreateStatusBar(2); | |
be5a51fb | 240 | SetStatusText(_T("Welcome to wxWidgets!")); |
c59e5089 VZ |
241 | #endif // wxUSE_STATUSBAR |
242 | ||
243 | Show(); | |
244 | } | |
245 | ||
246 | MyFrame::~MyFrame() | |
247 | { | |
248 | delete wxRendererNative::Set(NULL); | |
249 | } | |
250 | ||
251 | ||
252 | // event handlers | |
253 | ||
254 | void MyFrame::OnLoad(wxCommandEvent& WXUNUSED(event)) | |
255 | { | |
256 | static wxString s_name = _T("renddll"); | |
257 | ||
258 | wxString name = wxGetTextFromUser | |
259 | ( | |
260 | _T("Name of the renderer to load:"), | |
be5a51fb | 261 | _T("Render wxWidgets Sample"), |
c59e5089 VZ |
262 | s_name, |
263 | this | |
264 | ); | |
265 | if ( name.empty() ) | |
266 | { | |
267 | // cancelled | |
268 | return; | |
269 | } | |
270 | ||
271 | s_name = name; | |
272 | ||
273 | wxRendererNative *renderer = wxRendererNative::Load(name); | |
274 | if ( !renderer ) | |
275 | { | |
276 | wxLogError(_T("Failed to load renderer \"%s\"."), name.c_str()); | |
277 | } | |
278 | else // loaded ok | |
279 | { | |
280 | delete wxRendererNative::Set(renderer); | |
281 | ||
282 | m_panel->Refresh(); | |
283 | ||
284 | wxLogStatus(this, _T("Successfully loaded the renderer \"%s\"."), | |
285 | name.c_str()); | |
286 | } | |
287 | } | |
288 | ||
289 | void MyFrame::OnUnload(wxCommandEvent& WXUNUSED(event)) | |
290 | { | |
291 | wxRendererNative *renderer = wxRendererNative::Set(NULL); | |
292 | if ( renderer ) | |
293 | { | |
294 | delete renderer; | |
295 | ||
296 | m_panel->Refresh(); | |
297 | ||
298 | wxLogStatus(this, _T("Unloaded the previously loaded renderer.")); | |
299 | } | |
300 | else | |
301 | { | |
302 | wxLogWarning(_T("No renderer to unload.")); | |
303 | } | |
304 | } | |
305 | ||
306 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
307 | { | |
308 | // true is to force the frame to close | |
309 | Close(true); | |
310 | } | |
311 | ||
312 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
313 | { | |
314 | wxMessageBox(_T("Render sample shows how to use custom renderers.\n") | |
315 | _T("\n") | |
749bfe9a | 316 | _T("(c) 2003 Vadim Zeitlin"), |
be5a51fb | 317 | _T("About Render wxWidgets Sample"), |
c59e5089 VZ |
318 | wxOK | wxICON_INFORMATION, this); |
319 | } | |
320 |