1 ///////////////////////////////////////////////////////////////////////////// 
   4 // Author:      Stefan Csomor 
   8 // Copyright:   (c) Stefan Csomor 
   9 // Licence:     The wxWindows licence 
  10 ///////////////////////////////////////////////////////////////////////////// 
  16 #include "wx/toolbar.h" 
  17 #include "wx/notebook.h" 
  18 #include "wx/tabctrl.h" 
  19 #include "wx/bitmap.h" 
  21 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
) 
  23 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
) 
  24     EVT_MOUSE_EVENTS( wxToolBar::OnMouse 
)  
  25     EVT_PAINT( wxToolBar::OnPaint 
)  
  28 #include "wx/mac/uma.h" 
  29 #include "wx/geometry.h" 
  30 // ---------------------------------------------------------------------------- 
  32 // ---------------------------------------------------------------------------- 
  34 class wxToolBarTool 
: public wxToolBarToolBase
 
  37     wxToolBarTool(wxToolBar 
*tbar
, 
  39                   const wxString
& label
, 
  40                   const wxBitmap
& bmpNormal
, 
  41                   const wxBitmap
& bmpDisabled
, 
  44                   const wxString
& shortHelp
, 
  45                   const wxString
& longHelp
) ; 
  47     wxToolBarTool(wxToolBar 
*tbar
, wxControl 
*control
) 
  48         : wxToolBarToolBase(tbar
, control
) 
  55         if ( m_controlHandle 
) 
  56             DisposeControl( m_controlHandle 
) ; 
  59     ControlHandle   
GetControlHandle() { return m_controlHandle 
; } 
  60     void SetControlHandle( ControlHandle handle 
) { m_controlHandle 
= handle 
; } 
  62     void SetSize(const wxSize
& size
) ; 
  63     void SetPosition( const wxPoint
& position 
) ; 
  64     wxSize 
GetSize() const 
  68             return GetControl()->GetSize() ; 
  70         else if ( IsButton() ) 
  72             return GetToolBar()->GetToolSize() ; 
  76             wxSize sz 
= GetToolBar()->GetToolSize() ; 
  82     wxPoint 
GetPosition() const 
  84         return wxPoint(m_x
, m_y
); 
  89         m_controlHandle 
= NULL 
; 
  91     ControlHandle m_controlHandle 
; 
  97 // ============================================================================ 
  99 // ============================================================================ 
 101 // ---------------------------------------------------------------------------- 
 103 // ---------------------------------------------------------------------------- 
 105 void wxToolBarTool::SetSize(const wxSize
& size
) 
 109         GetControl()->SetSize( size 
) ; 
 113 void wxToolBarTool::SetPosition(const wxPoint
& position
) 
 122         WindowRef rootwindow 
= (WindowRef
) GetToolBar()->MacGetRootWindow() ;     
 123         GetToolBar()->MacWindowToRootWindow( &x 
, &y 
) ; 
 124         int mac_x 
= x 
+ position
.x 
; 
 125         int mac_y 
= y 
+ position
.y 
; 
 129         GetControlBounds( m_controlHandle 
, &contrlRect 
) ;  
 130         int former_mac_x 
= contrlRect
.left 
; 
 131         int former_mac_y 
= contrlRect
.top 
; 
 132         wxSize sz 
= GetToolBar()->GetToolSize() ; 
 134         if ( mac_x 
!= former_mac_x 
|| mac_y 
!= former_mac_y 
) 
 137                 Rect inval 
= { former_mac_y 
, former_mac_x 
, former_mac_y 
+ sz
.y 
, former_mac_x 
+ sz
.x 
} ; 
 138                 InvalWindowRect( rootwindow 
, &inval 
) ; 
 140             UMAMoveControl( m_controlHandle 
, mac_x 
, mac_y 
) ; 
 142                 Rect inval 
= { mac_y 
, mac_x 
, mac_y 
+ sz
.y 
, mac_x 
+ sz
.x 
} ; 
 143                 InvalWindowRect( rootwindow 
, &inval 
) ; 
 147     else if ( IsControl() ) 
 149         GetControl()->Move( position 
) ; 
 153 const short kwxMacToolBarToolDefaultWidth 
= 24 ; 
 154 const short kwxMacToolBarToolDefaultHeight 
= 22 ; 
 155 const short kwxMacToolBarTopMargin 
= 2 ; 
 156 const short kwxMacToolBarLeftMargin 
