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
)
247 int mac_x
= position
.x
;
248 int mac_y
= position
.y
;
250 // already correctly set up
252 WindowRef rootwindow
= (WindowRef
) GetToolBar()->MacGetTopLevelWindowRef() ;
253 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
) ;
278 int mac_x
= position
.x
;
279 int mac_y
= position
.y
;
282 GetControlBounds( m_controlHandle
, &contrlRect
) ;
283 int former_mac_x
= contrlRect
.left
;
284 int former_mac_y
= contrlRect
.top
;
286 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
288 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
) ;
294 void wxToolBarTool::UpdateToggleImage( bool toggle
)
299 int w
= m_bmpNormal
.GetWidth() ;
300 int h
= m_bmpNormal
.GetHeight() ;
301 wxBitmap
bmp( w
, h
) ;
303 dc
.SelectObject( bmp
) ;
304 dc
.SetPen( wxNullPen
) ;
305 dc
.SetBackground( *wxWHITE
) ;
306 dc
.DrawRectangle( 0 , 0 , w
, h
) ;
307 dc
.DrawBitmap( m_bmpNormal
, 0 , 0 , true) ;
308 dc
.SelectObject( wxNullBitmap
) ;
309 ControlButtonContentInfo info
;
310 wxMacCreateBitmapButton( &info
, bmp
) ;
311 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof( info
),
313 wxMacReleaseBitmapButton( &info
) ;
317 ControlButtonContentInfo info
;
318 wxMacCreateBitmapButton( &info
, m_bmpNormal
) ;
319 SetControlData( m_controlHandle
, 0, kControlIconContentTag
, sizeof( info
),
321 wxMacReleaseBitmapButton( &info
) ;
324 IconTransformType transform
= toggle
? kTransformSelected
: kTransformNone
;
325 SetControlData( m_controlHandle
, 0, kControlIconTransformTag
, sizeof( transform
),
327 HIViewSetNeedsDisplay( m_controlHandle
, true ) ;
330 ::SetControl32BitValue( m_controlHandle
, toggle
) ;
334 wxToolBarTool::wxToolBarTool(wxToolBar
*tbar
,
336 const wxString
& label
,
337 const wxBitmap
& bmpNormal
,
338 const wxBitmap
& bmpDisabled
,
340 wxObject
*clientData
,
341 const wxString
& shortHelp
,
342 const wxString
& longHelp
)
343 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
344 clientData
, shortHelp
, longHelp
)
348 WindowRef window
= (WindowRef
) tbar
->MacGetTopLevelWindowRef() ;
349 wxSize toolSize
= tbar
->GetToolSize() ;
350 Rect toolrect
= { 0, 0 , toolSize
.y
, toolSize
.x
} ;
352 if ( id
== wxID_SEPARATOR
)
356 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
358 toolrect
.bottom
= toolSize
.y
;
362 toolrect
.right
= toolSize
.x
;
365 // in flat style we need a visual separator
366 CreateSeparatorControl( window
, &toolrect
, &m_controlHandle
) ;
371 ControlButtonContentInfo info
;
372 wxMacCreateBitmapButton( &info
, GetNormalBitmap() , kControlContentIconRef
) ;
375 CreateIconControl( window
, &toolrect
, &info
, false , &m_controlHandle
) ;
377 SInt16 behaviour
= kControlBehaviorOffsetContents
;
378 if ( CanBeToggled() )
379 behaviour
+= kControlBehaviorToggles
;
380 CreateBevelButtonControl( window
, &toolrect
, CFSTR("") , kControlBevelButtonNormalBevel
, behaviour
, &info
,
381 0 , 0 , 0 , &m_controlHandle
) ;
384 wxMacReleaseBitmapButton( &info
) ;
386 SetBevelButtonTextPlacement( m_controlHandle , kControlBevelButtonPlaceBelowGraphic ) ;
387 UMASetControlTitle( m_controlHandle , label , wxFont::GetDefaultEncoding() ) ;
390 InstallControlEventHandler( (ControlRef
) m_controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
391 GetEventTypeCount(eventList
), eventList
, this,NULL
);
394 ControlRef container
= (ControlRef
) tbar
->GetHandle() ;
395 wxASSERT_MSG( container
!= NULL
, wxT("No valid mac container control") ) ;
396 if ( m_controlHandle
)
398 UMAShowControl( m_controlHandle
) ;
399 ::EmbedControl( m_controlHandle
, container
) ;
401 if ( CanBeToggled() && IsToggled() )
403 UpdateToggleImage( true ) ;
408 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
409 const wxString
& label
,
410 const wxBitmap
& bmpNormal
,
411 const wxBitmap
& bmpDisabled
,
413 wxObject
*clientData
,
414 const wxString
& shortHelp
,
415 const wxString
& longHelp
)
417 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
418 clientData
, shortHelp
, longHelp
);
421 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
423 return new wxToolBarTool(this, control
);
426 void wxToolBar::Init()
430 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
431 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
434 bool wxToolBar::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
435 long style
, const wxString
& name
)
438 if ( !wxToolBarBase::Create( parent
, id
, pos
, size
, style
) )
444 wxToolBar::~wxToolBar()
446 // we must refresh the frame size when the toolbar is deleted but the frame
447 // is not - otherwise toolbar leaves a hole in the place it used to occupy
450 bool wxToolBar::Realize()
452 if (m_tools
.GetCount() == 0)
455 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
456 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
464 int maxToolWidth
= 0;
465 int maxToolHeight
= 0;
467 // Find the maximum tool width and height
468 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
471 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
472 wxSize sz
= tool
->GetSize() ;
474 if ( sz
.x
> maxToolWidth
)
475 maxToolWidth
= sz
.x
;
476 if (sz
.y
> maxToolHeight
)
477 maxToolHeight
= sz
.y
;
479 node
= node
->GetNext();
482 bool lastWasRadio
= FALSE
;
483 node
= m_tools
.GetFirst();
486 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
487 wxSize cursize
= tool
->GetSize() ;
489 bool isRadio
= FALSE
;
491 if ( tool
->IsButton() && tool
->GetKind() == wxITEM_RADIO
)
495 if (tool
->Toggle(true))
497 DoToggleTool(tool
, true);
506 lastWasRadio
= isRadio
;
508 // for the moment we just do a single row/column alignement
509 if ( x
+ cursize
.x
> maxWidth
)
510 maxWidth
= x
+ cursize
.x
;
511 if ( y
+ cursize
.y
> maxHeight
)
512 maxHeight
= y
+ cursize
.y
;
514 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
516 int x1
= x
+ (maxToolWidth
- cursize
.x
)/2 ;
517 tool
->SetPosition( wxPoint( x1
, y
) ) ;
521 int y1
= y
+ (maxToolHeight
- cursize
.y
)/2 ;
522 tool
->SetPosition( wxPoint( x
, y1
) ) ;
524 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
527 y
+= kwxMacToolSpacing
;
532 x
+= kwxMacToolSpacing
;
535 node
= node
->GetNext();
538 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
540 if ( m_maxRows
== 0 )
542 // if not set yet, only one row
545 m_minWidth
= maxWidth
;
547 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
548 m_minHeight
= m_maxHeight
= maxHeight
;
552 if ( GetToolsCount() > 0 && m_maxRows
== 0 )
554 // if not set yet, have one column
555 SetRows(GetToolsCount());
557 m_minHeight
= maxHeight
;
559 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
560 m_minWidth
= m_maxWidth
= maxWidth
;
563 SetSize( maxWidth
, maxHeight
);
564 InvalidateBestSize();
569 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
571 m_defaultWidth
= size
.x
+kwxMacToolBorder
; m_defaultHeight
= size
.y
+kwxMacToolBorder
;
574 // The button size is bigger than the bitmap size
575 wxSize
wxToolBar::GetToolSize() const
577 return wxSize(m_defaultWidth
+ kwxMacToolBorder
, m_defaultHeight
+ kwxMacToolBorder
);
580 void wxToolBar::SetRows(int nRows
)
582 if ( nRows
== m_maxRows
)
584 // avoid resizing the frame uselessly
591 void wxToolBar::MacSuperChangedPosition()
593 wxWindow::MacSuperChangedPosition() ;
597 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
599 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
602 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData() ;
603 wxRect2DInt
r( tool
->GetPosition() , tool
->GetSize() ) ;
604 if ( r
.Contains( wxPoint( x
, y
) ) )
609 node
= node
->GetNext();
612 return (wxToolBarToolBase
*)NULL
;
615 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
617 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
620 return tool
->GetShortHelp() ;
622 return wxEmptyString
;
625 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
627 ((wxToolBarTool
*)t
)->DoEnable( enable
) ;
630 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
632 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
633 if ( tool
->IsButton() )
635 tool
->UpdateToggleImage( toggle
) ;
639 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
640 wxToolBarToolBase
*tool
)
642 // nothing special to do here - we relayout in Realize() later
644 InvalidateBestSize();
649 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
651 wxFAIL_MSG( _T("not implemented") );
654 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
656 wxToolBarToolsList::compatibility_iterator node
;
657 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
659 wxToolBarToolBase
*tool2
= node
->GetData();
662 // let node point to the next node in the list
663 node
= node
->GetNext();
669 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize() ;
673 // and finally reposition all the controls after this one
675 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
677 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
678 wxPoint pt
= tool2
->GetPosition() ;
680 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
688 tool2
->SetPosition( pt
) ;
691 InvalidateBestSize();
695 void wxToolBar::OnPaint(wxPaintEvent
& event
)
698 #if wxMAC_USE_CORE_GRAPHICS
699 // leave the background as it is (striped or metal)
701 wxMacPortSetter
helper(&dc
) ;
705 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
706 dc
.YLOG2DEVMAC(h
) , dc
.XLOG2DEVMAC(w
) } ;
708 if( toolbarrect.left < 0 )
709 toolbarrect.left = 0 ;
710 if ( toolbarrect.top < 0 )
711 toolbarrect.top = 0 ;
713 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
715 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
719 #if TARGET_API_MAC_OSX
720 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
721 if ( UMAGetSystemVersion() >= 0x1030 )
723 HIRect hiToolbarrect
= CGRectMake( dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
724 dc
.YLOG2DEVREL(h
) , dc
.XLOG2DEVREL(w
) );
725 CGContextRef cgContext
;
727 GetPortBounds( (CGrafPtr
) dc
.m_macPort
, &bounds
) ;
728 QDBeginCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
) ;
729 CGContextTranslateCTM( cgContext
, 0 , bounds
.bottom
- bounds
.top
) ;
730 CGContextScaleCTM( cgContext
, 1 , -1 ) ;
733 HIThemeBackgroundDrawInfo drawInfo
;
734 drawInfo
.version
= 0 ;
735 drawInfo
.state
= kThemeStateActive
;
736 drawInfo
.kind
= kThemeBackgroundMetal
;
737 HIThemeApplyBackground( &hiToolbarrect
, &drawInfo
, cgContext
,kHIThemeOrientationNormal
) ;
739 QDEndCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
) ;
744 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
753 #endif // wxUSE_TOOLBAR