]>
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 | ||
38 | Pixmap XCreateInsensitivePixmap( Display *display, Pixmap pixmap ); | |
39 | ||
4bb6408c | 40 | IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton) |
4bb6408c | 41 | |
a4294b78 JS |
42 | wxBitmapButton::wxBitmapButton() |
43 | { | |
44 | m_marginX = wxDEFAULT_BUTTON_MARGIN; m_marginY = wxDEFAULT_BUTTON_MARGIN; | |
45 | m_insensPixmap = (WXPixmap) 0; | |
46 | } | |
47 | ||
4bb6408c | 48 | bool wxBitmapButton::Create(wxWindow *parent, wxWindowID id, const wxBitmap& bitmap, |
2d120f83 JS |
49 | const wxPoint& pos, |
50 | const wxSize& size, long style, | |
51 | const wxValidator& validator, | |
52 | const wxString& name) | |
4bb6408c JS |
53 | { |
54 | m_buttonBitmap = bitmap; | |
321db4b6 JS |
55 | m_buttonBitmapOriginal = bitmap; |
56 | m_buttonBitmapSelected = bitmap; | |
57 | m_buttonBitmapSelectedOriginal = bitmap; | |
dfe1eee3 | 58 | |
4bb6408c JS |
59 | SetName(name); |
60 | SetValidator(validator); | |
61 | parent->AddChild(this); | |
dfe1eee3 | 62 | |
47bc1060 JS |
63 | m_backgroundColour = parent->GetBackgroundColour() ; |
64 | m_foregroundColour = parent->GetForegroundColour() ; | |
4bb6408c JS |
65 | m_windowStyle = style; |
66 | m_marginX = 0; | |
67 | m_marginY = 0; | |
dfe1eee3 | 68 | |
2d120f83 | 69 | /* |
4bb6408c JS |
70 | int x = pos.x; |
71 | int y = pos.y; | |
72 | int width = size.x; | |
73 | int height = size.y; | |
2d120f83 | 74 | */ |
dfe1eee3 | 75 | |
4bb6408c JS |
76 | if (id == -1) |
77 | m_windowId = NewControlId(); | |
78 | else | |
79 | m_windowId = id; | |
dfe1eee3 | 80 | |
a4294b78 | 81 | Widget parentWidget = (Widget) parent->GetClientWidget(); |
dfe1eee3 | 82 | |
2d120f83 JS |
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 | */ | |
a4294b78 | 92 | Widget buttonWidget = XtVaCreateManagedWidget ("button", |
dfe1eee3 | 93 | |
2d120f83 | 94 | // Gadget causes problems for default button operation. |
a4294b78 | 95 | #if wxUSE_GADGETS |
2d120f83 | 96 | xmPushButtonGadgetClass, parentWidget, |
a4294b78 | 97 | #else |
2d120f83 | 98 | xmPushButtonWidgetClass, parentWidget, |
a4294b78 | 99 | #endif |
2d120f83 JS |
100 | // XmNdefaultButtonShadowThickness, 1, // See comment for wxButton::SetDefault |
101 | NULL); | |
dfe1eee3 | 102 | |
a4294b78 | 103 | m_mainWidget = (WXWidget) buttonWidget; |
dfe1eee3 | 104 | |
da175b2c | 105 | m_font = parent->GetFont(); |
4b5f3fe6 | 106 | ChangeFont(FALSE); |
dfe1eee3 | 107 | |
321db4b6 | 108 | ChangeBackgroundColour (); |
dfe1eee3 | 109 | |
321db4b6 | 110 | DoSetBitmap(); |
dfe1eee3 | 111 | |
a4294b78 | 112 | XtAddCallback (buttonWidget, XmNactivateCallback, (XtCallbackProc) wxButtonCallback, |
2d120f83 | 113 | (XtPointer) this); |
dfe1eee3 | 114 | |
a4294b78 | 115 | SetCanAddEventHandler(TRUE); |
401eb3de RR |
116 | |
117 | wxSize newSize = size; | |
118 | ||
119 | if (m_buttonBitmap.Ok()) | |
120 | { | |
121 | int border = (style & wxNO_BORDER) ? 4 : 10; | |
122 | if (newSize.x == -1) | |
123 | newSize.x = m_buttonBitmap.GetWidth()+border; | |
124 | if (newSize.y == -1) | |
125 | newSize.y = m_buttonBitmap.GetHeight()+border; | |
126 | } | |
127 | ||
128 | AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, newSize.x, newSize.y); | |
dfe1eee3 | 129 | |
a4294b78 JS |
130 | return TRUE; |
131 | } | |
132 | ||
133 | wxBitmapButton::~wxBitmapButton() | |
134 | { | |
135 | SetBitmapLabel(wxNullBitmap); | |
dfe1eee3 | 136 | |
a4294b78 JS |
137 | if (m_insensPixmap) |
138 | XmDestroyPixmap (DefaultScreenOfDisplay ((Display*) GetXDisplay()), (Pixmap) m_insensPixmap); | |
4bb6408c JS |
139 | } |
140 | ||
141 | void wxBitmapButton::SetBitmapLabel(const wxBitmap& bitmap) | |
142 | { | |
321db4b6 | 143 | m_buttonBitmapOriginal = bitmap; |
4bb6408c | 144 | m_buttonBitmap = bitmap; |
dfe1eee3 | 145 | |
321db4b6 JS |
146 | DoSetBitmap(); |
147 | } | |
148 | ||
149 | void wxBitmapButton::SetBitmapSelected(const wxBitmap& sel) | |
150 | { | |
151 | m_buttonBitmapSelected = sel; | |
152 | m_buttonBitmapSelectedOriginal = sel; | |
dfe1eee3 | 153 | |
321db4b6 JS |
154 | DoSetBitmap(); |
155 | }; | |
156 | ||
157 | void wxBitmapButton::SetBitmapFocus(const wxBitmap& focus) | |
158 | { | |
159 | m_buttonBitmapFocus = focus; | |
160 | // Not used in Motif | |
161 | }; | |
162 | ||
163 | void wxBitmapButton::SetBitmapDisabled(const wxBitmap& disabled) | |
164 | { | |
165 | m_buttonBitmapDisabled = disabled; | |
166 | m_buttonBitmapDisabledOriginal = disabled; | |
dfe1eee3 | 167 | |
321db4b6 JS |
168 | DoSetBitmap(); |
169 | }; | |
170 | ||
171 | void wxBitmapButton::DoSetBitmap() | |
172 | { | |
173 | if (m_buttonBitmapOriginal.Ok()) | |
a4294b78 | 174 | { |
321db4b6 JS |
175 | Pixmap pixmap = 0; |
176 | Pixmap insensPixmap = 0; | |
177 | Pixmap armPixmap = 0; | |
dfe1eee3 | 178 | |
321db4b6 JS |
179 | // Must re-make the bitmap to have its transparent areas drawn |
180 | // in the current widget background colour. | |
181 | if (m_buttonBitmapOriginal.GetMask()) | |
182 | { | |
183 | int backgroundPixel; | |
184 | XtVaGetValues((Widget) m_mainWidget, XmNbackground, &backgroundPixel, | |
2d120f83 | 185 | NULL); |
dfe1eee3 | 186 | |
321db4b6 JS |
187 | wxColour col; |
188 | col.SetPixel(backgroundPixel); | |
dfe1eee3 | 189 | |
321db4b6 JS |
190 | wxBitmap newBitmap = wxCreateMaskedBitmap(m_buttonBitmapOriginal, col); |
191 | m_buttonBitmap = newBitmap; | |
dfe1eee3 | 192 | |
321db4b6 JS |
193 | pixmap = (Pixmap) m_buttonBitmap.GetPixmap(); |
194 | } | |
a4294b78 | 195 | else |
321db4b6 | 196 | pixmap = (Pixmap) m_buttonBitmap.GetLabelPixmap(m_mainWidget); |
dfe1eee3 | 197 | |
321db4b6 JS |
198 | if (m_buttonBitmapDisabledOriginal.Ok()) |
199 | { | |
200 | if (m_buttonBitmapDisabledOriginal.GetMask()) | |
201 | { | |
202 | int backgroundPixel; | |
203 | XtVaGetValues((Widget) m_mainWidget, XmNbackground, &backgroundPixel, | |
2d120f83 | 204 | NULL); |
dfe1eee3 | 205 | |
321db4b6 JS |
206 | wxColour col; |
207 | col.SetPixel(backgroundPixel); | |
dfe1eee3 | 208 | |
321db4b6 JS |
209 | wxBitmap newBitmap = wxCreateMaskedBitmap(m_buttonBitmapDisabledOriginal, col); |
210 | m_buttonBitmapDisabled = newBitmap; | |
dfe1eee3 | 211 | |
321db4b6 JS |
212 | insensPixmap = (Pixmap) m_buttonBitmapDisabled.GetPixmap(); |
213 | } | |
214 | else | |
215 | insensPixmap = (Pixmap) m_buttonBitmap.GetInsensPixmap(m_mainWidget); | |
216 | } | |
a4294b78 | 217 | else |
321db4b6 | 218 | insensPixmap = (Pixmap) m_buttonBitmap.GetInsensPixmap(m_mainWidget); |
dfe1eee3 | 219 | |
321db4b6 JS |
220 | // Now make the bitmap representing the armed state |
221 | if (m_buttonBitmapSelectedOriginal.Ok()) | |
a4294b78 | 222 | { |
321db4b6 JS |
223 | if (m_buttonBitmapSelectedOriginal.GetMask()) |
224 | { | |
225 | int backgroundPixel; | |
226 | XtVaGetValues((Widget) m_mainWidget, XmNarmColor, &backgroundPixel, | |
2d120f83 | 227 | NULL); |
dfe1eee3 | 228 | |
321db4b6 JS |
229 | wxColour col; |
230 | col.SetPixel(backgroundPixel); | |
dfe1eee3 | 231 | |
321db4b6 JS |
232 | wxBitmap newBitmap = wxCreateMaskedBitmap(m_buttonBitmapSelectedOriginal, col); |
233 | m_buttonBitmapSelected = newBitmap; | |
dfe1eee3 | 234 | |
321db4b6 JS |
235 | armPixmap = (Pixmap) m_buttonBitmapSelected.GetPixmap(); |
236 | } | |
237 | else | |
2d120f83 | 238 | armPixmap = (Pixmap) m_buttonBitmap.GetArmPixmap(m_mainWidget); |
321db4b6 JS |
239 | } |
240 | else | |
2d120f83 | 241 | armPixmap = (Pixmap) m_buttonBitmap.GetArmPixmap(m_mainWidget); |
dfe1eee3 | 242 | |
321db4b6 JS |
243 | if (insensPixmap == pixmap) // <- the Get...Pixmap()-functions return the same pixmap! |
244 | { | |
245 | insensPixmap = | |
246 | XCreateInsensitivePixmap(DisplayOfScreen(XtScreen((Widget) m_mainWidget)), pixmap); | |
a4294b78 JS |
247 | m_insensPixmap = (WXPixmap) insensPixmap; |
248 | } | |
dfe1eee3 | 249 | |
a4294b78 | 250 | XtVaSetValues ((Widget) m_mainWidget, |
321db4b6 JS |
251 | XmNlabelPixmap, pixmap, |
252 | XmNlabelInsensitivePixmap, insensPixmap, | |
a4294b78 JS |
253 | XmNarmPixmap, armPixmap, |
254 | XmNlabelType, XmPIXMAP, | |
255 | NULL); | |
256 | } | |
257 | else | |
258 | { | |
259 | // Null bitmap: must not use current pixmap | |
260 | // since it is no longer valid. | |
261 | XtVaSetValues ((Widget) m_mainWidget, | |
262 | XmNlabelType, XmSTRING, | |
1a3ac83f | 263 | XmNlabelPixmap, XmUNSPECIFIED_PIXMAP, |
2d120f83 | 264 | XmNlabelInsensitivePixmap, XmUNSPECIFIED_PIXMAP, |
321db4b6 | 265 | XmNarmPixmap, XmUNSPECIFIED_PIXMAP, |
a4294b78 JS |
266 | NULL); |
267 | } | |
4bb6408c JS |
268 | } |
269 | ||
321db4b6 | 270 | void wxBitmapButton::ChangeBackgroundColour() |
a4294b78 | 271 | { |
321db4b6 | 272 | DoChangeBackgroundColour(m_mainWidget, m_backgroundColour, TRUE); |
dfe1eee3 | 273 | |
321db4b6 JS |
274 | // Must reset the bitmaps since the colours have changed. |
275 | DoSetBitmap(); | |
276 | } | |
401eb3de RR |
277 | |
278 | wxSize wxBitmapButton::DoGetBestSize() const | |
279 | { | |
280 | wxSize ret( 30,30 ); | |
281 | ||
282 | if (m_buttonBitmap.Ok()) | |
283 | { | |
284 | int border = (GetWindowStyle() & wxNO_BORDER) ? 4 : 10; | |
285 | ret.x = m_buttonBitmap.GetWidth()+border; | |
286 | ret.y = m_buttonBitmap.GetHeight()+border; | |
287 | } | |
288 | ||
289 | if (!HasFlag(wxBU_EXACTFIT)) | |
290 | { | |
291 | if (ret.x < 80) ret.x = 80; | |
292 | } | |
293 | ||
294 | return ret; | |
295 | } | |
296 |