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