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