]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/button.cpp
DC reorganization
[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 {
25eb6881 64 // Button height is static in Mac, can't be changed, so we need to force it here
2a4c6734
SC
65 if ( GetWindowVariant() == wxWINDOW_VARIANT_NORMAL || GetWindowVariant() == wxWINDOW_VARIANT_LARGE )
66 {
67 bounds.bottom = bounds.top + 20 ;
68 m_maxHeight = 20 ;
69 }
70 else if ( GetWindowVariant() == wxWINDOW_VARIANT_SMALL )
71 {
72 bounds.bottom = bounds.top + 17 ;
73 m_maxHeight = 17 ;
74 }
75 else if ( GetWindowVariant() == wxWINDOW_VARIANT_MINI )
76 {
77 bounds.bottom = bounds.top + 15 ;
78 m_maxHeight = 15 ;
79 }
2e91d506
DS
80 err = CreatePushButtonControl(
81 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
82 &bounds, CFSTR(""), m_peer->GetControlRefAddr() );
eed9e479
SC
83 }
84 else
85 {
86 ControlButtonContentInfo info ;
87 info.contentType = kControlNoContent ;
2e91d506
DS
88 err = CreateBevelButtonControl(
89 MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds, CFSTR(""),
90 kControlBevelButtonLargeBevel, kControlBehaviorPushbutton,
91 &info, 0, 0, 0, m_peer->GetControlRefAddr() );
eed9e479 92 }
25eb6881 93
2e91d506 94 verify_noerr( err );
25eb6881
DS
95 wxASSERT_MSG( m_peer != NULL && m_peer->Ok() , wxT("No valid Mac control") ) ;
96
2e91d506 97 MacPostControlCreate( pos, size );
25eb6881
DS
98
99 return true;
e9576ca5
SC
100}
101
75722dba 102wxWindow *wxButton::SetDefault()
e9576ca5 103{
75722dba 104 wxWindow *btnOldDefault = wxButtonBase::SetDefault();
03e11df5 105
21fd5529 106 if ( btnOldDefault )
75722dba
VZ
107 {
108 // cast needed to access the protected member
109 btnOldDefault->GetPeer()->SetData(kControlButtonPart , kControlPushButtonDefaultTag , (Boolean) 0 ) ;
110 }
25eb6881 111
21fd5529 112 m_peer->SetData(kControlButtonPart , kControlPushButtonDefaultTag , (Boolean) 1 ) ;
75722dba
VZ
113
114 return btnOldDefault;
e9576ca5
SC
115}
116
37e2cb08 117wxSize wxButton::DoGetBestSize() const
51abe921 118{
49e3e2c2
SC
119 if ( GetId() == wxID_HELP )
120 return wxSize( 20 , 20 ) ;
6d325d81 121
25eb6881 122 wxSize sz = GetDefaultSize() ;
25eb6881
DS
123
124 switch (GetWindowVariant())
6d325d81 125 {
f3078f07
DS
126 case wxWINDOW_VARIANT_NORMAL:
127 case wxWINDOW_VARIANT_LARGE:
25eb6881 128 sz.y = 20 ;
25eb6881
DS
129 break;
130
f3078f07 131 case wxWINDOW_VARIANT_SMALL:
25eb6881
DS
132 sz.y = 17 ;
133 break;
134
f3078f07 135 case wxWINDOW_VARIANT_MINI:
25eb6881
DS
136 sz.y = 15 ;
137 break;
138
139 default:
140 break;
6d325d81 141 }
25eb6881 142
6d325d81 143 Rect bestsize = { 0 , 0 , 0 , 0 } ;
5ca0d812 144 m_peer->GetBestRect( &bestsize ) ;
8e609c82 145
23866d59 146 int wBtn;
6239ee05 147 if ( EmptyRect( &bestsize ) || ( GetWindowStyle() & wxBU_EXACTFIT) )
6d325d81 148 {
6239ee05
SC
149 Point bounds;
150
151 ControlFontStyleRec controlFont;
152 OSStatus err = m_peer->GetData<ControlFontStyleRec>( kControlEntireControl, kControlFontStyleTag, &controlFont );
153 verify_noerr( err );
154
155 SInt16 baseline;
156 wxMacCFStringHolder str( m_label, m_font.GetEncoding() );
157
158#ifndef __LP64__
159 if ( m_font.MacGetThemeFontID() != kThemeCurrentPortFont )
160 {
161 err = GetThemeTextDimensions(
162 (!m_label.empty() ? (CFStringRef)str : CFSTR(" ")),
163 m_font.MacGetThemeFontID(), kThemeStateActive, false, &bounds, &baseline );
164 verify_noerr( err );
165 }
166 else
167#endif
168 {
6239ee05
SC
169 wxClientDC dc(const_cast<wxButton*>(this));
170 wxCoord width, height ;
171 dc.GetTextExtent( m_label , &width, &height);
172 bounds.h = width;
173 bounds.v = height;
6239ee05
SC
174 }
175
176 wBtn = bounds.h + sz.y;
6d325d81
SC
177 }
178 else
179 {
5ca0d812 180 wBtn = bestsize.right - bestsize.left ;
6239ee05
SC
181 // non 'normal' window variants don't return the correct height
182 // sz.y = bestsize.bottom - bestsize.top ;
6d325d81 183 }
25eb6881 184
8e609c82 185 if ((wBtn > sz.x) || ( GetWindowStyle() & wxBU_EXACTFIT))
6d325d81
SC
186 sz.x = wBtn;
187
188 return sz ;
51abe921
SC
189}
190
7c74e7fe
SC
191wxSize wxButton::GetDefaultSize()
192{
8e609c82 193 int wBtn = 70 ;
e40298d5 194 int hBtn = 20 ;
25eb6881 195
7c74e7fe
SC
196 return wxSize(wBtn, hBtn);
197}
198
519cb848 199void wxButton::Command (wxCommandEvent & event)
e9576ca5 200{
21fd5529 201 m_peer->Flash(kControlButtonPart) ;
25eb6881 202 ProcessCommand(event);
e9576ca5
SC
203}
204
8e609c82 205wxInt32 wxButton::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
e9576ca5 206{
2e91d506 207 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, m_windowId);
4c37f124
SC
208 event.SetEventObject(this);
209 ProcessCommand(event);
25eb6881
DS
210
211 return noErr;
e9576ca5 212}