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