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"
28 // define a derived class to override SetBitmap() and also to provide
29 // InitButtonContentInfo() helper used by CreateBitmapButton()
30 class wxMacBitmapButton
: public wxMacControl
, public wxButtonImpl
33 wxMacBitmapButton(wxWindowMac
* peer
, const wxBitmap
& bitmap
, int style
)
36 // decide what kind of contents the button will have: we want to use an
37 // icon for buttons with wxBORDER_NONE style as bevel buttons always do
38 // have a border but icons are limited to a few standard sizes only and
39 // are resized by the system with extremely ugly results if they don't
40 // fit (in the past we also tried being smart and pasting a bitmap
41 // instead of a larger square icon to avoid resizing but this resulted
42 // in buttons having different size than specified by wx API and
43 // breaking the layouts and still didn't look good so we don't even try
44 // to do this any more)
45 m_isIcon
= (style
& wxBORDER_NONE
) &&
46 bitmap
.IsOk() && IsOfStandardSize(bitmap
);
49 virtual void SetBitmap(const wxBitmap
& bitmap
)
51 // unfortunately we can't avoid the ugly resizing problem mentioned
52 // above if a bitmap of supported size was used initially but was
53 // replaced with another one later as the control was already created
54 // as an icon control (although maybe we ought to recreate it?)
55 ControlButtonContentInfo info
;
56 InitButtonContentInfo(info
, bitmap
);
58 if ( info
.contentType
== kControlContentIconRef
)
59 SetData(kControlIconPart
, kControlIconContentTag
, info
);
60 else if ( info
.contentType
!= kControlNoContent
)
61 SetData(kControlButtonPart
, kControlBevelButtonContentTag
, info
);
63 wxMacReleaseBitmapButton(&info
);
66 void InitButtonContentInfo(ControlButtonContentInfo
& info
,
67 const wxBitmap
& bitmap
)
69 wxMacCreateBitmapButton(&info
, bitmap
,
70 m_isIcon
? kControlContentIconRef
: 0);
73 void SetPressedBitmap( const wxBitmap
& WXUNUSED(bitmap
) )
75 // not implemented under Carbon
79 // helper function: returns true if the given bitmap is of one of standard
80 // sizes supported by OS X icons
81 static bool IsOfStandardSize(const wxBitmap
& bmp
)
83 const int w
= bmp
.GetWidth();
85 return bmp
.GetHeight() == w
&&
86 (w
== 128 || w
== 48 || w
== 32 || w
== 16);
90 // true if this is an icon control, false if it's a bevel button
93 wxDECLARE_NO_COPY_CLASS(wxMacBitmapButton
);
96 } // anonymous namespace
98 wxWidgetImplType
* wxWidgetImpl::CreateBitmapButton( wxWindowMac
* wxpeer
,
100 wxWindowID
WXUNUSED(id
),
101 const wxBitmap
& bitmap
,
105 long WXUNUSED(extraStyle
))
107 wxMacBitmapButton
* peer
= new wxMacBitmapButton(wxpeer
, bitmap
, style
);
110 WXWindow macParent
= MAC_WXHWND(parent
->MacGetTopLevelWindowRef());
111 Rect bounds
= wxMacGetBoundsForControl( wxpeer
, pos
, size
);
113 ControlButtonContentInfo info
;
114 peer
->InitButtonContentInfo(info
, bitmap
);
116 if ( info
.contentType
== kControlContentIconRef
)
118 err
= CreateIconControl
124 peer
->GetControlRefAddr()
127 else // normal bevel button
129 err
= CreateBevelButtonControl
134 style
& wxBU_AUTODRAW
? kControlBevelButtonSmallBevel
135 : kControlBevelButtonNormalBevel
,
136 kControlBehaviorOffsetContents
,
138 0, // menu id (no associated menu)
139 0, // menu behaviour (unused)
140 0, // menu placement (unused too)
141 peer
->GetControlRefAddr()
147 wxMacReleaseBitmapButton( &info
);
151 #endif // wxUSE_BMPBUTTON