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