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 tbartool
->Toggle( GetControl32BitValue( (ControlRef
) tbartool
->GetControlHandle() ) ) ;
128 ((wxToolBar
*)tbartool
->GetToolBar())->OnLeftClick( tbartool
->GetId() , tbartool
-> IsToggled() ) ;
139 pascal OSStatus
wxMacToolBarToolEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
141 OSStatus result
= eventNotHandledErr
;
143 switch ( GetEventClass( event
) )
145 case kEventClassControl
:
146 result
= wxMacToolBarToolControlEventHandler( handler
, event
, data
) ;
154 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler
)
156 // ============================================================================
158 // ============================================================================
160 // ----------------------------------------------------------------------------
162 // ----------------------------------------------------------------------------
164 bool wxToolBarTool::DoEnable(bool enable
)
168 GetControl()->Enable( enable
) ;
170 else if ( IsButton() )
172 #if TARGET_API_MAC_OSX
174 EnableControl( m_controlHandle
) ;
176 DisableControl( m_controlHandle
) ;
179 ActivateControl( m_controlHandle
) ;
181 DeactivateControl( m_controlHandle
) ;
186 void wxToolBarTool::SetSize(const wxSize
& size
)
190 GetControl()->SetSize( size
) ;
194 void wxToolBarTool::SetPosition(const wxPoint
& position
)
203 int mac_x
= position
.x
;
204 int mac_y
= position
.y
;
205 #if !TARGET_API_MAC_OSX
206 WindowRef rootwindow
= (WindowRef
) GetToolBar()->MacGetTopLevelWindowRef() ;
207 GetToolBar()->MacWindowToRootWindow( &x
, &y
) ;
212 GetControlBounds( m_controlHandle
, &contrlRect
) ;
213 int former_mac_x
= contrlRect
.left
;
214 int former_mac_y
= contrlRect
.top
;
215 wxSize sz
= GetToolBar()->GetToolSize() ;
217 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
219 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
) ;
222 else if ( IsControl() )
224 GetControl()->Move( position
) ;
228 const short kwxMacToolBarToolDefaultWidth
= 24 ;
229 const short kwxMacToolBarToolDefaultHeight
= 22 ;
230 const short kwxMacToolBarTopMargin
= 2 ;
231 const short kwxMacToolBarLeftMargin
= 2 ;
233 wxToolBarTool::wxToolBarTool(wxToolBar
*tbar
,
235 const wxString
& label
,
236 const wxBitmap
& bmpNormal
,
237 const wxBitmap
& bmpDisabled
,
239 wxObject
*clientData
,
240 const wxString
& shortHelp
,
241 const wxString
& longHelp
)
242 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
243 clientData
, shortHelp
, longHelp
)
247 if (id
== wxID_SEPARATOR
) return;
249 WindowRef window
= (WindowRef
) tbar
->MacGetTopLevelWindowRef() ;
250 wxSize toolSize
= tbar
->GetToolSize() ;
251 Rect toolrect
= { 0, 0 , toolSize
.y
, toolSize
.x
} ;
253 ControlButtonContentInfo info
;
254 wxMacCreateBitmapButton( &info
, GetNormalBitmap() ) ;
256 SInt16 behaviour
= kControlBehaviorOffsetContents
;
257 if ( CanBeToggled() )
258 behaviour
+= kControlBehaviorToggles
;
260 CreateBevelButtonControl( window
, &toolrect
, CFSTR("") , kControlBevelButtonNormalBevel
, behaviour
, &info
,
261 0 , 0 , 0 , &m_controlHandle
) ;
263 InstallControlEventHandler( (ControlRef
) m_controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
264 GetEventTypeCount(eventList
), eventList
, this,NULL
);
266 UMAShowControl( m_controlHandle
) ;
268 if ( CanBeToggled() && IsToggled() )
269 ::SetControl32BitValue( m_controlHandle
, 1 ) ;
271 ::SetControl32BitValue( m_controlHandle
, 0 ) ;
273 ControlRef container
= (ControlRef
) tbar
->GetHandle() ;
274 wxASSERT_MSG( container
!= NULL
, wxT("No valid mac container control") ) ;
275 ::EmbedControl( m_controlHandle
, container
) ;
279 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
280 const wxString
& label
,
281 const wxBitmap
& bmpNormal
,
282 const wxBitmap
& bmpDisabled
,
284 wxObject
*clientData
,
285 const wxString
& shortHelp
,
286 const wxString
& longHelp
)
288 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
289 clientData
, shortHelp
, longHelp
);
292 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
294 return new wxToolBarTool(this, control
);
297 void wxToolBar::Init()
301 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
302 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
305 bool wxToolBar::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
306 long style
, const wxString
& name
)
308 if ( !wxToolBarBase::Create( parent
, id
, pos
, size
, style
) )
314 wxToolBar::~wxToolBar()
316 // we must refresh the frame size when the toolbar is deleted but the frame
317 // is not - otherwise toolbar leaves a hole in the place it used to occupy
320 bool wxToolBar::Realize()
322 if (m_tools
.GetCount() == 0)
325 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
326 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
334 int maxToolWidth
= 0;
335 int maxToolHeight
= 0;
337 // Find the maximum tool width and height
338 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
341 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
342 wxSize sz
= tool
->GetSize() ;
344 if ( sz
.x
> maxToolWidth
)
345 maxToolWidth
= sz
.x
;
346 if (sz
.y
> maxToolHeight
)
347 maxToolHeight
= sz
.y
;
349 node
= node
->GetNext();
352 node
= m_tools
.GetFirst();
355 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
356 wxSize cursize
= tool
->GetSize() ;
358 // for the moment we just do a single row/column alignement
359 if ( x
+ cursize
.x
> maxWidth
)
360 maxWidth
= x
+ cursize
.x
;
361 if ( y
+ cursize
.y
> maxHeight
)
362 maxHeight
= y
+ cursize
.y
;
364 tool
->SetPosition( wxPoint( x
, y
) ) ;
366 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
375 node
= node
->GetNext();
378 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
380 if ( m_maxRows
== 0 )
382 // if not set yet, only one row
386 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
387 m_maxHeight
= maxHeight
;
391 if ( GetToolsCount() > 0 && m_maxRows
== 0 )
393 // if not set yet, have one column
394 SetRows(GetToolsCount());
397 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
398 m_maxWidth
= maxWidth
;
401 SetSize( maxWidth
, maxHeight
);
402 InvalidateBestSize();
407 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
409 m_defaultWidth
= size
.x
+4; m_defaultHeight
= size
.y
+4;
412 // The button size is bigger than the bitmap size
413 wxSize
wxToolBar::GetToolSize() const
415 return wxSize(m_defaultWidth
+ 4, m_defaultHeight
+ 4);
418 void wxToolBar::SetRows(int nRows
)
420 if ( nRows
== m_maxRows
)
422 // avoid resizing the frame uselessly
429 void wxToolBar::MacSuperChangedPosition()
431 wxWindow::MacSuperChangedPosition() ;
435 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
437 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
440 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData() ;
441 wxRect2DInt
r( tool
->GetPosition() , tool
->GetSize() ) ;
442 if ( r
.Contains( wxPoint( x
, y
) ) )
447 node
= node
->GetNext();
450 return (wxToolBarToolBase
*)NULL
;
453 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
455 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
458 return tool
->GetShortHelp() ;
460 return wxEmptyString
;
463 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
468 ((wxToolBarTool
*)t
)->DoEnable( enable
) ;
471 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
476 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
477 if ( tool
->IsButton() )
479 ::SetControl32BitValue( (ControlRef
) tool
->GetControlHandle() , toggle
) ;
483 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
484 wxToolBarToolBase
*tool
)
486 // nothing special to do here - we relayout in Realize() later
488 InvalidateBestSize();
493 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
495 wxFAIL_MSG( _T("not implemented") );
498 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
500 wxToolBarToolsList::compatibility_iterator node
;
501 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
503 wxToolBarToolBase
*tool2
= node
->GetData();
506 // let node point to the next node in the list
507 node
= node
->GetNext();
513 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize() ;
517 // and finally reposition all the controls after this one
519 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
521 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
522 wxPoint pt
= tool2
->GetPosition() ;
524 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
532 tool2
->SetPosition( pt
) ;
535 InvalidateBestSize();
539 void wxToolBar::OnPaint(wxPaintEvent
& event
)
542 wxMacPortSetter
helper(&dc
) ;
546 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
547 dc
.YLOG2DEVMAC(h
) , dc
.XLOG2DEVMAC(w
) } ;
549 if( toolbarrect.left < 0 )
550 toolbarrect.left = 0 ;
551 if ( toolbarrect.top < 0 )
552 toolbarrect.top = 0 ;
554 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
556 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
560 #if TARGET_API_MAC_OSX
561 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
562 if ( UMAGetSystemVersion() >= 0x1030 )
564 HIRect hiToolbarrect
= CGRectMake( dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
565 dc
.YLOG2DEVREL(h
) , dc
.XLOG2DEVREL(w
) );
566 CGContextRef cgContext
;
568 GetPortBounds( (CGrafPtr
) dc
.m_macPort
, &bounds
) ;
569 QDBeginCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
) ;
570 CGContextTranslateCTM( cgContext
, 0 , bounds
.bottom
- bounds
.top
) ;
571 CGContextScaleCTM( cgContext
, 1 , -1 ) ;
574 HIThemeBackgroundDrawInfo drawInfo
;
575 drawInfo
.version
= 0 ;
576 drawInfo
.state
= kThemeStateActive
;
577 drawInfo
.kind
= kThemeBackgroundMetal
;
578 HIThemeApplyBackground( &hiToolbarrect
, &drawInfo
, cgContext
,kHIThemeOrientationNormal
) ;
580 QDEndCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
) ;
585 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
592 #endif // wxUSE_TOOLBAR