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