1 ///////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/mac/carbon/button.cpp 
   4 // Author:      Stefan Csomor 
   8 // Copyright:   (c) Stefan Csomor 
   9 // Licence:     wxWindows licence 
  10 ///////////////////////////////////////////////////////////////////////////// 
  12 #include "wx/wxprec.h" 
  14 #include "wx/button.h" 
  18     #include "wx/toplevel.h" 
  19     #include "wx/dcclient.h" 
  22 #include "wx/stockitem.h" 
  24 #include "wx/mac/uma.h" 
  26 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
) 
  29 bool wxButton::Create(wxWindow 
*parent
, 
  35     const wxValidator
& validator
, 
  39     if (label
.empty() && wxIsStockID(id
)) 
  40         label 
= wxGetStockLabel(id
); 
  42     m_macIsUserPane 
= false ; 
  44     if ( !wxButtonBase::Create(parent
, id
, pos
, size
, style
, validator
, name
) ) 
  47     m_labelOrig 
= m_label 
= label 
; 
  50     Rect bounds 
= wxMacGetBoundsForControl( this , pos 
, size 
) ; 
  51     m_peer 
= new wxMacControl(this) ; 
  52     if ( id 
== wxID_HELP 
) 
  54         ControlButtonContentInfo info 
; 
  55         info
.contentType 
= kControlContentIconRef 
; 
  56         GetIconRef(kOnSystemDisk
, kSystemIconsCreator
, kHelpIcon
, &info
.u
.iconRef
); 
  57         err 
= CreateRoundButtonControl( 
  58             MAC_WXHWND(parent
->MacGetTopLevelWindowRef()), 
  59             &bounds
, kControlRoundButtonNormalSize
, 
  60             &info
, m_peer
->GetControlRefAddr() ); 
  62     else if ( label
.Find('\n' ) == wxNOT_FOUND 
&& label
.Find('\r' ) == wxNOT_FOUND
) 
  64         // Button height is static in Mac, can't be changed, so we need to force it here 
  65         if ( GetWindowVariant() == wxWINDOW_VARIANT_NORMAL 
|| GetWindowVariant() == wxWINDOW_VARIANT_LARGE 
) 
  67             bounds
.bottom 
= bounds
.top 
+ 20 ; 
  70         else if ( GetWindowVariant() == wxWINDOW_VARIANT_SMALL 
) 
  72             bounds
.bottom 
= bounds
.top 
+ 17 ; 
  75         else if ( GetWindowVariant() == wxWINDOW_VARIANT_MINI 
) 
  77             bounds
.bottom 
= bounds
.top 
+ 15 ; 
  80         err 
= CreatePushButtonControl( 
  81             MAC_WXHWND(parent
->MacGetTopLevelWindowRef()), 
  82             &bounds
, CFSTR(""), m_peer
->GetControlRefAddr() ); 
  86         ControlButtonContentInfo info 
; 
  87         info
.contentType 
= kControlNoContent 
; 
  88         err 
= CreateBevelButtonControl( 
  89             MAC_WXHWND(parent
->MacGetTopLevelWindowRef()) , &bounds
, CFSTR(""), 
  90             kControlBevelButtonLargeBevel
, kControlBehaviorPushbutton
, 
  91             &info
, 0, 0, 0, m_peer
->GetControlRefAddr() ); 
  95     wxASSERT_MSG( m_peer 
!= NULL 
&& m_peer
->Ok() , wxT("No valid Mac control") ) ; 
  97     MacPostControlCreate( pos
, size 
); 
 102 wxWindow 
*wxButton::SetDefault() 
 104     wxWindow 
*btnOldDefault 
= wxButtonBase::SetDefault(); 
 108         // cast needed to access the protected member 
 109         btnOldDefault
->GetPeer()->SetData(kControlButtonPart 
, kControlPushButtonDefaultTag 
, (Boolean
) 0 ) ; 
 112     m_peer
->SetData(kControlButtonPart 
, kControlPushButtonDefaultTag 
, (Boolean
) 1 ) ; 
 114     return btnOldDefault
; 
 117 wxSize 
wxButton::DoGetBestSize() const 
 119     if ( GetId() == wxID_HELP 
) 
 120         return wxSize( 20 , 20 ) ; 
 122     wxSize sz 
= GetDefaultSize() ; 
 124     switch (GetWindowVariant()) 
 126         case wxWINDOW_VARIANT_NORMAL
: 
 127         case wxWINDOW_VARIANT_LARGE
: 
 131         case wxWINDOW_VARIANT_SMALL
: 
 135         case wxWINDOW_VARIANT_MINI
: 
 143     Rect    bestsize 
= { 0 , 0 , 0 , 0 } ; 
 144     m_peer
->GetBestRect( &bestsize 
) ; 
 147     if ( EmptyRect( &bestsize 
) || ( GetWindowStyle() & wxBU_EXACTFIT
) ) 
 151         ControlFontStyleRec controlFont
; 
 152         OSStatus err 
= m_peer
->GetData
<ControlFontStyleRec
>( kControlEntireControl
, kControlFontStyleTag
, &controlFont 
); 
 155         wxCFStringRef 
str( m_label
,  m_font
.GetEncoding() ); 
 157 #if wxMAC_USE_ATSU_TEXT 
 159         if ( m_font
.MacGetThemeFontID() != kThemeCurrentPortFont 
) 
 161             err 
= GetThemeTextDimensions( 
 162                 (!m_label
.empty() ? (CFStringRef
)str 
: CFSTR(" ")), 
 163                 m_font
.MacGetThemeFontID(), kThemeStateActive
, false, &bounds
, &baseline 
); 
 169             wxClientDC 
dc(const_cast<wxButton
*>(this)); 
 170             wxCoord width
, height 
; 
 171             dc
.GetTextExtent( m_label 
, &width
, &height
); 
 176         wBtn 
= bounds
.h 
+ sz
.y
; 
 180         wBtn 
= bestsize
.right 
- bestsize
.left 
; 
 181         // non 'normal' window variants don't return the correct height 
 182         // sz.y = bestsize.bottom - bestsize.top ; 
 185     if ((wBtn 
> sz
.x
) || ( GetWindowStyle() & wxBU_EXACTFIT
)) 
 191 wxSize 
wxButton::GetDefaultSize() 
 196     return wxSize(wBtn
, hBtn
); 
 199 void wxButton::Command (wxCommandEvent 
& event
) 
 201     m_peer
->Flash(kControlButtonPart
) ; 
 202     ProcessCommand(event
); 
 205 wxInt32 
wxButton::MacControlHit(WXEVENTHANDLERREF 
WXUNUSED(handler
) , WXEVENTREF 
WXUNUSED(event
) ) 
 207     wxCommandEvent 
event(wxEVT_COMMAND_BUTTON_CLICKED
, m_windowId
); 
 208     event
.SetEventObject(this); 
 209     ProcessCommand(event
); 
 214 //------------------------------------------------------- 
 215 // wxDisclosureTriangle 
 216 //------------------------------------------------------- 
 218 bool wxDisclosureTriangle::Create(wxWindow 
*parent
, wxWindowID id
, const wxString
& label
, 
 219    const wxPoint
& pos
, const wxSize
& size
, long style
,const wxValidator
& validator
, const wxString
& name 
) 
 221     m_macIsUserPane 
= false ; 
 223     if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) ) 
 226     Rect bounds 
