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