]>
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/dcmemory.h" | |
20 | #endif | |
21 | ||
22 | IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton) | |
23 | ||
24 | #include "wx/mac/uma.h" | |
25 | ||
26 | bool wxBitmapButton::Create( wxWindow *parent, | |
27 | wxWindowID id, const wxBitmap& bitmap, | |
28 | const wxPoint& pos, | |
29 | const wxSize& size, | |
30 | long style, | |
31 | const wxValidator& validator, | |
32 | const wxString& name ) | |
33 | { | |
34 | m_macIsUserPane = false; | |
35 | ||
36 | // since bitmapbuttonbase is subclass of button calling wxBitmapButtonBase::Create | |
37 | // essentially creates an additional button | |
38 | if ( !wxControl::Create( parent, id, pos, size, style, validator, name ) ) | |
39 | return false; | |
40 | ||
41 | if ( style & wxBU_AUTODRAW ) | |
42 | { | |
43 | m_marginX = | |
44 | m_marginY = wxDEFAULT_BUTTON_MARGIN; | |
45 | } | |
46 | else | |
47 | { | |
48 | m_marginX = | |
49 | m_marginY = 0; | |
50 | } | |
51 | ||
52 | OSStatus err = noErr; | |
53 | ControlButtonContentInfo info; | |
54 | ||
55 | Rect bounds = wxMacGetBoundsForControl( this, pos, size ); | |
56 | m_peer = new wxMacControl( this ); | |
57 | ||
58 | if ( bitmap.Ok() && HasFlag(wxBORDER_NONE) ) | |
59 | { | |
60 | // in Mac OS X the icon controls (which are used for borderless bitmap | |
61 | // buttons) can have only one of the few standard sizes and if they | |
62 | // don't, the OS rescales them automatically resulting in really ugly | |
63 | // images, so centre the image in a square of standard size instead | |
64 | ||
65 | // the supported sizes, sorted in decreasng order | |
66 | static const int stdSizes[] = { 128, 48, 32, 16, 0 }; | |
67 | ||
68 | const int width = bitmap.GetWidth(); | |
69 | const int height = bitmap.GetHeight(); | |
70 | ||
71 | int n; | |
72 | for ( n = 0; n < (int)WXSIZEOF(stdSizes); n++ ) | |
73 | { | |
74 | const int sizeStd = stdSizes[n]; | |
75 | if ( width > sizeStd || height > sizeStd ) | |
76 | { | |
77 | // it will become -1 if the bitmap is larger than the biggest | |
78 | // supported size, this is intentional | |
79 | n--; | |
80 | ||
81 | break; | |
82 | } | |
83 | } | |
84 | ||
85 | if ( n != -1 ) | |
86 | { | |
87 | const int sizeStd = stdSizes[n]; | |
88 | if ( width != sizeStd || height != sizeStd ) | |
89 | { | |
90 | wxASSERT_MSG( width <= sizeStd && height <= sizeStd, | |
91 | _T("bitmap shouldn't be cropped") ); | |
92 | ||
93 | m_bmpNormal.Create(sizeStd, sizeStd); | |
94 | wxMemoryDC dcMem; | |
95 | dcMem.SelectObject(m_bmpNormal); | |
96 | dcMem.Clear(); | |
97 | ||
98 | dcMem.DrawBitmap(bitmap, | |
99 | (sizeStd - width)/2, (sizeStd-height)/2, | |
100 | true); | |
101 | } | |
102 | } | |
103 | //else: let the system rescale the bitmap | |
104 | } | |
105 | ||
106 | if ( !m_bmpNormal.Ok() ) | |
107 | m_bmpNormal = bitmap; | |
108 | ||
109 | ||
110 | #ifdef __WXMAC_OSX__ | |
111 | if ( HasFlag( wxBORDER_NONE ) ) | |
112 | { | |
113 | wxMacCreateBitmapButton( &info, m_bmpNormal, kControlContentIconRef ); | |
114 | err = CreateIconControl( | |
115 | MAC_WXHWND(parent->MacGetTopLevelWindowRef()), | |
116 | &bounds, &info, false, m_peer->GetControlRefAddr() ); | |
117 | } | |
118 | else | |
119 | #endif | |
120 | { | |
121 | wxMacCreateBitmapButton( &info, m_bmpNormal ); | |
122 | err = CreateBevelButtonControl( | |
123 | MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds, CFSTR(""), | |
124 | ((style & wxBU_AUTODRAW) ? kControlBevelButtonSmallBevel : kControlBevelButtonNormalBevel ), | |
125 | kControlBehaviorOffsetContents, &info, 0, 0, 0, m_peer->GetControlRefAddr() ); | |
126 | } | |
127 | ||
128 | verify_noerr( err ); | |
129 | ||
130 | wxMacReleaseBitmapButton( &info ); | |
131 | wxASSERT_MSG( m_peer != NULL && m_peer->Ok(), wxT("No valid native Mac control") ); | |
132 | ||
133 | MacPostControlCreate( pos, size ); | |
134 | ||
135 | return true; | |
136 | } | |
137 | ||
138 | void wxBitmapButton::SetBitmapLabel( const wxBitmap& bitmap ) | |
139 | { | |
140 | m_bmpNormal = bitmap; | |
141 | InvalidateBestSize(); | |
142 | ||
143 | ControlButtonContentInfo info; | |
144 | ||
145 | #ifdef __WXMAC_OSX__ | |
146 | if ( HasFlag( wxBORDER_NONE ) ) | |
147 | { | |
148 | wxMacCreateBitmapButton( &info, m_bmpNormal, kControlContentIconRef ); | |
149 | if ( info.contentType != kControlNoContent ) | |
150 | m_peer->SetData( kControlIconPart, kControlIconContentTag, info ); | |
151 | } | |
152 | else | |
153 | #endif | |
154 | { | |
155 | wxMacCreateBitmapButton( &info, m_bmpNormal ); | |
156 | if ( info.contentType != kControlNoContent ) | |
157 | m_peer->SetData( kControlButtonPart, kControlBevelButtonContentTag, info ); | |
158 | } | |
159 | ||
160 | wxMacReleaseBitmapButton( &info ); | |
161 | } | |
162 | ||
163 | wxSize wxBitmapButton::DoGetBestSize() const | |
164 | { | |
165 | wxSize best; | |
166 | ||
167 | best.x = 2 * m_marginX; | |
168 | best.y = 2 * m_marginY; | |
169 | if ( m_bmpNormal.Ok() ) | |
170 | { | |
171 | best.x += m_bmpNormal.GetWidth(); | |
172 | best.y += m_bmpNormal.GetHeight(); | |
173 | } | |
174 | ||
175 | return best; | |
176 | } | |
177 | ||
178 | #endif |