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"
20 #include "wx/dcmemory.h"
23 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton
, wxButton
)
25 #include "wx/osx/uma.h"
27 //---------------------------------------------------------------------------
30 static wxBitmap
wxMakeStdSizeBitmap(const wxBitmap
& bitmap
)
32 // in Mac OS X the icon controls (which are used for borderless bitmap
33 // buttons) can have only one of the few standard sizes and if they
34 // don't, the OS rescales them automatically resulting in really ugly
35 // images, so centre the image in a square of standard size instead
37 // the supported sizes, sorted in decreasng order
38 static const int stdSizes
[] = { 128, 48, 32, 16, 0 };
40 const int width
= bitmap
.GetWidth();
41 const int height
= bitmap
.GetHeight();
43 wxBitmap
newBmp(bitmap
);
46 for ( n
= 0; n
< (int)WXSIZEOF(stdSizes
); n
++ )
48 const int sizeStd
= stdSizes
[n
];
49 if ( width
> sizeStd
|| height
> sizeStd
)
51 // it will become -1 if the bitmap is larger than the biggest
52 // supported size, this is intentional
61 const int sizeStd
= stdSizes
[n
];
62 if ( width
!= sizeStd
|| height
!= sizeStd
)
64 wxASSERT_MSG( width
<= sizeStd
&& height
<= sizeStd
,
65 _T("bitmap shouldn't be cropped") );
67 wxImage square_image
= bitmap
.ConvertToImage();
68 newBmp
= square_image
.Size
70 wxSize(sizeStd
, sizeStd
),
71 wxPoint((sizeStd
- width
)/2, (sizeStd
-height
)/2)
75 //else: let the system rescale the bitmap
80 //---------------------------------------------------------------------------
82 bool wxBitmapButton::Create( wxWindow
*parent
,
83 wxWindowID id
, const wxBitmap
& bitmap
,
87 const wxValidator
& validator
,
88 const wxString
& name
)
90 m_macIsUserPane
= false;
92 // since bitmapbuttonbase is subclass of button calling wxBitmapButtonBase::Create
93 // essentially creates an additional button
94 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
97 if ( style
& wxBU_AUTODRAW
)
100 m_marginY
= wxDEFAULT_BUTTON_MARGIN
;
108 OSStatus err
= noErr
;
109 ControlButtonContentInfo info
;
111 Rect bounds
= wxMacGetBoundsForControl( this, pos
, size
);
112 m_peer
= new wxMacControl( this );
114 if ( bitmap
.Ok() && HasFlag(wxBORDER_NONE
) )
115 m_bmpNormal
= wxMakeStdSizeBitmap(bitmap
);
117 m_bmpNormal
= bitmap
;
120 if ( HasFlag( wxBORDER_NONE
) )
122 // contrary to the docs this control only works with iconrefs
123 wxMacCreateBitmapButton( &info
, m_bmpNormal
, kControlContentIconRef
);
124 err
= CreateIconControl(
125 MAC_WXHWND(parent
->MacGetTopLevelWindowRef()),
126 &bounds
, &info
, false, m_peer
->GetControlRefAddr() );
130 wxMacCreateBitmapButton( &info
, m_bmpNormal
);
131 err
= CreateBevelButtonControl(
132 MAC_WXHWND(parent
->MacGetTopLevelWindowRef()), &bounds
, CFSTR(""),
133 ((style
& wxBU_AUTODRAW
) ? kControlBevelButtonSmallBevel
: kControlBevelButtonNormalBevel
),
134 kControlBehaviorOffsetContents
, &info
, 0, 0, 0, m_peer
->GetControlRefAddr() );
139 wxMacReleaseBitmapButton( &info
);
140 wxASSERT_MSG( m_peer
!= NULL
&& m_peer
->Ok(), wxT("No valid native Mac control") );
142 MacPostControlCreate( pos
, size
);
147 void wxBitmapButton::SetBitmapLabel( const wxBitmap
& bitmap
)
149 if ( HasFlag( wxBORDER_NONE
) )
150 m_bmpNormal
= wxMakeStdSizeBitmap(bitmap
);
152 m_bmpNormal
= bitmap
;
154 InvalidateBestSize();
156 ControlButtonContentInfo info
;
158 if ( HasFlag( wxBORDER_NONE
) )
160 wxMacCreateBitmapButton( &info
, m_bmpNormal
, kControlContentIconRef
);
161 if ( info
.contentType
!= kControlNoContent
)
162 m_peer
->SetData( kControlIconPart
, kControlIconContentTag
, info
);
166 wxMacCreateBitmapButton( &info
, m_bmpNormal
);
167 if ( info
.contentType
!= kControlNoContent
)
168 m_peer
->SetData( kControlButtonPart
, kControlBevelButtonContentTag
, info
);
171 wxMacReleaseBitmapButton( &info
);
174 wxSize
wxBitmapButton::DoGetBestSize() const
178 best
.x
= 2 * m_marginX
;
179 best
.y
= 2 * m_marginY
;
180 if ( m_bmpNormal
.Ok() )
182 best
.x
+= m_bmpNormal
.GetWidth();
183 best
.y
+= m_bmpNormal
.GetHeight();