]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/bmpbuttn.cpp
added wxBookCtrl::ChangeSelection() which is the same as SetSelection() but doesn...
[wxWidgets.git] / src / mac / carbon / bmpbuttn.cpp
CommitLineData
e9576ca5 1/////////////////////////////////////////////////////////////////////////////
cc374a2f 2// Name: src/mac/carbon/bmpbuttn.cpp
e9576ca5 3// Purpose: wxBitmapButton
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
0bca0373 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
a8e9860d
SC
12#include "wx/wxprec.h"
13
179e085f
RN
14#if wxUSE_BMPBUTTON
15
e9576ca5
SC
16#include "wx/bmpbuttn.h"
17
cdccdfab
WS
18#ifndef WX_PRECOMP
19 #include "wx/window.h"
0bca0373 20 #include "wx/bitmap.h"
cdccdfab
WS
21#endif
22
e9576ca5 23IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton)
e9576ca5 24
d497dca4 25#include "wx/mac/uma.h"
7c551d95 26
cc374a2f 27bool wxBitmapButton::Create( wxWindow *parent,
cdccdfab
WS
28 wxWindowID id, const wxBitmap& bitmap,
29 const wxPoint& pos,
30 const wxSize& size,
31 long style,
32 const wxValidator& validator,
33 const wxString& name )
e9576ca5 34{
cc374a2f
DS
35 m_macIsUserPane = false;
36
bf19e3f6
SC
37 // since bitmapbuttonbase is subclass of button calling wxBitmapButtonBase::Create
38 // essentially creates an additional button
cc374a2f 39 if ( !wxControl::Create( parent, id, pos, size, style, validator, name ) )
b45ed7a2
VZ
40 return false;
41
cc374a2f 42 if ( style & wxBU_AUTODRAW )
827e7a48 43 {
cc374a2f 44 m_marginX =
827e7a48
RR
45 m_marginY = wxDEFAULT_BUTTON_MARGIN;
46 }
47 else
48 {
cc374a2f 49 m_marginX =
827e7a48
RR
50 m_marginY = 0;
51 }
e9576ca5 52
cc374a2f
DS
53 OSStatus err = noErr;
54 ControlButtonContentInfo info;
55
56 Rect bounds = wxMacGetBoundsForControl( this, pos, size );
57 m_peer = new wxMacControl( this );
d32be04c 58
2acac1b4
VZ
59 if ( bitmap.Ok() && !size.IsFullySpecified() )
60 {
61 // in Mac OS X the bitmap buttons can have only one of the few standard
62 // sizes and if they don't, the OS rescales them automatically
63 // resulting in really ugly images, so centre the image in a square of
64 // standard size instead
65
66 // the supported sizes, sorted in decreasng order
67 static const int stdSizes[] = { 128, 48, 32, 16, 0 };
68
69 const int width = bitmap.GetWidth();
70 const int height = bitmap.GetHeight();
71
72 int n;
73 for ( n = 0; n < (int)WXSIZEOF(stdSizes); n++ )
74 {
75 const int sizeStd = stdSizes[n];
76 if ( width > sizeStd || height > sizeStd )
77 {
78 // it will become -1 if the bitmap is larger than the biggest
79 // supported size, this is intentional
80 n--;
81
82 break;
83 }
84 }
85
86 if ( n != -1 )
87 {
88 const int sizeStd = stdSizes[n];
89 if ( width != sizeStd || height != sizeStd )
90 {
91 wxASSERT_MSG( width <= sizeStd && height <= sizeStd,
92 _T("bitmap shouldn't be cropped") );
93
94 m_bmpNormal.Create(sizeStd, sizeStd);
95 wxMemoryDC dcMem;
96 dcMem.SelectObject(m_bmpNormal);
97 dcMem.Clear();
98
99 dcMem.DrawBitmap(bitmap,
100 (sizeStd - width)/2, (sizeStd-height)/2,
101 true);
102 }
103 }
104 //else: let the system rescale the bitmap
105 }
106
107 if ( !m_bmpNormal.Ok() )
108 m_bmpNormal = bitmap;
109
110
d32be04c
SC
111#ifdef __WXMAC_OSX__
112 if ( HasFlag( wxBORDER_NONE ) )
113 {
cc374a2f
DS
114 wxMacCreateBitmapButton( &info, m_bmpNormal, kControlContentIconRef );
115 err = CreateIconControl(
2acac1b4
VZ
116 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
117 &bounds, &info, false, m_peer->GetControlRefAddr() );
d32be04c
SC
118 }
119 else
120#endif
121 {
cc374a2f
DS
122 wxMacCreateBitmapButton( &info, m_bmpNormal );
123 err = CreateBevelButtonControl(
2acac1b4
VZ
124 MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds, CFSTR(""),
125 ((style & wxBU_AUTODRAW) ? kControlBevelButtonSmallBevel : kControlBevelButtonNormalBevel ),
126 kControlBehaviorOffsetContents, &info, 0, 0, 0, m_peer->GetControlRefAddr() );
d32be04c 127 }
e9576ca5 128
cc374a2f
DS
129 verify_noerr( err );
130
131 wxMacReleaseBitmapButton( &info );
132 wxASSERT_MSG( m_peer != NULL && m_peer->Ok(), wxT("No valid native Mac control") );
133
134 MacPostControlCreate( pos, size );
135
136 return true;
e9576ca5
SC
137}
138
cc374a2f 139void wxBitmapButton::SetBitmapLabel( const wxBitmap& bitmap )
e9576ca5 140{
d460ed40 141 m_bmpNormal = bitmap;
9f884528 142 InvalidateBestSize();
3dec57ad 143
cc374a2f
DS
144 ControlButtonContentInfo info;
145
d32be04c
SC
146#ifdef __WXMAC_OSX__
147 if ( HasFlag( wxBORDER_NONE ) )
148 {
cc374a2f 149 wxMacCreateBitmapButton( &info, m_bmpNormal, kControlContentIconRef );
d32be04c 150 if ( info.contentType != kControlNoContent )
cc374a2f 151 m_peer->SetData( kControlIconPart, kControlIconContentTag, info );
d32be04c
SC
152 }
153 else
154#endif
d460ed40 155 {
cc374a2f 156 wxMacCreateBitmapButton( &info, m_bmpNormal );
d32be04c 157 if ( info.contentType != kControlNoContent )
cc374a2f 158 m_peer->SetData( kControlButtonPart, kControlBevelButtonContentTag, info );
3dec57ad 159 }
e9576ca5 160
cc374a2f
DS
161 wxMacReleaseBitmapButton( &info );
162}
c0831a3c
RD
163
164wxSize wxBitmapButton::DoGetBestSize() const
165{
166 wxSize best;
cc374a2f
DS
167
168 best.x = 2 * m_marginX;
169 best.y = 2 * m_marginY;
170 if ( m_bmpNormal.Ok() )
c0831a3c 171 {
cc374a2f
DS
172 best.x += m_bmpNormal.GetWidth();
173 best.y += m_bmpNormal.GetHeight();
c0831a3c 174 }
cc374a2f 175
c0831a3c
RD
176 return best;
177}
179e085f
RN
178
179#endif