1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: The wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "toolbar.h"
20 #include "wx/toolbar.h"
21 #include "wx/notebook.h"
22 #include "wx/tabctrl.h"
23 #include "wx/bitmap.h"
25 #if !USE_SHARED_LIBRARY
26 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
28 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
29 EVT_PAINT( wxToolBar::OnPaint
)
33 #include "wx/mac/uma.h"
34 #include "wx/geometry.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 class wxToolBarTool
: public wxToolBarToolBase
42 wxToolBarTool(wxToolBar
*tbar
,
44 const wxString
& label
,
45 const wxBitmap
& bmpNormal
,
46 const wxBitmap
& bmpDisabled
,
49 const wxString
& shortHelp
,
50 const wxString
& longHelp
) ;
52 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
53 : wxToolBarToolBase(tbar
, control
)
60 if ( m_controlHandle
)
61 DisposeControl( m_controlHandle
) ;
64 WXWidget
GetControlHandle() { return (WXWidget
) m_controlHandle
; }
65 void SetControlHandle( ControlRef handle
) { m_controlHandle
= handle
; }
67 void SetSize(const wxSize
& size
) ;
68 void SetPosition( const wxPoint
& position
) ;
70 wxSize
GetSize() const
74 return GetControl()->GetSize() ;
76 else if ( IsButton() )
78 return GetToolBar()->GetToolSize() ;
82 wxSize sz
= GetToolBar()->GetToolSize() ;
88 wxPoint
GetPosition() const
90 return wxPoint(m_x
, m_y
);
92 bool DoEnable( bool enable
) ;
96 m_controlHandle
= NULL
;
98 ControlRef m_controlHandle
;
104 static const EventTypeSpec eventList
[] =
106 { kEventClassControl
, kEventControlHit
} ,
109 static pascal OSStatus
wxMacToolBarToolControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
111 OSStatus result
= eventNotHandledErr
;
113 wxMacCarbonEvent
cEvent( event
) ;
115 ControlRef controlRef
;
117 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
) ;
119 switch( GetEventKind( event
) )
121 case kEventControlHit
:
123 wxToolBarTool
* tbartool
= (wxToolBarTool
*)data
;
124 if ( tbartool
->CanBeToggled() )
126 ((wxToolBar
*)tbartool
->GetToolBar())->ToggleTool(tbartool
->GetId(), GetControl32BitValue((ControlRef
)tbartool
->GetControlHandle()));
128 ((wxToolBar
*)tbartool
->GetToolBar())->OnLeftClick( tbartool
->GetId() , tbartool
-> IsToggled() ) ;
138 pascal OSStatus
wxMacToolBarToolEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
140 OSStatus result
= eventNotHandledErr
;
142 switch ( GetEventClass( event
) )
144 case kEventClassControl
:
145 result
= wxMacToolBarToolControlEventHandler( handler
, event
, data
) ;
153 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler
)
155 // ============================================================================
157 // ============================================================================
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
163 bool wxToolBarTool::DoEnable(bool enable
)
167 GetControl()->Enable( enable
) ;
169 else if ( IsButton() )
171 #if TARGET_API_MAC_OSX
173 EnableControl( m_controlHandle
) ;
175 DisableControl( m_controlHandle
) ;
178 ActivateControl( m_controlHandle
) ;
180 DeactivateControl( m_controlHandle
) ;
185 void wxToolBarTool::SetSize(const wxSize
& size
)
189 GetControl()->SetSize( size
) ;
193 void wxToolBarTool::SetPosition(const wxPoint
& position
)
202 int mac_x
= position
.x
;
203 int mac_y
= position
.y
;
204 #if !TARGET_API_MAC_OSX
205 WindowRef rootwindow
= (WindowRef
) GetToolBar()->MacGetTopLevelWindowRef() ;
206 GetToolBar()->MacWindowToRootWindow( &x
, &y
) ;
211 GetControlBounds( m_controlHandle
, &contrlRect
) ;
212 int former_mac_x
= contrlRect
.left
;
213 int former_mac_y
= contrlRect
.top
;
214 GetToolBar()->GetToolSize() ;
216 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
218 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
) ;
221 else if ( IsControl() )
223 GetControl()->Move( position
) ;
227 const short kwxMacToolBarToolDefaultWidth
= 24 ;
228 const short kwxMacToolBarToolDefaultHeight
= 22 ;
229 const short kwxMacToolBarTopMargin
= 2 ;
230 const short kwxMacToolBarLeftMargin
= 2 ;
232 wxToolBarTool::wxToolBarTool(wxToolBar
*tbar
,
234 const wxString
& label
,
235 const wxBitmap
& bmpNormal
,
236 const wxBitmap
& bmpDisabled
,
238 wxObject
*clientData
,
239 const wxString
& shortHelp
,
240 const wxString
& longHelp
)
241 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
242 clientData
, shortHelp
, longHelp
)
246 if (id
== wxID_SEPARATOR
) return;
248 WindowRef window
= (WindowRef
) tbar
->MacGetTopLevelWindowRef() ;
249 wxSize toolSize
= tbar
->GetToolSize() ;
250 Rect toolrect
= { 0, 0 , toolSize
.y
, toolSize
.x
} ;
252 ControlButtonContentInfo info
;
253 wxMacCreateBitmapButton( &info
, GetNormalBitmap() ) ;
255 SInt16 behaviour
= kControlBehaviorOffsetContents
;
256 if ( CanBeToggled() )
257 behaviour
+= kControlBehaviorToggles
;
259 CreateBevelButtonControl( window
, &toolrect
, CFSTR("") , kControlBevelButtonNormalBevel
, behaviour
, &info
,
260 0 , 0 , 0 , &m_controlHandle
) ;
262 InstallControlEventHandler( (ControlRef
) m_controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
263 GetEventTypeCount(eventList
), eventList
, this,NULL
);
265 UMAShowControl( m_controlHandle
) ;
267 if ( CanBeToggled() && IsToggled() )
268 ::SetControl32BitValue( m_controlHandle
, 1 ) ;
270 ::SetControl32BitValue( m_controlHandle
, 0 ) ;
272 ControlRef container
= (ControlRef
) tbar
->GetHandle() ;
273 wxASSERT_MSG( container
!= NULL
, wxT("No valid mac container control") ) ;
274 ::EmbedControl( m_controlHandle
, container
) ;
278 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
279 const wxString
& label
,
280 const wxBitmap
& bmpNormal
,
281 const wxBitmap
& bmpDisabled
,
283 wxObject
*clientData
,
284 const wxString
& shortHelp
,
285 const wxString
& longHelp
)
287 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
288 clientData
, shortHelp
, longHelp
);
291 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
293 return new wxToolBarTool(this, control
);
296 void wxToolBar::Init()
300 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
301 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
304 bool wxToolBar::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
305 long style
, const wxString
& name
)
307 if ( !wxToolBarBase::Create( parent
, id
, pos
, size
, style
) )
313 wxToolBar::~wxToolBar()
315 // we must refresh the frame size when the toolbar is deleted but the frame
316 // is not - otherwise toolbar leaves a hole in the place it used to occupy
319 bool wxToolBar::Realize()
321 if (m_tools
.GetCount() == 0)
324 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
325 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
333 int maxToolWidth
= 0;
334 int maxToolHeight
= 0;
336 // Find the maximum tool width and height
337 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
340 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
341 wxSize sz
= tool
->GetSize() ;
343 if ( sz
.x
> maxToolWidth
)
344 maxToolWidth
= sz
.x
;
345 if (sz
.y
> maxToolHeight
)
346 maxToolHeight
= sz
.y
;
348 node
= node
->GetNext();
351 bool lastWasRadio
= FALSE
;
352 node
= m_tools
.GetFirst();
355 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
356 wxSize cursize
= tool
->GetSize() ;
358 bool isRadio
= FALSE
;
360 if ( tool
->IsButton() && tool
->GetKind() == wxITEM_RADIO
)
364 if (tool
->Toggle(true))
366 DoToggleTool(tool
, true);
375 lastWasRadio
= isRadio
;
377 // for the moment we just do a single row/column alignement
378 if ( x
+ cursize
.x
> maxWidth
)
379 maxWidth
= x
+ cursize
.x
;
380 if ( y
+ cursize
.y
> maxHeight
)
381 maxHeight
= y
+ cursize
.y
;
383 tool
->SetPosition( wxPoint( x
, y
) ) ;
385 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
394 node
= node
->GetNext();
397 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
399 if ( m_maxRows
== 0 )
401 // if not set yet, only one row
404 m_minWidth
= maxWidth
;
406 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
407 m_minHeight
= m_maxHeight
= maxHeight
;
411 if ( GetToolsCount() > 0 && m_maxRows
== 0 )
413 // if not set yet, have one column
414 SetRows(GetToolsCount());
416 m_minHeight
= maxHeight
;
418 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
419 m_minWidth
= m_maxWidth
= maxWidth
;
422 SetSize( maxWidth
, maxHeight
);
423 InvalidateBestSize();
428 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
430 m_defaultWidth
= size
.x
+4; m_defaultHeight
= size
.y
+4;
433 // The button size is bigger than the bitmap size
434 wxSize
wxToolBar::GetToolSize() const
436 return wxSize(m_defaultWidth
+ 4, m_defaultHeight
+ 4);
439 void wxToolBar::SetRows(int nRows
)
441 if ( nRows
== m_maxRows
)
443 // avoid resizing the frame uselessly
450 void wxToolBar::MacSuperChangedPosition()
452 wxWindow::MacSuperChangedPosition() ;
456 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
458 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
461 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData() ;
462 wxRect2DInt
r( tool
->GetPosition() , tool
->GetSize() ) ;
463 if ( r
.Contains( wxPoint( x
, y
) ) )
468 node
= node
->GetNext();
471 return (wxToolBarToolBase
*)NULL
;
474 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
476 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
479 return tool
->GetShortHelp() ;
481 return wxEmptyString
;
484 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
486 ((wxToolBarTool
*)t
)->DoEnable( enable
) ;
489 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
491 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
492 if ( tool
->IsButton() )
494 ::SetControl32BitValue( (ControlRef
) tool
->GetControlHandle() , toggle
) ;
498 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
499 wxToolBarToolBase
*tool
)
501 // nothing special to do here - we relayout in Realize() later
503 InvalidateBestSize();
508 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
510 wxFAIL_MSG( _T("not implemented") );
513 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
515 wxToolBarToolsList::compatibility_iterator node
;
516 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
518 wxToolBarToolBase
*tool2
= node
->GetData();
521 // let node point to the next node in the list
522 node
= node
->GetNext();
528 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize() ;
532 // and finally reposition all the controls after this one
534 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
536 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
537 wxPoint pt
= tool2
->GetPosition() ;
539 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
547 tool2
->SetPosition( pt
) ;
550 InvalidateBestSize();
554 void wxToolBar::OnPaint(wxPaintEvent
& event
)
557 wxMacPortSetter
helper(&dc
) ;
561 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
562 dc
.YLOG2DEVMAC(h
) , dc
.XLOG2DEVMAC(w
) } ;
564 if( toolbarrect.left < 0 )
565 toolbarrect.left = 0 ;
566 if ( toolbarrect.top < 0 )
567 toolbarrect.top = 0 ;
569 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
571 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
575 #if TARGET_API_MAC_OSX
576 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
577 if ( UMAGetSystemVersion() >= 0x1030 )
579 HIRect hiToolbarrect
= CGRectMake( dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
580 dc
.YLOG2DEVREL(h
) , dc
.XLOG2DEVREL(w
) );
581 CGContextRef cgContext
;
583 GetPortBounds( (CGrafPtr
) dc
.m_macPort
, &bounds
) ;
584 QDBeginCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
) ;
585 CGContextTranslateCTM( cgContext
, 0 , bounds
.bottom
- bounds
.top
) ;
586 CGContextScaleCTM( cgContext
, 1 , -1 ) ;
589 HIThemeBackgroundDrawInfo drawInfo
;
590 drawInfo
.version
= 0 ;
591 drawInfo
.state
= kThemeStateActive
;
592 drawInfo
.kind
= kThemeBackgroundMetal
;
593 HIThemeApplyBackground( &hiToolbarrect
, &drawInfo
, cgContext
,kHIThemeOrientationNormal
) ;
595 QDEndCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
) ;
600 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
607 #endif // wxUSE_TOOLBAR