]>
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"
39 #include "wx/apptrait.h"
40 #include "wx/renderer.h"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // the application icon (under Windows and OS/2 it is in resources)
47 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
48 #include "../sample.xpm"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 // A renderer class draws the header buttons in a "special" way
56 class MyRenderer
: public wxDelegateRendererNative
59 MyRenderer() : wxDelegateRendererNative(wxRendererNative::GetDefault()) { }
61 virtual void DrawHeaderButton(wxWindow
* WXUNUSED(win
),
64 int WXUNUSED(flags
) = 0)
66 dc
.SetBrush(*wxBLUE_BRUSH
);
67 dc
.SetTextForeground(*wxWHITE
);
68 dc
.DrawRoundedRectangle(rect
, 5);
69 dc
.DrawLabel(_T("MyRenderer"), wxNullBitmap
, rect
, wxALIGN_CENTER
);
73 // To use a different renderer from the very beginning we must override
74 // wxAppTraits method creating the renderer (another, simpler, alternative is
75 // to call wxRendererNative::Set() a.s.a.p. which should work in 99% of the
76 // cases, but we show this here just for completeness)
77 class MyTraits
: public wxGUIAppTraits
79 virtual wxRendererNative
*CreateRenderer()
81 // it will be deleted on program shutdown by wxWidgets itself
82 return new MyRenderer
;
86 // Define a new application type, each program should derive a class from wxApp
87 class MyApp
: public wxApp
90 virtual bool OnInit();
92 // if we want MyTraits to be used we must override CreateTraits()
93 virtual wxAppTraits
*CreateTraits() { return new MyTraits
; }
96 // Define a new frame type: this is going to be our main frame
97 class MyFrame
: public wxFrame
104 // event handlers (these functions should _not_ be virtual)
105 void OnLoad(wxCommandEvent
& event
);
106 void OnUnload(wxCommandEvent
& event
);
107 void OnQuit(wxCommandEvent
& event
);
108 void OnAbout(wxCommandEvent
& event
);
113 // any class wishing to process wxWidgets events must use this macro
114 DECLARE_EVENT_TABLE()
117 // a very simple class just to have something to draw on
118 class MyPanel
: public wxPanel
121 MyPanel(wxWindow
*parent
) : wxPanel(parent
) { }
123 void OnPaint(wxPaintEvent
&)
127 dc
.DrawText(_T("Below is the standard header button drawn"), 10, 10);
128 dc
.DrawText(_T("using the current renderer:"), 10, 40);
130 wxRendererNative::Get().DrawHeaderButton(this, dc
,
131 wxRect(20, 70, 100, 60));
134 DECLARE_EVENT_TABLE()
137 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
138 EVT_PAINT(MyPanel::OnPaint
)
141 // ----------------------------------------------------------------------------
143 // ----------------------------------------------------------------------------
145 // IDs for the controls and the menu commands
152 // standard menu items
153 Render_Quit
= wxID_EXIT
,
155 // it is important for the id corresponding to the "About" command to have
156 // this standard value as otherwise it won't be handled properly under Mac
157 // (where it is special and put into the "Apple" menu)
158 Render_About
= wxID_ABOUT
161 // ----------------------------------------------------------------------------
162 // event tables and other macros for wxWidgets
163 // ----------------------------------------------------------------------------
165 // the event tables connect the wxWidgets events with the functions (event
166 // handlers) which process them. It can be also done at run-time, but for the
167 // simple menu events like this the static method is much simpler.
168 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
169 EVT_MENU(Render_Load
, MyFrame::OnLoad
)
170 EVT_MENU(Render_Unload
,MyFrame::OnUnload
)
171 EVT_MENU(Render_Quit
, MyFrame::OnQuit
)
173 EVT_MENU(Render_About
, MyFrame::OnAbout
)
176 // Create a new application object: this macro will allow wxWidgets to create
177 // the application object during program execution (it's better than using a
178 // static object for many reasons) and also implements the accessor function
179 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
183 // ============================================================================
185 // ============================================================================
187 // ----------------------------------------------------------------------------
188 // the application class
189 // ----------------------------------------------------------------------------
191 // 'Main program' equivalent: the program execution "starts" here
194 // create the main application window
200 // ----------------------------------------------------------------------------
202 // ----------------------------------------------------------------------------
208 _T("Render wxWidgets Sample"),
212 // set the frame icon
213 SetIcon(wxICON(sample
));
217 wxMenu
*menuFile
= new wxMenu
;
218 menuFile
->Append(Render_Load
, _T("&Load renderer...\tCtrl-L"));
219 menuFile
->Append(Render_Unload
, _T("&Unload renderer\tCtrl-U"));
220 menuFile
->Append(Render_Quit
, _T("E&xit\tCtrl-Q"), _T("Quit this program"));
222 // the "About" item should be in the help menu
223 wxMenu
*helpMenu
= new wxMenu
;
224 helpMenu
->Append(Render_About
, _T("&About...\tF1"), _T("Show about dialog"));
226 // now append the freshly created menu to the menu bar...
227 wxMenuBar
*menuBar
= new wxMenuBar();
228 menuBar
->Append(menuFile
, _T("&File"));
229 menuBar
->Append(helpMenu
, _T("&Help"));
231 // ... and attach this menu bar to the frame
233 #endif // wxUSE_MENUS
235 m_panel
= new MyPanel(this);
238 // create a status bar just for fun (by default with 1 pane only)
240 SetStatusText(_T("Welcome to wxWidgets!"));
241 #endif // wxUSE_STATUSBAR
248 delete wxRendererNative::Set(NULL
);
254 void MyFrame::OnLoad(wxCommandEvent
& WXUNUSED(event
))
256 static wxString s_name
= _T("renddll");
258 wxString name
= wxGetTextFromUser
260 _T("Name of the renderer to load:"),
261 _T("Render wxWidgets Sample"),
273 wxRendererNative
*renderer
= wxRendererNative::Load(name
);
276 wxLogError(_T("Failed to load renderer \"%s\"."), name
.c_str());
280 delete wxRendererNative::Set(renderer
);
284 wxLogStatus(this, _T("Successfully loaded the renderer \"%s\"."),
289 void MyFrame::OnUnload(wxCommandEvent
& WXUNUSED(event
))
291 wxRendererNative
*renderer
= wxRendererNative::Set(NULL
);
298 wxLogStatus(this, _T("Unloaded the previously loaded renderer."));
302 wxLogWarning(_T("No renderer to unload."));
306 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
308 // true is to force the frame to close
312 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
314 wxMessageBox(_T("Render sample shows how to use custom renderers.\n")
316 _T("(c) 2003 Vadim Zeitlin"),
317 _T("About Render wxWidgets Sample"),
318 wxOK
| wxICON_INFORMATION
, this);