]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/button.cpp
55785b73ace804bdf6ab677ee3e3215dc54599fc
[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 #include "wx/toplevel.h"
19 #endif
20
21 #include "wx/stockitem.h"
22
23 #include "wx/mac/uma.h"
24
25 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
26
27
28 bool wxButton::Create(wxWindow *parent,
29 wxWindowID id,
30 const wxString& lbl,
31 const wxPoint& pos,
32 const wxSize& size,
33 long style,
34 const wxValidator& validator,
35 const wxString& name)
36 {
37 wxString label(lbl);
38 if (label.empty() && wxIsStockID(id))
39 label = wxGetStockLabel(id);
40
41 m_macIsUserPane = false ;
42
43 if ( !wxButtonBase::Create(parent, id, pos, size, style, validator, name) )
44 return false;
45
46 m_label = label ;
47
48 OSStatus err;
49 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
50 m_peer = new wxMacControl(this) ;
51 if ( id == wxID_HELP )
52 {
53 ControlButtonContentInfo info ;
54 info.contentType = kControlContentIconRef ;
55 GetIconRef(kOnSystemDisk, kSystemIconsCreator, kHelpIcon, &info.u.iconRef);
56 err = CreateRoundButtonControl(
57 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
58 &bounds, kControlRoundButtonNormalSize,
59 &info, m_peer->GetControlRefAddr() );
60 }
61 else if ( label.Find('\n' ) == wxNOT_FOUND && label.Find('\r' ) == wxNOT_FOUND)
62 {
63 #if TARGET_API_MAC_OSX
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 #endif
81
82 err = CreatePushButtonControl(
83 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
84 &bounds, CFSTR(""), m_peer->GetControlRefAddr() );
85 }
86 else
87 {
88 ControlButtonContentInfo info ;
89 info.contentType = kControlNoContent ;
90 err = CreateBevelButtonControl(
91 MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds, CFSTR(""),
92 kControlBevelButtonLargeBevel, kControlBehaviorPushbutton,
93 &info, 0, 0, 0, m_peer->GetControlRefAddr() );
94 }
95
96 verify_noerr( err );
97 wxASSERT_MSG( m_peer != NULL && m_peer->Ok() , wxT("No valid Mac control") ) ;
98
99 MacPostControlCreate( pos, size );
100
101 return true;
102 }
103
104 void wxButton::SetDefault()
105 {
106 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
107 wxButton *btnOldDefault = NULL;
108 if ( tlw )
109 {
110 btnOldDefault = wxDynamicCast(tlw->GetDefaultItem(), wxButton);
111 tlw->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 }