]> git.saurik.com Git - wxWidgets.git/blame - src/motif/bmpbuttn.cpp
reSWIGged
[wxWidgets.git] / src / motif / bmpbuttn.cpp
CommitLineData
4bb6408c
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: bmpbuttn.cpp
3// Purpose: wxBitmapButton
4// Author: Julian Smart
5// Modified by:
6// Created: 17/09/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
dfe1eee3 9// Licence: wxWindows licence
4bb6408c
JS
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
4bb6408c
JS
13#pragma implementation "bmpbuttn.h"
14#endif
15
bcd055ae
JJ
16#ifdef __VMS
17#define XtScreen XTSCREEN
18#endif
19
f6045f99
GD
20#include "wx/defs.h"
21
4bb6408c
JS
22#include "wx/bmpbuttn.h"
23
338dd992
JJ
24#ifdef __VMS__
25#pragma message disable nosimpint
26#endif
a4294b78
JS
27#include <Xm/PushBG.h>
28#include <Xm/PushB.h>
338dd992
JJ
29#ifdef __VMS__
30#pragma message enable nosimpint
31#endif
a4294b78
JS
32
33#include "wx/motif/private.h"
34
35// Implemented in button.cpp
36void wxButtonCallback (Widget w, XtPointer clientData, XtPointer ptr);
37
aae91497 38// Pixmap XCreateInsensitivePixmap( Display *display, Pixmap pixmap );
a4294b78 39
4bb6408c 40IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton)
4bb6408c 41
1169a919
JS
42wxBitmapButtonBase::wxBitmapButtonBase()
43 : m_bmpNormal(),
44 m_bmpSelected(),
45 m_bmpFocus(),
46 m_bmpDisabled(),
47 m_marginX(0),
48 m_marginY(0)
49{
50}
51
a4294b78
JS
52wxBitmapButton::wxBitmapButton()
53{
ea9868e8 54 m_marginX = m_marginY = wxDEFAULT_BUTTON_MARGIN;
a4294b78
JS
55 m_insensPixmap = (WXPixmap) 0;
56}
57
ea9868e8
MB
58bool wxBitmapButton::Create(wxWindow *parent, wxWindowID id,
59 const wxBitmap& bitmap,
2d120f83
JS
60 const wxPoint& pos,
61 const wxSize& size, long style,
62 const wxValidator& validator,
63 const wxString& name)
4bb6408c 64{
ea9868e8
MB
65 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
66 return false;
dfe1eee3 67
ea9868e8
MB
68 m_bmpNormal = m_bmpNormalOriginal = bitmap;
69 m_bmpSelected = m_bmpSelectedOriginal = bitmap;
dfe1eee3 70
a4294b78 71 Widget parentWidget = (Widget) parent->GetClientWidget();
dfe1eee3 72
2d120f83
JS
73 /*
74 * Patch Note (important)
75 * There is no major reason to put a defaultButtonThickness here.
76 * Not requesting it give the ability to put wxButton with a spacing
77 * as small as requested. However, if some button become a DefaultButton,
78 * other buttons are no more aligned -- This is why we set
79 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
80 * in the ::SetDefaultButton method.
81 */
a4294b78 82 Widget buttonWidget = XtVaCreateManagedWidget ("button",
dfe1eee3 83
2d120f83 84 // Gadget causes problems for default button operation.
a4294b78 85#if wxUSE_GADGETS
2d120f83 86 xmPushButtonGadgetClass, parentWidget,
a4294b78 87#else
2d120f83 88 xmPushButtonWidgetClass, parentWidget,
a4294b78 89#endif
ea9868e8
MB
90 // See comment for wxButton::SetDefault
91 // XmNdefaultButtonShadowThickness, 1,
92 XmNrecomputeSize, False,
2d120f83 93 NULL);
dfe1eee3 94
a4294b78 95 m_mainWidget = (WXWidget) buttonWidget;
dfe1eee3 96
4b5f3fe6 97 ChangeFont(FALSE);
dfe1eee3 98
321db4b6 99 ChangeBackgroundColour ();
dfe1eee3 100
321db4b6 101 DoSetBitmap();
dfe1eee3 102
ea9868e8
MB
103 XtAddCallback (buttonWidget,
104 XmNactivateCallback, (XtCallbackProc) wxButtonCallback,
105 (XtPointer) this);
dfe1eee3 106
ea9868e8
MB
107 wxSize best = m_bmpNormal.Ok() ? GetBestSize() : wxSize(30, 30);
108 if( size.x != -1 ) best.x = size.x;
109 if( size.y != -1 ) best.y = size.y;
110
111 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
112 pos.x, pos.y, best.x, best.y);
dfe1eee3 113
a4294b78
JS
114 return TRUE;
115}
116
117wxBitmapButton::~wxBitmapButton()
118{
119 SetBitmapLabel(wxNullBitmap);
dfe1eee3 120
a4294b78 121 if (m_insensPixmap)
ea9868e8
MB
122 XmDestroyPixmap (DefaultScreenOfDisplay ((Display*) GetXDisplay()),
123 (Pixmap) m_insensPixmap);
4bb6408c
JS
124}
125
126void wxBitmapButton::SetBitmapLabel(const wxBitmap& bitmap)
127{
ea9868e8
MB
128 m_bmpNormalOriginal = bitmap;
129 m_bmpNormal = bitmap;
dfe1eee3 130
321db4b6
JS
131 DoSetBitmap();
132}
133
134void wxBitmapButton::SetBitmapSelected(const wxBitmap& sel)
135{
ea9868e8
MB
136 m_bmpSelected = sel;
137 m_bmpSelectedOriginal = sel;
dfe1eee3 138
321db4b6
JS
139 DoSetBitmap();
140};
141
142void wxBitmapButton::SetBitmapFocus(const wxBitmap& focus)
143{
ea9868e8 144 m_bmpFocus = focus;
321db4b6
JS
145 // Not used in Motif
146};
147
148void wxBitmapButton::SetBitmapDisabled(const wxBitmap& disabled)
149{
ea9868e8
MB
150 m_bmpDisabled = disabled;
151 m_bmpDisabledOriginal = disabled;
dfe1eee3 152
321db4b6
JS
153 DoSetBitmap();
154};
155
156void wxBitmapButton::DoSetBitmap()
157{
ea9868e8 158 if (m_bmpNormalOriginal.Ok())
a4294b78 159 {
321db4b6
JS
160 Pixmap pixmap = 0;
161 Pixmap insensPixmap = 0;
162 Pixmap armPixmap = 0;
dfe1eee3 163
321db4b6
JS
164 // Must re-make the bitmap to have its transparent areas drawn
165 // in the current widget background colour.
ea9868e8 166 if (m_bmpNormalOriginal.GetMask())
321db4b6
JS
167 {
168 int backgroundPixel;
ea9868e8
MB
169 XtVaGetValues((Widget) m_mainWidget,
170 XmNbackground, &backgroundPixel,
171 NULL);
dfe1eee3 172
321db4b6
JS
173 wxColour col;
174 col.SetPixel(backgroundPixel);
dfe1eee3 175
ea9868e8
MB
176 wxBitmap newBitmap =
177 wxCreateMaskedBitmap(m_bmpNormalOriginal, col);
178 m_bmpNormal = newBitmap;
aae91497 179 m_bitmapCache.SetBitmap( m_bmpNormal );
dfe1eee3 180
aae0472b 181 pixmap = (Pixmap) m_bmpNormal.GetDrawable();
321db4b6 182 }
a4294b78 183 else
aae91497
MB
184 {
185 m_bitmapCache.SetBitmap( m_bmpNormal );
186 pixmap = (Pixmap) m_bitmapCache.GetLabelPixmap(m_mainWidget);
187 }
dfe1eee3 188
ea9868e8 189 if (m_bmpDisabledOriginal.Ok())
321db4b6 190 {
ea9868e8 191 if (m_bmpDisabledOriginal.GetMask())
321db4b6
JS
192 {
193 int backgroundPixel;
ea9868e8
MB
194 XtVaGetValues((Widget) m_mainWidget,
195 XmNbackground, &backgroundPixel,
196 NULL);
dfe1eee3 197
321db4b6
JS
198 wxColour col;
199 col.SetPixel(backgroundPixel);
dfe1eee3 200
ea9868e8
MB
201 wxBitmap newBitmap =
202 wxCreateMaskedBitmap(m_bmpDisabledOriginal, col);
203 m_bmpDisabled = newBitmap;
dfe1eee3 204
aae0472b 205 insensPixmap = (Pixmap) m_bmpDisabled.GetDrawable();
321db4b6
JS
206 }
207 else
aae91497 208 insensPixmap = (Pixmap) m_bitmapCache.GetInsensPixmap(m_mainWidget);
321db4b6 209 }
a4294b78 210 else
aae91497 211 insensPixmap = (Pixmap) m_bitmapCache.GetInsensPixmap(m_mainWidget);
dfe1eee3 212
321db4b6 213 // Now make the bitmap representing the armed state
ea9868e8 214 if (m_bmpSelectedOriginal.Ok())
a4294b78 215 {
ea9868e8 216 if (m_bmpSelectedOriginal.GetMask())
321db4b6
JS
217 {
218 int backgroundPixel;
ea9868e8
MB
219 XtVaGetValues((Widget) m_mainWidget,
220 XmNarmColor, &backgroundPixel,
221 NULL);
dfe1eee3 222
321db4b6
JS
223 wxColour col;
224 col.SetPixel(backgroundPixel);
dfe1eee3 225
ea9868e8
MB
226 wxBitmap newBitmap =
227 wxCreateMaskedBitmap(m_bmpSelectedOriginal, col);
228 m_bmpSelected = newBitmap;
dfe1eee3 229
aae0472b 230 armPixmap = (Pixmap) m_bmpSelected.GetDrawable();
321db4b6
JS
231 }
232 else
aae91497 233 armPixmap = (Pixmap) m_bitmapCache.GetArmPixmap(m_mainWidget);
321db4b6
JS
234 }
235 else
aae91497 236 armPixmap = (Pixmap) m_bitmapCache.GetArmPixmap(m_mainWidget);
dfe1eee3 237
a4294b78 238 XtVaSetValues ((Widget) m_mainWidget,
321db4b6
JS
239 XmNlabelPixmap, pixmap,
240 XmNlabelInsensitivePixmap, insensPixmap,
a4294b78
JS
241 XmNarmPixmap, armPixmap,
242 XmNlabelType, XmPIXMAP,
243 NULL);
244 }
245 else
246 {
247 // Null bitmap: must not use current pixmap
248 // since it is no longer valid.
249 XtVaSetValues ((Widget) m_mainWidget,
250 XmNlabelType, XmSTRING,
1a3ac83f 251 XmNlabelPixmap, XmUNSPECIFIED_PIXMAP,
2d120f83 252 XmNlabelInsensitivePixmap, XmUNSPECIFIED_PIXMAP,
321db4b6 253 XmNarmPixmap, XmUNSPECIFIED_PIXMAP,
a4294b78
JS
254 NULL);
255 }
4bb6408c
JS
256}
257
321db4b6 258void wxBitmapButton::ChangeBackgroundColour()
a4294b78 259{
a8680e3e 260 wxDoChangeBackgroundColour(m_mainWidget, m_backgroundColour, TRUE);
dfe1eee3 261
321db4b6
JS
262 // Must reset the bitmaps since the colours have changed.
263 DoSetBitmap();
264}
401eb3de
RR
265
266wxSize wxBitmapButton::DoGetBestSize() const
267{
268 wxSize ret( 30,30 );
269
ea9868e8 270 if (m_bmpNormal.Ok())
401eb3de
RR
271 {
272 int border = (GetWindowStyle() & wxNO_BORDER) ? 4 : 10;
ea9868e8
MB
273 ret.x = m_bmpNormal.GetWidth()+border;
274 ret.y = m_bmpNormal.GetHeight()+border;
401eb3de
RR
275 }
276
277 return ret;
278}
279