]>
git.saurik.com Git - wxWidgets.git/blob - samples/render/render.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Render wxWindows sample
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.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"
32 #include "wx/apptrait.h"
33 #include "wx/renderer.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 // the application icon (under Windows and OS/2 it is in resources)
40 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
41 #include "../sample.xpm"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 // A renderer class draws the header buttons in a "special" way
49 class MyRenderer
: public wxDelegateRendererNative
52 MyRenderer() : wxDelegateRendererNative(wxRendererNative::GetDefault()) { }
54 virtual void DrawHeaderButton(wxWindow
* WXUNUSED(win
),
57 int WXUNUSED(flags
) = 0)
59 dc
.SetBrush(*wxBLUE_BRUSH
);
60 dc
.SetTextForeground(*wxWHITE
);
61 dc
.DrawRoundedRectangle(rect
, 5);
62 dc
.DrawLabel(_T("MyRenderer"), wxNullBitmap
, rect
, wxALIGN_CENTER
);
66 // To use a different renderer from the very beginning we must override
67 // wxAppTraits method creating the renderer (another, simpler, alternative is
68 // to call wxRendererNative::Set() a.s.a.p. which should work in 99% of the
69 // cases, but we show this here just for completeness)
70 class MyTraits
: public wxGUIAppTraits
72 virtual wxRendererNative
*CreateRenderer()
74 // it will be deleted on program shutdown by wxWindows itself
75 return new MyRenderer
;
79 // Define a new application type, each program should derive a class from wxApp
80 class MyApp
: public wxApp
83 virtual bool OnInit();
85 // if we want MyTraits to be used we must override CreateTraits()
86 virtual wxAppTraits
*CreateTraits() { return new MyTraits
; }
89 // Define a new frame type: this is going to be our main frame
90 class MyFrame
: public wxFrame
97 // event handlers (these functions should _not_ be virtual)
98 void OnLoad(wxCommandEvent
& event
);
99 void OnUnload(wxCommandEvent
& event
);
100 void OnQuit(wxCommandEvent
& event
);
101 void OnAbout(wxCommandEvent
& event
);
106 // any class wishing to process wxWindows events must use this macro
107 DECLARE_EVENT_TABLE()
110 // a very simple class just to have something to draw on
111 class MyPanel
: public wxPanel
114 MyPanel(wxWindow
*parent
) : wxPanel(parent
) { }
116 void OnPaint(wxPaintEvent
&)
120 dc
.DrawText(_T("Below is the standard header button drawn"), 10, 10);
121 dc
.DrawText(_T("using the current renderer:"), 10, 40);
123 wxRendererNative::Get().DrawHeaderButton(this, dc
,
124 wxRect(20, 70, 100, 60));
127 DECLARE_EVENT_TABLE()
130 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
131 EVT_PAINT(MyPanel::OnPaint
)
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 // IDs for the controls and the menu commands
145 // standard menu items
146 Render_Quit
= wxID_EXIT
,
148 // it is important for the id corresponding to the "About" command to have
149 // this standard value as otherwise it won't be handled properly under Mac
150 // (where it is special and put into the "Apple" menu)
151 Render_About
= wxID_ABOUT
154 // ----------------------------------------------------------------------------
155 // event tables and other macros for wxWindows
156 // ----------------------------------------------------------------------------
158 // the event tables connect the wxWindows events with the functions (event
159 // handlers) which process them. It can be also done at run-time, but for the
160 // simple menu events like this the static method is much simpler.
161 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
162 EVT_MENU(Render_Load
, MyFrame::OnLoad
)
163 EVT_MENU(Render_Unload
,MyFrame::OnUnload
)
164 EVT_MENU(Render_Quit
, MyFrame::OnQuit
)
166 EVT_MENU(Render_About
, MyFrame::OnAbout
)
169 // Create a new application object: this macro will allow wxWindows to create
170 // the application object during program execution (it's better than using a
171 // static object for many reasons) and also implements the accessor function
172 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
176 // ============================================================================
178 // ============================================================================
180 // ----------------------------------------------------------------------------
181 // the application class
182 // ----------------------------------------------------------------------------
184 // 'Main program' equivalent: the program execution "starts" here
187 // create the main application window
193 // ----------------------------------------------------------------------------
195 // ----------------------------------------------------------------------------
201 _T("Render wxWindows Sample"),
205 // set the frame icon
206 SetIcon(wxICON(sample
));
210 wxMenu
*menuFile
= new wxMenu
;
211 menuFile
->Append(Render_Load
, _T("&Load renderer...\tCtrl-L"));
212 menuFile
->Append(Render_Unload
, _T("&Unload renderer\tCtrl-U"));
213 menuFile
->Append(Render_Quit
, _T("E&xit\tCtrl-Q"), _T("Quit this program"));
215 // the "About" item should be in the help menu
216 wxMenu
*helpMenu
= new wxMenu
;
217 helpMenu
->Append(Render_About
, _T("&About...\tF1"), _T("Show about dialog"));
219 // now append the freshly created menu to the menu bar...
220 wxMenuBar
*menuBar
= new wxMenuBar();
221 menuBar
->Append(menuFile
, _T("&File"));
222 menuBar
->Append(helpMenu
, _T("&Help"));
224 // ... and attach this menu bar to the frame
226 #endif // wxUSE_MENUS
228 m_panel
= new MyPanel(this);
231 // create a status bar just for fun (by default with 1 pane only)
233 SetStatusText(_T("Welcome to wxWindows!"));
234 #endif // wxUSE_STATUSBAR
241 delete wxRendererNative::Set(NULL
);
247 void MyFrame::OnLoad(wxCommandEvent
& WXUNUSED(event
))
249 static wxString s_name
= _T("renddll");
251 wxString name
= wxGetTextFromUser
253 _T("Name of the renderer to load:"),
254 _T("Render wxWindows Sample"),
266 wxRendererNative
*renderer
= wxRendererNative::Load(name
);
269 wxLogError(_T("Failed to load renderer \"%s\"."), name
.c_str());
273 delete wxRendererNative::Set(renderer
);
277 wxLogStatus(this, _T("Successfully loaded the renderer \"%s\"."),
282 void MyFrame::OnUnload(wxCommandEvent
& WXUNUSED(event
))
284 wxRendererNative
*renderer
= wxRendererNative::Set(NULL
);
291 wxLogStatus(this, _T("Unloaded the previously loaded renderer."));
295 wxLogWarning(_T("No renderer to unload."));
299 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
301 // true is to force the frame to close
305 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
307 wxMessageBox(_T("Render sample shows how to use custom renderers.\n")
309 _T("© 2003 Vadim Zeitlin"),
310 _T("About Render wxWindows Sample"),
311 wxOK
| wxICON_INFORMATION
, this);