]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/bmpbuttn.cpp
revert last change (r48897)
[wxWidgets.git] / src / mac / carbon / bmpbuttn.cpp
CommitLineData
e9576ca5 1/////////////////////////////////////////////////////////////////////////////
cc374a2f 2// Name: src/mac/carbon/bmpbuttn.cpp
e9576ca5 3// Purpose: wxBitmapButton
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
0bca0373 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
a8e9860d
SC
12#include "wx/wxprec.h"
13
179e085f
RN
14#if wxUSE_BMPBUTTON
15
e9576ca5
SC
16#include "wx/bmpbuttn.h"
17
cdccdfab 18#ifndef WX_PRECOMP
85313a9a 19 #include "wx/dcmemory.h"
cdccdfab
WS
20#endif
21
e9576ca5 22IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton)
e9576ca5 23
d497dca4 24#include "wx/mac/uma.h"
7c551d95 25
cc374a2f 26bool wxBitmapButton::Create( wxWindow *parent,
cdccdfab
WS
27 wxWindowID id, const wxBitmap& bitmap,
28 const wxPoint& pos,
29 const wxSize& size,
30 long style,
31 const wxValidator& validator,
32 const wxString& name )
e9576ca5 33{
cc374a2f
DS
34 m_macIsUserPane = false;
35
bf19e3f6
SC
36 // since bitmapbuttonbase is subclass of button calling wxBitmapButtonBase::Create
37 // essentially creates an additional button
cc374a2f 38 if ( !wxControl::Create( parent, id, pos, size, style, validator, name ) )
b45ed7a2
VZ
39 return false;
40
cc374a2f 41 if ( style & wxBU_AUTODRAW )
827e7a48 42 {
cc374a2f 43 m_marginX =
827e7a48
RR
44 m_marginY = wxDEFAULT_BUTTON_MARGIN;
45 }
46 else
47 {
cc374a2f 48 m_marginX =
827e7a48
RR
49 m_marginY = 0;
50 }
e9576ca5 51
cc374a2f
DS
52 OSStatus err = noErr;
53 ControlButtonContentInfo info;
54
55 Rect bounds = wxMacGetBoundsForControl( this, pos, size );
56 m_peer = new wxMacControl( this );
d32be04c 57
d24a9fc7 58 if ( bitmap.Ok() && HasFlag(wxBORDER_NONE) )
2acac1b4 59 {
d24a9fc7
VZ
60 // in Mac OS X the icon controls (which are used for borderless bitmap
61 // buttons) can have only one of the few standard sizes and if they
62 // don't, the OS rescales them automatically resulting in really ugly
63 // images, so centre the image in a square of standard size instead
2acac1b4
VZ
64
65 // the supported sizes, sorted in decreasng order
66 static const int stdSizes[] = { 128, 48, 32, 16, 0 };
67
68 const int width = bitmap.GetWidth();
69 const int height = bitmap.GetHeight();
70
71 int n;
72 for ( n = 0; n < (int)WXSIZEOF(stdSizes); n++ )
73 {
74 const int sizeStd = stdSizes[n];
75 if ( width > sizeStd || height > sizeStd )
76 {
77 // it will become -1 if the bitmap is larger than the biggest
78 // supported size, this is intentional
79 n--;
80
81 break;
82 }
83 }
84
85 if ( n != -1 )
86 {
87 const int sizeStd = stdSizes[n];
88 if ( width != sizeStd || height != sizeStd )
89 {
90 wxASSERT_MSG( width <= sizeStd && height <= sizeStd,
91 _T("bitmap shouldn't be cropped") );
92
93 m_bmpNormal.Create(sizeStd, sizeStd);
94 wxMemoryDC dcMem;
95 dcMem.SelectObject(m_bmpNormal);
96 dcMem.Clear();
97
98 dcMem.DrawBitmap(bitmap,
99 (sizeStd - width)/2, (sizeStd-height)/2,
100 true);
101 }
102 }
103 //else: let the system rescale the bitmap
104 }
105
106 if ( !m_bmpNormal.Ok() )
107 m_bmpNormal = bitmap;
108
109
d32be04c
SC
110#ifdef __WXMAC_OSX__
111 if ( HasFlag( wxBORDER_NONE ) )
112 {
cc374a2f
DS
113 wxMacCreateBitmapButton( &info, m_bmpNormal, kControlContentIconRef );
114 err = CreateIconControl(
2acac1b4
VZ
115 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
116 &bounds, &info, false, m_peer->GetControlRefAddr() );
d32be04c
SC
117 }
118 else
119#endif
120 {
cc374a2f
DS
121 wxMacCreateBitmapButton( &info, m_bmpNormal );
122 err = CreateBevelButtonControl(
2acac1b4
VZ
123 MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds, CFSTR(""),
124 ((style & wxBU_AUTODRAW) ? kControlBevelButtonSmallBevel : kControlBevelButtonNormalBevel ),
125 kControlBehaviorOffsetContents, &info, 0, 0, 0, m_peer->GetControlRefAddr() );
d32be04c 126 }
e9576ca5 127
cc374a2f
DS
128 verify_noerr( err );
129
130 wxMacReleaseBitmapButton( &info );
131 wxASSERT_MSG( m_peer != NULL && m_peer->Ok(), wxT("No valid native Mac control") );
132
133 MacPostControlCreate( pos, size );
134
135 return true;
e9576ca5
SC
136}
137
cc374a2f 138void wxBitmapButton::SetBitmapLabel( const wxBitmap& bitmap )
e9576ca5 139{
d460ed40 140 m_bmpNormal = bitmap;
9f884528 141 InvalidateBestSize();
3dec57ad 142
cc374a2f
DS
143 ControlButtonContentInfo info;
144
d32be04c
SC
145#ifdef __WXMAC_OSX__
146 if ( HasFlag( wxBORDER_NONE ) )
147 {
cc374a2f 148 wxMacCreateBitmapButton( &info, m_bmpNormal, kControlContentIconRef );
d32be04c 149 if ( info.contentType != kControlNoContent )
cc374a2f 150 m_peer->SetData( kControlIconPart, kControlIconContentTag, info );
d32be04c
SC
151 }
152 else
153#endif
d460ed40 154 {
cc374a2f 155 wxMacCreateBitmapButton( &info, m_bmpNormal );
d32be04c 156 if ( info.contentType != kControlNoContent )
cc374a2f 157 m_peer->SetData( kControlButtonPart, kControlBevelButtonContentTag, info );
3dec57ad 158 }
e9576ca5 159
cc374a2f
DS
160 wxMacReleaseBitmapButton( &info );
161}
c0831a3c
RD
162
163wxSize wxBitmapButton::DoGetBestSize() const
164{
165 wxSize best;
cc374a2f
DS
166
167 best.x = 2 * m_marginX;
168 best.y = 2 * m_marginY;
169 if ( m_bmpNormal.Ok() )
c0831a3c 170 {
cc374a2f
DS
171 best.x += m_bmpNormal.GetWidth();
172 best.y += m_bmpNormal.GetHeight();
c0831a3c 173 }
cc374a2f 174
c0831a3c
RD
175 return best;
176}
179e085f
RN
177
178#endif