1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomory
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
)
135 m_windowStyle
= style
;
136 parent
->AddChild(this);
138 m_backgroundColour
= parent
->GetBackgroundColour() ;
139 m_foregroundColour
= parent
->GetForegroundColour() ;
142 m_windowId
= NewControlId();
151 AdjustForParentClientOrigin(x
, y
, wxSIZE_USE_EXISTING
);
159 wxToolBar::~wxToolBar()
162 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
164 // Delete the control as we get ghosts otherwise
165 ::DisposeControl( (ControlHandle
) m_macToolHandles
[index
] );
168 // we must refresh the frame size when the toolbar is deleted but the frame
169 // is not - otherwise toolbar leaves a hole in the place it used to occupy
172 bool wxToolBar::Realize()
174 if (m_tools
.GetCount() == 0)
179 WindowRef window
= (WindowRef
) MacGetRootWindow() ;
184 MacWindowToRootWindow( &lx
, &ly
) ;
188 // GetParent()->MacGetPortParams( &localOrigin , &clipRect , &window , &win ) ;
190 Rect toolbarrect
= { localOrigin
.v
,localOrigin
.h
,
191 m_height
+ localOrigin
.v
, m_width
+ localOrigin
.h
} ;
192 ControlFontStyleRec controlstyle
;
194 controlstyle
.flags
= kControlUseFontMask
;
195 controlstyle
.font
= kControlFontSmallSystemFont
;
197 wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
201 wxSize toolSize
= GetToolSize() ;
210 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
212 if( !tool
->IsSeparator() )
214 Rect toolrect
= { toolbarrect
.top
+ y
+ m_yMargin
+ kwxMacToolBarTopMargin
,
215 toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
, 0 , 0 } ;
216 toolrect
.right
= toolrect
.left
+ toolSize
.x
;
217 toolrect
.bottom
= toolrect
.top
+ toolSize
.y
;
219 ControlButtonContentInfo info
;
220 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap() ) ;
221 ControlHandle m_macToolHandle
;
223 SInt16 behaviour
= kControlBehaviorOffsetContents
;
224 if ( tool
->CanBeToggled() )
225 behaviour
+= kControlBehaviorToggles
;
227 if ( info
.contentType
!= kControlNoContent
)
229 m_macToolHandle
= ::NewControl( window
, &toolrect
, "\p" , false , 0 ,
230 behaviour
+ info
.contentType
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
232 ::SetControlData( m_macToolHandle
, kControlButtonPart
, kControlBevelButtonContentTag
, sizeof(info
) , (char*) &info
) ;
236 m_macToolHandle
= ::NewControl( window
, &toolrect
, "\p" , false , 0 ,
237 behaviour
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
239 UMAShowControl( m_macToolHandle
) ;
240 m_macToolHandles
.Add( m_macToolHandle
) ;
241 tool
->m_index
= m_macToolHandles
.Count() -1 ;
242 if ( !tool
->IsEnabled() )
244 UMADeactivateControl( m_macToolHandle
) ;
246 if ( tool
->CanBeToggled() && tool
->IsToggled() )
248 ::SetControl32BitValue( m_macToolHandle
, 1 ) ;
252 ::SetControl32BitValue( m_macToolHandle
, 0 ) ;
255 ::SetControlFontStyle( m_macToolHandle , &controlstyle ) ;
257 ControlHandle container
= (ControlHandle
) GetParent()->MacGetContainerForEmbedding() ;
258 wxASSERT_MSG( container
!= NULL
, wxT("No valid mac container control") ) ;
259 ::EmbedControl( m_macToolHandle
, container
) ;
261 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
263 y
+= (int)toolSize
.y
;
267 x
+= (int)toolSize
.x
;
273 m_macToolHandles
.Add( NULL
) ;
275 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
277 y
+= (int)toolSize
.y
/ 4;
281 x
+= (int)toolSize
.x
/ 4;
284 if ( toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
- m_x
- localOrigin
.h
> maxWidth
) {
285 maxWidth
= toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
- m_x
- localOrigin
.h
;
287 if (toolbarrect
.top
+ y
+ m_yMargin
+ kwxMacToolBarTopMargin
- m_y
- localOrigin
.v
> maxHeight
) {
288 maxHeight
= toolbarrect
.top
+ y
+ m_yMargin
+ kwxMacToolBarTopMargin
- m_y
- localOrigin
.v
;
290 node
= node
->GetNext();
293 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
295 if ( m_maxRows
== 0 )
297 // if not set yet, only one row
301 maxHeight
+= toolSize
.y
;
302 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
303 m_maxHeight
= maxHeight
;
307 if ( noButtons
> 0 && m_maxRows
== 0 )
309 // if not set yet, have one column
313 maxWidth
+= toolSize
.x
;
314 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
315 m_maxWidth
= maxWidth
;
318 SetSize(maxWidth
, maxHeight
);
323 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
325 m_defaultWidth
= size
.x
+4; m_defaultHeight
= size
.y
+4;
328 // The button size is bigger than the bitmap size
329 wxSize
wxToolBar::GetToolSize() const
331 return wxSize(m_defaultWidth
+ 4, m_defaultHeight
+ 4);
334 void wxToolBar::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
)
337 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
339 if ( m_macToolHandles
[index
] == (void*) control
)
341 wxToolBarTool
*tool
= (wxToolBarTool
*)m_tools
.Item( index
)->GetData();
342 if ( tool
->CanBeToggled() )
344 tool
->Toggle( GetControl32BitValue( (ControlHandle
) control
) ) ;
346 OnLeftClick( tool
->GetId() , tool
-> IsToggled() ) ;
352 void wxToolBar::SetRows(int nRows
)
354 if ( nRows
== m_maxRows
)
356 // avoid resizing the frame uselessly
363 void wxToolBar::MacSuperChangedPosition()
365 if (m_tools
.GetCount() > 0)
370 // WindowRef window ;
374 MacWindowToRootWindow( &lx
, &ly
) ;
378 // GetParent()->MacGetPortParams( &localOrigin , &clipRect , &window , &win ) ;
380 Rect toolbarrect
= { localOrigin
.v
,localOrigin
.h
,
381 m_height
+ localOrigin
.v
, m_width
+ localOrigin
.h
} ;
382 ControlFontStyleRec controlstyle
;
384 controlstyle
.flags
= kControlUseFontMask
;
385 controlstyle
.font
= kControlFontSmallSystemFont
;
387 wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
390 wxSize toolSize
= GetToolSize() ;
398 WindowRef rootwindow
= (WindowRef
) MacGetRootWindow() ;
401 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
403 if( !tool
->IsSeparator() )
405 Rect toolrect
= { toolbarrect
.top
+ m_yMargin
+ kwxMacToolBarTopMargin
, toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
, 0 , 0 } ;
406 toolrect
.right
= toolrect
.left
+ toolSize
.x
;
407 toolrect
.bottom
= toolrect
.top
+ toolSize
.y
;
409 ControlHandle m_macToolHandle
= (ControlHandle
) m_macToolHandles
[toolcount
++] ;
413 GetControlBounds( m_macToolHandle
, &contrlRect
) ;
414 int former_mac_x
= contrlRect
.left
;
415 int former_mac_y
= contrlRect
.top
;
416 int mac_x
= toolrect
.left
;
417 int mac_y
= toolrect
.top
;
419 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
422 Rect inval
= { former_mac_y
, former_mac_x
, former_mac_y
+ toolSize
.y
, former_mac_x
+ toolSize
.y
} ;
423 InvalWindowRect( rootwindow
, &inval
) ;
425 UMAMoveControl( m_macToolHandle
, mac_x
, mac_y
) ;
427 Rect inval
= { mac_y
, mac_x
, mac_y
+ toolSize
.y
, mac_x
+ toolSize
.y
} ;
428 InvalWindowRect( rootwindow
, &inval
) ;
433 x
+= (int)toolSize
.x
;
439 x
+= (int)toolSize
.x
/ 4;
441 if ( toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
- m_x
- localOrigin
.h
> maxWidth
)
442 maxWidth
= toolbarrect
.left
+ x
+ kwxMacToolBarLeftMargin
+ m_xMargin
- m_x
- localOrigin
.h
;
443 if (toolbarrect
.top
+ m_yMargin
+ kwxMacToolBarTopMargin
- m_y
- localOrigin
.v
> maxHeight
)
444 maxHeight
= toolbarrect
.top
+ kwxMacToolBarTopMargin
+ m_yMargin
- m_y
- localOrigin
.v
;
446 node
= node
->GetNext();
450 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
452 if ( m_maxRows
== 0 )
454 // if not set yet, only one row
458 maxHeight
+= toolSize
.y
;
459 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
460 m_maxHeight
= maxHeight
;
464 if ( noButtons
> 0 && m_maxRows
== 0 )
466 // if not set yet, have one column
470 maxWidth
+= toolSize
.x
;
471 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
472 m_maxWidth
= maxWidth
;
475 SetSize(maxWidth
, maxHeight
);
478 wxWindow::MacSuperChangedPosition() ;
481 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
483 MacClientToRootWindow( &x
, &y
) ;
484 Point pt
= { y
,x
} ;
487 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
489 if ( m_macToolHandles
[index
] )
492 GetControlBounds((ControlHandle
) m_macToolHandles
[index
], &bounds
) ;
493 if ( PtInRect( pt
, &bounds
) )
495 return (wxToolBarTool
*) (m_tools
.Item( index
)->GetData() ) ;
500 return (wxToolBarToolBase
*)NULL
;
503 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
505 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
508 return tool
->GetShortHelp() ;
510 return wxEmptyString
;
513 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
518 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
519 if ( tool
->m_index
< 0 )
522 ControlHandle control
= (ControlHandle
) m_macToolHandles
[ tool
->m_index
] ;
525 UMAActivateControl( control
) ;
527 UMADeactivateControl( control
) ;
530 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
535 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
536 if ( tool
->m_index
< 0 )
539 ControlHandle control
= (ControlHandle
) m_macToolHandles
[ tool
->m_index
] ;
540 ::SetControl32BitValue( control
, toggle
) ;
543 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
544 wxToolBarToolBase
*tool
)
546 // nothing special to do here - we really create the toolbar buttons in
553 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
555 wxFAIL_MSG( _T("not implemented") );
558 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*WXUNUSED(tool
))
560 wxFAIL_MSG( _T("not implemented") );
564 void wxToolBar::OnPaint(wxPaintEvent
& event
)
567 wxMacPortSetter
helper(&dc
) ;
569 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
570 dc
.YLOG2DEVMAC(m_height
) , dc
.XLOG2DEVMAC(m_width
) } ;
571 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
574 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
576 if ( m_macToolHandles
[index
] )
578 UMADrawControl( (ControlHandle
) m_macToolHandles
[index
] ) ;
584 void wxToolBar::OnMouse( wxMouseEvent
&event
)
586 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
592 MacClientToRootWindow( &x
, &y
) ;
594 ControlHandle control
;
597 WindowRef window
= (WindowRef
) MacGetRootWindow() ;
604 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
605 modifiers
|= btnState
;
607 if ( event
.m_shiftDown
)
608 modifiers
|= shiftKey
;
610 if ( event
.m_controlDown
)
611 modifiers
|= controlKey
;
613 if ( event
.m_altDown
)
614 modifiers
|= optionKey
;
616 if ( event
.m_metaDown
)
617 modifiers
|= cmdKey
;
619 controlpart
= ::FindControl( localwhere
, window
, &control
) ;
621 if ( control
&& ::IsControlActive( control
) )
624 if ( controlpart
== kControlIndicatorPart
&& !UMAHasAppearance() )
625 controlpart
= ::HandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) NULL
) ;
627 controlpart
= ::HandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) -1 ) ;
628 wxTheApp
->s_lastMouseDown
= 0 ;
629 if ( controlpart
&& ! ( ( UMAHasAppearance() || (controlpart
!= kControlIndicatorPart
) )
630 && (IsKindOf( CLASSINFO( wxScrollBar
) ) ) ) ) // otherwise we will get the event twice
632 MacHandleControlClick( control
, controlpart
) ;
640 #endif // wxUSE_TOOLBAR