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"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 class wxToolBarTool
: public wxToolBarToolBase
43 wxToolBarTool(wxToolBar
*tbar
,
45 const wxString
& label
,
46 const wxBitmap
& bmpNormal
,
47 const wxBitmap
& bmpDisabled
,
50 const wxString
& shortHelp
,
51 const wxString
& longHelp
) ;
53 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
54 : wxToolBarToolBase(tbar
, control
)
61 if ( m_controlHandle
)
62 DisposeControl( m_controlHandle
) ;
65 WXWidget
GetControlHandle() { return (WXWidget
) m_controlHandle
; }
66 void SetControlHandle( ControlRef handle
) { m_controlHandle
= handle
; }
68 void SetSize(const wxSize
& size
) ;
69 void SetPosition( const wxPoint
& position
) ;
71 wxSize
GetSize() const
75 return GetControl()->GetSize() ;
77 else if ( IsButton() )
79 return GetToolBar()->GetToolSize() ;
83 wxSize sz
= GetToolBar()->GetToolSize() ;
89 wxPoint
GetPosition() const
91 return wxPoint(m_x
, m_y
);
93 bool DoEnable( bool enable
) ;
97 m_controlHandle
= NULL
;
99 ControlRef m_controlHandle
;
105 static const EventTypeSpec eventList
[] =
107 { kEventClassControl
, kEventControlHit
} ,
110 static pascal OSStatus
wxMacToolBarToolControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
112 OSStatus result
= eventNotHandledErr
;
114 wxMacCarbonEvent
cEvent( event
) ;
116 ControlRef controlRef
;
118 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
) ;
120 switch( GetEventKind( event
) )
122 case kEventControlHit
:
124 wxToolBarTool
* tbartool
= (wxToolBarTool
*)data
;
125 if ( tbartool
->CanBeToggled() )
127 ((wxToolBar
*)tbartool
->GetToolBar())->ToggleTool(tbartool
->GetId(), GetControl32BitValue((ControlRef
)tbartool
->GetControlHandle()));
129 ((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 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 bool lastWasRadio
= FALSE
;
353 node
= m_tools
.GetFirst();
356 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
357 wxSize cursize
= tool
->GetSize() ;
359 bool isRadio
= FALSE
;
361 if ( tool
->IsButton() && tool
->GetKind() == wxITEM_RADIO
)
365 if (tool
->Toggle(true))
367 DoToggleTool(tool
, true);
376 lastWasRadio
= isRadio
;
378 // for the moment we just do a single row/column alignement
379 if ( x
+ cursize
.x
> maxWidth
)
380 maxWidth
= x
+ cursize
.x
;
381 if ( y
+ cursize
.y
> maxHeight
)
382 maxHeight
= y
+ cursize
.y
;
384 tool
->SetPosition( wxPoint( x
, y
) ) ;
386 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
395 node
= node
->GetNext();
398 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
400 if ( m_maxRows
== 0 )
402 // if not set yet, only one row
405 m_minWidth
= maxWidth
;
407 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
408 m_minHeight
= m_maxHeight
= maxHeight
;
412 if ( GetToolsCount() > 0 && m_maxRows
== 0 )
414 // if not set yet, have one column
415 SetRows(GetToolsCount());
417 m_minHeight
= maxHeight
;
419 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
420 m_minWidth
= m_maxWidth
= maxWidth
;
423 SetSize( maxWidth
, maxHeight
);
424 InvalidateBestSize();
429 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
431 m_defaultWidth
= size
.x
+4; m_defaultHeight
= size
.y
+4;
434 // The button size is bigger than the bitmap size
435 wxSize
wxToolBar::GetToolSize() const
437 return wxSize(m_defaultWidth
+ 4, m_defaultHeight
+ 4);
440 void wxToolBar::SetRows(int nRows
)
442 if ( nRows
== m_maxRows
)
444 // avoid resizing the frame uselessly
451 void wxToolBar::MacSuperChangedPosition()
453 wxWindow::MacSuperChangedPosition() ;
457 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
459 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
462 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData() ;
463 wxRect2DInt
r( tool
->GetPosition() , tool
->GetSize() ) ;
464 if ( r
.Contains( wxPoint( x
, y
) ) )
469 node
= node
->GetNext();
472 return (wxToolBarToolBase
*)NULL
;
475 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
477 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
480 return tool
->GetShortHelp() ;
482 return wxEmptyString
;
485 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
487 ((wxToolBarTool
*)t
)->DoEnable( enable
) ;
490 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
492 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
493 if ( tool
->IsButton() )
495 ::SetControl32BitValue( (ControlRef
) tool
->GetControlHandle() , toggle
) ;
499 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
500 wxToolBarToolBase
*tool
)
502 // nothing special to do here - we relayout in Realize() later
504 InvalidateBestSize();
509 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
511 wxFAIL_MSG( _T("not implemented") );
514 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
516 wxToolBarToolsList::compatibility_iterator node
;
517 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
519 wxToolBarToolBase
*tool2
= node
->GetData();
522 // let node point to the next node in the list
523 node
= node
->GetNext();
529 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize() ;
533 // and finally reposition all the controls after this one
535 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
537 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
538 wxPoint pt
= tool2
->GetPosition() ;
540 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
548 tool2
->SetPosition( pt
) ;
551 InvalidateBestSize();
555 void wxToolBar::OnPaint(wxPaintEvent
& event
)
558 wxMacPortSetter
helper(&dc
) ;
562 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
563 dc
.YLOG2DEVMAC(h
) , dc
.XLOG2DEVMAC(w
) } ;
565 if( toolbarrect.left < 0 )
566 toolbarrect.left = 0 ;
567 if ( toolbarrect.top < 0 )
568 toolbarrect.top = 0 ;
570 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
572 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
576 #if TARGET_API_MAC_OSX
577 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
578 if ( UMAGetSystemVersion() >= 0x1030 )
580 HIRect hiToolbarrect
= CGRectMake( dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
581 dc
.YLOG2DEVREL(h
) , dc
.XLOG2DEVREL(w
) );
582 CGContextRef cgContext
;
584 GetPortBounds( (CGrafPtr
) dc
.m_macPort
, &bounds
) ;
585 QDBeginCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
) ;
586 CGContextTranslateCTM( cgContext
, 0 , bounds
.bottom
- bounds
.top
) ;
587 CGContextScaleCTM( cgContext
, 1 , -1 ) ;
590 HIThemeBackgroundDrawInfo drawInfo
;
591 drawInfo
.version
= 0 ;
592 drawInfo
.state
= kThemeStateActive
;
593 drawInfo
.kind
= kThemeBackgroundMetal
;
594 HIThemeApplyBackground( &hiToolbarrect
, &drawInfo
, cgContext
,kHIThemeOrientationNormal
) ;
596 QDEndCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
) ;
601 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
608 #endif // wxUSE_TOOLBAR