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