]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/bmpbuttn.cpp
better native types for carbon
[wxWidgets.git] / src / osx / carbon / bmpbuttn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/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 #include "wx/image.h"
18
19 #ifndef WX_PRECOMP
20 #include "wx/dcmemory.h"
21 #endif
22
23 #include "wx/osx/private.h"
24
25 //---------------------------------------------------------------------------
26 // Helper functions
27
28 static wxBitmap wxMakeStdSizeBitmap(const wxBitmap& bitmap)
29 {
30 // in Mac OS X the icon controls (which are used for borderless bitmap
31 // buttons) can have only one of the few standard sizes and if they
32 // don't, the OS rescales them automatically resulting in really ugly
33 // images, so centre the image in a square of standard size instead
34
35 // the supported sizes, sorted in decreasng order
36 static const int stdSizes[] = { 128, 48, 32, 16, 0 };
37
38 const int width = bitmap.GetWidth();
39 const int height = bitmap.GetHeight();
40
41 wxBitmap newBmp(bitmap);
42
43 int n;
44 for ( n = 0; n < (int)WXSIZEOF(stdSizes); n++ )
45 {
46 const int sizeStd = stdSizes[n];
47 if ( width > sizeStd || height > sizeStd )
48 {
49 // it will become -1 if the bitmap is larger than the biggest
50 // supported size, this is intentional
51 n--;
52
53 break;
54 }
55 }
56
57 if ( n != -1 )
58 {
59 const int sizeStd = stdSizes[n];
60 if ( width != sizeStd || height != sizeStd )
61 {
62 wxASSERT_MSG( width <= sizeStd && height <= sizeStd,
63 _T("bitmap shouldn't be cropped") );
64
65 wxImage square_image = bitmap.ConvertToImage();
66 newBmp = square_image.Size
67 (
68 wxSize(sizeStd, sizeStd),
69 wxPoint((sizeStd - width)/2, (sizeStd-height)/2)
70 );
71 }
72 }
73 //else: let the system rescale the bitmap
74
75 return newBmp;
76 }
77
78 //---------------------------------------------------------------------------
79
80 class wxMacBitmapButton : public wxMacControl
81 {
82 public:
83 wxMacBitmapButton( wxWindowMac* peer ) : wxMacControl(peer)
84 {
85 }
86
87 void SetBitmap(const wxBitmap& bitmap)
88 {
89 wxBitmap bmp;
90 if ( GetWXPeer()->HasFlag( wxBORDER_NONE ) )
91 {
92 bmp = wxMakeStdSizeBitmap(bitmap);
93 // TODO set bitmap in peer as well
94 }
95 else
96 bmp = bitmap;
97
98 ControlButtonContentInfo info;
99
100 if ( GetWXPeer()->HasFlag( wxBORDER_NONE ) )
101 {
102 wxMacCreateBitmapButton( &info, bmp, kControlContentIconRef );
103 if ( info.contentType != kControlNoContent )
104 SetData( kControlIconPart, kControlIconContentTag, info );
105 }
106 else
107 {
108 wxMacCreateBitmapButton( &info, bmp );
109 if ( info.contentType != kControlNoContent )
110 SetData( kControlButtonPart, kControlBevelButtonContentTag, info );
111 }
112
113 wxMacReleaseBitmapButton( &info );
114 }
115 };
116
117 wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
118 wxWindowMac* parent,
119 wxWindowID id,
120 const wxBitmap& bitmap,
121 const wxPoint& pos,
122 const wxSize& size,
123 long style,
124 long extraStyle)
125 {
126 OSStatus err = noErr;
127 ControlButtonContentInfo info;
128
129 Rect bounds = wxMacGetBoundsForControl( wxpeer, pos, size );
130 wxMacControl* peer = new wxMacBitmapButton( wxpeer );
131 wxBitmap bmp;
132
133 if ( bitmap.Ok() && (style & wxBORDER_NONE) )
134 {
135 bmp = wxMakeStdSizeBitmap(bitmap);
136 // TODO set bitmap in peer as well
137 }
138 else
139 bmp = bitmap;
140
141
142 if ( style & wxBORDER_NONE )
143 {
144 // contrary to the docs this control only works with iconrefs
145 wxMacCreateBitmapButton( &info, bmp, kControlContentIconRef );
146 err = CreateIconControl(
147 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
148 &bounds, &info, false, peer->GetControlRefAddr() );
149 }
150 else
151 {
152 wxMacCreateBitmapButton( &info, bmp );
153 err = CreateBevelButtonControl(
154 MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds, CFSTR(""),
155 ((style & wxBU_AUTODRAW) ? kControlBevelButtonSmallBevel : kControlBevelButtonNormalBevel ),
156 kControlBehaviorOffsetContents, &info, 0, 0, 0, peer->GetControlRefAddr() );
157 }
158
159 verify_noerr( err );
160
161 wxMacReleaseBitmapButton( &info );
162 return peer;
163 }
164 #endif