= 2 ; 
 158 wxToolBarTool::wxToolBarTool(wxToolBar 
*tbar
, 
 160   const wxString
& label
, 
 161   const wxBitmap
& bmpNormal
, 
 162   const wxBitmap
& bmpDisabled
, 
 164   wxObject 
*clientData
, 
 165   const wxString
& shortHelp
, 
 166   const wxString
& longHelp
) 
 167         : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
, 
 168                             clientData
, shortHelp
, longHelp
) 
 172     if (id 
== wxID_SEPARATOR
) return; 
 174     WindowRef window 
= (WindowRef
) tbar
->MacGetRootWindow() ;     
 175     wxSize toolSize 
= tbar
->GetToolSize() ;     
 176     Rect toolrect 
= { 0, 0 , toolSize
.y 
, toolSize
.x 
} ; 
 178     ControlButtonContentInfo info 
; 
 179     wxMacCreateBitmapButton( &info 
, GetNormalBitmap() ) ; 
 181     SInt16 behaviour 
= kControlBehaviorOffsetContents 
; 
 182     if ( CanBeToggled() ) 
 183         behaviour 
+= kControlBehaviorToggles 
; 
 185     if ( info
.contentType 
!= kControlNoContent 
)  
 187         m_controlHandle 
= ::NewControl( window 
, &toolrect 
, "\p" , false , 0 ,  
 188                                         behaviour 
+ info
.contentType 
, 0 , kControlBevelButtonNormalBevelProc 
, (long) this ) ; 
 190         ::SetControlData( m_controlHandle 
, kControlButtonPart 
, kControlBevelButtonContentTag 
, sizeof(info
) , (char*) &info 
) ; 
 194         m_controlHandle 
= ::NewControl( window 
, &toolrect 
, "\p" , false , 0 ,  
 195                                         behaviour  
, 0 , kControlBevelButtonNormalBevelProc 
, (long) this ) ; 
 197     UMAShowControl( m_controlHandle 
) ; 
 200         UMADeactivateControl( m_controlHandle 
) ; 
 202     if ( CanBeToggled() && IsToggled() ) 
 204         ::SetControl32BitValue( m_controlHandle 
, 1 ) ; 
 208         ::SetControl32BitValue( m_controlHandle 
, 0 ) ; 
 211     ControlHandle container 
= (ControlHandle
) tbar
->MacGetContainerForEmbedding() ; 
 212     wxASSERT_MSG( container 
!= NULL 
, wxT("No valid mac container control") ) ; 
 213     ::EmbedControl( m_controlHandle 
, container 
) ; 
 217 wxToolBarToolBase 
*wxToolBar::CreateTool(int id
, 
 218                                          const wxString
& label
, 
 219                                          const wxBitmap
& bmpNormal
, 
 220                                          const wxBitmap
& bmpDisabled
, 
 222                                          wxObject 
*clientData
, 
 223                                          const wxString
& shortHelp
, 
 224                                          const wxString
& longHelp
) 
 226     return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
, 
 227                              clientData
, shortHelp
, longHelp
); 
 230 wxToolBarToolBase 
*wxToolBar::CreateTool(wxControl 
*control
) 
 232     return new wxToolBarTool(this, control
); 
 235 void wxToolBar::Init() 
 239     m_defaultWidth 
= kwxMacToolBarToolDefaultWidth
; 
 240     m_defaultHeight 
= kwxMacToolBarToolDefaultHeight
; 
 243 bool wxToolBar::Create(wxWindow 
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, 
 244             long style
, const wxString
& name
) 
 262     m_windowStyle 
= style
; 
 263     parent
->AddChild(this); 
 265     m_backgroundColour 
= parent
->GetBackgroundColour() ; 
 266     m_foregroundColour 
= parent
->GetForegroundColour() ; 
 269         m_windowId 
= NewControlId(); 
 278         AdjustForParentClientOrigin(x
, y
, wxSIZE_USE_EXISTING
); 
 286 wxToolBar::~wxToolBar() 
 288     // we must refresh the frame size when the toolbar is deleted but the frame 
 289     // is not - otherwise toolbar leaves a hole in the place it used to occupy 
 292 bool wxToolBar::Realize() 
 294     if (m_tools
.GetCount() == 0) 
 297     int x 
= m_xMargin 
+ kwxMacToolBarLeftMargin 
; 
 298     int y 
= m_yMargin 
+ kwxMacToolBarTopMargin 
; 
 306     int maxToolWidth 
= 0; 
 307     int maxToolHeight 
= 0; 
 309     // Find the maximum tool width and height 
 310     wxToolBarToolsList::Node 
*node 
= m_tools
.GetFirst(); 
 313         wxToolBarTool 
*tool 
= (wxToolBarTool 
*)node
->GetData(); 
 314         wxSize sz 
