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
= (WindowRef
) 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
;
213 wxMacCreateBitmapButton( &info
, tool
->GetBitmap1() ) ;
214 ControlHandle m_macToolHandle
;
216 SInt16 behaviour
= kControlBehaviorOffsetContents
;
217 if ( tool
->CanBeToggled() )
218 behaviour
+= kControlBehaviorToggles
;
220 if ( info
.contentType
!= kControlNoContent
)
222 m_macToolHandle
= ::NewControl( window
, &toolrect
, "\p" , false , 0 ,
223 behaviour
+ info
.contentType
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
225 ::SetControlData( m_macToolHandle
, kControlButtonPart
, kControlBevelButtonContentTag
, sizeof(info
) , (char*) &info
) ;
229 m_macToolHandle
= ::NewControl( window
, &toolrect
, "\p" , false , 0 ,
230 behaviour
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
232 UMAShowControl( m_macToolHandle
) ;
233 m_macToolHandles
.Add( m_macToolHandle
) ;
234 tool
->m_index
= m_macToolHandles
.Count() -1 ;
235 if ( !tool
->IsEnabled() )
237 UMADeactivateControl( m_macToolHandle
) ;
239 if ( tool
->CanBeToggled() && tool
->IsToggled() )
241 ::SetControlValue( m_macToolHandle
, 1 ) ;
245 ::SetControlValue( m_macToolHandle
, 0 ) ;
248 ::SetControlFontStyle( m_macToolHandle , &controlstyle ) ;
250 ControlHandle container
= (ControlHandle
) GetParent()->MacGetContainerForEmbedding() ;
251 wxASSERT_MSG( container
!= NULL
, "No valid mac container control" ) ;
252 ::EmbedControl( m_macToolHandle
, container
) ;
254 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
256 x
+= (int)toolSize
.x
;
260 y
+= (int)toolSize
.y
;
266 m_macToolHandles
.Add( NULL
) ;
267 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
269 x
+= (int)toolSize
.x
/ 4;
273 y
+= (int)toolSize
.y
/ 4;
276 if ( toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
- m_x
- localOrigin
.h
> maxWidth
) {
277 maxWidth
= toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
- m_x
- localOrigin
.h
;
279 if (toolbarrect
.top
+ y
+ m_yMargin
+ kwxMacToolBarTopMargin
- m_y
- localOrigin
.v
> maxHeight
) {
280 maxHeight
= toolbarrect
.top
+ y
+ m_yMargin
+ kwxMacToolBarTopMargin
- 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( WXWidget control
, wxInt16 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( (ControlHandle
) 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)
366 MacWindowToRootWindow( &lx
, &ly
) ;
370 // GetParent()->MacGetPortParams( &localOrigin , &clipRect , &window , &win ) ;
372 Rect toolbarrect
= { localOrigin
.v
,localOrigin
.h
,
373 m_height
+ localOrigin
.v
, m_width
+ localOrigin
.h
} ;
374 ControlFontStyleRec controlstyle
;
376 controlstyle
.flags
= kControlUseFontMask
;
377 controlstyle
.font
= kControlFontSmallSystemFont
;
379 wxNode
*node
= m_tools
.First();
382 wxSize toolSize
= GetToolSize() ;
390 WindowRef rootwindow
= (WindowRef
) MacGetRootWindow() ;
393 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
394 wxBitmapRefData
* bmap
= (wxBitmapRefData
*) ( tool
->GetBitmap1().GetRefData()) ;
396 if( !tool
->IsSeparator() )
398 Rect toolrect
= { toolbarrect
.top
+ m_yMargin
+ kwxMacToolBarTopMargin
, toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
, 0 , 0 } ;
399 toolrect
.right
= toolrect
.left
+ toolSize
.x
;
400 toolrect
.bottom
= toolrect
.top
+ toolSize
.y
;
402 ControlHandle m_macToolHandle
= (ControlHandle
) m_macToolHandles
[toolcount
++] ;
406 GetControlBounds( m_macToolHandle
, &contrlRect
) ;
407 int former_mac_x
= contrlRect
.left
;
408 int former_mac_y
= contrlRect
.top
;
409 int mac_x
= toolrect
.left
;
410 int mac_y
= toolrect
.top
;
412 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
415 Rect inval
= { former_mac_y
, former_mac_x
, former_mac_y
+ toolSize
.y
, former_mac_x
+ toolSize
.y
} ;
416 InvalWindowRect( rootwindow
, &inval
) ;
418 UMAMoveControl( m_macToolHandle
, mac_x
, mac_y
) ;
420 Rect inval
= { mac_y
, mac_x
, mac_y
+ toolSize
.y
, mac_x
+ toolSize
.y
} ;
421 InvalWindowRect( rootwindow
, &inval
) ;
426 x
+= (int)toolSize
.x
;
432 x
+= (int)toolSize
.x
/ 4;
434 if ( toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
- m_x
- localOrigin
.h
> maxWidth
)
435 maxWidth
= toolbarrect
.left
+ x
+ kwxMacToolBarLeftMargin
+ m_xMargin
- m_x
- localOrigin
.h
;
436 if (toolbarrect
.top
+ m_yMargin
+ kwxMacToolBarTopMargin
- m_y
- localOrigin
.v
> maxHeight
)
437 maxHeight
= toolbarrect
.top
+ kwxMacToolBarTopMargin
+ m_yMargin
- m_y
- localOrigin
.v
;
443 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
445 if ( m_maxRows
== 0 )
447 // if not set yet, only one row
451 maxHeight
+= toolSize
.y
;
452 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
453 m_maxHeight
= maxHeight
;
457 if ( noButtons
> 0 && m_maxRows
== 0 )
459 // if not set yet, have one column
463 maxWidth
+= toolSize
.x
;
464 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
465 m_maxWidth
= maxWidth
;
468 SetSize(maxWidth
, maxHeight
);
471 wxWindow::MacSuperChangedPosition() ;
474 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
476 MacClientToRootWindow( &x
, &y
) ;
477 Point pt
= { y
,x
} ;
480 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
482 if ( m_macToolHandles
[index
] )
485 GetControlBounds((ControlHandle
) m_macToolHandles
[index
], &bounds
) ;
486 if ( PtInRect( pt
, &bounds
) )
488 return (wxToolBarTool
*) (m_tools
.Nth( index
)->Data() ) ;
493 return (wxToolBarToolBase
*)NULL
;
496 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
498 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
501 return tool
->GetShortHelp() ;
506 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
511 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
512 if ( tool
->m_index
< 0 )
515 ControlHandle control
= (ControlHandle
) m_macToolHandles
[ tool
->m_index
] ;
518 UMAActivateControl( control
) ;
520 UMADeactivateControl( control
) ;
523 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
528 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
529 if ( tool
->m_index
< 0 )
532 ControlHandle control
= (ControlHandle
) m_macToolHandles
[ tool
->m_index
] ;
533 ::SetControlValue( control
, toggle
) ;
536 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
537 wxToolBarToolBase
*tool
)
539 // nothing special to do here - we really create the toolbar buttons in
546 void wxToolBar::DoSetToggle(wxToolBarToolBase
*t
, bool toggle
)
548 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
549 // TODO: set toggle state
552 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
557 void wxToolBar::OnPaint(wxPaintEvent
& event
)
560 wxMacPortSetter
helper(&dc
) ;
562 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
563 dc
.YLOG2DEVMAC(m_height
) , dc
.XLOG2DEVMAC(m_width
) } ;
564 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
567 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
569 if ( m_macToolHandles
[index
] )
571 UMADrawControl( (ControlHandle
) m_macToolHandles
[index
] ) ;
577 void wxToolBar::OnMouse( wxMouseEvent
&event
)
579 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
585 MacClientToRootWindow( &x
, &y
) ;
587 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