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 wxMacReleaseBitmapButton( &info
) ;
265 InstallControlEventHandler( (ControlRef
) m_controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
266 GetEventTypeCount(eventList
), eventList
, this,NULL
);
268 UMAShowControl( m_controlHandle
) ;
270 if ( CanBeToggled() && IsToggled() )
271 ::SetControl32BitValue( m_controlHandle
, 1 ) ;
273 ::SetControl32BitValue( m_controlHandle
, 0 ) ;
275 ControlRef container
= (ControlRef
) tbar
->GetHandle() ;
276 wxASSERT_MSG( container
!= NULL
, wxT("No valid mac container control") ) ;
277 ::EmbedControl( m_controlHandle
, container
) ;
281 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
282 const wxString
& label
,
283 const wxBitmap
& bmpNormal
,
284 const wxBitmap
& bmpDisabled
,
286 wxObject
*clientData
,
287 const wxString
& shortHelp
,
288 const wxString
& longHelp
)
290 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
291 clientData
, shortHelp
, longHelp
);
294 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
296 return new wxToolBarTool(this, control
);
299 void wxToolBar::Init()
303 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
304 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
307 bool wxToolBar::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
308 long style
, const wxString
& name
)
310 if ( !wxToolBarBase::Create( parent
, id
, pos
, size
, style
) )
316 wxToolBar::~wxToolBar()
318 // we must refresh the frame size when the toolbar is deleted but the frame
319 // is not - otherwise toolbar leaves a hole in the place it used to occupy
322 bool wxToolBar::Realize()
324 if (m_tools
.GetCount() == 0)
327 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
328 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
336 int maxToolWidth
= 0;
337 int maxToolHeight
= 0;
339 // Find the maximum tool width and height
340 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
343 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
344 wxSize sz
= tool
->GetSize() ;
346 if ( sz
.x
> maxToolWidth
)
347 maxToolWidth
= sz
.x
;
348 if (sz
.y
> maxToolHeight
)
349 maxToolHeight
= sz
.y
;
351 node
= node
->GetNext();
354 bool lastWasRadio
= FALSE
;
355 node
= m_tools
.GetFirst();
358 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
359 wxSize cursize
= tool
->GetSize() ;
361 bool isRadio
= FALSE
;
363 if ( tool
->IsButton() && tool
->GetKind() == wxITEM_RADIO
)
367 if (tool
->Toggle(true))
369 DoToggleTool(tool
, true);
378 lastWasRadio
= isRadio
;
380 // for the moment we just do a single row/column alignement
381 if ( x
+ cursize
.x
> maxWidth
)
382 maxWidth
= x
+ cursize
.x
;
383 if ( y
+ cursize
.y
> maxHeight
)
384 maxHeight
= y
+ cursize
.y
;
386 tool
->SetPosition( wxPoint( x
, y
) ) ;
388 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
397 node
= node
->GetNext();
400 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
402 if ( m_maxRows
== 0 )
404 // if not set yet, only one row
407 m_minWidth
= maxWidth
;
409 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
410 m_minHeight
= m_maxHeight
= maxHeight
;
414 if ( GetToolsCount() > 0 && m_maxRows
== 0 )
416 // if not set yet, have one column
417 SetRows(GetToolsCount());
419 m_minHeight
= maxHeight
;
421 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
422 m_minWidth
= m_maxWidth
= maxWidth
;
425 SetSize( maxWidth
, maxHeight
);
426 InvalidateBestSize();
431 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
433 m_defaultWidth
= size
.x
+4; m_defaultHeight
= size
.y
+4;
436 // The button size is bigger than the bitmap size
437 wxSize
wxToolBar::GetToolSize() const
439 return wxSize(m_defaultWidth
+ 4, m_defaultHeight
+ 4);
442 void wxToolBar::SetRows(int nRows
)
444 if ( nRows
== m_maxRows
)
446 // avoid resizing the frame uselessly
453 void wxToolBar::MacSuperChangedPosition()
455 wxWindow::MacSuperChangedPosition() ;
459 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
461 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
464 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData() ;
465 wxRect2DInt
r( tool
->GetPosition() , tool
->GetSize() ) ;
466 if ( r
.Contains( wxPoint( x
, y
) ) )
471 node
= node
->GetNext();
474 return (wxToolBarToolBase
*)NULL
;
477 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
479 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
482 return tool
->GetShortHelp() ;
484 return wxEmptyString
;
487 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
489 ((wxToolBarTool
*)t
)->DoEnable( enable
) ;
492 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
494 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
495 if ( tool
->IsButton() )
497 ::SetControl32BitValue( (ControlRef
) tool
->GetControlHandle() , toggle
) ;
501 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
502 wxToolBarToolBase
*tool
)
504 // nothing special to do here - we relayout in Realize() later
506 InvalidateBestSize();
511 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
513 wxFAIL_MSG( _T("not implemented") );
516 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
518 wxToolBarToolsList::compatibility_iterator node
;
519 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
521 wxToolBarToolBase
*tool2
= node
->GetData();
524 // let node point to the next node in the list
525 node
= node
->GetNext();
531 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize() ;
535 // and finally reposition all the controls after this one
537 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
539 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
540 wxPoint pt
= tool2
->GetPosition() ;
542 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
550 tool2
->SetPosition( pt
) ;
553 InvalidateBestSize();
557 void wxToolBar::OnPaint(wxPaintEvent
& event
)
560 #if wxMAC_USE_CORE_GRAPHICS
562 wxMacPortSetter
helper(&dc
) ;
566 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
567 dc
.YLOG2DEVMAC(h
) , dc
.XLOG2DEVMAC(w
) } ;
569 if( toolbarrect.left < 0 )
570 toolbarrect.left = 0 ;
571 if ( toolbarrect.top < 0 )
572 toolbarrect.top = 0 ;
574 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
576 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
580 #if TARGET_API_MAC_OSX
581 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
582 if ( UMAGetSystemVersion() >= 0x1030 )
584 HIRect hiToolbarrect
= CGRectMake( dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
585 dc
.YLOG2DEVREL(h
) , dc
.XLOG2DEVREL(w
) );
586 CGContextRef cgContext
;
588 GetPortBounds( (CGrafPtr
) dc
.m_macPort
, &bounds
) ;
589 QDBeginCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
) ;
590 CGContextTranslateCTM( cgContext
, 0 , bounds
.bottom
- bounds
.top
) ;
591 CGContextScaleCTM( cgContext
, 1 , -1 ) ;
594 HIThemeBackgroundDrawInfo drawInfo
;
595 drawInfo
.version
= 0 ;
596 drawInfo
.state
= kThemeStateActive
;
597 drawInfo
.kind
= kThemeBackgroundMetal
;
598 HIThemeApplyBackground( &hiToolbarrect
, &drawInfo
, cgContext
,kHIThemeOrientationNormal
) ;
600 QDEndCGContext( (CGrafPtr
) dc
.m_macPort
, &cgContext
) ;
605 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
614 #endif // wxUSE_TOOLBAR