= wxMacGetBoundsForControl( this , pos 
, size 
) ; 
 227     m_peer 
= new wxMacControl(this) ; 
 229     OSStatus err 
= CreateDisclosureTriangleControl( 
 230             MAC_WXHWND(parent
->MacGetTopLevelWindowRef()) , &bounds
,  
 231             kControlDisclosureTrianglePointDefault
, 
 232             wxCFStringRef( label 
), 
 235             TRUE
, // auto toggle back and forth 
 236             m_peer
->GetControlRefAddr() ); 
 239     wxASSERT_MSG( m_peer 
!= NULL 
&& m_peer
->Ok() , wxT("No valid Mac control") ) ; 
 241     MacPostControlCreate( pos
, size 
); 
 242     // passing the text in the param doesn't seem to work, so lets do if again 
 248 void wxDisclosureTriangle::SetOpen( bool open 
) 
 250     m_peer
->SetValue( open 
? 1 : 0 ); 
 253 bool wxDisclosureTriangle::IsOpen() const 
 255    return m_peer
->GetValue() == 1; 
 258 wxInt32 
wxDisclosureTriangle::MacControlHit( WXEVENTHANDLERREF 
WXUNUSED(handler
) , WXEVENTREF 
WXUNUSED(event
) ) 
 260     // Just emit button event for now 
 261     wxCommandEvent 
event(wxEVT_COMMAND_BUTTON_CLICKED
, m_windowId
); 
 262     event
.SetEventObject(this); 
 263     ProcessCommand(event
); 
 268 wxSize 
wxDisclosureTriangle::DoGetBestSize() const 
 270     return wxSize(16,16);