]> git.saurik.com Git - wxWidgets.git/blob - src/osx/button_osx.cpp
ceedc162b404ae912dc3c1d9854bf4620f1108aa
[wxWidgets.git] / src / osx / button_osx.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/button_osx.cpp
3 // Purpose: wxButton
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id: button.cpp 54845 2008-07-30 14:52:41Z 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"
20 #include "wx/stattext.h"
21 #endif
22
23 #include "wx/stockitem.h"
24
25 #include "wx/osx/private.h"
26
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
45 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
46
47 BEGIN_EVENT_TABLE(wxButton, wxControl)
48 EVT_ENTER_WINDOW(wxButton::OnEnterWindow)
49 EVT_LEAVE_WINDOW(wxButton::OnLeaveWindow)
50 END_EVENT_TABLE()
51
52 bool wxButton::Create(wxWindow *parent,
53 wxWindowID id,
54 const wxString& labelOrig,
55 const wxPoint& pos,
56 const wxSize& size,
57 long style,
58 const wxValidator& validator,
59 const wxString& name)
60 {
61 m_marginX =
62 m_marginY = 0;
63
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 {
72 return wxControl::Create(parent, id, pos, size, style,
73 validator, name);
74 }
75
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 }
85
86 m_macIsUserPane = false ;
87
88 if ( !wxButtonBase::Create(parent, id, pos, size, style, validator, name) )
89 return false;
90
91 m_labelOrig =
92 m_label = label ;
93
94 m_peer = wxWidgetImpl::CreateButton( this, parent, id, label, pos, size, style, GetExtraStyle() );
95
96 MacPostControlCreate( pos, size );
97
98 return true;
99 }
100
101 void wxButton::SetLabel(const wxString& label)
102 {
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) )
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
121 wxBitmap wxButton::DoGetBitmap(State which) const
122 {
123 return m_bitmaps[which];
124 }
125
126 void wxButton::DoSetBitmap(const wxBitmap& bitmap, State which)
127 {
128 m_bitmaps[which] = bitmap;
129
130 if ( which == State_Normal )
131 m_peer->SetBitmap(bitmap);
132 else if ( which == State_Pressed )
133 {
134 wxButtonImpl* bi = dynamic_cast<wxButtonImpl*> (m_peer);
135 if ( bi )
136 bi->SetPressedBitmap(bitmap);
137 }
138 InvalidateBestSize();
139 }
140
141 void wxButton::DoSetBitmapPosition(wxDirection dir)
142 {
143 m_peer->SetBitmapPosition(dir);
144 InvalidateBestSize();
145 }
146
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
161 void wxButton::Command (wxCommandEvent & WXUNUSED(event))
162 {
163 m_peer->PerformClick() ;
164 // ProcessCommand(event);
165 }
166
167 void wxButton::OnEnterWindow( wxMouseEvent& WXUNUSED(event))
168 {
169 if ( DoGetBitmap( State_Current ).IsOk() )
170 m_peer->SetBitmap( DoGetBitmap( State_Current ) );
171 }
172
173 void wxButton::OnLeaveWindow( wxMouseEvent& WXUNUSED(event))
174 {
175 if ( DoGetBitmap( State_Current ).IsOk() )
176 m_peer->SetBitmap( DoGetBitmap( State_Normal ) );
177 }
178
179 bool wxButton::OSXHandleClicked( double WXUNUSED(timestampsec) )
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
199 m_peer = wxWidgetImpl::CreateDisclosureTriangle(this, parent, id, label, pos, size, style, GetExtraStyle() );
200
201 MacPostControlCreate( pos, size );
202 // passing the text in the param doesn't seem to work, so lets do it again
203 SetLabel( label );
204
205 return true;
206 }
207
208 void wxDisclosureTriangle::SetOpen( bool open )
209 {
210 m_peer->SetValue( open ? 1 : 0 );
211 }
212
213 bool wxDisclosureTriangle::IsOpen() const
214 {
215 return m_peer->GetValue() == 1;
216 }
217
218 bool wxDisclosureTriangle::OSXHandleClicked( double WXUNUSED(timestampsec) )
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 {
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;
239 }
240