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