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