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
)
136 m_windowStyle
= style
;
137 parent
->AddChild(this);
139 m_backgroundColour
= parent
->GetBackgroundColour() ;
140 m_foregroundColour
= parent
->GetForegroundColour() ;
143 m_windowId
= NewControlId();
152 AdjustForParentClientOrigin(x
, y
, wxSIZE_USE_EXISTING
);
160 wxToolBar::~wxToolBar()
163 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
165 // Delete the control as we get ghosts otherwise
166 ::DisposeControl( (ControlHandle
) m_macToolHandles
[index
] );
169 // we must refresh the frame size when the toolbar is deleted but the frame
170 // is not - otherwise toolbar leaves a hole in the place it used to occupy
173 bool wxToolBar::Realize()
175 if (m_tools
.GetCount() == 0)
180 WindowRef window
= (WindowRef
) MacGetRootWindow() ;
185 MacWindowToRootWindow( &lx
, &ly
) ;
189 // GetParent()->MacGetPortParams( &localOrigin , &clipRect , &window , &win ) ;
191 Rect toolbarrect
= { localOrigin
.v
,localOrigin
.h
,
192 m_height
+ localOrigin
.v
, m_width
+ localOrigin
.h
} ;
193 ControlFontStyleRec controlstyle
;
195 controlstyle
.flags
= kControlUseFontMask
;
196 controlstyle
.font
= kControlFontSmallSystemFont
;
198 wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
202 wxSize toolSize
= GetToolSize() ;
211 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
213 if( !tool
->IsSeparator() )
215 Rect toolrect
= { toolbarrect
.top
+ y
+ m_yMargin
+ kwxMacToolBarTopMargin
,
216 toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
, 0 , 0 } ;
217 toolrect
.right
= toolrect
.left
+ toolSize
.x
;
218 toolrect
.bottom
= toolrect
.top
+ toolSize
.y
;
220 ControlButtonContentInfo info
;
221 wxMacCreateBitmapButton( &info
, tool
->GetNormalBitmap() ) ;
222 ControlHandle m_macToolHandle
;
224 SInt16 behaviour
= kControlBehaviorOffsetContents
;
225 if ( tool
->CanBeToggled() )
226 behaviour
+= kControlBehaviorToggles
;
228 if ( info
.contentType
!= kControlNoContent
)
230 m_macToolHandle
= ::NewControl( window
, &toolrect
, "\p" , false , 0 ,
231 behaviour
+ info
.contentType
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
233 ::SetControlData( m_macToolHandle
, kControlButtonPart
, kControlBevelButtonContentTag
, sizeof(info
) , (char*) &info
) ;
237 m_macToolHandle
= ::NewControl( window
, &toolrect
, "\p" , false , 0 ,
238 behaviour
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
240 UMAShowControl( m_macToolHandle
) ;
241 m_macToolHandles
.Add( m_macToolHandle
) ;
242 tool
->m_index
= m_macToolHandles
.Count() -1 ;
243 if ( !tool
->IsEnabled() )
245 UMADeactivateControl( m_macToolHandle
) ;
247 if ( tool
->CanBeToggled() && tool
->IsToggled() )
249 ::SetControl32BitValue( m_macToolHandle
, 1 ) ;
253 ::SetControl32BitValue( m_macToolHandle
, 0 ) ;
256 ::SetControlFontStyle( m_macToolHandle , &controlstyle ) ;
258 ControlHandle container
= (ControlHandle
) GetParent()->MacGetContainerForEmbedding() ;
259 wxASSERT_MSG( container
!= NULL
, "No valid mac container control" ) ;
260 ::EmbedControl( m_macToolHandle
, container
) ;
262 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
264 y
+= (int)toolSize
.y
;
268 x
+= (int)toolSize
.x
;
274 m_macToolHandles
.Add( NULL
) ;
276 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
278 y
+= (int)toolSize
.y
/ 4;
282 x
+= (int)toolSize
.x
/ 4;
285 if ( toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
- m_x
- localOrigin
.h
> maxWidth
) {
286 maxWidth
= toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
- m_x
- localOrigin
.h
;
288 if (toolbarrect
.top
+ y
+ m_yMargin
+ kwxMacToolBarTopMargin
- m_y
- localOrigin
.v
> maxHeight
) {
289 maxHeight
= toolbarrect
.top
+ y
+ m_yMargin
+ kwxMacToolBarTopMargin
- m_y
- localOrigin
.v
;
291 node
= node
->GetNext();
294 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
296 if ( m_maxRows
== 0 )
298 // if not set yet, only one row
302 maxHeight
+= toolSize
.y
;
303 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
304 m_maxHeight
= maxHeight
;
308 if ( noButtons
> 0 && m_maxRows
== 0 )
310 // if not set yet, have one column
314 maxWidth
+= toolSize
.x
;
315 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
316 m_maxWidth
= maxWidth
;
319 SetSize(maxWidth
, maxHeight
);
324 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
326 m_defaultWidth
= size
.x
+4; m_defaultHeight
= size
.y
+4;
329 // The button size is bigger than the bitmap size
330 wxSize
wxToolBar::GetToolSize() const
332 return wxSize(m_defaultWidth
+ 4, m_defaultHeight
+ 4);
335 void wxToolBar::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
)
338 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
340 if ( m_macToolHandles
[index
] == (void*) control
)
342 wxToolBarTool
*tool
= (wxToolBarTool
*)m_tools
.Item( index
)->GetData();
343 if ( tool
->CanBeToggled() )
345 tool
->Toggle( GetControl32BitValue( (ControlHandle
) control
) ) ;
347 OnLeftClick( tool
->GetId() , tool
-> IsToggled() ) ;
353 void wxToolBar::SetRows(int nRows
)
355 if ( nRows
== m_maxRows
)
357 // avoid resizing the frame uselessly
364 void wxToolBar::MacSuperChangedPosition()
366 if (m_tools
.GetCount() > 0)
371 // WindowRef window ;
375 MacWindowToRootWindow( &lx
, &ly
) ;
379 // GetParent()->MacGetPortParams( &localOrigin , &clipRect , &window , &win ) ;
381 Rect toolbarrect
= { localOrigin
.v
,localOrigin
.h
,
382 m_height
+ localOrigin
.v
, m_width
+ localOrigin
.h
} ;
383 ControlFontStyleRec controlstyle
;
385 controlstyle
.flags
= kControlUseFontMask
;
386 controlstyle
.font
= kControlFontSmallSystemFont
;
388 wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
391 wxSize toolSize
= GetToolSize() ;
399 WindowRef rootwindow
= (WindowRef
) MacGetRootWindow() ;
402 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
404 if( !tool
->IsSeparator() )
406 Rect toolrect
= { toolbarrect
.top
+ m_yMargin
+ kwxMacToolBarTopMargin
, toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
, 0 , 0 } ;
407 toolrect
.right
= toolrect
.left
+ toolSize
.x
;
408 toolrect
.bottom
= toolrect
.top
+ toolSize
.y
;
410 ControlHandle m_macToolHandle
= (ControlHandle
) m_macToolHandles
[toolcount
++] ;
414 GetControlBounds( m_macToolHandle
, &contrlRect
) ;
415 int former_mac_x
= contrlRect
.left
;
416 int former_mac_y
= contrlRect
.top
;
417 int mac_x
= toolrect
.left
;
418 int mac_y
= toolrect
.top
;
420 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
423 Rect inval
= { former_mac_y
, former_mac_x
, former_mac_y
+ toolSize
.y
, former_mac_x
+ toolSize
.y
} ;
424 InvalWindowRect( rootwindow
, &inval
) ;
426 UMAMoveControl( m_macToolHandle
, mac_x
, mac_y
) ;
428 Rect inval
= { mac_y
, mac_x
, mac_y
+ toolSize
.y
, mac_x
+ toolSize
.y
} ;
429 InvalWindowRect( rootwindow
, &inval
) ;
434 x
+= (int)toolSize
.x
;
440 x
+= (int)toolSize
.x
/ 4;
442 if ( toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
- m_x
- localOrigin
.h
> maxWidth
)
443 maxWidth
= toolbarrect
.left
+ x
+ kwxMacToolBarLeftMargin
+ m_xMargin
- m_x
- localOrigin
.h
;
444 if (toolbarrect
.top
+ m_yMargin
+ kwxMacToolBarTopMargin
- m_y
- localOrigin
.v
> maxHeight
)
445 maxHeight
= toolbarrect
.top
+ kwxMacToolBarTopMargin
+ m_yMargin
- m_y
- localOrigin
.v
;
447 node
= node
->GetNext();
451 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
453 if ( m_maxRows
== 0 )
455 // if not set yet, only one row
459 maxHeight
+= toolSize
.y
;
460 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
461 m_maxHeight
= maxHeight
;
465 if ( noButtons
> 0 && m_maxRows
== 0 )
467 // if not set yet, have one column
471 maxWidth
+= toolSize
.x
;
472 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
473 m_maxWidth
= maxWidth
;
476 SetSize(maxWidth
, maxHeight
);
479 wxWindow::MacSuperChangedPosition() ;
482 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
484 MacClientToRootWindow( &x
, &y
) ;
485 Point pt
= { y
,x
} ;
488 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
490 if ( m_macToolHandles
[index
] )
493 GetControlBounds((ControlHandle
) m_macToolHandles
[index
], &bounds
) ;
494 if ( PtInRect( pt
, &bounds
) )
496 return (wxToolBarTool
*) (m_tools
.Item( index
)->GetData() ) ;
501 return (wxToolBarToolBase
*)NULL
;
504 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
506 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
509 return tool
->GetShortHelp() ;
514 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
519 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
520 if ( tool
->m_index
< 0 )
523 ControlHandle control
= (ControlHandle
) m_macToolHandles
[ tool
->m_index
] ;
526 UMAActivateControl( control
) ;
528 UMADeactivateControl( control
) ;
531 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
536 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
537 if ( tool
->m_index
< 0 )
540 ControlHandle control
= (ControlHandle
) m_macToolHandles
[ tool
->m_index
] ;
541 ::SetControl32BitValue( control
, toggle
) ;
544 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
545 wxToolBarToolBase
*tool
)
547 // nothing special to do here - we really create the toolbar buttons in
554 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
556 wxFAIL_MSG( _T("not implemented") );
559 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*WXUNUSED(tool
))
561 wxFAIL_MSG( _T("not implemented") );
565 void wxToolBar::OnPaint(wxPaintEvent
& event
)
568 wxMacPortSetter
helper(&dc
) ;
570 Rect toolbarrect
= { dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,
571 dc
.YLOG2DEVMAC(m_height
) , dc
.XLOG2DEVMAC(m_width
) } ;
572 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
575 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
577 if ( m_macToolHandles
[index
] )
579 UMADrawControl( (ControlHandle
) m_macToolHandles
[index
] ) ;
585 void wxToolBar::OnMouse( wxMouseEvent
&event
)
587 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
593 MacClientToRootWindow( &x
, &y
) ;
595 ControlHandle control
;
598 WindowRef window
= (WindowRef
) MacGetRootWindow() ;
605 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
606 modifiers
|= btnState
;
608 if ( event
.m_shiftDown
)
609 modifiers
|= shiftKey
;
611 if ( event
.m_controlDown
)
612 modifiers
|= controlKey
;
614 if ( event
.m_altDown
)
615 modifiers
|= optionKey
;
617 if ( event
.m_metaDown
)
618 modifiers
|= cmdKey
;
620 controlpart
= ::FindControl( localwhere
, window
, &control
) ;
622 if ( control
&& ::IsControlActive( control
) )
625 if ( controlpart
== kControlIndicatorPart
&& !UMAHasAppearance() )
626 controlpart
= ::HandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) NULL
) ;
628 controlpart
= ::HandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) -1 ) ;
629 wxTheApp
->s_lastMouseDown
= 0 ;
630 if ( controlpart
&& ! ( ( UMAHasAppearance() || (controlpart
!= kControlIndicatorPart
) )
631 && (IsKindOf( CLASSINFO( wxScrollBar
) ) ) ) ) // otherwise we will get the event twice
633 MacHandleControlClick( control
, controlpart
) ;
641 #endif // wxUSE_TOOLBAR