1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/bmpbuttn.cpp
3 // Purpose: wxBitmapButton
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/bmpbuttn.h"
20 #include "wx/dcmemory.h"
23 #include "wx/osx/private.h"
25 //---------------------------------------------------------------------------
28 static wxBitmap
wxMakeStdSizeBitmap(const wxBitmap
& bitmap
)
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
35 // the supported sizes, sorted in decreasng order
36 static const int stdSizes
[] = { 128, 48, 32, 16, 0 };
38 const int width
= bitmap
.GetWidth();
39 const int height
= bitmap
.GetHeight();
41 wxBitmap
newBmp(bitmap
);
44 for ( n
= 0; n
< (int)WXSIZEOF(stdSizes
); n
++ )
46 const int sizeStd
= stdSizes
[n
];
47 if ( width
> sizeStd
|| height
> sizeStd
)
49 // it will become -1 if the bitmap is larger than the biggest
50 // supported size, this is intentional
59 const int sizeStd
= stdSizes
[n
];
60 if ( width
!= sizeStd
|| height
!= sizeStd
)
62 wxASSERT_MSG( width
<= sizeStd
&& height
<= sizeStd
,
63 _T("bitmap shouldn't be cropped") );
65 wxImage square_image
= bitmap
.ConvertToImage();
66 newBmp
= square_image
.Size
68 wxSize(sizeStd
, sizeStd
),
69 wxPoint((sizeStd
- width
)/2, (sizeStd
-height
)/2)
73 //else: let the system rescale the bitmap
78 //---------------------------------------------------------------------------
80 class wxMacBitmapButton
: public wxMacControl
83 wxMacBitmapButton( wxWindowMac
* peer
) : wxMacControl(peer
)
87 void SetBitmap(const wxBitmap
& bitmap
)
90 if ( GetWXPeer()->HasFlag( wxBORDER_NONE
) )
92 bmp
= wxMakeStdSizeBitmap(bitmap
);
93 // TODO set bitmap in peer as well
98 ControlButtonContentInfo info
;
100 if ( GetWXPeer()->HasFlag( wxBORDER_NONE
) )
102 wxMacCreateBitmapButton( &info
, bmp
, kControlContentIconRef
);
103 if ( info
.contentType
!= kControlNoContent
)
104 SetData( kControlIconPart
, kControlIconContentTag
, info
);
108 wxMacCreateBitmapButton( &info
, bmp
);
109 if ( info
.contentType
!= kControlNoContent
)
110 SetData( kControlButtonPart
, kControlBevelButtonContentTag
, info
);
113 wxMacReleaseBitmapButton( &info
);
117 wxWidgetImplType
* wxWidgetImpl::CreateBitmapButton( wxWindowMac
* wxpeer
,
119 wxWindowID
WXUNUSED(id
),
120 const wxBitmap
& bitmap
,
124 long WXUNUSED(extraStyle
))
126 OSStatus err
= noErr
;
127 ControlButtonContentInfo info
;
129 Rect bounds
= wxMacGetBoundsForControl( wxpeer
, pos
, size
);
130 wxMacControl
* peer
= new wxMacBitmapButton( wxpeer
);
133 if ( bitmap
.Ok() && (style
& wxBORDER_NONE
) )
135 bmp
= wxMakeStdSizeBitmap(bitmap
);
136 // TODO set bitmap in peer as well
142 if ( style
& wxBORDER_NONE
)
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() );
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() );
161 wxMacReleaseBitmapButton( &info
);