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_HORIZONTAL
)
258 x
+= (int)toolSize
.x
;
262 y
+= (int)toolSize
.y
;
268 m_macToolHandles
.Add( NULL
) ;
269 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
271 x
+= (int)toolSize
.x
/ 4;
275 y
+= (int)toolSize
.y
/ 4;
278 if ( toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
- m_x
- localOrigin
.h
> maxWidth
) {
279 maxWidth
= toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
- m_x
- localOrigin
.h
;
281 if (toolbarrect
.top
+ y
+ m_yMargin
+ kwxMacToolBarTopMargin
- m_y
- localOrigin
.v
> maxHeight
) {
282 maxHeight
= toolbarrect
.top
+ y
+ m_yMargin
+ kwxMacToolBarTopMargin
- m_y
- localOrigin
.v
;
287 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
289 if ( m_maxRows
== 0 )
291 // if not set yet, only one row
295 maxHeight
+= toolSize
.y
;
296 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
297 m_maxHeight
= maxHeight
;
301 if ( noButtons
> 0 && m_maxRows
== 0 )
303 // if not set yet, have one column
307 maxWidth
+= toolSize
.x
;
308 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
309 m_maxWidth
= maxWidth
;
312 SetSize(maxWidth
, maxHeight
);
317 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
319 m_defaultWidth
= size
.x
+4; m_defaultHeight
= size
.y
+4;
322 // The button size is bigger than the bitmap size
323 wxSize
wxToolBar::GetToolSize() const
325 return wxSize(m_defaultWidth
+ 4, m_defaultHeight
+ 4);
328 void wxToolBar::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
)
331 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
333 if ( m_macToolHandles
[index
] == (void*) control
)
335 wxToolBarTool
*tool
= (wxToolBarTool
*)m_tools
.Nth( index
)->Data();
336 if ( tool
->CanBeToggled() )
338 tool
->Toggle( GetControlValue( (ControlHandle
) control
) ) ;
340 OnLeftClick( tool
->GetId() , tool
-> IsToggled() ) ;
346 void wxToolBar::SetRows(int nRows
)
348 if ( nRows
== m_maxRows
)
350 // avoid resizing the frame uselessly
357 void wxToolBar::MacSuperChangedPosition()
359 if (m_tools
.Number() > 0)
364 // WindowRef window ;
368 MacWindowToRootWindow( &lx
, &ly
) ;
372 // GetParent()->MacGetPortParams( &localOrigin , &clipRect , &window , &win ) ;
374 Rect toolbarrect
= { localOrigin
.v
,localOrigin
.h
,
375 m_height
+ localOrigin
.v
, m_width
+ localOrigin
.h
} ;
376 ControlFontStyleRec controlstyle
;
378 controlstyle
.flags
= kControlUseFontMask
;
379 controlstyle
.font
= kControlFontSmallSystemFont
;
381 wxNode
*node
= m_tools
.First();
384 wxSize toolSize
= GetToolSize() ;
392 WindowRef rootwindow
= (WindowRef
) MacGetRootWindow() ;
395 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
396 wxBitmapRefData
* bmap
= (wxBitmapRefData
*) ( tool
->GetNormalBitmap().GetRefData()) ;
398 if( !tool
->IsSeparator() )
400 Rect toolrect
= { toolbarrect
.top
+ m_yMargin
+ kwxMacToolBarTopMargin
, toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
, 0 , 0 } ;
401 toolrect
.right
= toolrect
.left
+ toolSize
.x
;
402 toolrect
.bottom
= toolrect
.top
+ toolSize
.y
;
404 ControlHandle m_macToolHandle
= (ControlHandle
) m_macToolHandles
[toolcount
++] ;
408 GetControlBounds( m_macToolHandle
, &contrlRect
) ;
409 int former_mac_x
= contrlRect
.left
;
410 int former_mac_y
= contrlRect
.top
;
411 int mac_x
= toolrect
.left
;
412 int mac_y
= toolrect
.top
;
414 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
417 Rect inval
= { former_mac_y
, former_mac_x
, former_mac_y
+ toolSize
.y
, former_mac_x
+ toolSize
.y
} ;
418 InvalWindowRect( rootwindow
, &inval
) ;
420 UMAMoveControl( m_macToolHandle
, mac_x
, mac_y
) ;
422 Rect inval
= { mac_y
, mac_x
, mac_y
+ toolSize
.y
, mac_x
+ toolSize
.y
} ;
423 InvalWindowRect( rootwindow
, &inval
) ;
428 x
+= (int)toolSize
.x
;
434 x
+= (int)toolSize
.x
/ 4;
436 if ( toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
- m_x
- localOrigin
.h
> maxWidth
)
437 maxWidth
= toolbarrect
.left
+ x
+ kwxMacToolBarLeftMargin
+ m_xMargin
- m_x
- localOrigin
.h
;
438 if (toolbarrect
.top
+ m_yMargin
+ kwxMacToolBarTopMargin
- m_y
- localOrigin
.v
> maxHeight
)
439 maxHeight
= toolbarrect
.top
+ kwxMacToolBarTopMargin
+ m_yMargin
- m_y
- localOrigin
.v
;
445 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
447 if ( m_maxRows
== 0 )
449 // if not set yet, only one row
453 maxHeight
+= toolSize
.y
;
454 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
455 m_maxHeight
= maxHeight
;
459 if ( noButtons
> 0 && m_maxRows
== 0 )
461 // if not set yet, have one column
465 maxWidth
+= toolSize
.x
;
466 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
467 m_maxWidth
= maxWidth
;
470 SetSize(maxWidth
, maxHeight
);
473 wxWindow::MacSuperChangedPosition() ;
476 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
478 MacClientToRootWindow( &x
, &y
) ;
479 Point pt
= { y
,x
} ;
482 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
484 if ( m_macToolHandles
[index
] )
487 GetControlBounds((ControlHandle
) m_macToolHandles
[index
], &bounds
) ;
488 if ( PtInRect( pt
, &bounds
) )
490 return (wxToolBarTool
*) (m_tools
.Nth( index
)->Data() ) ;
495 return (wxToolBarToolBase
*)NULL
;
498 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
500 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
503 return tool
->GetShortHelp() ;
508 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
513 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
514 if ( tool
->m_index
< 0 )
517 ControlHandle control
= (ControlHandle
) m_macToolHandles
[ tool
->m_index
] ;
520 UMAActivateControl( control
) ;
522 UMADeactivateControl( control
) ;
525 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
530 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
531 if ( tool
->m_index
< 0 )
534 ControlHandle control
= (ControlHandle
) m_macToolHandles
[ tool
->m_index
] ;
535 ::SetControlValue( control
, toggle
) ;
538 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
539 wxToolBarToolBase
*tool
)
541 // nothing special to do here - we really create the toolbar buttons in
548 void wxToolBar::DoSetToggle(wxToolBarToolBase
*t
, bool toggle
)
550 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
551 // TODO: set toggle state
554 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
559 void wxToolBar::OnPaint(wxPaintEvent
& event
)
562 wxMacPortSetter
helper(&dc
) ;
564 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
565 dc
.YLOG2DEVMAC(m_height
) , dc
.XLOG2DEVMAC(m_width
) } ;
566 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
569 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
571 if ( m_macToolHandles
[index
] )
573 UMADrawControl( (ControlHandle
) m_macToolHandles
[index
] ) ;
579 void wxToolBar::OnMouse( wxMouseEvent
&event
)
581 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
587 MacClientToRootWindow( &x
, &y
) ;
589 ControlHandle control
;
592 WindowRef window
= (WindowRef
) MacGetRootWindow() ;
599 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
600 modifiers
|= btnState
;
602 if ( event
.m_shiftDown
)
603 modifiers
|= shiftKey
;
605 if ( event
.m_controlDown
)
606 modifiers
|= controlKey
;
608 if ( event
.m_altDown
)
609 modifiers
|= optionKey
;
611 if ( event
.m_metaDown
)
612 modifiers
|= cmdKey
;
614 controlpart
= ::FindControl( localwhere
, window
, &control
) ;
616 if ( control
&& ::IsControlActive( control
) )
619 if ( controlpart
== kControlIndicatorPart
&& !UMAHasAppearance() )
620 controlpart
= ::HandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) NULL
) ;
622 controlpart
= ::HandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) -1 ) ;
623 wxTheApp
->s_lastMouseDown
= 0 ;
624 if ( controlpart
&& ! ( ( UMAHasAppearance() || (controlpart
!= kControlIndicatorPart
) )
625 && (IsKindOf( CLASSINFO( wxScrollBar
) ) ) ) ) // otherwise we will get the event twice
627 MacHandleControlClick( control
, controlpart
) ;
635 #endif // wxUSE_TOOLBAR