]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/mac/carbon/button.cpp | |
3 | // Purpose: wxButton | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 1998-01-01 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Stefan Csomor | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #include "wx/button.h" | |
15 | ||
16 | #ifndef WX_PRECOMP | |
17 | #include "wx/panel.h" | |
18 | #include "wx/toplevel.h" | |
19 | #include "wx/dcclient.h" | |
20 | #endif | |
21 | ||
22 | #include "wx/stockitem.h" | |
23 | ||
24 | #include "wx/mac/uma.h" | |
25 | ||
26 | IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl) | |
27 | ||
28 | ||
29 | bool 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) | |
37 | { | |
38 | wxString label(lbl); | |
39 | if (label.empty() && wxIsStockID(id)) | |
40 | label = wxGetStockLabel(id); | |
41 | ||
42 | m_macIsUserPane = false ; | |
43 | ||
44 | if ( !wxButtonBase::Create(parent, id, pos, size, style, validator, name) ) | |
45 | return false; | |
46 | ||
47 | m_labelOrig = m_label = label ; | |
48 | ||
49 | OSStatus err; | |
50 | Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ; | |
51 | m_peer = new wxMacControl(this) ; | |
52 | if ( id == wxID_HELP ) | |
53 | { | |
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() ); | |
61 | } | |
62 | else if ( label.Find('\n' ) == wxNOT_FOUND && label.Find('\r' ) == wxNOT_FOUND) | |
63 | { | |
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 ) | |
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 | } | |
80 | err = CreatePushButtonControl( | |
81 | MAC_WXHWND(parent->MacGetTopLevelWindowRef()), | |
82 | &bounds, CFSTR(""), m_peer->GetControlRefAddr() ); | |
83 | } | |
84 | else | |
85 | { | |
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() ); | |
92 | } | |
93 | ||
94 | verify_noerr( err ); | |
95 | wxASSERT_MSG( m_peer != NULL && m_peer->Ok() , wxT("No valid Mac control") ) ; | |
96 | ||
97 | MacPostControlCreate( pos, size ); | |
98 | ||
99 | return true; | |
100 | } | |
101 | ||
102 | wxWindow *wxButton::SetDefault() | |
103 | { | |
104 | wxWindow *btnOldDefault = wxButtonBase::SetDefault(); | |
105 | ||
106 | if ( btnOldDefault ) | |
107 | { | |
108 | // cast needed to access the protected member | |
109 | btnOldDefault->GetPeer()->SetData(kControlButtonPart , kControlPushButtonDefaultTag , (Boolean) 0 ) ; | |
110 | } | |
111 | ||
112 | m_peer->SetData(kControlButtonPart , kControlPushButtonDefaultTag , (Boolean) 1 ) ; | |
113 | ||
114 | return btnOldDefault; | |
115 | } | |
116 | ||
117 | wxSize wxButton::DoGetBestSize() const | |
118 | { | |
119 | if ( GetId() == wxID_HELP ) | |
120 | return wxSize( 20 , 20 ) ; | |
121 | ||
122 | wxSize sz = GetDefaultSize() ; | |
123 | ||
124 | switch (GetWindowVariant()) | |
125 | { | |
126 | case wxWINDOW_VARIANT_NORMAL: | |
127 | case wxWINDOW_VARIANT_LARGE: | |
128 | sz.y = 20 ; | |
129 | break; | |
130 | ||
131 | case wxWINDOW_VARIANT_SMALL: | |
132 | sz.y = 17 ; | |
133 | break; | |
134 | ||
135 | case wxWINDOW_VARIANT_MINI: | |
136 | sz.y = 15 ; | |
137 | break; | |
138 | ||
139 | default: | |
140 | break; | |
141 | } | |
142 | ||
143 | Rect bestsize = { 0 , 0 , 0 , 0 } ; | |
144 | m_peer->GetBestRect( &bestsize ) ; | |
145 | ||
146 | int wBtn; | |
147 | if ( EmptyRect( &bestsize ) || ( GetWindowStyle() & wxBU_EXACTFIT) ) | |
148 | { | |
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 | { | |
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; | |
174 | } | |
175 | ||
176 | wBtn = bounds.h + sz.y; | |
177 | } | |
178 | else | |
179 | { | |
180 | wBtn = bestsize.right - bestsize.left ; | |
181 | // non 'normal' window variants don't return the correct height | |
182 | // sz.y = bestsize.bottom - bestsize.top ; | |
183 | } | |
184 | ||
185 | if ((wBtn > sz.x) || ( GetWindowStyle() & wxBU_EXACTFIT)) | |
186 | sz.x = wBtn; | |
187 | ||
188 | return sz ; | |
189 | } | |
190 | ||
191 | wxSize wxButton::GetDefaultSize() | |
192 | { | |
193 | int wBtn = 70 ; | |
194 | int hBtn = 20 ; | |
195 | ||
196 | return wxSize(wBtn, hBtn); | |
197 | } | |
198 | ||
199 | void wxButton::Command (wxCommandEvent & event) | |
200 | { | |
201 | m_peer->Flash(kControlButtonPart) ; | |
202 | ProcessCommand(event); | |
203 | } | |
204 | ||
205 | wxInt32 wxButton::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) ) | |
206 | { | |
207 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, m_windowId); | |
208 | event.SetEventObject(this); | |
209 | ProcessCommand(event); | |
210 | ||
211 | return noErr; | |
212 | } |