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
,
62 // FIXME: this hack is needed because we're called from
63 // wxBitmapButton::Create() with this style and we currently use a
64 // different wxWidgetImpl method (CreateBitmapButton() rather than
65 // CreateButton()) for creating bitmap buttons, but we really ought
66 // to unify the creation of buttons of all kinds and then remove
68 if ( style
& wxBU_NOTEXT
)
70 return wxControl::Create(parent
, id
, pos
, size
, style
,
76 // Ignore the standard label for help buttons if possible, they use "?"
77 // label under Mac which looks better.
78 if ( !IsHelpButtonWithStandardLabel(id
, labelOrig
) )
80 label
= labelOrig
.empty() && wxIsStockID(id
) ? wxGetStockLabel(id
)
84 m_macIsUserPane
= false ;
86 if ( !wxButtonBase::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
92 m_peer
= wxWidgetImpl::CreateButton( this, parent
, id
, label
, pos
, size
, style
, GetExtraStyle() );
94 MacPostControlCreate( pos
, size
);
99 void wxButton::SetLabel(const wxString
& label
)
101 if ( IsHelpButtonWithStandardLabel(GetId(), label
) )
103 // ignore the standard label for the help buttons, it's not used
107 if ( HasFlag(wxBU_NOTEXT
) )
109 // just store the label internally but don't really use it for the
116 wxButtonBase::SetLabel(label
);
119 wxBitmap
wxButton::DoGetBitmap(State which
) const
121 return m_bitmaps
[which
];
124 void wxButton::DoSetBitmap(const wxBitmap
& bitmap
, State which
)
126 m_bitmaps
[which
] = bitmap
;
128 if ( which
== State_Normal
)
129 m_peer
->SetBitmap(bitmap
);
130 else if ( which
== State_Pressed
)
132 wxButtonImpl
* bi
= dynamic_cast<wxButtonImpl
*> (m_peer
);
134 bi
->SetPressedBitmap(bitmap
);
136 InvalidateBestSize();
139 void wxButton::DoSetBitmapPosition(wxDirection dir
)
141 m_peer
->SetBitmapPosition(dir
);
142 InvalidateBestSize();
145 wxWindow
*wxButton::SetDefault()
147 wxWindow
*btnOldDefault
= wxButtonBase::SetDefault();
151 btnOldDefault
->GetPeer()->SetDefaultButton( false );
154 m_peer
->SetDefaultButton( true );
156 return btnOldDefault
;
159 void wxButton::Command (wxCommandEvent
& WXUNUSED(event
))
161 m_peer
->PerformClick() ;
162 // ProcessCommand(event);
165 void wxButton::OnEnterWindow( wxMouseEvent
& WXUNUSED(event
))
167 if ( DoGetBitmap( State_Current
).IsOk() )
168 m_peer
->SetBitmap( DoGetBitmap( State_Current
) );
171 void wxButton::OnLeaveWindow( wxMouseEvent
& WXUNUSED(event
))
173 if ( DoGetBitmap( State_Current
).IsOk() )
174 m_peer
->SetBitmap( DoGetBitmap( State_Normal
) );
177 bool wxButton::OSXHandleClicked( double WXUNUSED(timestampsec
) )
179 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, m_windowId
);
180 event
.SetEventObject(this);
181 ProcessCommand(event
);
185 //-------------------------------------------------------
186 // wxDisclosureTriangle
187 //-------------------------------------------------------
189 bool wxDisclosureTriangle::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
190 const wxPoint
& pos
, const wxSize
& size
, long style
,const wxValidator
& validator
, const wxString
& name
)
192 m_macIsUserPane
= false ;
194 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
197 m_peer
= wxWidgetImpl::CreateDisclosureTriangle(this, parent
, id
, label
, pos
, size
, style
, GetExtraStyle() );
199 MacPostControlCreate( pos
, size
);
200 // passing the text in the param doesn't seem to work, so lets do it again
206 void wxDisclosureTriangle::SetOpen( bool open
)
208 m_peer
->SetValue( open
? 1 : 0 );
211 bool wxDisclosureTriangle::IsOpen() const
213 return m_peer
->GetValue() == 1;
216 bool wxDisclosureTriangle::OSXHandleClicked( double WXUNUSED(timestampsec
) )
218 // Just emit button event for now
219 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, m_windowId
);
220 event
.SetEventObject(this);
221 ProcessCommand(event
);
226 wxSize
wxDisclosureTriangle::DoGetBestSize() const
228 wxSize size
= wxWindow::DoGetBestSize();
230 // under Carbon the base class GetBestSize() implementation doesn't seem to
231 // take the label into account at all, correct for it here
233 size
.x
+= GetTextExtent(GetLabel()).x
;
234 #endif // wxOSX_USE_CARBON