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