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)
171 WindowRef window
= MacGetRootWindow() ;
176 MacWindowToRootWindow( &lx
, &ly
) ;
180 // GetParent()->MacGetPortParams( &localOrigin , &clipRect , &window , &win ) ;
182 Rect toolbarrect
= { localOrigin
.v
,localOrigin
.h
,
183 m_height
+ localOrigin
.v
, m_width
+ localOrigin
.h
} ;
184 ControlFontStyleRec controlstyle
;
186 controlstyle
.flags
= kControlUseFontMask
;
187 controlstyle
.font
= kControlFontSmallSystemFont
;
189 wxNode
*node
= m_tools
.First();
193 wxSize toolSize
= GetToolSize() ;
202 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
203 wxBitmapRefData
* bmap
= (wxBitmapRefData
*) ( tool
->GetBitmap1().GetRefData()) ;
205 if( !tool
->IsSeparator() )
207 Rect toolrect
= { toolbarrect
.top
+ y
+ m_yMargin
+ kwxMacToolBarTopMargin
,
208 toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
, 0 , 0 } ;
209 toolrect
.right
= toolrect
.left
+ toolSize
.x
;
210 toolrect
.bottom
= toolrect
.top
+ toolSize
.y
;
212 ControlButtonContentInfo info
;
215 if ( bmap
->m_bitmapType
== kMacBitmapTypePict
)
217 info
.contentType
= kControlContentPictHandle
;
218 info
.u
.picture
= bmap
->m_hPict
;
220 else if ( bmap
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
222 if ( tool
->GetBitmap1().GetMask() )
224 info
.contentType
= kControlContentCIconHandle
;
225 info
.u
.cIconHandle
= wxMacCreateCIcon( bmap
->m_hBitmap
, tool
->GetBitmap1().GetMask()->GetMaskBitmap() ,
230 info
.contentType
= kControlContentCIconHandle
;
231 info
.u
.cIconHandle
= wxMacCreateCIcon( bmap
->m_hBitmap
, NULL
,
237 ControlHandle m_macToolHandle
;
239 SInt16 behaviour
= kControlBehaviorOffsetContents
;
240 if ( tool
->CanBeToggled() )
241 behaviour
+= kControlBehaviorToggles
;
243 if ( info
.u
.cIconHandle
) // since it is a handle we can use one of them
245 m_macToolHandle
= ::NewControl( window
, &toolrect
, "\p" , false , 0 ,
246 behaviour
+ info
.contentType
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
248 ::SetControlData( m_macToolHandle
, kControlButtonPart
, kControlBevelButtonContentTag
, sizeof(info
) , (char*) &info
) ;
252 m_macToolHandle
= ::NewControl( window
, &toolrect
, "\p" , false , 0 ,
253 behaviour
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
255 UMAShowControl( m_macToolHandle
) ;
256 m_macToolHandles
.Add( m_macToolHandle
) ;
257 tool
->m_index
= m_macToolHandles
.Count() -1 ;
258 if ( !tool
->IsEnabled() )
260 UMADeactivateControl( m_macToolHandle
) ;
262 if ( tool
->CanBeToggled() && tool
->IsToggled() )
264 ::SetControlValue( m_macToolHandle
, 1 ) ;
268 ::SetControlValue( m_macToolHandle
, 0 ) ;
271 ::SetControlFontStyle( m_macToolHandle , &controlstyle ) ;
273 ControlHandle container
= GetParent()->MacGetContainerForEmbedding() ;
274 wxASSERT_MSG( container
!= NULL
, "No valid mac container control" ) ;
275 ::EmbedControl( m_macToolHandle
, container
) ;
277 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
279 x
+= (int)toolSize
.x
;
283 y
+= (int)toolSize
.y
;
289 m_macToolHandles
.Add( NULL
) ;
290 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
292 x
+= (int)toolSize
.x
/ 4;
296 y
+= (int)toolSize
.y
/ 4;
299 if ( toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
- m_x
- localOrigin
.h
> maxWidth
) {
300 maxWidth
= toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
- m_x
- localOrigin
.h
;
302 if (toolbarrect
.top
+ y
+ m_yMargin
+ kwxMacToolBarTopMargin
- m_y
- localOrigin
.v
> maxHeight
) {
303 maxHeight
= toolbarrect
.top
+ y
+ m_yMargin
+ kwxMacToolBarTopMargin
- m_y
- localOrigin
.v
;
308 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
310 if ( m_maxRows
== 0 )
312 // if not set yet, only one row
316 maxHeight
+= toolSize
.y
;
317 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
318 m_maxHeight
= maxHeight
;
322 if ( noButtons
> 0 && m_maxRows
== 0 )
324 // if not set yet, have one column
328 maxWidth
+= toolSize
.x
;
329 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
330 m_maxWidth
= maxWidth
;
333 SetSize(maxWidth
, maxHeight
);
338 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
340 m_defaultWidth
= size
.x
+4; m_defaultHeight
= size
.y
+4;
343 // The button size is bigger than the bitmap size
344 wxSize
wxToolBar::GetToolSize() const
346 return wxSize(m_defaultWidth
+ 4, m_defaultHeight
+ 4);
349 void wxToolBar::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
352 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
354 if ( m_macToolHandles
[index
] == (void*) control
)
356 wxToolBarTool
*tool
= (wxToolBarTool
*)m_tools
.Nth( index
)->Data();
357 if ( tool
->CanBeToggled() )
359 tool
->Toggle( GetControlValue( control
) ) ;
361 OnLeftClick( tool
->GetId() , tool
-> IsToggled() ) ;
367 void wxToolBar::SetRows(int nRows
)
369 if ( nRows
== m_maxRows
)
371 // avoid resizing the frame uselessly
378 void wxToolBar::MacSuperChangedPosition()
380 if (m_tools
.Number() > 0)
389 MacWindowToRootWindow( &lx
, &ly
) ;
393 // GetParent()->MacGetPortParams( &localOrigin , &clipRect , &window , &win ) ;
395 Rect toolbarrect
= { localOrigin
.v
,localOrigin
.h
,
396 m_height
+ localOrigin
.v
, m_width
+ localOrigin
.h
} ;
397 ControlFontStyleRec controlstyle
;
399 controlstyle
.flags
= kControlUseFontMask
;
400 controlstyle
.font
= kControlFontSmallSystemFont
;
402 wxNode
*node
= m_tools
.First();
405 wxSize toolSize
= GetToolSize() ;
413 WindowRef rootwindow
= MacGetRootWindow() ;
416 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
417 wxBitmapRefData
* bmap
= (wxBitmapRefData
*) ( tool
->GetBitmap1().GetRefData()) ;
419 if( !tool
->IsSeparator() )
421 Rect toolrect
= { toolbarrect
.top
+ m_yMargin
+ kwxMacToolBarTopMargin
, toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
, 0 , 0 } ;
422 toolrect
.right
= toolrect
.left
+ toolSize
.x
;
423 toolrect
.bottom
= toolrect
.top
+ toolSize
.y
;
425 ControlHandle m_macToolHandle
= (ControlHandle
) m_macToolHandles
[toolcount
++] ;
429 GetControlBounds( m_macToolHandle
, &contrlRect
) ;
430 int former_mac_x
= contrlRect
.left
;
431 int former_mac_y
= contrlRect
.top
;
432 int mac_x
= toolrect
.left
;
433 int mac_y
= toolrect
.top
;
435 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
438 Rect inval
= { former_mac_y
, former_mac_x
, former_mac_y
+ toolSize
.y
, former_mac_x
+ toolSize
.y
} ;
439 InvalWindowRect( rootwindow
, &inval
) ;
441 UMAMoveControl( m_macToolHandle
, mac_x
, mac_y
) ;
443 Rect inval
= { mac_y
, mac_x
, mac_y
+ toolSize
.y
, mac_x
+ toolSize
.y
} ;
444 InvalWindowRect( rootwindow
, &inval
) ;
449 x
+= (int)toolSize
.x
;
455 x
+= (int)toolSize
.x
/ 4;
457 if ( toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
- m_x
- localOrigin
.h
> maxWidth
)
458 maxWidth
= toolbarrect
.left
+ x
+ kwxMacToolBarLeftMargin
+ m_xMargin
- m_x
- localOrigin
.h
;
459 if (toolbarrect
.top
+ m_yMargin
+ kwxMacToolBarTopMargin
- m_y
- localOrigin
.v
> maxHeight
)
460 maxHeight
= toolbarrect
.top
+ kwxMacToolBarTopMargin
+ m_yMargin
- m_y
- localOrigin
.v
;
466 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
468 if ( m_maxRows
== 0 )
470 // if not set yet, only one row
474 maxHeight
+= toolSize
.y
;
475 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
476 m_maxHeight
= maxHeight
;
480 if ( noButtons
> 0 && m_maxRows
== 0 )
482 // if not set yet, have one column
486 maxWidth
+= toolSize
.x
;
487 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
488 m_maxWidth
= maxWidth
;
491 SetSize(maxWidth
, maxHeight
);
494 wxWindow::MacSuperChangedPosition() ;
497 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
499 MacClientToRootWindow( &x
, &y
) ;
500 Point pt
= { y
,x
} ;
503 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
505 if ( m_macToolHandles
[index
] )
508 GetControlBounds((ControlHandle
) m_macToolHandles
[index
], &bounds
) ;
509 if ( PtInRect( pt
, &bounds
) )
511 return (wxToolBarTool
*) (m_tools
.Nth( index
)->Data() ) ;
516 return (wxToolBarToolBase
*)NULL
;
519 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
521 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
524 return tool
->GetShortHelp() ;
529 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
534 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
535 if ( tool
->m_index
< 0 )
538 ControlHandle control
= (ControlHandle
) m_macToolHandles
[ tool
->m_index
] ;
541 UMAActivateControl( control
) ;
543 UMADeactivateControl( control
) ;
546 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
551 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
552 if ( tool
->m_index
< 0 )
555 ControlHandle control
= (ControlHandle
) m_macToolHandles
[ tool
->m_index
] ;
556 ::SetControlValue( control
, toggle
) ;
559 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
560 wxToolBarToolBase
*tool
)
562 // nothing special to do here - we really create the toolbar buttons in
569 void wxToolBar::DoSetToggle(wxToolBarToolBase
*t
, bool toggle
)
571 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
572 // TODO: set toggle state
575 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
580 void wxToolBar::OnPaint(wxPaintEvent
& event
)
583 wxMacPortSetter
helper(&dc
) ;
585 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
586 dc
.YLOG2DEVMAC(m_height
) , dc
.XLOG2DEVMAC(m_width
) } ;
587 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
590 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
592 if ( m_macToolHandles
[index
] )
594 UMADrawControl( (ControlHandle
) m_macToolHandles
[index
] ) ;
600 void wxToolBar::OnMouse( wxMouseEvent
&event
)
602 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
608 MacClientToRootWindow( &x
, &y
) ;
610 ControlHandle control
;
614 WindowRef window
= MacGetRootWindow() ;
621 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
622 modifiers
|= btnState
;
624 if ( event
.m_shiftDown
)
625 modifiers
|= shiftKey
;
627 if ( event
.m_controlDown
)
628 modifiers
|= controlKey
;
630 if ( event
.m_altDown
)
631 modifiers
|= optionKey
;
633 if ( event
.m_metaDown
)
634 modifiers
|= cmdKey
;
636 controlpart
= FindControl( localwhere
, window
, &control
) ;
638 if ( control
&& ::IsControlActive( control
) )
641 if ( controlpart
== kControlIndicatorPart
&& !UMAHasAppearance() )
642 controlpart
= ::HandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) NULL
) ;
644 controlpart
= ::HandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) -1 ) ;
645 wxTheApp
->s_lastMouseDown
= 0 ;
646 if ( controlpart
&& ! ( ( UMAHasAppearance() || (controlpart
!= kControlIndicatorPart
) )
647 && (IsKindOf( CLASSINFO( wxScrollBar
) ) ) ) ) // otherwise we will get the event twice
649 MacHandleControlClick( control
, controlpart
) ;
657 #endif // wxUSE_TOOLBAR