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 #if TARGET_API_MAC_OSX
65 // Button height is static in Mac, can't be changed, so we need to force it here
66 if ( GetWindowVariant() == wxWINDOW_VARIANT_NORMAL
|| GetWindowVariant() == wxWINDOW_VARIANT_LARGE
)
68 bounds
.bottom
= bounds
.top
+ 20 ;
71 else if ( GetWindowVariant() == wxWINDOW_VARIANT_SMALL
)
73 bounds
.bottom
= bounds
.top
+ 17 ;
76 else if ( GetWindowVariant() == wxWINDOW_VARIANT_MINI
)
78 bounds
.bottom
= bounds
.top
+ 15 ;
83 err
= CreatePushButtonControl(
84 MAC_WXHWND(parent
->MacGetTopLevelWindowRef()),
85 &bounds
, CFSTR(""), m_peer
->GetControlRefAddr() );
89 ControlButtonContentInfo info
;
90 info
.contentType
= kControlNoContent
;
91 err
= CreateBevelButtonControl(
92 MAC_WXHWND(parent
->MacGetTopLevelWindowRef()) , &bounds
, CFSTR(""),
93 kControlBevelButtonLargeBevel
, kControlBehaviorPushbutton
,
94 &info
, 0, 0, 0, m_peer
->GetControlRefAddr() );
98 wxASSERT_MSG( m_peer
!= NULL
&& m_peer
->Ok() , wxT("No valid Mac control") ) ;
100 MacPostControlCreate( pos
, size
);
105 wxWindow
*wxButton::SetDefault()
107 wxWindow
*btnOldDefault
= wxButtonBase::SetDefault();
111 // cast needed to access the protected member
112 btnOldDefault
->GetPeer()->SetData(kControlButtonPart
, kControlPushButtonDefaultTag
, (Boolean
) 0 ) ;
115 m_peer
->SetData(kControlButtonPart
, kControlPushButtonDefaultTag
, (Boolean
) 1 ) ;
117 return btnOldDefault
;
120 wxSize
wxButton::DoGetBestSize() const
122 if ( GetId() == wxID_HELP
)
123 return wxSize( 20 , 20 ) ;
125 wxSize sz
= GetDefaultSize() ;
127 switch (GetWindowVariant())
129 case wxWINDOW_VARIANT_NORMAL
:
130 case wxWINDOW_VARIANT_LARGE
:
134 case wxWINDOW_VARIANT_SMALL
:
138 case wxWINDOW_VARIANT_MINI
:
146 Rect bestsize
= { 0 , 0 , 0 , 0 } ;
147 m_peer
->GetBestRect( &bestsize
) ;
150 if ( EmptyRect( &bestsize
) || ( GetWindowStyle() & wxBU_EXACTFIT
) )
154 ControlFontStyleRec controlFont
;
155 OSStatus err
= m_peer
->GetData
<ControlFontStyleRec
>( kControlEntireControl
, kControlFontStyleTag
, &controlFont
);
159 wxMacCFStringHolder
str( m_label
, m_font
.GetEncoding() );
162 if ( m_font
.MacGetThemeFontID() != kThemeCurrentPortFont
)
164 err
= GetThemeTextDimensions(
165 (!m_label
.empty() ? (CFStringRef
)str
: CFSTR(" ")),
166 m_font
.MacGetThemeFontID(), kThemeStateActive
, false, &bounds
, &baseline
);
172 #if wxMAC_USE_CORE_GRAPHICS
173 wxClientDC
dc(const_cast<wxButton
*>(this));
174 wxCoord width
, height
;
175 dc
.GetTextExtent( m_label
, &width
, &height
);
179 wxMacWindowStateSaver
sv( this );
180 ::TextFont( m_font
.MacGetFontNum() );
181 ::TextSize( (short)(m_font
.MacGetFontSize()) );
182 ::TextFace( m_font
.MacGetFontStyle() );
184 err
= GetThemeTextDimensions(
185 (!m_label
.empty() ? (CFStringRef
)str
: CFSTR(" ")),
186 kThemeCurrentPortFont
, kThemeStateActive
, false, &bounds
, &baseline
);
191 wBtn
= bounds
.h
+ sz
.y
;
195 wBtn
= bestsize
.right
- bestsize
.left
;
196 // non 'normal' window variants don't return the correct height
197 // sz.y = bestsize.bottom - bestsize.top ;
200 if ((wBtn
> sz
.x
) || ( GetWindowStyle() & wxBU_EXACTFIT
))
206 wxSize
wxButton::GetDefaultSize()
211 return wxSize(wBtn
, hBtn
);
214 void wxButton::Command (wxCommandEvent
& event
)
216 m_peer
->Flash(kControlButtonPart
) ;
217 ProcessCommand(event
);
220 wxInt32
wxButton::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
222 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, m_windowId
);
223 event
.SetEventObject(this);
224 ProcessCommand(event
);