]>
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 | ||
adf07e34 | 62 | virtual int DrawHeaderButton(wxWindow *WXUNUSED(win), |
c59e5089 VZ |
63 | wxDC& dc, |
64 | const wxRect& rect, | |
1ca21e6d WS |
65 | int WXUNUSED(flags) = 0, |
66 | wxHeaderSortIconType WXUNUSED(sortArrow) = wxHDR_SORT_ICON_NONE, | |
67 | wxHeaderButtonParams* WXUNUSED(params) = NULL) | |
c59e5089 | 68 | { |
34456d33 VZ |
69 | wxDCBrushChanger setBrush(dc, *wxBLUE_BRUSH); |
70 | wxDCTextColourChanger setFgCol(dc, *wxWHITE); | |
c59e5089 | 71 | dc.DrawRoundedRectangle(rect, 5); |
9a83f860 | 72 | dc.DrawLabel(wxT("MyRenderer"), wxNullBitmap, rect, wxALIGN_CENTER); |
adf07e34 | 73 | return rect.width; |
c59e5089 VZ |
74 | } |
75 | }; | |
76 | ||
77 | // To use a different renderer from the very beginning we must override | |
78 | // wxAppTraits method creating the renderer (another, simpler, alternative is | |
79 | // to call wxRendererNative::Set() a.s.a.p. which should work in 99% of the | |
80 | // cases, but we show this here just for completeness) | |
81 | class MyTraits : public wxGUIAppTraits | |
82 | { | |
83 | virtual wxRendererNative *CreateRenderer() | |
84 | { | |
be5a51fb | 85 | // it will be deleted on program shutdown by wxWidgets itself |
c59e5089 VZ |
86 | return new MyRenderer; |
87 | } | |
88 | }; | |
89 | ||
90 | // Define a new application type, each program should derive a class from wxApp | |
91 | class MyApp : public wxApp | |
92 | { | |
93 | public: | |
94 | virtual bool OnInit(); | |
95 | ||
96 | // if we want MyTraits to be used we must override CreateTraits() | |
97 | virtual wxAppTraits *CreateTraits() { return new MyTraits; } | |
98 | }; | |
99 | ||
100 | // Define a new frame type: this is going to be our main frame | |
101 | class MyFrame : public wxFrame | |
102 | { | |
103 | public: | |
104 | // ctor(s) | |
105 | MyFrame(); | |
106 | virtual ~MyFrame(); | |
107 | ||
8c9d1533 | 108 | private: |
c59e5089 | 109 | // event handlers (these functions should _not_ be virtual) |
8c9d1533 VZ |
110 | void OnDrawDisabled(wxCommandEvent& event) |
111 | { OnToggleDrawFlag(event, wxCONTROL_DISABLED); } | |
112 | void OnDrawFocused(wxCommandEvent& event) | |
113 | { OnToggleDrawFlag(event, wxCONTROL_FOCUSED); } | |
114 | void OnDrawPressed(wxCommandEvent& event) | |
115 | { OnToggleDrawFlag(event, wxCONTROL_PRESSED); } | |
116 | void OnDrawChecked(wxCommandEvent& event) | |
117 | { OnToggleDrawFlag(event, wxCONTROL_CHECKED); } | |
118 | void OnDrawHot(wxCommandEvent& event) | |
119 | { OnToggleDrawFlag(event, wxCONTROL_CURRENT); } | |
120 | ||
ba46d9ea | 121 | #if wxUSE_DYNLIB_CLASS |
c59e5089 VZ |
122 | void OnLoad(wxCommandEvent& event); |
123 | void OnUnload(wxCommandEvent& event); | |
ba46d9ea | 124 | #endif // wxUSE_DYNLIB_CLASS |
c59e5089 VZ |
125 | void OnQuit(wxCommandEvent& event); |
126 | void OnAbout(wxCommandEvent& event); | |
127 | ||
8c9d1533 VZ |
128 | void OnToggleDrawFlag(wxCommandEvent& event, int flag); |
129 | ||
130 | class MyPanel *m_panel; | |
c59e5089 | 131 | |
be5a51fb | 132 | // any class wishing to process wxWidgets events must use this macro |
c59e5089 VZ |
133 | DECLARE_EVENT_TABLE() |
134 | }; | |
135 | ||
136 | // a very simple class just to have something to draw on | |
137 | class MyPanel : public wxPanel | |
138 | { | |
139 | public: | |
8c9d1533 VZ |
140 | MyPanel(wxWindow *parent) : wxPanel(parent) { m_flags = 0; } |
141 | ||
142 | int GetFlags() const { return m_flags; } | |
143 | void SetFlags(int flags) { m_flags = flags; } | |
c59e5089 | 144 | |
8c9d1533 | 145 | private: |
c59e5089 VZ |
146 | void OnPaint(wxPaintEvent&) |
147 | { | |
148 | wxPaintDC dc(this); | |
149 | ||
51c42fc5 | 150 | wxRendererNative& renderer = wxRendererNative::Get(); |
51c42fc5 | 151 | |
737e5335 VZ |
152 | int x1 = 10, // text offset |
153 | x2 = 200, // drawing offset | |
154 | y = 10; | |
155 | ||
156 | const int lineHeight = dc.GetCharHeight(); | |
157 | dc.DrawText("Demonstration of various wxRenderer functions:", x1, y); | |
8c9d1533 VZ |
158 | y += lineHeight; |
159 | wxString flagsString; | |
160 | if ( m_flags & wxCONTROL_DISABLED ) | |
161 | flagsString += "wxCONTROL_DISABLED "; | |
162 | if ( m_flags & wxCONTROL_FOCUSED ) | |
163 | flagsString += "wxCONTROL_FOCUSED "; | |
164 | if ( m_flags & wxCONTROL_PRESSED ) | |
165 | flagsString += "wxCONTROL_PRESSED "; | |
166 | if ( m_flags & wxCONTROL_CURRENT ) | |
167 | flagsString += "wxCONTROL_CURRENT "; | |
168 | if ( m_flags & wxCONTROL_CHECKED ) | |
169 | flagsString += "wxCONTROL_CHECKED "; | |
170 | if ( flagsString.empty() ) | |
171 | flagsString = "(none)"; | |
172 | dc.DrawText("Using flags: " + flagsString, x1, y); | |
737e5335 VZ |
173 | y += lineHeight*3; |
174 | ||
175 | dc.DrawText("DrawHeaderButton() (overridden)", x1, y); | |
176 | const wxCoord heightHdr = renderer.GetHeaderButtonHeight(this); | |
8c9d1533 VZ |
177 | renderer.DrawHeaderButton(this, dc, |
178 | wxRect(x2, y, 100, heightHdr), m_flags); | |
737e5335 VZ |
179 | y += lineHeight + heightHdr; |
180 | ||
181 | dc.DrawText("DrawCheckBox()", x1, y); | |
182 | const wxSize sizeCheck = renderer.GetCheckBoxSize(this); | |
8c9d1533 VZ |
183 | renderer.DrawCheckBox(this, dc, |
184 | wxRect(wxPoint(x2, y), sizeCheck), m_flags); | |
737e5335 VZ |
185 | y += lineHeight + sizeCheck.y; |
186 | ||
187 | dc.DrawText("DrawRadioBitmap()", x1, y); | |
8c9d1533 VZ |
188 | renderer.DrawRadioBitmap(this, dc, |
189 | wxRect(wxPoint(x2, y), sizeCheck), m_flags); | |
737e5335 VZ |
190 | y += lineHeight + sizeCheck.y; |
191 | ||
192 | dc.DrawText("DrawTreeItemButton()", x1, y); | |
8c9d1533 VZ |
193 | renderer.DrawTreeItemButton(this, dc, |
194 | wxRect(x2, y, 20, 20), m_flags); | |
737e5335 | 195 | y += lineHeight + 20; |
b50d93d1 VZ |
196 | |
197 | #ifdef wxHAS_DRAW_TITLE_BAR_BITMAP | |
198 | dc.DrawText("DrawTitleBarBitmap()", x1, y); | |
199 | wxRect rBtn(x2, y, 21, 21); | |
200 | renderer.DrawTitleBarBitmap(this, dc, rBtn, | |
201 | wxTITLEBAR_BUTTON_HELP, m_flags); | |
202 | rBtn.x += 2*rBtn.width; | |
203 | renderer.DrawTitleBarBitmap(this, dc, rBtn, | |
204 | wxTITLEBAR_BUTTON_ICONIZE, m_flags); | |
205 | rBtn.x += 2*rBtn.width; | |
206 | renderer.DrawTitleBarBitmap(this, dc, rBtn, | |
207 | wxTITLEBAR_BUTTON_RESTORE, m_flags); | |
208 | rBtn.x += 2*rBtn.width; | |
209 | renderer.DrawTitleBarBitmap(this, dc, rBtn, | |
210 | wxTITLEBAR_BUTTON_MAXIMIZE, m_flags); | |
211 | rBtn.x += 2*rBtn.width; | |
212 | renderer.DrawTitleBarBitmap(this, dc, rBtn, | |
213 | wxTITLEBAR_BUTTON_CLOSE, m_flags); | |
214 | ||
215 | y += lineHeight + rBtn.height; | |
216 | #endif // wxHAS_DRAW_TITLE_BAR_BITMAP | |
c59e5089 VZ |
217 | } |
218 | ||
8c9d1533 VZ |
219 | int m_flags; |
220 | ||
c59e5089 VZ |
221 | DECLARE_EVENT_TABLE() |
222 | }; | |
223 | ||
224 | BEGIN_EVENT_TABLE(MyPanel, wxPanel) | |
225 | EVT_PAINT(MyPanel::OnPaint) | |
226 | END_EVENT_TABLE() | |
227 | ||
228 | // ---------------------------------------------------------------------------- | |
229 | // constants | |
230 | // ---------------------------------------------------------------------------- | |
231 | ||
232 | // IDs for the controls and the menu commands | |
233 | enum | |
234 | { | |
235 | // our menu items | |
8c9d1533 VZ |
236 | Render_DrawDisabled = 100, |
237 | Render_DrawFocused, | |
238 | Render_DrawPressed, | |
239 | Render_DrawChecked, | |
240 | Render_DrawHot, | |
241 | ||
ba46d9ea | 242 | #if wxUSE_DYNLIB_CLASS |
8c9d1533 | 243 | Render_Load, |
c59e5089 | 244 | Render_Unload, |
ba46d9ea | 245 | #endif // wxUSE_DYNLIB_CLASS |
c59e5089 VZ |
246 | |
247 | // standard menu items | |
248 | Render_Quit = wxID_EXIT, | |
249 | ||
250 | // it is important for the id corresponding to the "About" command to have | |
251 | // this standard value as otherwise it won't be handled properly under Mac | |
252 | // (where it is special and put into the "Apple" menu) | |
253 | Render_About = wxID_ABOUT | |
254 | }; | |
255 | ||
256 | // ---------------------------------------------------------------------------- | |
be5a51fb | 257 | // event tables and other macros for wxWidgets |
c59e5089 VZ |
258 | // ---------------------------------------------------------------------------- |
259 | ||
be5a51fb | 260 | // the event tables connect the wxWidgets events with the functions (event |
c59e5089 VZ |
261 | // handlers) which process them. It can be also done at run-time, but for the |
262 | // simple menu events like this the static method is much simpler. | |
263 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
8c9d1533 VZ |
264 | EVT_MENU(Render_DrawDisabled, MyFrame::OnDrawDisabled) |
265 | EVT_MENU(Render_DrawFocused, MyFrame::OnDrawFocused) | |
266 | EVT_MENU(Render_DrawPressed, MyFrame::OnDrawPressed) | |
267 | EVT_MENU(Render_DrawChecked, MyFrame::OnDrawChecked) | |
268 | EVT_MENU(Render_DrawHot, MyFrame::OnDrawHot) | |
269 | ||
ba46d9ea | 270 | #if wxUSE_DYNLIB_CLASS |
c59e5089 VZ |
271 | EVT_MENU(Render_Load, MyFrame::OnLoad) |
272 | EVT_MENU(Render_Unload,MyFrame::OnUnload) | |
ba46d9ea | 273 | #endif // wxUSE_DYNLIB_CLASS |
c59e5089 VZ |
274 | EVT_MENU(Render_Quit, MyFrame::OnQuit) |
275 | ||
276 | EVT_MENU(Render_About, MyFrame::OnAbout) | |
277 | END_EVENT_TABLE() | |
278 | ||
be5a51fb | 279 | // Create a new application object: this macro will allow wxWidgets to create |
c59e5089 VZ |
280 | // the application object during program execution (it's better than using a |
281 | // static object for many reasons) and also implements the accessor function | |
282 | // wxGetApp() which will return the reference of the right type (i.e. MyApp and | |
283 | // not wxApp) | |
284 | IMPLEMENT_APP(MyApp) | |
285 | ||
286 | // ============================================================================ | |
287 | // implementation | |
288 | // ============================================================================ | |
289 | ||
290 | // ---------------------------------------------------------------------------- | |
291 | // the application class | |
292 | // ---------------------------------------------------------------------------- | |
293 | ||
294 | // 'Main program' equivalent: the program execution "starts" here | |
295 | bool MyApp::OnInit() | |
296 | { | |
45e6e6f8 VZ |
297 | if ( !wxApp::OnInit() ) |
298 | return false; | |
299 | ||
c59e5089 VZ |
300 | // create the main application window |
301 | new MyFrame; | |
302 | ||
303 | return true; | |
304 | } | |
305 | ||
306 | // ---------------------------------------------------------------------------- | |
307 | // main frame | |
308 | // ---------------------------------------------------------------------------- | |
309 | ||
310 | // frame constructor | |
311 | MyFrame::MyFrame() | |
312 | : wxFrame(NULL, | |
1ca21e6d | 313 | wxID_ANY, |
9a83f860 | 314 | wxT("Render wxWidgets Sample"), |
c59e5089 VZ |
315 | wxPoint(50, 50), |
316 | wxSize(450, 340)) | |
317 | { | |
318 | // set the frame icon | |
319 | SetIcon(wxICON(sample)); | |
320 | ||
321 | #if wxUSE_MENUS | |
322 | // create a menu bar | |
323 | wxMenu *menuFile = new wxMenu; | |
8c9d1533 VZ |
324 | menuFile->AppendCheckItem(Render_DrawDisabled, |
325 | "Draw in &disabled state\tCtrl-D"); | |
326 | menuFile->AppendCheckItem(Render_DrawFocused, | |
327 | "Draw in &focused state\tCtrl-F"); | |
328 | menuFile->AppendCheckItem(Render_DrawPressed, | |
329 | "Draw in &pressed state\tCtrl-P"); | |
330 | menuFile->AppendCheckItem(Render_DrawChecked, | |
331 | "Draw in &checked state\tCtrl-C"); | |
332 | menuFile->AppendCheckItem(Render_DrawHot, | |
333 | "Draw in &hot state\tCtrl-H"); | |
334 | menuFile->AppendSeparator(); | |
ba46d9ea | 335 | #if wxUSE_DYNLIB_CLASS |
9a83f860 VZ |
336 | menuFile->Append(Render_Load, wxT("&Load renderer...\tCtrl-L")); |
337 | menuFile->Append(Render_Unload, wxT("&Unload renderer\tCtrl-U")); | |
8c9d1533 | 338 | menuFile->AppendSeparator(); |
ba46d9ea | 339 | #endif // wxUSE_DYNLIB_CLASS |
8c9d1533 | 340 | menuFile->Append(Render_Quit); |
c59e5089 VZ |
341 | |
342 | // the "About" item should be in the help menu | |
343 | wxMenu *helpMenu = new wxMenu; | |
8c9d1533 | 344 | helpMenu->Append(Render_About); |
c59e5089 VZ |
345 | |
346 | // now append the freshly created menu to the menu bar... | |
347 | wxMenuBar *menuBar = new wxMenuBar(); | |
9a83f860 VZ |
348 | menuBar->Append(menuFile, wxT("&File")); |
349 | menuBar->Append(helpMenu, wxT("&Help")); | |
c59e5089 VZ |
350 | |
351 | // ... and attach this menu bar to the frame | |
352 | SetMenuBar(menuBar); | |
353 | #endif // wxUSE_MENUS | |
354 | ||
355 | m_panel = new MyPanel(this); | |
356 | ||
357 | #if wxUSE_STATUSBAR | |
358 | // create a status bar just for fun (by default with 1 pane only) | |
359 | CreateStatusBar(2); | |
9a83f860 | 360 | SetStatusText(wxT("Welcome to wxWidgets!")); |
c59e5089 VZ |
361 | #endif // wxUSE_STATUSBAR |
362 | ||
363 | Show(); | |
364 | } | |
365 | ||
366 | MyFrame::~MyFrame() | |
367 | { | |
368 | delete wxRendererNative::Set(NULL); | |
369 | } | |
370 | ||
371 | ||
372 | // event handlers | |
373 | ||
8c9d1533 VZ |
374 | void MyFrame::OnToggleDrawFlag(wxCommandEvent& event, int flag) |
375 | { | |
376 | int flags = m_panel->GetFlags(); | |
377 | if ( event.IsChecked() ) | |
378 | flags |= flag; | |
379 | else | |
380 | flags &= ~flag; | |
381 | ||
382 | m_panel->SetFlags(flags); | |
383 | m_panel->Refresh(); | |
384 | } | |
385 | ||
ba46d9ea VZ |
386 | #if wxUSE_DYNLIB_CLASS |
387 | ||
c59e5089 VZ |
388 | void MyFrame::OnLoad(wxCommandEvent& WXUNUSED(event)) |
389 | { | |
9a83f860 | 390 | static wxString s_name = wxT("renddll"); |
c59e5089 VZ |
391 | |
392 | wxString name = wxGetTextFromUser | |
393 | ( | |
9a83f860 VZ |
394 | wxT("Name of the renderer to load:"), |
395 | wxT("Render wxWidgets Sample"), | |
c59e5089 VZ |
396 | s_name, |
397 | this | |
398 | ); | |
399 | if ( name.empty() ) | |
400 | { | |
401 | // cancelled | |
402 | return; | |
403 | } | |
404 | ||
405 | s_name = name; | |
406 | ||
407 | wxRendererNative *renderer = wxRendererNative::Load(name); | |
408 | if ( !renderer ) | |
409 | { | |
9a83f860 | 410 | wxLogError(wxT("Failed to load renderer \"%s\"."), name.c_str()); |
c59e5089 VZ |
411 | } |
412 | else // loaded ok | |
413 | { | |
414 | delete wxRendererNative::Set(renderer); | |
415 | ||
416 | m_panel->Refresh(); | |
417 | ||
9a83f860 | 418 | wxLogStatus(this, wxT("Successfully loaded the renderer \"%s\"."), |
c59e5089 VZ |
419 | name.c_str()); |
420 | } | |
421 | } | |
422 | ||
423 | void MyFrame::OnUnload(wxCommandEvent& WXUNUSED(event)) | |
424 | { | |
425 | wxRendererNative *renderer = wxRendererNative::Set(NULL); | |
426 | if ( renderer ) | |
427 | { | |
428 | delete renderer; | |
429 | ||
430 | m_panel->Refresh(); | |
431 | ||
9a83f860 | 432 | wxLogStatus(this, wxT("Unloaded the previously loaded renderer.")); |
c59e5089 VZ |
433 | } |
434 | else | |
435 | { | |
9a83f860 | 436 | wxLogWarning(wxT("No renderer to unload.")); |
c59e5089 VZ |
437 | } |
438 | } | |
439 | ||
ba46d9ea VZ |
440 | #endif // wxUSE_DYNLIB_CLASS |
441 | ||
c59e5089 VZ |
442 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
443 | { | |
444 | // true is to force the frame to close | |
445 | Close(true); | |
446 | } | |
447 | ||
448 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
449 | { | |
9a83f860 VZ |
450 | wxMessageBox(wxT("Render sample shows how to use custom renderers.\n") |
451 | wxT("\n") | |
452 | wxT("(c) 2003 Vadim Zeitlin"), | |
453 | wxT("About Render wxWidgets Sample"), | |
c59e5089 VZ |
454 | wxOK | wxICON_INFORMATION, this); |
455 | } | |
456 |