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() ;
107 wxPoint
GetPosition() const
109 return wxPoint(m_x
, m_y
);
111 bool DoEnable( bool enable
) ;
115 m_controlHandle
= NULL
;
117 ControlRef m_controlHandle
;
123 static const EventTypeSpec eventList
[] =
125 { kEventClassControl
, kEventControlHit
} ,
128 static pascal OSStatus
wxMacToolBarToolControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
130 OSStatus result
= eventNotHandledErr
;
132 wxMacCarbonEvent
cEvent( event
) ;
134 ControlRef controlRef
;
136 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
) ;
138 switch( GetEventKind( event
) )
140 case kEventControlHit
:
142 wxToolBarTool
* tbartool
= (wxToolBarTool
*)data
;
143 if ( tbartool
->CanBeToggled() )
145 ((wxToolBar
*)tbartool
->GetToolBar())->ToggleTool(tbartool
->GetId(), GetControl32BitValue((ControlRef
)tbartool
->GetControlHandle()));
147 ((wxToolBar
*)tbartool
->GetToolBar())->OnLeftClick( tbartool
->GetId() , tbartool
-> IsToggled() ) ;
157 pascal OSStatus
wxMacToolBarToolEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
159 OSStatus result
= eventNotHandledErr
;
161 switch ( GetEventClass( event
) )
163 case kEventClassControl
:
164 result
= wxMacToolBarToolControlEventHandler( handler
, event
, data
) ;
172 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler
)
174 // ============================================================================
176 // ============================================================================
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
182 bool wxToolBarTool::DoEnable(bool enable
)
186 GetControl()->Enable( enable
) ;
188 else if ( IsButton() )
190 #if TARGET_API_MAC_OSX
192 EnableControl( m_controlHandle
) ;
194 DisableControl( m_controlHandle
) ;
197 ActivateControl( m_controlHandle
) ;
199 DeactivateControl( m_controlHandle
) ;
204 void wxToolBarTool::SetSize(const wxSize
& size
)
208 GetControl()->SetSize( size
) ;
212 void wxToolBarTool::SetPosition(const wxPoint
& position
)
221 int mac_x
= position
.x
;
222 int mac_y
= position
.y
;
224 wxSize toolSize
= m_tbar
->GetToolSize() ;
226 if ( toolSize
.x
>= 24 && toolSize
.y
>= 24)
230 mac_x
+= ( toolSize
.x
- iconsize
) / 2 ;
231 mac_y
+= ( toolSize
.y
- iconsize
) / 2 ;
233 WindowRef rootwindow
= (WindowRef
) GetToolBar()->MacGetTopLevelWindowRef() ;
234 GetToolBar()->MacWindowToRootWindow( &x
, &y
) ;
239 GetControlBounds( m_controlHandle
, &contrlRect
) ;
240 int former_mac_x
= contrlRect
.left
;
241 int former_mac_y
= contrlRect
.top
;
242 GetToolBar()->GetToolSize() ;
244 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
246 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
) ;
249 else if ( IsControl() )
251 GetControl()->Move( position
) ;
259 int mac_x
= position
.x
;
260 int mac_y
= position
.y
;
263 GetControlBounds( m_controlHandle
, &contrlRect
) ;
264 int former_mac_x
= contrlRect
.left
;
265 int former_mac_y
= contrlRect
.top
;
267 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
269 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
) ;
275 wxToolBarTool::wxToolBarTool(wxToolBar
*tbar
,
277 const wxString
& label
,
278 const wxBitmap
& bmpNormal
,
279 const wxBitmap
& bmpDisabled
,
281 wxObject
*clientData
,
282 const wxString
& shortHelp
,
283 const wxString
& longHelp
)
284 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
285 clientData
, shortHelp
, longHelp
)
289 WindowRef window
= (WindowRef
) tbar
->MacGetTopLevelWindowRef() ;
290 wxSize toolSize
= tbar
->GetToolSize() ;
291 Rect toolrect
= { 0, 0 , toolSize
.y
, toolSize
.x
} ;
293 if ( id
== wxID_SEPARATOR
)
297 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
299 toolrect
.bottom
= toolSize
.y
;
303 toolrect
.right
= toolSize
.x
;
306 // in flat style we need a visual separator
307 CreateSeparatorControl( window
, &toolrect
, &m_controlHandle
) ;
312 ControlButtonContentInfo info
;
313 wxMacCreateBitmapButton( &info
, GetNormalBitmap() ) ;
317 if ( toolSize
.x
>= 24 && toolSize
.y
>= 24)
321 toolrect
.left
+= ( toolSize
.x
- iconsize
) / 2 ;
322 toolrect
.right
= toolrect
.left
+ iconsize
;
323 toolrect
.top
+= ( toolSize
.y
- iconsize
) / 2 ;
324 toolrect
.bottom
= toolrect
.top
+ iconsize
;
325 CreateIconControl( window
, &toolrect
, &info
, false , &m_controlHandle
) ;
327 SInt16 behaviour
= kControlBehaviorOffsetContents
;
328 if ( CanBeToggled() )
329 behaviour
+= kControlBehaviorToggles
;
330 CreateBevelButtonControl( window
, &toolrect
, CFSTR("") , kControlBevelButtonNormalBevel
, behaviour
, &info
,
331 0 , 0 , 0 , &m_controlHandle
) ;
334 wxMacReleaseBitmapButton( &info
) ;
336 SetBevelButtonTextPlacement( m_controlHandle , kControlBevelButtonPlaceBelowGraphic ) ;
337 UMASetControlTitle( m_controlHandle , label , wxFont::GetDefaultEncoding() ) ;
340 InstallControlEventHandler( (ControlRef
) m_controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
341 GetEventTypeCount(eventList
), eventList
, this,NULL
);
343 if ( CanBeToggled() && IsToggled() )
346 // overlay with rounded white rect and set selected to 1
348 ::SetControl32BitValue( m_controlHandle
, 1 ) ;
352 ControlRef container
= (ControlRef
) tbar
->GetHandle() ;
353 wxASSERT_MSG( container
!= NULL
, wxT("No valid mac container control") ) ;
354 if ( m_controlHandle
)
356 UMAShowControl( m_controlHandle
) ;
357 ::EmbedControl( m_controlHandle
, container
) ;
362 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
363 const wxString
& label
,
364 const wxBitmap
& bmpNormal
,
365 const wxBitmap
& bmpDisabled
,
367 wxObject
*clientData
,
368 const wxString
& shortHelp
,
369 const wxString
& longHelp
)
371 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
372 clientData
, shortHelp
, longHelp
);
375 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
377 return new wxToolBarTool(this, control
);
380 void wxToolBar::Init()
384 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
385 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
388 bool wxToolBar::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
389 long style
, const wxString
& name
)
392 if ( !wxToolBarBase::Create( parent
, id
, pos
, size
, style
) )
398 wxToolBar::~wxToolBar()
400 // we must refresh the frame size when the toolbar is deleted but the frame
401 // is not - otherwise toolbar leaves a hole in the place it used to occupy
404 bool wxToolBar::Realize()
406 if (m_tools
.GetCount() == 0)
409 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
410 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
418 int maxToolWidth
= 0;
419 int maxToolHeight
= 0;
421 // Find the maximum tool width and height
422 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
425 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
426 wxSize sz
= tool
->GetSize() ;
428 if ( sz
.x
> maxToolWidth
)
429 maxToolWidth
= sz
.x
;
430 if (sz
.y
> maxToolHeight
)
431 maxToolHeight
= sz
.y
;
433 node
= node
->GetNext();
436 bool lastWasRadio
= FALSE
;
437 node
= m_tools
.GetFirst();
440 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
441 wxSize cursize
= tool
->GetSize() ;
443 bool isRadio
= FALSE
;
445 if ( tool
->IsButton() && tool
->GetKind() == wxITEM_RADIO
)
449 if (tool
->Toggle(true))
451 DoToggleTool(tool
, true);
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 tool
->SetPosition( wxPoint( x
, y
) ) ;
470 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
473 y
+= kwxMacToolSpacing
;
478 x
+= kwxMacToolSpacing
;
481 node
= node
->GetNext();
484 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
486 if ( m_maxRows
== 0 )
488 // if not set yet, only one row
491 m_minWidth
= maxWidth
;
493 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
494 m_minHeight
= m_maxHeight
= maxHeight
;
498 if ( GetToolsCount() > 0 && m_maxRows
== 0 )
500 // if not set yet, have one column
501 SetRows(GetToolsCount());
503 m_minHeight
= maxHeight
;
505 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
506 m_minWidth
= m_maxWidth
= maxWidth
;
509 SetSize( maxWidth
, maxHeight
);
510 InvalidateBestSize();
515 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
517 m_defaultWidth
= size
.x
+kwxMacToolBorder
; m_defaultHeight
= size
.y
+kwxMacToolBorder
;
520 // The button size is bigger than the bitmap size
521 wxSize
wxToolBar::GetToolSize() const
523 return wxSize(m_defaultWidth
+ kwxMacToolBorder
, m_defaultHeight
+ kwxMacToolBorder
);
526 void wxToolBar::SetRows(int nRows
)
528 if ( nRows
== m_maxRows
)
530 // avoid resizing the frame uselessly
537 void wxToolBar::MacSuperChangedPosition()
539 wxWindow::MacSuperChangedPosition() ;
543 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
545 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
548 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData() ;
549 wxRect2DInt
r( tool
->GetPosition() , tool
->GetSize() ) ;
550 if ( r
.Contains( wxPoint( x
, y
) ) )
555 node
= node
->GetNext();
558 return (wxToolBarToolBase
*)NULL
;
561 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
563 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
566 return tool
->GetShortHelp() ;
568 return wxEmptyString
;
571 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
573 ((wxToolBarTool
*)t
)->DoEnable( enable
) ;
576 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
578 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
579 if ( tool
->IsButton() )
582 IconTransformType transform
= toggle
? kTransformSelected
: kTransformNone
;
583 SetControlData( (ControlRef
) tool
->GetControlHandle(), 0, kControlIconTransformTag
, sizeof( transform
),
586 ::SetControl32BitValue( (ControlRef
) tool
->GetControlHandle() , toggle
) ;
591 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
592 wxToolBarToolBase
*tool
)
594 // nothing special to do here - we relayout in Realize() later
596 InvalidateBestSize();
601 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
603 wxFAIL_MSG( _T("not implemented") );
606 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
608 wxToolBarToolsList::compatibility_iterator node
;
609 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
611 wxToolBarToolBase
*tool2
= node
->GetData();
614 // let node point to the next node in the list
615 node
= node
->GetNext();
621 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize() ;
625 // and finally reposition all the controls after this one
627 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
629 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
630 wxPoint pt
= tool2
->GetPosition() ;
632 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
640 tool2
->SetPosition( pt
) ;
643 InvalidateBestSize();
647 void wxToolBar::OnPaint(wxPaintEvent
& event
)
650 #if wxMAC_USE_CORE_GRAPHICS
651 // leave the background as it is (striped or metal)
653 wxMacPortSetter
helper(&dc
) ;
657 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
658 dc
.YLOG2DEVMAC(h
) , dc
.XLOG2DEVMAC(w
) } ;
660 if( toolbarrect.left < 0 )
661 toolbarrect.left = 0 ;
662 if ( toolbarrect.top < 0 )
663 toolbarrect.top = 0 ;
665 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
667 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
671 #if TARGET_API_MAC_OSX
672 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
673 if ( UMAGetSystemVersion() >= 0x1030 )
675 HIRect hiToolbarrect
= CGRectMake( dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
676 dc
.YLOG2DEVREL(h
) , dc
.XLOG2DEVREL(w
) );
677 CGContextRef cgContext
;
679 GetPortBounds( (CGrafPtr
) dc
.m_macPort
, &bounds
) ;
680 QDBeginCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
) ;
681 CGContextTranslateCTM( cgContext
, 0 , bounds
.bottom
- bounds
.top
) ;
682 CGContextScaleCTM( cgContext
, 1 , -1 ) ;
685 HIThemeBackgroundDrawInfo drawInfo
;
686 drawInfo
.version
= 0 ;
687 drawInfo
.state
= kThemeStateActive
;
688 drawInfo
.kind
= kThemeBackgroundMetal
;
689 HIThemeApplyBackground( &hiToolbarrect
, &drawInfo
, cgContext
,kHIThemeOrientationNormal
) ;
691 QDEndCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
) ;
696 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
705 #endif // wxUSE_TOOLBAR