📄 toolbar.cpp
字号:
SetInitialSize(); return true;}void wxToolBar::SetToolBitmapSize(const wxSize& size){ m_defaultWidth = size.x + kwxMacToolBorder; m_defaultHeight = size.y + kwxMacToolBorder;#if wxMAC_USE_NATIVE_TOOLBAR if (m_macHIToolbarRef != NULL) { int maxs = wxMax( size.x, size.y ); HIToolbarDisplaySize sizeSpec; if ( maxs > 32 ) sizeSpec = kHIToolbarDisplaySizeNormal; else if ( maxs > 24 ) sizeSpec = kHIToolbarDisplaySizeDefault; else sizeSpec = kHIToolbarDisplaySizeSmall; HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef, sizeSpec ); }#endif}// The button size is bigger than the bitmap sizewxSize wxToolBar::GetToolSize() const{ return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder);}void wxToolBar::SetRows(int nRows){ // avoid resizing the frame uselessly if ( nRows != m_maxRows ) m_maxRows = nRows;}void wxToolBar::MacSuperChangedPosition(){ wxWindow::MacSuperChangedPosition();#if wxMAC_USE_NATIVE_TOOLBAR if (! m_macUsesNativeToolbar ) Realize();#else Realize();#endif}wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const{ wxToolBarTool *tool; wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); while ( node != NULL ) { tool = (wxToolBarTool *)node->GetData(); if (tool != NULL) { wxRect2DInt r( tool->GetPosition(), tool->GetSize() ); if ( r.Contains( wxPoint( x, y ) ) ) return tool; } node = node->GetNext(); } return (wxToolBarToolBase*)NULL;}wxString wxToolBar::MacGetToolTipString( wxPoint &pt ){ wxToolBarToolBase *tool = FindToolForPosition( pt.x, pt.y ); if ( tool != NULL ) return tool->GetShortHelp(); return wxEmptyString;}void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable){ if ( t != NULL ) ((wxToolBarTool*)t)->DoEnable( enable );}void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle){ wxToolBarTool *tool = (wxToolBarTool *)t; if ( ( tool != NULL ) && tool->IsButton() ) tool->UpdateToggleImage( toggle );}bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase){ wxToolBarTool *tool = wx_static_cast( wxToolBarTool*, toolBase ); if (tool == NULL) return false; WindowRef window = (WindowRef) MacGetTopLevelWindowRef(); wxSize toolSize = GetToolSize(); Rect toolrect = { 0, 0, toolSize.y, toolSize.x }; ControlRef controlHandle = NULL; OSStatus err = 0; switch (tool->GetStyle()) { case wxTOOL_STYLE_SEPARATOR: { wxASSERT( tool->GetControlHandle() == NULL ); toolSize.x /= 4; toolSize.y /= 4; if ( GetWindowStyleFlag() & wxTB_VERTICAL ) toolrect.bottom = toolSize.y; 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 CreateSeparatorControl( window, &toolrect, &controlHandle ); tool->SetControlHandle( controlHandle );#endif } break; case wxTOOL_STYLE_BUTTON: { wxASSERT( tool->GetControlHandle() == NULL ); ControlButtonContentInfo info; wxMacCreateBitmapButton( &info, tool->GetNormalBitmap(), kControlContentIconRef ); if ( UMAGetSystemVersion() >= 0x1000) { CreateIconControl( window, &toolrect, &info, false, &controlHandle ); } else { SInt16 behaviour = kControlBehaviorOffsetContents; if ( tool->CanBeToggled() ) behaviour |= kControlBehaviorToggles; err = CreateBevelButtonControl( window, &toolrect, CFSTR(""), kControlBevelButtonNormalBevel, behaviour, &info, 0, 0, 0, &controlHandle ); }#if wxMAC_USE_NATIVE_TOOLBAR HIToolbarItemRef item; wxString labelStr = wxString::Format(wxT("%xd"), (int)tool); err = HIToolbarItemCreate( wxMacCFStringHolder(labelStr, wxFont::GetDefaultEncoding()), kHIToolbarItemCantBeRemoved | kHIToolbarItemAnchoredLeft | kHIToolbarItemAllowDuplicates, &item ); if (err == noErr) { 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 wxMacReleaseBitmapButton( &info );#if 0 SetBevelButtonTextPlacement( m_controlHandle, kControlBevelButtonPlaceBelowGraphic ); UMASetControlTitle( m_controlHandle, label, wxFont::GetDefaultEncoding() );#endif InstallControlEventHandler( (ControlRef) controlHandle, GetwxMacToolBarToolEventHandlerUPP(), GetEventTypeCount(eventList), eventList, tool, NULL ); tool->SetControlHandle( controlHandle ); } break; case wxTOOL_STYLE_CONTROL:#if wxMAC_USE_NATIVE_TOOLBAR { wxASSERT( tool->GetControl() != NULL ); HIToolbarItemRef item; HIViewRef viewRef = (HIViewRef) tool->GetControl()->GetHandle() ; // as this control now is part of both the wxToolBar children and the native toolbar, we have to increase the // 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 ) ; if (err == noErr) { tool->SetToolbarItemRef( item ); } CFRelease( data ) ; }#else // right now there's nothing to do here#endif break; default: break; } if ( err == noErr ) { if ( controlHandle ) { ControlRef container = (ControlRef) GetHandle(); wxASSERT_MSG( container != NULL, wxT("No valid Mac container control") ); UMAShowControl( controlHandle ); ::EmbedControl( controlHandle, container ); } if ( tool->CanBeToggled() && tool->IsToggled() ) tool->UpdateToggleImage( true ); // nothing special to do here - we relayout in Realize() later tool->Attach( this ); InvalidateBestSize(); } else { wxString errMsg = wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long)err ); wxFAIL_MSG( errMsg.c_str() ); } return (err == noErr);}void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle)){ wxFAIL_MSG( wxT("not implemented") );}bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase){ wxToolBarTool* tool = wx_static_cast( wxToolBarTool*, toolbase ); wxToolBarToolsList::compatibility_iterator node; for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) { wxToolBarToolBase *tool2 = node->GetData(); if ( tool2 == tool ) { // let node point to the next node in the list node = node->GetNext(); break; } } wxSize sz = ((wxToolBarTool*)tool)->GetSize(); tool->Detach();#if wxMAC_USE_NATIVE_TOOLBAR CFIndex removeIndex = tool->GetIndex();#endif#if wxMAC_USE_NATIVE_TOOLBAR if ( removeIndex != -1 && m_macHIToolbarRef ) { HIToolbarRemoveItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, removeIndex ); tool->SetIndex( -1 ); }#endif switch ( tool->GetStyle() ) { case wxTOOL_STYLE_CONTROL: if ( tool->GetControl() ) tool->GetControl()->Destroy(); break; case wxTOOL_STYLE_BUTTON: case wxTOOL_STYLE_SEPARATOR: // nothing special break; default: break; } tool->ClearControl(); // and finally reposition all the controls after this one for ( /* node -> first after deleted */; node; node = node->GetNext() ) { wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData(); wxPoint pt = tool2->GetPosition(); if ( GetWindowStyleFlag() & wxTB_VERTICAL ) pt.y -= sz.y; else pt.x -= sz.x; tool2->SetPosition( pt );#if wxMAC_USE_NATIVE_TOOLBAR if ( removeIndex != -1 && tool2->GetIndex() > removeIndex ) tool2->SetIndex( tool2->GetIndex() - 1 );#endif } InvalidateBestSize(); return true;}void wxToolBar::OnPaint(wxPaintEvent& event){#if wxMAC_USE_NATIVE_TOOLBAR if ( m_macUsesNativeToolbar ) { event.Skip(true); return; }#endif wxPaintDC dc(this); int w, h; GetSize( &w, &h ); bool drawMetalTheme = MacGetTopLevelWindow()->MacGetMetalAppearance(); bool minimumUmaAvailable = (UMAGetSystemVersion() >= 0x1030);#if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3 if ( !drawMetalTheme && minimumUmaAvailable ) { HIThemePlacardDrawInfo info; memset( &info, 0, sizeof(info) ); info.version = 0; info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive; CGContextRef cgContext = (CGContextRef) MacGetCGContextRef(); HIRect rect = CGRectMake( 0, 0, w, h ); HIThemeDrawPlacard( &rect, &info, cgContext, kHIThemeOrientationNormal ); } else { // leave the background as it is (striped or metal) }#else const bool drawBorder = true; if (drawBorder) { wxMacPortSetter helper( &dc ); if ( !drawMetalTheme || !minimumUmaAvailable ) { Rect toolbarrect = { dc.YLOG2DEVMAC(0), dc.XLOG2DEVMAC(0), dc.YLOG2DEVMAC(h), dc.XLOG2DEVMAC(w) };#if 0 if ( toolbarrect.left < 0 ) toolbarrect.left = 0; if ( toolbarrect.top < 0 ) toolbarrect.top = 0;#endif UMADrawThemePlacard( &toolbarrect, IsEnabled() ? kThemeStateActive : kThemeStateInactive ); } else {#if TARGET_API_MAC_OSX HIRect hiToolbarrect = CGRectMake( dc.YLOG2DEVMAC(0), dc.XLOG2DEVMAC(0), dc.YLOG2DEVREL(h), dc.XLOG2DEVREL(w) ); CGContextRef cgContext; Rect bounds; GetPortBounds( (CGrafPtr) dc.m_macPort, &bounds ); QDBeginCGContext( (CGrafPtr) dc.m_macPort, &cgContext ); CGContextTranslateCTM( cgContext, 0, bounds.bottom - bounds.top ); CGContextScaleCTM( cgContext, 1, -1 ); HIThemeBackgroundDrawInfo drawInfo; drawInfo.version = 0; drawInfo.state = kThemeStateActive; drawInfo.kind = kThemeBackgroundMetal; HIThemeApplyBackground( &hiToolbarrect, &drawInfo, cgContext, kHIThemeOrientationNormal );#ifndef __LP64__ QDEndCGContext( (CGrafPtr) dc.m_macPort, &cgContext );#endif#endif } }#endif event.Skip();}#endif // wxUSE_TOOLBAR
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -