]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/bmpbuttn.cpp
another 64 bit fix: make SendMsg() return wxIntPtr instead of long too
[wxWidgets.git] / src / osx / carbon / bmpbuttn.cpp
CommitLineData
489468fe
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/mac/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
23IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton)
24
1f0c8f31 25#include "wx/osx/uma.h"
489468fe
SC
26
27//---------------------------------------------------------------------------
28// Helper functions
29
30static wxBitmap wxMakeStdSizeBitmap(const wxBitmap& bitmap)
31{
32 // in Mac OS X the icon controls (which are used for borderless bitmap
33 // buttons) can have only one of the few standard sizes and if they
34 // don't, the OS rescales them automatically resulting in really ugly
35 // images, so centre the image in a square of standard size instead
36
37 // the supported sizes, sorted in decreasng order
38 static const int stdSizes[] = { 128, 48, 32, 16, 0 };
39
40 const int width = bitmap.GetWidth();
41 const int height = bitmap.GetHeight();
42
43 wxBitmap newBmp(bitmap);
44
45 int n;
46 for ( n = 0; n < (int)WXSIZEOF(stdSizes); n++ )
47 {
48 const int sizeStd = stdSizes[n];
49 if ( width > sizeStd || height > sizeStd )
50 {
51 // it will become -1 if the bitmap is larger than the biggest
52 // supported size, this is intentional
53 n--;
54
55 break;
56 }
57 }
58
59 if ( n != -1 )
60 {
61 const int sizeStd = stdSizes[n];
62 if ( width != sizeStd || height != sizeStd )
63 {
64 wxASSERT_MSG( width <= sizeStd && height <= sizeStd,
65 _T("bitmap shouldn't be cropped") );
66
67 wxImage square_image = bitmap.ConvertToImage();
68 newBmp = square_image.Size
69 (
70 wxSize(sizeStd, sizeStd),
71 wxPoint((sizeStd - width)/2, (sizeStd-height)/2)
72 );
73 }
74 }
75 //else: let the system rescale the bitmap
76
77 return newBmp;
78}
79
80//---------------------------------------------------------------------------
81
82bool wxBitmapButton::Create( wxWindow *parent,
83 wxWindowID id, const wxBitmap& bitmap,
84 const wxPoint& pos,
85 const wxSize& size,
86 long style,
87 const wxValidator& validator,
88 const wxString& name )
89{
90 m_macIsUserPane = false;
91
92 // since bitmapbuttonbase is subclass of button calling wxBitmapButtonBase::Create
93 // essentially creates an additional button
94 if ( !wxControl::Create( parent, id, pos, size, style, validator, name ) )
95 return false;
96
97 if ( style & wxBU_AUTODRAW )
98 {
99 m_marginX =
100 m_marginY = wxDEFAULT_BUTTON_MARGIN;
101 }
102 else
103 {
104 m_marginX =
105 m_marginY = 0;
106 }
107
108 OSStatus err = noErr;
109 ControlButtonContentInfo info;
110
111 Rect bounds = wxMacGetBoundsForControl( this, pos, size );
112 m_peer = new wxMacControl( this );
113
114 if ( bitmap.Ok() && HasFlag(wxBORDER_NONE) )
115 m_bmpNormal = wxMakeStdSizeBitmap(bitmap);
116 else
117 m_bmpNormal = bitmap;
118
119
120 if ( HasFlag( wxBORDER_NONE ) )
121 {
122 // contrary to the docs this control only works with iconrefs
123 wxMacCreateBitmapButton( &info, m_bmpNormal, kControlContentIconRef );
124 err = CreateIconControl(
125 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
126 &bounds, &info, false, m_peer->GetControlRefAddr() );
127 }
128 else
129 {
130 wxMacCreateBitmapButton( &info, m_bmpNormal );
131 err = CreateBevelButtonControl(
132 MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds, CFSTR(""),
133 ((style & wxBU_AUTODRAW) ? kControlBevelButtonSmallBevel : kControlBevelButtonNormalBevel ),
134 kControlBehaviorOffsetContents, &info, 0, 0, 0, m_peer->GetControlRefAddr() );
135 }
136
137 verify_noerr( err );
138
139 wxMacReleaseBitmapButton( &info );
140 wxASSERT_MSG( m_peer != NULL && m_peer->Ok(), wxT("No valid native Mac control") );
141
142 MacPostControlCreate( pos, size );
143
144 return true;
145}
146
147void wxBitmapButton::SetBitmapLabel( const wxBitmap& bitmap )
148{
149 if ( HasFlag( wxBORDER_NONE ) )
150 m_bmpNormal = wxMakeStdSizeBitmap(bitmap);
151 else
152 m_bmpNormal = bitmap;
153
154 InvalidateBestSize();
155
156 ControlButtonContentInfo info;
157
158 if ( HasFlag( wxBORDER_NONE ) )
159 {
160 wxMacCreateBitmapButton( &info, m_bmpNormal, kControlContentIconRef );
161 if ( info.contentType != kControlNoContent )
162 m_peer->SetData( kControlIconPart, kControlIconContentTag, info );
163 }
164 else
165 {
166 wxMacCreateBitmapButton( &info, m_bmpNormal );
167 if ( info.contentType != kControlNoContent )
168 m_peer->SetData( kControlButtonPart, kControlBevelButtonContentTag, info );
169 }
170
171 wxMacReleaseBitmapButton( &info );
172}
173
174wxSize wxBitmapButton::DoGetBestSize() const
175{
176 wxSize best;
177
178 best.x = 2 * m_marginX;
179 best.y = 2 * m_marginY;
180 if ( m_bmpNormal.Ok() )
181 {
182 best.x += m_bmpNormal.GetWidth();
183 best.y += m_bmpNormal.GetHeight();
184 }
185
186 return best;
187}
188
189#endif