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 //---------------------------------------------------------------------------
29 static wxBitmap
wxMakeStdSizeBitmap(const wxBitmap
& bitmap
)
31 // in Mac OS X the icon controls (which are used for borderless bitmap
32 // buttons) can have only one of the few standard sizes and if they
33 // don't, the OS rescales them automatically resulting in really ugly
34 // images, so centre the image in a square of standard size instead
36 // the supported sizes, sorted in decreasng order
37 static const int stdSizes
[] = { 128, 48, 32, 16, 0 };
39 const int width
= bitmap
.GetWidth();
40 const int height
= bitmap
.GetHeight();
42 wxBitmap
newBmp(bitmap
);
45 for ( n
= 0; n
< (int)WXSIZEOF(stdSizes
); n
++ )
47 const int sizeStd
= stdSizes
[n
];
48 if ( width
> sizeStd
|| height
> sizeStd
)
50 // it will become -1 if the bitmap is larger than the biggest
51 // supported size, this is intentional
60 const int sizeStd
= stdSizes
[n
];
61 if ( width
!= sizeStd
|| height
!= sizeStd
)
63 wxASSERT_MSG( width
<= sizeStd
&& height
<= sizeStd
,
64 _T("bitmap shouldn't be cropped") );
66 newBmp
.Create(sizeStd
, sizeStd
);
68 dcMem
.SelectObject(newBmp
);
71 dcMem
.DrawBitmap(bitmap
,
72 (sizeStd
- width
)/2, (sizeStd
-height
)/2,
76 //else: let the system rescale the bitmap
81 //---------------------------------------------------------------------------
83 bool wxBitmapButton::Create( wxWindow
*parent
,
84 wxWindowID id
, const wxBitmap
& bitmap
,
88 const wxValidator
& validator
,
89 const wxString
& name
)
91 m_macIsUserPane
= false;
93 // since bitmapbuttonbase is subclass of button calling wxBitmapButtonBase::Create
94 // essentially creates an additional button
95 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
98 if ( style
& wxBU_AUTODRAW
)
101 m_marginY
= wxDEFAULT_BUTTON_MARGIN
;
109 OSStatus err
= noErr
;
110 ControlButtonContentInfo info
;
112 Rect bounds
= wxMacGetBoundsForControl( this, pos
, size
);
113 m_peer
= new wxMacControl( this );
115 if ( bitmap
.Ok() && HasFlag(wxBORDER_NONE
) )
116 m_bmpNormal
= wxMakeStdSizeBitmap(bitmap
);
118 m_bmpNormal
= bitmap
;
121 if ( HasFlag( wxBORDER_NONE
) )
123 // contrary to the docs this control only works with iconrefs
124 wxMacCreateBitmapButton( &info
, m_bmpNormal
, kControlContentIconRef
);
125 err
= CreateIconControl(
126 MAC_WXHWND(parent
->MacGetTopLevelWindowRef()),
127 &bounds
, &info
, false, m_peer
->GetControlRefAddr() );
131 wxMacCreateBitmapButton( &info
, m_bmpNormal
);
132 err
= CreateBevelButtonControl(
133 MAC_WXHWND(parent
->MacGetTopLevelWindowRef()), &bounds
, CFSTR(""),
134 ((style
& wxBU_AUTODRAW
) ? kControlBevelButtonSmallBevel
: kControlBevelButtonNormalBevel
),
135 kControlBehaviorOffsetContents
, &info
, 0, 0, 0, m_peer
->GetControlRefAddr() );
140 wxMacReleaseBitmapButton( &info
);
141 wxASSERT_MSG( m_peer
!= NULL
&& m_peer
->Ok(), wxT("No valid native Mac control") );
143 MacPostControlCreate( pos
, size
);
148 void wxBitmapButton::SetBitmapLabel( const wxBitmap
& bitmap
)
150 if ( HasFlag( wxBORDER_NONE
) )
151 m_bmpNormal
= wxMakeStdSizeBitmap(bitmap
);
153 m_bmpNormal
= bitmap
;
155 InvalidateBestSize();
157 ControlButtonContentInfo info
;
159 if ( HasFlag( wxBORDER_NONE
) )
161 wxMacCreateBitmapButton( &info
, m_bmpNormal
, kControlContentIconRef
);
162 if ( info
.contentType
!= kControlNoContent
)
163 m_peer
->SetData( kControlIconPart
, kControlIconContentTag
, info
);
167 wxMacCreateBitmapButton( &info
, m_bmpNormal
);
168 if ( info
.contentType
!= kControlNoContent
)
169 m_peer
->SetData( kControlButtonPart
, kControlBevelButtonContentTag
, info
);
172 wxMacReleaseBitmapButton( &info
);
175 wxSize
wxBitmapButton::DoGetBestSize() const
179 best
.x
= 2 * m_marginX
;
180 best
.y
= 2 * m_marginY
;
181 if ( m_bmpNormal
.Ok() )
183 best
.x
+= m_bmpNormal
.GetWidth();
184 best
.y
+= m_bmpNormal
.GetHeight();