]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/button.cpp
488ca0496cbda57f9f694dc0b50024998f18292a
[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 ( id == wxID_HELP )
44 {
45 ControlButtonContentInfo info ;
46 info.contentType = kControlContentIconRef ;
47 GetIconRef(kOnSystemDisk, kSystemIconsCreator, kHelpIcon, &info.u.iconRef);
48 verify_noerr ( CreateRoundButtonControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds , kControlRoundButtonNormalSize ,
49 &info , m_peer->GetControlRefAddr() ) );
50 }
51 else if ( label.Find('\n' ) == wxNOT_FOUND && label.Find('\r' ) == wxNOT_FOUND)
52 {
53 verify_noerr ( CreatePushButtonControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds , CFSTR("") , m_peer->GetControlRefAddr() ) );
54 }
55 else
56 {
57 ControlButtonContentInfo info ;
58 info.contentType = kControlNoContent ;
59 verify_noerr(CreateBevelButtonControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds,CFSTR(""),
60 kControlBevelButtonLargeBevel , kControlBehaviorPushbutton , &info , 0 , 0 , 0 , m_peer->GetControlRefAddr() ) );
61 }
62
63 wxASSERT_MSG( m_peer != NULL && m_peer->Ok() , wxT("No valid mac control") ) ;
64
65 MacPostControlCreate(pos,size) ;
66
67 return TRUE;
68 }
69
70 void wxButton::SetDefault()
71 {
72 wxWindow *parent = GetParent();
73 wxButton *btnOldDefault = NULL;
74 if ( parent )
75 {
76 btnOldDefault = wxDynamicCast(parent->GetDefaultItem(),
77 wxButton);
78 parent->SetDefaultItem(this);
79 }
80
81 if ( btnOldDefault )
82 btnOldDefault->m_peer->SetData(kControlButtonPart , kControlPushButtonDefaultTag , (Boolean) 0 ) ;
83 m_peer->SetData(kControlButtonPart , kControlPushButtonDefaultTag , (Boolean) 1 ) ;
84 }
85
86 wxSize wxButton::DoGetBestSize() const
87 {
88 if ( GetId() == wxID_HELP )
89 return wxSize( 20 , 20 ) ;
90
91 wxSize sz = GetDefaultSize() ;
92
93 int charspace = 8 ;
94 if ( GetWindowVariant() == wxWINDOW_VARIANT_NORMAL || GetWindowVariant() == wxWINDOW_VARIANT_LARGE )
95 {
96 sz.y = 20 ;
97 charspace = 10 ;
98 }
99 else if ( GetWindowVariant() == wxWINDOW_VARIANT_SMALL )
100 {
101 sz.y = 17 ;
102 charspace = 8 ;
103 }
104 else if ( GetWindowVariant() == wxWINDOW_VARIANT_MINI )
105 {
106 sz.y = 15 ;
107 charspace = 8 ;
108 }
109
110 Rect bestsize = { 0 , 0 , 0 , 0 } ;
111 m_peer->GetBestRect( &bestsize ) ;
112
113 int wBtn;
114 if ( EmptyRect( &bestsize ) )
115 {
116 wBtn = m_label.Length() * charspace + 12 ;
117 }
118 else
119 {
120 wBtn = bestsize.right - bestsize.left ;
121 sz.y = bestsize.bottom - bestsize.top ;
122 }
123
124 if (wBtn > sz.x || ( GetWindowStyle() & wxBU_EXACTFIT) )
125 sz.x = wBtn;
126
127 return sz ;
128 }
129
130 wxSize wxButton::GetDefaultSize()
131 {
132 int wBtn = 70 ;
133 int hBtn = 20 ;
134
135 return wxSize(wBtn, hBtn);
136 }
137
138 void wxButton::Command (wxCommandEvent & event)
139 {
140 m_peer->Flash(kControlButtonPart) ;
141 ProcessCommand (event);
142 }
143
144 wxInt32 wxButton::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
145 {
146 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, m_windowId );
147 event.SetEventObject(this);
148 ProcessCommand(event);
149 return noErr ;
150 }
151