wxMessageBox off the main thread lost result code.
[wxWidgets.git] / src / osx / carbon / bmpbuttn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/bmpbuttn.cpp
3 // Purpose: wxBitmapButton
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
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
22 #include "wx/osx/private.h"
23
24 namespace
25 {
26
27 // define a derived class to override SetBitmap() and also to provide
28 // InitButtonContentInfo() helper used by CreateBitmapButton()
29 class wxMacBitmapButton : public wxMacControl, public wxButtonImpl
30 {
31 public:
32 wxMacBitmapButton(wxWindowMac* peer, const wxBitmap& bitmap, int style)
33 : wxMacControl(peer)
34 {
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);
46 }
47
48 virtual void SetBitmap(const wxBitmap& bitmap)
49 {
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);
56
57 if ( info.contentType == kControlContentIconRef )
58 SetData(kControlIconPart, kControlIconContentTag, info);
59 else if ( info.contentType != kControlNoContent )
60 SetData(kControlButtonPart, kControlBevelButtonContentTag, info);
61
62 wxMacReleaseBitmapButton(&info);
63 }
64
65 void InitButtonContentInfo(ControlButtonContentInfo& info,
66 const wxBitmap& bitmap)
67 {
68 wxMacCreateBitmapButton(&info, bitmap,
69 m_isIcon ? kControlContentIconRef : 0);
70 }
71
72 void SetPressedBitmap( const wxBitmap& WXUNUSED(bitmap) )
73 {
74 // not implemented under Carbon
75 }
76
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();
83
84 return bmp.GetHeight() == w &&
85 (w == 128 || w == 48 || w == 32 || w == 16);
86 }
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);
93 };
94
95 } // anonymous namespace
96
97 wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
98 wxWindowMac* parent,
99 wxWindowID WXUNUSED(id),
100 const wxBitmap& bitmap,
101 const wxPoint& pos,
102 const wxSize& size,
103 long style,
104 long WXUNUSED(extraStyle))
105 {
106 wxMacBitmapButton* peer = new wxMacBitmapButton(wxpeer, bitmap, style);
107
108 OSStatus err;
109 WXWindow macParent = MAC_WXHWND(parent->MacGetTopLevelWindowRef());
110 Rect bounds = wxMacGetBoundsForControl( wxpeer, pos, size );
111
112 ControlButtonContentInfo info;
113 peer->InitButtonContentInfo(info, bitmap);
114
115 if ( info.contentType == kControlContentIconRef )
116 {
117 err = CreateIconControl
118 (
119 macParent,
120 &bounds,
121 &info,
122 false,
123 peer->GetControlRefAddr()
124 );
125 }
126 else // normal bevel button
127 {
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 );
142 }
143
144 verify_noerr( err );
145
146 wxMacReleaseBitmapButton( &info );
147 return peer;
148 }
149
150 #endif // wxUSE_BMPBUTTON