1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/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"
19 #include "wx/dcmemory.h"
22 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton
, wxButton
)
24 #include "wx/mac/uma.h"
26 bool wxBitmapButton::Create( wxWindow
*parent
,
27 wxWindowID id
, const wxBitmap
& bitmap
,
31 const wxValidator
& validator
,
32 const wxString
& name
)
34 m_macIsUserPane
= false;
36 // since bitmapbuttonbase is subclass of button calling wxBitmapButtonBase::Create
37 // essentially creates an additional button
38 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
41 if ( style
& wxBU_AUTODRAW
)
44 m_marginY
= wxDEFAULT_BUTTON_MARGIN
;
53 ControlButtonContentInfo info
;
55 Rect bounds
= wxMacGetBoundsForControl( this, pos
, size
);
56 m_peer
= new wxMacControl( this );
58 if ( bitmap
.Ok() && HasFlag(wxBORDER_NONE
) )
60 // in Mac OS X the icon controls (which are used for borderless bitmap
61 // buttons) can have only one of the few standard sizes and if they
62 // don't, the OS rescales them automatically resulting in really ugly
63 // images, so centre the image in a square of standard size instead
65 // the supported sizes, sorted in decreasng order
66 static const int stdSizes
[] = { 128, 48, 32, 16, 0 };
68 const int width
= bitmap
.GetWidth();
69 const int height
= bitmap
.GetHeight();
72 for ( n
= 0; n
< (int)WXSIZEOF(stdSizes
); n
++ )
74 const int sizeStd
= stdSizes
[n
];
75 if ( width
> sizeStd
|| height
> sizeStd
)
77 // it will become -1 if the bitmap is larger than the biggest
78 // supported size, this is intentional
87 const int sizeStd
= stdSizes
[n
];
88 if ( width
!= sizeStd
|| height
!= sizeStd
)
90 wxASSERT_MSG( width
<= sizeStd
&& height
<= sizeStd
,
91 _T("bitmap shouldn't be cropped") );
93 m_bmpNormal
.Create(sizeStd
, sizeStd
);
95 dcMem
.SelectObject(m_bmpNormal
);
98 dcMem
.DrawBitmap(bitmap
,
99 (sizeStd
- width
)/2, (sizeStd
-height
)/2,
103 //else: let the system rescale the bitmap
106 if ( !m_bmpNormal
.Ok() )
107 m_bmpNormal
= bitmap
;
111 if ( HasFlag( wxBORDER_NONE
) )
113 wxMacCreateBitmapButton( &info
, m_bmpNormal
, kControlContentIconRef
);
114 err
= CreateIconControl(
115 MAC_WXHWND(parent
->MacGetTopLevelWindowRef()),
116 &bounds
, &info
, false, m_peer
->GetControlRefAddr() );
121 wxMacCreateBitmapButton( &info
, m_bmpNormal
);
122 err
= CreateBevelButtonControl(
123 MAC_WXHWND(parent
->MacGetTopLevelWindowRef()), &bounds
, CFSTR(""),
124 ((style
& wxBU_AUTODRAW
) ? kControlBevelButtonSmallBevel
: kControlBevelButtonNormalBevel
),
125 kControlBehaviorOffsetContents
, &info
, 0, 0, 0, m_peer
->GetControlRefAddr() );
130 wxMacReleaseBitmapButton( &info
);
131 wxASSERT_MSG( m_peer
!= NULL
&& m_peer
->Ok(), wxT("No valid native Mac control") );
133 MacPostControlCreate( pos
, size
);
138 void wxBitmapButton::SetBitmapLabel( const wxBitmap
& bitmap
)
140 m_bmpNormal
= bitmap
;
141 InvalidateBestSize();
143 ControlButtonContentInfo info
;
146 if ( HasFlag( wxBORDER_NONE
) )
148 wxMacCreateBitmapButton( &info
, m_bmpNormal
, kControlContentIconRef
);
149 if ( info
.contentType
!= kControlNoContent
)
150 m_peer
->SetData( kControlIconPart
, kControlIconContentTag
, info
);
155 wxMacCreateBitmapButton( &info
, m_bmpNormal
);
156 if ( info
.contentType
!= kControlNoContent
)
157 m_peer
->SetData( kControlButtonPart
, kControlBevelButtonContentTag
, info
);
160 wxMacReleaseBitmapButton( &info
);
163 wxSize
wxBitmapButton::DoGetBestSize() const
167 best
.x
= 2 * m_marginX
;
168 best
.y
= 2 * m_marginY
;
169 if ( m_bmpNormal
.Ok() )
171 best
.x
+= m_bmpNormal
.GetWidth();
172 best
.y
+= m_bmpNormal
.GetHeight();