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();
186 wxSize toolSize
= GetToolSize() ;
195 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
196 wxBitmapRefData
* bmap
= (wxBitmapRefData
*) ( tool
->GetBitmap1().GetRefData()) ;
198 if( !tool
->IsSeparator() )
200 Rect toolrect
= { toolbarrect
.top
+ m_yMargin
+ kwxMacToolBarTopMargin
, toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
, 0 , 0 } ;
201 toolrect
.right
= toolrect
.left
+ toolSize
.x
;
202 toolrect
.bottom
= toolrect
.top
+ toolSize
.y
;
204 ControlButtonContentInfo info
;
207 if ( bmap
->m_bitmapType
== kMacBitmapTypePict
)
209 info
.contentType
= kControlContentPictHandle
;
210 info
.u
.picture
= bmap
->m_hPict
;
212 else if ( bmap
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
214 if ( tool
->GetBitmap1().GetMask() )
216 info
.contentType
= kControlContentCIconHandle
;
217 info
.u
.cIconHandle
= wxMacCreateCIcon( bmap
->m_hBitmap
, tool
->GetBitmap1().GetMask()->GetMaskBitmap() ,
222 info
.contentType
= kControlContentCIconHandle
;
223 info
.u
.cIconHandle
= wxMacCreateCIcon( bmap
->m_hBitmap
, NULL
,
229 ControlHandle m_macToolHandle
;
231 SInt16 behaviour
= kControlBehaviorOffsetContents
;
232 if ( tool
->CanBeToggled() )
233 behaviour
+= kControlBehaviorToggles
;
235 if ( info
.u
.cIconHandle
) // since it is a handle we can use one of them
237 m_macToolHandle
= ::NewControl( window
, &toolrect
, "\p" , false , 0 ,
238 behaviour
+ info
.contentType
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
240 ::SetControlData( m_macToolHandle
, kControlButtonPart
, kControlBevelButtonContentTag
, sizeof(info
) , (char*) &info
) ;
244 m_macToolHandle
= ::NewControl( window
, &toolrect
, "\p" , false , 0 ,
245 behaviour
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
247 UMAShowControl( m_macToolHandle
) ;
248 m_macToolHandles
.Add( m_macToolHandle
) ;
249 tool
->m_index
= m_macToolHandles
.Count() -1 ;
250 if ( !tool
->IsEnabled() )
252 UMADeactivateControl( m_macToolHandle
) ;
254 if ( tool
->CanBeToggled() && tool
->IsToggled() )
256 ::SetControlValue( m_macToolHandle
, 1 ) ;
260 ::SetControlValue( m_macToolHandle
, 0 ) ;
263 ::SetControlFontStyle( m_macToolHandle , &controlstyle ) ;
265 ControlHandle container
= GetParent()->MacGetContainerForEmbedding() ;
266 wxASSERT_MSG( container
!= NULL
, "No valid mac container control" ) ;
267 ::EmbedControl( m_macToolHandle
, container
) ;
269 x
+= (int)toolSize
.x
;
274 m_macToolHandles
.Add( NULL
) ;
275 x
+= (int)toolSize
.x
/ 4;
277 if ( toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
- m_x
- localOrigin
.h
> maxWidth
)
278 maxWidth
= toolbarrect
.left
+ x
+ kwxMacToolBarLeftMargin
+ m_xMargin
- m_x
- localOrigin
.h
;
279 if (toolbarrect
.top
+ m_yMargin
+ kwxMacToolBarTopMargin
- m_y
- localOrigin
.v
> maxHeight
)
280 maxHeight
= toolbarrect
.top
+ kwxMacToolBarTopMargin
+ m_yMargin
- m_y
- localOrigin
.v
;
285 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
287 if ( m_maxRows
== 0 )
289 // if not set yet, only one row
293 maxHeight
+= toolSize
.y
;
294 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
295 m_maxHeight
= maxHeight
;
299 if ( noButtons
> 0 && m_maxRows
== 0 )
301 // if not set yet, have one column
305 maxWidth
+= toolSize
.x
;
306 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
307 m_maxWidth
= maxWidth
;
310 SetSize(maxWidth
, maxHeight
);
315 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
317 m_defaultWidth
= size
.x
+4; m_defaultHeight
= size
.y
+4;
320 // The button size is bigger than the bitmap size
321 wxSize
wxToolBar::GetToolSize() const
323 return wxSize(m_defaultWidth
+ 4, m_defaultHeight
+ 4);
326 void wxToolBar::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
329 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
331 if ( m_macToolHandles
[index
] == (void*) control
)
333 wxToolBarTool
*tool
= (wxToolBarTool
*)m_tools
.Nth( index
)->Data();
334 if ( tool
->CanBeToggled() )
336 tool
->Toggle( GetControlValue( control
) ) ;
338 OnLeftClick( tool
->GetId() , tool
-> IsToggled() ) ;
344 void wxToolBar::SetRows(int nRows
)
346 if ( nRows
== m_maxRows
)
348 // avoid resizing the frame uselessly
355 void wxToolBar::MacSuperChangedPosition()
357 if (m_tools
.Number() > 0)
365 GetParent()->MacGetPortParams( &localOrigin
, &clipRect
, &window
, &win
) ;
367 Rect toolbarrect
= { m_y
+ localOrigin
.v
, m_x
+ localOrigin
.h
,
368 m_y
+ m_height
+ localOrigin
.v
, m_x
+ m_width
+ localOrigin
.h
} ;
369 ControlFontStyleRec controlstyle
;
371 controlstyle
.flags
= kControlUseFontMask
;
372 controlstyle
.font
= kControlFontSmallSystemFont
;
374 wxNode
*node
= m_tools
.First();
377 wxSize toolSize
= GetToolSize() ;
385 WindowRef rootwindow
= GetMacRootWindow() ;
386 wxWindow
* wxrootwindow
= wxFindWinFromMacWindow( rootwindow
) ;
387 ::SetThemeWindowBackground( rootwindow
, kThemeBrushDialogBackgroundActive
, false ) ;
388 wxMacDrawingHelper
focus( wxrootwindow
) ;
391 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
392 wxBitmapRefData
* bmap
= (wxBitmapRefData
*) ( tool
->GetBitmap1().GetRefData()) ;
394 if( !tool
->IsSeparator() )
396 Rect toolrect
= { toolbarrect
.top
+ m_yMargin
+ kwxMacToolBarTopMargin
, toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
, 0 , 0 } ;
397 toolrect
.right
= toolrect
.left
+ toolSize
.x
;
398 toolrect
.bottom
= toolrect
.top
+ toolSize
.y
;
400 ControlHandle m_macToolHandle
= (ControlHandle
) m_macToolHandles
[toolcount
++] ;
404 GetControlBounds( m_macToolHandle
, &contrlRect
) ;
405 int former_mac_x
= contrlRect
.left
;
406 int former_mac_y
= contrlRect
.top
;
407 int mac_x
= toolrect
.left
;
408 int mac_y
= toolrect
.top
;
410 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
413 Rect inval
= { former_mac_y
, former_mac_x
, former_mac_y
+ toolSize
.y
, former_mac_x
+ toolSize
.y
} ;
414 InvalWindowRect( rootwindow
, &inval
) ;
416 UMAMoveControl( m_macToolHandle
, mac_x
, mac_y
) ;
418 Rect inval
= { mac_y
, mac_x
, mac_y
+ toolSize
.y
, mac_x
+ toolSize
.y
} ;
419 InvalWindowRect( rootwindow
, &inval
) ;
424 x
+= (int)toolSize
.x
;
430 x
+= (int)toolSize
.x
/ 4;
432 if ( toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
- m_x
- localOrigin
.h
> maxWidth
)
433 maxWidth
= toolbarrect
.left
+ x
+ kwxMacToolBarLeftMargin
+ m_xMargin
- m_x
- localOrigin
.h
;
434 if (toolbarrect
.top
+ m_yMargin
+ kwxMacToolBarTopMargin
- m_y
- localOrigin
.v
> maxHeight
)
435 maxHeight
= toolbarrect
.top
+ kwxMacToolBarTopMargin
+ m_yMargin
- m_y
- localOrigin
.v
;
439 if ( wxrootwindow
->IsKindOf( CLASSINFO( wxDialog
) ) )
444 ::SetThemeWindowBackground( rootwindow
, kThemeBrushDocumentWindowBackground
, false ) ;
448 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
450 if ( m_maxRows
== 0 )
452 // if not set yet, only one row
456 maxHeight
+= toolSize
.y
;
457 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
458 m_maxHeight
= maxHeight
;
462 if ( noButtons
> 0 && m_maxRows
== 0 )
464 // if not set yet, have one column
468 maxWidth
+= toolSize
.x
;
469 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
470 m_maxWidth
= maxWidth
;
473 SetSize(maxWidth
, maxHeight
);
476 wxWindow::MacSuperChangedPosition() ;
479 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
481 MacClientToRootWindow( &x
, &y
) ;
482 Point pt
= { y
,x
} ;
485 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
487 if ( m_macToolHandles
[index
] )
490 GetControlBounds((ControlHandle
) m_macToolHandles
[index
], &bounds
) ;
491 if ( PtInRect( pt
, &bounds
) )
493 return (wxToolBarTool
*) (m_tools
.Nth( index
)->Data() ) ;
498 return (wxToolBarToolBase
*)NULL
;
501 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
503 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
506 return tool
->GetShortHelp() ;
511 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
)
527 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
528 if ( tool
->m_index
< 0 )
531 ControlHandle control
= (ControlHandle
) m_macToolHandles
[ tool
->m_index
] ;
532 ::SetControlValue( control
, toggle
) ;
535 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
536 wxToolBarToolBase
*tool
)
538 // nothing special to do here - we really create the toolbar buttons in
545 void wxToolBar::DoSetToggle(wxToolBarToolBase
*t
, bool toggle
)
547 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
548 // TODO: set toggle state
551 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
556 void wxToolBar::OnPaint(wxPaintEvent
& event
)
563 GetParent()->MacGetPortParams( &localOrigin
, &clipRect
, &window
, &win
) ;
566 wxMacDrawingHelper
help( win
) ;
567 // the mac control manager always assumes to have the origin at 0,0
570 bool hasTabBehind
= false ;
571 wxWindow
* parent
= GetParent() ;
574 if( parent
->MacGetWindowData() )
576 ::SetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, kThemeBrushDialogBackgroundActive
, false ) ;
580 if( parent
->IsKindOf( CLASSINFO( wxNotebook
) ) || parent
->IsKindOf( CLASSINFO( wxTabCtrl
) ))
582 if ( ((wxControl
*)parent
)->GetMacControl() )
583 SetUpControlBackground( ((wxControl
*)parent
)->GetMacControl() , -1 , true ) ;
587 parent
= parent
->GetParent() ;
590 Rect toolbarrect
= { m_y
+ localOrigin
.v
, m_x
+ localOrigin
.h
,
591 m_y
+ localOrigin
.v
+ m_height
, m_x
+ localOrigin
.h
+ m_width
} ;
593 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
596 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
598 if ( m_macToolHandles
[index
] )
600 UMADrawControl( (ControlHandle
) m_macToolHandles
[index
] ) ;
604 ::SetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, win
->MacGetWindowData()->m_macWindowBackgroundTheme
, false ) ;
608 void wxToolBar::OnMouse( wxMouseEvent
&event
)
611 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
617 MacClientToRootWindow( &x
, &y
) ;
619 ControlHandle control
;
623 WindowRef window
= GetMacRootWindow() ;
630 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
631 modifiers
|= btnState
;
633 if ( event
.m_shiftDown
)
634 modifiers
|= shiftKey
;
636 if ( event
.m_controlDown
)
637 modifiers
|= controlKey
;
639 if ( event
.m_altDown
)
640 modifiers
|= optionKey
;
642 if ( event
.m_metaDown
)
643 modifiers
|= cmdKey
;
645 controlpart
= FindControl( localwhere
, window
, &control
) ;
647 if ( control
&& ::IsControlActive( control
) )
650 if ( controlpart
== kControlIndicatorPart
&& !UMAHasAppearance() )
651 controlpart
= ::HandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) NULL
) ;
653 controlpart
= ::HandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) -1 ) ;
654 wxTheApp
->s_lastMouseDown
= 0 ;
655 if ( controlpart
&& ! ( ( UMAHasAppearance() || (controlpart
!= kControlIndicatorPart
) )
656 && (IsKindOf( CLASSINFO( wxScrollBar
) ) ) ) ) // otherwise we will get the event twice
658 MacHandleControlClick( control
, controlpart
) ;
666 #endif // wxUSE_TOOLBAR