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"
22 #include "wx/stockitem.h"
24 #include "wx/osx/private.h"
26 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
28 BEGIN_EVENT_TABLE(wxButton
, wxControl
)
29 EVT_ENTER_WINDOW(wxButton::OnEnterWindow
)
30 EVT_LEAVE_WINDOW(wxButton::OnLeaveWindow
)
33 bool wxButton::Create(wxWindow
*parent
,
39 const wxValidator
& validator
,
45 // FIXME: this hack is needed because we're called from
46 // wxBitmapButton::Create() with this style and we currently use a
47 // different wxWidgetImpl method (CreateBitmapButton() rather than
48 // CreateButton()) for creating bitmap buttons, but we really ought
49 // to unify the creation of buttons of all kinds and then remove
51 if ( style
& wxBU_NOTEXT
)
53 return wxControl::Create(parent
, id
, pos
, size
, style
,
58 if (label
.empty() && wxIsStockID(id
) && !(id
== wxID_HELP
))
59 label
= wxGetStockLabel(id
);
61 m_macIsUserPane
= false ;
63 if ( !wxButtonBase::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
69 m_peer
= wxWidgetImpl::CreateButton( this, parent
, id
, label
, pos
, size
, style
, GetExtraStyle() );
71 MacPostControlCreate( pos
, size
);
76 void wxButton::SetLabel(const wxString
& label
)
78 if ( GetId() == wxID_HELP
|| HasFlag(wxBU_NOTEXT
) )
80 // just store the label internally but don't really use it for the
87 wxButtonBase::SetLabel(label
);
90 wxBitmap
wxButton::DoGetBitmap(State which
) const
92 return m_bitmaps
[which
];
95 void wxButton::DoSetBitmap(const wxBitmap
& bitmap
, State which
)
97 m_bitmaps
[which
] = bitmap
;
99 if ( which
== State_Normal
)
100 m_peer
->SetBitmap(bitmap
);
101 else if ( which
== State_Pressed
)
103 wxButtonImpl
* bi
= dynamic_cast<wxButtonImpl
*> (m_peer
);
105 bi
->SetPressedBitmap(bitmap
);
107 InvalidateBestSize();
110 void wxButton::DoSetBitmapPosition(wxDirection dir
)
112 m_peer
->SetBitmapPosition(dir
);
113 InvalidateBestSize();
116 wxWindow
*wxButton::SetDefault()
118 wxWindow
*btnOldDefault
= wxButtonBase::SetDefault();
122 btnOldDefault
->GetPeer()->SetDefaultButton( false );
125 m_peer
->SetDefaultButton( true );
127 return btnOldDefault
;
130 void wxButton::Command (wxCommandEvent
& WXUNUSED(event
))
132 m_peer
->PerformClick() ;
133 // ProcessCommand(event);
136 void wxButton::OnEnterWindow( wxMouseEvent
& WXUNUSED(event
))
138 if ( DoGetBitmap( State_Current
).IsOk() )
139 m_peer
->SetBitmap( DoGetBitmap( State_Current
) );
142 void wxButton::OnLeaveWindow( wxMouseEvent
& WXUNUSED(event
))
144 if ( DoGetBitmap( State_Current
).IsOk() )
145 m_peer
->SetBitmap( DoGetBitmap( State_Normal
) );
148 bool wxButton::OSXHandleClicked( double WXUNUSED(timestampsec
) )
150 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, m_windowId
);
151 event
.SetEventObject(this);
152 ProcessCommand(event
);
156 //-------------------------------------------------------
157 // wxDisclosureTriangle
158 //-------------------------------------------------------
160 bool wxDisclosureTriangle::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
161 const wxPoint
& pos
, const wxSize
& size
, long style
,const wxValidator
& validator
, const wxString
& name
)
163 m_macIsUserPane
= false ;
165 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
168 m_peer
= wxWidgetImpl::CreateDisclosureTriangle(this, parent
, id
, label
, pos
, size
, style
, GetExtraStyle() );
170 MacPostControlCreate( pos
, size
);
171 // passing the text in the param doesn't seem to work, so lets do it again
177 void wxDisclosureTriangle::SetOpen( bool open
)
179 m_peer
->SetValue( open
? 1 : 0 );
182 bool wxDisclosureTriangle::IsOpen() const
184 return m_peer
->GetValue() == 1;
187 bool wxDisclosureTriangle::OSXHandleClicked( double WXUNUSED(timestampsec
) )
189 // Just emit button event for now
190 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, m_windowId
);
191 event
.SetEventObject(this);
192 ProcessCommand(event
);
197 wxSize
wxDisclosureTriangle::DoGetBestSize() const
199 wxSize size
= wxWindow::DoGetBestSize();
201 // under Carbon the base class GetBestSize() implementation doesn't seem to
202 // take the label into account at all, correct for it here
204 size
.x
+= GetTextExtent(GetLabel()).x
;
205 #endif // wxOSX_USE_CARBON