]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/button.cpp
reintroducing direct access, clipping to client area in dc
[wxWidgets.git] / src / mac / carbon / button.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: button.cpp
3 // Purpose: wxButton
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "button.h"
14 #endif
15
16 #include "wx/wxprec.h"
17
18 #include "wx/button.h"
19 #include "wx/panel.h"
20 #include "wx/stockitem.h"
21
22 #if !USE_SHARED_LIBRARY
23 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
24 #endif
25
26 #include "wx/mac/uma.h"
27 // Button
28
29 bool wxButton::Create(wxWindow *parent, wxWindowID id, const wxString& lbl,
30 const wxPoint& pos,
31 const wxSize& size, long style,
32 const wxValidator& validator,
33 const wxString& name)
34 {
35 wxString label(lbl);
36 if (label.empty() && wxIsStockID(id))
37 label = wxGetStockLabel(id);
38
39 m_macIsUserPane = FALSE ;
40
41 if ( !wxButtonBase::Create(parent, id, pos, size, style, validator, name) )
42 return false;
43
44 m_label = label ;
45
46 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
47 m_peer = new wxMacControl() ;
48 if ( id == wxID_HELP )
49 {
50 ControlButtonContentInfo info ;
51 info.contentType = kControlContentIconRef ;
52 GetIconRef(kOnSystemDisk, kSystemIconsCreator, kHelpIcon, &info.u.iconRef);
53 verify_noerr ( CreateRoundButtonControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds , kControlRoundButtonNormalSize ,
54 &info , m_peer->GetControlRefAddr() ) );
55 }
56 else if ( label.Find('\n' ) == wxNOT_FOUND && label.Find('\r' ) == wxNOT_FOUND)
57 {
58 #if TARGET_API_MAC_OSX
59 //Button height is static in Mac, can't be changed, so we need to force it here
60 if ( GetWindowVariant() == wxWINDOW_VARIANT_NORMAL || GetWindowVariant() == wxWINDOW_VARIANT_LARGE )
61 {
62 bounds.bottom = bounds.top + 20 ;
63 m_maxHeight = 20 ;
64 }
65 else if ( GetWindowVariant() == wxWINDOW_VARIANT_SMALL )
66 {
67 bounds.bottom = bounds.top + 17 ;
68 m_maxHeight = 17 ;
69 }
70 else if ( GetWindowVariant() == wxWINDOW_VARIANT_MINI )
71 {
72 bounds.bottom = bounds.top + 15 ;
73 m_maxHeight = 15 ;
74 }
75 #endif
76 verify_noerr ( CreatePushButtonControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds , CFSTR("") , m_peer->GetControlRefAddr() ) );
77 }
78 else
79 {
80 ControlButtonContentInfo info ;
81 info.contentType = kControlNoContent ;
82 verify_noerr(CreateBevelButtonControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds,CFSTR(""),
83 kControlBevelButtonLargeBevel , kControlBehaviorPushbutton , &info , 0 , 0 , 0 , m_peer->GetControlRefAddr() ) );
84 }
85
86 wxASSERT_MSG( m_peer != NULL && m_peer->Ok() , wxT("No valid mac control") ) ;
87
88 MacPostControlCreate(pos,size) ;
89
90 return TRUE;
91 }
92
93 void wxButton::SetDefault()
94 {
95 wxWindow *parent = GetParent();
96 wxButton *btnOldDefault = NULL;
97 if ( parent )
98 {
99 btnOldDefault = wxDynamicCast(parent->GetDefaultItem(),
100 wxButton);
101 parent->SetDefaultItem(this);
102 }
103
104 if ( btnOldDefault )
105 btnOldDefault->m_peer->SetData(kControlButtonPart , kControlPushButtonDefaultTag , (Boolean) 0 ) ;
106 m_peer->SetData(kControlButtonPart , kControlPushButtonDefaultTag , (Boolean) 1 ) ;
107 }
108
109 wxSize wxButton::DoGetBestSize() const
110 {
111 if ( GetId() == wxID_HELP )
112 return wxSize( 20 , 20 ) ;
113
114 wxSize sz = GetDefaultSize() ;
115
116 int charspace = 8 ;
117 if ( GetWindowVariant() == wxWINDOW_VARIANT_NORMAL || GetWindowVariant() == wxWINDOW_VARIANT_LARGE )
118 {
119 sz.y = 20 ;
120 charspace = 10 ;
121 }
122 else if ( GetWindowVariant() == wxWINDOW_VARIANT_SMALL )
123 {
124 sz.y = 17 ;
125 charspace = 8 ;
126 }
127 else if ( GetWindowVariant() == wxWINDOW_VARIANT_MINI )
128 {
129 sz.y = 15 ;
130 charspace = 8 ;
131 }
132
133 Rect bestsize = { 0 , 0 , 0 , 0 } ;
134 m_peer->GetBestRect( &bestsize ) ;
135
136 int wBtn;
137 if ( EmptyRect( &bestsize ) )
138 {
139 wBtn = m_label.Length() * charspace + 12 ;
140 }
141 else
142 {
143 wBtn = bestsize.right - bestsize.left ;
144 sz.y = bestsize.bottom - bestsize.top ;
145 }
146
147 if (wBtn > sz.x || ( GetWindowStyle() & wxBU_EXACTFIT) )
148 sz.x = wBtn;
149
150 return sz ;
151 }
152
153 wxSize wxButton::GetDefaultSize()
154 {
155 int wBtn = 70 ;
156 int hBtn = 20 ;
157
158 return wxSize(wBtn, hBtn);
159 }
160
161 void wxButton::Command (wxCommandEvent & event)
162 {
163 m_peer->Flash(kControlButtonPart) ;
164 ProcessCommand (event);
165 }
166
167 wxInt32 wxButton::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
168 {
169 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, m_windowId );
170 event.SetEventObject(this);
171 ProcessCommand(event);
172 return noErr ;
173 }
174