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
= 24 ;
39 const short kwxMacToolBarToolDefaultHeight
= 24 ;
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 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
= kControlButtonPart
;
172 cEvent
.SetParameter( kEventParamControlPart
, typeControlPartCode
, pc
) ;
183 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
)
343 WindowRef window
= (WindowRef
) tbar
->MacGetTopLevelWindowRef() ;
344 wxSize toolSize
= tbar
->GetToolSize() ;
345 Rect toolrect
= { 0, 0 , toolSize
.y
, toolSize
.x
} ;
347 if ( id
== wxID_SEPARATOR
)
351 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
353 toolrect
.bottom
= toolSize
.y
;
357 toolrect
.right
= toolSize
.x
;
360 // in flat style we need a visual separator
361 CreateSeparatorControl( window
, &toolrect
, &m_controlHandle
) ;
366 ControlButtonContentInfo info
;
367 wxMacCreateBitmapButton( &info
, GetNormalBitmap() , kControlContentIconRef
) ;
370 CreateIconControl( window
, &toolrect
, &info
, false , &m_controlHandle
) ;
372 SInt16 behaviour
= kControlBehaviorOffsetContents
;
373 if ( CanBeToggled() )
374 behaviour
+= kControlBehaviorToggles
;
375 CreateBevelButtonControl( window
, &toolrect
, CFSTR("") , kControlBevelButtonNormalBevel
, behaviour
, &info
,
376 0 , 0 , 0 , &m_controlHandle
) ;
379 wxMacReleaseBitmapButton( &info
) ;
381 SetBevelButtonTextPlacement( m_controlHandle , kControlBevelButtonPlaceBelowGraphic ) ;
382 UMASetControlTitle( m_controlHandle , label , wxFont::GetDefaultEncoding() ) ;
385 InstallControlEventHandler( (ControlRef
) m_controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
386 GetEventTypeCount(eventList
), eventList
, this,NULL
);
389 ControlRef container
= (ControlRef
) tbar
->GetHandle() ;
390 wxASSERT_MSG( container
!= NULL
, wxT("No valid mac container control") ) ;
391 if ( m_controlHandle
)
393 UMAShowControl( m_controlHandle
) ;
394 ::EmbedControl( m_controlHandle
, container
) ;
396 if ( CanBeToggled() && IsToggled() )
398 UpdateToggleImage( true ) ;
403 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
404 const wxString
& label
,
405 const wxBitmap
& bmpNormal
,
406 const wxBitmap
& bmpDisabled
,
408 wxObject
*clientData
,
409 const wxString
& shortHelp
,
410 const wxString
& longHelp
)
412 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
413 clientData
, shortHelp
, longHelp
);
416 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
418 return new wxToolBarTool(this, control
);
421 void wxToolBar::Init()
425 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
426 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
429 bool wxToolBar::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
430 long style
, const wxString
& name
)
433 if ( !wxToolBarBase::Create( parent
, id
, pos
, size
, style
) )
439 wxToolBar::~wxToolBar()
441 // we must refresh the frame size when the toolbar is deleted but the frame
442 // is not - otherwise toolbar leaves a hole in the place it used to occupy
445 bool wxToolBar::Realize()
447 if (m_tools
.GetCount() == 0)
450 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
451 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
459 int maxToolWidth
= 0;
460 int maxToolHeight
= 0;
462 // Find the maximum tool width and height
463 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
466 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
467 wxSize sz
= tool
->GetSize() ;
469 if ( sz
.x
> maxToolWidth
)
470 maxToolWidth
= sz
.x
;
471 if (sz
.y
> maxToolHeight
)
472 maxToolHeight
= sz
.y
;
474 node
= node
->GetNext();
477 bool lastWasRadio
= FALSE
;
478 node
= m_tools
.GetFirst();
481 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
482 wxSize cursize
= tool
->GetSize() ;
484 bool isRadio
= FALSE
;
486 if ( tool
->IsButton() && tool
->GetKind() == wxITEM_RADIO
)
490 if (tool
->Toggle(true))
492 DoToggleTool(tool
, true);
501 lastWasRadio
= isRadio
;
503 // for the moment we just do a single row/column alignement
504 if ( x
+ cursize
.x
> maxWidth
)
505 maxWidth
= x
+ cursize
.x
;
506 if ( y
+ cursize
.y
> maxHeight
)
507 maxHeight
= y
+ cursize
.y
;
509 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
511 int x1
= x
+ (maxToolWidth
- cursize
.x
)/2 ;
512 tool
->SetPosition( wxPoint( x1
, y
) ) ;
516 int y1
= y
+ (maxToolHeight
- cursize
.y
)/2 ;
517 tool
->SetPosition( wxPoint( x
, y1
) ) ;
519 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
522 y
+= kwxMacToolSpacing
;
527 x
+= kwxMacToolSpacing
;
530 node
= node
->GetNext();
533 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
535 if ( m_maxRows
== 0 )
537 // if not set yet, only one row
540 m_minWidth
= maxWidth
;
542 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
543 m_minHeight
= m_maxHeight
= maxHeight
;
547 if ( GetToolsCount() > 0 && m_maxRows
== 0 )
549 // if not set yet, have one column
550 SetRows(GetToolsCount());
552 m_minHeight
= maxHeight
;
554 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
555 m_minWidth
= m_maxWidth
= maxWidth
;
558 SetSize( maxWidth
, maxHeight
);
559 InvalidateBestSize();
564 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
566 m_defaultWidth
= size
.x
+kwxMacToolBorder
; m_defaultHeight
= size
.y
+kwxMacToolBorder
;
569 // The button size is bigger than the bitmap size
570 wxSize
wxToolBar::GetToolSize() const
572 return wxSize(m_defaultWidth
+ kwxMacToolBorder
, m_defaultHeight
+ kwxMacToolBorder
);
575 void wxToolBar::SetRows(int nRows
)
577 if ( nRows
== m_maxRows
)
579 // avoid resizing the frame uselessly
586 void wxToolBar::MacSuperChangedPosition()
588 wxWindow::MacSuperChangedPosition() ;
592 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
594 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
597 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData() ;
598 wxRect2DInt
r( tool
->GetPosition() , tool
->GetSize() ) ;
599 if ( r
.Contains( wxPoint( x
, y
) ) )
604 node
= node
->GetNext();
607 return (wxToolBarToolBase
*)NULL
;
610 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
612 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
615 return tool
->GetShortHelp() ;
617 return wxEmptyString
;
620 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
622 ((wxToolBarTool
*)t
)->DoEnable( enable
) ;
625 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
627 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
628 if ( tool
->IsButton() )
630 tool
->UpdateToggleImage( toggle
) ;
634 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
635 wxToolBarToolBase
*tool
)
637 // nothing special to do here - we relayout in Realize() later
639 InvalidateBestSize();
644 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
646 wxFAIL_MSG( _T("not implemented") );
649 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
651 wxToolBarToolsList::compatibility_iterator node
;
652 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
654 wxToolBarToolBase
*tool2
= node
->GetData();
657 // let node point to the next node in the list
658 node
= node
->GetNext();
664 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize() ;
668 // and finally reposition all the controls after this one
670 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
672 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
673 wxPoint pt
= tool2
->GetPosition() ;
675 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
683 tool2
->SetPosition( pt
) ;
686 InvalidateBestSize();
690 void wxToolBar::OnPaint(wxPaintEvent
& event
)
696 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
697 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
699 if ( UMAGetSystemVersion() >= 0x1030 )
701 HIThemePlacardDrawInfo info
;
702 memset( &info
, 0 , sizeof( info
) ) ;
704 info
.state
= IsEnabled() ? kThemeStateActive
: kThemeStateInactive
;
706 CGContextRef cgContext
= (CGContextRef
) MacGetCGContextRef() ;
707 HIRect rect
= CGRectMake( 0 , 0 , w
, h
) ;
708 HIThemeDrawPlacard( &rect
, & info
, cgContext
, kHIThemeOrientationNormal
) ;
713 // leave the background as it is (striped or metal)
716 wxMacPortSetter
helper(&dc
) ;
718 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
719 dc
.YLOG2DEVMAC(h
) , dc
.XLOG2DEVMAC(w
) } ;
721 if( toolbarrect.left < 0 )
722 toolbarrect.left = 0 ;
723 if ( toolbarrect.top < 0 )
724 toolbarrect.top = 0 ;
726 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
728 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
732 #if TARGET_API_MAC_OSX
733 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
734 if ( UMAGetSystemVersion() >= 0x1030 )
736 HIRect hiToolbarrect
= CGRectMake( dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
737 dc
.YLOG2DEVREL(h
) , dc
.XLOG2DEVREL(w
) );
738 CGContextRef cgContext
;
740 GetPortBounds( (CGrafPtr
) dc
.m_macPort
, &bounds
) ;
741 QDBeginCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
) ;
742 CGContextTranslateCTM( cgContext
, 0 , bounds
.bottom
- bounds
.top
) ;
743 CGContextScaleCTM( cgContext
, 1 , -1 ) ;
746 HIThemeBackgroundDrawInfo drawInfo
;
747 drawInfo
.version
= 0 ;
748 drawInfo
.state
= kThemeStateActive
;
749 drawInfo
.kind
= kThemeBackgroundMetal
;
750 HIThemeApplyBackground( &hiToolbarrect
, &drawInfo
, cgContext
,kHIThemeOrientationNormal
) ;
752 QDEndCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
) ;
757 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
766 #endif // wxUSE_TOOLBAR