]> git.saurik.com Git - wxWidgets.git/blame - src/osx/button_osx.cpp
trying to simplify and optimize font handling
[wxWidgets.git] / src / osx / button_osx.cpp
CommitLineData
e53b3d16
SC
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
26IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
27
b38dc31f
SC
28BEGIN_EVENT_TABLE(wxButton, wxControl)
29 EVT_ENTER_WINDOW(wxButton::OnEnterWindow)
30 EVT_LEAVE_WINDOW(wxButton::OnLeaveWindow)
31END_EVENT_TABLE()
32
e53b3d16
SC
33bool 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{
b38dc31f
SC
42 m_marginX =
43 m_marginY = 0;
44
8e4c2912
VZ
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 {
2ac0ac7c 53 return wxControl::Create(parent, id, pos, size, style,
8e4c2912
VZ
54 validator, name);
55 }
56
e53b3d16 57 wxString label(lbl);
423939b2 58 if (label.empty() && wxIsStockID(id) && !(id == wxID_HELP))
e53b3d16
SC
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
85284ca4
VZ
66 m_labelOrig =
67 m_label = label ;
4644cfba 68
e53b3d16
SC
69 m_peer = wxWidgetImpl::CreateButton( this, parent, id, label, pos, size, style, GetExtraStyle() );
70
71 MacPostControlCreate( pos, size );
72
73 return true;
74}
75
85284ca4
VZ
76void 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
e5d05b90
VZ
90wxBitmap wxButton::DoGetBitmap(State which) const
91{
b38dc31f 92 return m_bitmaps[which];
e5d05b90
VZ
93}
94
95void wxButton::DoSetBitmap(const wxBitmap& bitmap, State which)
96{
b38dc31f
SC
97 m_bitmaps[which] = bitmap;
98
e5d05b90
VZ
99 if ( which == State_Normal )
100 m_peer->SetBitmap(bitmap);
b38dc31f
SC
101 else if ( which == State_Pressed )
102 {
103 wxButtonImpl* bi = dynamic_cast<wxButtonImpl*> (m_peer);
104 if ( bi )
105 bi->SetPressedBitmap(bitmap);
106 }
06bb8d92 107 InvalidateBestSize();
e5d05b90
VZ
108}
109
110void wxButton::DoSetBitmapPosition(wxDirection dir)
111{
112 m_peer->SetBitmapPosition(dir);
06bb8d92 113 InvalidateBestSize();
e5d05b90
VZ
114}
115
e53b3d16
SC
116wxWindow *wxButton::SetDefault()
117{
118 wxWindow *btnOldDefault = wxButtonBase::SetDefault();
119
120 if ( btnOldDefault )
121 {
122 btnOldDefault->GetPeer()->SetDefaultButton( false );
123 }
124
125 m_peer->SetDefaultButton( true );
126
127 return btnOldDefault;
128}
129
0faf03bf 130void wxButton::Command (wxCommandEvent & WXUNUSED(event))
e53b3d16
SC
131{
132 m_peer->PerformClick() ;
133 // ProcessCommand(event);
134}
135
b38dc31f
SC
136void wxButton::OnEnterWindow( wxMouseEvent& WXUNUSED(event))
137{
138 if ( DoGetBitmap( State_Current ).IsOk() )
139 m_peer->SetBitmap( DoGetBitmap( State_Current ) );
140}
141
142void wxButton::OnLeaveWindow( wxMouseEvent& WXUNUSED(event))
143{
144 if ( DoGetBitmap( State_Current ).IsOk() )
145 m_peer->SetBitmap( DoGetBitmap( State_Normal ) );
146}
147
0faf03bf 148bool wxButton::OSXHandleClicked( double WXUNUSED(timestampsec) )
e53b3d16
SC
149{
150 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, m_windowId);
151 event.SetEventObject(this);
152 ProcessCommand(event);
153 return true;
154}
155
156//-------------------------------------------------------
157// wxDisclosureTriangle
158//-------------------------------------------------------
159
160bool wxDisclosureTriangle::Create(wxWindow *parent, wxWindowID id, const wxString& label,
161 const wxPoint& pos, const wxSize& size, long style,const wxValidator& validator, const wxString& name )
162{
163 m_macIsUserPane = false ;
164
165 if ( !wxControl::Create(parent, id, pos, size, style, validator, name) )
166 return false;
167
524c47aa 168 m_peer = wxWidgetImpl::CreateDisclosureTriangle(this, parent, id, label, pos, size, style, GetExtraStyle() );
e53b3d16
SC
169
170 MacPostControlCreate( pos, size );
4644cfba 171 // passing the text in the param doesn't seem to work, so lets do it again
e53b3d16 172 SetLabel( label );
4644cfba 173
e53b3d16
SC
174 return true;
175}
176
177void wxDisclosureTriangle::SetOpen( bool open )
178{
e53b3d16 179 m_peer->SetValue( open ? 1 : 0 );
e53b3d16
SC
180}
181
182bool wxDisclosureTriangle::IsOpen() const
183{
e53b3d16 184 return m_peer->GetValue() == 1;
e53b3d16
SC
185}
186
0faf03bf 187bool wxDisclosureTriangle::OSXHandleClicked( double WXUNUSED(timestampsec) )
e53b3d16
SC
188{
189 // Just emit button event for now
190 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, m_windowId);
191 event.SetEventObject(this);
192 ProcessCommand(event);
193
194 return true;
195}
196
197wxSize wxDisclosureTriangle::DoGetBestSize() const
198{
4644cfba
VZ
199 wxSize size = wxWindow::DoGetBestSize();
200
201 // under Carbon the base class GetBestSize() implementation doesn't seem to
202 // take the label into account at all, correct for it here
203#if wxOSX_USE_CARBON
204 size.x += GetTextExtent(GetLabel()).x;
205#endif // wxOSX_USE_CARBON
206
207 return size;
e53b3d16
SC
208}
209