added support for bitmaps in wxButton to wxOSX/Cocoa
[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 bool wxButton::Create(wxWindow *parent,
29 wxWindowID id,
30 const wxString& lbl,
31 const wxPoint& pos,
32 const wxSize& size,
33 long style,
34 const wxValidator& validator,
35 const wxString& name)
36 {
37 wxString label(lbl);
38 if (label.empty() && wxIsStockID(id) && !(id == wxID_HELP))
39 label = wxGetStockLabel(id);
40
41 m_macIsUserPane = false ;
42
43 if ( !wxButtonBase::Create(parent, id, pos, size, style, validator, name) )
44 return false;
45
46 m_labelOrig =
47 m_label = label ;
48
49 m_peer = wxWidgetImpl::CreateButton( this, parent, id, label, pos, size, style, GetExtraStyle() );
50
51 MacPostControlCreate( pos, size );
52
53 return true;
54 }
55
56 void wxButton::SetLabel(const wxString& label)
57 {
58 if ( GetId() == wxID_HELP || HasFlag(wxBU_NOTEXT) )
59 {
60 // just store the label internally but don't really use it for the
61 // button
62 m_labelOrig =
63 m_label = label;
64 return;
65 }
66
67 wxButtonBase::SetLabel(label);
68 }
69
70 // there is no support for button bitmaps in wxOSX/Carbon so there is no need
71 // for these methods there
72 #if wxOSX_USE_COCOA
73
74 wxBitmap wxButton::DoGetBitmap(State which) const
75 {
76 return which == State_Normal ? m_peer->GetBitmap() : wxBitmap();
77 }
78
79 void wxButton::DoSetBitmap(const wxBitmap& bitmap, State which)
80 {
81 if ( which == State_Normal )
82 m_peer->SetBitmap(bitmap);
83 }
84
85 void wxButton::DoSetBitmapPosition(wxDirection dir)
86 {
87 m_peer->SetBitmapPosition(dir);
88 }
89
90 #endif // wxOSX_USE_COCOA
91
92 wxWindow *wxButton::SetDefault()
93 {
94 wxWindow *btnOldDefault = wxButtonBase::SetDefault();
95
96 if ( btnOldDefault )
97 {
98 btnOldDefault->GetPeer()->SetDefaultButton( false );
99 }
100
101 m_peer->SetDefaultButton( true );
102
103 return btnOldDefault;
104 }
105
106 void wxButton::Command (wxCommandEvent & WXUNUSED(event))
107 {
108 m_peer->PerformClick() ;
109 // ProcessCommand(event);
110 }
111
112 bool wxButton::OSXHandleClicked( double WXUNUSED(timestampsec) )
113 {
114 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, m_windowId);
115 event.SetEventObject(this);
116 ProcessCommand(event);
117 return true;
118 }
119
120 //-------------------------------------------------------
121 // wxDisclosureTriangle
122 //-------------------------------------------------------
123
124 bool wxDisclosureTriangle::Create(wxWindow *parent, wxWindowID id, const wxString& label,
125 const wxPoint& pos, const wxSize& size, long style,const wxValidator& validator, const wxString& name )
126 {
127 m_macIsUserPane = false ;
128
129 if ( !wxControl::Create(parent, id, pos, size, style, validator, name) )
130 return false;
131
132 m_peer = wxWidgetImpl::CreateDisclosureTriangle(this, parent, id, label, pos, size, style, GetExtraStyle() );
133
134 MacPostControlCreate( pos, size );
135 // passing the text in the param doesn't seem to work, so lets do it again
136 SetLabel( label );
137
138 return true;
139 }
140
141 void wxDisclosureTriangle::SetOpen( bool open )
142 {
143 m_peer->SetValue( open ? 1 : 0 );
144 }
145
146 bool wxDisclosureTriangle::IsOpen() const
147 {
148 return m_peer->GetValue() == 1;
149 }
150
151 bool wxDisclosureTriangle::OSXHandleClicked( double WXUNUSED(timestampsec) )
152 {
153 // Just emit button event for now
154 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, m_windowId);
155 event.SetEventObject(this);
156 ProcessCommand(event);
157
158 return true;
159 }
160
161 wxSize wxDisclosureTriangle::DoGetBestSize() const
162 {
163 wxSize size = wxWindow::DoGetBestSize();
164
165 // under Carbon the base class GetBestSize() implementation doesn't seem to
166 // take the label into account at all, correct for it here
167 #if wxOSX_USE_CARBON
168 size.x += GetTextExtent(GetLabel()).x;
169 #endif // wxOSX_USE_CARBON
170
171 return size;
172 }
173