]>
git.saurik.com Git - wxWidgets.git/blob - samples/render/render.cpp
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"
40 #include "wx/apptrait.h"
41 #include "wx/renderer.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
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"
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 // A renderer class draws the header buttons in a "special" way
57 class MyRenderer
: public wxDelegateRendererNative
60 MyRenderer() : wxDelegateRendererNative(wxRendererNative::GetDefault()) { }
62 virtual void DrawHeaderButton(wxWindow
* WXUNUSED(win
),
65 int WXUNUSED(flags
) = 0)
67 dc
.SetBrush(*wxBLUE_BRUSH
);
68 dc
.SetTextForeground(*wxWHITE
);
69 dc
.DrawRoundedRectangle(rect
, 5);
70 dc
.DrawLabel(_T("MyRenderer"), wxNullBitmap
, rect
, wxALIGN_CENTER
);
74 // To use a different renderer from the very beginning we must override
75 // wxAppTraits method creating the renderer (another, simpler, alternative is
76 // to call wxRendererNative::Set() a.s.a.p. which should work in 99% of the
77 // cases, but we show this here just for completeness)
78 class MyTraits
: public wxGUIAppTraits
80 virtual wxRendererNative
*CreateRenderer()
82 // it will be deleted on program shutdown by wxWidgets itself
83 return new MyRenderer
;
87 // Define a new application type, each program should derive a class from wxApp
88 class MyApp
: public wxApp
91 virtual bool OnInit();
93 // if we want MyTraits to be used we must override CreateTraits()
94 virtual wxAppTraits
*CreateTraits() { return new MyTraits
; }
97 // Define a new frame type: this is going to be our main frame
98 class MyFrame
: public wxFrame
105 // event handlers (these functions should _not_ be virtual)
106 void OnLoad(wxCommandEvent
& event
);
107 void OnUnload(wxCommandEvent
& event
);
108 void OnQuit(wxCommandEvent
& event
);
109 void OnAbout(wxCommandEvent
& event
);
114 // any class wishing to process wxWidgets events must use this macro
115 DECLARE_EVENT_TABLE()
118 // a very simple class just to have something to draw on
119 class MyPanel
: public wxPanel
122 MyPanel(wxWindow
*parent
) : wxPanel(parent
) { }
124 void OnPaint(wxPaintEvent
&)
128 dc
.DrawText(_T("Below is the standard header button drawn"), 10, 10);
129 dc
.DrawText(_T("using the current renderer:"), 10, 40);
131 wxRendererNative::Get().DrawHeaderButton(this, dc
,
132 wxRect(20, 70, 100, 60));
135 DECLARE_EVENT_TABLE()
138 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
139 EVT_PAINT(MyPanel::OnPaint
)
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
146 // IDs for the controls and the menu commands
153 // standard menu items
154 Render_Quit
= wxID_EXIT
,
156 // it is important for the id corresponding to the "About" command to have
157 // this standard value as otherwise it won't be handled properly under Mac
158 // (where it is special and put into the "Apple" menu)
159 Render_About
= wxID_ABOUT
162 // ----------------------------------------------------------------------------
163 // event tables and other macros for wxWidgets
164 // ----------------------------------------------------------------------------
166 // the event tables connect the wxWidgets events with the functions (event
167 // handlers) which process them. It can be also done at run-time, but for the
168 // simple menu events like this the static method is much simpler.
169 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
170 EVT_MENU(Render_Load
, MyFrame::OnLoad
)
171 EVT_MENU(Render_Unload
,MyFrame::OnUnload
)
172 EVT_MENU(Render_Quit
, MyFrame::OnQuit
)
174 EVT_MENU(Render_About
, MyFrame::OnAbout
)
177 // Create a new application object: this macro will allow wxWidgets to create
178 // the application object during program execution (it's better than using a
179 // static object for many reasons) and also implements the accessor function
180 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
184 // ============================================================================
186 // ============================================================================
188 // ----------------------------------------------------------------------------
189 // the application class
190 // ----------------------------------------------------------------------------
192 // 'Main program' equivalent: the program execution "starts" here
195 // create the main application window
201 // ----------------------------------------------------------------------------
203 // ----------------------------------------------------------------------------
209 _T("Render wxWidgets Sample"),
213 // set the frame icon
214 SetIcon(wxICON(sample
));
218 wxMenu
*menuFile
= new wxMenu
;
219 menuFile
->Append(Render_Load
, _T("&Load renderer...\tCtrl-L"));
220 menuFile
->Append(Render_Unload
, _T("&Unload renderer\tCtrl-U"));
221 menuFile
->Append(Render_Quit
, _T("E&xit\tCtrl-Q"), _T("Quit this program"));
223 // the "About" item should be in the help menu
224 wxMenu
*helpMenu
= new wxMenu
;
225 helpMenu
->Append(Render_About
, _T("&About...\tF1"), _T("Show about dialog"));
227 // now append the freshly created menu to the menu bar...
228 wxMenuBar
*menuBar
= new wxMenuBar();
229 menuBar
->Append(menuFile
, _T("&File"));
230 menuBar
->Append(helpMenu
, _T("&Help"));
232 // ... and attach this menu bar to the frame
234 #endif // wxUSE_MENUS
236 m_panel
= new MyPanel(this);
239 // create a status bar just for fun (by default with 1 pane only)
241 SetStatusText(_T("Welcome to wxWidgets!"));
242 #endif // wxUSE_STATUSBAR
249 delete wxRendererNative::Set(NULL
);
255 void MyFrame::OnLoad(wxCommandEvent
& WXUNUSED(event
))
257 static wxString s_name
= _T("renddll");
259 wxString name
= wxGetTextFromUser
261 _T("Name of the renderer to load:"),
262 _T("Render wxWidgets Sample"),
274 wxRendererNative
*renderer
= wxRendererNative::Load(name
);
277 wxLogError(_T("Failed to load renderer \"%s\"."), name
.c_str());
281 delete wxRendererNative::Set(renderer
);
285 wxLogStatus(this, _T("Successfully loaded the renderer \"%s\"."),
290 void MyFrame::OnUnload(wxCommandEvent
& WXUNUSED(event
))
292 wxRendererNative
*renderer
= wxRendererNative::Set(NULL
);
299 wxLogStatus(this, _T("Unloaded the previously loaded renderer."));
303 wxLogWarning(_T("No renderer to unload."));
307 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
309 // true is to force the frame to close
313 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
315 wxMessageBox(_T("Render sample shows how to use custom renderers.\n")
317 _T("(c) 2003 Vadim Zeitlin"),
318 _T("About Render wxWidgets Sample"),
319 wxOK
| wxICON_INFORMATION
, this);