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