]>
Commit | Line | Data |
---|---|---|
e9576ca5 | 1 | ///////////////////////////////////////////////////////////////////////////// |
cc374a2f | 2 | // Name: src/mac/carbon/bmpbuttn.cpp |
e9576ca5 | 3 | // Purpose: wxBitmapButton |
a31a5f85 | 4 | // Author: Stefan Csomor |
e9576ca5 | 5 | // Modified by: |
a31a5f85 | 6 | // Created: 1998-01-01 |
e9576ca5 | 7 | // RCS-ID: $Id$ |
a31a5f85 | 8 | // Copyright: (c) Stefan Csomor |
0bca0373 | 9 | // Licence: wxWindows licence |
e9576ca5 SC |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
a8e9860d SC |
12 | #include "wx/wxprec.h" |
13 | ||
179e085f RN |
14 | #if wxUSE_BMPBUTTON |
15 | ||
e9576ca5 SC |
16 | #include "wx/bmpbuttn.h" |
17 | ||
cdccdfab | 18 | #ifndef WX_PRECOMP |
85313a9a | 19 | #include "wx/dcmemory.h" |
cdccdfab WS |
20 | #endif |
21 | ||
e9576ca5 | 22 | IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton) |
e9576ca5 | 23 | |
d497dca4 | 24 | #include "wx/mac/uma.h" |
7c551d95 | 25 | |
fde5da31 RD |
26 | //--------------------------------------------------------------------------- |
27 | // Helper functions | |
28 | ||
29 | static wxBitmap wxMakeStdSizeBitmap(const wxBitmap& bitmap) | |
30 | { | |
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 | |
35 | ||
36 | // the supported sizes, sorted in decreasng order | |
37 | static const int stdSizes[] = { 128, 48, 32, 16, 0 }; | |
38 | ||
39 | const int width = bitmap.GetWidth(); | |
40 | const int height = bitmap.GetHeight(); | |
41 | ||
42 | wxBitmap newBmp(bitmap); | |
43 | ||
44 | int n; | |
45 | for ( n = 0; n < (int)WXSIZEOF(stdSizes); n++ ) | |
46 | { | |
47 | const int sizeStd = stdSizes[n]; | |
48 | if ( width > sizeStd || height > sizeStd ) | |
49 | { | |
50 | // it will become -1 if the bitmap is larger than the biggest | |
51 | // supported size, this is intentional | |
52 | n--; | |
53 | ||
54 | break; | |
55 | } | |
56 | } | |
57 | ||
58 | if ( n != -1 ) | |
59 | { | |
60 | const int sizeStd = stdSizes[n]; | |
61 | if ( width != sizeStd || height != sizeStd ) | |
62 | { | |
63 | wxASSERT_MSG( width <= sizeStd && height <= sizeStd, | |
64 | _T("bitmap shouldn't be cropped") ); | |
65 | ||
66 | newBmp.Create(sizeStd, sizeStd); | |
67 | wxMemoryDC dcMem; | |
68 | dcMem.SelectObject(newBmp); | |
69 | dcMem.Clear(); | |
70 | ||
71 | dcMem.DrawBitmap(bitmap, | |
72 | (sizeStd - width)/2, (sizeStd-height)/2, | |
73 | true); | |
74 | } | |
75 | } | |
76 | //else: let the system rescale the bitmap | |
77 | ||
78 | return newBmp; | |
79 | } | |
80 | ||
81 | //--------------------------------------------------------------------------- | |
82 | ||
cc374a2f | 83 | bool wxBitmapButton::Create( wxWindow *parent, |
cdccdfab WS |
84 | wxWindowID id, const wxBitmap& bitmap, |
85 | const wxPoint& pos, | |
86 | const wxSize& size, | |
87 | long style, | |
88 | const wxValidator& validator, | |
89 | const wxString& name ) | |
e9576ca5 | 90 | { |
cc374a2f DS |
91 | m_macIsUserPane = false; |
92 | ||
bf19e3f6 SC |
93 | // since bitmapbuttonbase is subclass of button calling wxBitmapButtonBase::Create |
94 | // essentially creates an additional button | |
cc374a2f | 95 | if ( !wxControl::Create( parent, id, pos, size, style, validator, name ) ) |
b45ed7a2 VZ |
96 | return false; |
97 | ||
cc374a2f | 98 | if ( style & wxBU_AUTODRAW ) |
827e7a48 | 99 | { |
cc374a2f | 100 | m_marginX = |
827e7a48 RR |
101 | m_marginY = wxDEFAULT_BUTTON_MARGIN; |
102 | } | |
103 | else | |
104 | { | |
cc374a2f | 105 | m_marginX = |
827e7a48 RR |
106 | m_marginY = 0; |
107 | } | |
e9576ca5 | 108 | |
cc374a2f DS |
109 | OSStatus err = noErr; |
110 | ControlButtonContentInfo info; | |
111 | ||
112 | Rect bounds = wxMacGetBoundsForControl( this, pos, size ); | |
113 | m_peer = new wxMacControl( this ); | |
d32be04c | 114 | |
d24a9fc7 | 115 | if ( bitmap.Ok() && HasFlag(wxBORDER_NONE) ) |
fde5da31 RD |
116 | m_bmpNormal = wxMakeStdSizeBitmap(bitmap); |
117 | else | |
2acac1b4 VZ |
118 | m_bmpNormal = bitmap; |
119 | ||
120 | ||
d32be04c SC |
121 | #ifdef __WXMAC_OSX__ |
122 | if ( HasFlag( wxBORDER_NONE ) ) | |
123 | { | |
6239ee05 | 124 | // contrary to the docs this control only works with iconrefs |
cc374a2f DS |
125 | wxMacCreateBitmapButton( &info, m_bmpNormal, kControlContentIconRef ); |
126 | err = CreateIconControl( | |
2acac1b4 VZ |
127 | MAC_WXHWND(parent->MacGetTopLevelWindowRef()), |
128 | &bounds, &info, false, m_peer->GetControlRefAddr() ); | |
d32be04c SC |
129 | } |
130 | else | |
131 | #endif | |
132 | { | |
cc374a2f DS |
133 | wxMacCreateBitmapButton( &info, m_bmpNormal ); |
134 | err = CreateBevelButtonControl( | |
2acac1b4 VZ |
135 | MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds, CFSTR(""), |
136 | ((style & wxBU_AUTODRAW) ? kControlBevelButtonSmallBevel : kControlBevelButtonNormalBevel ), | |
137 | kControlBehaviorOffsetContents, &info, 0, 0, 0, m_peer->GetControlRefAddr() ); | |
d32be04c | 138 | } |
e9576ca5 | 139 | |
cc374a2f DS |
140 | verify_noerr( err ); |
141 | ||
142 | wxMacReleaseBitmapButton( &info ); | |
143 | wxASSERT_MSG( m_peer != NULL && m_peer->Ok(), wxT("No valid native Mac control") ); | |
144 | ||
145 | MacPostControlCreate( pos, size ); | |
146 | ||
147 | return true; | |
e9576ca5 SC |
148 | } |
149 | ||
cc374a2f | 150 | void wxBitmapButton::SetBitmapLabel( const wxBitmap& bitmap ) |
e9576ca5 | 151 | { |
fde5da31 RD |
152 | if ( HasFlag( wxBORDER_NONE ) ) |
153 | m_bmpNormal = wxMakeStdSizeBitmap(bitmap); | |
154 | else | |
155 | m_bmpNormal = bitmap; | |
156 | ||
9f884528 | 157 | InvalidateBestSize(); |
3dec57ad | 158 | |
cc374a2f DS |
159 | ControlButtonContentInfo info; |
160 | ||
d32be04c SC |
161 | #ifdef __WXMAC_OSX__ |
162 | if ( HasFlag( wxBORDER_NONE ) ) | |
fde5da31 | 163 | { |
cc374a2f | 164 | wxMacCreateBitmapButton( &info, m_bmpNormal, kControlContentIconRef ); |
d32be04c | 165 | if ( info.contentType != kControlNoContent ) |
cc374a2f | 166 | m_peer->SetData( kControlIconPart, kControlIconContentTag, info ); |
d32be04c SC |
167 | } |
168 | else | |
169 | #endif | |
d460ed40 | 170 | { |
cc374a2f | 171 | wxMacCreateBitmapButton( &info, m_bmpNormal ); |
d32be04c | 172 | if ( info.contentType != kControlNoContent ) |
cc374a2f | 173 | m_peer->SetData( kControlButtonPart, kControlBevelButtonContentTag, info ); |
3dec57ad | 174 | } |
e9576ca5 | 175 | |
cc374a2f DS |
176 | wxMacReleaseBitmapButton( &info ); |
177 | } | |
c0831a3c RD |
178 | |
179 | wxSize wxBitmapButton::DoGetBestSize() const | |
180 | { | |
181 | wxSize best; | |
cc374a2f DS |
182 | |
183 | best.x = 2 * m_marginX; | |
184 | best.y = 2 * m_marginY; | |
185 | if ( m_bmpNormal.Ok() ) | |
c0831a3c | 186 | { |
cc374a2f DS |
187 | best.x += m_bmpNormal.GetWidth(); |
188 | best.y += m_bmpNormal.GetHeight(); | |
c0831a3c | 189 | } |
cc374a2f | 190 | |
c0831a3c RD |
191 | return best; |
192 | } | |
179e085f RN |
193 | |
194 | #endif |