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