= tool
->GetSize() ; 
 316         if ( sz
.x 
> maxToolWidth 
) 
 317             maxToolWidth 
= sz
.x 
; 
 318         if (sz
.y
> maxToolHeight
) 
 319             maxToolHeight 
= sz
.y
; 
 321         node 
= node
->GetNext(); 
 324     node 
= m_tools
.GetFirst(); 
 327         wxToolBarTool 
*tool 
= (wxToolBarTool 
*)node
->GetData(); 
 328         wxSize cursize 
= tool
->GetSize() ; 
 330         // for the moment we just do a single row/column alignement 
 331         if ( x 
+ cursize
.x 
> maxWidth 
) 
 332             maxWidth 
= x 
+ cursize
.x 
; 
 333         if ( y 
+ cursize
.y 
> maxHeight 
) 
 334             maxHeight 
= y 
+ cursize
.y 
; 
 336         tool
->SetPosition( wxPoint( x 
, y 
) ) ; 
 338         if ( GetWindowStyleFlag() & wxTB_VERTICAL 
) 
 347         node 
= node
->GetNext(); 
 350     if ( GetWindowStyleFlag() & wxTB_HORIZONTAL 
) 
 352         if ( m_maxRows 
== 0 ) 
 354             // if not set yet, only one row 
 358         maxHeight 
+= m_yMargin 
+ kwxMacToolBarTopMargin
; 
 359         m_maxHeight 
= maxHeight 
; 
 363         if ( GetToolsCount() > 0 && m_maxRows 
== 0 ) 
 365             // if not set yet, have one column 
 366             SetRows(GetToolsCount()); 
 369         maxWidth 
+= m_xMargin 
+ kwxMacToolBarLeftMargin
; 
 370         m_maxWidth 
= maxWidth 
; 
 373     SetSize(maxWidth
, maxHeight
); 
 374     InvalidateBestSize(); 
 379 void wxToolBar::SetToolBitmapSize(const wxSize
& size
) 
 381     m_defaultWidth 
= size
.x
+4; m_defaultHeight 
= size
.y
+4; 
 384 // The button size is bigger than the bitmap size 
 385 wxSize 
wxToolBar::GetToolSize() const 
 387     return wxSize(m_defaultWidth 
+ 4, m_defaultHeight 
+ 4); 
 390 void wxToolBar::MacHandleControlClick( WXWidget control 
, wxInt16 controlpart 
, bool WXUNUSED( mouseStillDown 
) )  
 392     wxToolBarToolsList::Node 
*node
; 
 393     for ( node 
= m_tools
.GetFirst(); node
; node 
= node
->GetNext() ) 
 395         wxToolBarTool
* tool 
= (wxToolBarTool
*) node
->GetData() ;  
 396         if ( tool
->IsButton() ) 
 398            if( (WXWidget
) tool
->GetControlHandle() == control 
)  
 400                 if ( tool
->CanBeToggled() ) 
 402                     tool
->Toggle( GetControl32BitValue( (ControlHandle
) control 
) ) ; 
 404                 OnLeftClick( tool
->GetId() , tool 
-> IsToggled() ) ; 
 411 void wxToolBar::SetRows(int nRows
) 
 413     if ( nRows 
== m_maxRows 
) 
 415         // avoid resizing the frame uselessly 
 422 void wxToolBar::MacSuperChangedPosition()  
 424     wxWindow::MacSuperChangedPosition() ; 
 428 wxToolBarToolBase 
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const 
 430     wxToolBarToolsList::Node 
*node 
= m_tools
.GetFirst(); 
 433         wxToolBarTool 
*tool 
= (wxToolBarTool 
*)node
->GetData() ; 
 434         wxRect2DInt 
r( tool
->GetPosition() , tool
->GetSize() ) ; 
 435         if ( r
.Contains( wxPoint( x 
, y 
) ) ) 
 440         node 
= node
->GetNext(); 
 443     return (wxToolBarToolBase 
*)NULL
; 
 446 wxString 
wxToolBar::MacGetToolTipString( wxPoint 
&pt 
) 
 448     wxToolBarToolBase
* tool 
= FindToolForPosition( pt
.x 
, pt
.y 
) ; 
 451         return tool
->GetShortHelp() ; 
 453     return wxEmptyString 
; 
 456 void wxToolBar::DoEnableTool(wxToolBarToolBase 
*t
, bool enable
) 
 461     wxToolBarTool 
*tool 
= (wxToolBarTool 
*)t
; 
 462     if ( tool
->IsControl() ) 
 464         tool
