1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomory
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "toolbar.h"
19 #include "wx/toolbar.h"
20 #include "wx/notebook.h"
21 #include "wx/tabctrl.h"
22 #include "wx/bitmap.h"
24 #if !USE_SHARED_LIBRARY
25 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxToolBarBase
)
27 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
28 EVT_MOUSE_EVENTS( wxToolBar::OnMouse
)
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 ControlHandle
GetControlHandle() { return m_controlHandle
; }
65 void SetControlHandle( ControlHandle 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 ControlHandle m_controlHandle
;
102 // ============================================================================
104 // ============================================================================
106 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
110 void wxToolBarTool::SetSize(const wxSize
& size
)
114 GetControl()->SetSize( size
) ;
118 void wxToolBarTool::SetPosition(const wxPoint
& position
)
127 WindowRef rootwindow
= (WindowRef
) GetToolBar()->MacGetRootWindow() ;
128 GetToolBar()->MacWindowToRootWindow( &x
, &y
) ;
129 int mac_x
= x
+ position
.x
;
130 int mac_y
= y
+ position
.y
;
134 GetControlBounds( m_controlHandle
, &contrlRect
) ;
135 int former_mac_x
= contrlRect
.left
;
136 int former_mac_y
= contrlRect
.top
;
137 wxSize sz
= GetToolBar()->GetToolSize() ;
139 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
142 Rect inval
= { former_mac_y
, former_mac_x
, former_mac_y
+ sz
.y
, former_mac_x
+ sz
.x
} ;
143 InvalWindowRect( rootwindow
, &inval
) ;
145 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
) ;
147 Rect inval
= { mac_y
, mac_x
, mac_y
+ sz
.y
, mac_x
+ sz
.x
} ;
148 InvalWindowRect( rootwindow
, &inval
) ;
152 else if ( IsControl() )
154 GetControl()->Move( position
) ;
158 const short kwxMacToolBarToolDefaultWidth
= 24 ;
159 const short kwxMacToolBarToolDefaultHeight
= 22 ;
160 const short kwxMacToolBarTopMargin
= 2 ;
161 const short kwxMacToolBarLeftMargin
= 2 ;
163 wxToolBarTool::wxToolBarTool(wxToolBar
*tbar
,
165 const wxString
& label
,
166 const wxBitmap
& bmpNormal
,
167 const wxBitmap
& bmpDisabled
,
169 wxObject
*clientData
,
170 const wxString
& shortHelp
,
171 const wxString
& longHelp
)
172 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
173 clientData
, shortHelp
, longHelp
)
177 WindowRef window
= (WindowRef
) tbar
->MacGetRootWindow() ;
178 wxSize toolSize
= tbar
->GetToolSize() ;
179 Rect toolrect
= { 0, 0 , toolSize
.y
, toolSize
.x
} ;
181 ControlButtonContentInfo info
;
182 wxMacCreateBitmapButton( &info
, GetNormalBitmap() ) ;
184 SInt16 behaviour
= kControlBehaviorOffsetContents
;
185 if ( CanBeToggled() )
186 behaviour
+= kControlBehaviorToggles
;
188 if ( info
.contentType
!= kControlNoContent
)
190 m_controlHandle
= ::NewControl( window
, &toolrect
, "\p" , false , 0 ,
191 behaviour
+ info
.contentType
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
193 ::SetControlData( m_controlHandle
, kControlButtonPart
, kControlBevelButtonContentTag
, sizeof(info
) , (char*) &info
) ;
197 m_controlHandle
= ::NewControl( window
, &toolrect
, "\p" , false , 0 ,
198 behaviour
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
200 UMAShowControl( m_controlHandle
) ;
203 UMADeactivateControl( m_controlHandle
) ;
205 if ( CanBeToggled() && IsToggled() )
207 ::SetControl32BitValue( m_controlHandle
, 1 ) ;
211 ::SetControl32BitValue( m_controlHandle
, 0 ) ;
214 ControlHandle container
= (ControlHandle
) tbar
->MacGetContainerForEmbedding() ;
215 wxASSERT_MSG( container
!= NULL
, wxT("No valid mac container control") ) ;
216 ::EmbedControl( m_controlHandle
, container
) ;
220 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
221 const wxString
& label
,
222 const wxBitmap
& bmpNormal
,
223 const wxBitmap
& bmpDisabled
,
225 wxObject
*clientData
,
226 const wxString
& shortHelp
,
227 const wxString
& longHelp
)
229 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
230 clientData
, shortHelp
, longHelp
);
233 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
235 return new wxToolBarTool(this, control
);
238 void wxToolBar::Init()
242 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
243 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
246 bool wxToolBar::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
247 long style
, const wxString
& name
)
265 m_windowStyle
= style
;
266 parent
->AddChild(this);
268 m_backgroundColour
= parent
->GetBackgroundColour() ;
269 m_foregroundColour
= parent
->GetForegroundColour() ;
272 m_windowId
= NewControlId();
281 AdjustForParentClientOrigin(x
, y
, wxSIZE_USE_EXISTING
);
289 wxToolBar::~wxToolBar()
291 // we must refresh the frame size when the toolbar is deleted but the frame
292 // is not - otherwise toolbar leaves a hole in the place it used to occupy
295 bool wxToolBar::Realize()
297 if (m_tools
.GetCount() == 0)
300 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
301 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
309 int maxToolWidth
= 0;
310 int maxToolHeight
= 0;
312 // Find the maximum tool width and height
313 wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
316 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
317 wxSize sz
= tool
->GetSize() ;
319 if ( sz
.x
> maxToolWidth
)
320 maxToolWidth
= sz
.x
;
321 if (sz
.y
> maxToolHeight
)
322 maxToolHeight
= sz
.y
;
324 node
= node
->GetNext();
327 int separatorSize
= GetToolSize().x
/ 4 ;
329 node
= m_tools
.GetFirst();
332 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
333 wxSize cursize
= tool
->GetSize() ;
335 // for the moment we just do a single row/column alignement
336 if ( x
+ cursize
.x
> maxWidth
)
337 maxWidth
= x
+ cursize
.x
;
338 if ( y
+ cursize
.y
> maxHeight
)
339 maxHeight
= y
+ cursize
.y
;
341 tool
->SetPosition( wxPoint( x
, y
) ) ;
343 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
352 node
= node
->GetNext();
355 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
357 if ( m_maxRows
== 0 )
359 // if not set yet, only one row
363 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
364 m_maxHeight
= maxHeight
;
368 if ( GetToolsCount() > 0 && m_maxRows
== 0 )
370 // if not set yet, have one column
371 SetRows(GetToolsCount());
374 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
375 m_maxWidth
= maxWidth
;
378 SetSize(maxWidth
, maxHeight
);
383 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
385 m_defaultWidth
= size
.x
+4; m_defaultHeight
= size
.y
+4;
388 // The button size is bigger than the bitmap size
389 wxSize
wxToolBar::GetToolSize() const
391 return wxSize(m_defaultWidth
+ 4, m_defaultHeight
+ 4);
394 void wxToolBar::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
)
396 wxToolBarToolsList::Node
*node
;
397 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
399 wxToolBarTool
* tool
= (wxToolBarTool
*) node
->GetData() ;
400 if ( tool
->IsButton() )
402 if( tool
->GetControlHandle() == control
)
404 if ( tool
->CanBeToggled() )
406 tool
->Toggle( GetControl32BitValue( (ControlHandle
) control
) ) ;
408 OnLeftClick( tool
->GetId() , tool
-> IsToggled() ) ;
415 void wxToolBar::SetRows(int nRows
)
417 if ( nRows
== m_maxRows
)
419 // avoid resizing the frame uselessly
426 void wxToolBar::MacSuperChangedPosition()
428 wxWindow::MacSuperChangedPosition() ;
432 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
434 wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
437 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData() ;
438 wxRect2DInt
r( tool
->GetPosition() , tool
->GetSize() ) ;
439 if ( r
.Contains( wxPoint( x
, y
) ) )
444 node
= node
->GetNext();
447 return (wxToolBarToolBase
*)NULL
;
450 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
452 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
455 return tool
->GetShortHelp() ;
457 return wxEmptyString
;
460 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
465 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
466 if ( tool
->IsControl() )
468 tool
->GetControl()->Enable( enable
) ;
470 else if ( tool
->IsButton() )
473 UMAActivateControl( tool
->GetControlHandle() ) ;
475 UMADeactivateControl( tool
->GetControlHandle() ) ;
479 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
484 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
485 if ( tool
->IsButton() )
487 ::SetControl32BitValue( tool
->GetControlHandle() , toggle
) ;
491 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
492 wxToolBarToolBase
*tool
)
494 // nothing special to do here - we relayout in Realize() later
500 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
502 wxFAIL_MSG( _T("not implemented") );
505 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
507 wxToolBarToolsList::Node
*node
;
508 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
510 wxToolBarToolBase
*tool2
= node
->GetData();
513 // let node point to the next node in the list
514 node
= node
->GetNext();
520 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize() ;
524 // and finally reposition all the controls after this one
526 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
528 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
529 wxPoint pt
= tool2
->GetPosition() ;
531 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
539 tool2
->SetPosition( pt
) ;
545 void wxToolBar::OnPaint(wxPaintEvent
& event
)
548 wxMacPortSetter
helper(&dc
) ;
550 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
551 dc
.YLOG2DEVMAC(m_height
) , dc
.XLOG2DEVMAC(m_width
) } ;
552 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
554 wxToolBarToolsList::Node
*node
;
555 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
557 wxToolBarTool
* tool
= (wxToolBarTool
*) node
->GetData() ;
558 if ( tool
->IsButton() )
560 UMADrawControl( tool
->GetControlHandle() ) ;
566 void wxToolBar::OnMouse( wxMouseEvent
&event
)
568 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
574 MacClientToRootWindow( &x
, &y
) ;
576 ControlHandle control
;
579 WindowRef window
= (WindowRef
) MacGetRootWindow() ;
586 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
587 modifiers
|= btnState
;
589 if ( event
.m_shiftDown
)
590 modifiers
|= shiftKey
;
592 if ( event
.m_controlDown
)
593 modifiers
|= controlKey
;
595 if ( event
.m_altDown
)
596 modifiers
|= optionKey
;
598 if ( event
.m_metaDown
)
599 modifiers
|= cmdKey
;
601 controlpart
= ::FindControl( localwhere
, window
, &control
) ;
603 if ( control
&& ::IsControlActive( control
) )
606 if ( controlpart
== kControlIndicatorPart
&& !UMAHasAppearance() )
607 controlpart
= ::HandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) NULL
) ;
609 controlpart
= ::HandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) -1 ) ;
610 wxTheApp
->s_lastMouseDown
= 0 ;
611 if ( controlpart
&& ! ( ( UMAHasAppearance() || (controlpart
!= kControlIndicatorPart
) )
612 && (IsKindOf( CLASSINFO( wxScrollBar
) ) ) ) ) // otherwise we will get the event twice
614 MacHandleControlClick( control
, controlpart
) ;
622 #endif // wxUSE_TOOLBAR