]> git.saurik.com Git - wxWidgets.git/blame - samples/render/render.cpp
Added wxPropertyGrid::SetSortFunction(); moved Sort() and SortChildren() to wxPropert...
[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"
c59e5089
VZ
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
57class MyRenderer : public wxDelegateRendererNative
58{
59public:
60 MyRenderer() : wxDelegateRendererNative(wxRendererNative::GetDefault()) { }
61
adf07e34 62 virtual int DrawHeaderButton(wxWindow *WXUNUSED(win),
c59e5089
VZ
63 wxDC& dc,
64 const wxRect& rect,
1ca21e6d
WS
65 int WXUNUSED(flags) = 0,
66 wxHeaderSortIconType WXUNUSED(sortArrow) = wxHDR_SORT_ICON_NONE,
67 wxHeaderButtonParams* WXUNUSED(params) = NULL)
c59e5089
VZ
68 {
69 dc.SetBrush(*wxBLUE_BRUSH);
70 dc.SetTextForeground(*wxWHITE);
71 dc.DrawRoundedRectangle(rect, 5);
72 dc.DrawLabel(_T("MyRenderer"), wxNullBitmap, rect, wxALIGN_CENTER);
adf07e34 73 return rect.width;
c59e5089
VZ
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)
81class MyTraits : public wxGUIAppTraits
82{
83 virtual wxRendererNative *CreateRenderer()
84 {
be5a51fb 85 // it will be deleted on program shutdown by wxWidgets itself
c59e5089
VZ
86 return new MyRenderer;
87 }
88};
89
90// Define a new application type, each program should derive a class from wxApp
91class MyApp : public wxApp
92{
93public:
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
101class MyFrame : public wxFrame
102{
103public:
104 // ctor(s)
105 MyFrame();
106 virtual ~MyFrame();
107
108 // event handlers (these functions should _not_ be virtual)
ba46d9ea 109#if wxUSE_DYNLIB_CLASS
c59e5089
VZ
110 void OnLoad(wxCommandEvent& event);
111 void OnUnload(wxCommandEvent& event);
ba46d9ea 112#endif // wxUSE_DYNLIB_CLASS
c59e5089
VZ
113 void OnQuit(wxCommandEvent& event);
114 void OnAbout(wxCommandEvent& event);
115
116private:
117 wxPanel *m_panel;
118
be5a51fb 119 // any class wishing to process wxWidgets events must use this macro
c59e5089
VZ
120 DECLARE_EVENT_TABLE()
121};
122
123// a very simple class just to have something to draw on
124class MyPanel : public wxPanel
125{
126public:
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
51c42fc5
VZ
136 wxRendererNative& renderer = wxRendererNative::Get();
137 const wxCoord height = renderer.GetHeaderButtonHeight(this);
138
139 renderer.DrawHeaderButton(this, dc, wxRect(20, 70, 100, height));
c59e5089
VZ
140 }
141
142 DECLARE_EVENT_TABLE()
143};
144
145BEGIN_EVENT_TABLE(MyPanel, wxPanel)
146 EVT_PAINT(MyPanel::OnPaint)
147END_EVENT_TABLE()
148
149// ----------------------------------------------------------------------------
150// constants
151// ----------------------------------------------------------------------------
152
153// IDs for the controls and the menu commands
154enum
155{
156 // our menu items
ba46d9ea 157#if wxUSE_DYNLIB_CLASS
c59e5089
VZ
158 Render_Load = 100,
159 Render_Unload,
ba46d9ea 160#endif // wxUSE_DYNLIB_CLASS
c59e5089
VZ
161
162 // standard menu items
163 Render_Quit = wxID_EXIT,
164
165 // it is important for the id corresponding to the "About" command to have
166 // this standard value as otherwise it won't be handled properly under Mac
167 // (where it is special and put into the "Apple" menu)
168 Render_About = wxID_ABOUT
169};
170
171// ----------------------------------------------------------------------------
be5a51fb 172// event tables and other macros for wxWidgets
c59e5089
VZ
173// ----------------------------------------------------------------------------
174
be5a51fb 175// the event tables connect the wxWidgets events with the functions (event
c59e5089
VZ
176// handlers) which process them. It can be also done at run-time, but for the
177// simple menu events like this the static method is much simpler.
178BEGIN_EVENT_TABLE(MyFrame, wxFrame)
ba46d9ea 179#if wxUSE_DYNLIB_CLASS
c59e5089
VZ
180 EVT_MENU(Render_Load, MyFrame::OnLoad)
181 EVT_MENU(Render_Unload,MyFrame::OnUnload)
ba46d9ea 182#endif // wxUSE_DYNLIB_CLASS
c59e5089
VZ
183 EVT_MENU(Render_Quit, MyFrame::OnQuit)
184
185 EVT_MENU(Render_About, MyFrame::OnAbout)
186END_EVENT_TABLE()
187
be5a51fb 188// Create a new application object: this macro will allow wxWidgets to create
c59e5089
VZ
189// the application object during program execution (it's better than using a
190// static object for many reasons) and also implements the accessor function
191// wxGetApp() which will return the reference of the right type (i.e. MyApp and
192// not wxApp)
193IMPLEMENT_APP(MyApp)
194
195// ============================================================================
196// implementation
197// ============================================================================
198
199// ----------------------------------------------------------------------------
200// the application class
201// ----------------------------------------------------------------------------
202
203// 'Main program' equivalent: the program execution "starts" here
204bool MyApp::OnInit()
205{
45e6e6f8
VZ
206 if ( !wxApp::OnInit() )
207 return false;
208
c59e5089
VZ
209 // create the main application window
210 new MyFrame;
211
212 return true;
213}
214
215// ----------------------------------------------------------------------------
216// main frame
217// ----------------------------------------------------------------------------
218
219// frame constructor
220MyFrame::MyFrame()
221 : wxFrame(NULL,
1ca21e6d 222 wxID_ANY,
be5a51fb 223 _T("Render wxWidgets Sample"),
c59e5089
VZ
224 wxPoint(50, 50),
225 wxSize(450, 340))
226{
227 // set the frame icon
228 SetIcon(wxICON(sample));
229
230#if wxUSE_MENUS
231 // create a menu bar
232 wxMenu *menuFile = new wxMenu;
ba46d9ea 233#if wxUSE_DYNLIB_CLASS
c59e5089
VZ
234 menuFile->Append(Render_Load, _T("&Load renderer...\tCtrl-L"));
235 menuFile->Append(Render_Unload, _T("&Unload renderer\tCtrl-U"));
ba46d9ea 236#endif // wxUSE_DYNLIB_CLASS
c59e5089
VZ
237 menuFile->Append(Render_Quit, _T("E&xit\tCtrl-Q"), _T("Quit this program"));
238
239 // the "About" item should be in the help menu
240 wxMenu *helpMenu = new wxMenu;
241 helpMenu->Append(Render_About, _T("&About...\tF1"), _T("Show about dialog"));
242
243 // now append the freshly created menu to the menu bar...
244 wxMenuBar *menuBar = new wxMenuBar();
245 menuBar->Append(menuFile, _T("&File"));
246 menuBar->Append(helpMenu, _T("&Help"));
247
248 // ... and attach this menu bar to the frame
249 SetMenuBar(menuBar);
250#endif // wxUSE_MENUS
251
252 m_panel = new MyPanel(this);
253
254#if wxUSE_STATUSBAR
255 // create a status bar just for fun (by default with 1 pane only)
256 CreateStatusBar(2);
be5a51fb 257 SetStatusText(_T("Welcome to wxWidgets!"));
c59e5089
VZ
258#endif // wxUSE_STATUSBAR
259
260 Show();
261}
262
263MyFrame::~MyFrame()
264{
265 delete wxRendererNative::Set(NULL);
266}
267
268
269// event handlers
270
ba46d9ea
VZ
271#if wxUSE_DYNLIB_CLASS
272
c59e5089
VZ
273void MyFrame::OnLoad(wxCommandEvent& WXUNUSED(event))
274{
275 static wxString s_name = _T("renddll");
276
277 wxString name = wxGetTextFromUser
278 (
279 _T("Name of the renderer to load:"),
be5a51fb 280 _T("Render wxWidgets Sample"),
c59e5089
VZ
281 s_name,
282 this
283 );
284 if ( name.empty() )
285 {
286 // cancelled
287 return;
288 }
289
290 s_name = name;
291
292 wxRendererNative *renderer = wxRendererNative::Load(name);
293 if ( !renderer )
294 {
295 wxLogError(_T("Failed to load renderer \"%s\"."), name.c_str());
296 }
297 else // loaded ok
298 {
299 delete wxRendererNative::Set(renderer);
300
301 m_panel->Refresh();
302
303 wxLogStatus(this, _T("Successfully loaded the renderer \"%s\"."),
304 name.c_str());
305 }
306}
307
308void MyFrame::OnUnload(wxCommandEvent& WXUNUSED(event))
309{
310 wxRendererNative *renderer = wxRendererNative::Set(NULL);
311 if ( renderer )
312 {
313 delete renderer;
314
315 m_panel->Refresh();
316
317 wxLogStatus(this, _T("Unloaded the previously loaded renderer."));
318 }
319 else
320 {
321 wxLogWarning(_T("No renderer to unload."));
322 }
323}
324
ba46d9ea
VZ
325#endif // wxUSE_DYNLIB_CLASS
326
c59e5089
VZ
327void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
328{
329 // true is to force the frame to close
330 Close(true);
331}
332
333void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
334{
335 wxMessageBox(_T("Render sample shows how to use custom renderers.\n")
336 _T("\n")
749bfe9a 337 _T("(c) 2003 Vadim Zeitlin"),
be5a51fb 338 _T("About Render wxWidgets Sample"),
c59e5089
VZ
339 wxOK | wxICON_INFORMATION, this);
340}
341