Add demonstration of a few more wxRenderer methods.
[wxWidgets.git] / samples / render / render.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: render.cpp
3 // Purpose: Render wxWidgets sample
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 04.08.03
7 // RCS-ID: $Id$
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
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"
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"
37 #include "wx/icon.h"
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 int DrawHeaderButton(wxWindow *WXUNUSED(win),
63 wxDC& dc,
64 const wxRect& rect,
65 int WXUNUSED(flags) = 0,
66 wxHeaderSortIconType WXUNUSED(sortArrow) = wxHDR_SORT_ICON_NONE,
67 wxHeaderButtonParams* WXUNUSED(params) = NULL)
68 {
69 wxDCBrushChanger setBrush(dc, *wxBLUE_BRUSH);
70 wxDCTextColourChanger setFgCol(dc, *wxWHITE);
71 dc.DrawRoundedRectangle(rect, 5);
72 dc.DrawLabel(wxT("MyRenderer"), wxNullBitmap, rect, wxALIGN_CENTER);
73 return rect.width;
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 {
85 // it will be deleted on program shutdown by wxWidgets itself
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
108 // event handlers (these functions should _not_ be virtual)
109 #if wxUSE_DYNLIB_CLASS
110 void OnLoad(wxCommandEvent& event);
111 void OnUnload(wxCommandEvent& event);
112 #endif // wxUSE_DYNLIB_CLASS
113 void OnQuit(wxCommandEvent& event);
114 void OnAbout(wxCommandEvent& event);
115
116 private:
117 wxPanel *m_panel;
118
119 // any class wishing to process wxWidgets events must use this macro
120 DECLARE_EVENT_TABLE()
121 };
122
123 // a very simple class just to have something to draw on
124 class MyPanel : public wxPanel
125 {
126 public:
127 MyPanel(wxWindow *parent) : wxPanel(parent) { }
128
129 void OnPaint(wxPaintEvent&)
130 {
131 wxPaintDC dc(this);
132
133 wxRendererNative& renderer = wxRendererNative::Get();
134
135 int x1 = 10, // text offset
136 x2 = 200, // drawing offset
137 y = 10;
138
139 const int lineHeight = dc.GetCharHeight();
140 dc.DrawText("Demonstration of various wxRenderer functions:", x1, y);
141 y += lineHeight*3;
142
143 dc.DrawText("DrawHeaderButton() (overridden)", x1, y);
144 const wxCoord heightHdr = renderer.GetHeaderButtonHeight(this);
145 renderer.DrawHeaderButton(this, dc, wxRect(x2, y, 100, heightHdr));
146 y += lineHeight + heightHdr;
147
148 dc.DrawText("DrawCheckBox()", x1, y);
149 const wxSize sizeCheck = renderer.GetCheckBoxSize(this);
150 renderer.DrawCheckBox(this, dc, wxRect(wxPoint(x2, y), sizeCheck));
151 y += lineHeight + sizeCheck.y;
152
153 dc.DrawText("DrawRadioBitmap()", x1, y);
154 renderer.DrawRadioBitmap(this, dc, wxRect(wxPoint(x2, y), sizeCheck));
155 y += lineHeight + sizeCheck.y;
156
157 dc.DrawText("DrawTreeItemButton()", x1, y);
158 renderer.DrawTreeItemButton(this, dc, wxRect(x2, y, 20, 20));
159 y += lineHeight + 20;
160 }
161
162 DECLARE_EVENT_TABLE()
163 };
164
165 BEGIN_EVENT_TABLE(MyPanel, wxPanel)
166 EVT_PAINT(MyPanel::OnPaint)
167 END_EVENT_TABLE()
168
169 // ----------------------------------------------------------------------------
170 // constants
171 // ----------------------------------------------------------------------------
172
173 // IDs for the controls and the menu commands
174 enum
175 {
176 // our menu items
177 #if wxUSE_DYNLIB_CLASS
178 Render_Load = 100,
179 Render_Unload,
180 #endif // wxUSE_DYNLIB_CLASS
181
182 // standard menu items
183 Render_Quit = wxID_EXIT,
184
185 // it is important for the id corresponding to the "About" command to have
186 // this standard value as otherwise it won't be handled properly under Mac
187 // (where it is special and put into the "Apple" menu)
188 Render_About = wxID_ABOUT
189 };
190
191 // ----------------------------------------------------------------------------
192 // event tables and other macros for wxWidgets
193 // ----------------------------------------------------------------------------
194
195 // the event tables connect the wxWidgets events with the functions (event
196 // handlers) which process them. It can be also done at run-time, but for the
197 // simple menu events like this the static method is much simpler.
198 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
199 #if wxUSE_DYNLIB_CLASS
200 EVT_MENU(Render_Load, MyFrame::OnLoad)
201 EVT_MENU(Render_Unload,MyFrame::OnUnload)
202 #endif // wxUSE_DYNLIB_CLASS
203 EVT_MENU(Render_Quit, MyFrame::OnQuit)
204
205 EVT_MENU(Render_About, MyFrame::OnAbout)
206 END_EVENT_TABLE()
207
208 // Create a new application object: this macro will allow wxWidgets to create
209 // the application object during program execution (it's better than using a
210 // static object for many reasons) and also implements the accessor function
211 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
212 // not wxApp)
213 IMPLEMENT_APP(MyApp)
214
215 // ============================================================================
216 // implementation
217 // ============================================================================
218
219 // ----------------------------------------------------------------------------
220 // the application class
221 // ----------------------------------------------------------------------------
222
223 // 'Main program' equivalent: the program execution "starts" here
224 bool MyApp::OnInit()
225 {
226 if ( !wxApp::OnInit() )
227 return false;
228
229 // create the main application window
230 new MyFrame;
231
232 return true;
233 }
234
235 // ----------------------------------------------------------------------------
236 // main frame
237 // ----------------------------------------------------------------------------
238
239 // frame constructor
240 MyFrame::MyFrame()
241 : wxFrame(NULL,
242 wxID_ANY,
243 wxT("Render wxWidgets Sample"),
244 wxPoint(50, 50),
245 wxSize(450, 340))
246 {
247 // set the frame icon
248 SetIcon(wxICON(sample));
249
250 #if wxUSE_MENUS
251 // create a menu bar
252 wxMenu *menuFile = new wxMenu;
253 #if wxUSE_DYNLIB_CLASS
254 menuFile->Append(Render_Load, wxT("&Load renderer...\tCtrl-L"));
255 menuFile->Append(Render_Unload, wxT("&Unload renderer\tCtrl-U"));
256 #endif // wxUSE_DYNLIB_CLASS
257 menuFile->Append(Render_Quit, wxT("E&xit\tCtrl-Q"), wxT("Quit this program"));
258
259 // the "About" item should be in the help menu
260 wxMenu *helpMenu = new wxMenu;
261 helpMenu->Append(Render_About, wxT("&About...\tF1"), wxT("Show about dialog"));
262
263 // now append the freshly created menu to the menu bar...
264 wxMenuBar *menuBar = new wxMenuBar();
265 menuBar->Append(menuFile, wxT("&File"));
266 menuBar->Append(helpMenu, wxT("&Help"));
267
268 // ... and attach this menu bar to the frame
269 SetMenuBar(menuBar);
270 #endif // wxUSE_MENUS
271
272 m_panel = new MyPanel(this);
273
274 #if wxUSE_STATUSBAR
275 // create a status bar just for fun (by default with 1 pane only)
276 CreateStatusBar(2);
277 SetStatusText(wxT("Welcome to wxWidgets!"));
278 #endif // wxUSE_STATUSBAR
279
280 Show();
281 }
282
283 MyFrame::~MyFrame()
284 {
285 delete wxRendererNative::Set(NULL);
286 }
287
288
289 // event handlers
290
291 #if wxUSE_DYNLIB_CLASS
292
293 void MyFrame::OnLoad(wxCommandEvent& WXUNUSED(event))
294 {
295 static wxString s_name = wxT("renddll");
296
297 wxString name = wxGetTextFromUser
298 (
299 wxT("Name of the renderer to load:"),
300 wxT("Render wxWidgets Sample"),
301 s_name,
302 this
303 );
304 if ( name.empty() )
305 {
306 // cancelled
307 return;
308 }
309
310 s_name = name;
311
312 wxRendererNative *renderer = wxRendererNative::Load(name);
313 if ( !renderer )
314 {
315 wxLogError(wxT("Failed to load renderer \"%s\"."), name.c_str());
316 }
317 else // loaded ok
318 {
319 delete wxRendererNative::Set(renderer);
320
321 m_panel->Refresh();
322
323 wxLogStatus(this, wxT("Successfully loaded the renderer \"%s\"."),
324 name.c_str());
325 }
326 }
327
328 void MyFrame::OnUnload(wxCommandEvent& WXUNUSED(event))
329 {
330 wxRendererNative *renderer = wxRendererNative::Set(NULL);
331 if ( renderer )
332 {
333 delete renderer;
334
335 m_panel->Refresh();
336
337 wxLogStatus(this, wxT("Unloaded the previously loaded renderer."));
338 }
339 else
340 {
341 wxLogWarning(wxT("No renderer to unload."));
342 }
343 }
344
345 #endif // wxUSE_DYNLIB_CLASS
346
347 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
348 {
349 // true is to force the frame to close
350 Close(true);
351 }
352
353 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
354 {
355 wxMessageBox(wxT("Render sample shows how to use custom renderers.\n")
356 wxT("\n")
357 wxT("(c) 2003 Vadim Zeitlin"),
358 wxT("About Render wxWidgets Sample"),
359 wxOK | wxICON_INFORMATION, this);
360 }
361