]>
Commit | Line | Data |
---|---|---|
489468fe | 1 | ///////////////////////////////////////////////////////////////////////////// |
524c47aa | 2 | // Name: src/osx/carbon/bmpbuttn.cpp |
489468fe SC |
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 | #include "wx/image.h" | |
18 | ||
19 | #ifndef WX_PRECOMP | |
20 | #include "wx/dcmemory.h" | |
21 | #endif | |
22 | ||
524c47aa | 23 | #include "wx/osx/private.h" |
489468fe SC |
24 | |
25 | //--------------------------------------------------------------------------- | |
26 | // Helper functions | |
27 | ||
28 | static wxBitmap wxMakeStdSizeBitmap(const wxBitmap& bitmap) | |
29 | { | |
30 | // in Mac OS X the icon controls (which are used for borderless bitmap | |
31 | // buttons) can have only one of the few standard sizes and if they | |
32 | // don't, the OS rescales them automatically resulting in really ugly | |
33 | // images, so centre the image in a square of standard size instead | |
34 | ||
35 | // the supported sizes, sorted in decreasng order | |
36 | static const int stdSizes[] = { 128, 48, 32, 16, 0 }; | |
37 | ||
38 | const int width = bitmap.GetWidth(); | |
39 | const int height = bitmap.GetHeight(); | |
40 | ||
41 | wxBitmap newBmp(bitmap); | |
42 | ||
43 | int n; | |
44 | for ( n = 0; n < (int)WXSIZEOF(stdSizes); n++ ) | |
45 | { | |
46 | const int sizeStd = stdSizes[n]; | |
47 | if ( width > sizeStd || height > sizeStd ) | |
48 | { | |
49 | // it will become -1 if the bitmap is larger than the biggest | |
50 | // supported size, this is intentional | |
51 | n--; | |
52 | ||
53 | break; | |
54 | } | |
55 | } | |
56 | ||
57 | if ( n != -1 ) | |
58 | { | |
59 | const int sizeStd = stdSizes[n]; | |
60 | if ( width != sizeStd || height != sizeStd ) | |
61 | { | |
62 | wxASSERT_MSG( width <= sizeStd && height <= sizeStd, | |
63 | _T("bitmap shouldn't be cropped") ); | |
64 | ||
65 | wxImage square_image = bitmap.ConvertToImage(); | |
66 | newBmp = square_image.Size | |
67 | ( | |
68 | wxSize(sizeStd, sizeStd), | |
69 | wxPoint((sizeStd - width)/2, (sizeStd-height)/2) | |
70 | ); | |
71 | } | |
72 | } | |
73 | //else: let the system rescale the bitmap | |
74 | ||
75 | return newBmp; | |
76 | } | |
77 | ||
78 | //--------------------------------------------------------------------------- | |
79 | ||
524c47aa | 80 | class wxMacBitmapButton : public wxMacControl |
489468fe | 81 | { |
524c47aa SC |
82 | public: |
83 | wxMacBitmapButton( wxWindowMac* peer ) : wxMacControl(peer) | |
489468fe | 84 | { |
489468fe | 85 | } |
524c47aa SC |
86 | |
87 | void SetBitmap(const wxBitmap& bitmap) | |
489468fe | 88 | { |
524c47aa SC |
89 | wxBitmap bmp; |
90 | if ( GetWXPeer()->HasFlag( wxBORDER_NONE ) ) | |
91 | { | |
92 | bmp = wxMakeStdSizeBitmap(bitmap); | |
93 | // TODO set bitmap in peer as well | |
94 | } | |
95 | else | |
96 | bmp = bitmap; | |
97 | ||
98 | ControlButtonContentInfo info; | |
489468fe | 99 | |
524c47aa SC |
100 | if ( GetWXPeer()->HasFlag( wxBORDER_NONE ) ) |
101 | { | |
102 | wxMacCreateBitmapButton( &info, bmp, kControlContentIconRef ); | |
103 | if ( info.contentType != kControlNoContent ) | |
104 | SetData( kControlIconPart, kControlIconContentTag, info ); | |
105 | } | |
106 | else | |
107 | { | |
108 | wxMacCreateBitmapButton( &info, bmp ); | |
109 | if ( info.contentType != kControlNoContent ) | |
110 | SetData( kControlButtonPart, kControlBevelButtonContentTag, info ); | |
111 | } | |
112 | ||
113 | wxMacReleaseBitmapButton( &info ); | |
114 | } | |
115 | }; | |
116 | ||
117 | wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer, | |
118 | wxWindowMac* parent, | |
119 | wxWindowID id, | |
120 | const wxBitmap& bitmap, | |
121 | const wxPoint& pos, | |
122 | const wxSize& size, | |
123 | long style, | |
124 | long extraStyle) | |
125 | { | |
489468fe SC |
126 | OSStatus err = noErr; |
127 | ControlButtonContentInfo info; | |
128 | ||
524c47aa SC |
129 | Rect bounds = wxMacGetBoundsForControl( wxpeer, pos, size ); |
130 | wxMacControl* peer = new wxMacBitmapButton( wxpeer ); | |
131 | wxBitmap bmp; | |
489468fe | 132 | |
524c47aa SC |
133 | if ( bitmap.Ok() && (style & wxBORDER_NONE) ) |
134 | { | |
135 | bmp = wxMakeStdSizeBitmap(bitmap); | |
136 | // TODO set bitmap in peer as well | |
137 | } | |
489468fe | 138 | else |
524c47aa | 139 | bmp = bitmap; |
489468fe SC |
140 | |
141 | ||
524c47aa | 142 | if ( style & wxBORDER_NONE ) |
489468fe SC |
143 | { |
144 | // contrary to the docs this control only works with iconrefs | |
524c47aa | 145 | wxMacCreateBitmapButton( &info, bmp, kControlContentIconRef ); |
489468fe SC |
146 | err = CreateIconControl( |
147 | MAC_WXHWND(parent->MacGetTopLevelWindowRef()), | |
524c47aa | 148 | &bounds, &info, false, peer->GetControlRefAddr() ); |
489468fe SC |
149 | } |
150 | else | |
151 | { | |
524c47aa | 152 | wxMacCreateBitmapButton( &info, bmp ); |
489468fe SC |
153 | err = CreateBevelButtonControl( |
154 | MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds, CFSTR(""), | |
155 | ((style & wxBU_AUTODRAW) ? kControlBevelButtonSmallBevel : kControlBevelButtonNormalBevel ), | |
524c47aa | 156 | kControlBehaviorOffsetContents, &info, 0, 0, 0, peer->GetControlRefAddr() ); |
489468fe SC |
157 | } |
158 | ||
159 | verify_noerr( err ); | |
160 | ||
161 | wxMacReleaseBitmapButton( &info ); | |
524c47aa | 162 | return peer; |
489468fe | 163 | } |
489468fe | 164 | #endif |