]> git.saurik.com Git - wxWidgets.git/blame - src/osx/button_osx.cpp
fix memory leak in wxScreenDC, fixes #13249
[wxWidgets.git] / src / osx / button_osx.cpp
CommitLineData
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
27namespace
28{
29
30// Returns true only if the id is wxID_HELP and the label is "Help" or empty.
31bool 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
45BEGIN_EVENT_TABLE(wxButton, wxControl)
46 EVT_ENTER_WINDOW(wxButton::OnEnterWindow)
47 EVT_LEAVE_WINDOW(wxButton::OnLeaveWindow)
48END_EVENT_TABLE()
49
e53b3d16
SC
50bool 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{
d15694e8
SC
59 DontCreatePeer();
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 85
e53b3d16
SC
86
87 if ( !wxButtonBase::Create(parent, id, pos, size, style, validator, name) )
88 return false;
89
85284ca4
VZ
90 m_labelOrig =
91 m_label = label ;
4644cfba 92
22756322 93 SetPeer(wxWidgetImpl::CreateButton( this, parent, id, label, pos, size, style, GetExtraStyle() ));
e53b3d16
SC
94
95 MacPostControlCreate( pos, size );
96
97 return true;
98}
99
85284ca4
VZ
100void wxButton::SetLabel(const wxString& label)
101{
01495abf
VZ
102 if ( IsHelpButtonWithStandardLabel(GetId(), label) )
103 {
104 // ignore the standard label for the help buttons, it's not used
105 return;
106 }
107
108 if ( HasFlag(wxBU_NOTEXT) )
85284ca4
VZ
109 {
110 // just store the label internally but don't really use it for the
111 // button
112 m_labelOrig =
113 m_label = label;
114 return;
115 }
116
117 wxButtonBase::SetLabel(label);
118}
119
e5d05b90
VZ
120wxBitmap wxButton::DoGetBitmap(State which) const
121{
b38dc31f 122 return m_bitmaps[which];
e5d05b90
VZ
123}
124
125void wxButton::DoSetBitmap(const wxBitmap& bitmap, State which)
126{
b38dc31f 127 m_bitmaps[which] = bitmap;
ce00f59b 128
e5d05b90 129 if ( which == State_Normal )
22756322 130 GetPeer()->SetBitmap(bitmap);
b38dc31f
SC
131 else if ( which == State_Pressed )
132 {
22756322 133 wxButtonImpl* bi = dynamic_cast<wxButtonImpl*> (GetPeer());
b38dc31f
SC
134 if ( bi )
135 bi->SetPressedBitmap(bitmap);
136 }
06bb8d92 137 InvalidateBestSize();
e5d05b90
VZ
138}
139
140void wxButton::DoSetBitmapPosition(wxDirection dir)
141{
22756322 142 GetPeer()->SetBitmapPosition(dir);
06bb8d92 143 InvalidateBestSize();
e5d05b90
VZ
144}
145
f672c969
VZ
146#if wxUSE_MARKUP && wxOSX_USE_COCOA
147
148bool wxButton::DoSetLabelMarkup(const wxString& markup)
149{
150 if ( !wxButtonBase::DoSetLabelMarkup(markup) )
151 return false;
152
22756322 153 GetPeer()->SetLabelMarkup(markup);
f672c969
VZ
154
155 return true;
156}
157
158#endif // wxUSE_MARKUP && wxOSX_USE_COCOA
159
e53b3d16
SC
160wxWindow *wxButton::SetDefault()
161{
162 wxWindow *btnOldDefault = wxButtonBase::SetDefault();
163
164 if ( btnOldDefault )
165 {
166 btnOldDefault->GetPeer()->SetDefaultButton( false );
167 }
168
22756322 169 GetPeer()->SetDefaultButton( true );
e53b3d16
SC
170
171 return btnOldDefault;
172}
173
0faf03bf 174void wxButton::Command (wxCommandEvent & WXUNUSED(event))
e53b3d16 175{
22756322 176 GetPeer()->PerformClick() ;
e53b3d16
SC
177 // ProcessCommand(event);
178}
179
b38dc31f
SC
180void wxButton::OnEnterWindow( wxMouseEvent& WXUNUSED(event))
181{
182 if ( DoGetBitmap( State_Current ).IsOk() )
22756322 183 GetPeer()->SetBitmap( DoGetBitmap( State_Current ) );
b38dc31f
SC
184}
185
186void wxButton::OnLeaveWindow( wxMouseEvent& WXUNUSED(event))
187{
188 if ( DoGetBitmap( State_Current ).IsOk() )
22756322 189 GetPeer()->SetBitmap( DoGetBitmap( State_Normal ) );
b38dc31f
SC
190}
191
0faf03bf 192bool wxButton::OSXHandleClicked( double WXUNUSED(timestampsec) )
e53b3d16
SC
193{
194 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, m_windowId);
195 event.SetEventObject(this);
196 ProcessCommand(event);
197 return true;
198}
199
200//-------------------------------------------------------
201// wxDisclosureTriangle
202//-------------------------------------------------------
203
204bool wxDisclosureTriangle::Create(wxWindow *parent, wxWindowID id, const wxString& label,
205 const wxPoint& pos, const wxSize& size, long style,const wxValidator& validator, const wxString& name )
d15694e8
SC
206{
207 DontCreatePeer();
e53b3d16
SC
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 );
4c51a665 214 // passing the text in the param doesn't seem to work, so let's do it again
e53b3d16 215 SetLabel( label );
4644cfba 216
e53b3d16
SC
217 return true;
218}
219
220void wxDisclosureTriangle::SetOpen( bool open )
221{
22756322 222 GetPeer()->SetValue( open ? 1 : 0 );
e53b3d16
SC
223}
224
225bool wxDisclosureTriangle::IsOpen() const
226{
22756322 227 return GetPeer()->GetValue() == 1;
e53b3d16
SC
228}
229
0faf03bf 230bool 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
240wxSize 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