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
) ;
69 wxSize
GetSize() const
73 return GetControl()->GetSize() ;
75 else if ( IsButton() )
77 return GetToolBar()->GetToolSize() ;
81 wxSize sz
= GetToolBar()->GetToolSize() ;
87 wxPoint
GetPosition() const
89 return wxPoint(m_x
, m_y
);
94 m_controlHandle
= NULL
;
96 ControlRef m_controlHandle
;
102 static const EventTypeSpec eventList
[] =
104 { kEventClassControl
, kEventControlHit
} ,
107 static pascal OSStatus
wxMacToolBarToolControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
109 OSStatus result
= eventNotHandledErr
;
111 wxMacCarbonEvent
cEvent( event
) ;
113 ControlRef controlRef
;
115 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
) ;
117 switch( GetEventKind( event
) )
119 case kEventControlHit
:
121 wxToolBarTool
* tbartool
= (wxToolBarTool
*)data
;
122 if ( tbartool
->CanBeToggled() )
124 tbartool
->Toggle( GetControl32BitValue( (ControlRef
) tbartool
->GetControlHandle() ) ) ;
126 ((wxToolBar
*)tbartool
->GetToolBar())->OnLeftClick( tbartool
->GetId() , tbartool
-> IsToggled() ) ;
137 pascal OSStatus
wxMacToolBarToolEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
139 OSStatus result
= eventNotHandledErr
;
141 switch ( GetEventClass( event
) )
143 case kEventClassControl
:
144 result
= wxMacToolBarToolControlEventHandler( handler
, event
, data
) ;
152 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler
)
154 // ============================================================================
156 // ============================================================================
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
162 void wxToolBarTool::SetSize(const wxSize
& size
)
166 GetControl()->SetSize( size
) ;
170 void wxToolBarTool::SetPosition(const wxPoint
& position
)
179 int mac_x
= position
.x
;
180 int mac_y
= position
.y
;
181 #if !TARGET_API_MAC_OSX
182 WindowRef rootwindow
= (WindowRef
) GetToolBar()->MacGetTopLevelWindowRef() ;
183 GetToolBar()->MacWindowToRootWindow( &x
, &y
) ;
188 GetControlBounds( m_controlHandle
, &contrlRect
) ;
189 int former_mac_x
= contrlRect
.left
;
190 int former_mac_y
= contrlRect
.top
;
191 wxSize sz
= GetToolBar()->GetToolSize() ;
193 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
195 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
) ;
198 else if ( IsControl() )
200 GetControl()->Move( position
) ;
204 const short kwxMacToolBarToolDefaultWidth
= 24 ;
205 const short kwxMacToolBarToolDefaultHeight
= 22 ;
206 const short kwxMacToolBarTopMargin
= 2 ;
207 const short kwxMacToolBarLeftMargin
= 2 ;
209 wxToolBarTool::wxToolBarTool(wxToolBar
*tbar
,
211 const wxString
& label
,
212 const wxBitmap
& bmpNormal
,
213 const wxBitmap
& bmpDisabled
,
215 wxObject
*clientData
,
216 const wxString
& shortHelp
,
217 const wxString
& longHelp
)
218 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
219 clientData
, shortHelp
, longHelp
)
223 if (id
== wxID_SEPARATOR
) return;
225 WindowRef window
= (WindowRef
) tbar
->MacGetTopLevelWindowRef() ;
226 wxSize toolSize
= tbar
->GetToolSize() ;
227 Rect toolrect
= { 0, 0 , toolSize
.y
, toolSize
.x
} ;
229 ControlButtonContentInfo info
;
230 wxMacCreateBitmapButton( &info
, GetNormalBitmap() ) ;
232 SInt16 behaviour
= kControlBehaviorOffsetContents
;
233 if ( CanBeToggled() )
234 behaviour
+= kControlBehaviorToggles
;
236 CreateBevelButtonControl( window
, &toolrect
, CFSTR("") , kControlBevelButtonNormalBevel
, behaviour
, &info
,
237 0 , 0 , 0 , &m_controlHandle
) ;
239 InstallControlEventHandler( (ControlRef
) m_controlHandle
, GetwxMacToolBarToolEventHandlerUPP(),
240 GetEventTypeCount(eventList
), eventList
, this,NULL
);
242 UMAShowControl( m_controlHandle
) ;
244 DisableControl( m_controlHandle
) ;
246 if ( CanBeToggled() && IsToggled() )
247 ::SetControl32BitValue( m_controlHandle
, 1 ) ;
249 ::SetControl32BitValue( m_controlHandle
, 0 ) ;
251 ControlRef container
= (ControlRef
) tbar
->GetHandle() ;
252 wxASSERT_MSG( container
!= NULL
, wxT("No valid mac container control") ) ;
253 ::EmbedControl( m_controlHandle
, container
) ;
257 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
258 const wxString
& label
,
259 const wxBitmap
& bmpNormal
,
260 const wxBitmap
& bmpDisabled
,
262 wxObject
*clientData
,
263 const wxString
& shortHelp
,
264 const wxString
& longHelp
)
266 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
267 clientData
, shortHelp
, longHelp
);
270 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
272 return new wxToolBarTool(this, control
);
275 void wxToolBar::Init()
279 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
280 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
283 bool wxToolBar::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
284 long style
, const wxString
& name
)
286 if ( !wxToolBarBase::Create( parent
, id
, pos
, size
, style
) )
292 wxToolBar::~wxToolBar()
294 // we must refresh the frame size when the toolbar is deleted but the frame
295 // is not - otherwise toolbar leaves a hole in the place it used to occupy
298 bool wxToolBar::Realize()
300 if (m_tools
.GetCount() == 0)
303 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
304 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
312 int maxToolWidth
= 0;
313 int maxToolHeight
= 0;
315 // Find the maximum tool width and height
316 wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
319 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
320 wxSize sz
= tool
->GetSize() ;
322 if ( sz
.x
> maxToolWidth
)
323 maxToolWidth
= sz
.x
;
324 if (sz
.y
> maxToolHeight
)
325 maxToolHeight
= sz
.y
;
327 node
= node
->GetNext();
330 node
= m_tools
.GetFirst();
333 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
334 wxSize cursize
= tool
->GetSize() ;
336 // for the moment we just do a single row/column alignement
337 if ( x
+ cursize
.x
> maxWidth
)
338 maxWidth
= x
+ cursize
.x
;
339 if ( y
+ cursize
.y
> maxHeight
)
340 maxHeight
= y
+ cursize
.y
;
342 tool
->SetPosition( wxPoint( x
, y
) ) ;
344 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
353 node
= node
->GetNext();
356 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
358 if ( m_maxRows
== 0 )
360 // if not set yet, only one row
364 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
365 m_maxHeight
= maxHeight
;
369 if ( GetToolsCount() > 0 && m_maxRows
== 0 )
371 // if not set yet, have one column
372 SetRows(GetToolsCount());
375 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
376 m_maxWidth
= maxWidth
;
379 SetSize( maxWidth
, maxHeight
);
384 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
386 m_defaultWidth
= size
.x
+4; m_defaultHeight
= size
.y
+4;
389 // The button size is bigger than the bitmap size
390 wxSize
wxToolBar::GetToolSize() const
392 return wxSize(m_defaultWidth
+ 4, m_defaultHeight
+ 4);
395 void wxToolBar::SetRows(int nRows
)
397 if ( nRows
== m_maxRows
)
399 // avoid resizing the frame uselessly
406 void wxToolBar::MacSuperChangedPosition()
408 wxWindow::MacSuperChangedPosition() ;
412 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
414 wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
417 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData() ;
418 wxRect2DInt
r( tool
->GetPosition() , tool
->GetSize() ) ;
419 if ( r
.Contains( wxPoint( x
, y
) ) )
424 node
= node
->GetNext();
427 return (wxToolBarToolBase
*)NULL
;
430 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
432 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
435 return tool
->GetShortHelp() ;
437 return wxEmptyString
;
440 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
445 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
446 if ( tool
->IsControl() )
448 tool
->GetControl()->Enable( enable
) ;
450 else if ( tool
->IsButton() )
453 UMAActivateControl( (ControlRef
) tool
->GetControlHandle() ) ;
455 UMADeactivateControl( (ControlRef
) tool
->GetControlHandle() ) ;
459 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
464 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
465 if ( tool
->IsButton() )
467 ::SetControl32BitValue( (ControlRef
) tool
->GetControlHandle() , toggle
) ;
471 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
472 wxToolBarToolBase
*tool
)
474 // nothing special to do here - we relayout in Realize() later
480 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
482 wxFAIL_MSG( _T("not implemented") );
485 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
487 wxToolBarToolsList::Node
*node
;
488 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
490 wxToolBarToolBase
*tool2
= node
->GetData();
493 // let node point to the next node in the list
494 node
= node
->GetNext();
500 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize() ;
504 // and finally reposition all the controls after this one
506 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
508 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
509 wxPoint pt
= tool2
->GetPosition() ;
511 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
519 tool2
->SetPosition( pt
) ;
525 void wxToolBar::OnPaint(wxPaintEvent
& event
)
528 wxMacPortSetter
helper(&dc
) ;
532 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
533 dc
.YLOG2DEVMAC(h
) , dc
.XLOG2DEVMAC(w
) } ;
535 if( toolbarrect.left < 0 )
536 toolbarrect.left = 0 ;
537 if ( toolbarrect.top < 0 )
538 toolbarrect.top = 0 ;
540 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
545 #endif // wxUSE_TOOLBAR