1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/bmpbuttn.cpp
3 // Purpose: wxBitmapButton
4 // Author: Stefan Csomor
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
15 #include "wx/bmpbuttn.h"
19 #include "wx/dcmemory.h"
22 #include "wx/osx/private.h"
27 // define a derived class to override SetBitmap() and also to provide
28 // InitButtonContentInfo() helper used by CreateBitmapButton()
29 class wxMacBitmapButton
: public wxMacControl
, public wxButtonImpl
32 wxMacBitmapButton(wxWindowMac
* peer
, const wxBitmap
& bitmap
, int style
)
35 // decide what kind of contents the button will have: we want to use an
36 // icon for buttons with wxBORDER_NONE style as bevel buttons always do
37 // have a border but icons are limited to a few standard sizes only and
38 // are resized by the system with extremely ugly results if they don't
39 // fit (in the past we also tried being smart and pasting a bitmap
40 // instead of a larger square icon to avoid resizing but this resulted
41 // in buttons having different size than specified by wx API and
42 // breaking the layouts and still didn't look good so we don't even try
43 // to do this any more)
44 m_isIcon
= (style
& wxBORDER_NONE
) &&
45 bitmap
.IsOk() && IsOfStandardSize(bitmap
);
48 virtual void SetBitmap(const wxBitmap
& bitmap
)
50 // unfortunately we can't avoid the ugly resizing problem mentioned
51 // above if a bitmap of supported size was used initially but was
52 // replaced with another one later as the control was already created
53 // as an icon control (although maybe we ought to recreate it?)
54 ControlButtonContentInfo info
;
55 InitButtonContentInfo(info
, bitmap
);
57 if ( info
.contentType
== kControlContentIconRef
)
58 SetData(kControlIconPart
, kControlIconContentTag
, info
);
59 else if ( info
.contentType
!= kControlNoContent
)
60 SetData(kControlButtonPart
, kControlBevelButtonContentTag
, info
);
62 wxMacReleaseBitmapButton(&info
);
65 void InitButtonContentInfo(ControlButtonContentInfo
& info
,
66 const wxBitmap
& bitmap
)
68 wxMacCreateBitmapButton(&info
, bitmap
,
69 m_isIcon
? kControlContentIconRef
: 0);
72 void SetPressedBitmap( const wxBitmap
& WXUNUSED(bitmap
) )
74 // not implemented under Carbon
78 // helper function: returns true if the given bitmap is of one of standard
79 // sizes supported by OS X icons
80 static bool IsOfStandardSize(const wxBitmap
& bmp
)
82 const int w
= bmp
.GetWidth();
84 return bmp
.GetHeight() == w
&&
85 (w
== 128 || w
== 48 || w
== 32 || w
== 16);
89 // true if this is an icon control, false if it's a bevel button
92 wxDECLARE_NO_COPY_CLASS(wxMacBitmapButton
);
95 } // anonymous namespace
97 wxWidgetImplType
* wxWidgetImpl::CreateBitmapButton( wxWindowMac
* wxpeer
,
99 wxWindowID
WXUNUSED(id
),
100 const wxBitmap
& bitmap
,
104 long WXUNUSED(extraStyle
))
106 wxMacBitmapButton
* peer
= new wxMacBitmapButton(wxpeer
, bitmap
, style
);
109 WXWindow macParent
= MAC_WXHWND(parent
->MacGetTopLevelWindowRef());
110 Rect bounds
= wxMacGetBoundsForControl( wxpeer
, pos
, size
);
112 ControlButtonContentInfo info
;
113 peer
->InitButtonContentInfo(info
, bitmap
);
115 if ( info
.contentType
== kControlContentIconRef
)
117 err
= CreateIconControl
123 peer
->GetControlRefAddr()
126 else // normal bevel button
128 err
= CreateBevelButtonControl
133 style
& wxBU_AUTODRAW
? kControlBevelButtonSmallBevel
134 : kControlBevelButtonNormalBevel
,
135 kControlBehaviorOffsetContents
,
137 0, // menu id (no associated menu)
138 0, // menu behaviour (unused)
139 0, // menu placement (unused too)
140 peer
->GetControlRefAddr()
146 wxMacReleaseBitmapButton( &info
);
150 #endif // wxUSE_BMPBUTTON