| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: render.cpp |
| 3 | // Purpose: Render wxWidgets sample |
| 4 | // Author: Vadim Zeitlin |
| 5 | // Modified by: |
| 6 | // Created: 04.08.03 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org> |
| 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" |
| 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" |
| 37 | #include "wx/icon.h" |
| 38 | #include "wx/image.h" |
| 39 | #endif |
| 40 | |
| 41 | #include "wx/apptrait.h" |
| 42 | #include "wx/renderer.h" |
| 43 | |
| 44 | // ---------------------------------------------------------------------------- |
| 45 | // resources |
| 46 | // ---------------------------------------------------------------------------- |
| 47 | |
| 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" |
| 51 | #endif |
| 52 | |
| 53 | // ---------------------------------------------------------------------------- |
| 54 | // private classes |
| 55 | // ---------------------------------------------------------------------------- |
| 56 | |
| 57 | // A renderer class draws the header buttons in a "special" way |
| 58 | class MyRenderer : public wxDelegateRendererNative |
| 59 | { |
| 60 | public: |
| 61 | MyRenderer() : wxDelegateRendererNative(wxRendererNative::GetDefault()) { } |
| 62 | |
| 63 | virtual int DrawHeaderButton(wxWindow *WXUNUSED(win), |
| 64 | wxDC& dc, |
| 65 | const wxRect& rect, |
| 66 | int WXUNUSED(flags) = 0, |
| 67 | wxHeaderSortIconType WXUNUSED(sortArrow) = wxHDR_SORT_ICON_NONE, |
| 68 | wxHeaderButtonParams* WXUNUSED(params) = NULL) |
| 69 | { |
| 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); |
| 74 | return rect.width; |
| 75 | } |
| 76 | }; |
| 77 | |
| 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 |
| 83 | { |
| 84 | virtual wxRendererNative *CreateRenderer() |
| 85 | { |
| 86 | // it will be deleted on program shutdown by wxWidgets itself |
| 87 | return new MyRenderer; |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | // Define a new application type, each program should derive a class from wxApp |
| 92 | class MyApp : public wxApp |
| 93 | { |
| 94 | public: |
| 95 | virtual bool OnInit(); |
| 96 | |
| 97 | // if we want MyTraits to be used we must override CreateTraits() |
| 98 | virtual wxAppTraits *CreateTraits() { return new MyTraits; } |
| 99 | }; |
| 100 | |
| 101 | // Define a new frame type: this is going to be our main frame |
| 102 | class MyFrame : public wxFrame |
| 103 | { |
| 104 | public: |
| 105 | // ctor(s) |
| 106 | MyFrame(); |
| 107 | virtual ~MyFrame(); |
| 108 | |
| 109 | private: |
| 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); } |
| 121 | |
| 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); |
| 128 | |
| 129 | void OnToggleDrawFlag(wxCommandEvent& event, int flag); |
| 130 | |
| 131 | class MyPanel *m_panel; |
| 132 | |
| 133 | // any class wishing to process wxWidgets events must use this macro |
| 134 | DECLARE_EVENT_TABLE() |
| 135 | }; |
| 136 | |
| 137 | // a very simple class just to have something to draw on |
| 138 | class MyPanel : public wxPanel |
| 139 | { |
| 140 | public: |
| 141 | MyPanel(wxWindow *parent) : wxPanel(parent) { m_flags = 0; } |
| 142 | |
| 143 | int GetFlags() const { return m_flags; } |
| 144 | void SetFlags(int flags) { m_flags = flags; } |
| 145 | |
| 146 | private: |
| 147 | void OnPaint(wxPaintEvent&) |
| 148 | { |
| 149 | wxPaintDC dc(this); |
| 150 | |
| 151 | wxRendererNative& renderer = wxRendererNative::Get(); |
| 152 | |
| 153 | int x1 = 10, // text offset |
| 154 | x2 = 200, // drawing offset |
| 155 | y = 10; |
| 156 | |
| 157 | const int lineHeight = dc.GetCharHeight(); |
| 158 | dc.DrawText("Demonstration of various wxRenderer functions:", x1, y); |
| 159 | y += lineHeight; |
| 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); |
| 174 | y += lineHeight*3; |
| 175 | |
| 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; |
| 181 | |
| 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; |
| 187 | |
| 188 | dc.DrawText("DrawRadioBitmap()", x1, y); |
| 189 | renderer.DrawRadioBitmap(this, dc, |
| 190 | wxRect(wxPoint(x2, y), sizeCheck), m_flags); |
| 191 | y += lineHeight + sizeCheck.y; |
| 192 | |
| 193 | dc.DrawText("DrawTreeItemButton()", x1, y); |
| 194 | renderer.DrawTreeItemButton(this, dc, |
| 195 | wxRect(x2, y, 20, 20), m_flags); |
| 196 | y += lineHeight + 20; |
| 197 | |
| 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); |
| 215 | |
| 216 | y += lineHeight + rBtn.height; |
| 217 | #endif // wxHAS_DRAW_TITLE_BAR_BITMAP |
| 218 | } |
| 219 | |
| 220 | int m_flags; |
| 221 | |
| 222 | DECLARE_EVENT_TABLE() |
| 223 | }; |
| 224 | |
| 225 | BEGIN_EVENT_TABLE(MyPanel, wxPanel) |
| 226 | EVT_PAINT(MyPanel::OnPaint) |
| 227 | END_EVENT_TABLE() |
| 228 | |
| 229 | // ---------------------------------------------------------------------------- |
| 230 | // constants |
| 231 | // ---------------------------------------------------------------------------- |
| 232 | |
| 233 | // IDs for the controls and the menu commands |
| 234 | enum |
| 235 | { |
| 236 | // our menu items |
| 237 | Render_DrawDisabled = 100, |
| 238 | Render_DrawFocused, |
| 239 | Render_DrawPressed, |
| 240 | Render_DrawChecked, |
| 241 | Render_DrawHot, |
| 242 | |
| 243 | #if wxUSE_DYNLIB_CLASS |
| 244 | Render_Load, |
| 245 | Render_Unload, |
| 246 | #endif // wxUSE_DYNLIB_CLASS |
| 247 | |
| 248 | // standard menu items |
| 249 | Render_Quit = wxID_EXIT, |
| 250 | |
| 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 |
| 255 | }; |
| 256 | |
| 257 | // ---------------------------------------------------------------------------- |
| 258 | // event tables and other macros for wxWidgets |
| 259 | // ---------------------------------------------------------------------------- |
| 260 | |
| 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) |
| 270 | |
| 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) |
| 276 | |
| 277 | EVT_MENU(Render_About, MyFrame::OnAbout) |
| 278 | END_EVENT_TABLE() |
| 279 | |
| 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 |
| 284 | // not wxApp) |
| 285 | IMPLEMENT_APP(MyApp) |
| 286 | |
| 287 | // ============================================================================ |
| 288 | // implementation |
| 289 | // ============================================================================ |
| 290 | |
| 291 | // ---------------------------------------------------------------------------- |
| 292 | // the application class |
| 293 | // ---------------------------------------------------------------------------- |
| 294 | |
| 295 | // 'Main program' equivalent: the program execution "starts" here |
| 296 | bool MyApp::OnInit() |
| 297 | { |
| 298 | if ( !wxApp::OnInit() ) |
| 299 | return false; |
| 300 | |
| 301 | #ifdef __WXOSX__ |
| 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 |
| 304 | // this function |
| 305 | wxImage::AddHandler(new wxPNGHandler); |
| 306 | #endif // OS X |
| 307 | |
| 308 | // create the main application window |
| 309 | new MyFrame; |
| 310 | |
| 311 | return true; |
| 312 | } |
| 313 | |
| 314 | // ---------------------------------------------------------------------------- |
| 315 | // main frame |
| 316 | // ---------------------------------------------------------------------------- |
| 317 | |
| 318 | // frame constructor |
| 319 | MyFrame::MyFrame() |
| 320 | : wxFrame(NULL, |
| 321 | wxID_ANY, |
| 322 | wxT("Render wxWidgets Sample"), |
| 323 | wxPoint(50, 50), |
| 324 | wxSize(450, 340)) |
| 325 | { |
| 326 | // set the frame icon |
| 327 | SetIcon(wxICON(sample)); |
| 328 | |
| 329 | #if wxUSE_MENUS |
| 330 | // create a menu bar |
| 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); |
| 349 | |
| 350 | // the "About" item should be in the help menu |
| 351 | wxMenu *helpMenu = new wxMenu; |
| 352 | helpMenu->Append(Render_About); |
| 353 | |
| 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")); |
| 358 | |
| 359 | // ... and attach this menu bar to the frame |
| 360 | SetMenuBar(menuBar); |
| 361 | #endif // wxUSE_MENUS |
| 362 | |
| 363 | m_panel = new MyPanel(this); |
| 364 | |
| 365 | #if wxUSE_STATUSBAR |
| 366 | // create a status bar just for fun (by default with 1 pane only) |
| 367 | CreateStatusBar(2); |
| 368 | SetStatusText(wxT("Welcome to wxWidgets!")); |
| 369 | #endif // wxUSE_STATUSBAR |
| 370 | |
| 371 | Show(); |
| 372 | } |
| 373 | |
| 374 | MyFrame::~MyFrame() |
| 375 | { |
| 376 | delete wxRendererNative::Set(NULL); |
| 377 | } |
| 378 | |
| 379 | |
| 380 | // event handlers |
| 381 | |
| 382 | void MyFrame::OnToggleDrawFlag(wxCommandEvent& event, int flag) |
| 383 | { |
| 384 | int flags = m_panel->GetFlags(); |
| 385 | if ( event.IsChecked() ) |
| 386 | flags |= flag; |
| 387 | else |
| 388 | flags &= ~flag; |
| 389 | |
| 390 | m_panel->SetFlags(flags); |
| 391 | m_panel->Refresh(); |
| 392 | } |
| 393 | |
| 394 | #if wxUSE_DYNLIB_CLASS |
| 395 | |
| 396 | void MyFrame::OnLoad(wxCommandEvent& WXUNUSED(event)) |
| 397 | { |
| 398 | static wxString s_name = wxT("renddll"); |
| 399 | |
| 400 | wxString name = wxGetTextFromUser |
| 401 | ( |
| 402 | wxT("Name of the renderer to load:"), |
| 403 | wxT("Render wxWidgets Sample"), |
| 404 | s_name, |
| 405 | this |
| 406 | ); |
| 407 | if ( name.empty() ) |
| 408 | { |
| 409 | // cancelled |
| 410 | return; |
| 411 | } |
| 412 | |
| 413 | s_name = name; |
| 414 | |
| 415 | wxRendererNative *renderer = wxRendererNative::Load(name); |
| 416 | if ( !renderer ) |
| 417 | { |
| 418 | wxLogError(wxT("Failed to load renderer \"%s\"."), name.c_str()); |
| 419 | } |
| 420 | else // loaded ok |
| 421 | { |
| 422 | delete wxRendererNative::Set(renderer); |
| 423 | |
| 424 | m_panel->Refresh(); |
| 425 | |
| 426 | wxLogStatus(this, wxT("Successfully loaded the renderer \"%s\"."), |
| 427 | name.c_str()); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | void MyFrame::OnUnload(wxCommandEvent& WXUNUSED(event)) |
| 432 | { |
| 433 | wxRendererNative *renderer = wxRendererNative::Set(NULL); |
| 434 | if ( renderer ) |
| 435 | { |
| 436 | delete renderer; |
| 437 | |
| 438 | m_panel->Refresh(); |
| 439 | |
| 440 | wxLogStatus(this, wxT("Unloaded the previously loaded renderer.")); |
| 441 | } |
| 442 | else |
| 443 | { |
| 444 | wxLogWarning(wxT("No renderer to unload.")); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | #endif // wxUSE_DYNLIB_CLASS |
| 449 | |
| 450 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
| 451 | { |
| 452 | // true is to force the frame to close |
| 453 | Close(true); |
| 454 | } |
| 455 | |
| 456 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
| 457 | { |
| 458 | wxMessageBox(wxT("Render sample shows how to use custom renderers.\n") |
| 459 | wxT("\n") |
| 460 | wxT("(c) 2003 Vadim Zeitlin"), |
| 461 | wxT("About Render wxWidgets Sample"), |
| 462 | wxOK | wxICON_INFORMATION, this); |
| 463 | } |
| 464 | |