1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: The wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
17 #include "wx/bitmap.h"
18 #include "wx/toolbar.h"
20 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
22 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
23 EVT_PAINT( wxToolBar::OnPaint
)
26 #include "wx/mac/uma.h"
27 #include "wx/geometry.h"
30 const short kwxMacToolBarToolDefaultWidth
= 16 ;
31 const short kwxMacToolBarToolDefaultHeight
= 16 ;
32 const short kwxMacToolBarTopMargin
= 4 ; // 1 ; // used to be 4
33 const short kwxMacToolBarLeftMargin
= 4 ; //1 ; // used to be 4
34 const short kwxMacToolBorder
= 0 ; // used to be 0
35 const short kwxMacToolSpacing
= 6 ; // 2 ; // used to be 6
37 const short kwxMacToolBarToolDefaultWidth
= 24 ;
38 const short kwxMacToolBarToolDefaultHeight
= 22 ;
39 const short kwxMacToolBarTopMargin
= 2 ;
40 const short kwxMacToolBarLeftMargin
= 2 ;
41 const short kwxMacToolBorder
= 4 ;
42 const short kwxMacToolSpacing
= 0 ;
46 #pragma mark Tool Implementation
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 // We have a dual implementation for each tool, ControlRef and HIToolbarItemRef
55 class wxToolBarTool
: public wxToolBarToolBase
58 wxToolBarTool(wxToolBar
*tbar
,
60 const wxString
& label
,
61 const wxBitmap
& bmpNormal
,
62 const wxBitmap
& bmpDisabled
,
65 const wxString
& shortHelp
,
66 const wxString
& longHelp
) ;
68 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
69 : wxToolBarToolBase(tbar
, control
)
73 SetControlHandle( (ControlRef
) control
->GetHandle() ) ;
79 if ( m_controlHandle
)
80 DisposeControl( m_controlHandle
) ;
81 #if wxMAC_USE_NATIVE_TOOLBAR
82 if ( m_toolbarItemRef
)
83 CFRelease( m_toolbarItemRef
) ;
87 WXWidget
GetControlHandle()
89 return (WXWidget
) m_controlHandle
;
92 void SetControlHandle( ControlRef handle
)
94 m_controlHandle
= handle
;
97 void SetPosition( const wxPoint
& position
) ;
102 #if wxMAC_USE_NATIVE_TOOLBAR
103 m_toolbarItemRef
= NULL
;
107 wxSize
GetSize() const
111 return GetControl()->GetSize() ;
113 else if ( IsButton() )
115 return GetToolBar()->GetToolSize() ;
120 wxSize sz
= GetToolBar()->GetToolSize() ;
121 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
128 wxPoint
GetPosition() const
130 return wxPoint(m_x
, m_y
);
132 bool DoEnable( bool enable
) ;
134 void UpdateToggleImage( bool toggle
) ;
136 #if wxMAC_USE_NATIVE_TOOLBAR
137 void SetToolbarItemRef( HIToolbarItemRef ref
)
139 if ( m_controlHandle
)
140 HideControl( m_controlHandle
) ;
141 if ( m_toolbarItemRef
)
142 CFRelease( m_toolbarItemRef
) ;
143 m_toolbarItemRef
= ref
;
144 if ( m_toolbarItemRef
)
146 HIToolbarItemSetHelpText(
147 m_toolbarItemRef
, wxMacCFStringHolder( GetShortHelp() , GetToolBar()->GetFont().GetEncoding() ) ,
148 wxMacCFStringHolder( GetLongHelp() , GetToolBar()->GetFont().GetEncoding() ) ) ;
151 HIToolbarItemRef
GetToolbarItemRef() const
153 return m_toolbarItemRef
;
156 void SetIndex( CFIndex idx
)
161 CFIndex
GetIndex() const
170 m_controlHandle
= NULL
;
171 #if wxMAC_USE_NATIVE_TOOLBAR
172 m_toolbarItemRef
= NULL
;
176 ControlRef m_controlHandle
;
177 #if wxMAC_USE_NATIVE_TOOLBAR
178 HIToolbarItemRef m_toolbarItemRef
;
179 // position in its toolbar, -1 means not inserted
186 static const EventTypeSpec eventList
[] =
188 { kEventClassControl
, kEventControlHit
} ,
190 { kEventClassControl
, kEventControlHitTest
} ,
194 static pascal OSStatus
wxMacToolBarToolControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
196 OSStatus result
= eventNotHandledErr
;
198 wxMacCarbonEvent
cEvent( event
) ;
200 ControlRef controlRef
;
202 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
) ;
204 switch( GetEventKind( event
) )
206 case kEventControlHit
:
208 wxToolBarTool
* tbartool
= (wxToolBarTool
*)data
;
209 wxToolBar
*tbar
= tbartool
!= NULL
? ( wxToolBar
* ) ( tbartool
->GetToolBar() ) : NULL
;
210 if ((tbartool
!= NULL
) && tbartool
->CanBeToggled() )
214 shouldToggle
= !tbartool
->IsToggled();
216 shouldToggle
= ( GetControl32BitValue((ControlRef
) tbartool
->GetControlHandle()) != 0 );
218 tbar
->ToggleTool( tbartool
->GetId(), shouldToggle
);
220 if (tbartool
!= NULL
)
221 tbar
->OnLeftClick( tbartool
->GetId(), tbartool
->IsToggled() );
227 case kEventControlHitTest
:
229 HIPoint pt
= cEvent
.GetParameter
<HIPoint
>(kEventParamMouseLocation
) ;
231 HIViewGetBounds( controlRef
, &rect
) ;
233 ControlPartCode pc
= kControlNoPart
;
234 if ( CGRectContainsPoint( rect
, pt
) )
235 pc
= kControlIconPart
;
236 cEvent
.SetParameter( kEventParamControlPart
, typeControlPartCode
, pc
) ;
248 static pascal OSStatus
wxMacToolBarToolEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
250 OSStatus result
= eventNotHandledErr
;
252 switch ( GetEventClass( event
) )
254 case kEventClassControl
:
255 result
= wxMacToolBarToolControlEventHandler( handler
, event
, data
) ;
264 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler
)
266 #if wxMAC_USE_NATIVE_TOOLBAR
272 static const EventTypeSpec toolBarEventList
[] =
274 { kEventClassToolbarItem
, kEventToolbarItemPerformAction
} ,
277 static pascal OSStatus
wxMacToolBarCommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
279 OSStatus result
= eventNotHandledErr
;
281 switch( GetEventKind( event
) )
283 case kEventToolbarItemPerformAction
:
285 wxToolBarTool
* tbartool
= (wxToolBarTool
*) data
;
286 if ( tbartool
!= NULL
)
288 int toolID
= tbartool
->GetId();
289 wxToolBar
*tbar
= ( wxToolBar
* ) ( tbartool
->GetToolBar() );
290 if ( tbartool
->CanBeToggled() )
293 tbar
->ToggleTool(toolID
, !tbartool
->IsToggled() );
296 tbar
->OnLeftClick( toolID
, tbartool
-> IsToggled() ) ;
308 static pascal OSStatus
wxMacToolBarEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
310 OSStatus result
= eventNotHandledErr
;
311 switch( GetEventClass( event
) )
313 case kEventClassToolbarItem
:
314 result
= wxMacToolBarCommandEventHandler( handler
, event
, data
) ;
323 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarEventHandler
)
327 // ============================================================================
329 // ============================================================================
331 // ----------------------------------------------------------------------------
333 // ----------------------------------------------------------------------------
335 bool wxToolBarTool::DoEnable(bool enable
)
339 GetControl()->Enable( enable
) ;
341 else if ( IsButton() )
343 #if wxMAC_USE_NATIVE_TOOLBAR
344 if ( m_toolbarItemRef
)
345 HIToolbarItemSetEnabled( m_toolbarItemRef
, enable
) ;
348 if ( m_controlHandle
)
350 #if TARGET_API_MAC_OSX
352 EnableControl( m_controlHandle
) ;
354 DisableControl( m_controlHandle
) ;
357 ActivateControl( m_controlHandle
) ;
359 DeactivateControl( m_controlHandle
) ;
366 void wxToolBarTool::SetPosition(const wxPoint
& position
)
373 int mac_x
= position
.x
;
374 int mac_y
= position
.y
;
376 if ( ! GetToolBar()->MacGetTopLevelWindow()->MacUsesCompositing() )
378 GetToolBar()->MacWindowToRootWindow( &x
, &y
) ;
386 GetControlBounds( m_controlHandle
, &contrlRect
) ;
387 int former_mac_x
= contrlRect
.left
;
388 int former_mac_y
= contrlRect
.top
;
389 GetToolBar()->GetToolSize() ;
391 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
393 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
) ;
396 else if ( IsControl() )
398 GetControl()->Move( position
) ;
405 GetControlBounds( m_controlHandle
, &contrlRect
) ;
406 int former_mac_x
= contrlRect
.left
;
407 int former_mac_y
= contrlRect
.top
;
409 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
411 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
) ;
417 void wxToolBarTool::UpdateToggleImage( bool toggle
)
419 #if wxMAC_USE_NATIVE_TOOLBAR
421 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_4
422 #define kHIToolbarItemSelected (1 << 7)
425 // FIXME: this should be a OSX v10.4 runtime check
426 if (m_toolbarItemRef
!= NULL
)
428 OptionBits addAttrs
, removeAttrs
;
433 addAttrs
= kHIToolbarItemSelected
;
434 removeAttrs
= kHIToolbarItemNoAttributes
;
438 addAttrs
= kHIToolbarItemNoAttributes
;
439 removeAttrs
= kHIToolbarItemSelected
;
442 result
= HIToolbarItemChangeAttributes( m_toolbarItemRef
, addAttrs
, removeAttrs
);
449 int w
= m_bmpNormal
.GetWidth() ;
450 int h
= m_bmpNormal
.GetHeight() ;
451 wxBitmap
bmp( w
, h
) ;
453 dc
.SelectObject( bmp
) ;
454 dc
.SetPen( wxNullPen
) ;
455 dc
.SetBackground( *wxWHITE
) ;
456 dc
.DrawRectangle( 0 , 0 , w
, h
) ;
457 dc
.DrawBitmap( m_bmpNormal
, 0 , 0 , true) ;
458 dc
.SelectObject( wxNullBitmap
) ;
459 ControlButtonContentInfo info
;
460 wxMacCreateBitmapButton( &info
, bmp
) ;
461 SetControlData( m_controlHandle
, 0, kControlIconContentTag
,
462 sizeof( info
), (Ptr
)&info
);
463 wxMacReleaseBitmapButton( &info
) ;
467 ControlButtonContentInfo info
;
468 wxMacCreateBitmapButton( &info
, m_bmpNormal
) ;
469 SetControlData( m_controlHandle
, 0, kControlIconContentTag
,
470 sizeof( info
), (Ptr
)&info
);
471 wxMacReleaseBitmapButton( &info
) ;
474 IconTransformType transform
= toggle
? kTransformSelected
: kTransformNone
;
475 SetControlData( m_controlHandle
, 0, kControlIconTransformTag
,
476 sizeof( transform
), (Ptr
)&transform
);
477 HIViewSetNeedsDisplay( m_controlHandle
, true ) ;
480 ::SetControl32BitValue( m_controlHandle
, toggle
) ;
484 wxToolBarTool::wxToolBarTool(wxToolBar
*tbar
,
486 const wxString
& label
,
487 const wxBitmap
& bmpNormal
,
488 const wxBitmap
& bmpDisabled
,
490 wxObject
*clientData
,
491 const wxString
& shortHelp
,
492 const wxString
& longHelp
)
493 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
494 clientData
, shortHelp
, longHelp
)
500 #pragma mark Toolbar Implementation
502 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
503 const wxString
& label
,
504 const wxBitmap
& bmpNormal
,
505 const wxBitmap
& bmpDisabled
,
507 wxObject
*clientData
,
508 const wxString
& shortHelp
,
509 const wxString
& longHelp
)
511 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
512 clientData
, shortHelp
, longHelp
);
515 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
517 return new wxToolBarTool(this, control
);
520 void wxToolBar::Init()
524 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
525 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
526 #if wxMAC_USE_NATIVE_TOOLBAR
527 m_macHIToolbarRef
= NULL
;
528 m_macUsesNativeToolbar
= false ;
532 // also for the toolbar we have the dual implementation:
533 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
535 bool wxToolBar::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
536 long style
, const wxString
& name
)
538 if ( !wxToolBarBase::Create( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
543 #if wxMAC_USE_NATIVE_TOOLBAR
544 wxString labelStr
= wxString::Format(wxT("%xd"), (int)this);
545 err
= HIToolbarCreate( wxMacCFStringHolder(labelStr
, wxFont::GetDefaultEncoding() ) , 0 ,
546 (HIToolbarRef
*) &m_macHIToolbarRef
);
548 if (m_macHIToolbarRef
!= NULL
)
550 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
551 HIToolbarDisplaySize displaySize
= kHIToolbarDisplaySizeSmall
;
553 if ( style
& wxTB_NOICONS
)
554 mode
= kHIToolbarDisplayModeLabelOnly
;
555 else if ( style
& wxTB_TEXT
)
556 mode
= kHIToolbarDisplayModeIconAndLabel
;
558 mode
= kHIToolbarDisplayModeIconOnly
;
560 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macHIToolbarRef
, mode
) ;
561 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, displaySize
) ;
568 wxToolBar::~wxToolBar()
570 #if wxMAC_USE_NATIVE_TOOLBAR
571 if ( m_macHIToolbarRef
)
573 // if this is the installed toolbar, then deinstall it
574 if (m_macUsesNativeToolbar
)
575 MacInstallNativeToolbar( false );
577 CFRelease( (HIToolbarRef
) m_macHIToolbarRef
);
578 m_macHIToolbarRef
= NULL
;
583 bool wxToolBar::Show( bool show
)
586 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
588 bResult
= (tlw
!= NULL
);
591 #if wxMAC_USE_NATIVE_TOOLBAR
592 bool ownToolbarInstalled
= false;
593 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
594 if (ownToolbarInstalled
)
596 bResult
= ( IsWindowToolbarVisible(tlw
) != show
);
598 ShowHideWindowToolbar( tlw
, show
, false );
602 bResult
= wxToolBarBase::Show( show
);
608 bool wxToolBar::IsShown() const
612 #if wxMAC_USE_NATIVE_TOOLBAR
613 bool ownToolbarInstalled
;
614 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
615 if (ownToolbarInstalled
)
617 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
618 bResult
= IsWindowToolbarVisible(tlw
) ;
622 bResult
= wxToolBarBase::IsShown();
627 void wxToolBar::DoGetSize( int *width
, int *height
) const
629 #if wxMAC_USE_NATIVE_TOOLBAR
631 bool ownToolbarInstalled
;
633 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
634 if ( ownToolbarInstalled
)
636 // TODO is this really a control ?
637 GetControlBounds( (ControlRef
) m_macHIToolbarRef
, &boundsR
);
639 *width
= boundsR
.right
- boundsR
.left
;
640 if ( height
!= NULL
)
641 *height
= boundsR
.bottom
- boundsR
.top
;
645 wxToolBarBase::DoGetSize( width
, height
);
648 wxSize
wxToolBar::DoGetBestSize() const
651 DoGetSize( &width
, &height
) ;
652 return wxSize( width
, height
) ;
655 void wxToolBar::SetWindowStyleFlag( long style
)
657 wxToolBarBase::SetWindowStyleFlag( style
);
658 #if wxMAC_USE_NATIVE_TOOLBAR
659 if (m_macHIToolbarRef
!= NULL
)
661 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
663 if ( style
& wxTB_NOICONS
)
664 mode
= kHIToolbarDisplayModeLabelOnly
;
665 else if ( style
& wxTB_TEXT
)
666 mode
= kHIToolbarDisplayModeIconAndLabel
;
668 mode
= kHIToolbarDisplayModeIconOnly
;
670 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macHIToolbarRef
, mode
);
675 #if wxMAC_USE_NATIVE_TOOLBAR
676 bool wxToolBar::MacWantsNativeToolbar()
678 return m_macUsesNativeToolbar
;
681 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled
) const
683 bool bResultV
= false;
685 if (ownToolbarInstalled
!= NULL
)
686 *ownToolbarInstalled
= false;
688 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
691 HIToolbarRef curToolbarRef
= NULL
;
692 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
693 bResultV
= ((err
== 0) && (curToolbarRef
!= NULL
));
694 if (bResultV
&& (ownToolbarInstalled
!= NULL
))
695 *ownToolbarInstalled
= (curToolbarRef
== m_macHIToolbarRef
);
701 bool wxToolBar::MacInstallNativeToolbar(bool usesNative
)
703 bool bResult
= false;
705 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
709 if (usesNative
&& (m_macHIToolbarRef
== NULL
))
712 if (usesNative
&& ((GetWindowStyleFlag() & wxTB_VERTICAL
) != 0))
715 // check the existing toolbar
716 HIToolbarRef curToolbarRef
= NULL
;
717 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
719 curToolbarRef
= NULL
;
721 m_macUsesNativeToolbar
= usesNative
;
723 if (m_macUsesNativeToolbar
)
725 // only install toolbar if there isn't one installed already
726 if (curToolbarRef
== NULL
)
730 SetWindowToolbar( tlw
, (HIToolbarRef
) m_macHIToolbarRef
);
731 ShowHideWindowToolbar( tlw
, true, false );
732 ChangeWindowAttributes( tlw
, kWindowToolbarButtonAttribute
, 0 );
733 SetAutomaticControlDragTrackingEnabledForWindow( tlw
, true );
735 // FIXME: which is best, which is necessary?
737 // m_peer->SetVisibility( false, true );
740 Rect r
= { 0 , 0 , 0 , 0 };
743 m_peer
->SetRect( &r
);
745 // FIXME: which is best, which is necessary?
747 SetSize( wxSIZE_AUTO_WIDTH
, 0 );
749 m_peer
->SetVisibility( false, true );
750 wxToolBarBase::Show( false );
755 // only deinstall toolbar if this is the installed one
756 if (m_macHIToolbarRef
== curToolbarRef
)
760 ShowHideWindowToolbar( tlw
, false, false );
761 ChangeWindowAttributes( tlw
, 0 , kWindowToolbarButtonAttribute
);
762 SetWindowToolbar( tlw
, NULL
);
764 // FIXME: which is best, which is necessary?
765 m_peer
->SetVisibility( true, true );
768 // wxToolBarBase::Show( true );
774 InvalidateBestSize();
776 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
781 bool wxToolBar::Realize()
783 if (m_tools
.GetCount() == 0)
786 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
787 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
795 int maxToolWidth
= 0;
796 int maxToolHeight
= 0;
798 // find the maximum tool width and height
799 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
800 while ( node
!= NULL
)
802 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
806 wxSize sz
= tool
->GetSize();
808 if ( sz
.x
> maxToolWidth
)
810 if ( sz
.y
> maxToolHeight
)
811 maxToolHeight
= sz
.y
;
814 node
= node
->GetNext();
817 bool lastIsRadio
= false;
818 bool curIsRadio
= false;
819 bool setChoiceInGroup
= false;
821 node
= m_tools
.GetFirst();
823 #if wxMAC_USE_NATIVE_TOOLBAR
824 CFIndex currentPosition
= 0 ;
825 bool insertAll
= false ;
826 #endif // wxMAC_USE_NATIVE_TOOLBAR
828 while ( node
!= NULL
)
830 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
834 node
= node
->GetNext();
839 // for the moment just perform a single row/column alignment
840 wxSize cursize
= tool
->GetSize();
841 if ( x
+ cursize
.x
> maxWidth
)
842 maxWidth
= x
+ cursize
.x
;
843 if ( y
+ cursize
.y
> maxHeight
)
844 maxHeight
= y
+ cursize
.y
;
846 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
848 int x1
= x
+ ( maxToolWidth
- cursize
.x
) / 2;
849 tool
->SetPosition( wxPoint(x1
, y
) );
853 int y1
= y
+ ( maxToolHeight
- cursize
.y
) / 2;
854 tool
->SetPosition( wxPoint(x
, y1
) );
857 // update the item positioning state
858 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
859 y
+= cursize
.y
+ kwxMacToolSpacing
;
861 x
+= cursize
.x
+ kwxMacToolSpacing
;
863 #if wxMAC_USE_NATIVE_TOOLBAR
864 // install in native HIToolbar
865 if ( m_macHIToolbarRef
!= NULL
)
867 HIToolbarItemRef hiItemRef
= tool
->GetToolbarItemRef();
868 if ( hiItemRef
!= NULL
)
870 if ( tool
->GetIndex() != currentPosition
|| insertAll
== true )
872 OSStatus err
= noErr
;
875 // if this is the first tool that gets newly inserted or repositioned
876 // first remove all 'old' tools from here to the right, because of this
877 // all following tools will have to be reinserted (insertAll). i = 100 because there's
878 // no way to determine how many there are in a toolbar, so just a high number :-(
879 for ( CFIndex i
= 100 ; i
>= currentPosition
; --i
)
881 err
= HIToolbarRemoveItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, i
) ;
883 wxASSERT_MSG( err
== noErr
, _T("HIToolbarRemoveItemAtIndex failed") );
886 err
= HIToolbarInsertItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, hiItemRef
, currentPosition
) ;
887 wxASSERT_MSG( err
== noErr
, _T("HIToolbarInsertItemAtIndex failed") );
888 tool
->SetIndex( currentPosition
) ;
893 #endif // wxMAC_USE_NATIVE_TOOLBAR
895 // update radio button (and group) state
896 lastIsRadio
= curIsRadio
;
897 curIsRadio
= ( tool
->IsButton() && (tool
->GetKind() == wxITEM_RADIO
) );
901 if ( tool
->IsToggled() )
902 DoToggleTool( tool
, true );
904 setChoiceInGroup
= false;
910 if ( tool
->Toggle(true) )
912 DoToggleTool( tool
, true );
913 setChoiceInGroup
= true;
916 else if ( tool
->IsToggled() )
918 if ( tool
->IsToggled() )
919 DoToggleTool( tool
, true );
921 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
922 while ( nodePrev
!= NULL
)
924 wxToolBarToolBase
*toggleTool
= nodePrev
->GetData();
925 if ( (toggleTool
== NULL
) || !toggleTool
->IsButton() || (toggleTool
->GetKind() != wxITEM_RADIO
) )
928 if ( toggleTool
->Toggle(false) )
929 DoToggleTool( toggleTool
, false );
931 nodePrev
= nodePrev
->GetPrevious();
936 node
= node
->GetNext();
939 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
941 // if not set yet, only one row
942 if ( m_maxRows
<= 0 )
945 m_minWidth
= maxWidth
;
947 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
948 m_minHeight
= m_maxHeight
= maxHeight
;
952 // if not set yet, have one column
953 if ( (GetToolsCount() > 0) && (m_maxRows
<= 0) )
954 SetRows( GetToolsCount() );
956 m_minHeight
= maxHeight
;
958 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
959 m_minWidth
= m_maxWidth
= maxWidth
;
963 // FIXME: should this be OSX-only?
965 bool wantNativeToolbar
, ownToolbarInstalled
;
967 // attempt to install the native toolbar
968 wantNativeToolbar
= ((GetWindowStyleFlag() & wxTB_VERTICAL
) == 0);
969 MacInstallNativeToolbar( wantNativeToolbar
);
970 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
971 if (!ownToolbarInstalled
)
973 SetSize( maxWidth
, maxHeight
);
974 InvalidateBestSize();
978 SetSize( maxWidth
, maxHeight
);
979 InvalidateBestSize();
982 SetBestFittingSize();
987 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
989 m_defaultWidth
= size
.x
+ kwxMacToolBorder
;
990 m_defaultHeight
= size
.y
+ kwxMacToolBorder
;
992 #if wxMAC_USE_NATIVE_TOOLBAR
993 if (m_macHIToolbarRef
!= NULL
)
995 int maxs
= wxMax( size
.x
, size
.y
);
996 HIToolbarDisplaySize sizeSpec
;
998 sizeSpec
= kHIToolbarDisplaySizeNormal
;
999 else if ( maxs
> 24 )
1000 sizeSpec
= kHIToolbarDisplaySizeDefault
;
1002 sizeSpec
= kHIToolbarDisplaySizeSmall
;
1004 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, sizeSpec
);
1009 // The button size is bigger than the bitmap size
1010 wxSize
wxToolBar::GetToolSize() const
1012 return wxSize(m_defaultWidth
+ kwxMacToolBorder
, m_defaultHeight
+ kwxMacToolBorder
);
1015 void wxToolBar::SetRows(int nRows
)
1017 // avoid resizing the frame uselessly
1018 if ( nRows
!= m_maxRows
)
1024 void wxToolBar::MacSuperChangedPosition()
1026 wxWindow::MacSuperChangedPosition();
1027 #if wxMAC_USE_NATIVE_TOOLBAR
1028 if (! m_macUsesNativeToolbar
)
1035 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1037 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
1038 while ( node
!= NULL
)
1040 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData() ;
1044 wxRect2DInt
r( tool
->GetPosition(), tool
->GetSize() );
1045 if ( r
.Contains( wxPoint( x
, y
) ) )
1049 node
= node
->GetNext();
1052 return (wxToolBarToolBase
*)NULL
;
1055 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
1057 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
1059 return tool
->GetShortHelp() ;
1061 return wxEmptyString
;
1064 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
1067 ((wxToolBarTool
*)t
)->DoEnable( enable
) ;
1070 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
1072 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
1073 if ( ( tool
!= NULL
) && tool
->IsButton() )
1074 tool
->UpdateToggleImage( toggle
);
1077 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
1078 wxToolBarToolBase
*toolBase
)
1080 wxToolBarTool
* tool
= wx_static_cast( wxToolBarTool
* , toolBase
);
1084 WindowRef window
= (WindowRef
) MacGetTopLevelWindowRef();
1085 wxSize toolSize
= GetToolSize();
1086 Rect toolrect
= { 0, 0 , toolSize
.y
, toolSize
.x
};
1087 ControlRef controlHandle
= NULL
;
1090 switch (tool
->GetStyle())
1092 case wxTOOL_STYLE_SEPARATOR
:
1094 wxASSERT( tool
->GetControlHandle() == NULL
);
1097 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1098 toolrect
.bottom
= toolSize
.y
;
1100 toolrect
.right
= toolSize
.x
;
1102 #ifdef __WXMAC_OSX__
1103 // in flat style we need a visual separator
1104 #if wxMAC_USE_NATIVE_TOOLBAR
1105 HIToolbarItemRef item
;
1106 err
= HIToolbarItemCreate( kHIToolbarSeparatorIdentifier
, kHIToolbarItemCantBeRemoved
| kHIToolbarItemIsSeparator
| kHIToolbarItemAllowDuplicates
, &item
);
1108 tool
->SetToolbarItemRef( item
);
1109 #endif // wxMAC_USE_NATIVE_TOOLBAR
1110 CreateSeparatorControl( window
, &toolrect
, &controlHandle
);
1111 tool
->SetControlHandle( controlHandle
);
1112 #endif // __WXMAC_OSX__
1116 case wxTOOL_STYLE_BUTTON
:
1118 wxASSERT( tool
->GetControlHandle() == NULL
) ;
1119 ControlButtonContentInfo info
;
1120 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap() , kControlContentIconRef
) ;
1122 if ( UMAGetSystemVersion() >= 0x1000)
1123 CreateIconControl( window
, &toolrect
, &info
, false , &controlHandle
) ;
1126 SInt16 behaviour
= kControlBehaviorOffsetContents
;
1127 if ( tool
->CanBeToggled() )
1128 behaviour
+= kControlBehaviorToggles
;
1129 CreateBevelButtonControl( window
, &toolrect
, CFSTR("") ,
1130 kControlBevelButtonNormalBevel
,
1132 0 , 0 , 0 , &controlHandle
) ;
1135 #if wxMAC_USE_NATIVE_TOOLBAR
1136 HIToolbarItemRef item
;
1137 wxString labelStr
= wxString::Format(wxT("%xd"), (int)tool
);
1138 err
= HIToolbarItemCreate(
1139 wxMacCFStringHolder(labelStr
, wxFont::GetDefaultEncoding()),
1140 kHIToolbarItemCantBeRemoved
| kHIToolbarItemAnchoredLeft
| kHIToolbarItemAllowDuplicates
, &item
);
1143 InstallEventHandler( HIObjectGetEventTarget(item
), GetwxMacToolBarEventHandlerUPP(),
1144 GetEventTypeCount(toolBarEventList
), toolBarEventList
, tool
, NULL
);
1145 HIToolbarItemSetLabel( item
, wxMacCFStringHolder(tool
->GetLabel(), m_font
.GetEncoding()) );
1146 HIToolbarItemSetIconRef( item
, info
.u
.iconRef
);
1147 HIToolbarItemSetCommandID( item
, kHIToolbarCommandPressAction
);
1148 tool
->SetToolbarItemRef( item
);
1150 #endif // wxMAC_USE_NATIVE_TOOLBAR
1152 wxMacReleaseBitmapButton( &info
) ;
1154 SetBevelButtonTextPlacement( m_controlHandle , kControlBevelButtonPlaceBelowGraphic ) ;
1155 UMASetControlTitle( m_controlHandle , label , wxFont::GetDefaultEncoding() ) ;
1158 InstallControlEventHandler( (ControlRef
) controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
1159 GetEventTypeCount(eventList
), eventList
, tool
, NULL
);
1161 tool
->SetControlHandle( controlHandle
);
1165 case wxTOOL_STYLE_CONTROL
:
1166 wxASSERT( tool
->GetControl() != NULL
);
1167 #if 0 // wxMAC_USE_NATIVE_TOOLBAR
1168 // FIXME: doesn't work yet...
1170 HIToolbarItemRef item
;
1171 wxString labelStr
= wxString::Format( wxT("%xd"), (int) tool
);
1172 result
= HIToolbarItemCreate( wxMacCFStringHolder(labelStr
, wxFont::GetDefaultEncoding()),
1173 kHIToolbarItemCantBeRemoved
| kHIToolbarItemAnchoredLeft
| kHIToolbarItemAllowDuplicates
,
1177 HIToolbarItemSetLabel( item
, wxMacCFStringHolder(tool
->GetLabel(), m_font
.GetEncoding()) );
1178 HIToolbarItemSetCommandID( item
, tool
->GetId() );
1179 tool
->SetToolbarItemRef( item
);
1181 controlHandle
= ( ControlRef
) tool
->GetControlHandle();
1182 wxASSERT_MSG( controlHandle
!= NULL
, wxT("NULL tool control") );
1184 // FIXME: is this necessary ??
1185 ::GetControlBounds( controlHandle
, &toolrect
);
1186 UMAMoveControl( controlHandle
, -toolrect
.left
, -toolrect
.top
);
1188 // FIXME: is this necessary ??
1189 InstallControlEventHandler( controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
1190 GetEventTypeCount(eventList
), eventList
, tool
, NULL
);
1195 // FIXME: right now there's nothing to do here
1205 if ( controlHandle
)
1207 ControlRef container
= (ControlRef
) GetHandle();
1208 wxASSERT_MSG( container
!= NULL
, wxT("No valid mac container control") );
1210 UMAShowControl( controlHandle
);
1211 ::EmbedControl( controlHandle
, container
);
1214 if ( tool
->CanBeToggled() && tool
->IsToggled() )
1215 tool
->UpdateToggleImage( true );
1217 // nothing special to do here - we relayout in Realize() later
1219 InvalidateBestSize();
1223 wxString errMsg
= wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long) err
);
1224 wxASSERT_MSG( false, errMsg
.c_str() );
1230 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1232 wxFAIL_MSG( _T("not implemented") );
1235 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolbase
)
1237 wxToolBarTool
* tool
= wx_static_cast( wxToolBarTool
* , toolbase
) ;
1238 wxToolBarToolsList::compatibility_iterator node
;
1239 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1241 wxToolBarToolBase
*tool2
= node
->GetData();
1242 if ( tool2
== tool
)
1244 // let node point to the next node in the list
1245 node
= node
->GetNext();
1251 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize() ;
1255 #if wxMAC_USE_NATIVE_TOOLBAR
1256 CFIndex removeIndex
= tool
->GetIndex();
1257 #endif // wxMAC_USE_NATIVE_TOOLBAR
1259 switch ( tool
->GetStyle() )
1261 case wxTOOL_STYLE_CONTROL
:
1263 tool
->GetControl()->Destroy();
1264 tool
->ClearControl() ;
1268 case wxTOOL_STYLE_BUTTON
:
1269 case wxTOOL_STYLE_SEPARATOR
:
1270 if ( tool
->GetControlHandle() )
1272 DisposeControl( (ControlRef
) tool
->GetControlHandle() ) ;
1273 #if wxMAC_USE_NATIVE_TOOLBAR
1274 if ( removeIndex
!= -1 && m_macHIToolbarRef
)
1276 HIToolbarRemoveItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, removeIndex
) ;
1277 tool
->SetIndex( -1 ) ;
1279 #endif // wxMAC_USE_NATIVE_TOOLBAR
1280 tool
->ClearControl() ;
1288 // and finally reposition all the controls after this one
1290 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
1292 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
1293 wxPoint pt
= tool2
->GetPosition() ;
1295 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1300 tool2
->SetPosition( pt
) ;
1302 #if wxMAC_USE_NATIVE_TOOLBAR
1303 if ( removeIndex
!= -1 && tool2
->GetIndex() > removeIndex
)
1304 tool2
->SetIndex( tool2
->GetIndex() - 1 ) ;
1309 InvalidateBestSize();
1313 void wxToolBar::OnPaint(wxPaintEvent
& event
)
1315 #if wxMAC_USE_NATIVE_TOOLBAR
1316 if ( m_macUsesNativeToolbar
)
1323 wxPaintDC
dc(this) ;
1326 GetSize( &w
, &h
) ;
1327 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1328 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
1330 if ( UMAGetSystemVersion() >= 0x1030 )
1332 HIThemePlacardDrawInfo info
;
1333 memset( &info
, 0 , sizeof( info
) ) ;
1335 info
.state
= IsEnabled() ? kThemeStateActive
: kThemeStateInactive
;
1337 CGContextRef cgContext
= (CGContextRef
) MacGetCGContextRef() ;
1338 HIRect rect
= CGRectMake( 0 , 0 , w
, h
) ;
1339 HIThemeDrawPlacard( &rect
, & info
, cgContext
, kHIThemeOrientationNormal
) ;
1344 // leave the background as it is (striped or metal)
1347 wxMacPortSetter
helper(&dc
) ;
1349 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
1350 dc
.YLOG2DEVMAC(h
) , dc
.XLOG2DEVMAC(w
) } ;
1352 if( toolbarrect.left < 0 )
1353 toolbarrect.left = 0 ;
1354 if ( toolbarrect.top < 0 )
1355 toolbarrect.top = 0 ;
1357 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
1359 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
1363 #if TARGET_API_MAC_OSX
1364 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
1365 if ( UMAGetSystemVersion() >= 0x1030 )
1367 HIRect hiToolbarrect
= CGRectMake( dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
1368 dc
.YLOG2DEVREL(h
) , dc
.XLOG2DEVREL(w
) );
1369 CGContextRef cgContext
;
1371 GetPortBounds( (CGrafPtr
) dc
.m_macPort
, &bounds
) ;
1372 QDBeginCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
) ;
1373 CGContextTranslateCTM( cgContext
, 0 , bounds
.bottom
- bounds
.top
) ;
1374 CGContextScaleCTM( cgContext
, 1 , -1 ) ;
1377 HIThemeBackgroundDrawInfo drawInfo
;
1378 drawInfo
.version
= 0 ;
1379 drawInfo
.state
= kThemeStateActive
;
1380 drawInfo
.kind
= kThemeBackgroundMetal
;
1381 HIThemeApplyBackground( &hiToolbarrect
, &drawInfo
, cgContext
,kHIThemeOrientationNormal
) ;
1384 QDEndCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
) ;
1389 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
1398 #endif // wxUSE_TOOLBAR