]>
Commit | Line | Data |
---|---|---|
e53b3d16 SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/osx/button_osx.cpp | |
3 | // Purpose: wxButton | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 1998-01-01 | |
b5b208a1 | 7 | // RCS-ID: $Id$ |
e53b3d16 SC |
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" | |
01495abf | 20 | #include "wx/stattext.h" |
e53b3d16 SC |
21 | #endif |
22 | ||
23 | #include "wx/stockitem.h" | |
24 | ||
25 | #include "wx/osx/private.h" | |
26 | ||
01495abf VZ |
27 | namespace |
28 | { | |
29 | ||
30 | // Returns true only if the id is wxID_HELP and the label is "Help" or empty. | |
31 | bool IsHelpButtonWithStandardLabel(wxWindowID id, const wxString& label) | |
32 | { | |
33 | if ( id != wxID_HELP ) | |
34 | return false; | |
35 | ||
36 | if ( label.empty() ) | |
37 | return true; | |
38 | ||
39 | const wxString labelText = wxStaticText::GetLabelText(label); | |
40 | return labelText == "Help" || labelText == _("Help"); | |
41 | } | |
42 | ||
43 | } // anonymous namespace | |
44 | ||
b38dc31f SC |
45 | BEGIN_EVENT_TABLE(wxButton, wxControl) |
46 | EVT_ENTER_WINDOW(wxButton::OnEnterWindow) | |
47 | EVT_LEAVE_WINDOW(wxButton::OnLeaveWindow) | |
48 | END_EVENT_TABLE() | |
49 | ||
e53b3d16 SC |
50 | bool wxButton::Create(wxWindow *parent, |
51 | wxWindowID id, | |
01495abf | 52 | const wxString& labelOrig, |
e53b3d16 SC |
53 | const wxPoint& pos, |
54 | const wxSize& size, | |
55 | long style, | |
56 | const wxValidator& validator, | |
57 | const wxString& name) | |
58 | { | |
b38dc31f SC |
59 | m_marginX = |
60 | m_marginY = 0; | |
61 | ||
8e4c2912 VZ |
62 | // FIXME: this hack is needed because we're called from |
63 | // wxBitmapButton::Create() with this style and we currently use a | |
64 | // different wxWidgetImpl method (CreateBitmapButton() rather than | |
65 | // CreateButton()) for creating bitmap buttons, but we really ought | |
66 | // to unify the creation of buttons of all kinds and then remove | |
67 | // this check | |
68 | if ( style & wxBU_NOTEXT ) | |
69 | { | |
2ac0ac7c | 70 | return wxControl::Create(parent, id, pos, size, style, |
8e4c2912 VZ |
71 | validator, name); |
72 | } | |
73 | ||
01495abf VZ |
74 | wxString label; |
75 | ||
76 | // Ignore the standard label for help buttons if possible, they use "?" | |
77 | // label under Mac which looks better. | |
78 | if ( !IsHelpButtonWithStandardLabel(id, labelOrig) ) | |
79 | { | |
80 | label = labelOrig.empty() && wxIsStockID(id) ? wxGetStockLabel(id) | |
81 | : labelOrig; | |
82 | } | |
e53b3d16 SC |
83 | |
84 | m_macIsUserPane = false ; | |
85 | ||
86 | if ( !wxButtonBase::Create(parent, id, pos, size, style, validator, name) ) | |
87 | return false; | |
88 | ||
85284ca4 VZ |
89 | m_labelOrig = |
90 | m_label = label ; | |
4644cfba | 91 | |
22756322 | 92 | SetPeer(wxWidgetImpl::CreateButton( this, parent, id, label, pos, size, style, GetExtraStyle() )); |
e53b3d16 SC |
93 | |
94 | MacPostControlCreate( pos, size ); | |
95 | ||
96 | return true; | |
97 | } | |
98 | ||
85284ca4 VZ |
99 | void wxButton::SetLabel(const wxString& label) |
100 | { | |
01495abf VZ |
101 | if ( IsHelpButtonWithStandardLabel(GetId(), label) ) |
102 | { | |
103 | // ignore the standard label for the help buttons, it's not used | |
104 | return; | |
105 | } | |
106 | ||
107 | if ( HasFlag(wxBU_NOTEXT) ) | |
85284ca4 VZ |
108 | { |
109 | // just store the label internally but don't really use it for the | |
110 | // button | |
111 | m_labelOrig = | |
112 | m_label = label; | |
113 | return; | |
114 | } | |
115 | ||
116 | wxButtonBase::SetLabel(label); | |
117 | } | |
118 | ||
e5d05b90 VZ |
119 | wxBitmap wxButton::DoGetBitmap(State which) const |
120 | { | |
b38dc31f | 121 | return m_bitmaps[which]; |
e5d05b90 VZ |
122 | } |
123 | ||
124 | void wxButton::DoSetBitmap(const wxBitmap& bitmap, State which) | |
125 | { | |
b38dc31f | 126 | m_bitmaps[which] = bitmap; |
ce00f59b | 127 | |
e5d05b90 | 128 | if ( which == State_Normal ) |
22756322 | 129 | GetPeer()->SetBitmap(bitmap); |
b38dc31f SC |
130 | else if ( which == State_Pressed ) |
131 | { | |
22756322 | 132 | wxButtonImpl* bi = dynamic_cast<wxButtonImpl*> (GetPeer()); |
b38dc31f SC |
133 | if ( bi ) |
134 | bi->SetPressedBitmap(bitmap); | |
135 | } | |
06bb8d92 | 136 | InvalidateBestSize(); |
e5d05b90 VZ |
137 | } |
138 | ||
139 | void wxButton::DoSetBitmapPosition(wxDirection dir) | |
140 | { | |
22756322 | 141 | GetPeer()->SetBitmapPosition(dir); |
06bb8d92 | 142 | InvalidateBestSize(); |
e5d05b90 VZ |
143 | } |
144 | ||
f672c969 VZ |
145 | #if wxUSE_MARKUP && wxOSX_USE_COCOA |
146 | ||
147 | bool wxButton::DoSetLabelMarkup(const wxString& markup) | |
148 | { | |
149 | if ( !wxButtonBase::DoSetLabelMarkup(markup) ) | |
150 | return false; | |
151 | ||
22756322 | 152 | GetPeer()->SetLabelMarkup(markup); |
f672c969 VZ |
153 | |
154 | return true; | |
155 | } | |
156 | ||
157 | #endif // wxUSE_MARKUP && wxOSX_USE_COCOA | |
158 | ||
e53b3d16 SC |
159 | wxWindow *wxButton::SetDefault() |
160 | { | |
161 | wxWindow *btnOldDefault = wxButtonBase::SetDefault(); | |
162 | ||
163 | if ( btnOldDefault ) | |
164 | { | |
165 | btnOldDefault->GetPeer()->SetDefaultButton( false ); | |
166 | } | |
167 | ||
22756322 | 168 | GetPeer()->SetDefaultButton( true ); |
e53b3d16 SC |
169 | |
170 | return btnOldDefault; | |
171 | } | |
172 | ||
0faf03bf | 173 | void wxButton::Command (wxCommandEvent & WXUNUSED(event)) |
e53b3d16 | 174 | { |
22756322 | 175 | GetPeer()->PerformClick() ; |
e53b3d16 SC |
176 | // ProcessCommand(event); |
177 | } | |
178 | ||
b38dc31f SC |
179 | void wxButton::OnEnterWindow( wxMouseEvent& WXUNUSED(event)) |
180 | { | |
181 | if ( DoGetBitmap( State_Current ).IsOk() ) | |
22756322 | 182 | GetPeer()->SetBitmap( DoGetBitmap( State_Current ) ); |
b38dc31f SC |
183 | } |
184 | ||
185 | void wxButton::OnLeaveWindow( wxMouseEvent& WXUNUSED(event)) | |
186 | { | |
187 | if ( DoGetBitmap( State_Current ).IsOk() ) | |
22756322 | 188 | GetPeer()->SetBitmap( DoGetBitmap( State_Normal ) ); |
b38dc31f SC |
189 | } |
190 | ||
0faf03bf | 191 | bool wxButton::OSXHandleClicked( double WXUNUSED(timestampsec) ) |
e53b3d16 SC |
192 | { |
193 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, m_windowId); | |
194 | event.SetEventObject(this); | |
195 | ProcessCommand(event); | |
196 | return true; | |
197 | } | |
198 | ||
199 | //------------------------------------------------------- | |
200 | // wxDisclosureTriangle | |
201 | //------------------------------------------------------- | |
202 | ||
203 | bool wxDisclosureTriangle::Create(wxWindow *parent, wxWindowID id, const wxString& label, | |
204 | const wxPoint& pos, const wxSize& size, long style,const wxValidator& validator, const wxString& name ) | |
205 | { | |
206 | m_macIsUserPane = false ; | |
207 | ||
208 | if ( !wxControl::Create(parent, id, pos, size, style, validator, name) ) | |
209 | return false; | |
210 | ||
22756322 | 211 | SetPeer(wxWidgetImpl::CreateDisclosureTriangle(this, parent, id, label, pos, size, style, GetExtraStyle() )); |
e53b3d16 SC |
212 | |
213 | MacPostControlCreate( pos, size ); | |
4644cfba | 214 | // passing the text in the param doesn't seem to work, so lets do it again |
e53b3d16 | 215 | SetLabel( label ); |
4644cfba | 216 | |
e53b3d16 SC |
217 | return true; |
218 | } | |
219 | ||
220 | void wxDisclosureTriangle::SetOpen( bool open ) | |
221 | { | |
22756322 | 222 | GetPeer()->SetValue( open ? 1 : 0 ); |
e53b3d16 SC |
223 | } |
224 | ||
225 | bool wxDisclosureTriangle::IsOpen() const | |
226 | { | |
22756322 | 227 | return GetPeer()->GetValue() == 1; |
e53b3d16 SC |
228 | } |
229 | ||
0faf03bf | 230 | bool wxDisclosureTriangle::OSXHandleClicked( double WXUNUSED(timestampsec) ) |
e53b3d16 SC |
231 | { |
232 | // Just emit button event for now | |
233 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, m_windowId); | |
234 | event.SetEventObject(this); | |
235 | ProcessCommand(event); | |
236 | ||
237 | return true; | |
238 | } | |
239 | ||
240 | wxSize wxDisclosureTriangle::DoGetBestSize() const | |
241 | { | |
4644cfba VZ |
242 | wxSize size = wxWindow::DoGetBestSize(); |
243 | ||
244 | // under Carbon the base class GetBestSize() implementation doesn't seem to | |
245 | // take the label into account at all, correct for it here | |
246 | #if wxOSX_USE_CARBON | |
247 | size.x += GetTextExtent(GetLabel()).x; | |
248 | #endif // wxOSX_USE_CARBON | |
249 | ||
250 | return size; | |
e53b3d16 SC |
251 | } |
252 |