]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/button.cpp
Suppress assertion when the array is sorted
[wxWidgets.git] / src / mac / carbon / button.cpp
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 #endif
19
20 #include "wx/stockitem.h"
21
22 #include "wx/mac/uma.h"
23
24 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
25
26
27 bool wxButton::Create(wxWindow *parent,
28 wxWindowID id,
29 const wxString& lbl,
30 const wxPoint& pos,
31 const wxSize& size,
32 long style,
33 const wxValidator& validator,
34 const wxString& name)
35 {
36 wxString label(lbl);
37 if (label.empty() && wxIsStockID(id))
38 label = wxGetStockLabel(id);
39
40 m_macIsUserPane = false ;
41
42 if ( !wxButtonBase::Create(parent, id, pos, size, style, validator, name) )
43 return false;
44
45 m_label = label ;
46
47 OSStatus err;
48 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
49 m_peer = new wxMacControl(this) ;
50 if ( id == wxID_HELP )
51 {
52 ControlButtonContentInfo info ;
53 info.contentType = kControlContentIconRef ;
54 GetIconRef(kOnSystemDisk, kSystemIconsCreator, kHelpIcon, &info.u.iconRef);
55 err = CreateRoundButtonControl(
56 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
57 &bounds, kControlRoundButtonNormalSize,
58 &info, m_peer->GetControlRefAddr() );
59 }
60 else if ( label.Find('\n' ) == wxNOT_FOUND && label.Find('\r' ) == wxNOT_FOUND)
61 {
62 #if TARGET_API_MAC_OSX
63 // Button height is static in Mac, can't be changed, so we need to force it here
64 if ( GetWindowVariant() == wxWINDOW_VARIANT_NORMAL || GetWindowVariant() == wxWINDOW_VARIANT_LARGE )
65 {
66 bounds.bottom = bounds.top + 20 ;
67 m_maxHeight = 20 ;
68 }
69 else if ( GetWindowVariant() == wxWINDOW_VARIANT_SMALL )
70 {
71 bounds.bottom = bounds.top + 17 ;
72 m_maxHeight = 17 ;
73 }
74 else if ( GetWindowVariant() == wxWINDOW_VARIANT_MINI )
75 {
76 bounds.bottom = bounds.top + 15 ;
77 m_maxHeight = 15 ;
78 }
79 #endif
80
81 err = CreatePushButtonControl(
82 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
83 &bounds, CFSTR(""), m_peer->GetControlRefAddr() );
84 }
85 else
86 {
87 ControlButtonContentInfo info ;
88 info.contentType = kControlNoContent ;
89 err = CreateBevelButtonControl(
90 MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds, CFSTR(""),
91 kControlBevelButtonLargeBevel, kControlBehaviorPushbutton,
92 &info, 0, 0, 0, m_peer->GetControlRefAddr() );
93 }
94
95 verify_noerr( err );
96 wxASSERT_MSG( m_peer != NULL && m_peer->Ok() , wxT("No valid Mac control") ) ;
97
98 MacPostControlCreate( pos, size );
99
100 return true;
101 }
102
103 void wxButton::SetDefault()
104 {
105 wxWindow *parent = GetParent();
106 wxButton *btnOldDefault = NULL;
107
108 if ( parent )
109 {
110 btnOldDefault = wxDynamicCast(parent->GetDefaultItem(), wxButton);
111 parent->SetDefaultItem(this);
112 }
113
114 if ( btnOldDefault )
115 btnOldDefault->m_peer->SetData(kControlButtonPart , kControlPushButtonDefaultTag , (Boolean) 0 ) ;
116
117 m_peer->SetData(kControlButtonPart , kControlPushButtonDefaultTag , (Boolean) 1 ) ;
118 }
119
120 wxSize wxButton::DoGetBestSize() const
121 {
122 if ( GetId() == wxID_HELP )
123 return wxSize( 20 , 20 ) ;
124
125 wxSize sz = GetDefaultSize() ;
126 int charspace = 8 ;
127
128 switch (GetWindowVariant())
129 {
130 case wxWINDOW_VARIANT_NORMAL:
131 case wxWINDOW_VARIANT_LARGE:
132 sz.y = 20 ;
133 charspace = 10 ;
134 break;
135
136 case wxWINDOW_VARIANT_SMALL:
137 sz.y = 17 ;
138 break;
139
140 case wxWINDOW_VARIANT_MINI:
141 sz.y = 15 ;
142 break;
143
144 default:
145 break;
146 }
147
148 Rect bestsize = { 0 , 0 , 0 , 0 } ;
149 m_peer->GetBestRect( &bestsize ) ;
150
151 int wBtn;
152 if ( EmptyRect( &bestsize ) )
153 {
154 wBtn = m_label.length() * charspace + 12 ;
155 }
156 else
157 {
158 wBtn = bestsize.right - bestsize.left ;
159 sz.y = bestsize.bottom - bestsize.top ;
160 }
161
162 if ((wBtn > sz.x) || ( GetWindowStyle() & wxBU_EXACTFIT))
163 sz.x = wBtn;
164
165 return sz ;
166 }
167
168 wxSize wxButton::GetDefaultSize()
169 {
170 int wBtn = 70 ;
171 int hBtn = 20 ;
172
173 return wxSize(wBtn, hBtn);
174 }
175
176 void wxButton::Command (wxCommandEvent & event)
177 {
178 m_peer->Flash(kControlButtonPart) ;
179 ProcessCommand(event);
180 }
181
182 wxInt32 wxButton::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
183 {
184 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, m_windowId);
185 event.SetEventObject(this);
186 ProcessCommand(event);
187
188 return noErr;
189 }