]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/button.cpp
Border corrections
[wxWidgets.git] / src / mac / carbon / button.cpp
CommitLineData
e9576ca5 1/////////////////////////////////////////////////////////////////////////////
8e609c82 2// Name: src/mac/carbon/button.cpp
e9576ca5 3// Purpose: wxButton
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
8e609c82 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
a8e9860d 12#include "wx/wxprec.h"
d8c736e5 13
e9576ca5 14#include "wx/button.h"
8e609c82
WS
15
16#ifndef WX_PRECOMP
17 #include "wx/panel.h"
b84aec03 18 #include "wx/toplevel.h"
6239ee05 19 #include "wx/dcclient.h"
8e609c82
WS
20#endif
21
5f7bcb48 22#include "wx/stockitem.h"
e9576ca5 23
25eb6881
DS
24#include "wx/mac/uma.h"
25
e9576ca5 26IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
e9576ca5 27
e9576ca5 28
25eb6881
DS
29bool wxButton::Create(wxWindow *parent,
30 wxWindowID id,
31 const wxString& lbl,
32 const wxPoint& pos,
33 const wxSize& size,
34 long style,
35 const wxValidator& validator,
36 const wxString& name)
e9576ca5 37{
5f7bcb48
VS
38 wxString label(lbl);
39 if (label.empty() && wxIsStockID(id))
40 label = wxGetStockLabel(id);
25eb6881
DS
41
42 m_macIsUserPane = false ;
43
b45ed7a2
VZ
44 if ( !wxButtonBase::Create(parent, id, pos, size, style, validator, name) )
45 return false;
25eb6881 46
ab0f37b9 47 m_labelOrig = m_label = label ;
e40298d5 48
2e91d506 49 OSStatus err;
facd6764 50 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
b905d6cc 51 m_peer = new wxMacControl(this) ;
49e3e2c2
SC
52 if ( id == wxID_HELP )
53 {
54 ControlButtonContentInfo info ;
55 info.contentType = kControlContentIconRef ;
56 GetIconRef(kOnSystemDisk, kSystemIconsCreator, kHelpIcon, &info.u.iconRef);
2e91d506
DS
57 err = CreateRoundButtonControl(
58 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
59 &bounds, kControlRoundButtonNormalSize,
60 &info, m_peer->GetControlRefAddr() );
49e3e2c2
SC
61 }
62 else if ( label.Find('\n' ) == wxNOT_FOUND && label.Find('\r' ) == wxNOT_FOUND)
eed9e479 63 {
2a4c6734 64#if TARGET_API_MAC_OSX
25eb6881 65 // Button height is static in Mac, can't be changed, so we need to force it here
2a4c6734
SC
66 if ( GetWindowVariant() == wxWINDOW_VARIANT_NORMAL || GetWindowVariant() == wxWINDOW_VARIANT_LARGE )
67 {
68 bounds.bottom = bounds.top + 20 ;
69 m_maxHeight = 20 ;
70 }
71 else if ( GetWindowVariant() == wxWINDOW_VARIANT_SMALL )
72 {
73 bounds.bottom = bounds.top + 17 ;
74 m_maxHeight = 17 ;
75 }
76 else if ( GetWindowVariant() == wxWINDOW_VARIANT_MINI )
77 {
78 bounds.bottom = bounds.top + 15 ;
79 m_maxHeight = 15 ;
80 }
81#endif
25eb6881 82
2e91d506
DS
83 err = CreatePushButtonControl(
84 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
85 &bounds, CFSTR(""), m_peer->GetControlRefAddr() );
eed9e479
SC
86 }
87 else
88 {
89 ControlButtonContentInfo info ;
90 info.contentType = kControlNoContent ;
2e91d506
DS
91 err = CreateBevelButtonControl(
92 MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds, CFSTR(""),
93 kControlBevelButtonLargeBevel, kControlBehaviorPushbutton,
94 &info, 0, 0, 0, m_peer->GetControlRefAddr() );
eed9e479 95 }
25eb6881 96
2e91d506 97 verify_noerr( err );
25eb6881
DS
98 wxASSERT_MSG( m_peer != NULL && m_peer->Ok() , wxT("No valid Mac control") ) ;
99
2e91d506 100 MacPostControlCreate( pos, size );
25eb6881
DS
101
102 return true;
e9576ca5
SC
103}
104
75722dba 105wxWindow *wxButton::SetDefault()
e9576ca5 106{
75722dba 107 wxWindow *btnOldDefault = wxButtonBase::SetDefault();
03e11df5 108
21fd5529 109 if ( btnOldDefault )
75722dba
VZ
110 {
111 // cast needed to access the protected member
112 btnOldDefault->GetPeer()->SetData(kControlButtonPart , kControlPushButtonDefaultTag , (Boolean) 0 ) ;
113 }
25eb6881 114
21fd5529 115 m_peer->SetData(kControlButtonPart , kControlPushButtonDefaultTag , (Boolean) 1 ) ;
75722dba
VZ
116
117 return btnOldDefault;
e9576ca5
SC
118}
119
37e2cb08 120wxSize wxButton::DoGetBestSize() const
51abe921 121{
49e3e2c2
SC
122 if ( GetId() == wxID_HELP )
123 return wxSize( 20 , 20 ) ;
6d325d81 124
25eb6881 125 wxSize sz = GetDefaultSize() ;
25eb6881
DS
126
127 switch (GetWindowVariant())
6d325d81 128 {
f3078f07
DS
129 case wxWINDOW_VARIANT_NORMAL:
130 case wxWINDOW_VARIANT_LARGE:
25eb6881 131 sz.y = 20 ;
25eb6881
DS
132 break;
133
f3078f07 134 case wxWINDOW_VARIANT_SMALL:
25eb6881
DS
135 sz.y = 17 ;
136 break;
137
f3078f07 138 case wxWINDOW_VARIANT_MINI:
25eb6881
DS
139 sz.y = 15 ;
140 break;
141
142 default:
143 break;
6d325d81 144 }
25eb6881 145
6d325d81 146 Rect bestsize = { 0 , 0 , 0 , 0 } ;
5ca0d812 147 m_peer->GetBestRect( &bestsize ) ;
8e609c82 148
23866d59 149 int wBtn;
6239ee05 150 if ( EmptyRect( &bestsize ) || ( GetWindowStyle() & wxBU_EXACTFIT) )
6d325d81 151 {
6239ee05
SC
152 Point bounds;
153
154 ControlFontStyleRec controlFont;
155 OSStatus err = m_peer->GetData<ControlFontStyleRec>( kControlEntireControl, kControlFontStyleTag, &controlFont );
156 verify_noerr( err );
157
158 SInt16 baseline;
159 wxMacCFStringHolder str( m_label, m_font.GetEncoding() );
160
161#ifndef __LP64__
162 if ( m_font.MacGetThemeFontID() != kThemeCurrentPortFont )
163 {
164 err = GetThemeTextDimensions(
165 (!m_label.empty() ? (CFStringRef)str : CFSTR(" ")),
166 m_font.MacGetThemeFontID(), kThemeStateActive, false, &bounds, &baseline );
167 verify_noerr( err );
168 }
169 else
170#endif
171 {
172#if wxMAC_USE_CORE_GRAPHICS
173 wxClientDC dc(const_cast<wxButton*>(this));
174 wxCoord width, height ;
175 dc.GetTextExtent( m_label , &width, &height);
176 bounds.h = width;
177 bounds.v = height;
178#else
179 wxMacWindowStateSaver sv( this );
180 ::TextFont( m_font.MacGetFontNum() );
181 ::TextSize( (short)(m_font.MacGetFontSize()) );
182 ::TextFace( m_font.MacGetFontStyle() );
183
184 err = GetThemeTextDimensions(
185 (!m_label.empty() ? (CFStringRef)str : CFSTR(" ")),
186 kThemeCurrentPortFont, kThemeStateActive, false, &bounds, &baseline );
187 verify_noerr( err );
188#endif
189 }
190
191 wBtn = bounds.h + sz.y;
6d325d81
SC
192 }
193 else
194 {
5ca0d812 195 wBtn = bestsize.right - bestsize.left ;
6239ee05
SC
196 // non 'normal' window variants don't return the correct height
197 // sz.y = bestsize.bottom - bestsize.top ;
6d325d81 198 }
25eb6881 199
8e609c82 200 if ((wBtn > sz.x) || ( GetWindowStyle() & wxBU_EXACTFIT))
6d325d81
SC
201 sz.x = wBtn;
202
203 return sz ;
51abe921
SC
204}
205
7c74e7fe
SC
206wxSize wxButton::GetDefaultSize()
207{
8e609c82 208 int wBtn = 70 ;
e40298d5 209 int hBtn = 20 ;
25eb6881 210
7c74e7fe
SC
211 return wxSize(wBtn, hBtn);
212}
213
519cb848 214void wxButton::Command (wxCommandEvent & event)
e9576ca5 215{
21fd5529 216 m_peer->Flash(kControlButtonPart) ;
25eb6881 217 ProcessCommand(event);
e9576ca5
SC
218}
219
8e609c82 220wxInt32 wxButton::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
e9576ca5 221{
2e91d506 222 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, m_windowId);
4c37f124
SC
223 event.SetEventObject(this);
224 ProcessCommand(event);
25eb6881
DS
225
226 return noErr;
e9576ca5 227}