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 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
28 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
29 EVT_PAINT( wxToolBar::OnPaint
)
32 #include "wx/mac/uma.h"
33 #include "wx/geometry.h"
36 const short kwxMacToolBarToolDefaultWidth
= 16 ;
37 const short kwxMacToolBarToolDefaultHeight
= 16 ;
38 const short kwxMacToolBarTopMargin
= 4 ;
39 const short kwxMacToolBarLeftMargin
= 4 ;
40 const short kwxMacToolBorder
= 0 ;
41 const short kwxMacToolSpacing
= 6 ;
43 const short kwxMacToolBarToolDefaultWidth
= 24 ;
44 const short kwxMacToolBarToolDefaultHeight
= 22 ;
45 const short kwxMacToolBarTopMargin
= 2 ;
46 const short kwxMacToolBarLeftMargin
= 2 ;
47 const short kwxMacToolBorder
= 4 ;
48 const short kwxMacToolSpacing
= 0 ;
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
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
)
76 if ( m_controlHandle
)
77 DisposeControl( m_controlHandle
) ;
80 WXWidget
GetControlHandle() { return (WXWidget
) m_controlHandle
; }
81 void SetControlHandle( ControlRef handle
) { m_controlHandle
= handle
; }
83 void SetSize(const wxSize
& size
) ;
84 void SetPosition( const wxPoint
& position
) ;
86 void ClearControl() { m_control
= NULL
; }
88 wxSize
GetSize() const
92 return GetControl()->GetSize() ;
94 else if ( IsButton() )
96 return GetToolBar()->GetToolSize() ;
101 wxSize sz
= GetToolBar()->GetToolSize() ;
102 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
109 wxPoint
GetPosition() const
111 return wxPoint(m_x
, m_y
);
113 bool DoEnable( bool enable
) ;
115 void UpdateToggleImage( bool toggle
) ;
119 m_controlHandle
= NULL
;
121 ControlRef m_controlHandle
;
127 static const EventTypeSpec eventList
[] =
129 { kEventClassControl
, kEventControlHit
} ,
131 { kEventClassControl
, kEventControlHitTest
} ,
135 static pascal OSStatus
wxMacToolBarToolControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
137 OSStatus result
= eventNotHandledErr
;
139 wxMacCarbonEvent
cEvent( event
) ;
141 ControlRef controlRef
;
143 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
) ;
145 switch( GetEventKind( event
) )
147 case kEventControlHit
:
149 wxToolBarTool
* tbartool
= (wxToolBarTool
*)data
;
150 if ( tbartool
->CanBeToggled() )
153 ((wxToolBar
*)tbartool
->GetToolBar())->ToggleTool(tbartool
->GetId(), !tbartool
->IsToggled() );
155 ((wxToolBar
*)tbartool
->GetToolBar())->ToggleTool(tbartool
->GetId(), GetControl32BitValue((ControlRef
)tbartool
->GetControlHandle()));
158 ((wxToolBar
*)tbartool
->GetToolBar())->OnLeftClick( tbartool
->GetId() , tbartool
-> IsToggled() ) ;
163 case kEventControlHitTest
:
165 HIPoint pt
= cEvent
.GetParameter
<HIPoint
>(kEventParamMouseLocation
) ;
167 HIViewGetBounds( controlRef
, &rect
) ;
169 ControlPartCode pc
= kControlNoPart
;
170 if ( CGRectContainsPoint( rect
, pt
) )
171 pc
= kControlIconPart
;
172 cEvent
.SetParameter( kEventParamControlPart
, typeControlPartCode
, pc
) ;
183 static pascal OSStatus
wxMacToolBarToolEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
185 OSStatus result
= eventNotHandledErr
;
187 switch ( GetEventClass( event
) )
189 case kEventClassControl
:
190 result
= wxMacToolBarToolControlEventHandler( handler
, event
, data
) ;
198 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler
)
200 // ============================================================================
202 // ============================================================================
204 // ----------------------------------------------------------------------------
206 // ----------------------------------------------------------------------------
208 bool wxToolBarTool::DoEnable(bool enable
)
212 GetControl()->Enable( enable
) ;
214 else if ( IsButton() )
216 #if TARGET_API_MAC_OSX
218 EnableControl( m_controlHandle
) ;
220 DisableControl( m_controlHandle
) ;
223 ActivateControl( m_controlHandle
) ;
225 DeactivateControl( m_controlHandle
) ;
230 void wxToolBarTool::SetSize(const wxSize
& size
)
234 GetControl()->SetSize( size
) ;
238 void wxToolBarTool::SetPosition(const wxPoint
& position
)
245 int mac_x
= position
.x
;
246 int mac_y
= position
.y
;
248 if ( ! GetToolBar()->MacGetTopLevelWindow()->MacUsesCompositing() )
250 GetToolBar()->MacWindowToRootWindow( &x
, &y
) ;
258 GetControlBounds( m_controlHandle
, &contrlRect
) ;
259 int former_mac_x
= contrlRect
.left
;
260 int former_mac_y
= contrlRect
.top
;
261 GetToolBar()->GetToolSize() ;
263 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
265 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
) ;
268 else if ( IsControl() )
270 GetControl()->Move( position
) ;
277 GetControlBounds( m_controlHandle
, &contrlRect
) ;
278 int former_mac_x
= contrlRect
.left
;
279 int former_mac_y
= contrlRect
.top
;
281 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
283 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
) ;
289 void wxToolBarTool::UpdateToggleImage( bool toggle
)
294 int w
= m_bmpNormal
.GetWidth() ;
295 int h
= m_bmpNormal
.GetHeight() ;
296 wxBitmap
bmp( w
, h
) ;
298 dc
.SelectObject( bmp
) ;
299 dc
.SetPen( wxNullPen
) ;
300 dc
.SetBackground( *wxWHITE
) ;
301 dc
.DrawRectangle( 0 , 0 , w
, h
) ;
302 dc
.DrawBitmap( m_bmpNormal
, 0 , 0 , true) ;
303 dc
.SelectObject( wxNullBitmap
) ;
304 ControlButtonContentInfo info
;
305 wxMacCreateBitmapButton( &info
, bmp
) ;
306 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof( info
),
308 wxMacReleaseBitmapButton( &info
) ;
312 ControlButtonContentInfo info
;
313 wxMacCreateBitmapButton( &info
, m_bmpNormal
) ;
314 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof( info
),
316 wxMacReleaseBitmapButton( &info
) ;
319 IconTransformType transform
= toggle
? kTransformSelected
: kTransformNone
;
320 SetControlData( m_controlHandle
, 0, kControlIconTransformTag
, sizeof( transform
),
322 HIViewSetNeedsDisplay( m_controlHandle
, true ) ;
325 ::SetControl32BitValue( m_controlHandle
, toggle
) ;
329 wxToolBarTool::wxToolBarTool(wxToolBar
*tbar
,
331 const wxString
& label
,
332 const wxBitmap
& bmpNormal
,
333 const wxBitmap
& bmpDisabled
,
335 wxObject
*clientData
,
336 const wxString
& shortHelp
,
337 const wxString
& longHelp
)
338 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
339 clientData
, shortHelp
, longHelp
)
345 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
346 const wxString
& label
,
347 const wxBitmap
& bmpNormal
,
348 const wxBitmap
& bmpDisabled
,
350 wxObject
*clientData
,
351 const wxString
& shortHelp
,
352 const wxString
& longHelp
)
354 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
355 clientData
, shortHelp
, longHelp
);
358 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
360 return new wxToolBarTool(this, control
);
363 void wxToolBar::Init()
367 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
368 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
371 bool wxToolBar::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
372 long style
, const wxString
& name
)
375 if ( !wxToolBarBase::Create( parent
, id
, pos
, size
, style
) )
381 wxToolBar::~wxToolBar()
383 // we must refresh the frame size when the toolbar is deleted but the frame
384 // is not - otherwise toolbar leaves a hole in the place it used to occupy
387 bool wxToolBar::Realize()
389 if (m_tools
.GetCount() == 0)
392 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
393 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
401 int maxToolWidth
= 0;
402 int maxToolHeight
= 0;
404 // Find the maximum tool width and height
405 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
408 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
409 wxSize sz
= tool
->GetSize() ;
411 if ( sz
.x
> maxToolWidth
)
412 maxToolWidth
= sz
.x
;
413 if (sz
.y
> maxToolHeight
)
414 maxToolHeight
= sz
.y
;
416 node
= node
->GetNext();
419 bool lastWasRadio
= FALSE
;
420 node
= m_tools
.GetFirst();
423 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
424 wxSize cursize
= tool
->GetSize() ;
426 bool isRadio
= FALSE
;
428 if ( tool
->IsButton() && tool
->GetKind() == wxITEM_RADIO
)
432 if (tool
->Toggle(true))
434 DoToggleTool(tool
, true);
437 else if (tool
->IsToggled())
439 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
442 wxToolBarToolBase
*tool
= nodePrev
->GetData();
443 if ( !tool
->IsButton() || (tool
->GetKind() != wxITEM_RADIO
) )
445 if ( tool
->Toggle(false) )
447 DoToggleTool(tool
, false);
449 nodePrev
= nodePrev
->GetPrevious();
458 lastWasRadio
= isRadio
;
460 // for the moment we just do a single row/column alignement
461 if ( x
+ cursize
.x
> maxWidth
)
462 maxWidth
= x
+ cursize
.x
;
463 if ( y
+ cursize
.y
> maxHeight
)
464 maxHeight
= y
+ cursize
.y
;
466 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
468 int x1
= x
+ (maxToolWidth
- cursize
.x
)/2 ;
469 tool
->SetPosition( wxPoint( x1
, y
) ) ;
473 int y1
= y
+ (maxToolHeight
- cursize
.y
)/2 ;
474 tool
->SetPosition( wxPoint( x
, y1
) ) ;
476 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
479 y
+= kwxMacToolSpacing
;
484 x
+= kwxMacToolSpacing
;
487 node
= node
->GetNext();
490 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
492 if ( m_maxRows
== 0 )
494 // if not set yet, only one row
497 m_minWidth
= maxWidth
;
499 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
500 m_minHeight
= m_maxHeight
= maxHeight
;
504 if ( GetToolsCount() > 0 && m_maxRows
== 0 )
506 // if not set yet, have one column
507 SetRows(GetToolsCount());
509 m_minHeight
= maxHeight
;
511 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
512 m_minWidth
= m_maxWidth
= maxWidth
;
515 SetSize( maxWidth
, maxHeight
);
516 InvalidateBestSize();
521 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
523 m_defaultWidth
= size
.x
+kwxMacToolBorder
; m_defaultHeight
= size
.y
+kwxMacToolBorder
;
526 // The button size is bigger than the bitmap size
527 wxSize
wxToolBar::GetToolSize() const
529 return wxSize(m_defaultWidth
+ kwxMacToolBorder
, m_defaultHeight
+ kwxMacToolBorder
);
532 void wxToolBar::SetRows(int nRows
)
534 if ( nRows
== m_maxRows
)
536 // avoid resizing the frame uselessly
543 void wxToolBar::MacSuperChangedPosition()
545 wxWindow::MacSuperChangedPosition() ;
549 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
551 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
554 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData() ;
555 wxRect2DInt
r( tool
->GetPosition() , tool
->GetSize() ) ;
556 if ( r
.Contains( wxPoint( x
, y
) ) )
561 node
= node
->GetNext();
564 return (wxToolBarToolBase
*)NULL
;
567 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
569 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
572 return tool
->GetShortHelp() ;
574 return wxEmptyString
;
577 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
579 ((wxToolBarTool
*)t
)->DoEnable( enable
) ;
582 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
584 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
585 if ( tool
->IsButton() )
587 tool
->UpdateToggleImage( toggle
) ;
591 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
592 wxToolBarToolBase
*toolBase
)
594 wxToolBarTool
* tool
= wx_static_cast( wxToolBarTool
* , toolBase
) ;
596 WindowRef window
= (WindowRef
) MacGetTopLevelWindowRef() ;
597 wxSize toolSize
= GetToolSize() ;
598 Rect toolrect
= { 0, 0 , toolSize
.y
, toolSize
.x
} ;
599 ControlRef controlHandle
= NULL
;
601 switch( tool
->GetStyle() )
603 case wxTOOL_STYLE_SEPARATOR
:
605 wxASSERT( tool
->GetControlHandle() == NULL
) ;
608 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
610 toolrect
.bottom
= toolSize
.y
;
614 toolrect
.right
= toolSize
.x
;
617 // in flat style we need a visual separator
618 CreateSeparatorControl( window
, &toolrect
, &controlHandle
) ;
619 tool
->SetControlHandle( controlHandle
) ;
623 case wxTOOL_STYLE_BUTTON
:
625 wxASSERT( tool
->GetControlHandle() == NULL
) ;
626 ControlButtonContentInfo info
;
627 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap() , kControlContentIconRef
) ;
630 CreateIconControl( window
, &toolrect
, &info
, false , &controlHandle
) ;
632 SInt16 behaviour
= kControlBehaviorOffsetContents
;
633 if ( tool
->CanBeToggled() )
634 behaviour
+= kControlBehaviorToggles
;
635 CreateBevelButtonControl( window
, &toolrect
, CFSTR("") , kControlBevelButtonNormalBevel
, behaviour
, &info
,
636 0 , 0 , 0 , &controlHandle
) ;
639 wxMacReleaseBitmapButton( &info
) ;
641 SetBevelButtonTextPlacement( m_controlHandle , kControlBevelButtonPlaceBelowGraphic ) ;
642 UMASetControlTitle( m_controlHandle , label , wxFont::GetDefaultEncoding() ) ;
645 InstallControlEventHandler( (ControlRef
) controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
646 GetEventTypeCount(eventList
), eventList
, tool
,NULL
);
648 tool
->SetControlHandle( controlHandle
) ;
651 case wxTOOL_STYLE_CONTROL
:
652 wxASSERT( tool
->GetControl() != NULL
) ;
653 // right now there's nothing to do here
659 ControlRef container
= (ControlRef
) GetHandle() ;
660 wxASSERT_MSG( container
!= NULL
, wxT("No valid mac container control") ) ;
662 UMAShowControl( controlHandle
) ;
663 ::EmbedControl( controlHandle
, container
) ;
666 if ( tool
->CanBeToggled() && tool
->IsToggled() )
668 tool
->UpdateToggleImage( true ) ;
671 // nothing special to do here - we relayout in Realize() later
673 InvalidateBestSize();
678 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
680 wxFAIL_MSG( _T("not implemented") );
683 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolbase
)
685 wxToolBarTool
* tool
= wx_static_cast( wxToolBarTool
* , toolbase
) ;
686 wxToolBarToolsList::compatibility_iterator node
;
687 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
689 wxToolBarToolBase
*tool2
= node
->GetData();
692 // let node point to the next node in the list
693 node
= node
->GetNext();
699 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize() ;
703 switch ( tool
->GetStyle() )
705 case wxTOOL_STYLE_CONTROL
:
707 tool
->GetControl()->Destroy();
708 tool
->ClearControl() ;
712 case wxTOOL_STYLE_BUTTON
:
713 case wxTOOL_STYLE_SEPARATOR
:
714 if ( tool
->GetControlHandle() )
716 DisposeControl( (ControlRef
) tool
->GetControlHandle() ) ;
717 tool
->SetControlHandle( (ControlRef
) NULL
) ;
722 // and finally reposition all the controls after this one
724 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
726 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
727 wxPoint pt
= tool2
->GetPosition() ;
729 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
737 tool2
->SetPosition( pt
) ;
740 InvalidateBestSize();
744 void wxToolBar::OnPaint(wxPaintEvent
& event
)
750 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
751 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
753 if ( UMAGetSystemVersion() >= 0x1030 )
755 HIThemePlacardDrawInfo info
;
756 memset( &info
, 0 , sizeof( info
) ) ;
758 info
.state
= IsEnabled() ? kThemeStateActive
: kThemeStateInactive
;
760 CGContextRef cgContext
= (CGContextRef
) MacGetCGContextRef() ;
761 HIRect rect
= CGRectMake( 0 , 0 , w
, h
) ;
762 HIThemeDrawPlacard( &rect
, & info
, cgContext
, kHIThemeOrientationNormal
) ;
767 // leave the background as it is (striped or metal)
770 wxMacPortSetter
helper(&dc
) ;
772 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
773 dc
.YLOG2DEVMAC(h
) , dc
.XLOG2DEVMAC(w
) } ;
775 if( toolbarrect.left < 0 )
776 toolbarrect.left = 0 ;
777 if ( toolbarrect.top < 0 )
778 toolbarrect.top = 0 ;
780 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
782 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
786 #if TARGET_API_MAC_OSX
787 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
788 if ( UMAGetSystemVersion() >= 0x1030 )
790 HIRect hiToolbarrect
= CGRectMake( dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
791 dc
.YLOG2DEVREL(h
) , dc
.XLOG2DEVREL(w
) );
792 CGContextRef cgContext
;
794 GetPortBounds( (CGrafPtr
) dc
.m_macPort
, &bounds
) ;
795 QDBeginCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
) ;
796 CGContextTranslateCTM( cgContext
, 0 , bounds
.bottom
- bounds
.top
) ;
797 CGContextScaleCTM( cgContext
, 1 , -1 ) ;
800 HIThemeBackgroundDrawInfo drawInfo
;
801 drawInfo
.version
= 0 ;
802 drawInfo
.state
= kThemeStateActive
;
803 drawInfo
.kind
= kThemeBackgroundMetal
;
804 HIThemeApplyBackground( &hiToolbarrect
, &drawInfo
, cgContext
,kHIThemeOrientationNormal
) ;
806 QDEndCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
) ;
811 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
820 #endif // wxUSE_TOOLBAR