1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/button_osx.cpp
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/button.h"
18 #include "wx/toplevel.h"
19 #include "wx/dcclient.h"
20 #include "wx/stattext.h"
23 #include "wx/stockitem.h"
25 #include "wx/osx/private.h"
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
)
33 if ( id
!= wxID_HELP
)
39 const wxString labelText
= wxStaticText::GetLabelText(label
);
40 return labelText
== "Help" || labelText
== _("Help");
43 } // anonymous namespace
45 BEGIN_EVENT_TABLE(wxButton
, wxControl
)
46 EVT_ENTER_WINDOW(wxButton::OnEnterWindow
)
47 EVT_LEAVE_WINDOW(wxButton::OnLeaveWindow
)
50 bool wxButton::Create(wxWindow
*parent
,
52 const wxString
& labelOrig
,
56 const wxValidator
& validator
,
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
70 if ( style
& wxBU_NOTEXT
)
72 return wxControl::Create(parent
, id
, pos
, size
, style
,
78 // Ignore the standard label for help buttons if possible, they use "?"
79 // label under Mac which looks better.
80 if ( !IsHelpButtonWithStandardLabel(id
, labelOrig
) )
82 label
= labelOrig
.empty() && wxIsStockID(id
) ? wxGetStockLabel(id
)
87 if ( !wxButtonBase::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
93 SetPeer(wxWidgetImpl::CreateButton( this, parent
, id
, label
, pos
, size
, style
, GetExtraStyle() ));
95 MacPostControlCreate( pos
, size
);
100 void wxButton::SetLabel(const wxString
& label
)
102 if ( IsHelpButtonWithStandardLabel(GetId(), label
) )
104 // ignore the standard label for the help buttons, it's not used
108 if ( HasFlag(wxBU_NOTEXT
) )
110 // just store the label internally but don't really use it for the
117 wxButtonBase::SetLabel(label
);
120 wxBitmap
wxButton::DoGetBitmap(State which
) const
122 return m_bitmaps
[which
];
125 void wxButton::DoSetBitmap(const wxBitmap
& bitmap
, State which
)
127 m_bitmaps
[which
] = bitmap
;
129 if ( which
== State_Normal
)
130 GetPeer()->SetBitmap(bitmap
);
131 else if ( which
== State_Pressed
)
133 wxButtonImpl
* bi
= dynamic_cast<wxButtonImpl
*> (GetPeer());
135 bi
->SetPressedBitmap(bitmap
);
137 InvalidateBestSize();
140 void wxButton::DoSetBitmapPosition(wxDirection dir
)
142 GetPeer()->SetBitmapPosition(dir
);
143 InvalidateBestSize();
146 #if wxUSE_MARKUP && wxOSX_USE_COCOA
148 bool wxButton::DoSetLabelMarkup(const wxString
& markup
)
150 if ( !wxButtonBase::DoSetLabelMarkup(markup
) )
153 GetPeer()->SetLabelMarkup(markup
);
158 #endif // wxUSE_MARKUP && wxOSX_USE_COCOA
160 wxWindow
*wxButton::SetDefault()
162 wxWindow
*btnOldDefault
= wxButtonBase::SetDefault();
166 btnOldDefault
->GetPeer()->SetDefaultButton( false );
169 GetPeer()->SetDefaultButton( true );
171 return btnOldDefault
;
174 void wxButton::Command (wxCommandEvent
& WXUNUSED(event
))
176 GetPeer()->PerformClick() ;
177 // ProcessCommand(event);
180 void wxButton::OnEnterWindow( wxMouseEvent
& WXUNUSED(event
))
182 if ( DoGetBitmap( State_Current
).IsOk() )
183 GetPeer()->SetBitmap( DoGetBitmap( State_Current
) );
186 void wxButton::OnLeaveWindow( wxMouseEvent
& WXUNUSED(event
))
188 if ( DoGetBitmap( State_Current
).IsOk() )
189 GetPeer()->SetBitmap( DoGetBitmap( State_Normal
) );
192 bool wxButton::OSXHandleClicked( double WXUNUSED(timestampsec
) )
194 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, m_windowId
);
195 event
.SetEventObject(this);
196 ProcessCommand(event
);
200 //-------------------------------------------------------
201 // wxDisclosureTriangle
202 //-------------------------------------------------------
204 bool wxDisclosureTriangle::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
205 const wxPoint
& pos
, const wxSize
& size
, long style
,const wxValidator
& validator
, const wxString
& name
)
208 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
211 SetPeer(wxWidgetImpl::CreateDisclosureTriangle(this, parent
, id
, label
, pos
, size
, style
, GetExtraStyle() ));
213 MacPostControlCreate( pos
, size
);
214 // passing the text in the param doesn't seem to work, so lets do it again
220 void wxDisclosureTriangle::SetOpen( bool open
)
222 GetPeer()->SetValue( open
? 1 : 0 );
225 bool wxDisclosureTriangle::IsOpen() const
227 return GetPeer()->GetValue() == 1;
230 bool wxDisclosureTriangle::OSXHandleClicked( double WXUNUSED(timestampsec
) )
232 // Just emit button event for now
233 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, m_windowId
);
234 event
.SetEventObject(this);
235 ProcessCommand(event
);
240 wxSize
wxDisclosureTriangle::DoGetBestSize() const
242 wxSize size
= wxWindow::DoGetBestSize();
244 // under Carbon the base class GetBestSize() implementation doesn't seem to
245 // take the label into account at all, correct for it here
247 size
.x
+= GetTextExtent(GetLabel()).x
;
248 #endif // wxOSX_USE_CARBON