X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/9c743a7c783b9c21e049fd88445d4051c99ce2db..2439f1d96880ed2fd1551b85795ccee1704dc29c:/samples/render/render.cpp diff --git a/samples/render/render.cpp b/samples/render/render.cpp index b14b0ffc18..7cb63278be 100644 --- a/samples/render/render.cpp +++ b/samples/render/render.cpp @@ -1,11 +1,11 @@ ///////////////////////////////////////////////////////////////////////////// // Name: render.cpp -// Purpose: Render wxWindows sample +// Purpose: Render wxWidgets sample // Author: Vadim Zeitlin // Modified by: // Created: 04.08.03 // RCS-ID: $Id$ -// Copyright: (c) 2003 Vadim Zeitlin +// Copyright: (c) 2003 Vadim Zeitlin // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -34,6 +34,8 @@ #include "wx/textdlg.h" #include "wx/log.h" #include "wx/msgdlg.h" + #include "wx/icon.h" + #include "wx/image.h" #endif #include "wx/apptrait.h" @@ -58,15 +60,18 @@ class MyRenderer : public wxDelegateRendererNative public: MyRenderer() : wxDelegateRendererNative(wxRendererNative::GetDefault()) { } - virtual void DrawHeaderButton(wxWindow * WXUNUSED(win), + virtual int DrawHeaderButton(wxWindow *WXUNUSED(win), wxDC& dc, const wxRect& rect, - int WXUNUSED(flags) = 0) + int WXUNUSED(flags) = 0, + wxHeaderSortIconType WXUNUSED(sortArrow) = wxHDR_SORT_ICON_NONE, + wxHeaderButtonParams* WXUNUSED(params) = NULL) { - dc.SetBrush(*wxBLUE_BRUSH); - dc.SetTextForeground(*wxWHITE); + wxDCBrushChanger setBrush(dc, *wxBLUE_BRUSH); + wxDCTextColourChanger setFgCol(dc, *wxWHITE); dc.DrawRoundedRectangle(rect, 5); - dc.DrawLabel(_T("MyRenderer"), wxNullBitmap, rect, wxALIGN_CENTER); + dc.DrawLabel(wxT("MyRenderer"), wxNullBitmap, rect, wxALIGN_CENTER); + return rect.width; } }; @@ -78,7 +83,7 @@ class MyTraits : public wxGUIAppTraits { virtual wxRendererNative *CreateRenderer() { - // it will be deleted on program shutdown by wxWindows itself + // it will be deleted on program shutdown by wxWidgets itself return new MyRenderer; } }; @@ -101,16 +106,31 @@ public: MyFrame(); virtual ~MyFrame(); +private: // event handlers (these functions should _not_ be virtual) + void OnDrawDisabled(wxCommandEvent& event) + { OnToggleDrawFlag(event, wxCONTROL_DISABLED); } + void OnDrawFocused(wxCommandEvent& event) + { OnToggleDrawFlag(event, wxCONTROL_FOCUSED); } + void OnDrawPressed(wxCommandEvent& event) + { OnToggleDrawFlag(event, wxCONTROL_PRESSED); } + void OnDrawChecked(wxCommandEvent& event) + { OnToggleDrawFlag(event, wxCONTROL_CHECKED); } + void OnDrawHot(wxCommandEvent& event) + { OnToggleDrawFlag(event, wxCONTROL_CURRENT); } + +#if wxUSE_DYNLIB_CLASS void OnLoad(wxCommandEvent& event); void OnUnload(wxCommandEvent& event); +#endif // wxUSE_DYNLIB_CLASS void OnQuit(wxCommandEvent& event); void OnAbout(wxCommandEvent& event); -private: - wxPanel *m_panel; + void OnToggleDrawFlag(wxCommandEvent& event, int flag); + + class MyPanel *m_panel; - // any class wishing to process wxWindows events must use this macro + // any class wishing to process wxWidgets events must use this macro DECLARE_EVENT_TABLE() }; @@ -118,19 +138,87 @@ private: class MyPanel : public wxPanel { public: - MyPanel(wxWindow *parent) : wxPanel(parent) { } + MyPanel(wxWindow *parent) : wxPanel(parent) { m_flags = 0; } + int GetFlags() const { return m_flags; } + void SetFlags(int flags) { m_flags = flags; } + +private: void OnPaint(wxPaintEvent&) { wxPaintDC dc(this); - dc.DrawText(_T("Below is the standard header button drawn"), 10, 10); - dc.DrawText(_T("using the current renderer:"), 10, 40); - - wxRendererNative::Get().DrawHeaderButton(this, dc, - wxRect(20, 70, 100, 60)); + wxRendererNative& renderer = wxRendererNative::Get(); + + int x1 = 10, // text offset + x2 = 200, // drawing offset + y = 10; + + const int lineHeight = dc.GetCharHeight(); + dc.DrawText("Demonstration of various wxRenderer functions:", x1, y); + y += lineHeight; + wxString flagsString; + if ( m_flags & wxCONTROL_DISABLED ) + flagsString += "wxCONTROL_DISABLED "; + if ( m_flags & wxCONTROL_FOCUSED ) + flagsString += "wxCONTROL_FOCUSED "; + if ( m_flags & wxCONTROL_PRESSED ) + flagsString += "wxCONTROL_PRESSED "; + if ( m_flags & wxCONTROL_CURRENT ) + flagsString += "wxCONTROL_CURRENT "; + if ( m_flags & wxCONTROL_CHECKED ) + flagsString += "wxCONTROL_CHECKED "; + if ( flagsString.empty() ) + flagsString = "(none)"; + dc.DrawText("Using flags: " + flagsString, x1, y); + y += lineHeight*3; + + dc.DrawText("DrawHeaderButton() (overridden)", x1, y); + const wxCoord heightHdr = renderer.GetHeaderButtonHeight(this); + renderer.DrawHeaderButton(this, dc, + wxRect(x2, y, 100, heightHdr), m_flags); + y += lineHeight + heightHdr; + + dc.DrawText("DrawCheckBox()", x1, y); + const wxSize sizeCheck = renderer.GetCheckBoxSize(this); + renderer.DrawCheckBox(this, dc, + wxRect(wxPoint(x2, y), sizeCheck), m_flags); + y += lineHeight + sizeCheck.y; + + dc.DrawText("DrawRadioBitmap()", x1, y); + renderer.DrawRadioBitmap(this, dc, + wxRect(wxPoint(x2, y), sizeCheck), m_flags); + y += lineHeight + sizeCheck.y; + + dc.DrawText("DrawTreeItemButton()", x1, y); + renderer.DrawTreeItemButton(this, dc, + wxRect(x2, y, 20, 20), m_flags); + y += lineHeight + 20; + +#ifdef wxHAS_DRAW_TITLE_BAR_BITMAP + dc.DrawText("DrawTitleBarBitmap()", x1, y); + wxRect rBtn(x2, y, 21, 21); + renderer.DrawTitleBarBitmap(this, dc, rBtn, + wxTITLEBAR_BUTTON_HELP, m_flags); + rBtn.x += 2*rBtn.width; + renderer.DrawTitleBarBitmap(this, dc, rBtn, + wxTITLEBAR_BUTTON_ICONIZE, m_flags); + rBtn.x += 2*rBtn.width; + renderer.DrawTitleBarBitmap(this, dc, rBtn, + wxTITLEBAR_BUTTON_RESTORE, m_flags); + rBtn.x += 2*rBtn.width; + renderer.DrawTitleBarBitmap(this, dc, rBtn, + wxTITLEBAR_BUTTON_MAXIMIZE, m_flags); + rBtn.x += 2*rBtn.width; + renderer.DrawTitleBarBitmap(this, dc, rBtn, + wxTITLEBAR_BUTTON_CLOSE, m_flags); + + y += lineHeight + rBtn.height; +#endif // wxHAS_DRAW_TITLE_BAR_BITMAP } + int m_flags; + DECLARE_EVENT_TABLE() }; @@ -146,8 +234,16 @@ END_EVENT_TABLE() enum { // our menu items - Render_Load = 100, + Render_DrawDisabled = 100, + Render_DrawFocused, + Render_DrawPressed, + Render_DrawChecked, + Render_DrawHot, + +#if wxUSE_DYNLIB_CLASS + Render_Load, Render_Unload, +#endif // wxUSE_DYNLIB_CLASS // standard menu items Render_Quit = wxID_EXIT, @@ -159,21 +255,29 @@ enum }; // ---------------------------------------------------------------------------- -// event tables and other macros for wxWindows +// event tables and other macros for wxWidgets // ---------------------------------------------------------------------------- -// the event tables connect the wxWindows events with the functions (event +// the event tables connect the wxWidgets events with the functions (event // handlers) which process them. It can be also done at run-time, but for the // simple menu events like this the static method is much simpler. BEGIN_EVENT_TABLE(MyFrame, wxFrame) + EVT_MENU(Render_DrawDisabled, MyFrame::OnDrawDisabled) + EVT_MENU(Render_DrawFocused, MyFrame::OnDrawFocused) + EVT_MENU(Render_DrawPressed, MyFrame::OnDrawPressed) + EVT_MENU(Render_DrawChecked, MyFrame::OnDrawChecked) + EVT_MENU(Render_DrawHot, MyFrame::OnDrawHot) + +#if wxUSE_DYNLIB_CLASS EVT_MENU(Render_Load, MyFrame::OnLoad) EVT_MENU(Render_Unload,MyFrame::OnUnload) +#endif // wxUSE_DYNLIB_CLASS EVT_MENU(Render_Quit, MyFrame::OnQuit) EVT_MENU(Render_About, MyFrame::OnAbout) END_EVENT_TABLE() -// Create a new application object: this macro will allow wxWindows to create +// Create a new application object: this macro will allow wxWidgets to create // the application object during program execution (it's better than using a // static object for many reasons) and also implements the accessor function // wxGetApp() which will return the reference of the right type (i.e. MyApp and @@ -191,6 +295,16 @@ IMPLEMENT_APP(MyApp) // 'Main program' equivalent: the program execution "starts" here bool MyApp::OnInit() { + if ( !wxApp::OnInit() ) + return false; + +#ifdef __WXOSX__ + // currently the images used by DrawTitleBarBitmap() are hard coded as PNG + // images inside the library itself so we need to enable PNG support to use + // this function + wxImage::AddHandler(new wxPNGHandler); +#endif // OS X + // create the main application window new MyFrame; @@ -204,8 +318,8 @@ bool MyApp::OnInit() // frame constructor MyFrame::MyFrame() : wxFrame(NULL, - -1, - _T("Render wxWindows Sample"), + wxID_ANY, + wxT("Render wxWidgets Sample"), wxPoint(50, 50), wxSize(450, 340)) { @@ -215,18 +329,32 @@ MyFrame::MyFrame() #if wxUSE_MENUS // create a menu bar wxMenu *menuFile = new wxMenu; - menuFile->Append(Render_Load, _T("&Load renderer...\tCtrl-L")); - menuFile->Append(Render_Unload, _T("&Unload renderer\tCtrl-U")); - menuFile->Append(Render_Quit, _T("E&xit\tCtrl-Q"), _T("Quit this program")); + menuFile->AppendCheckItem(Render_DrawDisabled, + "Draw in &disabled state\tCtrl-D"); + menuFile->AppendCheckItem(Render_DrawFocused, + "Draw in &focused state\tCtrl-F"); + menuFile->AppendCheckItem(Render_DrawPressed, + "Draw in &pressed state\tCtrl-P"); + menuFile->AppendCheckItem(Render_DrawChecked, + "Draw in &checked state\tCtrl-C"); + menuFile->AppendCheckItem(Render_DrawHot, + "Draw in &hot state\tCtrl-H"); + menuFile->AppendSeparator(); +#if wxUSE_DYNLIB_CLASS + menuFile->Append(Render_Load, wxT("&Load renderer...\tCtrl-L")); + menuFile->Append(Render_Unload, wxT("&Unload renderer\tCtrl-U")); + menuFile->AppendSeparator(); +#endif // wxUSE_DYNLIB_CLASS + menuFile->Append(Render_Quit); // the "About" item should be in the help menu wxMenu *helpMenu = new wxMenu; - helpMenu->Append(Render_About, _T("&About...\tF1"), _T("Show about dialog")); + helpMenu->Append(Render_About); // now append the freshly created menu to the menu bar... wxMenuBar *menuBar = new wxMenuBar(); - menuBar->Append(menuFile, _T("&File")); - menuBar->Append(helpMenu, _T("&Help")); + menuBar->Append(menuFile, wxT("&File")); + menuBar->Append(helpMenu, wxT("&Help")); // ... and attach this menu bar to the frame SetMenuBar(menuBar); @@ -237,7 +365,7 @@ MyFrame::MyFrame() #if wxUSE_STATUSBAR // create a status bar just for fun (by default with 1 pane only) CreateStatusBar(2); - SetStatusText(_T("Welcome to wxWindows!")); + SetStatusText(wxT("Welcome to wxWidgets!")); #endif // wxUSE_STATUSBAR Show(); @@ -251,14 +379,28 @@ MyFrame::~MyFrame() // event handlers +void MyFrame::OnToggleDrawFlag(wxCommandEvent& event, int flag) +{ + int flags = m_panel->GetFlags(); + if ( event.IsChecked() ) + flags |= flag; + else + flags &= ~flag; + + m_panel->SetFlags(flags); + m_panel->Refresh(); +} + +#if wxUSE_DYNLIB_CLASS + void MyFrame::OnLoad(wxCommandEvent& WXUNUSED(event)) { - static wxString s_name = _T("renddll"); + static wxString s_name = wxT("renddll"); wxString name = wxGetTextFromUser ( - _T("Name of the renderer to load:"), - _T("Render wxWindows Sample"), + wxT("Name of the renderer to load:"), + wxT("Render wxWidgets Sample"), s_name, this ); @@ -273,7 +415,7 @@ void MyFrame::OnLoad(wxCommandEvent& WXUNUSED(event)) wxRendererNative *renderer = wxRendererNative::Load(name); if ( !renderer ) { - wxLogError(_T("Failed to load renderer \"%s\"."), name.c_str()); + wxLogError(wxT("Failed to load renderer \"%s\"."), name.c_str()); } else // loaded ok { @@ -281,7 +423,7 @@ void MyFrame::OnLoad(wxCommandEvent& WXUNUSED(event)) m_panel->Refresh(); - wxLogStatus(this, _T("Successfully loaded the renderer \"%s\"."), + wxLogStatus(this, wxT("Successfully loaded the renderer \"%s\"."), name.c_str()); } } @@ -295,14 +437,16 @@ void MyFrame::OnUnload(wxCommandEvent& WXUNUSED(event)) m_panel->Refresh(); - wxLogStatus(this, _T("Unloaded the previously loaded renderer.")); + wxLogStatus(this, wxT("Unloaded the previously loaded renderer.")); } else { - wxLogWarning(_T("No renderer to unload.")); + wxLogWarning(wxT("No renderer to unload.")); } } +#endif // wxUSE_DYNLIB_CLASS + void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) { // true is to force the frame to close @@ -311,10 +455,10 @@ void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) { - wxMessageBox(_T("Render sample shows how to use custom renderers.\n") - _T("\n") - _T("© 2003 Vadim Zeitlin"), - _T("About Render wxWindows Sample"), + wxMessageBox(wxT("Render sample shows how to use custom renderers.\n") + wxT("\n") + wxT("(c) 2003 Vadim Zeitlin"), + wxT("About Render wxWidgets Sample"), wxOK | wxICON_INFORMATION, this); }