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