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
405 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
406 m_maxHeight
= maxHeight
;
410 if ( GetToolsCount() > 0 && m_maxRows
== 0 )
412 // if not set yet, have one column
413 SetRows(GetToolsCount());
416 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
417 m_maxWidth
= maxWidth
;
420 SetSize( maxWidth
, maxHeight
);
421 InvalidateBestSize();
426 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
428 m_defaultWidth
= size
.x
+4; m_defaultHeight
= size
.y
+4;
431 // The button size is bigger than the bitmap size
432 wxSize
wxToolBar::GetToolSize() const
434 return wxSize(m_defaultWidth
+ 4, m_defaultHeight
+ 4);
437 void wxToolBar::SetRows(int nRows
)
439 if ( nRows
== m_maxRows
)
441 // avoid resizing the frame uselessly
448 void wxToolBar::MacSuperChangedPosition()
450 wxWindow::MacSuperChangedPosition() ;
454 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
456 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
459 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData() ;
460 wxRect2DInt
r( tool
->GetPosition() , tool
->GetSize() ) ;
461 if ( r
.Contains( wxPoint( x
, y
) ) )
466 node
= node
->GetNext();
469 return (wxToolBarToolBase
*)NULL
;
472 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
474 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
477 return tool
->GetShortHelp() ;
479 return wxEmptyString
;
482 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
487 ((wxToolBarTool
*)t
)->DoEnable( enable
) ;
490 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
495 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
496 if ( tool
->IsButton() )
498 ::SetControl32BitValue( (ControlRef
) tool
->GetControlHandle() , toggle
) ;
502 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
503 wxToolBarToolBase
*tool
)
505 // nothing special to do here - we relayout in Realize() later
507 InvalidateBestSize();
512 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
514 wxFAIL_MSG( _T("not implemented") );
517 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
519 wxToolBarToolsList::compatibility_iterator node
;
520 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
522 wxToolBarToolBase
*tool2
= node
->GetData();
525 // let node point to the next node in the list
526 node
= node
->GetNext();
532 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize() ;
536 // and finally reposition all the controls after this one
538 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
540 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
541 wxPoint pt
= tool2
->GetPosition() ;
543 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
551 tool2
->SetPosition( pt
) ;
554 InvalidateBestSize();
558 void wxToolBar::OnPaint(wxPaintEvent
& event
)
561 wxMacPortSetter
helper(&dc
) ;
565 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
566 dc
.YLOG2DEVMAC(h
) , dc
.XLOG2DEVMAC(w
) } ;
568 if( toolbarrect.left < 0 )
569 toolbarrect.left = 0 ;
570 if ( toolbarrect.top < 0 )
571 toolbarrect.top = 0 ;
573 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
575 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
579 #if TARGET_API_MAC_OSX
580 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
581 if ( UMAGetSystemVersion() >= 0x1030 )
583 HIRect hiToolbarrect
= CGRectMake( dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
584 dc
.YLOG2DEVREL(h
) , dc
.XLOG2DEVREL(w
) );
585 CGContextRef cgContext
;
587 GetPortBounds( (CGrafPtr
) dc
.m_macPort
, &bounds
) ;
588 QDBeginCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
) ;
589 CGContextTranslateCTM( cgContext
, 0 , bounds
.bottom
- bounds
.top
) ;
590 CGContextScaleCTM( cgContext
, 1 , -1 ) ;
593 HIThemeBackgroundDrawInfo drawInfo
;
594 drawInfo
.version
= 0 ;
595 drawInfo
.state
= kThemeStateActive
;
596 drawInfo
.kind
= kThemeBackgroundMetal
;
597 HIThemeApplyBackground( &hiToolbarrect
, &drawInfo
, cgContext
,kHIThemeOrientationNormal
) ;
599 QDEndCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
) ;
604 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
611 #endif // wxUSE_TOOLBAR