]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/button.cpp
moving collapsible pane a few steps forward os OSX
[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
dbe4a80c 155 wxCFStringRef str( m_label, m_font.GetEncoding() );
6239ee05 156
8b534a7b
SC
157#if wxMAC_USE_ATSU_TEXT
158 SInt16 baseline;
6239ee05
SC
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}
47922888
RR
213
214//-------------------------------------------------------
215// wxDisclosureTriangle
216//-------------------------------------------------------
217
218bool wxDisclosureTriangle::Create(wxWindow *parent, wxWindowID id, const wxString& label,
219 const wxPoint& pos, const wxSize& size, long style,const wxValidator& validator, const wxString& name )
220{
221 m_macIsUserPane = false ;
222
223 if ( !wxControl::Create(parent, id, pos, size, style, validator, name) )
224 return false;
225
226 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
227 m_peer = new wxMacControl(this) ;
228
229 OSStatus err = CreateDisclosureTriangleControl(
230 MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds,
231 kControlDisclosureTrianglePointDefault,
5dd1b7a6 232 wxCFStringRef( label ),
47922888
RR
233 0, // closed
234 TRUE, // draw title
235 TRUE, // auto toggle back and forth
236 m_peer->GetControlRefAddr() );
5dd1b7a6 237
47922888
RR
238 verify_noerr( err );
239 wxASSERT_MSG( m_peer != NULL && m_peer->Ok() , wxT("No valid Mac control") ) ;
240
241 MacPostControlCreate( pos, size );
5dd1b7a6
SC
242 // passing the text in the param doesn't seem to work, so lets do if again
243 SetLabel( label );
244
47922888
RR
245 return true;
246}
247
248void wxDisclosureTriangle::SetOpen( bool open )
249{
5dd1b7a6 250 m_peer->SetValue( open ? 1 : 0 );
47922888
RR
251}
252
253bool wxDisclosureTriangle::IsOpen() const
254{
5dd1b7a6 255 return m_peer->GetValue() == 1;
47922888
RR
256}
257
258wxInt32 wxDisclosureTriangle::MacControlHit( WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
259{
260 // Just emit button event for now
261 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, m_windowId);
262 event.SetEventObject(this);
263 ProcessCommand(event);
264
265 return noErr;
266}
267
268wxSize wxDisclosureTriangle::DoGetBestSize() const
269{
270 return wxSize(16,16);
271}
272
273