1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/button_osx.cpp
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: button.cpp 54845 2008-07-30 14:52:41Z SC $
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 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
47 BEGIN_EVENT_TABLE(wxButton
, wxControl
)
48 EVT_ENTER_WINDOW(wxButton::OnEnterWindow
)
49 EVT_LEAVE_WINDOW(wxButton::OnLeaveWindow
)
52 bool wxButton::Create(wxWindow
*parent
,
54 const wxString
& labelOrig
,
58 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
)
86 m_macIsUserPane
= false ;
88 if ( !wxButtonBase::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
94 m_peer
= wxWidgetImpl::CreateButton( this, parent
, id
, label
, pos
, size
, style
, GetExtraStyle() );
96 MacPostControlCreate( pos
, size
);
101 void wxButton::SetLabel(const wxString
& label
)
103 if ( IsHelpButtonWithStandardLabel(GetId(), label
) )
105 // ignore the standard label for the help buttons, it's not used
109 if ( HasFlag(wxBU_NOTEXT
) )
111 // just store the label internally but don't really use it for the
118 wxButtonBase::SetLabel(label
);
121 wxBitmap
wxButton::DoGetBitmap(State which
) const
123 return m_bitmaps
[which
];
126 void wxButton::DoSetBitmap(const wxBitmap
& bitmap
, State which
)
128 m_bitmaps
[which
] = bitmap
;
130 if ( which
== State_Normal
)
131 m_peer
->SetBitmap(bitmap
);
132 else if ( which
== State_Pressed
)
134 wxButtonImpl
* bi
= dynamic_cast<wxButtonImpl
*> (m_peer
);
136 bi
->SetPressedBitmap(bitmap
);
138 InvalidateBestSize();
141 void wxButton::DoSetBitmapPosition(wxDirection dir
)
143 m_peer
->SetBitmapPosition(dir
);
144 InvalidateBestSize();
147 wxWindow
*wxButton::SetDefault()
149 wxWindow
*btnOldDefault
= wxButtonBase::SetDefault();
153 btnOldDefault
->GetPeer()->SetDefaultButton( false );
156 m_peer
->SetDefaultButton( true );
158 return btnOldDefault
;
161 void wxButton::Command (wxCommandEvent
& WXUNUSED(event
))
163 m_peer
->PerformClick() ;
164 // ProcessCommand(event);
167 void wxButton::OnEnterWindow( wxMouseEvent
& WXUNUSED(event
))
169 if ( DoGetBitmap( State_Current
).IsOk() )
170 m_peer
->SetBitmap( DoGetBitmap( State_Current
) );
173 void wxButton::OnLeaveWindow( wxMouseEvent
& WXUNUSED(event
))
175 if ( DoGetBitmap( State_Current
).IsOk() )
176 m_peer
->SetBitmap( DoGetBitmap( State_Normal
) );
179 bool wxButton::OSXHandleClicked( double WXUNUSED(timestampsec
) )
181 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, m_windowId
);
182 event
.SetEventObject(this);
183 ProcessCommand(event
);
187 //-------------------------------------------------------
188 // wxDisclosureTriangle
189 //-------------------------------------------------------
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
)
194 m_macIsUserPane
= false ;
196 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
199 m_peer
= wxWidgetImpl::CreateDisclosureTriangle(this, parent
, id
, label
, pos
, size
, style
, GetExtraStyle() );
201 MacPostControlCreate( pos
, size
);
202 // passing the text in the param doesn't seem to work, so lets do it again
208 void wxDisclosureTriangle::SetOpen( bool open
)
210 m_peer
->SetValue( open
? 1 : 0 );
213 bool wxDisclosureTriangle::IsOpen() const
215 return m_peer
->GetValue() == 1;
218 bool wxDisclosureTriangle::OSXHandleClicked( double WXUNUSED(timestampsec
) )
220 // Just emit button event for now
221 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, m_windowId
);
222 event
.SetEventObject(this);
223 ProcessCommand(event
);
228 wxSize
wxDisclosureTriangle::DoGetBestSize() const
230 wxSize size
= wxWindow::DoGetBestSize();
232 // under Carbon the base class GetBestSize() implementation doesn't seem to
233 // take the label into account at all, correct for it here
235 size
.x
+= GetTextExtent(GetLabel()).x
;
236 #endif // wxOSX_USE_CARBON