]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/mac/carbon/bmpbuttn.cpp | |
3 | // Purpose: wxBitmapButton | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 1998-01-01 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Stefan Csomor | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #if wxUSE_BMPBUTTON | |
15 | ||
16 | #include "wx/bmpbuttn.h" | |
17 | ||
18 | #ifndef WX_PRECOMP | |
19 | #include "wx/window.h" | |
20 | #include "wx/bitmap.h" | |
21 | #endif | |
22 | ||
23 | IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton) | |
24 | ||
25 | #include "wx/mac/uma.h" | |
26 | ||
27 | bool wxBitmapButton::Create( wxWindow *parent, | |
28 | wxWindowID id, const wxBitmap& bitmap, | |
29 | const wxPoint& pos, | |
30 | const wxSize& size, | |
31 | long style, | |
32 | const wxValidator& validator, | |
33 | const wxString& name ) | |
34 | { | |
35 | m_macIsUserPane = false; | |
36 | ||
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 ) ) | |
40 | return false; | |
41 | ||
42 | if ( style & wxBU_AUTODRAW ) | |
43 | { | |
44 | m_marginX = | |
45 | m_marginY = wxDEFAULT_BUTTON_MARGIN; | |
46 | } | |
47 | else | |
48 | { | |
49 | m_marginX = | |
50 | m_marginY = 0; | |
51 | } | |
52 | ||
53 | OSStatus err = noErr; | |
54 | ControlButtonContentInfo info; | |
55 | ||
56 | Rect bounds = wxMacGetBoundsForControl( this, pos, size ); | |
57 | m_peer = new wxMacControl( this ); | |
58 | ||
59 | if ( bitmap.Ok() && !size.IsFullySpecified() ) | |
60 | { | |
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 | |
65 | ||
66 | // the supported sizes, sorted in decreasng order | |
67 | static const int stdSizes[] = { 128, 48, 32, 16, 0 }; | |
68 | ||
69 | const int width = bitmap.GetWidth(); | |
70 | const int height = bitmap.GetHeight(); | |
71 | ||
72 | int n; | |
73 | for ( n = 0; n < (int)WXSIZEOF(stdSizes); n++ ) | |
74 | { | |
75 | const int sizeStd = stdSizes[n]; | |
76 | if ( width > sizeStd || height > sizeStd ) | |
77 | { | |
78 | // it will become -1 if the bitmap is larger than the biggest | |
79 | // supported size, this is intentional | |
80 | n--; | |
81 | ||
82 | break; | |
83 | } | |
84 | } | |
85 | ||
86 | if ( n != -1 ) | |
87 | { | |
88 | const int sizeStd = stdSizes[n]; | |
89 | if ( width != sizeStd || height != sizeStd ) | |
90 | { | |
91 | wxASSERT_MSG( width <= sizeStd && height <= sizeStd, | |
92 | _T("bitmap shouldn't be cropped") ); | |
93 | ||
94 | m_bmpNormal.Create(sizeStd, sizeStd); | |
95 | wxMemoryDC dcMem; | |
96 | dcMem.SelectObject(m_bmpNormal); | |
97 | dcMem.Clear(); | |
98 | ||
99 | dcMem.DrawBitmap(bitmap, | |
100 | (sizeStd - width)/2, (sizeStd-height)/2, | |
101 | true); | |
102 | } | |
103 | } | |
104 | //else: let the system rescale the bitmap | |
105 | } | |
106 | ||
107 | if ( !m_bmpNormal.Ok() ) | |
108 | m_bmpNormal = bitmap; | |
109 | ||
110 | ||
111 | #ifdef __WXMAC_OSX__ | |
112 | if ( HasFlag( wxBORDER_NONE ) ) | |
113 | { | |
114 | wxMacCreateBitmapButton( &info, m_bmpNormal, kControlContentIconRef ); | |
115 | err = CreateIconControl( | |
116 | MAC_WXHWND(parent->MacGetTopLevelWindowRef()), | |
117 | &bounds, &info, false, m_peer->GetControlRefAddr() ); | |
118 | } | |
119 | else | |
120 | #endif | |
121 | { | |
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() ); | |
127 | } | |
128 | ||
129 | verify_noerr( err ); | |
130 | ||
131 | wxMacReleaseBitmapButton( &info ); | |
132 | wxASSERT_MSG( m_peer != NULL && m_peer->Ok(), wxT("No valid native Mac control") ); | |
133 | ||
134 | MacPostControlCreate( pos, size ); | |
135 | ||
136 | return true; | |
137 | } | |
138 | ||
139 | void wxBitmapButton::SetBitmapLabel( const wxBitmap& bitmap ) | |
140 | { | |
141 | m_bmpNormal = bitmap; | |
142 | InvalidateBestSize(); | |
143 | ||
144 | ControlButtonContentInfo info; | |
145 | ||
146 | #ifdef __WXMAC_OSX__ | |
147 | if ( HasFlag( wxBORDER_NONE ) ) | |
148 | { | |
149 | wxMacCreateBitmapButton( &info, m_bmpNormal, kControlContentIconRef ); | |
150 | if ( info.contentType != kControlNoContent ) | |
151 | m_peer->SetData( kControlIconPart, kControlIconContentTag, info ); | |
152 | } | |
153 | else | |
154 | #endif | |
155 | { | |
156 | wxMacCreateBitmapButton( &info, m_bmpNormal ); | |
157 | if ( info.contentType != kControlNoContent ) | |
158 | m_peer->SetData( kControlButtonPart, kControlBevelButtonContentTag, info ); | |
159 | } | |
160 | ||
161 | wxMacReleaseBitmapButton( &info ); | |
162 | } | |
163 | ||
164 | wxSize wxBitmapButton::DoGetBestSize() const | |
165 | { | |
166 | wxSize best; | |
167 | ||
168 | best.x = 2 * m_marginX; | |
169 | best.y = 2 * m_marginY; | |
170 | if ( m_bmpNormal.Ok() ) | |
171 | { | |
172 | best.x += m_bmpNormal.GetWidth(); | |
173 | best.y += m_bmpNormal.GetHeight(); | |
174 | } | |
175 | ||
176 | return best; | |
177 | } | |
178 | ||
179 | #endif |