X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/3c55b3652b16f892e9064460bbeea94769f3d27c..8a28cd66a63ce30c75173e10948e9d49d366d218:/src/generic/toolbkg.cpp diff --git a/src/generic/toolbkg.cpp b/src/generic/toolbkg.cpp index 611db38af5..f324ea96db 100644 --- a/src/generic/toolbkg.cpp +++ b/src/generic/toolbkg.cpp @@ -9,14 +9,6 @@ // Licence: wxWindows licence /////////////////////////////////////////////////////////////////////////////// -// ============================================================================ -// declarations -// ============================================================================ - -// ---------------------------------------------------------------------------- -// headers -// ---------------------------------------------------------------------------- - // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" @@ -24,12 +16,21 @@ #pragma hdrstop #endif -#if wxUSE_LISTBOOK +#if wxUSE_TOOLBOOK -#include "wx/toolbar.h" -#include "wx/toolbook.h" -#include "wx/settings.h" +#ifndef WX_PRECOMP + #include "wx/icon.h" + #include "wx/settings.h" + #include "wx/toolbar.h" +#endif + +#include "wx/imaglist.h" #include "wx/sysopt.h" +#include "wx/toolbook.h" + +#if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON +#include "wx/generic/buttonbar.h" +#endif // ---------------------------------------------------------------------------- // various wxWidgets macros @@ -43,11 +44,9 @@ // ---------------------------------------------------------------------------- IMPLEMENT_DYNAMIC_CLASS(wxToolbook, wxBookCtrlBase) -IMPLEMENT_DYNAMIC_CLASS(wxToolbookEvent, wxNotifyEvent) -const wxEventType wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING = wxNewEventType(); -const wxEventType wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED = wxNewEventType(); -const int wxID_TOOLBOOKTOOLBAR = wxNewId(); +wxDEFINE_EVENT( wxEVT_TOOLBOOK_PAGE_CHANGING, wxBookCtrlEvent ); +wxDEFINE_EVENT( wxEVT_TOOLBOOK_PAGE_CHANGED, wxBookCtrlEvent ); BEGIN_EVENT_TABLE(wxToolbook, wxBookCtrlBase) EVT_SIZE(wxToolbook::OnSize) @@ -65,12 +64,10 @@ END_EVENT_TABLE() void wxToolbook::Init() { - m_selection = wxNOT_FOUND; m_needsRealizing = false; } -bool -wxToolbook::Create(wxWindow *parent, +bool wxToolbook::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, @@ -78,9 +75,7 @@ wxToolbook::Create(wxWindow *parent, const wxString& name) { if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT ) - { style |= wxBK_TOP; - } // no border for this control style &= ~wxBORDER_MASK; @@ -90,15 +85,41 @@ wxToolbook::Create(wxWindow *parent, wxDefaultValidator, name) ) return false; - // TODO: make configurable - m_bookctrl = new wxToolBar + int tbFlags = wxTB_TEXT | wxTB_FLAT | wxBORDER_NONE; + if ( (style & (wxBK_LEFT | wxBK_RIGHT)) != 0 ) + tbFlags |= wxTB_VERTICAL; + else + tbFlags |= wxTB_HORIZONTAL; + + if ( style & wxTBK_HORZ_LAYOUT ) + tbFlags |= wxTB_HORZ_LAYOUT; + + // TODO: make more configurable + +#if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON + if (style & wxTBK_BUTTONBAR) + { + m_bookctrl = new wxButtonToolBar ( this, - wxID_TOOLBOOKTOOLBAR, + wxID_ANY, wxDefaultPosition, wxDefaultSize, - wxTB_HORIZONTAL|wxTB_TEXT|wxTB_FLAT|wxTB_NODIVIDER + tbFlags ); + } + else +#endif + { + m_bookctrl = new wxToolBar + ( + this, + wxID_ANY, + wxDefaultPosition, + wxDefaultSize, + tbFlags | wxTB_NODIVIDER + ); + } return true; } @@ -107,55 +128,14 @@ wxToolbook::Create(wxWindow *parent, // wxToolbook geometry management // ---------------------------------------------------------------------------- -wxSize wxToolbook::GetControllerSize() const -{ - const wxSize sizeClient = GetClientSize(), - sizeBorder = m_bookctrl->GetSize() - m_bookctrl->GetClientSize(), - sizeToolBar = GetToolBar()->GetSize() + sizeBorder; - - wxSize size; - - if ( IsVertical() ) - { - size.x = sizeClient.x; - size.y = sizeToolBar.y; - } - else // left/right aligned - { - size.x = sizeToolBar.x; - size.y = sizeClient.y; - } - - return size; -} - void wxToolbook::OnSize(wxSizeEvent& event) { if (m_needsRealizing) Realize(); - - wxBookCtrlBase::OnSize(event); -} - -wxSize wxToolbook::CalcSizeFromPage(const wxSize& sizePage) const -{ - // we need to add the size of the list control and the border between - const wxSize sizeToolBar = GetControllerSize(); - - wxSize size = sizePage; - if ( IsVertical() ) - { - size.y += sizeToolBar.y + GetInternalBorder(); - } - else // left/right aligned - { - size.x += sizeToolBar.x + GetInternalBorder(); - } - return size; + wxBookCtrlBase::OnSize(event); } - // ---------------------------------------------------------------------------- // accessing the pages // ---------------------------------------------------------------------------- @@ -163,7 +143,7 @@ wxSize wxToolbook::CalcSizeFromPage(const wxSize& sizePage) const bool wxToolbook::SetPageText(size_t n, const wxString& strText) { // Assume tool ids start from 1 - wxToolBarToolBase* tool = GetToolBar()->FindById(n+1); + wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1); if (tool) { tool->SetLabel(strText); @@ -175,18 +155,16 @@ bool wxToolbook::SetPageText(size_t n, const wxString& strText) wxString wxToolbook::GetPageText(size_t n) const { - wxToolBarToolBase* tool = GetToolBar()->FindById(n+1); + wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1); if (tool) - { return tool->GetLabel(); - } else return wxEmptyString; } int wxToolbook::GetPageImage(size_t WXUNUSED(n)) const { - wxFAIL_MSG( _T("wxToolbook::GetPageImage() not implemented") ); + wxFAIL_MSG( wxT("wxToolbook::GetPageImage() not implemented") ); return wxNOT_FOUND; } @@ -196,8 +174,8 @@ bool wxToolbook::SetPageImage(size_t n, int imageId) wxASSERT( GetImageList() != NULL ); if (!GetImageList()) return false; - - wxToolBarToolBase* tool = GetToolBar()->FindById(n+1); + + wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1); if (tool) { // Find the image list index for this tool @@ -222,44 +200,20 @@ void wxToolbook::SetImageList(wxImageList *imageList) // selection // ---------------------------------------------------------------------------- -int wxToolbook::GetSelection() const +wxBookCtrlEvent* wxToolbook::CreatePageChangingEvent() const { - return m_selection; + return new wxBookCtrlEvent(wxEVT_TOOLBOOK_PAGE_CHANGING, m_windowId); } -int wxToolbook::SetSelection(size_t n) +void wxToolbook::MakeChangedEvent(wxBookCtrlEvent &event) { - wxCHECK_MSG( IS_VALID_PAGE(n), wxNOT_FOUND, - wxT("invalid page index in wxToolbook::SetSelection()") ); - - const int oldSel = m_selection; - - if ( int(n) != m_selection ) - { - wxToolbookEvent event(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING, m_windowId); - event.SetSelection(n); - event.SetOldSelection(m_selection); - event.SetEventObject(this); - if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() ) - { - if ( m_selection != wxNOT_FOUND ) - m_pages[m_selection]->Hide(); - - wxWindow *page = m_pages[n]; - page->SetSize(GetPageRect()); - page->Show(); - - // change m_selection now to ignore the selection change event - m_selection = n; - GetToolBar()->ToggleTool(n+1, true); - - // program allows the page change - event.SetEventType(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED); - (void)GetEventHandler()->ProcessEvent(event); - } - } + event.SetEventType(wxEVT_TOOLBOOK_PAGE_CHANGED); +} - return oldSel; +void wxToolbook::UpdateSelectedPage(size_t newsel) +{ + m_selection = newsel; + GetToolBar()->ToggleTool(newsel + 1, true); } // Not part of the wxBookctrl API, but must be called in OnIdle or @@ -268,28 +222,59 @@ void wxToolbook::Realize() { if (m_needsRealizing) { + m_needsRealizing = false; + GetToolBar()->SetToolBitmapSize(m_maxBitmapSize); - - wxSystemOptions::SetOption(wxT("msw.remap"), 0); + GetToolBar()->Realize(); - wxSystemOptions::SetOption(wxT("msw.remap"), 1); } - - m_needsRealizing = false; - - if (m_selection == -1) + + if (m_selection == wxNOT_FOUND) m_selection = 0; if (GetPageCount() > 0) { int sel = m_selection; - m_selection = -1; + m_selection = wxNOT_FOUND; SetSelection(sel); } - + DoSize(); } +int wxToolbook::HitTest(const wxPoint& pt, long *flags) const +{ + int pagePos = wxNOT_FOUND; + + if ( flags ) + *flags = wxBK_HITTEST_NOWHERE; + + // convert from wxToolbook coordinates to wxToolBar ones + const wxToolBarBase * const tbar = GetToolBar(); + const wxPoint tbarPt = tbar->ScreenToClient(ClientToScreen(pt)); + + // is the point over the toolbar? + if ( wxRect(tbar->GetSize()).Contains(tbarPt) ) + { + const wxToolBarToolBase * const + tool = tbar->FindToolForPosition(tbarPt.x, tbarPt.y); + + if ( tool ) + { + pagePos = tbar->GetToolPos(tool->GetId()); + if ( flags ) + *flags = wxBK_HITTEST_ONICON | wxBK_HITTEST_ONLABEL; + } + } + else // not over the toolbar + { + if ( flags && GetPageRect().Contains(pt) ) + *flags |= wxBK_HITTEST_ONPAGE; + } + + return pagePos; +} + void wxToolbook::OnIdle(wxIdleEvent& event) { if (m_needsRealizing) @@ -301,8 +286,7 @@ void wxToolbook::OnIdle(wxIdleEvent& event) // adding/removing the pages // ---------------------------------------------------------------------------- -bool -wxToolbook::InsertPage(size_t n, +bool wxToolbook::InsertPage(size_t n, wxWindow *page, const wxString& text, bool bSelect, @@ -312,26 +296,32 @@ wxToolbook::InsertPage(size_t n, return false; m_needsRealizing = true; - + wxASSERT(GetImageList() != NULL); - + if (!GetImageList()) return false; + // TODO: make sure all platforms can convert between icon and bitmap, + // and/or test whether the image is a bitmap or an icon. +#ifdef __WXMAC__ + wxBitmap bitmap = GetImageList()->GetBitmap(imageId); +#else // On Windows, we can lose information by using GetBitmap, so extract icon instead wxIcon icon = GetImageList()->GetIcon(imageId); wxBitmap bitmap; bitmap.CopyFromIcon(icon); - +#endif + m_maxBitmapSize.x = wxMax(bitmap.GetWidth(), m_maxBitmapSize.x); 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, wxNullBitmap, text); if (bSelect) { - // GetToolBar()->ToggleTool(n, true); + GetToolBar()->ToggleTool(n, true); m_selection = n; } else @@ -343,28 +333,13 @@ wxToolbook::InsertPage(size_t n, wxWindow *wxToolbook::DoRemovePage(size_t page) { - const size_t page_count = GetPageCount(); wxWindow *win = wxBookCtrlBase::DoRemovePage(page); if ( win ) { - GetToolBar()->DeleteTool(page+1); + GetToolBar()->DeleteTool(page + 1); - if (m_selection >= (int)page) - { - // force new sel valid if possible - int sel = m_selection - 1; - if (page_count == 1) - sel = wxNOT_FOUND; - else if ((page_count == 2) || (sel == -1)) - sel = 0; - - // force sel invalid if deleting current page - don't try to hide it - m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1; - - if ((sel != wxNOT_FOUND) && (sel != m_selection)) - SetSelection(sel); - } + DoSetSelectionAfterRemoval(page); } return win; @@ -383,7 +358,7 @@ bool wxToolbook::DeleteAllPages() void wxToolbook::OnToolSelected(wxCommandEvent& event) { - const int selNew = event.GetId() -1; + const int selNew = event.GetId() - 1; if ( selNew == m_selection ) {