1 /////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHORy
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"
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
)
51 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
52 clientData
, shortHelp
, longHelp
)
58 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
59 : wxToolBarToolBase(tbar
, control
)
65 // set/get the number of separators which we use to cover the space used by
66 // a control in the toolbar
67 void SetSeparatorsCount(size_t count
) { m_nSepCount
= count
; }
68 size_t GetSeparatorsCount() const { return m_nSepCount
; }
76 // ============================================================================
78 // ============================================================================
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 const short kwxMacToolBarToolDefaultWidth
= 24 ;
85 const short kwxMacToolBarToolDefaultHeight
= 22 ;
86 const short kwxMacToolBarTopMargin
= 2 ;
87 const short kwxMacToolBarLeftMargin
= 2 ;
90 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
91 const wxString
& label
,
92 const wxBitmap
& bmpNormal
,
93 const wxBitmap
& bmpDisabled
,
96 const wxString
& shortHelp
,
97 const wxString
& longHelp
)
99 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
100 clientData
, shortHelp
, longHelp
);
103 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
105 return new wxToolBarTool(this, control
);
108 void wxToolBar::Init()
112 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
113 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
116 bool wxToolBar::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
117 long style
, const wxString
& name
)
136 m_windowStyle
= style
;
137 parent
->AddChild(this);
139 m_backgroundColour
= parent
->GetBackgroundColour() ;
140 m_foregroundColour
= parent
->GetForegroundColour() ;
143 m_windowId
= NewControlId();
152 AdjustForParentClientOrigin(x
, y
, wxSIZE_USE_EXISTING
);
160 wxToolBar::~wxToolBar()
162 // we must refresh the frame size when the toolbar is deleted but the frame
163 // is not - otherwise toolbar leaves a hole in the place it used to occupy
166 bool wxToolBar::Realize()
168 if (m_tools
.Number() == 0)
173 WindowRef window
= (WindowRef
) MacGetRootWindow() ;
178 MacWindowToRootWindow( &lx
, &ly
) ;
182 // GetParent()->MacGetPortParams( &localOrigin , &clipRect , &window , &win ) ;
184 Rect toolbarrect
= { localOrigin
.v
,localOrigin
.h
,
185 m_height
+ localOrigin
.v
, m_width
+ localOrigin
.h
} ;
186 ControlFontStyleRec controlstyle
;
188 controlstyle
.flags
= kControlUseFontMask
;
189 controlstyle
.font
= kControlFontSmallSystemFont
;
191 wxNode
*node
= m_tools
.First();
195 wxSize toolSize
= GetToolSize() ;
204 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
205 wxBitmapRefData
* bmap
= (wxBitmapRefData
*) ( tool
->GetNormalBitmap().GetRefData()) ;
207 if( !tool
->IsSeparator() )
209 Rect toolrect
= { toolbarrect
.top
+ y
+ m_yMargin
+ kwxMacToolBarTopMargin
,
210 toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
, 0 , 0 } ;
211 toolrect
.right
= toolrect
.left
+ toolSize
.x
;
212 toolrect
.bottom
= toolrect
.top
+ toolSize
.y
;
214 ControlButtonContentInfo info
;
215 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap() ) ;
216 ControlHandle m_macToolHandle
;
218 SInt16 behaviour
= kControlBehaviorOffsetContents
;
219 if ( tool
->CanBeToggled() )
220 behaviour
+= kControlBehaviorToggles
;
222 if ( info
.contentType
!= kControlNoContent
)
224 m_macToolHandle
= ::NewControl( window
, &toolrect
, "\p" , false , 0 ,
225 behaviour
+ info
.contentType
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
227 ::SetControlData( m_macToolHandle
, kControlButtonPart
, kControlBevelButtonContentTag
, sizeof(info
) , (char*) &info
) ;
231 m_macToolHandle
= ::NewControl( window
, &toolrect
, "\p" , false , 0 ,
232 behaviour
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
234 UMAShowControl( m_macToolHandle
) ;
235 m_macToolHandles
.Add( m_macToolHandle
) ;
236 tool
->m_index
= m_macToolHandles
.Count() -1 ;
237 if ( !tool
->IsEnabled() )
239 UMADeactivateControl( m_macToolHandle
) ;
241 if ( tool
->CanBeToggled() && tool
->IsToggled() )
243 ::SetControlValue( m_macToolHandle
, 1 ) ;
247 ::SetControlValue( m_macToolHandle
, 0 ) ;
250 ::SetControlFontStyle( m_macToolHandle , &controlstyle ) ;
252 ControlHandle container
= (ControlHandle
) GetParent()->MacGetContainerForEmbedding() ;
253 wxASSERT_MSG( container
!= NULL
, "No valid mac container control" ) ;
254 ::EmbedControl( m_macToolHandle
, container
) ;
256 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
258 y
+= (int)toolSize
.y
;
262 x
+= (int)toolSize
.x
;
268 m_macToolHandles
.Add( NULL
) ;
270 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
272 y
+= (int)toolSize
.y
/ 4;
276 x
+= (int)toolSize
.x
/ 4;
279 if ( toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
- m_x
- localOrigin
.h
> maxWidth
) {
280 maxWidth
= toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
- m_x
- localOrigin
.h
;
282 if (toolbarrect
.top
+ y
+ m_yMargin
+ kwxMacToolBarTopMargin
- m_y
- localOrigin
.v
> maxHeight
) {
283 maxHeight
= toolbarrect
.top
+ y
+ m_yMargin
+ kwxMacToolBarTopMargin
- m_y
- localOrigin
.v
;
288 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
290 if ( m_maxRows
== 0 )
292 // if not set yet, only one row
296 maxHeight
+= toolSize
.y
;
297 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
298 m_maxHeight
= maxHeight
;
302 if ( noButtons
> 0 && m_maxRows
== 0 )
304 // if not set yet, have one column
308 maxWidth
+= toolSize
.x
;
309 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
310 m_maxWidth
= maxWidth
;
313 SetSize(maxWidth
, maxHeight
);
318 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
320 m_defaultWidth
= size
.x
+4; m_defaultHeight
= size
.y
+4;
323 // The button size is bigger than the bitmap size
324 wxSize
wxToolBar::GetToolSize() const
326 return wxSize(m_defaultWidth
+ 4, m_defaultHeight
+ 4);
329 void wxToolBar::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
)
332 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
334 if ( m_macToolHandles
[index
] == (void*) control
)
336 wxToolBarTool
*tool
= (wxToolBarTool
*)m_tools
.Nth( index
)->Data();
337 if ( tool
->CanBeToggled() )
339 tool
->Toggle( GetControlValue( (ControlHandle
) control
) ) ;
341 OnLeftClick( tool
->GetId() , tool
-> IsToggled() ) ;
347 void wxToolBar::SetRows(int nRows
)
349 if ( nRows
== m_maxRows
)
351 // avoid resizing the frame uselessly
358 void wxToolBar::MacSuperChangedPosition()
360 if (m_tools
.Number() > 0)
365 // WindowRef window ;
369 MacWindowToRootWindow( &lx
, &ly
) ;
373 // GetParent()->MacGetPortParams( &localOrigin , &clipRect , &window , &win ) ;
375 Rect toolbarrect
= { localOrigin
.v
,localOrigin
.h
,
376 m_height
+ localOrigin
.v
, m_width
+ localOrigin
.h
} ;
377 ControlFontStyleRec controlstyle
;
379 controlstyle
.flags
= kControlUseFontMask
;
380 controlstyle
.font
= kControlFontSmallSystemFont
;
382 wxNode
*node
= m_tools
.First();
385 wxSize toolSize
= GetToolSize() ;
393 WindowRef rootwindow
= (WindowRef
) MacGetRootWindow() ;
396 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
397 wxBitmapRefData
* bmap
= (wxBitmapRefData
*) ( tool
->GetNormalBitmap().GetRefData()) ;
399 if( !tool
->IsSeparator() )
401 Rect toolrect
= { toolbarrect
.top
+ m_yMargin
+ kwxMacToolBarTopMargin
, toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
, 0 , 0 } ;
402 toolrect
.right
= toolrect
.left
+ toolSize
.x
;
403 toolrect
.bottom
= toolrect
.top
+ toolSize
.y
;
405 ControlHandle m_macToolHandle
= (ControlHandle
) m_macToolHandles
[toolcount
++] ;
409 GetControlBounds( m_macToolHandle
, &contrlRect
) ;
410 int former_mac_x
= contrlRect
.left
;
411 int former_mac_y
= contrlRect
.top
;
412 int mac_x
= toolrect
.left
;
413 int mac_y
= toolrect
.top
;
415 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
418 Rect inval
= { former_mac_y
, former_mac_x
, former_mac_y
+ toolSize
.y
, former_mac_x
+ toolSize
.y
} ;
419 InvalWindowRect( rootwindow
, &inval
) ;
421 UMAMoveControl( m_macToolHandle
, mac_x
, mac_y
) ;
423 Rect inval
= { mac_y
, mac_x
, mac_y
+ toolSize
.y
, mac_x
+ toolSize
.y
} ;
424 InvalWindowRect( rootwindow
, &inval
) ;
429 x
+= (int)toolSize
.x
;
435 x
+= (int)toolSize
.x
/ 4;
437 if ( toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
- m_x
- localOrigin
.h
> maxWidth
)
438 maxWidth
= toolbarrect
.left
+ x
+ kwxMacToolBarLeftMargin
+ m_xMargin
- m_x
- localOrigin
.h
;
439 if (toolbarrect
.top
+ m_yMargin
+ kwxMacToolBarTopMargin
- m_y
- localOrigin
.v
> maxHeight
)
440 maxHeight
= toolbarrect
.top
+ kwxMacToolBarTopMargin
+ m_yMargin
- m_y
- localOrigin
.v
;
446 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
448 if ( m_maxRows
== 0 )
450 // if not set yet, only one row
454 maxHeight
+= toolSize
.y
;
455 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
456 m_maxHeight
= maxHeight
;
460 if ( noButtons
> 0 && m_maxRows
== 0 )
462 // if not set yet, have one column
466 maxWidth
+= toolSize
.x
;
467 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
468 m_maxWidth
= maxWidth
;
471 SetSize(maxWidth
, maxHeight
);
474 wxWindow::MacSuperChangedPosition() ;
477 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
479 MacClientToRootWindow( &x
, &y
) ;
480 Point pt
= { y
,x
} ;
483 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
485 if ( m_macToolHandles
[index
] )
488 GetControlBounds((ControlHandle
) m_macToolHandles
[index
], &bounds
) ;
489 if ( PtInRect( pt
, &bounds
) )
491 return (wxToolBarTool
*) (m_tools
.Nth( index
)->Data() ) ;
496 return (wxToolBarToolBase
*)NULL
;
499 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
501 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
504 return tool
->GetShortHelp() ;
509 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
514 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
515 if ( tool
->m_index
< 0 )
518 ControlHandle control
= (ControlHandle
) m_macToolHandles
[ tool
->m_index
] ;
521 UMAActivateControl( control
) ;
523 UMADeactivateControl( control
) ;
526 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
531 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
532 if ( tool
->m_index
< 0 )
535 ControlHandle control
= (ControlHandle
) m_macToolHandles
[ tool
->m_index
] ;
536 ::SetControlValue( control
, toggle
) ;
539 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
540 wxToolBarToolBase
*tool
)
542 // nothing special to do here - we really create the toolbar buttons in
549 void wxToolBar::DoSetToggle(wxToolBarToolBase
*t
, bool toggle
)
551 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
552 // TODO: set toggle state
555 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
560 void wxToolBar::OnPaint(wxPaintEvent
& event
)
563 wxMacPortSetter
helper(&dc
) ;
565 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
566 dc
.YLOG2DEVMAC(m_height
) , dc
.XLOG2DEVMAC(m_width
) } ;
567 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
570 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
572 if ( m_macToolHandles
[index
] )
574 UMADrawControl( (ControlHandle
) m_macToolHandles
[index
] ) ;
580 void wxToolBar::OnMouse( wxMouseEvent
&event
)
582 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
588 MacClientToRootWindow( &x
, &y
) ;
590 ControlHandle control
;
593 WindowRef window
= (WindowRef
) MacGetRootWindow() ;
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 if ( controlpart
== kControlIndicatorPart
&& !UMAHasAppearance() )
621 controlpart
= ::HandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) NULL
) ;
623 controlpart
= ::HandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) -1 ) ;
624 wxTheApp
->s_lastMouseDown
= 0 ;
625 if ( controlpart
&& ! ( ( UMAHasAppearance() || (controlpart
!= kControlIndicatorPart
) )
626 && (IsKindOf( CLASSINFO( wxScrollBar
) ) ) ) ) // otherwise we will get the event twice
628 MacHandleControlClick( control
, controlpart
) ;
636 #endif // wxUSE_TOOLBAR