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/toolbar.h"
22 #include "wx/notebook.h"
23 #include "wx/tabctrl.h"
24 #include "wx/bitmap.h"
26 #if !USE_SHARED_LIBRARY
27 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
29 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
30 EVT_PAINT( wxToolBar::OnPaint
)
34 #include "wx/mac/uma.h"
35 #include "wx/geometry.h"
38 const short kwxMacToolBarToolDefaultWidth
= 16 ;
39 const short kwxMacToolBarToolDefaultHeight
= 16 ;
40 const short kwxMacToolBarTopMargin
= 4 ;
41 const short kwxMacToolBarLeftMargin
= 4 ;
42 const short kwxMacToolBorder
= 0 ;
43 const short kwxMacToolSpacing
= 6 ;
45 const short kwxMacToolBarToolDefaultWidth
= 24 ;
46 const short kwxMacToolBarToolDefaultHeight
= 22 ;
47 const short kwxMacToolBarTopMargin
= 2 ;
48 const short kwxMacToolBarLeftMargin
= 2 ;
49 const short kwxMacToolBorder
= 4 ;
50 const short kwxMacToolSpacing
= 0 ;
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 class wxToolBarTool
: public wxToolBarToolBase
60 wxToolBarTool(wxToolBar
*tbar
,
62 const wxString
& label
,
63 const wxBitmap
& bmpNormal
,
64 const wxBitmap
& bmpDisabled
,
67 const wxString
& shortHelp
,
68 const wxString
& longHelp
) ;
70 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
71 : wxToolBarToolBase(tbar
, control
)
78 if ( m_controlHandle
)
79 DisposeControl( m_controlHandle
) ;
82 WXWidget
GetControlHandle() { return (WXWidget
) m_controlHandle
; }
83 void SetControlHandle( ControlRef handle
) { m_controlHandle
= handle
; }
85 void SetSize(const wxSize
& size
) ;
86 void SetPosition( const wxPoint
& position
) ;
88 void ClearControl() { m_control
= NULL
; }
90 wxSize
GetSize() const
94 return GetControl()->GetSize() ;
96 else if ( IsButton() )
98 return GetToolBar()->GetToolSize() ;
103 wxSize sz
= GetToolBar()->GetToolSize() ;
104 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
111 wxPoint
GetPosition() const
113 return wxPoint(m_x
, m_y
);
115 bool DoEnable( bool enable
) ;
117 void UpdateToggleImage( bool toggle
) ;
121 m_controlHandle
= NULL
;
123 ControlRef m_controlHandle
;
129 static const EventTypeSpec eventList
[] =
131 { kEventClassControl
, kEventControlHit
} ,
133 { kEventClassControl
, kEventControlHitTest
} ,
137 static pascal OSStatus
wxMacToolBarToolControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
139 OSStatus result
= eventNotHandledErr
;
141 wxMacCarbonEvent
cEvent( event
) ;
143 ControlRef controlRef
;
145 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
) ;
147 switch( GetEventKind( event
) )
149 case kEventControlHit
:
151 wxToolBarTool
* tbartool
= (wxToolBarTool
*)data
;
152 if ( tbartool
->CanBeToggled() )
155 ((wxToolBar
*)tbartool
->GetToolBar())->ToggleTool(tbartool
->GetId(), !tbartool
->IsToggled() );
157 ((wxToolBar
*)tbartool
->GetToolBar())->ToggleTool(tbartool
->GetId(), GetControl32BitValue((ControlRef
)tbartool
->GetControlHandle()));
160 ((wxToolBar
*)tbartool
->GetToolBar())->OnLeftClick( tbartool
->GetId() , tbartool
-> IsToggled() ) ;
165 case kEventControlHitTest
:
167 HIPoint pt
= cEvent
.GetParameter
<HIPoint
>(kEventParamMouseLocation
) ;
169 HIViewGetBounds( controlRef
, &rect
) ;
171 ControlPartCode pc
= kControlNoPart
;
172 if ( CGRectContainsPoint( rect
, pt
) )
173 pc
= kControlIconPart
;
174 cEvent
.SetParameter( kEventParamControlPart
, typeControlPartCode
, pc
) ;
185 pascal OSStatus
wxMacToolBarToolEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
187 OSStatus result
= eventNotHandledErr
;
189 switch ( GetEventClass( event
) )
191 case kEventClassControl
:
192 result
= wxMacToolBarToolControlEventHandler( handler
, event
, data
) ;
200 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler
)
202 // ============================================================================
204 // ============================================================================
206 // ----------------------------------------------------------------------------
208 // ----------------------------------------------------------------------------
210 bool wxToolBarTool::DoEnable(bool enable
)
214 GetControl()->Enable( enable
) ;
216 else if ( IsButton() )
218 #if TARGET_API_MAC_OSX
220 EnableControl( m_controlHandle
) ;
222 DisableControl( m_controlHandle
) ;
225 ActivateControl( m_controlHandle
) ;
227 DeactivateControl( m_controlHandle
) ;
232 void wxToolBarTool::SetSize(const wxSize
& size
)
236 GetControl()->SetSize( size
) ;
240 void wxToolBarTool::SetPosition(const wxPoint
& position
)
247 int mac_x
= position
.x
;
248 int mac_y
= position
.y
;
250 if ( ! GetToolBar()->MacGetTopLevelWindow()->MacUsesCompositing() )
252 GetToolBar()->MacWindowToRootWindow( &x
, &y
) ;
260 GetControlBounds( m_controlHandle
, &contrlRect
) ;
261 int former_mac_x
= contrlRect
.left
;
262 int former_mac_y
= contrlRect
.top
;
263 GetToolBar()->GetToolSize() ;
265 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
267 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
) ;
270 else if ( IsControl() )
272 GetControl()->Move( position
) ;
279 GetControlBounds( m_controlHandle
, &contrlRect
) ;
280 int former_mac_x
= contrlRect
.left
;
281 int former_mac_y
= contrlRect
.top
;
283 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
285 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
) ;
291 void wxToolBarTool::UpdateToggleImage( bool toggle
)
296 int w
= m_bmpNormal
.GetWidth() ;
297 int h
= m_bmpNormal
.GetHeight() ;
298 wxBitmap
bmp( w
, h
) ;
300 dc
.SelectObject( bmp
) ;
301 dc
.SetPen( wxNullPen
) ;
302 dc
.SetBackground( *wxWHITE
) ;
303 dc
.DrawRectangle( 0 , 0 , w
, h
) ;
304 dc
.DrawBitmap( m_bmpNormal
, 0 , 0 , true) ;
305 dc
.SelectObject( wxNullBitmap
) ;
306 ControlButtonContentInfo info
;
307 wxMacCreateBitmapButton( &info
, bmp
) ;
308 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof( info
),
310 wxMacReleaseBitmapButton( &info
) ;
314 ControlButtonContentInfo info
;
315 wxMacCreateBitmapButton( &info
, m_bmpNormal
) ;
316 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof( info
),
318 wxMacReleaseBitmapButton( &info
) ;
321 IconTransformType transform
= toggle
? kTransformSelected
: kTransformNone
;
322 SetControlData( m_controlHandle
, 0, kControlIconTransformTag
, sizeof( transform
),
324 HIViewSetNeedsDisplay( m_controlHandle
, true ) ;
327 ::SetControl32BitValue( m_controlHandle
, toggle
) ;
331 wxToolBarTool::wxToolBarTool(wxToolBar
*tbar
,
333 const wxString
& label
,
334 const wxBitmap
& bmpNormal
,
335 const wxBitmap
& bmpDisabled
,
337 wxObject
*clientData
,
338 const wxString
& shortHelp
,
339 const wxString
& longHelp
)
340 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
341 clientData
, shortHelp
, longHelp
)
347 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
348 const wxString
& label
,
349 const wxBitmap
& bmpNormal
,
350 const wxBitmap
& bmpDisabled
,
352 wxObject
*clientData
,
353 const wxString
& shortHelp
,
354 const wxString
& longHelp
)
356 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
357 clientData
, shortHelp
, longHelp
);
360 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
362 return new wxToolBarTool(this, control
);
365 void wxToolBar::Init()
369 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
370 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
373 bool wxToolBar::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
374 long style
, const wxString
& name
)
377 if ( !wxToolBarBase::Create( parent
, id
, pos
, size
, style
) )
383 wxToolBar::~wxToolBar()
385 // we must refresh the frame size when the toolbar is deleted but the frame
386 // is not - otherwise toolbar leaves a hole in the place it used to occupy
389 bool wxToolBar::Realize()
391 if (m_tools
.GetCount() == 0)
394 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
395 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
403 int maxToolWidth
= 0;
404 int maxToolHeight
= 0;
406 // Find the maximum tool width and height
407 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
410 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
411 wxSize sz
= tool
->GetSize() ;
413 if ( sz
.x
> maxToolWidth
)
414 maxToolWidth
= sz
.x
;
415 if (sz
.y
> maxToolHeight
)
416 maxToolHeight
= sz
.y
;
418 node
= node
->GetNext();
421 bool lastWasRadio
= FALSE
;
422 node
= m_tools
.GetFirst();
425 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
426 wxSize cursize
= tool
->GetSize() ;
428 bool isRadio
= FALSE
;
430 if ( tool
->IsButton() && tool
->GetKind() == wxITEM_RADIO
)
434 if (tool
->Toggle(true))
436 DoToggleTool(tool
, true);
439 else if (tool
->IsToggled())
441 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
444 wxToolBarToolBase
*tool
= nodePrev
->GetData();
445 if ( !tool
->IsButton() || (tool
->GetKind() != wxITEM_RADIO
) )
447 if ( tool
->Toggle(false) )
449 DoToggleTool(tool
, false);
451 nodePrev
= nodePrev
->GetPrevious();
460 lastWasRadio
= isRadio
;
462 // for the moment we just do a single row/column alignement
463 if ( x
+ cursize
.x
> maxWidth
)
464 maxWidth
= x
+ cursize
.x
;
465 if ( y
+ cursize
.y
> maxHeight
)
466 maxHeight
= y
+ cursize
.y
;
468 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
470 int x1
= x
+ (maxToolWidth
- cursize
.x
)/2 ;
471 tool
->SetPosition( wxPoint( x1
, y
) ) ;
475 int y1
= y
+ (maxToolHeight
- cursize
.y
)/2 ;
476 tool
->SetPosition( wxPoint( x
, y1
) ) ;
478 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
481 y
+= kwxMacToolSpacing
;
486 x
+= kwxMacToolSpacing
;
489 node
= node
->GetNext();
492 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
494 if ( m_maxRows
== 0 )
496 // if not set yet, only one row
499 m_minWidth
= maxWidth
;
501 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
502 m_minHeight
= m_maxHeight
= maxHeight
;
506 if ( GetToolsCount() > 0 && m_maxRows
== 0 )
508 // if not set yet, have one column
509 SetRows(GetToolsCount());
511 m_minHeight
= maxHeight
;
513 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
514 m_minWidth
= m_maxWidth
= maxWidth
;
517 SetSize( maxWidth
, maxHeight
);
518 InvalidateBestSize();
523 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
525 m_defaultWidth
= size
.x
+kwxMacToolBorder
; m_defaultHeight
= size
.y
+kwxMacToolBorder
;
528 // The button size is bigger than the bitmap size
529 wxSize
wxToolBar::GetToolSize() const
531 return wxSize(m_defaultWidth
+ kwxMacToolBorder
, m_defaultHeight
+ kwxMacToolBorder
);
534 void wxToolBar::SetRows(int nRows
)
536 if ( nRows
== m_maxRows
)
538 // avoid resizing the frame uselessly
545 void wxToolBar::MacSuperChangedPosition()
547 wxWindow::MacSuperChangedPosition() ;
551 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
553 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
556 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData() ;
557 wxRect2DInt
r( tool
->GetPosition() , tool
->GetSize() ) ;
558 if ( r
.Contains( wxPoint( x
, y
) ) )
563 node
= node
->GetNext();
566 return (wxToolBarToolBase
*)NULL
;
569 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
571 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
574 return tool
->GetShortHelp() ;
576 return wxEmptyString
;
579 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
581 ((wxToolBarTool
*)t
)->DoEnable( enable
) ;
584 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
586 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
587 if ( tool
->IsButton() )
589 tool
->UpdateToggleImage( toggle
) ;
593 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
594 wxToolBarToolBase
*toolBase
)
596 wxToolBarTool
* tool
= wx_static_cast( wxToolBarTool
* , toolBase
) ;
598 WindowRef window
= (WindowRef
) MacGetTopLevelWindowRef() ;
599 wxSize toolSize
= GetToolSize() ;
600 Rect toolrect
= { 0, 0 , toolSize
.y
, toolSize
.x
} ;
601 ControlRef controlHandle
= NULL
;
603 switch( tool
->GetStyle() )
605 case wxTOOL_STYLE_SEPARATOR
:
607 wxASSERT( tool
->GetControlHandle() == NULL
) ;
610 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
612 toolrect
.bottom
= toolSize
.y
;
616 toolrect
.right
= toolSize
.x
;
619 // in flat style we need a visual separator
620 CreateSeparatorControl( window
, &toolrect
, &controlHandle
) ;
621 tool
->SetControlHandle( controlHandle
) ;
625 case wxTOOL_STYLE_BUTTON
:
627 wxASSERT( tool
->GetControlHandle() == NULL
) ;
628 ControlButtonContentInfo info
;
629 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap() , kControlContentIconRef
) ;
632 CreateIconControl( window
, &toolrect
, &info
, false , &controlHandle
) ;
634 SInt16 behaviour
= kControlBehaviorOffsetContents
;
635 if ( CanBeToggled() )
636 behaviour
+= kControlBehaviorToggles
;
637 CreateBevelButtonControl( window
, &toolrect
, CFSTR("") , kControlBevelButtonNormalBevel
, behaviour
, &info
,
638 0 , 0 , 0 , &controlHandle
) ;
641 wxMacReleaseBitmapButton( &info
) ;
643 SetBevelButtonTextPlacement( m_controlHandle , kControlBevelButtonPlaceBelowGraphic ) ;
644 UMASetControlTitle( m_controlHandle , label , wxFont::GetDefaultEncoding() ) ;
647 InstallControlEventHandler( (ControlRef
) controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
648 GetEventTypeCount(eventList
), eventList
, tool
,NULL
);
650 tool
->SetControlHandle( controlHandle
) ;
653 case wxTOOL_STYLE_CONTROL
:
654 wxASSERT( tool
->GetControl() != NULL
) ;
655 // right now there's nothing to do here
661 ControlRef container
= (ControlRef
) GetHandle() ;
662 wxASSERT_MSG( container
!= NULL
, wxT("No valid mac container control") ) ;
664 UMAShowControl( controlHandle
) ;
665 ::EmbedControl( controlHandle
, container
) ;
668 if ( tool
->CanBeToggled() && tool
->IsToggled() )
670 tool
->UpdateToggleImage( true ) ;
673 // nothing special to do here - we relayout in Realize() later
675 InvalidateBestSize();
680 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
682 wxFAIL_MSG( _T("not implemented") );
685 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolbase
)
687 wxToolBarTool
* tool
= wx_static_cast( wxToolBarTool
* , toolbase
) ;
688 wxToolBarToolsList::compatibility_iterator node
;
689 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
691 wxToolBarToolBase
*tool2
= node
->GetData();
694 // let node point to the next node in the list
695 node
= node
->GetNext();
701 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize() ;
705 switch ( tool
->GetStyle() )
707 case wxTOOL_STYLE_CONTROL
:
709 tool
->GetControl()->Destroy();
710 tool
->ClearControl() ;
714 case wxTOOL_STYLE_BUTTON
:
715 case wxTOOL_STYLE_SEPARATOR
:
716 if ( tool
->GetControlHandle() )
718 DisposeControl( (ControlRef
) tool
->GetControlHandle() ) ;
719 tool
->SetControlHandle( (ControlRef
) NULL
) ;
724 // and finally reposition all the controls after this one
726 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
728 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
729 wxPoint pt
= tool2
->GetPosition() ;
731 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
739 tool2
->SetPosition( pt
) ;
742 InvalidateBestSize();
746 void wxToolBar::OnPaint(wxPaintEvent
& event
)
752 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
753 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
755 if ( UMAGetSystemVersion() >= 0x1030 )
757 HIThemePlacardDrawInfo info
;
758 memset( &info
, 0 , sizeof( info
) ) ;
760 info
.state
= IsEnabled() ? kThemeStateActive
: kThemeStateInactive
;
762 CGContextRef cgContext
= (CGContextRef
) MacGetCGContextRef() ;
763 HIRect rect
= CGRectMake( 0 , 0 , w
, h
) ;
764 HIThemeDrawPlacard( &rect
, & info
, cgContext
, kHIThemeOrientationNormal
) ;
769 // leave the background as it is (striped or metal)
772 wxMacPortSetter
helper(&dc
) ;
774 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
775 dc
.YLOG2DEVMAC(h
) , dc
.XLOG2DEVMAC(w
) } ;
777 if( toolbarrect.left < 0 )
778 toolbarrect.left = 0 ;
779 if ( toolbarrect.top < 0 )
780 toolbarrect.top = 0 ;
782 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
784 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
788 #if TARGET_API_MAC_OSX
789 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
790 if ( UMAGetSystemVersion() >= 0x1030 )
792 HIRect hiToolbarrect
= CGRectMake( dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
793 dc
.YLOG2DEVREL(h
) , dc
.XLOG2DEVREL(w
) );
794 CGContextRef cgContext
;
796 GetPortBounds( (CGrafPtr
) dc
.m_macPort
, &bounds
) ;
797 QDBeginCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
) ;
798 CGContextTranslateCTM( cgContext
, 0 , bounds
.bottom
- bounds
.top
) ;
799 CGContextScaleCTM( cgContext
, 1 , -1 ) ;
802 HIThemeBackgroundDrawInfo drawInfo
;
803 drawInfo
.version
= 0 ;
804 drawInfo
.state
= kThemeStateActive
;
805 drawInfo
.kind
= kThemeBackgroundMetal
;
806 HIThemeApplyBackground( &hiToolbarrect
, &drawInfo
, cgContext
,kHIThemeOrientationNormal
) ;
808 QDEndCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
) ;
813 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
822 #endif // wxUSE_TOOLBAR