->GetControl()->Enable( enable 
) ; 
 466     else if ( tool
->IsButton() ) 
 469             UMAActivateControl( tool
->GetControlHandle() ) ; 
 471             UMADeactivateControl( tool
->GetControlHandle() ) ; 
 475 void wxToolBar::DoToggleTool(wxToolBarToolBase 
*t
, bool toggle
) 
 480     wxToolBarTool 
*tool 
= (wxToolBarTool 
*)t
; 
 481     if ( tool
->IsButton() ) 
 483         ::SetControl32BitValue( tool
->GetControlHandle() , toggle 
) ; 
 487 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), 
 488                              wxToolBarToolBase 
*tool
) 
 490     // nothing special to do here - we relayout in Realize() later 
 492     InvalidateBestSize(); 
 497 void wxToolBar::DoSetToggle(wxToolBarToolBase 
*WXUNUSED(tool
), bool WXUNUSED(toggle
)) 
 499     wxFAIL_MSG( _T("not implemented") ); 
 502 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase 
*tool
) 
 504     wxToolBarToolsList::Node 
*node
; 
 505     for ( node 
= m_tools
.GetFirst(); node
; node 
= node
->GetNext() ) 
 507         wxToolBarToolBase 
*tool2 
= node
->GetData(); 
 510             // let node point to the next node in the list 
 511             node 
= node
->GetNext(); 
 517     wxSize sz 
= ((wxToolBarTool
*)tool
)->GetSize() ; 
 521     // and finally reposition all the controls after this one 
 523     for ( /* node -> first after deleted */ ; node
; node 
= node
->GetNext() ) 
 525         wxToolBarTool 
*tool2 
= (wxToolBarTool
*) node
->GetData(); 
 526         wxPoint pt 
= tool2
->GetPosition() ; 
 528         if ( GetWindowStyleFlag() & wxTB_VERTICAL 
) 
 536         tool2
->SetPosition( pt 
) ; 
 539     InvalidateBestSize(); 
 543 void wxToolBar::OnPaint(wxPaintEvent
& event
) 
 546     wxMacPortSetter 
helper(&dc
) ; 
 548     Rect toolbarrect 
= { dc
.YLOG2DEVMAC(0) , dc
.XLOG2DEVMAC(0) ,  
 549         dc
.YLOG2DEVMAC(m_height
) , dc
.XLOG2DEVMAC(m_width
) } ; 
 550     UMADrawThemePlacard( &toolbarrect 
, IsEnabled() ? kThemeStateActive 
: kThemeStateInactive
) ; 
 552         wxToolBarToolsList::Node 
*node
; 
 553         for ( node 
= m_tools
.GetFirst(); node
; node 
= node
->GetNext() ) 
 555             wxToolBarTool
* tool 
= (wxToolBarTool
*) node
->GetData() ;  
 556             if ( tool
->IsButton() ) 
 558                UMADrawControl( tool
->GetControlHandle() ) ; 
 564 void  wxToolBar::OnMouse( wxMouseEvent 
&event 
)  
 566     if (event
.GetEventType() == wxEVT_LEFT_DOWN 
|| event
.GetEventType() == wxEVT_LEFT_DCLICK 
) 
 572         MacClientToRootWindow( &x 
, &y 
) ; 
 574         ControlHandle   control 
; 
 577         WindowRef   window 
= (WindowRef
) MacGetRootWindow() ; 
 584         if ( !event
.m_leftDown 
&& !event
.m_rightDown 
) 
 585             modifiers  
|= btnState 
; 
 587         if ( event
.m_shiftDown 
) 
 588             modifiers 
|= shiftKey 
; 
 590         if ( event
.m_controlDown 
) 
 591             modifiers 
|= controlKey 
; 
 593         if ( event
.m_altDown 
) 
 594             modifiers 
|= optionKey 
; 
 596         if ( event
.m_metaDown 
) 
 597             modifiers 
|= cmdKey 
; 
 599         controlpart 
= ::FindControl( localwhere 
, window 
, &control 
) ; 
 601             if ( control 
&& ::IsControlActive( control 
) ) 
 604                     controlpart 
= ::HandleControlClick( control 
, localwhere 
, modifiers 
, (ControlActionUPP
) -1 ) ; 
 605                     wxTheApp
->s_lastMouseDown 
= 0 ; 
 606                     if ( control 
&& controlpart 
!= kControlNoPart 
) // otherwise we will get the event twice 
 608                         MacHandleControlClick( (WXWidget
) control 
, controlpart 
, false /* not down anymore */ ) ; 
 616 #endif // wxUSE_TOOLBAR