1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/classic/toolbar.cpp
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: The wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/toolbar.h"
22 #include "wx/notebook.h"
23 #include "wx/tabctrl.h"
25 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
27 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
28 EVT_MOUSE_EVENTS( wxToolBar::OnMouse
)
29 EVT_PAINT( wxToolBar::OnPaint
)
32 #include "wx/mac/uma.h"
33 #include "wx/geometry.h"
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 class wxToolBarTool
: public wxToolBarToolBase
41 wxToolBarTool(wxToolBar
*tbar
,
43 const wxString
& label
,
44 const wxBitmap
& bmpNormal
,
45 const wxBitmap
& bmpDisabled
,
48 const wxString
& shortHelp
,
49 const wxString
& longHelp
) ;
51 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
52 : wxToolBarToolBase(tbar
, control
)
59 if ( m_controlHandle
)
60 DisposeControl( m_controlHandle
) ;
63 ControlHandle
GetControlHandle() { return m_controlHandle
; }
64 void SetControlHandle( ControlHandle handle
) { m_controlHandle
= handle
; }
66 void SetSize(const wxSize
& size
) ;
67 void SetPosition( const wxPoint
& position
) ;
68 wxSize
GetSize() const
72 return GetControl()->GetSize() ;
74 else if ( IsButton() )
76 return GetToolBar()->GetToolSize() ;
80 wxSize sz
= GetToolBar()->GetToolSize() ;
86 wxPoint
GetPosition() const
88 return wxPoint(m_x
, m_y
);
93 m_controlHandle
= NULL
;
95 ControlHandle m_controlHandle
;
101 // ============================================================================
103 // ============================================================================
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 void wxToolBarTool::SetSize(const wxSize
& size
)
113 GetControl()->SetSize( size
) ;
117 void wxToolBarTool::SetPosition(const wxPoint
& position
)
126 WindowRef rootwindow
= (WindowRef
) GetToolBar()->MacGetRootWindow() ;
127 GetToolBar()->MacWindowToRootWindow( &x
, &y
) ;
128 int mac_x
= x
+ position
.x
;
129 int mac_y
= y
+ position
.y
;
133 GetControlBounds( m_controlHandle
, &contrlRect
) ;
134 int former_mac_x
= contrlRect
.left
;
135 int former_mac_y
= contrlRect
.top
;
136 wxSize sz
= GetToolBar()->GetToolSize() ;
138 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
141 Rect inval
= { former_mac_y
, former_mac_x
, former_mac_y
+ sz
.y
, former_mac_x
+ sz
.x
} ;
142 InvalWindowRect( rootwindow
, &inval
) ;
144 UMAMoveControl( m_controlHandle
, mac_x
, mac_y
) ;
146 Rect inval
= { mac_y
, mac_x
, mac_y
+ sz
.y
, mac_x
+ sz
.x
} ;
147 InvalWindowRect( rootwindow
, &inval
) ;
151 else if ( IsControl() )
153 GetControl()->Move( position
) ;
157 const short kwxMacToolBarToolDefaultWidth
= 24 ;
158 const short kwxMacToolBarToolDefaultHeight
= 22 ;
159 const short kwxMacToolBarTopMargin
= 2 ;
160 const short kwxMacToolBarLeftMargin
= 2 ;
162 wxToolBarTool::wxToolBarTool(wxToolBar
*tbar
,
164 const wxString
& label
,
165 const wxBitmap
& bmpNormal
,
166 const wxBitmap
& bmpDisabled
,
168 wxObject
*clientData
,
169 const wxString
& shortHelp
,
170 const wxString
& longHelp
)
171 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
172 clientData
, shortHelp
, longHelp
)
176 if (id
== wxID_SEPARATOR
) return;
178 WindowRef window
= (WindowRef
) tbar
->MacGetRootWindow() ;
179 wxSize toolSize
= tbar
->GetToolSize() ;
180 Rect toolrect
= { 0, 0 , toolSize
.y
, toolSize
.x
} ;
182 ControlButtonContentInfo info
;
183 wxMacCreateBitmapButton( &info
, GetNormalBitmap() ) ;
185 SInt16 behaviour
= kControlBehaviorOffsetContents
;
186 if ( CanBeToggled() )
187 behaviour
+= kControlBehaviorToggles
;
189 if ( info
.contentType
!= kControlNoContent
)
191 m_controlHandle
= ::NewControl( window
, &toolrect
, "\p" , false , 0 ,
192 behaviour
+ info
.contentType
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
194 ::SetControlData( m_controlHandle
, kControlButtonPart
, kControlBevelButtonContentTag
, sizeof(info
) , (char*) &info
) ;
198 m_controlHandle
= ::NewControl( window
, &toolrect
, "\p" , false , 0 ,
199 behaviour
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
201 UMAShowControl( m_controlHandle
) ;
204 UMADeactivateControl( m_controlHandle
) ;
206 if ( CanBeToggled() && IsToggled() )
208 ::SetControl32BitValue( m_controlHandle
, 1 ) ;
212 ::SetControl32BitValue( m_controlHandle
, 0 ) ;
215 ControlHandle container
= (ControlHandle
) tbar
->MacGetContainerForEmbedding() ;
216 wxASSERT_MSG( container
!= NULL
, wxT("No valid mac container control") ) ;
217 ::EmbedControl( m_controlHandle
, container
) ;
221 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
222 const wxString
& label
,
223 const wxBitmap
& bmpNormal
,
224 const wxBitmap
& bmpDisabled
,
226 wxObject
*clientData
,
227 const wxString
& shortHelp
,
228 const wxString
& longHelp
)
230 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
231 clientData
, shortHelp
, longHelp
);
234 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
236 return new wxToolBarTool(this, control
);
239 void wxToolBar::Init()
243 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
244 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
247 bool wxToolBar::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
248 long style
, const wxString
& name
)
266 m_windowStyle
= style
;
267 parent
->AddChild(this);
269 m_backgroundColour
= parent
->GetBackgroundColour() ;
270 m_foregroundColour
= parent
->GetForegroundColour() ;
273 m_windowId
= NewControlId();
282 AdjustForParentClientOrigin(x
, y
, wxSIZE_USE_EXISTING
);
290 wxToolBar::~wxToolBar()
292 // we must refresh the frame size when the toolbar is deleted but the frame
293 // is not - otherwise toolbar leaves a hole in the place it used to occupy
296 bool wxToolBar::Realize()
298 if (m_tools
.GetCount() == 0)
301 int x
= m_xMargin
+ kwxMacToolBarLeftMargin
;
302 int y
= m_yMargin
+ kwxMacToolBarTopMargin
;
310 int maxToolWidth
= 0;
311 int maxToolHeight
= 0;
313 // Find the maximum tool width and height
314 wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
317 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
318 wxSize sz
= tool
->GetSize() ;
320 if ( sz
.x
> maxToolWidth
)
321 maxToolWidth
= sz
.x
;
322 if (sz
.y
> maxToolHeight
)
323 maxToolHeight
= sz
.y
;
325 node
= node
->GetNext();
328 node
= m_tools
.GetFirst();
331 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
332 wxSize cursize
= tool
->GetSize() ;
334 // for the moment we just do a single row/column alignement
335 if ( x
+ cursize
.x
> maxWidth
)
336 maxWidth
= x
+ cursize
.x
;
337 if ( y
+ cursize
.y
> maxHeight
)
338 maxHeight
= y
+ cursize
.y
;
340 tool
->SetPosition( wxPoint( x
, y
) ) ;
342 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
351 node
= node
->GetNext();
354 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
356 if ( m_maxRows
== 0 )
358 // if not set yet, only one row
362 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
363 m_maxHeight
= maxHeight
;
367 if ( GetToolsCount() > 0 && m_maxRows
== 0 )
369 // if not set yet, have one column
370 SetRows(GetToolsCount());
373 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
374 m_maxWidth
= maxWidth
;
377 SetSize(maxWidth
, maxHeight
);
378 InvalidateBestSize();
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
, bool WXUNUSED( mouseStillDown
) )
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( (WXWidget
) 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
496 InvalidateBestSize();
501 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
503 wxFAIL_MSG( _T("not implemented") );
506 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
508 wxToolBarToolsList::Node
*node
;
509 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
511 wxToolBarToolBase
*tool2
= node
->GetData();
514 // let node point to the next node in the list
515 node
= node
->GetNext();
521 wxSize sz
= ((wxToolBarTool
*)tool
)->GetSize() ;
525 // and finally reposition all the controls after this one
527 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
529 wxToolBarTool
*tool2
= (wxToolBarTool
*) node
->GetData();
530 wxPoint pt
= tool2
->GetPosition() ;
532 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
540 tool2
->SetPosition( pt
) ;
543 InvalidateBestSize();
547 void wxToolBar::OnPaint(wxPaintEvent
& event
)
550 wxMacPortSetter
helper(&dc
) ;
552 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
553 dc
.YLOG2DEVMAC(m_height
) , dc
.XLOG2DEVMAC(m_width
) } ;
554 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
556 wxToolBarToolsList::Node
*node
;
557 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
559 wxToolBarTool
* tool
= (wxToolBarTool
*) node
->GetData() ;
560 if ( tool
->IsButton() )
562 UMADrawControl( tool
->GetControlHandle() ) ;
568 void wxToolBar::OnMouse( wxMouseEvent
&event
)
570 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
576 MacClientToRootWindow( &x
, &y
) ;
578 ControlHandle control
;
581 WindowRef window
= (WindowRef
) MacGetRootWindow() ;
588 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
589 modifiers
|= btnState
;
591 if ( event
.m_shiftDown
)
592 modifiers
|= shiftKey
;
594 if ( event
.m_controlDown
)
595 modifiers
|= controlKey
;
597 if ( event
.m_altDown
)
598 modifiers
|= optionKey
;
600 if ( event
.m_metaDown
)
601 modifiers
|= cmdKey
;
603 controlpart
= ::FindControl( localwhere
, window
, &control
) ;
605 if ( control
&& ::IsControlActive( control
) )
608 controlpart
= ::HandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) -1 ) ;
609 wxTheApp
->s_lastMouseDown
= 0 ;
610 if ( control
&& controlpart
!= kControlNoPart
) // otherwise we will get the event twice
612 MacHandleControlClick( (WXWidget
) control
, controlpart
, false /* not down anymore */ ) ;
620 #endif // wxUSE_TOOLBAR