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