From 974b7c09902960c6d607c386be64e8ebe3883076 Mon Sep 17 00:00:00 2001 From: Stefan Ziegler Date: Mon, 3 Dec 2018 14:02:11 +0100 Subject: [PATCH] Add wxToolbook::EnablePage() Add functions to enable or disable pages inside wxToolbook. Using the new functions you can present disabled icons so that the user can expect more functionality and you do not need to add/remove pages in different states. Closes https://github.com/wxWidgets/wxWidgets/pull/1038 --- docs/changes.txt | 1 + include/wx/toolbook.h | 4 ++++ interface/wx/toolbook.h | 42 +++++++++++++++++++++++++++++++++++++++++ src/generic/toolbkg.cpp | 26 +++++++++++++++++++++++-- 4 files changed, 71 insertions(+), 2 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index b414530c9c..37e3a5e8ac 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -140,6 +140,7 @@ All (GUI): - Add wxDisplay::GetPPI(). - Add wxJoystickEvent::GetButtonOrdinal() (Mick Phillips). - Add wxGraphicsContext::GetWindow() and implement wxGraphicsContext::GetDPI(). +- Add wxToolbook::EnablePage() (Stefan Ziegler). wxGTK: diff --git a/include/wx/toolbook.h b/include/wx/toolbook.h index ecd703cfc2..ee3e018fe0 100644 --- a/include/wx/toolbook.h +++ b/include/wx/toolbook.h @@ -92,6 +92,10 @@ public: // get the underlying toolbar wxToolBarBase* GetToolBar() const { return (wxToolBarBase*)m_bookctrl; } + // enable/disable a page + bool EnablePage(wxWindow *page, bool enable); + bool EnablePage(size_t page, bool enable); + // must be called in OnIdle or by application to realize the toolbar and // select the initial page. void Realize(); diff --git a/interface/wx/toolbook.h b/interface/wx/toolbook.h index 08d1f45991..ad8447d386 100644 --- a/interface/wx/toolbook.h +++ b/interface/wx/toolbook.h @@ -23,6 +23,9 @@ wxEventType wxEVT_TOOLBOOK_PAGE_CHANGING; refer to that class documentation for now. You can also use the @ref page_samples_notebook to see wxToolbook in action. + One feature of this class not supported by wxBookCtrlBase is the support + for disabling some of the pages, see EnablePage(). + @beginStyleTable @style{wxTBK_BUTTONBAR} Use wxButtonToolBar-based implementation under OS X (ignored under @@ -82,5 +85,44 @@ public: Returns the wxToolBarBase associated with the control. */ wxToolBarBase* GetToolBar() const; + + /** + Enables or disables the specified page. + + Using this function, a page can be disabled when it can't be used, while + still remaining present to let the users know that more functionality is + available, even if currently inaccessible. + + Icons for disabled pages are created by wxBitmap::ConvertToDisabled(). + + @param page + The index of the page. + @param enable + @true to enable the page and @false to disable it. + + @return @true if successful, @false otherwise (currently only if the + index is invalid). + + @since 3.1.2 + */ + bool EnablePage(size_t page, bool enable); + + /** + Enables or disables the specified page. + + This is similar to the overload above, but finds the index of the + specified page. + + @param page + Pointer of a page windows inside the book control. + @param enable + @true to enable the page and @false to disable it. + + @return @true if successful, @false otherwise, e.g. if @a page is not + one of the pages of this control. + + @since 3.1.2 + */ + bool EnablePage(wxWindow *page, bool enable); }; diff --git a/src/generic/toolbkg.cpp b/src/generic/toolbkg.cpp index 07cd9fa962..a7ad0601fb 100644 --- a/src/generic/toolbkg.cpp +++ b/src/generic/toolbkg.cpp @@ -167,7 +167,9 @@ bool wxToolbook::SetPageImage(size_t n, int imageId) if (!GetImageList()) return false; - GetToolBar()->SetToolNormalBitmap(n + 1, GetImageList()->GetBitmap(imageId)); + wxBitmap bmp = GetImageList()->GetBitmap(imageId); + GetToolBar()->SetToolNormalBitmap(n + 1, bmp); + GetToolBar()->SetToolDisabledBitmap(n + 1, bmp.ConvertToDisabled()); return true; } @@ -301,7 +303,7 @@ bool wxToolbook::InsertPage(size_t n, m_maxBitmapSize.y = wxMax(bitmap.GetHeight(), m_maxBitmapSize.y); GetToolBar()->SetToolBitmapSize(m_maxBitmapSize); - GetToolBar()->AddRadioTool(n + 1, text, bitmap, wxNullBitmap, text); + GetToolBar()->AddRadioTool(n + 1, text, bitmap, bitmap.ConvertToDisabled(), text); if (bSelect) { @@ -336,6 +338,26 @@ bool wxToolbook::DeleteAllPages() return wxBookCtrlBase::DeleteAllPages(); } +bool wxToolbook::EnablePage(size_t page, bool enable) +{ + GetToolBar()->EnableTool(page + 1, enable); + if (!enable && GetSelection() == (int)page) + { + AdvanceSelection(); + } + return true; +} + +bool wxToolbook::EnablePage(wxWindow *page, bool enable) +{ + const int pageIndex = FindPage(page); + if (pageIndex == wxNOT_FOUND) + { + return false; + } + return EnablePage(pageIndex, enable); +} + // ---------------------------------------------------------------------------- // wxToolbook events // ----------------------------------------------------------------------------