1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Render wxWidgets sample
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
31 #include "wx/dcclient.h"
34 #include "wx/textdlg.h"
36 #include "wx/msgdlg.h"
41 #include "wx/apptrait.h"
42 #include "wx/renderer.h"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 // the application icon (under Windows and OS/2 it is in resources)
49 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
50 #include "../sample.xpm"
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 // A renderer class draws the header buttons in a "special" way
58 class MyRenderer
: public wxDelegateRendererNative
61 MyRenderer() : wxDelegateRendererNative(wxRendererNative::GetDefault()) { }
63 virtual int DrawHeaderButton(wxWindow
*WXUNUSED(win
),
66 int WXUNUSED(flags
) = 0,
67 wxHeaderSortIconType
WXUNUSED(sortArrow
) = wxHDR_SORT_ICON_NONE
,
68 wxHeaderButtonParams
* WXUNUSED(params
) = NULL
)
70 wxDCBrushChanger
setBrush(dc
, *wxBLUE_BRUSH
);
71 wxDCTextColourChanger
setFgCol(dc
, *wxWHITE
);
72 dc
.DrawRoundedRectangle(rect
, 5);
73 dc
.DrawLabel(wxT("MyRenderer"), wxNullBitmap
, rect
, wxALIGN_CENTER
);
78 // To use a different renderer from the very beginning we must override
79 // wxAppTraits method creating the renderer (another, simpler, alternative is
80 // to call wxRendererNative::Set() a.s.a.p. which should work in 99% of the
81 // cases, but we show this here just for completeness)
82 class MyTraits
: public wxGUIAppTraits
84 virtual wxRendererNative
*CreateRenderer()
86 // it will be deleted on program shutdown by wxWidgets itself
87 return new MyRenderer
;
91 // Define a new application type, each program should derive a class from wxApp
92 class MyApp
: public wxApp
95 virtual bool OnInit();
97 // if we want MyTraits to be used we must override CreateTraits()
98 virtual wxAppTraits
*CreateTraits() { return new MyTraits
; }
101 // Define a new frame type: this is going to be our main frame
102 class MyFrame
: public wxFrame
110 // event handlers (these functions should _not_ be virtual)
111 void OnDrawDisabled(wxCommandEvent
& event
)
112 { OnToggleDrawFlag(event
, wxCONTROL_DISABLED
); }
113 void OnDrawFocused(wxCommandEvent
& event
)
114 { OnToggleDrawFlag(event
, wxCONTROL_FOCUSED
); }
115 void OnDrawPressed(wxCommandEvent
& event
)
116 { OnToggleDrawFlag(event
, wxCONTROL_PRESSED
); }
117 void OnDrawChecked(wxCommandEvent
& event
)
118 { OnToggleDrawFlag(event
, wxCONTROL_CHECKED
); }
119 void OnDrawHot(wxCommandEvent
& event
)
120 { OnToggleDrawFlag(event
, wxCONTROL_CURRENT
); }
122 #if wxUSE_DYNLIB_CLASS
123 void OnLoad(wxCommandEvent
& event
);
124 void OnUnload(wxCommandEvent
& event
);
125 #endif // wxUSE_DYNLIB_CLASS
126 void OnQuit(wxCommandEvent
& event
);
127 void OnAbout(wxCommandEvent
& event
);
129 void OnToggleDrawFlag(wxCommandEvent
& event
, int flag
);
131 class MyPanel
*m_panel
;
133 // any class wishing to process wxWidgets events must use this macro
134 DECLARE_EVENT_TABLE()
137 // a very simple class just to have something to draw on
138 class MyPanel
: public wxPanel
141 MyPanel(wxWindow
*parent
) : wxPanel(parent
) { m_flags
= 0; }
143 int GetFlags() const { return m_flags
; }
144 void SetFlags(int flags
) { m_flags
= flags
; }
147 void OnPaint(wxPaintEvent
&)
151 wxRendererNative
& renderer
= wxRendererNative::Get();
153 int x1
= 10, // text offset
154 x2
= 200, // drawing offset
157 const int lineHeight
= dc
.GetCharHeight();
158 dc
.DrawText("Demonstration of various wxRenderer functions:", x1
, y
);
160 wxString flagsString
;
161 if ( m_flags
& wxCONTROL_DISABLED
)
162 flagsString
+= "wxCONTROL_DISABLED ";
163 if ( m_flags
& wxCONTROL_FOCUSED
)
164 flagsString
+= "wxCONTROL_FOCUSED ";
165 if ( m_flags
& wxCONTROL_PRESSED
)
166 flagsString
+= "wxCONTROL_PRESSED ";
167 if ( m_flags
& wxCONTROL_CURRENT
)
168 flagsString
+= "wxCONTROL_CURRENT ";
169 if ( m_flags
& wxCONTROL_CHECKED
)
170 flagsString
+= "wxCONTROL_CHECKED ";
171 if ( flagsString
.empty() )
172 flagsString
= "(none)";
173 dc
.DrawText("Using flags: " + flagsString
, x1
, y
);
176 dc
.DrawText("DrawHeaderButton() (overridden)", x1
, y
);
177 const wxCoord heightHdr
= renderer
.GetHeaderButtonHeight(this);
178 renderer
.DrawHeaderButton(this, dc
,
179 wxRect(x2
, y
, 100, heightHdr
), m_flags
);
180 y
+= lineHeight
+ heightHdr
;
182 dc
.DrawText("DrawCheckBox()", x1
, y
);
183 const wxSize sizeCheck
= renderer
.GetCheckBoxSize(this);
184 renderer
.DrawCheckBox(this, dc
,
185 wxRect(wxPoint(x2
, y
), sizeCheck
), m_flags
);
186 y
+= lineHeight
+ sizeCheck
.y
;
188 dc
.DrawText("DrawRadioBitmap()", x1
, y
);
189 renderer
.DrawRadioBitmap(this, dc
,
190 wxRect(wxPoint(x2
, y
), sizeCheck
), m_flags
);
191 y
+= lineHeight
+ sizeCheck
.y
;
193 dc
.DrawText("DrawTreeItemButton()", x1
, y
);
194 renderer
.DrawTreeItemButton(this, dc
,
195 wxRect(x2
, y
, 20, 20), m_flags
);
196 y
+= lineHeight
+ 20;
198 #ifdef wxHAS_DRAW_TITLE_BAR_BITMAP
199 dc
.DrawText("DrawTitleBarBitmap()", x1
, y
);
200 wxRect
rBtn(x2
, y
, 21, 21);
201 renderer
.DrawTitleBarBitmap(this, dc
, rBtn
,
202 wxTITLEBAR_BUTTON_HELP
, m_flags
);
203 rBtn
.x
+= 2*rBtn
.width
;
204 renderer
.DrawTitleBarBitmap(this, dc
, rBtn
,
205 wxTITLEBAR_BUTTON_ICONIZE
, m_flags
);
206 rBtn
.x
+= 2*rBtn
.width
;
207 renderer
.DrawTitleBarBitmap(this, dc
, rBtn
,
208 wxTITLEBAR_BUTTON_RESTORE
, m_flags
);
209 rBtn
.x
+= 2*rBtn
.width
;
210 renderer
.DrawTitleBarBitmap(this, dc
, rBtn
,
211 wxTITLEBAR_BUTTON_MAXIMIZE
, m_flags
);
212 rBtn
.x
+= 2*rBtn
.width
;
213 renderer
.DrawTitleBarBitmap(this, dc
, rBtn
,
214 wxTITLEBAR_BUTTON_CLOSE
, m_flags
);
216 y
+= lineHeight
+ rBtn
.height
;
217 #endif // wxHAS_DRAW_TITLE_BAR_BITMAP
222 DECLARE_EVENT_TABLE()
225 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
226 EVT_PAINT(MyPanel::OnPaint
)
229 // ----------------------------------------------------------------------------
231 // ----------------------------------------------------------------------------
233 // IDs for the controls and the menu commands
237 Render_DrawDisabled
= 100,
243 #if wxUSE_DYNLIB_CLASS
246 #endif // wxUSE_DYNLIB_CLASS
248 // standard menu items
249 Render_Quit
= wxID_EXIT
,
251 // it is important for the id corresponding to the "About" command to have
252 // this standard value as otherwise it won't be handled properly under Mac
253 // (where it is special and put into the "Apple" menu)
254 Render_About
= wxID_ABOUT
257 // ----------------------------------------------------------------------------
258 // event tables and other macros for wxWidgets
259 // ----------------------------------------------------------------------------
261 // the event tables connect the wxWidgets events with the functions (event
262 // handlers) which process them. It can be also done at run-time, but for the
263 // simple menu events like this the static method is much simpler.
264 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
265 EVT_MENU(Render_DrawDisabled
, MyFrame::OnDrawDisabled
)
266 EVT_MENU(Render_DrawFocused
, MyFrame::OnDrawFocused
)
267 EVT_MENU(Render_DrawPressed
, MyFrame::OnDrawPressed
)
268 EVT_MENU(Render_DrawChecked
, MyFrame::OnDrawChecked
)
269 EVT_MENU(Render_DrawHot
, MyFrame::OnDrawHot
)
271 #if wxUSE_DYNLIB_CLASS
272 EVT_MENU(Render_Load
, MyFrame::OnLoad
)
273 EVT_MENU(Render_Unload
,MyFrame::OnUnload
)
274 #endif // wxUSE_DYNLIB_CLASS
275 EVT_MENU(Render_Quit
, MyFrame::OnQuit
)
277 EVT_MENU(Render_About
, MyFrame::OnAbout
)
280 // Create a new application object: this macro will allow wxWidgets to create
281 // the application object during program execution (it's better than using a
282 // static object for many reasons) and also implements the accessor function
283 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
287 // ============================================================================
289 // ============================================================================
291 // ----------------------------------------------------------------------------
292 // the application class
293 // ----------------------------------------------------------------------------
295 // 'Main program' equivalent: the program execution "starts" here
298 if ( !wxApp::OnInit() )
302 // currently the images used by DrawTitleBarBitmap() are hard coded as PNG
303 // images inside the library itself so we need to enable PNG support to use
305 wxImage::AddHandler(new wxPNGHandler
);
308 // create the main application window
314 // ----------------------------------------------------------------------------
316 // ----------------------------------------------------------------------------
322 wxT("Render wxWidgets Sample"),
326 // set the frame icon
327 SetIcon(wxICON(sample
));
331 wxMenu
*menuFile
= new wxMenu
;
332 menuFile
->AppendCheckItem(Render_DrawDisabled
,
333 "Draw in &disabled state\tCtrl-D");
334 menuFile
->AppendCheckItem(Render_DrawFocused
,
335 "Draw in &focused state\tCtrl-F");
336 menuFile
->AppendCheckItem(Render_DrawPressed
,
337 "Draw in &pressed state\tCtrl-P");
338 menuFile
->AppendCheckItem(Render_DrawChecked
,
339 "Draw in &checked state\tCtrl-C");
340 menuFile
->AppendCheckItem(Render_DrawHot
,
341 "Draw in &hot state\tCtrl-H");
342 menuFile
->AppendSeparator();
343 #if wxUSE_DYNLIB_CLASS
344 menuFile
->Append(Render_Load
, wxT("&Load renderer...\tCtrl-L"));
345 menuFile
->Append(Render_Unload
, wxT("&Unload renderer\tCtrl-U"));
346 menuFile
->AppendSeparator();
347 #endif // wxUSE_DYNLIB_CLASS
348 menuFile
->Append(Render_Quit
);
350 // the "About" item should be in the help menu
351 wxMenu
*helpMenu
= new wxMenu
;
352 helpMenu
->Append(Render_About
);
354 // now append the freshly created menu to the menu bar...
355 wxMenuBar
*menuBar
= new wxMenuBar();
356 menuBar
->Append(menuFile
, wxT("&File"));
357 menuBar
->Append(helpMenu
, wxT("&Help"));
359 // ... and attach this menu bar to the frame
361 #endif // wxUSE_MENUS
363 m_panel
= new MyPanel(this);
366 // create a status bar just for fun (by default with 1 pane only)
368 SetStatusText(wxT("Welcome to wxWidgets!"));
369 #endif // wxUSE_STATUSBAR
376 delete wxRendererNative::Set(NULL
);
382 void MyFrame::OnToggleDrawFlag(wxCommandEvent
& event
, int flag
)
384 int flags
= m_panel
->GetFlags();
385 if ( event
.IsChecked() )
390 m_panel
->SetFlags(flags
);
394 #if wxUSE_DYNLIB_CLASS
396 void MyFrame::OnLoad(wxCommandEvent
& WXUNUSED(event
))
398 static wxString s_name
= wxT("renddll");
400 wxString name
= wxGetTextFromUser
402 wxT("Name of the renderer to load:"),
403 wxT("Render wxWidgets Sample"),
415 wxRendererNative
*renderer
= wxRendererNative::Load(name
);
418 wxLogError(wxT("Failed to load renderer \"%s\"."), name
.c_str());
422 delete wxRendererNative::Set(renderer
);
426 wxLogStatus(this, wxT("Successfully loaded the renderer \"%s\"."),
431 void MyFrame::OnUnload(wxCommandEvent
& WXUNUSED(event
))
433 wxRendererNative
*renderer
= wxRendererNative::Set(NULL
);
440 wxLogStatus(this, wxT("Unloaded the previously loaded renderer."));
444 wxLogWarning(wxT("No renderer to unload."));
448 #endif // wxUSE_DYNLIB_CLASS
450 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
452 // true is to force the frame to close
456 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
458 wxMessageBox(wxT("Render sample shows how to use custom renderers.\n")
460 wxT("(c) 2003 Vadim Zeitlin"),
461 wxT("About Render wxWidgets Sample"),
462 wxOK
| wxICON_INFORMATION
, this);