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