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