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 wxBitmap
& bitmap1
,
45 const wxBitmap
& bitmap2
,
48 const wxString
& shortHelpString
,
49 const wxString
& longHelpString
)
50 : wxToolBarToolBase(tbar
, id
, bitmap1
, bitmap2
, toggle
,
51 clientData
, shortHelpString
, longHelpString
)
57 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
58 : wxToolBarToolBase(tbar
, control
)
64 // set/get the number of separators which we use to cover the space used by
65 // a control in the toolbar
66 void SetSeparatorsCount(size_t count
) { m_nSepCount
= count
; }
67 size_t GetSeparatorsCount() const { return m_nSepCount
; }
75 // ============================================================================
77 // ============================================================================
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 const short kwxMacToolBarToolDefaultWidth
= 24 ;
84 const short kwxMacToolBarToolDefaultHeight
= 22 ;
85 const short kwxMacToolBarTopMargin
= 2 ;
86 const short kwxMacToolBarLeftMargin
= 2 ;
89 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
90 const wxBitmap
& bitmap1
,
91 const wxBitmap
& bitmap2
,
94 const wxString
& shortHelpString
,
95 const wxString
& longHelpString
)
97 return new wxToolBarTool(this, id
, bitmap1
, bitmap2
, toggle
,
98 clientData
, shortHelpString
, longHelpString
);
101 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
103 return new wxToolBarTool(this, control
);
106 void wxToolBar::Init()
110 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
111 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
114 bool wxToolBar::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
115 long style
, const wxString
& name
)
134 m_windowStyle
= style
;
135 parent
->AddChild(this);
137 m_backgroundColour
= parent
->GetBackgroundColour() ;
138 m_foregroundColour
= parent
->GetForegroundColour() ;
141 m_windowId
= NewControlId();
150 AdjustForParentClientOrigin(x
, y
, wxSIZE_USE_EXISTING
);
158 wxToolBar::~wxToolBar()
160 // we must refresh the frame size when the toolbar is deleted but the frame
161 // is not - otherwise toolbar leaves a hole in the place it used to occupy
164 bool wxToolBar::Realize()
166 if (m_tools
.Number() == 0)
174 GetParent()->MacGetPortParams( &localOrigin
, &clipRect
, &window
, &win
) ;
176 Rect toolbarrect
= { m_y
+ localOrigin
.v
, m_x
+ localOrigin
.h
,
177 m_y
+ m_height
+ localOrigin
.v
, m_x
+ m_width
+ localOrigin
.h
} ;
178 ControlFontStyleRec controlstyle
;
180 controlstyle
.flags
= kControlUseFontMask
;
181 controlstyle
.font
= kControlFontSmallSystemFont
;
183 wxNode
*node
= m_tools
.First();
187 wxSize toolSize
= GetToolSize() ;
196 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
197 wxBitmapRefData
* bmap
= (wxBitmapRefData
*) ( tool
->GetBitmap1().GetRefData()) ;
199 if( !tool
->IsSeparator() )
201 Rect toolrect
= { toolbarrect
.top
+ y
+ m_yMargin
+ kwxMacToolBarTopMargin
,
202 toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
, 0 , 0 } ;
203 toolrect
.right
= toolrect
.left
+ toolSize
.x
;
204 toolrect
.bottom
= toolrect
.top
+ toolSize
.y
;
206 ControlButtonContentInfo info
;
209 if ( bmap
->m_bitmapType
== kMacBitmapTypePict
)
211 info
.contentType
= kControlContentPictHandle
;
212 info
.u
.picture
= bmap
->m_hPict
;
214 else if ( bmap
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
216 if ( tool
->GetBitmap1().GetMask() )
218 info
.contentType
= kControlContentCIconHandle
;
219 info
.u
.cIconHandle
= wxMacCreateCIcon( bmap
->m_hBitmap
, tool
->GetBitmap1().GetMask()->GetMaskBitmap() ,
224 info
.contentType
= kControlContentCIconHandle
;
225 info
.u
.cIconHandle
= wxMacCreateCIcon( bmap
->m_hBitmap
, NULL
,
231 ControlHandle m_macToolHandle
;
233 SInt16 behaviour
= kControlBehaviorOffsetContents
;
234 if ( tool
->CanBeToggled() )
235 behaviour
+= kControlBehaviorToggles
;
237 if ( info
.u
.cIconHandle
) // since it is a handle we can use one of them
239 m_macToolHandle
= ::NewControl( window
, &toolrect
, "\p" , false , 0 ,
240 behaviour
+ info
.contentType
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
242 ::SetControlData( m_macToolHandle
, kControlButtonPart
, kControlBevelButtonContentTag
, sizeof(info
) , (char*) &info
) ;
246 m_macToolHandle
= ::NewControl( window
, &toolrect
, "\p" , false , 0 ,
247 behaviour
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
249 UMAShowControl( m_macToolHandle
) ;
250 m_macToolHandles
.Add( m_macToolHandle
) ;
251 tool
->m_index
= m_macToolHandles
.Count() -1 ;
252 if ( !tool
->IsEnabled() )
254 UMADeactivateControl( m_macToolHandle
) ;
256 if ( tool
->CanBeToggled() && tool
->IsToggled() )
258 ::SetControlValue( m_macToolHandle
, 1 ) ;
262 ::SetControlValue( m_macToolHandle
, 0 ) ;
265 ::SetControlFontStyle( m_macToolHandle , &controlstyle ) ;
267 ControlHandle container
= GetParent()->MacGetContainerForEmbedding() ;
268 wxASSERT_MSG( container
!= NULL
, "No valid mac container control" ) ;
269 ::EmbedControl( m_macToolHandle
, container
) ;
271 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
273 x
+= (int)toolSize
.x
;
277 y
+= (int)toolSize
.y
;
283 m_macToolHandles
.Add( NULL
) ;
284 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
286 x
+= (int)toolSize
.x
/ 4;
290 y
+= (int)toolSize
.y
/ 4;
293 if ( toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
- m_x
- localOrigin
.h
> maxWidth
) {
294 maxWidth
= toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
- m_x
- localOrigin
.h
;
296 if (toolbarrect
.top
+ y
+ m_yMargin
+ kwxMacToolBarTopMargin
- m_y
- localOrigin
.v
> maxHeight
) {
297 maxHeight
= toolbarrect
.top
+ y
+ m_yMargin
+ kwxMacToolBarTopMargin
- m_y
- localOrigin
.v
;
302 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
304 if ( m_maxRows
== 0 )
306 // if not set yet, only one row
310 maxHeight
+= toolSize
.y
;
311 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
312 m_maxHeight
= maxHeight
;
316 if ( noButtons
> 0 && m_maxRows
== 0 )
318 // if not set yet, have one column
322 maxWidth
+= toolSize
.x
;
323 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
324 m_maxWidth
= maxWidth
;
327 SetSize(maxWidth
, maxHeight
);
332 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
334 m_defaultWidth
= size
.x
+4; m_defaultHeight
= size
.y
+4;
337 // The button size is bigger than the bitmap size
338 wxSize
wxToolBar::GetToolSize() const
340 return wxSize(m_defaultWidth
+ 4, m_defaultHeight
+ 4);
343 void wxToolBar::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
346 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
348 if ( m_macToolHandles
[index
] == (void*) control
)
350 wxToolBarTool
*tool
= (wxToolBarTool
*)m_tools
.Nth( index
)->Data();
351 if ( tool
->CanBeToggled() )
353 tool
->Toggle( GetControlValue( control
) ) ;
355 OnLeftClick( tool
->GetId() , tool
-> IsToggled() ) ;
361 void wxToolBar::SetRows(int nRows
)
363 if ( nRows
== m_maxRows
)
365 // avoid resizing the frame uselessly
372 void wxToolBar::MacSuperChangedPosition()
374 if (m_tools
.Number() > 0)
382 GetParent()->MacGetPortParams( &localOrigin
, &clipRect
, &window
, &win
) ;
384 Rect toolbarrect
= { m_y
+ localOrigin
.v
, m_x
+ localOrigin
.h
,
385 m_y
+ m_height
+ localOrigin
.v
, m_x
+ m_width
+ localOrigin
.h
} ;
386 ControlFontStyleRec controlstyle
;
388 controlstyle
.flags
= kControlUseFontMask
;
389 controlstyle
.font
= kControlFontSmallSystemFont
;
391 wxNode
*node
= m_tools
.First();
394 wxSize toolSize
= GetToolSize() ;
402 WindowRef rootwindow
= GetMacRootWindow() ;
403 wxWindow
* wxrootwindow
= wxFindWinFromMacWindow( rootwindow
) ;
404 ::SetThemeWindowBackground( rootwindow
, kThemeBrushDialogBackgroundActive
, false ) ;
405 wxMacDrawingHelper
focus( wxrootwindow
) ;
408 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
409 wxBitmapRefData
* bmap
= (wxBitmapRefData
*) ( tool
->GetBitmap1().GetRefData()) ;
411 if( !tool
->IsSeparator() )
413 Rect toolrect
= { toolbarrect
.top
+ m_yMargin
+ kwxMacToolBarTopMargin
, toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
, 0 , 0 } ;
414 toolrect
.right
= toolrect
.left
+ toolSize
.x
;
415 toolrect
.bottom
= toolrect
.top
+ toolSize
.y
;
417 ControlHandle m_macToolHandle
= (ControlHandle
) m_macToolHandles
[toolcount
++] ;
421 GetControlBounds( m_macToolHandle
, &contrlRect
) ;
422 int former_mac_x
= contrlRect
.left
;
423 int former_mac_y
= contrlRect
.top
;
424 int mac_x
= toolrect
.left
;
425 int mac_y
= toolrect
.top
;
427 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
430 Rect inval
= { former_mac_y
, former_mac_x
, former_mac_y
+ toolSize
.y
, former_mac_x
+ toolSize
.y
} ;
431 InvalWindowRect( rootwindow
, &inval
) ;
433 UMAMoveControl( m_macToolHandle
, mac_x
, mac_y
) ;
435 Rect inval
= { mac_y
, mac_x
, mac_y
+ toolSize
.y
, mac_x
+ toolSize
.y
} ;
436 InvalWindowRect( rootwindow
, &inval
) ;
441 x
+= (int)toolSize
.x
;
447 x
+= (int)toolSize
.x
/ 4;
449 if ( toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
- m_x
- localOrigin
.h
> maxWidth
)
450 maxWidth
= toolbarrect
.left
+ x
+ kwxMacToolBarLeftMargin
+ m_xMargin
- m_x
- localOrigin
.h
;
451 if (toolbarrect
.top
+ m_yMargin
+ kwxMacToolBarTopMargin
- m_y
- localOrigin
.v
> maxHeight
)
452 maxHeight
= toolbarrect
.top
+ kwxMacToolBarTopMargin
+ m_yMargin
- m_y
- localOrigin
.v
;
456 if ( wxrootwindow
->IsKindOf( CLASSINFO( wxDialog
) ) )
461 ::SetThemeWindowBackground( rootwindow
, kThemeBrushDocumentWindowBackground
, false ) ;
465 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
467 if ( m_maxRows
== 0 )
469 // if not set yet, only one row
473 maxHeight
+= toolSize
.y
;
474 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
475 m_maxHeight
= maxHeight
;
479 if ( noButtons
> 0 && m_maxRows
== 0 )
481 // if not set yet, have one column
485 maxWidth
+= toolSize
.x
;
486 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
487 m_maxWidth
= maxWidth
;
490 SetSize(maxWidth
, maxHeight
);
493 wxWindow::MacSuperChangedPosition() ;
496 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
498 MacClientToRootWindow( &x
, &y
) ;
499 Point pt
= { y
,x
} ;
502 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
504 if ( m_macToolHandles
[index
] )
507 GetControlBounds((ControlHandle
) m_macToolHandles
[index
], &bounds
) ;
508 if ( PtInRect( pt
, &bounds
) )
510 return (wxToolBarTool
*) (m_tools
.Nth( index
)->Data() ) ;
515 return (wxToolBarToolBase
*)NULL
;
518 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
520 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
523 return tool
->GetShortHelp() ;
528 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
533 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
534 if ( tool
->m_index
< 0 )
537 ControlHandle control
= (ControlHandle
) m_macToolHandles
[ tool
->m_index
] ;
540 UMAActivateControl( control
) ;
542 UMADeactivateControl( control
) ;
545 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
550 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
551 if ( tool
->m_index
< 0 )
554 ControlHandle control
= (ControlHandle
) m_macToolHandles
[ tool
->m_index
] ;
555 ::SetControlValue( control
, toggle
) ;
558 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
559 wxToolBarToolBase
*tool
)
561 // nothing special to do here - we really create the toolbar buttons in
568 void wxToolBar::DoSetToggle(wxToolBarToolBase
*t
, bool toggle
)
570 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
571 // TODO: set toggle state
574 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
579 void wxToolBar::OnPaint(wxPaintEvent
& event
)
586 GetParent()->MacGetPortParams( &localOrigin
, &clipRect
, &window
, &win
) ;
589 wxMacDrawingHelper
help( win
) ;
590 // the mac control manager always assumes to have the origin at 0,0
593 bool hasTabBehind
= false ;
594 wxWindow
* parent
= GetParent() ;
597 if( parent
->MacGetWindowData() )
599 ::SetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, kThemeBrushDialogBackgroundActive
, false ) ;
603 if( parent
->IsKindOf( CLASSINFO( wxNotebook
) ) || parent
->IsKindOf( CLASSINFO( wxTabCtrl
) ))
605 if ( ((wxControl
*)parent
)->GetMacControl() )
606 SetUpControlBackground( ((wxControl
*)parent
)->GetMacControl() , -1 , true ) ;
610 parent
= parent
->GetParent() ;
613 Rect toolbarrect
= { m_y
+ localOrigin
.v
, m_x
+ localOrigin
.h
,
614 m_y
+ localOrigin
.v
+ m_height
, m_x
+ localOrigin
.h
+ m_width
} ;
616 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
619 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
621 if ( m_macToolHandles
[index
] )
623 UMADrawControl( (ControlHandle
) m_macToolHandles
[index
] ) ;
627 ::SetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, win
->MacGetWindowData()->m_macWindowBackgroundTheme
, false ) ;
631 void wxToolBar::OnMouse( wxMouseEvent
&event
)
634 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
640 MacClientToRootWindow( &x
, &y
) ;
642 ControlHandle control
;
646 WindowRef window
= GetMacRootWindow() ;
653 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
654 modifiers
|= btnState
;
656 if ( event
.m_shiftDown
)
657 modifiers
|= shiftKey
;
659 if ( event
.m_controlDown
)
660 modifiers
|= controlKey
;
662 if ( event
.m_altDown
)
663 modifiers
|= optionKey
;
665 if ( event
.m_metaDown
)
666 modifiers
|= cmdKey
;
668 controlpart
= FindControl( localwhere
, window
, &control
) ;
670 if ( control
&& ::IsControlActive( control
) )
673 if ( controlpart
== kControlIndicatorPart
&& !UMAHasAppearance() )
674 controlpart
= ::HandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) NULL
) ;
676 controlpart
= ::HandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) -1 ) ;
677 wxTheApp
->s_lastMouseDown
= 0 ;
678 if ( controlpart
&& ! ( ( UMAHasAppearance() || (controlpart
!= kControlIndicatorPart
) )
679 && (IsKindOf( CLASSINFO( wxScrollBar
) ) ) ) ) // otherwise we will get the event twice
681 MacHandleControlClick( control
, controlpart
) ;
689 #endif // wxUSE_TOOLBAR