moving bitmapbutton functionality up to button for OSX
[wxWidgets.git] / src / osx / button_osx.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/button_osx.cpp
3 // Purpose: wxButton
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id: button.cpp 54845 2008-07-30 14:52:41Z 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"
20 #endif
21
22 #include "wx/stockitem.h"
23
24 #include "wx/osx/private.h"
25
26 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
27
28 BEGIN_EVENT_TABLE(wxButton, wxControl)
29 EVT_ENTER_WINDOW(wxButton::OnEnterWindow)
30 EVT_LEAVE_WINDOW(wxButton::OnLeaveWindow)
31 END_EVENT_TABLE()
32
33 bool wxButton::Create(wxWindow *parent,
34 wxWindowID id,
35 const wxString& lbl,
36 const wxPoint& pos,
37 const wxSize& size,
38 long style,
39 const wxValidator& validator,
40 const wxString& name)
41 {
42 m_marginX =
43 m_marginY = 0;
44
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
50 // this check
51 if ( style & wxBU_NOTEXT )
52 {
53 return wxControl::Create(parent, id, pos, size, style,
54 validator, name);
55 }
56
57 wxString label(lbl);
58 if (label.empty() && wxIsStockID(id) && !(id == wxID_HELP))
59 label = wxGetStockLabel(id);
60
61 m_macIsUserPane = false ;
62
63 if ( !wxButtonBase::Create(parent, id, pos, size, style, validator, name) )
64 return false;
65
66 m_labelOrig =
67 m_label = label ;
68
69 m_peer = wxWidgetImpl::CreateButton( this, parent, id, label, pos, size, style, GetExtraStyle() );
70
71 MacPostControlCreate( pos, size );
72
73 return true;
74 }
75
76 void wxButton::SetLabel(const wxString& label)
77 {
78 if ( GetId() == wxID_HELP || HasFlag(wxBU_NOTEXT) )
79 {
80 // just store the label internally but don't really use it for the
81 // button
82 m_labelOrig =
83 m_label = label;
84 return;
85 }
86
87 wxButtonBase::SetLabel(label);
88 }
89
90 wxBitmap wxButton::DoGetBitmap(State which) const
91 {
92 return m_bitmaps[which];
93 }
94
95 void wxButton::DoSetBitmap(const wxBitmap& bitmap, State which)
96 {
97 m_bitmaps[which] = bitmap;
98
99 if ( which == State_Normal )
100 m_peer->SetBitmap(bitmap);
101 else if ( which == State_Pressed )
102 {
103 wxButtonImpl* bi = dynamic_cast<wxButtonImpl*> (m_peer);
104 if ( bi )
105 bi->SetPressedBitmap(bitmap);
106 }
107 }
108
109 void wxButton::DoSetBitmapPosition(wxDirection dir)
110 {
111 m_peer->SetBitmapPosition(dir);
112 }
113
114 wxWindow *wxButton::SetDefault()
115 {
116 wxWindow *btnOldDefault = wxButtonBase::SetDefault();
117
118 if ( btnOldDefault )
119 {
120 btnOldDefault->GetPeer()->SetDefaultButton( false );
121 }
122
123 m_peer->SetDefaultButton( true );
124
125 return btnOldDefault;
126 }
127
128 void wxButton::Command (wxCommandEvent & WXUNUSED(event))
129 {
130 m_peer->PerformClick() ;
131 // ProcessCommand(event);
132 }
133
134 void wxButton::OnEnterWindow( wxMouseEvent& WXUNUSED(event))
135 {
136 if ( DoGetBitmap( State_Current ).IsOk() )
137 m_peer->SetBitmap( DoGetBitmap( State_Current ) );
138 }
139
140 void wxButton::OnLeaveWindow( wxMouseEvent& WXUNUSED(event))
141 {
142 if ( DoGetBitmap( State_Current ).IsOk() )
143 m_peer->SetBitmap( DoGetBitmap( State_Normal ) );
144 }
145
146 bool wxButton::OSXHandleClicked( double WXUNUSED(timestampsec) )
147 {
148 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, m_windowId);
149 event.SetEventObject(this);
150 ProcessCommand(event);
151 return true;
152 }
153
154 //-------------------------------------------------------
155 // wxDisclosureTriangle
156 //-------------------------------------------------------
157
158 bool wxDisclosureTriangle::Create(wxWindow *parent, wxWindowID id, const wxString& label,
159 const wxPoint& pos, const wxSize& size, long style,const wxValidator& validator, const wxString& name )
160 {
161 m_macIsUserPane = false ;
162
163 if ( !wxControl::Create(parent, id, pos, size, style, validator, name) )
164 return false;
165
166 m_peer = wxWidgetImpl::CreateDisclosureTriangle(this, parent, id, label, pos, size, style, GetExtraStyle() );
167
168 MacPostControlCreate( pos, size );
169 // passing the text in the param doesn't seem to work, so lets do it again
170 SetLabel( label );
171
172 return true;
173 }
174
175 void wxDisclosureTriangle::SetOpen( bool open )
176 {
177 m_peer->SetValue( open ? 1 : 0 );
178 }
179
180 bool wxDisclosureTriangle::IsOpen() const
181 {
182 return m_peer->GetValue() == 1;
183 }
184
185 bool wxDisclosureTriangle::OSXHandleClicked( double WXUNUSED(timestampsec) )
186 {
187 // Just emit button event for now
188 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, m_windowId);
189 event.SetEventObject(this);
190 ProcessCommand(event);
191
192 return true;
193 }
194
195 wxSize wxDisclosureTriangle::DoGetBestSize() const
196 {
197 wxSize size = wxWindow::DoGetBestSize();
198
199 // under Carbon the base class GetBestSize() implementation doesn't seem to
200 // take the label into account at all, correct for it here
201 #if wxOSX_USE_CARBON
202 size.x += GetTextExtent(GetLabel()).x;
203 #endif // wxOSX_USE_CARBON
204
205 return size;
206 }
207