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
;
160 void SetIndex( CFIndex idx
)
165 CFIndex
GetIndex() const
174 m_controlHandle
= NULL
;
175 #if wxMAC_USE_NATIVE_TOOLBAR
176 m_toolbarItemRef
= NULL
;
180 ControlRef m_controlHandle
;
181 #if wxMAC_USE_NATIVE_TOOLBAR
182 HIToolbarItemRef m_toolbarItemRef
;
183 // position in its toolbar, -1 means not inserted
190 static const EventTypeSpec eventList
[] =
192 { kEventClassControl
, kEventControlHit
} ,
194 { kEventClassControl
, kEventControlHitTest
} ,
198 static pascal OSStatus
wxMacToolBarToolControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
200 OSStatus result
= eventNotHandledErr
;
202 wxMacCarbonEvent
cEvent( event
) ;
204 ControlRef controlRef
;
206 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
) ;
208 switch( GetEventKind( event
) )
210 case kEventControlHit
:
212 wxToolBarTool
* tbartool
= (wxToolBarTool
*)data
;
213 wxToolBar
*tbar
= tbartool
!= NULL
? ( wxToolBar
* ) ( tbartool
->GetToolBar() ) : NULL
;
214 if ((tbartool
!= NULL
) && tbartool
->CanBeToggled() )
218 shouldToggle
= !tbartool
->IsToggled();
220 shouldToggle
= ( GetControl32BitValue((ControlRef
) tbartool
->GetControlHandle()) != 0 );
222 tbar
->ToggleTool( tbartool
->GetId(), shouldToggle
);
224 if (tbartool
!= NULL
)
225 tbar
->OnLeftClick( tbartool
->GetId(), tbartool
->IsToggled() );
231 case kEventControlHitTest
:
233 HIPoint pt
= cEvent
.GetParameter
<HIPoint
>(kEventParamMouseLocation
) ;
235 HIViewGetBounds( controlRef
, &rect
) ;
237 ControlPartCode pc
= kControlNoPart
;
238 if ( CGRectContainsPoint( rect
, pt
) )
239 pc
= kControlIconPart
;
240 cEvent
.SetParameter( kEventParamControlPart
, typeControlPartCode
, pc
) ;
252 static pascal OSStatus
wxMacToolBarToolEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
254 OSStatus result
= eventNotHandledErr
;
256 switch ( GetEventClass( event
) )
258 case kEventClassControl
:
259 result
= wxMacToolBarToolControlEventHandler( handler
, event
, data
) ;
268 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler
)
270 #if wxMAC_USE_NATIVE_TOOLBAR
276 static const EventTypeSpec toolBarEventList
[] =
278 { kEventClassToolbarItem
, kEventToolbarItemPerformAction
} ,
281 static pascal OSStatus
wxMacToolBarCommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
283 OSStatus result
= eventNotHandledErr
;
285 switch( GetEventKind( event
) )
287 case kEventToolbarItemPerformAction
:
289 wxToolBarTool
* tbartool
= (wxToolBarTool
*) data
;
290 if ( tbartool
!= NULL
)
292 int toolID
= tbartool
->GetId();
293 wxToolBar
*tbar
= ( wxToolBar
* ) ( tbartool
->GetToolBar() );
294 if ( tbartool
->CanBeToggled() )
297 tbar
->ToggleTool(toolID
, !tbartool
->IsToggled() );
300 tbar
->OnLeftClick( toolID
, tbartool
-> IsToggled() ) ;
312 static pascal OSStatus
wxMacToolBarEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
314 OSStatus result
= eventNotHandledErr
;
315 switch( GetEventClass( event
) )
317 case kEventClassToolbarItem
:
318 result
= wxMacToolBarCommandEventHandler( handler
, event
, data
) ;
327 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarEventHandler
)
331 // ============================================================================
333 // ============================================================================
335 // ----------------------------------------------------------------------------
337 // ----------------------------------------------------------------------------
339 bool wxToolBarTool::DoEnable(bool enable
)
343 GetControl()->Enable( enable
) ;
345 else if ( IsButton() )
347 #if wxMAC_USE_NATIVE_TOOLBAR
348 if ( m_toolbarItemRef
)
349 HIToolbarItemSetEnabled( m_toolbarItemRef
, enable
) ;
352 if ( m_controlHandle
)
354 #if TARGET_API_MAC_OSX
356 EnableControl( m_controlHandle
) ;
358 DisableControl( m_controlHandle
) ;
361 ActivateControl( m_controlHandle
) ;
363 DeactivateControl( m_controlHandle
) ;
370 void wxToolBarTool::SetPosition(const wxPoint
& position
)
377 int mac_x
= position
.x
;
378 int mac_y
= position
.y
;
380 if ( ! GetToolBar()->MacGetTopLevelWindow()->MacUsesCompositing() )
382 GetToolBar()->MacWindowToRootWindow( &x
, &y
) ;
390 GetControlBounds( m_controlHandle
, &contrlRect
) ;
391 int former_mac_x
= contrlRect
.left
;
392 int former_mac_y
= contrlRect
.top
;
393 GetToolBar()->GetToolSize() ;
395 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
397 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
) ;
400 else if ( IsControl() )
402 GetControl()->Move( position
) ;
409 GetControlBounds( m_controlHandle
, &contrlRect
) ;
410 int former_mac_x
= contrlRect
.left
;
411 int former_mac_y
= contrlRect
.top
;
413 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
415 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
) ;
421 void wxToolBarTool::UpdateToggleImage( bool toggle
)
423 #if wxMAC_USE_NATIVE_TOOLBAR
425 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_4
426 #define kHIToolbarItemSelected (1 << 7)
429 // FIXME: this should be a OSX v10.4 runtime check
430 if (m_toolbarItemRef
!= NULL
)
432 OptionBits addAttrs
, removeAttrs
;
437 addAttrs
= kHIToolbarItemSelected
;
438 removeAttrs
= kHIToolbarItemNoAttributes
;
442 addAttrs
= kHIToolbarItemNoAttributes
;
443 removeAttrs
= kHIToolbarItemSelected
;
446 result
= HIToolbarItemChangeAttributes( m_toolbarItemRef
, addAttrs
, removeAttrs
);
453 int w
= m_bmpNormal
.GetWidth() ;
454 int h
= m_bmpNormal
.GetHeight() ;
455 wxBitmap
bmp( w
, h
) ;
457 dc
.SelectObject( bmp
) ;
458 dc
.SetPen( wxNullPen
) ;
459 dc
.SetBackground( *wxWHITE
) ;
460 dc
.DrawRectangle( 0 , 0 , w
, h
) ;
461 dc
.DrawBitmap( m_bmpNormal
, 0 , 0 , true) ;
462 dc
.SelectObject( wxNullBitmap
) ;
463 ControlButtonContentInfo info
;
464 wxMacCreateBitmapButton( &info
, bmp
) ;
465 SetControlData( m_controlHandle
, 0, kControlIconContentTag
,
466 sizeof( info
), (Ptr
)&info
);
467 wxMacReleaseBitmapButton( &info
) ;
471 ControlButtonContentInfo info
;
472 wxMacCreateBitmapButton( &info
, m_bmpNormal
) ;
473 SetControlData( m_controlHandle
, 0, kControlIconContentTag
,
474 sizeof( info
), (Ptr
)&info
);
475 wxMacReleaseBitmapButton( &info
) ;
478 IconTransformType transform
= toggle
? kTransformSelected
: kTransformNone
;
479 SetControlData( m_controlHandle
, 0, kControlIconTransformTag
,
480 sizeof( transform
), (Ptr
)&transform
);
481 HIViewSetNeedsDisplay( m_controlHandle
, true ) ;
484 ::SetControl32BitValue( m_controlHandle
, toggle
) ;
488 wxToolBarTool::wxToolBarTool(wxToolBar
*tbar
,
490 const wxString
& label
,
491 const wxBitmap
& bmpNormal
,
492 const wxBitmap
& bmpDisabled
,
494 wxObject
*clientData
,
495 const wxString
& shortHelp
,
496 const wxString
& longHelp
)
497 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
498 clientData
, shortHelp
, longHelp
)
504 #pragma mark Toolbar Implementation
506 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
507 const wxString
& label
,
508 const wxBitmap
& bmpNormal
,
509 const wxBitmap
& bmpDisabled
,
511 wxObject
*clientData
,
512 const wxString
& shortHelp
,
513 const wxString
& longHelp
)
515 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
516 clientData
, shortHelp
, longHelp
);
519 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
521 return new wxToolBarTool(this, control
);
524 void wxToolBar::Init()
528 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
529 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
530 #if wxMAC_USE_NATIVE_TOOLBAR
531 m_macHIToolbarRef
= NULL
;
532 m_macUsesNativeToolbar
= false ;
536 // also for the toolbar we have the dual implementation:
537 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
539 bool wxToolBar::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
540 long style
, const wxString
& name
)
542 if ( !wxToolBarBase::Create( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
547 #if wxMAC_USE_NATIVE_TOOLBAR
548 wxString labelStr
= wxString::Format(wxT("%xd"), (int)this);
549 err
= HIToolbarCreate( wxMacCFStringHolder(labelStr
, wxFont::GetDefaultEncoding() ) , 0 ,
550 (HIToolbarRef
*) &m_macHIToolbarRef
);
552 if (m_macHIToolbarRef
!= NULL
)
554 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
555 HIToolbarDisplaySize displaySize
= kHIToolbarDisplaySizeSmall
;
557 if ( style
& wxTB_NOICONS
)
558 mode
= kHIToolbarDisplayModeLabelOnly
;
559 else if ( style
& wxTB_TEXT
)
560 mode
= kHIToolbarDisplayModeIconAndLabel
;
562 mode
= kHIToolbarDisplayModeIconOnly
;
564 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macHIToolbarRef
, mode
) ;
565 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, displaySize
) ;
572 wxToolBar::~wxToolBar()
574 #if wxMAC_USE_NATIVE_TOOLBAR
575 if ( m_macHIToolbarRef
)
577 // if this is the installed toolbar, then deinstall it
578 if (m_macUsesNativeToolbar
)
579 MacInstallNativeToolbar( false );
581 CFRelease( (HIToolbarRef
) m_macHIToolbarRef
);
582 m_macHIToolbarRef
= NULL
;
587 bool wxToolBar::Show( bool show
)
590 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
592 bResult
= (tlw
!= NULL
);
595 #if wxMAC_USE_NATIVE_TOOLBAR
596 bool ownToolbarInstalled
= false;
597 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
598 if (ownToolbarInstalled
)
600 bResult
= ( IsWindowToolbarVisible(tlw
) != show
);
602 ShowHideWindowToolbar( tlw
, show
, false );
606 bResult
= wxToolBarBase::Show( show
);
612 bool wxToolBar::IsShown() const
616 #if wxMAC_USE_NATIVE_TOOLBAR
617 bool ownToolbarInstalled
;
618 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
619 if (ownToolbarInstalled
)
621 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
622 bResult
= IsWindowToolbarVisible(tlw
) ;
626 bResult
= wxToolBarBase::IsShown();
631 void wxToolBar::DoGetSize( int *width
, int *height
) const
633 #if wxMAC_USE_NATIVE_TOOLBAR
635 bool ownToolbarInstalled
;
637 MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
638 if ( ownToolbarInstalled
)
640 // TODO is this really a control ?
641 GetControlBounds( (ControlRef
) m_macHIToolbarRef
, &boundsR
);
643 *width
= boundsR
.right
- boundsR
.left
;
644 if ( height
!= NULL
)
645 *height
= boundsR
.bottom
- boundsR
.top
;
649 wxToolBarBase::DoGetSize( width
, height
);
652 wxSize
wxToolBar::DoGetBestSize() const
655 DoGetSize( &width
, &height
) ;
656 return wxSize( width
, height
) ;
659 void wxToolBar::SetWindowStyleFlag( long style
)
661 wxToolBarBase::SetWindowStyleFlag( style
);
662 #if wxMAC_USE_NATIVE_TOOLBAR
663 if (m_macHIToolbarRef
!= NULL
)
665 HIToolbarDisplayMode mode
= kHIToolbarDisplayModeDefault
;
667 if ( style
& wxTB_NOICONS
)
668 mode
= kHIToolbarDisplayModeLabelOnly
;
669 else if ( style
& wxTB_TEXT
)
670 mode
= kHIToolbarDisplayModeIconAndLabel
;
672 mode
= kHIToolbarDisplayModeIconOnly
;
674 HIToolbarSetDisplayMode( (HIToolbarRef
) m_macHIToolbarRef
, mode
);
679 #if wxMAC_USE_NATIVE_TOOLBAR
680 bool wxToolBar::MacWantsNativeToolbar()
682 return m_macUsesNativeToolbar
;
685 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled
) const
687 bool bResultV
= false;
689 if (ownToolbarInstalled
!= NULL
)
690 *ownToolbarInstalled
= false;
692 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
695 HIToolbarRef curToolbarRef
= NULL
;
696 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
697 bResultV
= ((err
== 0) && (curToolbarRef
!= NULL
));
698 if (bResultV
&& (ownToolbarInstalled
!= NULL
))
699 *ownToolbarInstalled
= (curToolbarRef
== m_macHIToolbarRef
);
705 bool wxToolBar::MacInstallNativeToolbar(bool usesNative
)
707 bool bResult
= false;
709 WindowRef tlw
= MAC_WXHWND(MacGetTopLevelWindowRef());
713 if (usesNative
&& (m_macHIToolbarRef
== NULL
))
716 if (usesNative
&& ((GetWindowStyleFlag() & wxTB_VERTICAL
) != 0))
719 // check the existing toolbar
720 HIToolbarRef curToolbarRef
= NULL
;
721 OSStatus err
= GetWindowToolbar( tlw
, &curToolbarRef
);
723 curToolbarRef
= NULL
;
725 m_macUsesNativeToolbar
= usesNative
;
727 if (m_macUsesNativeToolbar
)
729 // only install toolbar if there isn't one installed already
730 if (curToolbarRef
== NULL
)
734 SetWindowToolbar( tlw
, (HIToolbarRef
) m_macHIToolbarRef
);
735 ShowHideWindowToolbar( tlw
, true, false );
736 ChangeWindowAttributes( tlw
, kWindowToolbarButtonAttribute
, 0 );
737 SetAutomaticControlDragTrackingEnabledForWindow( tlw
, true );
739 // FIXME: which is best, which is necessary?
741 // m_peer->SetVisibility( false, true );
744 Rect r
= { 0 , 0 , 0 , 0 };
747 m_peer
->SetRect( &r
);
749 // FIXME: which is best, which is necessary?
751 SetSize( wxSIZE_AUTO_WIDTH
, 0 );
753 m_peer
->SetVisibility( false, true );
754 wxToolBarBase::Show( false );
759 // only deinstall toolbar if this is the installed one
760 if (m_macHIToolbarRef
== curToolbarRef
)
764 ShowHideWindowToolbar( tlw
, false, false );
765 ChangeWindowAttributes( tlw
, 0 , kWindowToolbarButtonAttribute
);
766 SetWindowToolbar( tlw
, NULL
);
768 // FIXME: which is best, which is necessary?
769 m_peer
->SetVisibility( true, true );
772 // wxToolBarBase::Show( true );
778 InvalidateBestSize();
780 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
785 bool wxToolBar::Realize()
787 if (m_tools
.GetCount() == 0)
790 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
791 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
799 int maxToolWidth
= 0;
800 int maxToolHeight
= 0;
802 // find the maximum tool width and height
803 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
804 while ( node
!= NULL
)
806 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
810 wxSize sz
= tool
->GetSize();
812 if ( sz
.x
> maxToolWidth
)
814 if ( sz
.y
> maxToolHeight
)
815 maxToolHeight
= sz
.y
;
818 node
= node
->GetNext();
821 bool lastIsRadio
= false;
822 bool curIsRadio
= false;
823 bool setChoiceInGroup
= false;
825 node
= m_tools
.GetFirst();
827 #if wxMAC_USE_NATIVE_TOOLBAR
828 CFIndex currentPosition
= 0 ;
829 bool insertAll
= false ;
830 #endif // wxMAC_USE_NATIVE_TOOLBAR
832 while ( node
!= NULL
)
834 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
838 node
= node
->GetNext();
843 // for the moment just perform a single row/column alignment
844 wxSize cursize
= tool
->GetSize();
845 if ( x
+ cursize
.x
> maxWidth
)
846 maxWidth
= x
+ cursize
.x
;
847 if ( y
+ cursize
.y
> maxHeight
)
848 maxHeight
= y
+ cursize
.y
;
850 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
852 int x1
= x
+ ( maxToolWidth
- cursize
.x
) / 2;
853 tool
->SetPosition( wxPoint(x1
, y
) );
857 int y1
= y
+ ( maxToolHeight
- cursize
.y
) / 2;
858 tool
->SetPosition( wxPoint(x
, y1
) );
861 // update the item positioning state
862 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
863 y
+= cursize
.y
+ kwxMacToolSpacing
;
865 x
+= cursize
.x
+ kwxMacToolSpacing
;
867 #if wxMAC_USE_NATIVE_TOOLBAR
868 // install in native HIToolbar
869 if ( m_macHIToolbarRef
!= NULL
)
871 HIToolbarItemRef hiItemRef
= tool
->GetToolbarItemRef();
872 if ( hiItemRef
!= NULL
)
874 if ( tool
->GetIndex() != currentPosition
|| insertAll
== true )
876 OSStatus err
= noErr
;
879 // if this is the first tool that gets newly inserted or repositioned
880 // first remove all 'old' tools from here to the right, because of this
881 // all following tools will have to be reinserted (insertAll). i = 100 because there's
882 // no way to determine how many there are in a toolbar, so just a high number :-(
883 for ( CFIndex i
= 100 ; i
>= currentPosition
; --i
)
885 err
= HIToolbarRemoveItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, i
) ;
887 wxASSERT_MSG( err
== noErr
, _T("HIToolbarRemoveItemAtIndex failed") );
890 err
= HIToolbarInsertItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, hiItemRef
, currentPosition
) ;
891 wxASSERT_MSG( err
== noErr
, _T("HIToolbarInsertItemAtIndex failed") );
892 tool
->SetIndex( currentPosition
) ;
897 #endif // wxMAC_USE_NATIVE_TOOLBAR
899 // update radio button (and group) state
900 lastIsRadio
= curIsRadio
;
901 curIsRadio
= ( tool
->IsButton() && (tool
->GetKind() == wxITEM_RADIO
) );
905 if ( tool
->IsToggled() )
906 DoToggleTool( tool
, true );
908 setChoiceInGroup
= false;
914 if ( tool
->Toggle(true) )
916 DoToggleTool( tool
, true );
917 setChoiceInGroup
= true;
920 else if ( tool
->IsToggled() )
922 if ( tool
->IsToggled() )
923 DoToggleTool( tool
, true );
925 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
926 while ( nodePrev
!= NULL
)
928 wxToolBarToolBase
*toggleTool
= nodePrev
->GetData();
929 if ( (toggleTool
== NULL
) || !toggleTool
->IsButton() || (toggleTool
->GetKind() != wxITEM_RADIO
) )
932 if ( toggleTool
->Toggle(false) )
933 DoToggleTool( toggleTool
, false );
935 nodePrev
= nodePrev
->GetPrevious();
940 node
= node
->GetNext();
943 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
945 // if not set yet, only one row
946 if ( m_maxRows
<= 0 )
949 m_minWidth
= maxWidth
;
951 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
952 m_minHeight
= m_maxHeight
= maxHeight
;
956 // if not set yet, have one column
957 if ( (GetToolsCount() > 0) && (m_maxRows
<= 0) )
958 SetRows( GetToolsCount() );
960 m_minHeight
= maxHeight
;
962 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
963 m_minWidth
= m_maxWidth
= maxWidth
;
967 // FIXME: should this be OSX-only?
969 bool wantNativeToolbar
, ownToolbarInstalled
;
971 // attempt to install the native toolbar
972 wantNativeToolbar
= ((GetWindowStyleFlag() & wxTB_VERTICAL
) == 0);
973 MacInstallNativeToolbar( wantNativeToolbar
);
974 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled
);
975 if (!ownToolbarInstalled
)
977 SetSize( maxWidth
, maxHeight
);
978 InvalidateBestSize();
982 SetSize( maxWidth
, maxHeight
);
983 InvalidateBestSize();
986 SetBestFittingSize();
991 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
993 m_defaultWidth
= size
.x
+ kwxMacToolBorder
;
994 m_defaultHeight
= size
.y
+ kwxMacToolBorder
;
996 #if wxMAC_USE_NATIVE_TOOLBAR
997 if (m_macHIToolbarRef
!= NULL
)
999 int maxs
= wxMax( size
.x
, size
.y
);
1000 HIToolbarDisplaySize sizeSpec
;
1002 sizeSpec
= kHIToolbarDisplaySizeNormal
;
1003 else if ( maxs
> 24 )
1004 sizeSpec
= kHIToolbarDisplaySizeDefault
;
1006 sizeSpec
= kHIToolbarDisplaySizeSmall
;
1008 HIToolbarSetDisplaySize( (HIToolbarRef
) m_macHIToolbarRef
, sizeSpec
);
1013 // The button size is bigger than the bitmap size
1014 wxSize
wxToolBar::GetToolSize() const
1016 return wxSize(m_defaultWidth
+ kwxMacToolBorder
, m_defaultHeight
+ kwxMacToolBorder
);
1019 void wxToolBar::SetRows(int nRows
)
1021 // avoid resizing the frame uselessly
1022 if ( nRows
!= m_maxRows
)
1028 void wxToolBar::MacSuperChangedPosition()
1030 wxWindow::MacSuperChangedPosition();
1031 #if wxMAC_USE_NATIVE_TOOLBAR
1032 if (! m_macUsesNativeToolbar
)
1039 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1041 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
1042 while ( node
!= NULL
)
1044 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData() ;
1048 wxRect2DInt
r( tool
->GetPosition(), tool
->GetSize() );
1049 if ( r
.Contains( wxPoint( x
, y
) ) )
1053 node
= node
->GetNext();
1056 return (wxToolBarToolBase
*)NULL
;
1059 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
1061 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
1063 return tool
->GetShortHelp() ;
1065 return wxEmptyString
;
1068 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
1071 ((wxToolBarTool
*)t
)->DoEnable( enable
) ;
1074 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
1076 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
1077 if ( ( tool
!= NULL
) && tool
->IsButton() )
1078 tool
->UpdateToggleImage( toggle
);
1081 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
1082 wxToolBarToolBase
*toolBase
)
1084 wxToolBarTool
* tool
= wx_static_cast( wxToolBarTool
* , toolBase
);
1088 WindowRef window
= (WindowRef
) MacGetTopLevelWindowRef();
1089 wxSize toolSize
= GetToolSize();
1090 Rect toolrect
= { 0, 0 , toolSize
.y
, toolSize
.x
};
1091 ControlRef controlHandle
= NULL
;
1094 switch (tool
->GetStyle())
1096 case wxTOOL_STYLE_SEPARATOR
:
1098 wxASSERT( tool
->GetControlHandle() == NULL
);
1101 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1102 toolrect
.bottom
= toolSize
.y
;
1104 toolrect
.right
= toolSize
.x
;
1106 #ifdef __WXMAC_OSX__
1107 // in flat style we need a visual separator
1108 #if wxMAC_USE_NATIVE_TOOLBAR
1109 HIToolbarItemRef item
;
1110 err
= HIToolbarItemCreate( kHIToolbarSeparatorIdentifier
, kHIToolbarItemCantBeRemoved
| kHIToolbarItemIsSeparator
| kHIToolbarItemAllowDuplicates
, &item
);
1112 tool
->SetToolbarItemRef( item
);
1113 #endif // wxMAC_USE_NATIVE_TOOLBAR
1114 CreateSeparatorControl( window
, &toolrect
, &controlHandle
);
1115 tool
->SetControlHandle( controlHandle
);
1116 #endif // __WXMAC_OSX__
1120 case wxTOOL_STYLE_BUTTON
:
1122 wxASSERT( tool
->GetControlHandle() == NULL
) ;
1123 ControlButtonContentInfo info
;
1124 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap() , kControlContentIconRef
) ;
1126 if ( UMAGetSystemVersion() >= 0x1000)
1127 CreateIconControl( window
, &toolrect
, &info
, false , &controlHandle
) ;
1130 SInt16 behaviour
= kControlBehaviorOffsetContents
;
1131 if ( tool
->CanBeToggled() )
1132 behaviour
+= kControlBehaviorToggles
;
1133 CreateBevelButtonControl( window
, &toolrect
, CFSTR("") ,
1134 kControlBevelButtonNormalBevel
,
1136 0 , 0 , 0 , &controlHandle
) ;
1139 #if wxMAC_USE_NATIVE_TOOLBAR
1140 HIToolbarItemRef item
;
1141 wxString labelStr
= wxString::Format(wxT("%xd"), (int)tool
);
1142 err
= HIToolbarItemCreate(
1143 wxMacCFStringHolder(labelStr
, wxFont::GetDefaultEncoding()),
1144 kHIToolbarItemCantBeRemoved
| kHIToolbarItemAnchoredLeft
| kHIToolbarItemAllowDuplicates
, &item
);
1147 InstallEventHandler( HIObjectGetEventTarget(item
), GetwxMacToolBarEventHandlerUPP(),
1148 GetEventTypeCount(toolBarEventList
), toolBarEventList
, tool
, NULL
);
1149 HIToolbarItemSetLabel( item
, wxMacCFStringHolder(tool
->GetLabel(), m_font
.GetEncoding()) );
1150 HIToolbarItemSetIconRef( item
, info
.u
.iconRef
);
1151 HIToolbarItemSetCommandID( item
, kHIToolbarCommandPressAction
);
1152 tool
->SetToolbarItemRef( item
);
1154 #endif // wxMAC_USE_NATIVE_TOOLBAR
1156 wxMacReleaseBitmapButton( &info
) ;
1158 SetBevelButtonTextPlacement( m_controlHandle , kControlBevelButtonPlaceBelowGraphic ) ;
1159 UMASetControlTitle( m_controlHandle , label , wxFont::GetDefaultEncoding() ) ;
1162 InstallControlEventHandler( (ControlRef
) controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
1163 GetEventTypeCount(eventList
), eventList
, tool
, NULL
);
1165 tool
->SetControlHandle( controlHandle
);
1169 case wxTOOL_STYLE_CONTROL
:
1170 wxASSERT( tool
->GetControl() != NULL
);
1171 #if 0 // wxMAC_USE_NATIVE_TOOLBAR
1172 // FIXME: doesn't work yet...
1174 HIToolbarItemRef item
;
1175 wxString labelStr
= wxString::Format( wxT("%xd"), (int) tool
);
1176 result
= HIToolbarItemCreate( wxMacCFStringHolder(labelStr
, wxFont::GetDefaultEncoding()),
1177 kHIToolbarItemCantBeRemoved
| kHIToolbarItemAnchoredLeft
| kHIToolbarItemAllowDuplicates
,
1181 HIToolbarItemSetLabel( item
, wxMacCFStringHolder(tool
->GetLabel(), m_font
.GetEncoding()) );
1182 HIToolbarItemSetCommandID( item
, tool
->GetId() );
1183 tool
->SetToolbarItemRef( item
);
1185 controlHandle
= ( ControlRef
) tool
->GetControlHandle();
1186 wxASSERT_MSG( controlHandle
!= NULL
, wxT("NULL tool control") );
1188 // FIXME: is this necessary ??
1189 ::GetControlBounds( controlHandle
, &toolrect
);
1190 UMAMoveControl( controlHandle
, -toolrect
.left
, -toolrect
.top
);
1192 // FIXME: is this necessary ??
1193 InstallControlEventHandler( controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
1194 GetEventTypeCount(eventList
), eventList
, tool
, NULL
);
1199 // FIXME: right now there's nothing to do here
1209 if ( controlHandle
)
1211 ControlRef container
= (ControlRef
) GetHandle();
1212 wxASSERT_MSG( container
!= NULL
, wxT("No valid mac container control") );
1214 UMAShowControl( controlHandle
);
1215 ::EmbedControl( controlHandle
, container
);
1218 if ( tool
->CanBeToggled() && tool
->IsToggled() )
1219 tool
->UpdateToggleImage( true );
1221 // nothing special to do here - we relayout in Realize() later
1223 InvalidateBestSize();
1227 wxString errMsg
= wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long) err
);
1228 wxASSERT_MSG( false, errMsg
.c_str() );
1234 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1236 wxFAIL_MSG( _T("not implemented") );
1239 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolbase
)
1241 wxToolBarTool
* tool
= wx_static_cast( wxToolBarTool
* , toolbase
) ;
1242 wxToolBarToolsList::compatibility_iterator node
;
1243 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1245 wxToolBarToolBase
*tool2
= node
->GetData();
1246 if ( tool2
== tool
)
1248 // let node point to the next node in the list
1249 node
= node
->GetNext();
1255 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize() ;
1259 #if wxMAC_USE_NATIVE_TOOLBAR
1260 CFIndex removeIndex
= tool
->GetIndex();
1261 #endif // wxMAC_USE_NATIVE_TOOLBAR
1263 switch ( tool
->GetStyle() )
1265 case wxTOOL_STYLE_CONTROL
:
1267 tool
->GetControl()->Destroy();
1268 tool
->ClearControl() ;
1272 case wxTOOL_STYLE_BUTTON
:
1273 case wxTOOL_STYLE_SEPARATOR
:
1274 if ( tool
->GetControlHandle() )
1276 DisposeControl( (ControlRef
) tool
->GetControlHandle() ) ;
1277 #if wxMAC_USE_NATIVE_TOOLBAR
1278 if ( removeIndex
!= -1 && m_macHIToolbarRef
)
1280 HIToolbarRemoveItemAtIndex( (HIToolbarRef
) m_macHIToolbarRef
, removeIndex
) ;
1281 tool
->SetIndex( -1 ) ;
1283 #endif // wxMAC_USE_NATIVE_TOOLBAR
1284 tool
->ClearControl() ;
1292 // and finally reposition all the controls after this one
1294 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
1296 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
1297 wxPoint pt
= tool2
->GetPosition() ;
1299 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
1304 tool2
->SetPosition( pt
) ;
1306 #if wxMAC_USE_NATIVE_TOOLBAR
1307 if ( removeIndex
!= -1 && tool2
->GetIndex() > removeIndex
)
1308 tool2
->SetIndex( tool2
->GetIndex() - 1 ) ;
1313 InvalidateBestSize();
1317 void wxToolBar::OnPaint(wxPaintEvent
& event
)
1319 #if wxMAC_USE_NATIVE_TOOLBAR
1320 if ( m_macUsesNativeToolbar
)
1327 wxPaintDC
dc(this) ;
1330 GetSize( &w
, &h
) ;
1331 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1332 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
1334 if ( UMAGetSystemVersion() >= 0x1030 )
1336 HIThemePlacardDrawInfo info
;
1337 memset( &info
, 0 , sizeof( info
) ) ;
1339 info
.state
= IsEnabled() ? kThemeStateActive
: kThemeStateInactive
;
1341 CGContextRef cgContext
= (CGContextRef
) MacGetCGContextRef() ;
1342 HIRect rect
= CGRectMake( 0 , 0 , w
, h
) ;
1343 HIThemeDrawPlacard( &rect
, & info
, cgContext
, kHIThemeOrientationNormal
) ;
1348 // leave the background as it is (striped or metal)
1351 wxMacPortSetter
helper(&dc
) ;
1353 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
1354 dc
.YLOG2DEVMAC(h
) , dc
.XLOG2DEVMAC(w
) } ;
1356 if( toolbarrect.left < 0 )
1357 toolbarrect.left = 0 ;
1358 if ( toolbarrect.top < 0 )
1359 toolbarrect.top = 0 ;
1361 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
1363 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
1367 #if TARGET_API_MAC_OSX
1368 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
1369 if ( UMAGetSystemVersion() >= 0x1030 )
1371 HIRect hiToolbarrect
= CGRectMake( dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
1372 dc
.YLOG2DEVREL(h
) , dc
.XLOG2DEVREL(w
) );
1373 CGContextRef cgContext
;
1375 GetPortBounds( (CGrafPtr
) dc
.m_macPort
, &bounds
) ;
1376 QDBeginCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
) ;
1377 CGContextTranslateCTM( cgContext
, 0 , bounds
.bottom
- bounds
.top
) ;
1378 CGContextScaleCTM( cgContext
, 1 , -1 ) ;
1381 HIThemeBackgroundDrawInfo drawInfo
;
1382 drawInfo
.version
= 0 ;
1383 drawInfo
.state
= kThemeStateActive
;
1384 drawInfo
.kind
= kThemeBackgroundMetal
;
1385 HIThemeApplyBackground( &hiToolbarrect
, &drawInfo
, cgContext
,kHIThemeOrientationNormal
) ;
1388 QDEndCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
) ;
1393 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
1402 #endif // wxUSE_TOOLBAR