]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/button.cpp
* Implemented BestSize cache
[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 ) );
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 ) );
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 int wBtn = m_label.Length() * charspace + 12 ;
100
101 if (wBtn > sz.x || ( GetWindowStyle() & wxBU_EXACTFIT) )
102 sz.x = wBtn;
103
104 return sz ;
105 }
106
107 wxSize wxButton::GetDefaultSize()
108 {
109 int wBtn = 70 ;
110 int hBtn = 20 ;
111
112 return wxSize(wBtn, hBtn);
113 }
114
115 void wxButton::Command (wxCommandEvent & event)
116 {
117 m_peer->Flash(kControlButtonPart) ;
118 ProcessCommand (event);
119 }
120
121 wxInt32 wxButton::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
122 {
123 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, m_windowId );
124 event.SetEventObject(this);
125 ProcessCommand(event);
126 return noErr ;
127 }
128