1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: The wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "toolbar.h"
16 #include "wx/wxprec.h"
21 #include "wx/bitmap.h"
22 #include "wx/toolbar.h"
24 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
26 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
27 EVT_PAINT( wxToolBar::OnPaint
)
30 #include "wx/mac/uma.h"
31 #include "wx/geometry.h"
34 const short kwxMacToolBarToolDefaultWidth
= 16 ;
35 const short kwxMacToolBarToolDefaultHeight
= 16 ;
36 const short kwxMacToolBarTopMargin
= 4 ; // 1 ; // used to be 4
37 const short kwxMacToolBarLeftMargin
= 4 ; //1 ; // used to be 4
38 const short kwxMacToolBorder
= 0 ; // used to be 0
39 const short kwxMacToolSpacing
= 6 ; // 2 ; // used to be 6
41 const short kwxMacToolBarToolDefaultWidth
= 24 ;
42 const short kwxMacToolBarToolDefaultHeight
= 22 ;
43 const short kwxMacToolBarTopMargin
= 2 ;
44 const short kwxMacToolBarLeftMargin
= 2 ;
45 const short kwxMacToolBorder
= 4 ;
46 const short kwxMacToolSpacing
= 0 ;
50 #pragma mark Tool Implementation
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 // We have a dual implementation for each tool, ControlRef and HIToolbarItemRef
59 class wxToolBarTool
: public wxToolBarToolBase
62 wxToolBarTool(wxToolBar
*tbar
,
64 const wxString
& label
,
65 const wxBitmap
& bmpNormal
,
66 const wxBitmap
& bmpDisabled
,
69 const wxString
& shortHelp
,
70 const wxString
& longHelp
) ;
72 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
73 : wxToolBarToolBase(tbar
, control
)
77 SetControlHandle( (ControlRef
) control
->GetHandle() ) ;
83 if ( m_controlHandle
)
84 DisposeControl( m_controlHandle
) ;
85 #if wxMAC_USE_NATIVE_TOOLBAR
86 if ( m_toolbarItemRef
)
87 CFRelease( m_toolbarItemRef
) ;
91 WXWidget
GetControlHandle()
93 return (WXWidget
) m_controlHandle
;
96 void SetControlHandle( ControlRef handle
)
98 m_controlHandle
= handle
;
101 void SetPosition( const wxPoint
& position
) ;
106 #if wxMAC_USE_NATIVE_TOOLBAR
107 m_toolbarItemRef
= NULL
;
111 wxSize
GetSize() const
115 return GetControl()->GetSize() ;
117 else if ( IsButton() )
119 return GetToolBar()->GetToolSize() ;
124 wxSize sz
= GetToolBar()->GetToolSize() ;
125 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
132 wxPoint
GetPosition() const
134 return wxPoint(m_x
, m_y
);
136 bool DoEnable( bool enable
) ;
138 void UpdateToggleImage( bool toggle
) ;
140 #if wxMAC_USE_NATIVE_TOOLBAR
141 void SetToolbarItemRef( HIToolbarItemRef ref
)
143 if ( m_controlHandle
)
144 HideControl( m_controlHandle
) ;
145 if ( m_toolbarItemRef
)
146 CFRelease( m_toolbarItemRef
) ;
147 m_toolbarItemRef
= ref
;
148 if ( m_toolbarItemRef
)
150 HIToolbarItemSetHelpText(
151 m_toolbarItemRef
, wxMacCFStringHolder( GetShortHelp() , GetToolBar()->GetFont().GetEncoding() ) ,
152 wxMacCFStringHolder( GetLongHelp() , GetToolBar()->GetFont().GetEncoding() ) ) ;
155 HIToolbarItemRef
GetToolbarItemRef() const
157 return m_toolbarItemRef
;
164 m_controlHandle
= NULL
;
165 #if wxMAC_USE_NATIVE_TOOLBAR
166 m_toolbarItemRef
= NULL
;
169 ControlRef m_controlHandle
;
170 #if wxMAC_USE_NATIVE_TOOLBAR
171 HIToolbarItemRef m_toolbarItemRef
;
177 static const EventTypeSpec eventList
[] =
179 { kEventClassControl
, kEventControlHit
} ,
181 { kEventClassControl
, kEventControlHitTest
} ,
185 static pascal OSStatus
wxMacToolBarToolControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
187 OSStatus result
= eventNotHandledErr
;
189 wxMacCarbonEvent
cEvent( event
) ;
191 ControlRef controlRef
;
193 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
) ;
195 switch( GetEventKind( event
) )
197 case kEventControlHit
:
199 wxToolBarTool
* tbartool
= (wxToolBarTool
*)data
;
200 wxToolBar
*tbar
= tbartool
!= NULL
? ( wxToolBar
* ) ( tbartool
->GetToolBar() ) : NULL
;
201 if ((tbartool
!= NULL
) && tbartool
->CanBeToggled() )
205 shouldToggle
= !tbartool
->IsToggled();
207 shouldToggle
= ( GetControl32BitValue((ControlRef
) tbartool
->GetControlHandle()) != 0 );
209 tbar
->ToggleTool( tbartool
->GetId(), shouldToggle
);
211 if (tbartool
!= NULL
)
212 tbar
->OnLeftClick( tbartool
->GetId(), tbartool
->IsToggled() );
218 case kEventControlHitTest
:
220 HIPoint pt
= cEvent
.GetParameter
<HIPoint
>(kEventParamMouseLocation
) ;
222 HIViewGetBounds( controlRef
, &rect
) ;
224 ControlPartCode pc
= kControlNoPart
;
225 if ( CGRectContainsPoint( rect
, pt
) )
226 pc
= kControlIconPart
;
227 cEvent
.SetParameter( kEventParamControlPart
, typeControlPartCode
, pc
) ;
239 static pascal OSStatus
wxMacToolBarToolEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
241 OSStatus result
= eventNotHandledErr
;
243 switch ( GetEventClass( event
) )
245 case kEventClassControl
:
246 result
= wxMacToolBarToolControlEventHandler( handler
, event
, data
) ;
255 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler
)
257 #if wxMAC_USE_NATIVE_TOOLBAR
263 static const EventTypeSpec toolBarEventList
[] =
265 { kEventClassToolbarItem
, kEventToolbarItemPerformAction
} ,
268 static pascal OSStatus
wxMacToolBarCommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
270 OSStatus result
= eventNotHandledErr
;
272 switch( GetEventKind( event
) )
274 case kEventToolbarItemPerformAction
:
276 wxToolBarTool
* tbartool
= (wxToolBarTool
*) data
;
277 if ( tbartool
!= NULL
)
279 int toolID
= tbartool
->GetId();
280 wxToolBar
*tbar
= ( wxToolBar
* ) ( tbartool
->GetToolBar() );
281 if ( tbartool
->CanBeToggled() )
283 tbar
->ToggleTool(toolID
, !tbartool
->IsToggled() );
285 tbar
->OnLeftClick( toolID
, tbartool
-> IsToggled() ) ;
297 static pascal OSStatus
wxMacToolBarEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
299 OSStatus result
= eventNotHandledErr
;
300 switch( GetEventClass( event
) )
302 case kEventClassToolbarItem
:
303 result
= wxMacToolBarCommandEventHandler( handler
, event
, data
) ;
312 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarEventHandler
)
316 // ============================================================================
318 // ============================================================================
320 // ----------------------------------------------------------------------------
322 // ----------------------------------------------------------------------------
324 bool wxToolBarTool::DoEnable(bool enable
)
328 GetControl()->Enable( enable
) ;
330 else if ( IsButton() )
332 #if wxMAC_USE_NATIVE_TOOLBAR
333 if ( m_toolbarItemRef
)
334 HIToolbarItemSetEnabled( m_toolbarItemRef
, enable
) ;
337 if ( m_controlHandle
)
339 #if TARGET_API_MAC_OSX
341 EnableControl( m_controlHandle
) ;
343 DisableControl( m_controlHandle
) ;
346 ActivateControl( m_controlHandle
) ;
348 DeactivateControl( m_controlHandle
) ;
355 void wxToolBarTool::SetPosition(const wxPoint
& position
)
362 int mac_x
= position
.x
;
363 int mac_y
= position
.y
;
365 if ( ! GetToolBar()->MacGetTopLevelWindow()->MacUsesCompositing() )
367 GetToolBar()->MacWindowToRootWindow( &x
, &y
) ;
375 GetControlBounds( m_controlHandle
, &contrlRect
) ;
376 int former_mac_x
= contrlRect
.left
;
377 int former_mac_y
= contrlRect
.top
;
378 GetToolBar()->GetToolSize() ;
380 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
382 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
) ;
385 else if ( IsControl() )
387 GetControl()->Move( position
) ;
394 GetControlBounds( m_controlHandle
, &contrlRect
) ;
395 int former_mac_x
= contrlRect
.left
;
396 int former_mac_y
= contrlRect
.top
;
398 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
400 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
) ;
406 void wxToolBarTool::UpdateToggleImage( bool toggle
)
408 #if wxMAC_USE_NATIVE_TOOLBAR
410 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_4
411 #define kHIToolbarItemSelected (1 << 7)
414 // FIXME: this should be a OSX v10.4 runtime check
415 if (m_toolbarItemRef
!= NULL
)
417 OptionBits addAttrs
, removeAttrs
;
422 addAttrs
= kHIToolbarItemSelected
;
423 removeAttrs
= kHIToolbarItemNoAttributes
;
427 addAttrs
= kHIToolbarItemNoAttributes
;
428 removeAttrs
= kHIToolbarItemSelected
;
431 result
= HIToolbarItemChangeAttributes( m_toolbarItemRef
, addAttrs
, removeAttrs
);
438 int w
= m_bmpNormal
.GetWidth() ;
439 int h
= m_bmpNormal
.GetHeight() ;
440 wxBitmap
bmp( w
, h
) ;
442 dc
.SelectObject( bmp
) ;
443 dc
.SetPen( wxNullPen
) ;
444 dc
.SetBackground( *wxWHITE
) ;
445 dc
.DrawRectangle( 0 , 0 , w
, h
) ;
446 dc
.DrawBitmap( m_bmpNormal
, 0 , 0 , true) ;
447 dc
.SelectObject( wxNullBitmap
) ;
448 ControlButtonContentInfo info
;
449 wxMacCreateBitmapButton( &info
, bmp
) ;
450 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof( info
),
452 wxMacReleaseBitmapButton( &info
) ;
456 ControlButtonContentInfo info
;
457 wxMacCreateBitmapButton( &info
, m_bmpNormal
) ;
458 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof( info
),
460 wxMacReleaseBitmapButton( &info
) ;
463 IconTransformType transform
= toggle
? kTransformSelected
: kTransformNone
;
464 SetControlData( m_controlHandle
, 0, kControlIconTransformTag
, sizeof( transform
),
466 HIViewSetNeedsDisplay( m_controlHandle
, true ) ;
469 ::SetControl32BitValue( m_controlHandle
, toggle
) ;
473 wxToolBarTool::wxToolBarTool(wxToolBar
*tbar
,
475 const wxString
& label
,
476 const wxBitmap
& bmpNormal
,
477 const wxBitmap
& bmpDisabled
,
479 wxObject
*clientData
,
480 const wxString
& shortHelp
,
481 const wxString
& longHelp
)
482 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
483 clientData
, shortHelp
, longHelp
)
489 #pragma mark Toolbar Implementation
491 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
492 const wxString
& label
,
493 const wxBitmap
& bmpNormal
,
494 const wxBitmap
& bmpDisabled
,
496 wxObject
*clientData
,
497 const wxString
& shortHelp
,
498 const wxString
& longHelp
)
500 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
501 clientData
, shortHelp
, longHelp
);
504 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
506 return new wxToolBarTool(this, control
);
509 void wxToolBar::Init()
513 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
514 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
515 #if wxMAC_USE_NATIVE_TOOLBAR
516 m_macHIToolbarRef
= NULL
;
517 m_macUsesNativeToolbar
= false ;
521 // also for the toolbar we have the dual implementation:
522 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
524 bool wxToolBar::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
525 long style
, const wxString
& name
)
527 if ( !wxToolBarBase::Create( parent
, id
, pos
, size
, style
) )
532 #if wxMAC_USE_NATIVE_TOOLBAR
534 labelStr
.Format(wxT("%xd"), (int)this);
535 err
= HIToolbarCreate( wxMacCFStringHolder(labelStr
, wxFont::GetDefaultEncoding() ) , 0 ,
536 (HIToolbarRef
*) &m_macHIToolbarRef
);
538 if (m_macHIToolbarRef
!= NULL
)
540 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
541 HIToolbarDisplaySize displaySize
= kHIToolbarDisplaySizeSmall
;
543 if ( style
& wxTB_NOICONS
)
544 mode
= kHIToolbarDisplayModeLabelOnly
;
545 else if ( style
& wxTB_TEXT
)
546 mode
= kHIToolbarDisplayModeIconAndLabel
;
548 mode
= kHIToolbarDisplayModeIconOnly
;
550 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macHIToolbarRef
, mode
) ;
551 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, displaySize
) ;
558 wxToolBar::~wxToolBar()
560 #if wxMAC_USE_NATIVE_TOOLBAR
561 if ( m_macHIToolbarRef
)
563 // if this is the installed toolbar, then deinstall it
564 if (m_macUsesNativeToolbar
)
565 MacInstallNativeToolbar( false );
567 CFRelease( (HIToolbarRef
) m_macHIToolbarRef
);
568 m_macHIToolbarRef
= NULL
;
573 bool wxToolBar::Show( bool show
)
576 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
578 bResult
= (tlw
!= NULL
);
581 #if wxMAC_USE_NATIVE_TOOLBAR
582 bool ownToolbarInstalled
= false;
583 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
584 if (ownToolbarInstalled
)
586 bResult
= (HIViewIsVisible( (HIViewRef
)m_macHIToolbarRef
) != show
);
587 ShowHideWindowToolbar( tlw
, show
, false );
591 bResult
= wxToolBarBase::Show( show
);
597 bool wxToolBar::IsShown() const
601 #if wxMAC_USE_NATIVE_TOOLBAR
602 bool ownToolbarInstalled
;
603 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
604 if (ownToolbarInstalled
)
605 bResult
= HIViewIsVisible( (HIViewRef
)m_macHIToolbarRef
);
608 bResult
= wxToolBarBase::IsShown();
613 void wxToolBar::DoGetSize( int *width
, int *height
) const
615 #if wxMAC_USE_NATIVE_TOOLBAR
617 bool ownToolbarInstalled
;
619 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
620 if ( ownToolbarInstalled
)
622 // TODO is this really a control ?
623 GetControlBounds( (ControlRef
) m_macHIToolbarRef
, &boundsR
);
625 *width
= boundsR
.right
- boundsR
.left
;
626 if ( height
!= NULL
)
627 *height
= boundsR
.bottom
- boundsR
.top
;
631 wxToolBarBase::DoGetSize( width
, height
);
634 void wxToolBar::SetWindowStyleFlag( long style
)
636 wxToolBarBase::SetWindowStyleFlag( style
);
637 #if wxMAC_USE_NATIVE_TOOLBAR
638 if (m_macHIToolbarRef
!= NULL
)
640 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
642 if ( style
& wxTB_NOICONS
)
643 mode
= kHIToolbarDisplayModeLabelOnly
;
644 else if ( style
& wxTB_TEXT
)
645 mode
= kHIToolbarDisplayModeIconAndLabel
;
647 mode
= kHIToolbarDisplayModeIconOnly
;
649 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macHIToolbarRef
, mode
);
654 #if wxMAC_USE_NATIVE_TOOLBAR
655 bool wxToolBar::MacWantsNativeToolbar()
657 return m_macUsesNativeToolbar
;
660 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled
) const
662 bool bResultV
= false;
664 if (ownToolbarInstalled
!= NULL
)
665 *ownToolbarInstalled
= false;
667 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
670 HIToolbarRef curToolbarRef
= NULL
;
671 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
672 bResultV
= ((err
== 0) && (curToolbarRef
!= NULL
));
673 if (bResultV
&& (ownToolbarInstalled
!= NULL
))
674 *ownToolbarInstalled
= (curToolbarRef
== m_macHIToolbarRef
);
680 bool wxToolBar::MacInstallNativeToolbar(bool usesNative
)
682 bool bResult
= false;
684 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
688 if (usesNative
&& (m_macHIToolbarRef
== NULL
))
691 if (usesNative
&& ((GetWindowStyleFlag() & wxTB_VERTICAL
) != 0))
694 // check the existing toolbar
695 HIToolbarRef curToolbarRef
= NULL
;
696 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
698 curToolbarRef
= NULL
;
700 m_macUsesNativeToolbar
= usesNative
;
702 if (m_macUsesNativeToolbar
)
704 // only install toolbar if there isn't one installed already
705 if (curToolbarRef
== NULL
)
709 SetWindowToolbar( tlw
, (HIToolbarRef
) m_macHIToolbarRef
);
710 ShowHideWindowToolbar( tlw
, true, false );
711 ChangeWindowAttributes( tlw
, kWindowToolbarButtonAttribute
, 0 );
712 SetAutomaticControlDragTrackingEnabledForWindow( tlw
, true );
714 // FIXME: which is best, which is necessary?
716 // m_peer->SetVisibility( false, true );
719 Rect r
= { 0 , 0 , 0 , 0 };
722 m_peer
->SetRect( &r
);
724 // FIXME: which is best, which is necessary?
726 SetSize( wxSIZE_AUTO_WIDTH
, 0 );
728 m_peer
->SetVisibility( false, true );
729 wxToolBarBase::Show( false );
734 // only deinstall toolbar if this is the installed one
735 if (m_macHIToolbarRef
== curToolbarRef
)
739 ShowHideWindowToolbar( tlw
, false, false );
740 ChangeWindowAttributes( tlw
, 0 , kWindowToolbarButtonAttribute
);
741 SetWindowToolbar( tlw
, NULL
);
743 // FIXME: which is best, which is necessary?
744 m_peer
->SetVisibility( true, true );
747 // wxToolBarBase::Show( true );
753 InvalidateBestSize();
755 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
760 bool wxToolBar::Realize()
762 if (m_tools
.GetCount() == 0)
765 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
766 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
774 int maxToolWidth
= 0;
775 int maxToolHeight
= 0;
777 // find the maximum tool width and height
778 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
779 while ( node
!= NULL
)
781 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
785 wxSize sz
= tool
->GetSize();
787 if ( sz
.x
> maxToolWidth
)
789 if ( sz
.y
> maxToolHeight
)
790 maxToolHeight
= sz
.y
;
793 node
= node
->GetNext();
796 bool lastIsRadio
= false;
797 bool curIsRadio
= false;
798 bool setChoiceInGroup
= false;
800 node
= m_tools
.GetFirst();
801 while ( node
!= NULL
)
803 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
807 node
= node
->GetNext();
812 // for the moment just perform a single row/column alignment
813 wxSize cursize
= tool
->GetSize();
814 if ( x
+ cursize
.x
> maxWidth
)
815 maxWidth
= x
+ cursize
.x
;
816 if ( y
+ cursize
.y
> maxHeight
)
817 maxHeight
= y
+ cursize
.y
;
819 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
821 int x1
= x
+ ( maxToolWidth
- cursize
.x
) / 2;
822 tool
->SetPosition( wxPoint(x1
, y
) );
826 int y1
= y
+ ( maxToolHeight
- cursize
.y
) / 2;
827 tool
->SetPosition( wxPoint(x
, y1
) );
830 // update the item positioning state
831 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
832 y
+= cursize
.y
+ kwxMacToolSpacing
;
834 x
+= cursize
.x
+ kwxMacToolSpacing
;
836 #if wxMAC_USE_NATIVE_TOOLBAR
837 // install in native HIToolbar
838 if ( m_macHIToolbarRef
!= NULL
)
840 HIToolbarItemRef hiItemRef
= tool
->GetToolbarItemRef();
841 if ( hiItemRef
!= NULL
)
843 OSStatus result
= HIToolbarAppendItem( (HIToolbarRef
) m_macHIToolbarRef
, hiItemRef
);
846 InstallEventHandler( HIObjectGetEventTarget(hiItemRef
), GetwxMacToolBarEventHandlerUPP(),
847 GetEventTypeCount(toolBarEventList
), toolBarEventList
, tool
, NULL
);
853 // update radio button (and group) state
854 lastIsRadio
= curIsRadio
;
855 curIsRadio
= ( tool
->IsButton() && (tool
->GetKind() == wxITEM_RADIO
) );
859 if ( tool
->IsToggled() )
860 DoToggleTool( tool
, true );
862 setChoiceInGroup
= false;
868 if ( tool
->Toggle(true) )
870 DoToggleTool( tool
, true );
871 setChoiceInGroup
= true;
874 else if ( tool
->IsToggled() )
876 if ( tool
->IsToggled() )
877 DoToggleTool( tool
, true );
879 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
880 while ( nodePrev
!= NULL
)
882 wxToolBarToolBase
*toggleTool
= nodePrev
->GetData();
883 if ( (toggleTool
== NULL
) || !toggleTool
->IsButton() || (toggleTool
->GetKind() != wxITEM_RADIO
) )
886 if ( toggleTool
->Toggle(false) )
887 DoToggleTool( toggleTool
, false );
889 nodePrev
= nodePrev
->GetPrevious();
894 node
= node
->GetNext();
897 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
899 // if not set yet, only one row
900 if ( m_maxRows
<= 0 )
903 m_minWidth
= maxWidth
;
905 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
906 m_minHeight
= m_maxHeight
= maxHeight
;
910 // if not set yet, have one column
911 if ( (GetToolsCount() > 0) && (m_maxRows
<= 0) )
912 SetRows( GetToolsCount() );
914 m_minHeight
= maxHeight
;
916 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
917 m_minWidth
= m_maxWidth
= maxWidth
;
921 // FIXME: should this be OSX-only?
923 bool wantNativeToolbar
, ownToolbarInstalled
;
925 // attempt to install the native toolbar
926 wantNativeToolbar
= ((GetWindowStyleFlag() & wxTB_VERTICAL
) == 0);
927 MacInstallNativeToolbar( wantNativeToolbar
);
928 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
929 if (!ownToolbarInstalled
)
931 SetSize( maxWidth
, maxHeight
);
932 InvalidateBestSize();
936 SetSize( maxWidth
, maxHeight
);
937 InvalidateBestSize();
942 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
944 m_defaultWidth
= size
.x
+ kwxMacToolBorder
;
945 m_defaultHeight
= size
.y
+ kwxMacToolBorder
;
947 #if wxMAC_USE_NATIVE_TOOLBAR
948 if (m_macHIToolbarRef
!= NULL
)
950 int maxs
= wxMax( size
.x
, size
.y
);
951 HIToolbarDisplaySize sizeSpec
;
953 sizeSpec
= kHIToolbarDisplaySizeNormal
;
954 else if ( maxs
> 24 )
955 sizeSpec
= kHIToolbarDisplaySizeDefault
;
957 sizeSpec
= kHIToolbarDisplaySizeSmall
;
959 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, sizeSpec
);
964 // The button size is bigger than the bitmap size
965 wxSize
wxToolBar::GetToolSize() const
967 return wxSize(m_defaultWidth
+ kwxMacToolBorder
, m_defaultHeight
+ kwxMacToolBorder
);
970 void wxToolBar::SetRows(int nRows
)
972 // avoid resizing the frame uselessly
973 if ( nRows
!= m_maxRows
)
979 void wxToolBar::MacSuperChangedPosition()
981 wxWindow::MacSuperChangedPosition();
982 #if wxMAC_USE_NATIVE_TOOLBAR
983 if (! m_macUsesNativeToolbar
)
990 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
992 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
993 while ( node
!= NULL
)
995 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData() ;
999 wxRect2DInt
r( tool
->GetPosition(), tool
->GetSize() );
1000 if ( r
.Contains( wxPoint( x
, y
) ) )
1004 node
= node
->GetNext();
1007 return (wxToolBarToolBase
*)NULL
;
1010 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
1012 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
1014 return tool
->GetShortHelp() ;
1016 return wxEmptyString
;
1019 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
1022 ((wxToolBarTool
*)t
)->DoEnable( enable
) ;
1025 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
1027 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
1028 if ( ( tool
!= NULL
) && tool
->IsButton() )
1029 tool
->UpdateToggleImage( toggle
);
1032 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
1033 wxToolBarToolBase
*toolBase
)
1035 wxToolBarTool
* tool
= wx_static_cast( wxToolBarTool
* , toolBase
);
1039 WindowRef window
= (WindowRef
) MacGetTopLevelWindowRef();
1040 wxSize toolSize
= GetToolSize();
1041 Rect toolrect
= { 0, 0 , toolSize
.y
, toolSize
.x
};
1042 ControlRef controlHandle
= NULL
;
1045 switch (tool
->GetStyle())
1047 case wxTOOL_STYLE_SEPARATOR
:
1049 wxASSERT( tool
->GetControlHandle() == NULL
);
1052 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1053 toolrect
.bottom
= toolSize
.y
;
1055 toolrect
.right
= toolSize
.x
;
1057 #ifdef __WXMAC_OSX__
1058 // in flat style we need a visual separator
1059 #if wxMAC_USE_NATIVE_TOOLBAR
1060 HIToolbarItemRef item
;
1061 err
= HIToolbarItemCreate( kHIToolbarSeparatorIdentifier
, kHIToolbarItemCantBeRemoved
| kHIToolbarItemIsSeparator
| kHIToolbarItemAllowDuplicates
, &item
);
1063 tool
->SetToolbarItemRef( item
);
1065 CreateSeparatorControl( window
, &toolrect
, &controlHandle
);
1066 tool
->SetControlHandle( controlHandle
);
1071 case wxTOOL_STYLE_BUTTON
:
1073 wxASSERT( tool
->GetControlHandle() == NULL
) ;
1074 ControlButtonContentInfo info
;
1075 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap() , kControlContentIconRef
) ;
1077 if ( UMAGetSystemVersion() >= 0x1000)
1078 CreateIconControl( window
, &toolrect
, &info
, false , &controlHandle
) ;
1081 SInt16 behaviour
= kControlBehaviorOffsetContents
;
1082 if ( tool
->CanBeToggled() )
1083 behaviour
+= kControlBehaviorToggles
;
1084 CreateBevelButtonControl( window
, &toolrect
, CFSTR("") , kControlBevelButtonNormalBevel
, behaviour
, &info
,
1085 0 , 0 , 0 , &controlHandle
) ;
1088 #if wxMAC_USE_NATIVE_TOOLBAR
1089 HIToolbarItemRef item
;
1091 labelStr
.Format(wxT("%xd"), (int)tool
);
1092 err
= HIToolbarItemCreate(
1093 wxMacCFStringHolder(labelStr
, wxFont::GetDefaultEncoding()),
1094 kHIToolbarItemCantBeRemoved
| kHIToolbarItemAnchoredLeft
| kHIToolbarItemAllowDuplicates
, &item
);
1097 HIToolbarItemSetLabel( item
, wxMacCFStringHolder(tool
->GetLabel(), m_font
.GetEncoding()) );
1098 HIToolbarItemSetIconRef( item
, info
.u
.iconRef
);
1099 HIToolbarItemSetCommandID( item
, tool
->GetId() );
1100 tool
->SetToolbarItemRef( item
);
1104 wxMacReleaseBitmapButton( &info
) ;
1106 SetBevelButtonTextPlacement( m_controlHandle , kControlBevelButtonPlaceBelowGraphic ) ;
1107 UMASetControlTitle( m_controlHandle , label , wxFont::GetDefaultEncoding() ) ;
1110 InstallControlEventHandler( (ControlRef
) controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
1111 GetEventTypeCount(eventList
), eventList
, tool
, NULL
);
1113 tool
->SetControlHandle( controlHandle
);
1117 case wxTOOL_STYLE_CONTROL
:
1118 wxASSERT( tool
->GetControl() != NULL
);
1119 #if 0 // wxMAC_USE_NATIVE_TOOLBAR
1120 // FIXME: doesn't work yet...
1122 HIToolbarItemRef item
;
1124 labelStr
.Format( wxT("%xd"), (int) tool
);
1125 result
= HIToolbarItemCreate( wxMacCFStringHolder(labelStr
, wxFont::GetDefaultEncoding()),
1126 kHIToolbarItemCantBeRemoved
| kHIToolbarItemAnchoredLeft
| kHIToolbarItemAllowDuplicates
,
1130 HIToolbarItemSetLabel( item
, wxMacCFStringHolder(tool
->GetLabel(), m_font
.GetEncoding()) );
1131 HIToolbarItemSetCommandID( item
, tool
->GetId() );
1132 tool
->SetToolbarItemRef( item
);
1134 controlHandle
= ( ControlRef
) tool
->GetControlHandle();
1135 wxASSERT_MSG( controlHandle
!= NULL
, wxT("NULL tool control") );
1137 // FIXME: is this necessary ??
1138 ::GetControlBounds( controlHandle
, &toolrect
);
1139 UMAMoveControl( controlHandle
, -toolrect
.left
, -toolrect
.top
);
1141 // FIXME: is this necessary ??
1142 InstallControlEventHandler( controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
1143 GetEventTypeCount(eventList
), eventList
, tool
, NULL
);
1148 // FIXME: right now there's nothing to do here
1158 if ( controlHandle
)
1160 ControlRef container
= (ControlRef
) GetHandle();
1161 wxASSERT_MSG( container
!= NULL
, wxT("No valid mac container control") );
1163 UMAShowControl( controlHandle
);
1164 ::EmbedControl( controlHandle
, container
);
1167 if ( tool
->CanBeToggled() && tool
->IsToggled() )
1168 tool
->UpdateToggleImage( true );
1170 // nothing special to do here - we relayout in Realize() later
1172 InvalidateBestSize();
1177 errMsg
.Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long) err
);
1178 wxASSERT_MSG( false, errMsg
.c_str() );
1184 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1186 wxFAIL_MSG( _T("not implemented") );
1189 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolbase
)
1191 wxToolBarTool
* tool
= wx_static_cast( wxToolBarTool
* , toolbase
) ;
1192 wxToolBarToolsList::compatibility_iterator node
;
1193 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1195 wxToolBarToolBase
*tool2
= node
->GetData();
1196 if ( tool2
== tool
)
1198 // let node point to the next node in the list
1199 node
= node
->GetNext();
1205 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize() ;
1209 switch ( tool
->GetStyle() )
1211 case wxTOOL_STYLE_CONTROL
:
1213 tool
->GetControl()->Destroy();
1214 tool
->ClearControl() ;
1218 case wxTOOL_STYLE_BUTTON
:
1219 case wxTOOL_STYLE_SEPARATOR
:
1220 if ( tool
->GetControlHandle() )
1222 DisposeControl( (ControlRef
) tool
->GetControlHandle() ) ;
1223 #if wxMAC_USE_NATIVE_TOOLBAR
1224 if ( tool
->GetToolbarItemRef() )
1225 CFRelease( tool
->GetToolbarItemRef() ) ;
1227 tool
->ClearControl() ;
1235 // and finally reposition all the controls after this one
1237 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
1239 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
1240 wxPoint pt
= tool2
->GetPosition() ;
1242 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1247 tool2
->SetPosition( pt
) ;
1250 InvalidateBestSize();
1254 void wxToolBar::OnPaint(wxPaintEvent
& event
)
1256 #if wxMAC_USE_NATIVE_TOOLBAR
1257 if ( m_macUsesNativeToolbar
)
1264 wxPaintDC
dc(this) ;
1267 GetSize( &w
, &h
) ;
1268 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1269 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
1271 if ( UMAGetSystemVersion() >= 0x1030 )
1273 HIThemePlacardDrawInfo info
;
1274 memset( &info
, 0 , sizeof( info
) ) ;
1276 info
.state
= IsEnabled() ? kThemeStateActive
: kThemeStateInactive
;
1278 CGContextRef cgContext
= (CGContextRef
) MacGetCGContextRef() ;
1279 HIRect rect
= CGRectMake( 0 , 0 , w
, h
) ;
1280 HIThemeDrawPlacard( &rect
, & info
, cgContext
, kHIThemeOrientationNormal
) ;
1285 // leave the background as it is (striped or metal)
1288 wxMacPortSetter
helper(&dc
) ;
1290 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
1291 dc
.YLOG2DEVMAC(h
) , dc
.XLOG2DEVMAC(w
) } ;
1293 if( toolbarrect.left < 0 )
1294 toolbarrect.left = 0 ;
1295 if ( toolbarrect.top < 0 )
1296 toolbarrect.top = 0 ;
1298 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
1300 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
1304 #if TARGET_API_MAC_OSX
1305 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
1306 if ( UMAGetSystemVersion() >= 0x1030 )
1308 HIRect hiToolbarrect
= CGRectMake( dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
1309 dc
.YLOG2DEVREL(h
) , dc
.XLOG2DEVREL(w
) );
1310 CGContextRef cgContext
;
1312 GetPortBounds( (CGrafPtr
) dc
.m_macPort
, &bounds
) ;
1313 QDBeginCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
) ;
1314 CGContextTranslateCTM( cgContext
, 0 , bounds
.bottom
- bounds
.top
) ;
1315 CGContextScaleCTM( cgContext
, 1 , -1 ) ;
1318 HIThemeBackgroundDrawInfo drawInfo
;
1319 drawInfo
.version
= 0 ;
1320 drawInfo
.state
= kThemeStateActive
;
1321 drawInfo
.kind
= kThemeBackgroundMetal
;
1322 HIThemeApplyBackground( &hiToolbarrect
, &drawInfo
, cgContext
,kHIThemeOrientationNormal
) ;
1325 QDEndCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
) ;
1330 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
1339 #endif // wxUSE_TOOLBAR