]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/button.cpp
fixed what looked like a bug in determining the default width in DoGetBestSize()
[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 #ifdef __GNUG__
13 #pragma implementation "button.h"
14 #endif
15
16 #include "wx/defs.h"
17
18 #include "wx/button.h"
19 #include "wx/panel.h"
20
21 #if !USE_SHARED_LIBRARY
22 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
23 #endif
24
25 #include "wx/mac/uma.h"
26 // Button
27
28 bool wxButton::Create(wxWindow *parent, wxWindowID id, const wxString& label,
29 const wxPoint& pos,
30 const wxSize& size, long style,
31 const wxValidator& validator,
32 const wxString& name)
33 {
34 m_macIsUserPane = FALSE ;
35
36 if ( !wxButtonBase::Create(parent, id, pos, size, style, validator, name) )
37 return false;
38
39 m_label = label ;
40
41 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
42 m_peer = new wxMacControl() ;
43 if ( label.Find('\n' ) == wxNOT_FOUND && label.Find('\r' ) == wxNOT_FOUND)
44 {
45 verify_noerr ( CreatePushButtonControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds , CFSTR("") , m_peer->GetControlRefAddr() ) );
46 }
47 else
48 {
49 ControlButtonContentInfo info ;
50 info.contentType = kControlNoContent ;
51 verify_noerr(CreateBevelButtonControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds,CFSTR(""),
52 kControlBevelButtonLargeBevel , kControlBehaviorPushbutton , &info , 0 , 0 , 0 , m_peer->GetControlRefAddr() ) );
53 }
54
55 wxASSERT_MSG( m_peer != NULL && m_peer->Ok() , wxT("No valid mac control") ) ;
56
57 MacPostControlCreate(pos,size) ;
58
59 return TRUE;
60 }
61
62 void wxButton::SetDefault()
63 {
64 wxWindow *parent = GetParent();
65 wxButton *btnOldDefault = NULL;
66 if ( parent )
67 {
68 btnOldDefault = wxDynamicCast(parent->GetDefaultItem(),
69 wxButton);
70 parent->SetDefaultItem(this);
71 }
72
73 if ( btnOldDefault )
74 btnOldDefault->m_peer->SetData(kControlButtonPart , kControlPushButtonDefaultTag , (Boolean) 0 ) ;
75 m_peer->SetData(kControlButtonPart , kControlPushButtonDefaultTag , (Boolean) 1 ) ;
76 }
77
78 wxSize wxButton::DoGetBestSize() const
79 {
80 wxSize sz = GetDefaultSize() ;
81
82 int charspace = 8 ;
83 if ( GetWindowVariant() == wxWINDOW_VARIANT_NORMAL || GetWindowVariant() == wxWINDOW_VARIANT_LARGE )
84 {
85 sz.y = 20 ;
86 charspace = 10 ;
87 }
88 else if ( GetWindowVariant() == wxWINDOW_VARIANT_SMALL )
89 {
90 sz.y = 17 ;
91 charspace = 8 ;
92 }
93 else if ( GetWindowVariant() == wxWINDOW_VARIANT_MINI )
94 {
95 sz.y = 15 ;
96 charspace = 8 ;
97 }
98
99 Rect bestsize = { 0 , 0 , 0 , 0 } ;
100 m_peer->GetBestRect( &bestsize ) ;
101
102 int wBtn;
103 if ( EmptyRect( &bestsize ) )
104 {
105 wBtn = m_label.Length() * charspace + 12 ;
106 }
107 else
108 {
109 wBtn = bestsize.right - bestsize.left ;
110 sz.y = bestsize.bottom - bestsize.top ;
111 }
112
113 if (wBtn > sz.x || ( GetWindowStyle() & wxBU_EXACTFIT) )
114 sz.x = wBtn;
115
116 return sz ;
117 }
118
119 wxSize wxButton::GetDefaultSize()
120 {
121 int wBtn = 70 ;
122 int hBtn = 20 ;
123
124 return wxSize(wBtn, hBtn);
125 }
126
127 void wxButton::Command (wxCommandEvent & event)
128 {
129 m_peer->Flash(kControlButtonPart) ;
130 ProcessCommand (event);
131 }
132
133 wxInt32 wxButton::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
134 {
135 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, m_windowId );
136 event.SetEventObject(this);
137 ProcessCommand(event);
138 return noErr ;
139 }
140