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();
206 if( !tool
->IsSeparator() )
208 Rect toolrect
= { toolbarrect
.top
+ y
+ m_yMargin
+ kwxMacToolBarTopMargin
,
209 toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
, 0 , 0 } ;
210 toolrect
.right
= toolrect
.left
+ toolSize
.x
;
211 toolrect
.bottom
= toolrect
.top
+ toolSize
.y
;
213 ControlButtonContentInfo info
;
214 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap() ) ;
215 ControlHandle m_macToolHandle
;
217 SInt16 behaviour
= kControlBehaviorOffsetContents
;
218 if ( tool
->CanBeToggled() )
219 behaviour
+= kControlBehaviorToggles
;
221 if ( info
.contentType
!= kControlNoContent
)
223 m_macToolHandle
= ::NewControl( window
, &toolrect
, "\p" , false , 0 ,
224 behaviour
+ info
.contentType
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
226 ::SetControlData( m_macToolHandle
, kControlButtonPart
, kControlBevelButtonContentTag
, sizeof(info
) , (char*) &info
) ;
230 m_macToolHandle
= ::NewControl( window
, &toolrect
, "\p" , false , 0 ,
231 behaviour
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
233 UMAShowControl( m_macToolHandle
) ;
234 m_macToolHandles
.Add( m_macToolHandle
) ;
235 tool
->m_index
= m_macToolHandles
.Count() -1 ;
236 if ( !tool
->IsEnabled() )
238 UMADeactivateControl( m_macToolHandle
) ;
240 if ( tool
->CanBeToggled() && tool
->IsToggled() )
242 ::SetControl32BitValue( m_macToolHandle
, 1 ) ;
246 ::SetControl32BitValue( m_macToolHandle
, 0 ) ;
249 ::SetControlFontStyle( m_macToolHandle , &controlstyle ) ;
251 ControlHandle container
= (ControlHandle
) GetParent()->MacGetContainerForEmbedding() ;
252 wxASSERT_MSG( container
!= NULL
, "No valid mac container control" ) ;
253 ::EmbedControl( m_macToolHandle
, container
) ;
255 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
257 y
+= (int)toolSize
.y
;
261 x
+= (int)toolSize
.x
;
267 m_macToolHandles
.Add( NULL
) ;
269 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
271 y
+= (int)toolSize
.y
/ 4;
275 x
+= (int)toolSize
.x
/ 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( GetControl32BitValue( (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();
397 if( !tool
->IsSeparator() )
399 Rect toolrect
= { toolbarrect
.top
+ m_yMargin
+ kwxMacToolBarTopMargin
, toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
, 0 , 0 } ;
400 toolrect
.right
= toolrect
.left
+ toolSize
.x
;
401 toolrect
.bottom
= toolrect
.top
+ toolSize
.y
;
403 ControlHandle m_macToolHandle
= (ControlHandle
) m_macToolHandles
[toolcount
++] ;
407 GetControlBounds( m_macToolHandle
, &contrlRect
) ;
408 int former_mac_x
= contrlRect
.left
;
409 int former_mac_y
= contrlRect
.top
;
410 int mac_x
= toolrect
.left
;
411 int mac_y
= toolrect
.top
;
413 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
416 Rect inval
= { former_mac_y
, former_mac_x
, former_mac_y
+ toolSize
.y
, former_mac_x
+ toolSize
.y
} ;
417 InvalWindowRect( rootwindow
, &inval
) ;
419 UMAMoveControl( m_macToolHandle
, mac_x
, mac_y
) ;
421 Rect inval
= { mac_y
, mac_x
, mac_y
+ toolSize
.y
, mac_x
+ toolSize
.y
} ;
422 InvalWindowRect( rootwindow
, &inval
) ;
427 x
+= (int)toolSize
.x
;
433 x
+= (int)toolSize
.x
/ 4;
435 if ( toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
- m_x
- localOrigin
.h
> maxWidth
)
436 maxWidth
= toolbarrect
.left
+ x
+ kwxMacToolBarLeftMargin
+ m_xMargin
- m_x
- localOrigin
.h
;
437 if (toolbarrect
.top
+ m_yMargin
+ kwxMacToolBarTopMargin
- m_y
- localOrigin
.v
> maxHeight
)
438 maxHeight
= toolbarrect
.top
+ kwxMacToolBarTopMargin
+ m_yMargin
- m_y
- localOrigin
.v
;
444 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
446 if ( m_maxRows
== 0 )
448 // if not set yet, only one row
452 maxHeight
+= toolSize
.y
;
453 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
454 m_maxHeight
= maxHeight
;
458 if ( noButtons
> 0 && m_maxRows
== 0 )
460 // if not set yet, have one column
464 maxWidth
+= toolSize
.x
;
465 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
466 m_maxWidth
= maxWidth
;
469 SetSize(maxWidth
, maxHeight
);
472 wxWindow::MacSuperChangedPosition() ;
475 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
477 MacClientToRootWindow( &x
, &y
) ;
478 Point pt
= { y
,x
} ;
481 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
483 if ( m_macToolHandles
[index
] )
486 GetControlBounds((ControlHandle
) m_macToolHandles
[index
], &bounds
) ;
487 if ( PtInRect( pt
, &bounds
) )
489 return (wxToolBarTool
*) (m_tools
.Nth( index
)->Data() ) ;
494 return (wxToolBarToolBase
*)NULL
;
497 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
499 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
502 return tool
->GetShortHelp() ;
507 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
512 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
513 if ( tool
->m_index
< 0 )
516 ControlHandle control
= (ControlHandle
) m_macToolHandles
[ tool
->m_index
] ;
519 UMAActivateControl( control
) ;
521 UMADeactivateControl( control
) ;
524 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
529 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
530 if ( tool
->m_index
< 0 )
533 ControlHandle control
= (ControlHandle
) m_macToolHandles
[ tool
->m_index
] ;
534 ::SetControl32BitValue( control
, toggle
) ;
537 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
538 wxToolBarToolBase
*tool
)
540 // nothing special to do here - we really create the toolbar buttons in
547 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
549 wxFAIL_MSG( _T("not implemented") );
552 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*WXUNUSED(tool
))
554 wxFAIL_MSG( _T("not implemented") );
558 void wxToolBar::OnPaint(wxPaintEvent
& event
)
561 wxMacPortSetter
helper(&dc
) ;
563 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
564 dc
.YLOG2DEVMAC(m_height
) , dc
.XLOG2DEVMAC(m_width
) } ;
565 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
568 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
570 if ( m_macToolHandles
[index
] )
572 UMADrawControl( (ControlHandle
) m_macToolHandles
[index
] ) ;
578 void wxToolBar::OnMouse( wxMouseEvent
&event
)
580 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
586 MacClientToRootWindow( &x
, &y
) ;
588 ControlHandle control
;
591 WindowRef window
= (WindowRef
) MacGetRootWindow() ;
598 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
599 modifiers
|= btnState
;
601 if ( event
.m_shiftDown
)
602 modifiers
|= shiftKey
;
604 if ( event
.m_controlDown
)
605 modifiers
|= controlKey
;
607 if ( event
.m_altDown
)
608 modifiers
|= optionKey
;
610 if ( event
.m_metaDown
)
611 modifiers
|= cmdKey
;
613 controlpart
= ::FindControl( localwhere
, window
, &control
) ;
615 if ( control
&& ::IsControlActive( control
) )
618 if ( controlpart
== kControlIndicatorPart
&& !UMAHasAppearance() )
619 controlpart
= ::HandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) NULL
) ;
621 controlpart
= ::HandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) -1 ) ;
622 wxTheApp
->s_lastMouseDown
= 0 ;
623 if ( controlpart
&& ! ( ( UMAHasAppearance() || (controlpart
!= kControlIndicatorPart
) )
624 && (IsKindOf( CLASSINFO( wxScrollBar
) ) ) ) ) // otherwise we will get the event twice
626 MacHandleControlClick( control
, controlpart
) ;
634 #endif // wxUSE_TOOLBAR