]> git.saurik.com Git - wxWidgets.git/blob - src/osx/button_osx.cpp
Refactor wxMSW helper functions to allow reuse in wxGTK.
[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$
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 bool wxButton::Create(wxWindow *parent,
46 wxWindowID id,
47 const wxString& labelOrig,
48 const wxPoint& pos,
49 const wxSize& size,
50 long style,
51 const wxValidator& validator,
52 const wxString& name)
53 {
54 DontCreatePeer();
55
56 m_marginX =
57 m_marginY = 0;
58
59 // FIXME: this hack is needed because we're called from
60 // wxBitmapButton::Create() with this style and we currently use a
61 // different wxWidgetImpl method (CreateBitmapButton() rather than
62 // CreateButton()) for creating bitmap buttons, but we really ought
63 // to unify the creation of buttons of all kinds and then remove
64 // this check
65 if ( style & wxBU_NOTEXT )
66 {
67 return wxControl::Create(parent, id, pos, size, style,
68 validator, name);
69 }
70
71 wxString label;
72
73 // Ignore the standard label for help buttons if possible, they use "?"
74 // label under Mac which looks better.
75 if ( !IsHelpButtonWithStandardLabel(id, labelOrig) )
76 {
77 label = labelOrig.empty() && wxIsStockID(id) ? wxGetStockLabel(id)
78 : labelOrig;
79 }
80
81
82 if ( !wxButtonBase::Create(parent, id, pos, size, style, validator, name) )
83 return false;
84
85 m_labelOrig =
86 m_label = label ;
87
88 SetPeer(wxWidgetImpl::CreateButton( this, parent, id, label, pos, size, style, GetExtraStyle() ));
89
90 MacPostControlCreate( pos, size );
91
92 return true;
93 }
94
95 void wxButton::SetLabel(const wxString& label)
96 {
97 if ( IsHelpButtonWithStandardLabel(GetId(), label) )
98 {
99 // ignore the standard label for the help buttons, it's not used
100 return;
101 }
102
103 wxAnyButton::SetLabel(label);
104 }
105
106 wxWindow *wxButton::SetDefault()
107 {
108 wxWindow *btnOldDefault = wxButtonBase::SetDefault();
109
110 if ( btnOldDefault )
111 {
112 btnOldDefault->GetPeer()->SetDefaultButton( false );
113 }
114
115 GetPeer()->SetDefaultButton( true );
116
117 return btnOldDefault;
118 }
119
120 void wxButton::Command (wxCommandEvent & WXUNUSED(event))
121 {
122 GetPeer()->PerformClick() ;
123 // ProcessCommand(event);
124 }
125
126 bool wxButton::OSXHandleClicked( double WXUNUSED(timestampsec) )
127 {
128 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, m_windowId);
129 event.SetEventObject(this);
130 ProcessCommand(event);
131 return true;
132 }
133
134 /* static */
135 wxSize wxButtonBase::GetDefaultSize()
136 {
137 return wxAnyButton::GetDefaultSize();
138 }
139
140 //-------------------------------------------------------
141 // wxDisclosureTriangle
142 //-------------------------------------------------------
143
144 bool wxDisclosureTriangle::Create(wxWindow *parent, wxWindowID id, const wxString& label,
145 const wxPoint& pos, const wxSize& size, long style,const wxValidator& validator, const wxString& name )
146 {
147 DontCreatePeer();
148 if ( !wxControl::Create(parent, id, pos, size, style, validator, name) )
149 return false;
150
151 SetPeer(wxWidgetImpl::CreateDisclosureTriangle(this, parent, id, label, pos, size, style, GetExtraStyle() ));
152
153 MacPostControlCreate( pos, size );
154 // passing the text in the param doesn't seem to work, so let's do it again
155 SetLabel( label );
156
157 return true;
158 }
159
160 void wxDisclosureTriangle::SetOpen( bool open )
161 {
162 GetPeer()->SetValue( open ? 1 : 0 );
163 }
164
165 bool wxDisclosureTriangle::IsOpen() const
166 {
167 return GetPeer()->GetValue() == 1;
168 }
169
170 bool wxDisclosureTriangle::OSXHandleClicked( double WXUNUSED(timestampsec) )
171 {
172 // Just emit button event for now
173 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, m_windowId);
174 event.SetEventObject(this);
175 ProcessCommand(event);
176
177 return true;
178 }
179
180 wxSize wxDisclosureTriangle::DoGetBestSize() const
181 {
182 wxSize size = wxWindow::DoGetBestSize();
183
184 // under Carbon the base class GetBestSize() implementation doesn't seem to
185 // take the label into account at all, correct for it here
186 #if wxOSX_USE_CARBON
187 size.x += GetTextExtent(GetLabel()).x;
188 #endif // wxOSX_USE_CARBON
189
190 return size;
191 }
192