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