Applied patch [ 832096 ] Final separation for GUI and console for Open Watcom
[wxWidgets.git] / src / motif / bmpbuttn.cpp
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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "bmpbuttn.h"
14 #endif
15
16 #ifdef __VMS
17 #define XtScreen XTSCREEN
18 #endif
19
20 #include "wx/defs.h"
21
22 #include "wx/bmpbuttn.h"
23
24 #ifdef __VMS__
25 #pragma message disable nosimpint
26 #endif
27 #include <Xm/PushBG.h>
28 #include <Xm/PushB.h>
29 #ifdef __VMS__
30 #pragma message enable nosimpint
31 #endif
32
33 #include "wx/motif/private.h"
34
35 // Implemented in button.cpp
36 void wxButtonCallback (Widget w, XtPointer clientData, XtPointer ptr);
37
38 // Pixmap XCreateInsensitivePixmap( Display *display, Pixmap pixmap );
39
40 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton)
41
42 wxBitmapButtonBase::wxBitmapButtonBase()
43 : m_bmpNormal(),
44 m_bmpSelected(),
45 m_bmpFocus(),
46 m_bmpDisabled(),
47 m_marginX(0),
48 m_marginY(0)
49 {
50 }
51
52 wxBitmapButton::wxBitmapButton()
53 {
54 m_marginX = m_marginY = wxDEFAULT_BUTTON_MARGIN;
55 m_insensPixmap = (WXPixmap) 0;
56 }
57
58 bool wxBitmapButton::Create(wxWindow *parent, wxWindowID id,
59 const wxBitmap& bitmap,
60 const wxPoint& pos,
61 const wxSize& size, long style,
62 const wxValidator& validator,
63 const wxString& name)
64 {
65 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
66 return false;
67
68 m_bmpNormal = m_bmpNormalOriginal = bitmap;
69 m_bmpSelected = m_bmpSelectedOriginal = bitmap;
70
71 Widget parentWidget = (Widget) parent->GetClientWidget();
72
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 */
82 Widget buttonWidget = XtVaCreateManagedWidget ("button",
83
84 // Gadget causes problems for default button operation.
85 #if wxUSE_GADGETS
86 xmPushButtonGadgetClass, parentWidget,
87 #else
88 xmPushButtonWidgetClass, parentWidget,
89 #endif
90 // See comment for wxButton::SetDefault
91 // XmNdefaultButtonShadowThickness, 1,
92 XmNrecomputeSize, False,
93 NULL);
94
95 m_mainWidget = (WXWidget) buttonWidget;
96
97 ChangeFont(FALSE);
98
99 ChangeBackgroundColour ();
100
101 DoSetBitmap();
102
103 XtAddCallback (buttonWidget,
104 XmNactivateCallback, (XtCallbackProc) wxButtonCallback,
105 (XtPointer) this);
106
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);
113
114 return TRUE;
115 }
116
117 wxBitmapButton::~wxBitmapButton()
118 {
119 SetBitmapLabel(wxNullBitmap);
120
121 if (m_insensPixmap)
122 XmDestroyPixmap (DefaultScreenOfDisplay ((Display*) GetXDisplay()),
123 (Pixmap) m_insensPixmap);
124 }
125
126 void wxBitmapButton::SetBitmapLabel(const wxBitmap& bitmap)
127 {
128 m_bmpNormalOriginal = bitmap;
129 m_bmpNormal = bitmap;
130
131 DoSetBitmap();
132 }
133
134 void wxBitmapButton::SetBitmapSelected(const wxBitmap& sel)
135 {
136 m_bmpSelected = sel;
137 m_bmpSelectedOriginal = sel;
138
139 DoSetBitmap();
140 };
141
142 void wxBitmapButton::SetBitmapFocus(const wxBitmap& focus)
143 {
144 m_bmpFocus = focus;
145 // Not used in Motif
146 };
147
148 void wxBitmapButton::SetBitmapDisabled(const wxBitmap& disabled)
149 {
150 m_bmpDisabled = disabled;
151 m_bmpDisabledOriginal = disabled;
152
153 DoSetBitmap();
154 };
155
156 void wxBitmapButton::DoSetBitmap()
157 {
158 if (m_bmpNormalOriginal.Ok())
159 {
160 Pixmap pixmap = 0;
161 Pixmap insensPixmap = 0;
162 Pixmap armPixmap = 0;
163
164 // Must re-make the bitmap to have its transparent areas drawn
165 // in the current widget background colour.
166 if (m_bmpNormalOriginal.GetMask())
167 {
168 int backgroundPixel;
169 XtVaGetValues((Widget) m_mainWidget,
170 XmNbackground, &backgroundPixel,
171 NULL);
172
173 wxColour col;
174 col.SetPixel(backgroundPixel);
175
176 wxBitmap newBitmap =
177 wxCreateMaskedBitmap(m_bmpNormalOriginal, col);
178 m_bmpNormal = newBitmap;
179 m_bitmapCache.SetBitmap( m_bmpNormal );
180
181 pixmap = (Pixmap) m_bmpNormal.GetDrawable();
182 }
183 else
184 {
185 m_bitmapCache.SetBitmap( m_bmpNormal );
186 pixmap = (Pixmap) m_bitmapCache.GetLabelPixmap(m_mainWidget);
187 }
188
189 if (m_bmpDisabledOriginal.Ok())
190 {
191 if (m_bmpDisabledOriginal.GetMask())
192 {
193 int backgroundPixel;
194 XtVaGetValues((Widget) m_mainWidget,
195 XmNbackground, &backgroundPixel,
196 NULL);
197
198 wxColour col;
199 col.SetPixel(backgroundPixel);
200
201 wxBitmap newBitmap =
202 wxCreateMaskedBitmap(m_bmpDisabledOriginal, col);
203 m_bmpDisabled = newBitmap;
204
205 insensPixmap = (Pixmap) m_bmpDisabled.GetDrawable();
206 }
207 else
208 insensPixmap = (Pixmap) m_bitmapCache.GetInsensPixmap(m_mainWidget);
209 }
210 else
211 insensPixmap = (Pixmap) m_bitmapCache.GetInsensPixmap(m_mainWidget);
212
213 // Now make the bitmap representing the armed state
214 if (m_bmpSelectedOriginal.Ok())
215 {
216 if (m_bmpSelectedOriginal.GetMask())
217 {
218 int backgroundPixel;
219 XtVaGetValues((Widget) m_mainWidget,
220 XmNarmColor, &backgroundPixel,
221 NULL);
222
223 wxColour col;
224 col.SetPixel(backgroundPixel);
225
226 wxBitmap newBitmap =
227 wxCreateMaskedBitmap(m_bmpSelectedOriginal, col);
228 m_bmpSelected = newBitmap;
229
230 armPixmap = (Pixmap) m_bmpSelected.GetDrawable();
231 }
232 else
233 armPixmap = (Pixmap) m_bitmapCache.GetArmPixmap(m_mainWidget);
234 }
235 else
236 armPixmap = (Pixmap) m_bitmapCache.GetArmPixmap(m_mainWidget);
237
238 XtVaSetValues ((Widget) m_mainWidget,
239 XmNlabelPixmap, pixmap,
240 XmNlabelInsensitivePixmap, insensPixmap,
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,
251 XmNlabelPixmap, XmUNSPECIFIED_PIXMAP,
252 XmNlabelInsensitivePixmap, XmUNSPECIFIED_PIXMAP,
253 XmNarmPixmap, XmUNSPECIFIED_PIXMAP,
254 NULL);
255 }
256 }
257
258 void wxBitmapButton::ChangeBackgroundColour()
259 {
260 wxDoChangeBackgroundColour(m_mainWidget, m_backgroundColour, TRUE);
261
262 // Must reset the bitmaps since the colours have changed.
263 DoSetBitmap();
264 }
265
266 wxSize wxBitmapButton::DoGetBestSize() const
267 {
268 wxSize ret( 30,30 );
269
270 if (m_bmpNormal.Ok())
271 {
272 int border = (GetWindowStyle() & wxNO_BORDER) ? 4 : 10;
273 ret.x = m_bmpNormal.GetWidth()+border;
274 ret.y = m_bmpNormal.GetHeight()+border;
275 }
276
277 return ret;
278 }
279