Add demonstration of flags to the render sample.
[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 private:
109 // event handlers (these functions should _not_ be virtual)
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
121 #if wxUSE_DYNLIB_CLASS
122 void OnLoad(wxCommandEvent& event);
123 void OnUnload(wxCommandEvent& event);
124 #endif // wxUSE_DYNLIB_CLASS
125 void OnQuit(wxCommandEvent& event);
126 void OnAbout(wxCommandEvent& event);
127
128 void OnToggleDrawFlag(wxCommandEvent& event, int flag);
129
130 class MyPanel *m_panel;
131
132 // any class wishing to process wxWidgets events must use this macro
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:
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; }
144
145 private:
146 void OnPaint(wxPaintEvent&)
147 {
148 wxPaintDC dc(this);
149
150 wxRendererNative& renderer = wxRendererNative::Get();
151
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);
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);
173 y += lineHeight*3;
174
175 dc.DrawText("DrawHeaderButton() (overridden)", x1, y);
176 const wxCoord heightHdr = renderer.GetHeaderButtonHeight(this);
177 renderer.DrawHeaderButton(this, dc,
178 wxRect(x2, y, 100, heightHdr), m_flags);
179 y += lineHeight + heightHdr;
180
181 dc.DrawText("DrawCheckBox()", x1, y);
182 const wxSize sizeCheck = renderer.GetCheckBoxSize(this);
183 renderer.DrawCheckBox(this, dc,
184 wxRect(wxPoint(x2, y), sizeCheck), m_flags);
185 y += lineHeight + sizeCheck.y;
186
187 dc.DrawText("DrawRadioBitmap()", x1, y);
188 renderer.DrawRadioBitmap(this, dc,
189 wxRect(wxPoint(x2, y), sizeCheck), m_flags);
190 y += lineHeight + sizeCheck.y;
191
192 dc.DrawText("DrawTreeItemButton()", x1, y);
193 renderer.DrawTreeItemButton(this, dc,
194 wxRect(x2, y, 20, 20), m_flags);
195 y += lineHeight + 20;
196 }
197
198 int m_flags;
199
200 DECLARE_EVENT_TABLE()
201 };
202
203 BEGIN_EVENT_TABLE(MyPanel, wxPanel)
204 EVT_PAINT(MyPanel::OnPaint)
205 END_EVENT_TABLE()
206
207 // ----------------------------------------------------------------------------
208 // constants
209 // ----------------------------------------------------------------------------
210
211 // IDs for the controls and the menu commands
212 enum
213 {
214 // our menu items
215 Render_DrawDisabled = 100,
216 Render_DrawFocused,
217 Render_DrawPressed,
218 Render_DrawChecked,
219 Render_DrawHot,
220
221 #if wxUSE_DYNLIB_CLASS
222 Render_Load,
223 Render_Unload,
224 #endif // wxUSE_DYNLIB_CLASS
225
226 // standard menu items
227 Render_Quit = wxID_EXIT,
228
229 // it is important for the id corresponding to the "About" command to have
230 // this standard value as otherwise it won't be handled properly under Mac
231 // (where it is special and put into the "Apple" menu)
232 Render_About = wxID_ABOUT
233 };
234
235 // ----------------------------------------------------------------------------
236 // event tables and other macros for wxWidgets
237 // ----------------------------------------------------------------------------
238
239 // the event tables connect the wxWidgets events with the functions (event
240 // handlers) which process them. It can be also done at run-time, but for the
241 // simple menu events like this the static method is much simpler.
242 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
243 EVT_MENU(Render_DrawDisabled, MyFrame::OnDrawDisabled)
244 EVT_MENU(Render_DrawFocused, MyFrame::OnDrawFocused)
245 EVT_MENU(Render_DrawPressed, MyFrame::OnDrawPressed)
246 EVT_MENU(Render_DrawChecked, MyFrame::OnDrawChecked)
247 EVT_MENU(Render_DrawHot, MyFrame::OnDrawHot)
248
249 #if wxUSE_DYNLIB_CLASS
250 EVT_MENU(Render_Load, MyFrame::OnLoad)
251 EVT_MENU(Render_Unload,MyFrame::OnUnload)
252 #endif // wxUSE_DYNLIB_CLASS
253 EVT_MENU(Render_Quit, MyFrame::OnQuit)
254
255 EVT_MENU(Render_About, MyFrame::OnAbout)
256 END_EVENT_TABLE()
257
258 // Create a new application object: this macro will allow wxWidgets to create
259 // the application object during program execution (it's better than using a
260 // static object for many reasons) and also implements the accessor function
261 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
262 // not wxApp)
263 IMPLEMENT_APP(MyApp)
264
265 // ============================================================================
266 // implementation
267 // ============================================================================
268
269 // ----------------------------------------------------------------------------
270 // the application class
271 // ----------------------------------------------------------------------------
272
273 // 'Main program' equivalent: the program execution "starts" here
274 bool MyApp::OnInit()
275 {
276 if ( !wxApp::OnInit() )
277 return false;
278
279 // create the main application window
280 new MyFrame;
281
282 return true;
283 }
284
285 // ----------------------------------------------------------------------------
286 // main frame
287 // ----------------------------------------------------------------------------
288
289 // frame constructor
290 MyFrame::MyFrame()
291 : wxFrame(NULL,
292 wxID_ANY,
293 wxT("Render wxWidgets Sample"),
294 wxPoint(50, 50),
295 wxSize(450, 340))
296 {
297 // set the frame icon
298 SetIcon(wxICON(sample));
299
300 #if wxUSE_MENUS
301 // create a menu bar
302 wxMenu *menuFile = new wxMenu;
303 menuFile->AppendCheckItem(Render_DrawDisabled,
304 "Draw in &disabled state\tCtrl-D");
305 menuFile->AppendCheckItem(Render_DrawFocused,
306 "Draw in &focused state\tCtrl-F");
307 menuFile->AppendCheckItem(Render_DrawPressed,
308 "Draw in &pressed state\tCtrl-P");
309 menuFile->AppendCheckItem(Render_DrawChecked,
310 "Draw in &checked state\tCtrl-C");
311 menuFile->AppendCheckItem(Render_DrawHot,
312 "Draw in &hot state\tCtrl-H");
313 menuFile->AppendSeparator();
314 #if wxUSE_DYNLIB_CLASS
315 menuFile->Append(Render_Load, wxT("&Load renderer...\tCtrl-L"));
316 menuFile->Append(Render_Unload, wxT("&Unload renderer\tCtrl-U"));
317 menuFile->AppendSeparator();
318 #endif // wxUSE_DYNLIB_CLASS
319 menuFile->Append(Render_Quit);
320
321 // the "About" item should be in the help menu
322 wxMenu *helpMenu = new wxMenu;
323 helpMenu->Append(Render_About);
324
325 // now append the freshly created menu to the menu bar...
326 wxMenuBar *menuBar = new wxMenuBar();
327 menuBar->Append(menuFile, wxT("&File"));
328 menuBar->Append(helpMenu, wxT("&Help"));
329
330 // ... and attach this menu bar to the frame
331 SetMenuBar(menuBar);
332 #endif // wxUSE_MENUS
333
334 m_panel = new MyPanel(this);
335
336 #if wxUSE_STATUSBAR
337 // create a status bar just for fun (by default with 1 pane only)
338 CreateStatusBar(2);
339 SetStatusText(wxT("Welcome to wxWidgets!"));
340 #endif // wxUSE_STATUSBAR
341
342 Show();
343 }
344
345 MyFrame::~MyFrame()
346 {
347 delete wxRendererNative::Set(NULL);
348 }
349
350
351 // event handlers
352
353 void MyFrame::OnToggleDrawFlag(wxCommandEvent& event, int flag)
354 {
355 int flags = m_panel->GetFlags();
356 if ( event.IsChecked() )
357 flags |= flag;
358 else
359 flags &= ~flag;
360
361 m_panel->SetFlags(flags);
362 m_panel->Refresh();
363 }
364
365 #if wxUSE_DYNLIB_CLASS
366
367 void MyFrame::OnLoad(wxCommandEvent& WXUNUSED(event))
368 {
369 static wxString s_name = wxT("renddll");
370
371 wxString name = wxGetTextFromUser
372 (
373 wxT("Name of the renderer to load:"),
374 wxT("Render wxWidgets Sample"),
375 s_name,
376 this
377 );
378 if ( name.empty() )
379 {
380 // cancelled
381 return;
382 }
383
384 s_name = name;
385
386 wxRendererNative *renderer = wxRendererNative::Load(name);
387 if ( !renderer )
388 {
389 wxLogError(wxT("Failed to load renderer \"%s\"."), name.c_str());
390 }
391 else // loaded ok
392 {
393 delete wxRendererNative::Set(renderer);
394
395 m_panel->Refresh();
396
397 wxLogStatus(this, wxT("Successfully loaded the renderer \"%s\"."),
398 name.c_str());
399 }
400 }
401
402 void MyFrame::OnUnload(wxCommandEvent& WXUNUSED(event))
403 {
404 wxRendererNative *renderer = wxRendererNative::Set(NULL);
405 if ( renderer )
406 {
407 delete renderer;
408
409 m_panel->Refresh();
410
411 wxLogStatus(this, wxT("Unloaded the previously loaded renderer."));
412 }
413 else
414 {
415 wxLogWarning(wxT("No renderer to unload."));
416 }
417 }
418
419 #endif // wxUSE_DYNLIB_CLASS
420
421 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
422 {
423 // true is to force the frame to close
424 Close(true);
425 }
426
427 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
428 {
429 wxMessageBox(wxT("Render sample shows how to use custom renderers.\n")
430 wxT("\n")
431 wxT("(c) 2003 Vadim Zeitlin"),
432 wxT("About Render wxWidgets Sample"),
433 wxOK | wxICON_INFORMATION, this);
434 }
435