From cdb11cb99e14b18bc284fce01ccbd18fa9c56cb7 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 18 Mar 2007 23:13:18 +0000 Subject: [PATCH] added labels support for toolbar controls for wxMac and wxMSW (modified patch 1613603) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@44903 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + docs/latex/wx/toolbar.tex | 10 ++- include/wx/generic/buttonbar.h | 9 ++- include/wx/mac/carbon/toolbar.h | 3 +- include/wx/msw/tbar95.h | 6 +- include/wx/tbarbase.h | 22 ++++-- include/wx/univ/toolbar.h | 3 +- samples/toolbar/toolbar.cpp | 6 +- src/common/tbarbase.cpp | 12 ++- src/generic/buttonbar.cpp | 11 ++- src/mac/carbon/toolbar.cpp | 130 ++++++++++++++++++-------------- src/msw/tbar95.cpp | 114 ++++++++++++++++++++++++---- src/univ/toolbar.cpp | 9 ++- 13 files changed, 233 insertions(+), 103 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 12766177a4..2ad1a71a45 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -69,6 +69,7 @@ All: All (GUI): - Added wxDC::StretchBlit() for wxMac and wxMSW (Vince Harron) +- Added support for labels for toolbar controls (Vince Harron) - Added wxEventBlocker class (Francesco Montorsi). - Added wxFile/DirPickerCtrl::Get/SetFile/DirName() (Francesco Montorsi). - Added wxSizerFlags::Top() and Bottom(). diff --git a/docs/latex/wx/toolbar.tex b/docs/latex/wx/toolbar.tex index 400dd86252..8bdbe3481a 100644 --- a/docs/latex/wx/toolbar.tex +++ b/docs/latex/wx/toolbar.tex @@ -165,12 +165,20 @@ Toolbar destructor. \membersection{wxToolBar::AddControl}\label{wxtoolbaraddcontrol} -\func{bool}{AddControl}{\param{wxControl*}{ control}} +\func{bool}{AddControl}{\param{wxControl*}{ control}, \param{const wxString&}{ label = ""}} Adds any control to the toolbar, typically e.g. a combobox. \docparam{control}{The control to be added.} +\docparam{label}{Text to be displayed near the control.} + +\wxheading{Remarks} + +wxMSW: the label is only displayed if there is enough space available below the embedded control. + +wxMac: labels are only displayed if wxWidgets is built with wxMAC_USE_NATIVE_TOOLBAR set to 1 + \membersection{wxToolBar::AddSeparator}\label{wxtoolbaraddseparator} diff --git a/include/wx/generic/buttonbar.h b/include/wx/generic/buttonbar.h index c0f319b5c9..e34a1207a5 100644 --- a/include/wx/generic/buttonbar.h +++ b/include/wx/generic/buttonbar.h @@ -6,7 +6,7 @@ // Created: 2006-04-13 // Id: $Id$ // Copyright: (c) Julian Smart, Robert Roebling, Vadim Zeitlin, -// SciTech Software, Inc. +// SciTech Software, Inc. // Licence: wxWindows licence /////////////////////////////////////////////////////////////////////////////// @@ -23,7 +23,7 @@ class WXDLLEXPORT wxButtonToolBarTool; // ---------------------------------------------------------------------------- class WXDLLEXPORT wxButtonToolBar : public wxToolBarBase -{ +{ public: // construction/destruction wxButtonToolBar() { Init(); } @@ -45,7 +45,7 @@ public: const wxSize& size = wxDefaultSize, long style = 0, const wxString& name = wxToolBarNameStr ); - + virtual ~wxButtonToolBar(); virtual bool Realize(); @@ -73,7 +73,8 @@ protected: wxObject *clientData, const wxString& shortHelp, const wxString& longHelp); - virtual wxToolBarToolBase *CreateTool(wxControl *control); + virtual wxToolBarToolBase *CreateTool(wxControl *control, + const wxString& label); virtual wxSize DoGetBestClientSize() const; diff --git a/include/wx/mac/carbon/toolbar.h b/include/wx/mac/carbon/toolbar.h index 7673d04a43..00d7309b30 100644 --- a/include/wx/mac/carbon/toolbar.h +++ b/include/wx/mac/carbon/toolbar.h @@ -93,7 +93,8 @@ protected: wxObject *clientData, const wxString& shortHelp, const wxString& longHelp); - virtual wxToolBarToolBase *CreateTool(wxControl *control); + virtual wxToolBarToolBase *CreateTool(wxControl *control, + const wxString& label); DECLARE_EVENT_TABLE() #if wxMAC_USE_NATIVE_TOOLBAR diff --git a/include/wx/msw/tbar95.h b/include/wx/msw/tbar95.h index 4a3be73333..ec80d8d956 100644 --- a/include/wx/msw/tbar95.h +++ b/include/wx/msw/tbar95.h @@ -56,7 +56,7 @@ public: virtual void SetToolNormalBitmap(int id, const wxBitmap& bitmap); virtual void SetToolDisabledBitmap(int id, const wxBitmap& bitmap); - + // implementation only from now on // ------------------------------- @@ -104,7 +104,9 @@ protected: wxObject *clientData, const wxString& shortHelp, const wxString& longHelp); - virtual wxToolBarToolBase *CreateTool(wxControl *control); + + virtual wxToolBarToolBase *CreateTool(wxControl *control, + const wxString& label); // return the appropriate size and flags for the toolbar control virtual wxSize DoGetBestSize() const; diff --git a/include/wx/tbarbase.h b/include/wx/tbarbase.h index 9f0f42fa4f..50cc2bc217 100644 --- a/include/wx/tbarbase.h +++ b/include/wx/tbarbase.h @@ -91,7 +91,10 @@ public: : wxTOOL_STYLE_BUTTON; } - wxToolBarToolBase(wxToolBarBase *tbar, wxControl *control) + wxToolBarToolBase(wxToolBarBase *tbar, + wxControl *control, + const wxString& label) + : m_label(label) { m_tbar = tbar; m_control = control; @@ -316,13 +319,17 @@ public: virtual wxToolBarToolBase *AddTool (wxToolBarToolBase *tool); virtual wxToolBarToolBase *InsertTool (size_t pos, wxToolBarToolBase *tool); - // add an arbitrary control to the toolbar (notice that - // the control will be deleted by the toolbar and that it will also adjust - // its position/size) + // add an arbitrary control to the toolbar (notice that the control will be + // deleted by the toolbar and that it will also adjust its position/size) // + // the label is optional and, if specified, will be shown near the control // NB: the control should have toolbar as its parent - virtual wxToolBarToolBase *AddControl(wxControl *control); - virtual wxToolBarToolBase *InsertControl(size_t pos, wxControl *control); + virtual wxToolBarToolBase * + AddControl(wxControl *control, const wxString& label = wxEmptyString); + + virtual wxToolBarToolBase * + InsertControl(size_t pos, wxControl *control, + const wxString& label = wxEmptyString); // get the control with the given id or return NULL virtual wxControl *FindControl( int toolid ); @@ -565,7 +572,8 @@ protected: const wxString& shortHelp, const wxString& longHelp) = 0; - virtual wxToolBarToolBase *CreateTool(wxControl *control) = 0; + virtual wxToolBarToolBase *CreateTool(wxControl *control, + const wxString& label) = 0; // helper functions // ---------------- diff --git a/include/wx/univ/toolbar.h b/include/wx/univ/toolbar.h index 1743a174fb..2ee3b1eeb8 100644 --- a/include/wx/univ/toolbar.h +++ b/include/wx/univ/toolbar.h @@ -98,7 +98,8 @@ protected: wxObject *clientData, const wxString& shortHelp, const wxString& longHelp); - virtual wxToolBarToolBase *CreateTool(wxControl *control); + virtual wxToolBarToolBase *CreateTool(wxControl *control, + const wxString& label); virtual wxSize DoGetBestClientSize() const; virtual void DoSetSize(int x, int y, diff --git a/samples/toolbar/toolbar.cpp b/samples/toolbar/toolbar.cpp index 8812040c03..99bd1fe665 100644 --- a/samples/toolbar/toolbar.cpp +++ b/samples/toolbar/toolbar.cpp @@ -402,14 +402,14 @@ void MyFrame::RecreateToolbar() combo->Append(_T("combobox")); combo->Append(_T("in a")); combo->Append(_T("toolbar")); - toolBar->AddControl(combo); + toolBar->AddControl(combo, _T("Combo Label")); wxSpinCtrl *spin = new wxSpinCtrl( toolBar, ID_SPIN, wxT("0"), wxDefaultPosition, wxSize(80,wxDefaultCoord), 0, 0, 100 ); toolBar->AddControl( spin ); - + wxTextCtrl *text = new wxTextCtrl( toolBar, -1, wxT("text"), wxDefaultPosition, wxSize(80,wxDefaultCoord) ); toolBar->AddControl( text ); - + wxSearchCtrl *srch = new wxSearchCtrl( toolBar, -1, wxT("xx"), wxDefaultPosition, wxSize(80,wxDefaultCoord), wxSUNKEN_BORDER ); toolBar->AddControl( srch ); } diff --git a/src/common/tbarbase.cpp b/src/common/tbarbase.cpp index 553065ee73..a8ada3fa9b 100644 --- a/src/common/tbarbase.cpp +++ b/src/common/tbarbase.cpp @@ -197,12 +197,16 @@ wxToolBarBase::InsertTool(size_t pos, wxToolBarToolBase *tool) return tool; } -wxToolBarToolBase *wxToolBarBase::AddControl(wxControl *control) +wxToolBarToolBase * +wxToolBarBase::AddControl(wxControl *control, const wxString& label) { - return InsertControl(GetToolsCount(), control); + return InsertControl(GetToolsCount(), control, label); } -wxToolBarToolBase *wxToolBarBase::InsertControl(size_t pos, wxControl *control) +wxToolBarToolBase * +wxToolBarBase::InsertControl(size_t pos, + wxControl *control, + const wxString& label) { wxCHECK_MSG( control, (wxToolBarToolBase *)NULL, _T("toolbar: can't insert NULL control") ); @@ -213,7 +217,7 @@ wxToolBarToolBase *wxToolBarBase::InsertControl(size_t pos, wxControl *control) wxCHECK_MSG( pos <= GetToolsCount(), (wxToolBarToolBase *)NULL, _T("invalid position in wxToolBar::InsertControl()") ); - wxToolBarToolBase *tool = CreateTool(control); + wxToolBarToolBase *tool = CreateTool(control, label); if ( !InsertTool(pos, tool) ) { diff --git a/src/generic/buttonbar.cpp b/src/generic/buttonbar.cpp index 343e7e585a..091dfd01b1 100644 --- a/src/generic/buttonbar.cpp +++ b/src/generic/buttonbar.cpp @@ -66,8 +66,10 @@ public: m_button = NULL; } - wxButtonToolBarTool(wxButtonToolBar *tbar, wxControl *control) - : wxToolBarToolBase(tbar, control) + wxButtonToolBarTool(wxButtonToolBar *tbar, + wxControl *control, + const wxString& label) + : wxToolBarToolBase(tbar, control, label) { m_x = m_y = wxDefaultCoord; m_width = @@ -271,9 +273,10 @@ wxToolBarToolBase *wxButtonToolBar::CreateTool(int id, clientData, shortHelp, longHelp); } -wxToolBarToolBase *wxButtonToolBar::CreateTool(wxControl *control) +wxToolBarToolBase *wxButtonToolBar::CreateTool(wxControl *control, + const wxString& label) { - return new wxButtonToolBarTool(this, control); + return new wxButtonToolBarTool(this, control, label); } // ---------------------------------------------------------------------------- diff --git a/src/mac/carbon/toolbar.cpp b/src/mac/carbon/toolbar.cpp index f65ba6d135..74250797a4 100644 --- a/src/mac/carbon/toolbar.cpp +++ b/src/mac/carbon/toolbar.cpp @@ -72,8 +72,8 @@ public: const wxString& shortHelp, const wxString& longHelp ); - wxToolBarTool(wxToolBar *tbar, wxControl *control) - : wxToolBarToolBase(tbar, control) + wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label) + : wxToolBarToolBase(tbar, control, label) { Init(); if (control != NULL) @@ -176,7 +176,7 @@ public: enc = f.GetEncoding(); else enc = wxFont::GetDefaultEncoding(); - + HIToolbarItemSetHelpText( m_toolbarItemRef, wxMacCFStringHolder( GetShortHelp(), enc ), @@ -533,9 +533,10 @@ wxToolBarToolBase *wxToolBar::CreateTool( clientData, shortHelp, longHelp ); } -wxToolBarToolBase * wxToolBar::CreateTool( wxControl *control ) +wxToolBarToolBase * +wxToolBar::CreateTool(wxControl *control, const wxString& label) { - return new wxToolBarTool( this, control ); + return new wxToolBarTool(this, control, label); } void wxToolBar::Init() @@ -553,26 +554,26 @@ void wxToolBar::Init() #define kControlToolbarItemClassID CFSTR( "org.wxwidgets.controltoolbaritem" ) -const EventTypeSpec kEvents[] = +const EventTypeSpec kEvents[] = { { kEventClassHIObject, kEventHIObjectConstruct }, { kEventClassHIObject, kEventHIObjectInitialize }, { kEventClassHIObject, kEventHIObjectDestruct }, - + { kEventClassToolbarItem, kEventToolbarItemCreateCustomView } }; -const EventTypeSpec kViewEvents[] = -{ - { kEventClassControl, kEventControlGetSizeConstraints } +const EventTypeSpec kViewEvents[] = +{ + { kEventClassControl, kEventControlGetSizeConstraints } }; -struct ControlToolbarItem -{ - HIToolbarItemRef toolbarItem; +struct ControlToolbarItem +{ + HIToolbarItemRef toolbarItem; HIViewRef viewRef; wxSize lastValidSize ; -}; +}; static pascal OSStatus ControlToolbarItemHandler( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData ) { @@ -588,20 +589,20 @@ static pascal OSStatus ControlToolbarItemHandler( EventHandlerCallRef inCallRef, { HIObjectRef toolbarItem; ControlToolbarItem* item; - + GetEventParameter( inEvent, kEventParamHIObjectInstance, typeHIObjectRef, NULL, sizeof( HIObjectRef ), NULL, &toolbarItem ); - + item = (ControlToolbarItem*) malloc(sizeof(ControlToolbarItem)) ; item->toolbarItem = toolbarItem ; item->viewRef = NULL ; - + SetEventParameter( inEvent, kEventParamHIObjectInstance, typeVoidPtr, sizeof( void * ), &item ); - + result = noErr ; } break; - + case kEventHIObjectInitialize: result = CallNextEventHandler( inCallRef, inEvent ); if ( result == noErr ) @@ -609,12 +610,12 @@ static pascal OSStatus ControlToolbarItemHandler( EventHandlerCallRef inCallRef, CFDataRef data; GetEventParameter( inEvent, kEventParamToolbarItemConfigData, typeCFTypeRef, NULL, sizeof( CFTypeRef ), NULL, &data ); - + HIViewRef viewRef ; - + wxASSERT_MSG( CFDataGetLength( data ) == sizeof( viewRef ) , wxT("Illegal Data passed") ) ; memcpy( &viewRef , CFDataGetBytePtr( data ) , sizeof( viewRef ) ) ; - + object->viewRef = (HIViewRef) viewRef ; result = noErr ; @@ -628,7 +629,7 @@ static pascal OSStatus ControlToolbarItemHandler( EventHandlerCallRef inCallRef, HIViewRef viewRef = object->viewRef ; if( viewRef && IsValidControlHandle( viewRef) ) { - CFIndex count = CFGetRetainCount( viewRef ) ; + CFIndex count = CFGetRetainCount( viewRef ) ; if ( count >= 1 ) CFRelease( viewRef ) ; } @@ -638,10 +639,10 @@ static pascal OSStatus ControlToolbarItemHandler( EventHandlerCallRef inCallRef, break; } break; - + case kEventClassToolbarItem: switch ( GetEventKind( inEvent ) ) - { + { case kEventToolbarItemCreateCustomView: { HIViewRef viewRef = object->viewRef ; @@ -650,13 +651,13 @@ static pascal OSStatus ControlToolbarItemHandler( EventHandlerCallRef inCallRef, HIViewSetVisible(viewRef, true) ; InstallEventHandler( GetControlEventTarget( viewRef ), ControlToolbarItemHandler, GetEventTypeCount( kViewEvents ), kViewEvents, object, NULL ); - + result = SetEventParameter( inEvent, kEventParamControlRef, typeControlRef, sizeof( HIViewRef ), &viewRef ); } break; } break; - + case kEventClassControl: switch ( GetEventKind( inEvent ) ) { @@ -675,14 +676,14 @@ static pascal OSStatus ControlToolbarItemHandler( EventHandlerCallRef inCallRef, object->lastValidSize = sz ; else sz = object->lastValidSize ; - + HISize min, max; min.width = max.width = sz.x ; min.height = max.height = sz.y ; - + result = SetEventParameter( inEvent, kEventParamMinimumSize, typeHISize, sizeof( HISize ), &min ); - + result = SetEventParameter( inEvent, kEventParamMaximumSize, typeHISize, sizeof( HISize ), &max ); result = noErr ; @@ -692,19 +693,19 @@ static pascal OSStatus ControlToolbarItemHandler( EventHandlerCallRef inCallRef, } break; } - + return result; } void RegisterControlToolbarItemClass() { static bool sRegistered; - + if ( !sRegistered ) { HIObjectRegisterSubclass( kControlToolbarItemClassID, kHIToolbarItemClassID, 0, ControlToolbarItemHandler, GetEventTypeCount( kEvents ), kEvents, 0, NULL ); - + sRegistered = true; } } @@ -712,26 +713,26 @@ void RegisterControlToolbarItemClass() HIToolbarItemRef CreateControlToolbarItem(CFStringRef inIdentifier, CFTypeRef inConfigData) { RegisterControlToolbarItemClass(); - + OSStatus err; EventRef event; UInt32 options = kHIToolbarItemAllowDuplicates; HIToolbarItemRef result = NULL; - + err = CreateEvent( NULL, kEventClassHIObject, kEventHIObjectInitialize, GetCurrentEventTime(), 0, &event ); require_noerr( err, CantCreateEvent ); - + SetEventParameter( event, kEventParamAttributes, typeUInt32, sizeof( UInt32 ), &options ); SetEventParameter( event, kEventParamToolbarItemIdentifier, typeCFStringRef, sizeof( CFStringRef ), &inIdentifier ); - + if ( inConfigData ) SetEventParameter( event, kEventParamToolbarItemConfigData, typeCFTypeRef, sizeof( CFTypeRef ), &inConfigData ); - + err = HIObjectCreate( kControlToolbarItemClassID, event, (HIObjectRef*)&result ); check_noerr( err ); - + ReleaseEvent( event ); -CantCreateEvent : +CantCreateEvent : return result ; } @@ -761,7 +762,7 @@ static OSStatus ToolbarDelegateHandler( EventHandlerCallRef inCallRef, EventRef result = noErr; } break; - + case kEventToolbarGetAllowedIdentifiers: { GetEventParameter( inEvent, kEventParamMutableArray, typeCFMutableArrayRef, NULL, @@ -776,13 +777,13 @@ static OSStatus ToolbarDelegateHandler( EventHandlerCallRef inCallRef, EventRef HIToolbarItemRef item = NULL; CFTypeRef data = NULL; CFStringRef identifier = NULL ; - + GetEventParameter( inEvent, kEventParamToolbarItemIdentifier, typeCFStringRef, NULL, sizeof( CFStringRef ), NULL, &identifier ); - + GetEventParameter( inEvent, kEventParamToolbarItemConfigData, typeCFTypeRef, NULL, sizeof( CFTypeRef ), NULL, &data ); - + if ( CFStringCompare( kControlToolbarItemClassID, identifier, kCFCompareBackwards ) == kCFCompareEqualTo ) { item = CreateControlToolbarItem( kControlToolbarItemClassID, data ); @@ -793,7 +794,7 @@ static OSStatus ToolbarDelegateHandler( EventHandlerCallRef inCallRef, EventRef result = noErr; } } - + } break; } @@ -843,7 +844,7 @@ bool wxToolBar::Create( HIToolbarSetDisplayMode( (HIToolbarRef) m_macHIToolbarRef, mode ); HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef, displaySize ); } -#endif +#endif // wxMAC_USE_NATIVE_TOOLBAR return (err == noErr); } @@ -1332,7 +1333,7 @@ void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap ) // a side-effect of the UpdateToggleImage function is that it always changes the bitmap used on the button. tool->UpdateToggleImage( tool->CanBeToggled() && tool->IsToggled() ); - } + } } void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap ) @@ -1343,9 +1344,9 @@ void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap ) wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools.")); tool->SetDisabledBitmap(bitmap); - + // TODO: what to do for this one? - } + } } wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const @@ -1403,6 +1404,10 @@ bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase) OSStatus err = 0; tool->Attach( this ); +#if wxMAC_USE_NATIVE_TOOLBAR + HIToolbarItemRef item; +#endif + switch (tool->GetStyle()) { case wxTOOL_STYLE_SEPARATOR: @@ -1415,21 +1420,18 @@ bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase) else toolrect.right = toolSize.x; -#ifdef __WXMAC_OSX__ // in flat style we need a visual separator #if wxMAC_USE_NATIVE_TOOLBAR - HIToolbarItemRef item; err = HIToolbarItemCreate( kHIToolbarSeparatorIdentifier, kHIToolbarItemCantBeRemoved | kHIToolbarItemIsSeparator | kHIToolbarItemAllowDuplicates, &item ); if (err == noErr) tool->SetToolbarItemRef( item ); -#endif +#endif // wxMAC_USE_NATIVE_TOOLBAR CreateSeparatorControl( window, &toolrect, &controlHandle ); tool->SetControlHandle( controlHandle ); -#endif } break; @@ -1454,7 +1456,6 @@ bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase) } #if wxMAC_USE_NATIVE_TOOLBAR - HIToolbarItemRef item; wxString labelStr = wxString::Format(wxT("%xd"), (int)tool); err = HIToolbarItemCreate( wxMacCFStringHolder(labelStr, wxFont::GetDefaultEncoding()), @@ -1464,12 +1465,12 @@ bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase) InstallEventHandler( HIObjectGetEventTarget(item), GetwxMacToolBarEventHandlerUPP(), GetEventTypeCount(toolBarEventList), toolBarEventList, tool, NULL ); - HIToolbarItemSetLabel( item, wxMacCFStringHolder(tool->GetLabel(), m_font.GetEncoding()) ); + HIToolbarItemSetIconRef( item, info.u.iconRef ); HIToolbarItemSetCommandID( item, kHIToolbarCommandPressAction ); tool->SetToolbarItemRef( item ); } -#endif +#endif // wxMAC_USE_NATIVE_TOOLBAR wxMacReleaseBitmapButton( &info ); @@ -1497,8 +1498,8 @@ bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase) // reference count to make sure we are not dealing with zombie controls after the native toolbar has released its views CFRetain( viewRef ) ; CFDataRef data = CFDataCreate( kCFAllocatorDefault , (UInt8*) &viewRef , sizeof(viewRef) ) ; - err = HIToolbarCreateItemWithIdentifier((HIToolbarRef) m_macHIToolbarRef,kControlToolbarItemClassID, - data , &item ) ; + err = HIToolbarCreateItemWithIdentifier((HIToolbarRef) m_macHIToolbarRef,kControlToolbarItemClassID, + data , &item ) ; if (err == noErr) { @@ -1516,6 +1517,19 @@ bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase) break; } +#if wxMAC_USE_NATIVE_TOOLBAR + wxString label = tool->GetLabel(); + if ( !label.empty() ) + { + // strip mnemonics from the label for compatibility + // with the usual labels in wxStaticText sense + label = wxStaticText(label); + + HIToolbarItemSetLabel(item, + wxMacCFStringHolder(label, m_font.GetEncoding())); + } +#endif // wxMAC_USE_NATIVE_TOOLBAR + if ( err == noErr ) { if ( controlHandle ) diff --git a/src/msw/tbar95.cpp b/src/msw/tbar95.cpp index 734b22be10..48e7014658 100644 --- a/src/msw/tbar95.cpp +++ b/src/msw/tbar95.cpp @@ -148,14 +148,38 @@ public: clientData, shortHelp, longHelp) { m_nSepCount = 0; + m_staticText = 0; } - wxToolBarTool(wxToolBar *tbar, wxControl *control) - : wxToolBarToolBase(tbar, control) + wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label) + : wxToolBarToolBase(tbar, control, label) { + if ( IsControl() && !m_label.empty() ) + { + // create a control to render the control's label + m_staticText = new wxStaticText + ( + m_tbar, + wxID_ANY, + m_label, + wxDefaultPosition, + wxDefaultSize, + wxALIGN_CENTRE | wxST_NO_AUTORESIZE + ); + } + else // no label + { + m_staticText = NULL; + } + m_nSepCount = 1; } + virtual ~wxToolBarTool() + { + delete m_staticText; + } + virtual void SetLabel(const wxString& label) { if ( label == m_label ) @@ -163,12 +187,23 @@ public: wxToolBarToolBase::SetLabel(label); + if ( m_staticText ) + m_staticText->SetLabel(label); + // we need to update the label shown in the toolbar because it has a // pointer to the internal buffer of the old label // // TODO: use TB_SETBUTTONINFO } + wxStaticText* GetStaticText() + { + wxASSERT_MSG( IsControl(), + _T("only makes sense for embedded control tools") ); + + return m_staticText; + } + // set/get the number of separators which we use to cover the space used by // a control in the toolbar void SetSeparatorsCount(size_t count) { m_nSepCount = count; } @@ -176,6 +211,7 @@ public: private: size_t m_nSepCount; + wxStaticText *m_staticText; DECLARE_NO_COPY_CLASS(wxToolBarTool) }; @@ -201,9 +237,10 @@ wxToolBarToolBase *wxToolBar::CreateTool(int id, clientData, shortHelp, longHelp); } -wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control) +wxToolBarToolBase * +wxToolBar::CreateTool(wxControl *control, const wxString& label) { - return new wxToolBarTool(this, control); + return new wxToolBarTool(this, control, label); } // ---------------------------------------------------------------------------- @@ -489,13 +526,16 @@ bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool) // takes care of all normal items) for ( /* node -> first after deleted */ ; node; node = node->GetNext() ) { - wxToolBarToolBase *tool2 = node->GetData(); + wxToolBarTool *tool2 = (wxToolBarTool*)node->GetData(); if ( tool2->IsControl() ) { int x; wxControl *control = tool2->GetControl(); control->GetPosition(&x, NULL); control->Move(x - width, wxDefaultCoord); + + wxStaticText* staticText = tool2->GetStaticText(); + staticText->Move(x - width, wxDefaultCoord); } } @@ -944,7 +984,7 @@ bool wxToolBar::Realize() size_t index = 0; for ( node = m_tools.GetFirst(); node; node = node->GetNext(), index++ ) { - wxToolBarToolBase *tool = node->GetData(); + wxToolBarTool *tool = (wxToolBarTool*)node->GetData(); // we calculate the running y coord for vertical toolbars so we need to // get the items size for all items but for the horizontal ones we @@ -971,7 +1011,15 @@ bool wxToolBar::Realize() } wxControl *control = tool->GetControl(); + wxStaticText * const staticText = tool->GetStaticText(); + wxSize size = control->GetSize(); + wxSize staticTextSize; + if ( staticText ) + { + staticTextSize = staticText->GetSize(); + staticTextSize.y += 3; // margin between control and its label + } // the position of the leftmost controls corner int left = wxDefaultCoord; @@ -1026,18 +1074,36 @@ bool wxToolBar::Realize() ((wxToolBarTool *)tool)->SetSeparatorsCount(nSeparators); // adjust the controls width to exactly cover the separators - control->SetSize((nSeparators + 1)*widthSep, wxDefaultCoord); + size.x = (nSeparators + 1)*widthSep; + control->SetSize(size.x, wxDefaultCoord); } - // position the control itself correctly vertically - int height = r.bottom - r.top; + // position the control itself correctly vertically centering it on the + // icon area of the toolbar + int height = r.bottom - r.top - staticTextSize.y; + int diff = height - size.y; - if ( diff < 0 ) + if ( diff < 0 || !HasFlag(wxTB_TEXT) ) { - // the control is too high, resize to fit - control->SetSize(wxDefaultCoord, height - 2); + // not enough room for the static text + if ( staticText ) + staticText->Hide(); + + // recalculate height & diff without the staticText control + height = r.bottom - r.top; + diff = height - size.y; + if ( diff < 0 ) + { + // the control is too high, resize to fit + control->SetSize(wxDefaultCoord, height - 2); - diff = 2; + diff = 2; + } + } + else // enough space for both the control and the label + { + if ( staticText ) + staticText->Show(); } int top; @@ -1057,6 +1123,11 @@ bool wxToolBar::Realize() } control->Move(left, top + (diff + 1) / 2); + if ( staticText ) + { + staticText->Move(left + (size.x - staticTextSize.x)/2, + r.bottom - staticTextSize.y); + } } // the max index is the "real" number of buttons - i.e. counting even the @@ -1539,12 +1610,18 @@ bool wxToolBar::HandlePaint(WXWPARAM wParam, WXLPARAM lParam) // otherwise for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) { - wxToolBarToolBase *tool = node->GetData(); + wxToolBarTool *tool = (wxToolBarTool*)node->GetData(); if ( tool->IsControl() ) { // get the control rect in our client coords wxControl *control = tool->GetControl(); + wxStaticText *staticText = tool->GetStaticText(); wxRect rectCtrl = control->GetRect(); + wxRect rectStaticText(0,0,0,0); + if ( staticText ) + { + rectStaticText = staticText->GetRect(); + } // iterate over all buttons TBBUTTON tbb; @@ -1585,6 +1662,15 @@ bool wxToolBar::HandlePaint(WXWPARAM wParam, WXLPARAM lParam) // style in the parent: the controls can disappear control->Refresh(false); } + if ( staticText && rectStaticText.Intersects(rectItem) ) + { + // yes, do erase it! + dc.DrawRectangle(rectItem); + + // Necessary in case we use a no-paint-on-size + // style in the parent: the controls can disappear + staticText->Refresh(false); + } } } } diff --git a/src/univ/toolbar.cpp b/src/univ/toolbar.cpp index 264bddd043..ccdff2fd2c 100644 --- a/src/univ/toolbar.cpp +++ b/src/univ/toolbar.cpp @@ -104,8 +104,8 @@ public: m_underMouse = false; } - wxToolBarTool(wxToolBar *tbar, wxControl *control) - : wxToolBarToolBase(tbar, control) + wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label) + : wxToolBarToolBase(tbar, control, label) { // no position yet m_x = @@ -328,9 +328,10 @@ wxToolBarToolBase *wxToolBar::CreateTool(int id, clientData, shortHelp, longHelp); } -wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control) +wxToolBarToolBase * +wxToolBar::CreateTool(wxControl *control, const wxString& label) { - return new wxToolBarTool(this, control); + return new wxToolBarTool(this, control, label); } // ---------------------------------------------------------------------------- -- 2.47.2