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