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