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