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