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/window.h"
20 #include "wx/bitmap.h"
23 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton
, wxButton
)
25 #include "wx/mac/uma.h"
27 bool wxBitmapButton::Create( wxWindow
*parent
,
28 wxWindowID id
, const wxBitmap
& bitmap
,
32 const wxValidator
& validator
,
33 const wxString
& name
)
35 m_macIsUserPane
= false;
37 // since bitmapbuttonbase is subclass of button calling wxBitmapButtonBase::Create
38 // essentially creates an additional button
39 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
42 if ( style
& wxBU_AUTODRAW
)
45 m_marginY
= wxDEFAULT_BUTTON_MARGIN
;
54 ControlButtonContentInfo info
;
56 Rect bounds
= wxMacGetBoundsForControl( this, pos
, size
);
57 m_peer
= new wxMacControl( this );
59 if ( bitmap
.Ok() && !size
.IsFullySpecified() )
61 // in Mac OS X the bitmap buttons can have only one of the few standard
62 // sizes and if they don't, the OS rescales them automatically
63 // resulting in really ugly images, so centre the image in a square of
64 // standard size instead
66 // the supported sizes, sorted in decreasng order
67 static const int stdSizes
[] = { 128, 48, 32, 16, 0 };
69 const int width
= bitmap
.GetWidth();
70 const int height
= bitmap
.GetHeight();
73 for ( n
= 0; n
< (int)WXSIZEOF(stdSizes
); n
++ )
75 const int sizeStd
= stdSizes
[n
];
76 if ( width
> sizeStd
|| height
> sizeStd
)
78 // it will become -1 if the bitmap is larger than the biggest
79 // supported size, this is intentional
88 const int sizeStd
= stdSizes
[n
];
89 if ( width
!= sizeStd
|| height
!= sizeStd
)
91 wxASSERT_MSG( width
<= sizeStd
&& height
<= sizeStd
,
92 _T("bitmap shouldn't be cropped") );
94 m_bmpNormal
.Create(sizeStd
, sizeStd
);
96 dcMem
.SelectObject(m_bmpNormal
);
99 dcMem
.DrawBitmap(bitmap
,
100 (sizeStd
- width
)/2, (sizeStd
-height
)/2,
104 //else: let the system rescale the bitmap
107 if ( !m_bmpNormal
.Ok() )
108 m_bmpNormal
= bitmap
;
112 if ( HasFlag( wxBORDER_NONE
) )
114 wxMacCreateBitmapButton( &info
, m_bmpNormal
, kControlContentIconRef
);
115 err
= CreateIconControl(
116 MAC_WXHWND(parent
->MacGetTopLevelWindowRef()),
117 &bounds
, &info
, false, m_peer
->GetControlRefAddr() );
122 wxMacCreateBitmapButton( &info
, m_bmpNormal
);
123 err
= CreateBevelButtonControl(
124 MAC_WXHWND(parent
->MacGetTopLevelWindowRef()), &bounds
, CFSTR(""),
125 ((style
& wxBU_AUTODRAW
) ? kControlBevelButtonSmallBevel
: kControlBevelButtonNormalBevel
),
126 kControlBehaviorOffsetContents
, &info
, 0, 0, 0, m_peer
->GetControlRefAddr() );
131 wxMacReleaseBitmapButton( &info
);
132 wxASSERT_MSG( m_peer
!= NULL
&& m_peer
->Ok(), wxT("No valid native Mac control") );
134 MacPostControlCreate( pos
, size
);
139 void wxBitmapButton::SetBitmapLabel( const wxBitmap
& bitmap
)
141 m_bmpNormal
= bitmap
;
142 InvalidateBestSize();
144 ControlButtonContentInfo info
;
147 if ( HasFlag( wxBORDER_NONE
) )
149 wxMacCreateBitmapButton( &info
, m_bmpNormal
, kControlContentIconRef
);
150 if ( info
.contentType
!= kControlNoContent
)
151 m_peer
->SetData( kControlIconPart
, kControlIconContentTag
, info
);
156 wxMacCreateBitmapButton( &info
, m_bmpNormal
);
157 if ( info
.contentType
!= kControlNoContent
)
158 m_peer
->SetData( kControlButtonPart
, kControlBevelButtonContentTag
, info
);
161 wxMacReleaseBitmapButton( &info
);
164 wxSize
wxBitmapButton::DoGetBestSize() const
168 best
.x
= 2 * m_marginX
;
169 best
.y
= 2 * m_marginY
;
170 if ( m_bmpNormal
.Ok() )
172 best
.x
+= m_bmpNormal
.GetWidth();
173 best
.y
+= m_bmpNormal
.GetHeight();