]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/bmpbuttn.cpp
Fix crash in wxDC::GetMultiLineTextExtent() after last commit.
[wxWidgets.git] / src / osx / carbon / bmpbuttn.cpp
CommitLineData
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 24
6398a32d 25namespace
489468fe 26{
489468fe 27
6398a32d
VZ
28// define a derived class to override SetBitmap() and also to provide
29// InitButtonContentInfo() helper used by CreateBitmapButton()
b38dc31f 30class wxMacBitmapButton : public wxMacControl, public wxButtonImpl
6398a32d
VZ
31{
32public:
33 wxMacBitmapButton(wxWindowMac* peer, const wxBitmap& bitmap, int style)
34 : wxMacControl(peer)
489468fe 35 {
6398a32d
VZ
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);
489468fe
SC
47 }
48
6398a32d 49 virtual void SetBitmap(const wxBitmap& bitmap)
489468fe 50 {
6398a32d
VZ
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);
489468fe 57
6398a32d
VZ
58 if ( info.contentType == kControlContentIconRef )
59 SetData(kControlIconPart, kControlIconContentTag, info);
60 else if ( info.contentType != kControlNoContent )
61 SetData(kControlButtonPart, kControlBevelButtonContentTag, info);
489468fe 62
6398a32d
VZ
63 wxMacReleaseBitmapButton(&info);
64 }
489468fe 65
6398a32d
VZ
66 void InitButtonContentInfo(ControlButtonContentInfo& info,
67 const wxBitmap& bitmap)
489468fe 68 {
6398a32d
VZ
69 wxMacCreateBitmapButton(&info, bitmap,
70 m_isIcon ? kControlContentIconRef : 0);
489468fe 71 }
524c47aa 72
411a1c35
SC
73 void SetPressedBitmap( const wxBitmap& WXUNUSED(bitmap) )
74 {
75 // not implemented under Carbon
ce00f59b
VZ
76 }
77
6398a32d
VZ
78private:
79 // helper function: returns true if the given bitmap is of one of standard
80 // sizes supported by OS X icons
81 static bool IsOfStandardSize(const wxBitmap& bmp)
82 {
83 const int w = bmp.GetWidth();
489468fe 84
6398a32d
VZ
85 return bmp.GetHeight() == w &&
86 (w == 128 || w == 48 || w == 32 || w == 16);
524c47aa 87 }
6398a32d
VZ
88
89
90 // true if this is an icon control, false if it's a bevel button
91 bool m_isIcon;
92
93 wxDECLARE_NO_COPY_CLASS(wxMacBitmapButton);
524c47aa
SC
94};
95
6398a32d
VZ
96} // anonymous namespace
97
98wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer,
99 wxWindowMac* parent,
100 wxWindowID WXUNUSED(id),
524c47aa 101 const wxBitmap& bitmap,
6398a32d 102 const wxPoint& pos,
524c47aa 103 const wxSize& size,
6398a32d 104 long style,
a4fec5b4 105 long WXUNUSED(extraStyle))
524c47aa 106{
6398a32d 107 wxMacBitmapButton* peer = new wxMacBitmapButton(wxpeer, bitmap, style);
489468fe 108
6398a32d
VZ
109 OSStatus err;
110 WXWindow macParent = MAC_WXHWND(parent->MacGetTopLevelWindowRef());
524c47aa 111 Rect bounds = wxMacGetBoundsForControl( wxpeer, pos, size );
489468fe 112
6398a32d
VZ
113 ControlButtonContentInfo info;
114 peer->InitButtonContentInfo(info, bitmap);
489468fe 115
6398a32d 116 if ( info.contentType == kControlContentIconRef )
489468fe 117 {
6398a32d
VZ
118 err = CreateIconControl
119 (
120 macParent,
121 &bounds,
122 &info,
123 false,
124 peer->GetControlRefAddr()
125 );
489468fe 126 }
6398a32d 127 else // normal bevel button
489468fe 128 {
6398a32d
VZ
129 err = CreateBevelButtonControl
130 (
131 macParent,
132 &bounds,
133 CFSTR(""),
134 style & wxBU_AUTODRAW ? kControlBevelButtonSmallBevel
135 : kControlBevelButtonNormalBevel,
136 kControlBehaviorOffsetContents,
137 &info,
138 0, // menu id (no associated menu)
139 0, // menu behaviour (unused)
140 0, // menu placement (unused too)
141 peer->GetControlRefAddr()
142 );
489468fe
SC
143 }
144
145 verify_noerr( err );
146
147 wxMacReleaseBitmapButton( &info );
524c47aa 148 return peer;
489468fe 149}
6398a32d
VZ
150
151#endif // wxUSE_BMPBUTTON