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