]>
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 | |
489468fe SC |
7 | // Copyright: (c) Stefan Csomor |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #if wxUSE_BMPBUTTON | |
14 | ||
15 | #include "wx/bmpbuttn.h" | |
16 | #include "wx/image.h" | |
17 | ||
18 | #ifndef WX_PRECOMP | |
19 | #include "wx/dcmemory.h" | |
20 | #endif | |
21 | ||
524c47aa | 22 | #include "wx/osx/private.h" |
489468fe | 23 | |
6398a32d | 24 | namespace |
489468fe | 25 | { |
489468fe | 26 | |
6398a32d VZ |
27 | // define a derived class to override SetBitmap() and also to provide |
28 | // InitButtonContentInfo() helper used by CreateBitmapButton() | |
b38dc31f | 29 | class wxMacBitmapButton : public wxMacControl, public wxButtonImpl |
6398a32d VZ |
30 | { |
31 | public: | |
32 | wxMacBitmapButton(wxWindowMac* peer, const wxBitmap& bitmap, int style) | |
33 | : wxMacControl(peer) | |
489468fe | 34 | { |
6398a32d VZ |
35 | // decide what kind of contents the button will have: we want to use an |
36 | // icon for buttons with wxBORDER_NONE style as bevel buttons always do | |
37 | // have a border but icons are limited to a few standard sizes only and | |
38 | // are resized by the system with extremely ugly results if they don't | |
39 | // fit (in the past we also tried being smart and pasting a bitmap | |
40 | // instead of a larger square icon to avoid resizing but this resulted | |
41 | // in buttons having different size than specified by wx API and | |
42 | // breaking the layouts and still didn't look good so we don't even try | |
43 | // to do this any more) | |
44 | m_isIcon = (style & wxBORDER_NONE) && | |
45 | bitmap.IsOk() && IsOfStandardSize(bitmap); | |
489468fe SC |
46 | } |
47 | ||
6398a32d | 48 | virtual void SetBitmap(const wxBitmap& bitmap) |
489468fe | 49 | { |
6398a32d VZ |
50 | // unfortunately we can't avoid the ugly resizing problem mentioned |
51 | // above if a bitmap of supported size was used initially but was | |
52 | // replaced with another one later as the control was already created | |
53 | // as an icon control (although maybe we ought to recreate it?) | |
54 | ControlButtonContentInfo info; | |
55 | InitButtonContentInfo(info, bitmap); | |
489468fe | 56 | |
6398a32d VZ |
57 | if ( info.contentType == kControlContentIconRef ) |
58 | SetData(kControlIconPart, kControlIconContentTag, info); | |
59 | else if ( info.contentType != kControlNoContent ) | |
60 | SetData(kControlButtonPart, kControlBevelButtonContentTag, info); | |
489468fe | 61 | |
6398a32d VZ |
62 | wxMacReleaseBitmapButton(&info); |
63 | } | |
489468fe | 64 | |
6398a32d VZ |
65 | void InitButtonContentInfo(ControlButtonContentInfo& info, |
66 | const wxBitmap& bitmap) | |
489468fe | 67 | { |
6398a32d VZ |
68 | wxMacCreateBitmapButton(&info, bitmap, |
69 | m_isIcon ? kControlContentIconRef : 0); | |
489468fe | 70 | } |
524c47aa | 71 | |
411a1c35 SC |
72 | void SetPressedBitmap( const wxBitmap& WXUNUSED(bitmap) ) |
73 | { | |
74 | // not implemented under Carbon | |
ce00f59b VZ |
75 | } |
76 | ||
6398a32d VZ |
77 | private: |
78 | // helper function: returns true if the given bitmap is of one of standard | |
79 | // sizes supported by OS X icons | |
80 | static bool IsOfStandardSize(const wxBitmap& bmp) | |
81 | { | |
82 | const int w = bmp.GetWidth(); | |
489468fe | 83 | |
6398a32d VZ |
84 | return bmp.GetHeight() == w && |
85 | (w == 128 || w == 48 || w == 32 || w == 16); | |
524c47aa | 86 | } |
6398a32d VZ |
87 | |
88 | ||
89 | // true if this is an icon control, false if it's a bevel button | |
90 | bool m_isIcon; | |
91 | ||
92 | wxDECLARE_NO_COPY_CLASS(wxMacBitmapButton); | |
524c47aa SC |
93 | }; |
94 | ||
6398a32d VZ |
95 | } // anonymous namespace |
96 | ||
97 | wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer, | |
98 | wxWindowMac* parent, | |
99 | wxWindowID WXUNUSED(id), | |
524c47aa | 100 | const wxBitmap& bitmap, |
6398a32d | 101 | const wxPoint& pos, |
524c47aa | 102 | const wxSize& size, |
6398a32d | 103 | long style, |
a4fec5b4 | 104 | long WXUNUSED(extraStyle)) |
524c47aa | 105 | { |
6398a32d | 106 | wxMacBitmapButton* peer = new wxMacBitmapButton(wxpeer, bitmap, style); |
489468fe | 107 | |
6398a32d VZ |
108 | OSStatus err; |
109 | WXWindow macParent = MAC_WXHWND(parent->MacGetTopLevelWindowRef()); | |
524c47aa | 110 | Rect bounds = wxMacGetBoundsForControl( wxpeer, pos, size ); |
489468fe | 111 | |
6398a32d VZ |
112 | ControlButtonContentInfo info; |
113 | peer->InitButtonContentInfo(info, bitmap); | |
489468fe | 114 | |
6398a32d | 115 | if ( info.contentType == kControlContentIconRef ) |
489468fe | 116 | { |
6398a32d VZ |
117 | err = CreateIconControl |
118 | ( | |
119 | macParent, | |
120 | &bounds, | |
121 | &info, | |
122 | false, | |
123 | peer->GetControlRefAddr() | |
124 | ); | |
489468fe | 125 | } |
6398a32d | 126 | else // normal bevel button |
489468fe | 127 | { |
6398a32d VZ |
128 | err = CreateBevelButtonControl |
129 | ( | |
130 | macParent, | |
131 | &bounds, | |
132 | CFSTR(""), | |
133 | style & wxBU_AUTODRAW ? kControlBevelButtonSmallBevel | |
134 | : kControlBevelButtonNormalBevel, | |
135 | kControlBehaviorOffsetContents, | |
136 | &info, | |
137 | 0, // menu id (no associated menu) | |
138 | 0, // menu behaviour (unused) | |
139 | 0, // menu placement (unused too) | |
140 | peer->GetControlRefAddr() | |
141 | ); | |
489468fe SC |
142 | } |
143 | ||
144 | verify_noerr( err ); | |
145 | ||
146 | wxMacReleaseBitmapButton( &info ); | |
524c47aa | 147 | return peer; |
489468fe | 148 | } |
6398a32d VZ |
149 | |
150 | #endif // wxUSE_BMPBUTTON |