]> git.saurik.com Git - wxWidgets.git/blob - src/osx/button_osx.cpp
Compilation fix for wxFont in wxGTK1.
[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 // FIXME: this hack is needed because we're called from
55 // wxBitmapButton::Create() with this style and we currently use a
56 // different wxWidgetImpl method (CreateBitmapButton() rather than
57 // CreateButton()) for creating bitmap buttons, but we really ought
58 // to unify the creation of buttons of all kinds and then remove
59 // this check
60 if ( style & wxBU_NOTEXT && !ShouldCreatePeer() )
61 {
62 return wxControl::Create(parent, id, pos, size, style,
63 validator, name);
64 }
65
66 DontCreatePeer();
67
68 m_marginX =
69 m_marginY = 0;
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 #if wxOSX_USE_COCOA
105 OSXUpdateAfterLabelChange(label);
106 #endif
107 }
108
109 wxWindow *wxButton::SetDefault()
110 {
111 wxWindow *btnOldDefault = wxButtonBase::SetDefault();
112
113 if ( btnOldDefault )
114 {
115 btnOldDefault->GetPeer()->SetDefaultButton( false );
116 }
117
118 GetPeer()->SetDefaultButton( true );
119
120 return btnOldDefault;
121 }
122
123 void wxButton::Command (wxCommandEvent & WXUNUSED(event))
124 {
125 GetPeer()->PerformClick() ;
126 // ProcessCommand(event);
127 }
128
129 bool wxButton::OSXHandleClicked( double WXUNUSED(timestampsec) )
130 {
131 wxCommandEvent event(wxEVT_BUTTON, m_windowId);
132 event.SetEventObject(this);
133 ProcessCommand(event);
134 return true;
135 }
136
137 /* static */
138 wxSize wxButtonBase::GetDefaultSize()
139 {
140 return wxAnyButton::GetDefaultSize();
141 }
142
143 //-------------------------------------------------------
144 // wxDisclosureTriangle
145 //-------------------------------------------------------
146
147 bool wxDisclosureTriangle::Create(wxWindow *parent, wxWindowID id, const wxString& label,
148 const wxPoint& pos, const wxSize& size, long style,const wxValidator& validator, const wxString& name )
149 {
150 DontCreatePeer();
151 if ( !wxControl::Create(parent, id, pos, size, style, validator, name) )
152 return false;
153
154 SetPeer(wxWidgetImpl::CreateDisclosureTriangle(this, parent, id, label, pos, size, style, GetExtraStyle() ));
155
156 MacPostControlCreate( pos, size );
157 // passing the text in the param doesn't seem to work, so let's do it again
158 SetLabel( label );
159
160 return true;
161 }
162
163 void wxDisclosureTriangle::SetOpen( bool open )
164 {
165 GetPeer()->SetValue( open ? 1 : 0 );
166 }
167
168 bool wxDisclosureTriangle::IsOpen() const
169 {
170 return GetPeer()->GetValue() == 1;
171 }
172
173 bool wxDisclosureTriangle::OSXHandleClicked( double WXUNUSED(timestampsec) )
174 {
175 // Just emit button event for now
176 wxCommandEvent event(wxEVT_BUTTON, m_windowId);
177 event.SetEventObject(this);
178 ProcessCommand(event);
179
180 return true;
181 }
182
183 wxSize wxDisclosureTriangle::DoGetBestSize() const
184 {
185 wxSize size = wxWindow::DoGetBestSize();
186
187 // under Carbon the base class GetBestSize() implementation doesn't seem to
188 // take the label into account at all, correct for it here
189 #if wxOSX_USE_CARBON
190 size.x += GetTextExtent(GetLabel()).x;
191 #endif // wxOSX_USE_CARBON
192
193 return size;
194 }
195