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_MOUSE_EVENTS( wxToolBar::OnMouse
)
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
) ;
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
);
95 m_controlHandle
= NULL
;
97 ControlRef m_controlHandle
;
103 static const EventTypeSpec eventList
[] =
105 { kEventClassControl
, kEventControlHit
} ,
108 static pascal OSStatus
wxMacToolBarToolControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
110 OSStatus result
= eventNotHandledErr
;
112 wxMacCarbonEvent
cEvent( event
) ;
114 ControlRef controlRef
;
116 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
) ;
118 switch( GetEventKind( event
) )
120 case kEventControlHit
:
122 wxToolBarTool
* tbartool
= (wxToolBarTool
*)data
;
123 if ( tbartool
->CanBeToggled() )
125 tbartool
->Toggle( GetControl32BitValue( (ControlRef
) tbartool
->GetControlHandle() ) ) ;
127 ((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 void wxToolBarTool::SetSize(const wxSize
& size
)
167 GetControl()->SetSize( size
) ;
171 void wxToolBarTool::SetPosition(const wxPoint
& position
)
180 int mac_x
= position
.x
;
181 int mac_y
= position
.y
;
182 #if !TARGET_API_MAC_OSX
183 WindowRef rootwindow
= (WindowRef
) GetToolBar()->MacGetTopLevelWindowRef() ;
184 GetToolBar()->MacWindowToRootWindow( &x
, &y
) ;
189 GetControlBounds( m_controlHandle
, &contrlRect
) ;
190 int former_mac_x
= contrlRect
.left
;
191 int former_mac_y
= contrlRect
.top
;
192 wxSize sz
= GetToolBar()->GetToolSize() ;
194 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
196 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
) ;
199 else if ( IsControl() )
201 GetControl()->Move( position
) ;
205 const short kwxMacToolBarToolDefaultWidth
= 24 ;
206 const short kwxMacToolBarToolDefaultHeight
= 22 ;
207 const short kwxMacToolBarTopMargin
= 2 ;
208 const short kwxMacToolBarLeftMargin
= 2 ;
210 wxToolBarTool::wxToolBarTool(wxToolBar
*tbar
,
212 const wxString
& label
,
213 const wxBitmap
& bmpNormal
,
214 const wxBitmap
& bmpDisabled
,
216 wxObject
*clientData
,
217 const wxString
& shortHelp
,
218 const wxString
& longHelp
)
219 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
220 clientData
, shortHelp
, longHelp
)
224 if (id
== wxID_SEPARATOR
) return;
226 WindowRef window
= (WindowRef
) tbar
->MacGetTopLevelWindowRef() ;
227 wxSize toolSize
= tbar
->GetToolSize() ;
228 Rect toolrect
= { 0, 0 , toolSize
.y
, toolSize
.x
} ;
230 ControlButtonContentInfo info
;
231 wxMacCreateBitmapButton( &info
, GetNormalBitmap() ) ;
233 SInt16 behaviour
= kControlBehaviorOffsetContents
;
234 if ( CanBeToggled() )
235 behaviour
+= kControlBehaviorToggles
;
237 if ( info
.contentType
!= kControlNoContent
)
239 m_controlHandle
= ::NewControl( window
, &toolrect
, "\p" , true , 0 ,
240 behaviour
+ info
.contentType
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
242 ::SetControlData( m_controlHandle
, kControlButtonPart
, kControlBevelButtonContentTag
, sizeof(info
) , (char*) &info
) ;
246 m_controlHandle
= ::NewControl( window
, &toolrect
, "\p" , true , 0 ,
247 behaviour
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
249 InstallControlEventHandler( (ControlRef
) m_controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
250 GetEventTypeCount(eventList
), eventList
, this,NULL
);
251 UMAShowControl( m_controlHandle
) ;
254 UMADeactivateControl( m_controlHandle
) ;
256 if ( CanBeToggled() && IsToggled() )
258 ::SetControl32BitValue( m_controlHandle
, 1 ) ;
262 ::SetControl32BitValue( m_controlHandle
, 0 ) ;
265 ControlRef container
= (ControlRef
) tbar
->GetHandle() ;
266 wxASSERT_MSG( container
!= NULL
, wxT("No valid mac container control") ) ;
267 ::EmbedControl( m_controlHandle
, container
) ;
271 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
272 const wxString
& label
,
273 const wxBitmap
& bmpNormal
,
274 const wxBitmap
& bmpDisabled
,
276 wxObject
*clientData
,
277 const wxString
& shortHelp
,
278 const wxString
& longHelp
)
280 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
281 clientData
, shortHelp
, longHelp
);
284 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
286 return new wxToolBarTool(this, control
);
289 void wxToolBar::Init()
293 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
294 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
297 bool wxToolBar::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
298 long style
, const wxString
& name
)
300 if ( !wxToolBarBase::Create( parent
, id
, pos
, size
, style
) )
306 wxToolBar::~wxToolBar()
308 // we must refresh the frame size when the toolbar is deleted but the frame
309 // is not - otherwise toolbar leaves a hole in the place it used to occupy
312 bool wxToolBar::Realize()
314 if (m_tools
.GetCount() == 0)
317 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
318 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
326 int maxToolWidth
= 0;
327 int maxToolHeight
= 0;
329 // Find the maximum tool width and height
330 wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
333 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
334 wxSize sz
= tool
->GetSize() ;
336 if ( sz
.x
> maxToolWidth
)
337 maxToolWidth
= sz
.x
;
338 if (sz
.y
> maxToolHeight
)
339 maxToolHeight
= sz
.y
;
341 node
= node
->GetNext();
344 node
= m_tools
.GetFirst();
347 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
348 wxSize cursize
= tool
->GetSize() ;
350 // for the moment we just do a single row/column alignement
351 if ( x
+ cursize
.x
> maxWidth
)
352 maxWidth
= x
+ cursize
.x
;
353 if ( y
+ cursize
.y
> maxHeight
)
354 maxHeight
= y
+ cursize
.y
;
356 tool
->SetPosition( wxPoint( x
, y
) ) ;
358 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
367 node
= node
->GetNext();
370 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
372 if ( m_maxRows
== 0 )
374 // if not set yet, only one row
378 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
379 m_maxHeight
= maxHeight
;
383 if ( GetToolsCount() > 0 && m_maxRows
== 0 )
385 // if not set yet, have one column
386 SetRows(GetToolsCount());
389 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
390 m_maxWidth
= maxWidth
;
393 SetSize( maxWidth
, maxHeight
);
398 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
400 m_defaultWidth
= size
.x
+4; m_defaultHeight
= size
.y
+4;
403 // The button size is bigger than the bitmap size
404 wxSize
wxToolBar::GetToolSize() const
406 return wxSize(m_defaultWidth
+ 4, m_defaultHeight
+ 4);
409 void wxToolBar::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool WXUNUSED( mouseStillDown
) )
411 wxToolBarToolsList::Node
*node
;
412 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
414 wxToolBarTool
* tool
= (wxToolBarTool
*) node
->GetData() ;
415 if ( tool
->IsButton() )
417 if( tool
->GetControlHandle() == control
)
419 if ( tool
->CanBeToggled() )
421 tool
->Toggle( GetControl32BitValue( (ControlRef
) control
) ) ;
423 OnLeftClick( tool
->GetId() , tool
-> IsToggled() ) ;
430 void wxToolBar::SetRows(int nRows
)
432 if ( nRows
== m_maxRows
)
434 // avoid resizing the frame uselessly
441 void wxToolBar::MacSuperChangedPosition()
443 wxWindow::MacSuperChangedPosition() ;
447 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
449 wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
452 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData() ;
453 wxRect2DInt
r( tool
->GetPosition() , tool
->GetSize() ) ;
454 if ( r
.Contains( wxPoint( x
, y
) ) )
459 node
= node
->GetNext();
462 return (wxToolBarToolBase
*)NULL
;
465 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
467 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
470 return tool
->GetShortHelp() ;
472 return wxEmptyString
;
475 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
480 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
481 if ( tool
->IsControl() )
483 tool
->GetControl()->Enable( enable
) ;
485 else if ( tool
->IsButton() )
488 UMAActivateControl( (ControlRef
) tool
->GetControlHandle() ) ;
490 UMADeactivateControl( (ControlRef
) tool
->GetControlHandle() ) ;
494 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
499 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
500 if ( tool
->IsButton() )
502 ::SetControl32BitValue( (ControlRef
) tool
->GetControlHandle() , toggle
) ;
506 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
507 wxToolBarToolBase
*tool
)
509 // nothing special to do here - we relayout in Realize() later
515 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
517 wxFAIL_MSG( _T("not implemented") );
520 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
522 wxToolBarToolsList::Node
*node
;
523 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
525 wxToolBarToolBase
*tool2
= node
->GetData();
528 // let node point to the next node in the list
529 node
= node
->GetNext();
535 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize() ;
539 // and finally reposition all the controls after this one
541 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
543 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
544 wxPoint pt
= tool2
->GetPosition() ;
546 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
554 tool2
->SetPosition( pt
) ;
560 void wxToolBar::OnPaint(wxPaintEvent
& event
)
563 wxMacPortSetter
helper(&dc
) ;
567 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
568 dc
.YLOG2DEVMAC(h
) , dc
.XLOG2DEVMAC(w
) } ;
570 if( toolbarrect.left < 0 )
571 toolbarrect.left = 0 ;
572 if ( toolbarrect.top < 0 )
573 toolbarrect.top = 0 ;
575 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
580 void wxToolBar::OnMouse( wxMouseEvent
&event
)
582 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
588 MacClientToRootWindow( &x
, &y
) ;
593 WindowRef window
= (WindowRef
) MacGetTopLevelWindowRef() ;
600 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
601 modifiers
|= btnState
;
603 if ( event
.m_shiftDown
)
604 modifiers
|= shiftKey
;
606 if ( event
.m_controlDown
)
607 modifiers
|= controlKey
;
609 if ( event
.m_altDown
)
610 modifiers
|= optionKey
;
612 if ( event
.m_metaDown
)
613 modifiers
|= cmdKey
;
615 controlpart
= ::FindControl( localwhere
, window
, &control
) ;
617 if ( control
&& ::IsControlActive( control
) )
620 controlpart
= ::HandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) -1 ) ;
621 wxTheApp
->s_lastMouseDown
= 0 ;
622 if ( control
&& controlpart
!= kControlNoPart
) // otherwise we will get the event twice
624 MacHandleControlClick((WXWidget
) control
, controlpart
, false /* not down anymore */ ) ;
632 #endif // wxUSE_TOOLBAR