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