]> git.saurik.com Git - wxWidgets.git/blob - samples/render/render.cpp
check for HAVE_VISIBILITY first when setting WXIMPORT/WXEXPORT; only set it on platfo...
[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 dc.SetBrush(*wxBLUE_BRUSH);
70 dc.SetTextForeground(*wxWHITE);
71 dc.DrawRoundedRectangle(rect, 5);
72 dc.DrawLabel(_T("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 dc.DrawText(_T("Below is the standard header button drawn"), 10, 10);
134 dc.DrawText(_T("using the current renderer:"), 10, 40);
135
136 wxRendererNative::Get().DrawHeaderButton(this, dc,
137 wxRect(20, 70, 100, 60));
138 }
139
140 DECLARE_EVENT_TABLE()
141 };
142
143 BEGIN_EVENT_TABLE(MyPanel, wxPanel)
144 EVT_PAINT(MyPanel::OnPaint)
145 END_EVENT_TABLE()
146
147 // ----------------------------------------------------------------------------
148 // constants
149 // ----------------------------------------------------------------------------
150
151 // IDs for the controls and the menu commands
152 enum
153 {
154 // our menu items
155 #if wxUSE_DYNLIB_CLASS
156 Render_Load = 100,
157 Render_Unload,
158 #endif // wxUSE_DYNLIB_CLASS
159
160 // standard menu items
161 Render_Quit = wxID_EXIT,
162
163 // it is important for the id corresponding to the "About" command to have
164 // this standard value as otherwise it won't be handled properly under Mac
165 // (where it is special and put into the "Apple" menu)
166 Render_About = wxID_ABOUT
167 };
168
169 // ----------------------------------------------------------------------------
170 // event tables and other macros for wxWidgets
171 // ----------------------------------------------------------------------------
172
173 // the event tables connect the wxWidgets events with the functions (event
174 // handlers) which process them. It can be also done at run-time, but for the
175 // simple menu events like this the static method is much simpler.
176 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
177 #if wxUSE_DYNLIB_CLASS
178 EVT_MENU(Render_Load, MyFrame::OnLoad)
179 EVT_MENU(Render_Unload,MyFrame::OnUnload)
180 #endif // wxUSE_DYNLIB_CLASS
181 EVT_MENU(Render_Quit, MyFrame::OnQuit)
182
183 EVT_MENU(Render_About, MyFrame::OnAbout)
184 END_EVENT_TABLE()
185
186 // Create a new application object: this macro will allow wxWidgets to create
187 // the application object during program execution (it's better than using a
188 // static object for many reasons) and also implements the accessor function
189 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
190 // not wxApp)
191 IMPLEMENT_APP(MyApp)
192
193 // ============================================================================
194 // implementation
195 // ============================================================================
196
197 // ----------------------------------------------------------------------------
198 // the application class
199 // ----------------------------------------------------------------------------
200
201 // 'Main program' equivalent: the program execution "starts" here
202 bool MyApp::OnInit()
203 {
204 if ( !wxApp::OnInit() )
205 return false;
206
207 // create the main application window
208 new MyFrame;
209
210 return true;
211 }
212
213 // ----------------------------------------------------------------------------
214 // main frame
215 // ----------------------------------------------------------------------------
216
217 // frame constructor
218 MyFrame::MyFrame()
219 : wxFrame(NULL,
220 wxID_ANY,
221 _T("Render wxWidgets Sample"),
222 wxPoint(50, 50),
223 wxSize(450, 340))
224 {
225 // set the frame icon
226 SetIcon(wxICON(sample));
227
228 #if wxUSE_MENUS
229 // create a menu bar
230 wxMenu *menuFile = new wxMenu;
231 #if wxUSE_DYNLIB_CLASS
232 menuFile->Append(Render_Load, _T("&Load renderer...\tCtrl-L"));
233 menuFile->Append(Render_Unload, _T("&Unload renderer\tCtrl-U"));
234 #endif // wxUSE_DYNLIB_CLASS
235 menuFile->Append(Render_Quit, _T("E&xit\tCtrl-Q"), _T("Quit this program"));
236
237 // the "About" item should be in the help menu
238 wxMenu *helpMenu = new wxMenu;
239 helpMenu->Append(Render_About, _T("&About...\tF1"), _T("Show about dialog"));
240
241 // now append the freshly created menu to the menu bar...
242 wxMenuBar *menuBar = new wxMenuBar();
243 menuBar->Append(menuFile, _T("&File"));
244 menuBar->Append(helpMenu, _T("&Help"));
245
246 // ... and attach this menu bar to the frame
247 SetMenuBar(menuBar);
248 #endif // wxUSE_MENUS
249
250 m_panel = new MyPanel(this);
251
252 #if wxUSE_STATUSBAR
253 // create a status bar just for fun (by default with 1 pane only)
254 CreateStatusBar(2);
255 SetStatusText(_T("Welcome to wxWidgets!"));
256 #endif // wxUSE_STATUSBAR
257
258 Show();
259 }
260
261 MyFrame::~MyFrame()
262 {
263 delete wxRendererNative::Set(NULL);
264 }
265
266
267 // event handlers
268
269 #if wxUSE_DYNLIB_CLASS
270
271 void MyFrame::OnLoad(wxCommandEvent& WXUNUSED(event))
272 {
273 static wxString s_name = _T("renddll");
274
275 wxString name = wxGetTextFromUser
276 (
277 _T("Name of the renderer to load:"),
278 _T("Render wxWidgets Sample"),
279 s_name,
280 this
281 );
282 if ( name.empty() )
283 {
284 // cancelled
285 return;
286 }
287
288 s_name = name;
289
290 wxRendererNative *renderer = wxRendererNative::Load(name);
291 if ( !renderer )
292 {
293 wxLogError(_T("Failed to load renderer \"%s\"."), name.c_str());
294 }
295 else // loaded ok
296 {
297 delete wxRendererNative::Set(renderer);
298
299 m_panel->Refresh();
300
301 wxLogStatus(this, _T("Successfully loaded the renderer \"%s\"."),
302 name.c_str());
303 }
304 }
305
306 void MyFrame::OnUnload(wxCommandEvent& WXUNUSED(event))
307 {
308 wxRendererNative *renderer = wxRendererNative::Set(NULL);
309 if ( renderer )
310 {
311 delete renderer;
312
313 m_panel->Refresh();
314
315 wxLogStatus(this, _T("Unloaded the previously loaded renderer."));
316 }
317 else
318 {
319 wxLogWarning(_T("No renderer to unload."));
320 }
321 }
322
323 #endif // wxUSE_DYNLIB_CLASS
324
325 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
326 {
327 // true is to force the frame to close
328 Close(true);
329 }
330
331 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
332 {
333 wxMessageBox(_T("Render sample shows how to use custom renderers.\n")
334 _T("\n")
335 _T("(c) 2003 Vadim Zeitlin"),
336 _T("About Render wxWidgets Sample"),
337 wxOK | wxICON_INFORMATION, this);
338 }
339