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