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"
21 #include "wx/stockitem.h"
23 #include "wx/mac/uma.h"
25 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
28 bool wxButton::Create(wxWindow
*parent
,
34 const wxValidator
& validator
,
38 if (label
.empty() && wxIsStockID(id
))
39 label
= wxGetStockLabel(id
);
41 m_macIsUserPane
= false ;
43 if ( !wxButtonBase::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
46 m_labelOrig
= m_label
= label
;
49 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
) ;
50 m_peer
= new wxMacControl(this) ;
51 if ( id
== wxID_HELP
)
53 ControlButtonContentInfo info
;
54 info
.contentType
= kControlContentIconRef
;
55 GetIconRef(kOnSystemDisk
, kSystemIconsCreator
, kHelpIcon
, &info
.u
.iconRef
);
56 err
= CreateRoundButtonControl(
57 MAC_WXHWND(parent
->MacGetTopLevelWindowRef()),
58 &bounds
, kControlRoundButtonNormalSize
,
59 &info
, m_peer
->GetControlRefAddr() );
61 else if ( label
.Find('\n' ) == wxNOT_FOUND
&& label
.Find('\r' ) == wxNOT_FOUND
)
63 #if TARGET_API_MAC_OSX
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 ;
82 err
= CreatePushButtonControl(
83 MAC_WXHWND(parent
->MacGetTopLevelWindowRef()),
84 &bounds
, CFSTR(""), m_peer
->GetControlRefAddr() );
88 ControlButtonContentInfo info
;
89 info
.contentType
= kControlNoContent
;
90 err
= CreateBevelButtonControl(
91 MAC_WXHWND(parent
->MacGetTopLevelWindowRef()) , &bounds
, CFSTR(""),
92 kControlBevelButtonLargeBevel
, kControlBehaviorPushbutton
,
93 &info
, 0, 0, 0, m_peer
->GetControlRefAddr() );
97 wxASSERT_MSG( m_peer
!= NULL
&& m_peer
->Ok() , wxT("No valid Mac control") ) ;
99 MacPostControlCreate( pos
, size
);
104 wxWindow
*wxButton::SetDefault()
106 wxWindow
*btnOldDefault
= wxButtonBase::SetDefault();
110 // cast needed to access the protected member
111 btnOldDefault
->GetPeer()->SetData(kControlButtonPart
, kControlPushButtonDefaultTag
, (Boolean
) 0 ) ;
114 m_peer
->SetData(kControlButtonPart
, kControlPushButtonDefaultTag
, (Boolean
) 1 ) ;
116 return btnOldDefault
;
119 wxSize
wxButton::DoGetBestSize() const
121 if ( GetId() == wxID_HELP
)
122 return wxSize( 20 , 20 ) ;
124 wxSize sz
= GetDefaultSize() ;
127 switch (GetWindowVariant())
129 case wxWINDOW_VARIANT_NORMAL
:
130 case wxWINDOW_VARIANT_LARGE
:
135 case wxWINDOW_VARIANT_SMALL
:
139 case wxWINDOW_VARIANT_MINI
:
147 Rect bestsize
= { 0 , 0 , 0 , 0 } ;
148 m_peer
->GetBestRect( &bestsize
) ;
151 if ( EmptyRect( &bestsize
) )
153 wBtn
= m_label
.length() * charspace
+ 12 ;
157 wBtn
= bestsize
.right
- bestsize
.left
;
158 sz
.y
= bestsize
.bottom
- bestsize
.top
;
161 if ((wBtn
> sz
.x
) || ( GetWindowStyle() & wxBU_EXACTFIT
))
167 wxSize
wxButton::GetDefaultSize()
172 return wxSize(wBtn
, hBtn
);
175 void wxButton::Command (wxCommandEvent
& event
)
177 m_peer
->Flash(kControlButtonPart
) ;
178 ProcessCommand(event
);
181 wxInt32
wxButton::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
183 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, m_windowId
);
184 event
.SetEventObject(this);
185 ProcessCommand(event
);