]>
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 JS |
38 | bool wxBitmapButton::Create(wxWindow *parent, wxWindowID id, const wxBitmap& bitmap, |
39 | const wxPoint& pos, | |
40 | const wxSize& size, long style, | |
41 | const wxValidator& validator, | |
42 | const wxString& name) | |
43 | { | |
44 | m_buttonBitmap = bitmap; | |
321db4b6 JS |
45 | m_buttonBitmapOriginal = bitmap; |
46 | m_buttonBitmapSelected = bitmap; | |
47 | m_buttonBitmapSelectedOriginal = bitmap; | |
48 | ||
4bb6408c JS |
49 | SetName(name); |
50 | SetValidator(validator); | |
51 | parent->AddChild(this); | |
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; | |
58 | ||
f9e02ac7 | 59 | /* |
4bb6408c JS |
60 | int x = pos.x; |
61 | int y = pos.y; | |
62 | int width = size.x; | |
63 | int height = size.y; | |
f9e02ac7 | 64 | */ |
4bb6408c JS |
65 | |
66 | if (id == -1) | |
67 | m_windowId = NewControlId(); | |
68 | else | |
69 | m_windowId = id; | |
70 | ||
a4294b78 JS |
71 | Widget parentWidget = (Widget) parent->GetClientWidget(); |
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 | */ | |
82 | Widget buttonWidget = XtVaCreateManagedWidget ("button", | |
83 | ||
84 | // Gadget causes problems for default button operation. | |
85 | #if wxUSE_GADGETS | |
86 | xmPushButtonGadgetClass, parentWidget, | |
87 | #else | |
88 | xmPushButtonWidgetClass, parentWidget, | |
89 | #endif | |
90 | // XmNdefaultButtonShadowThickness, 1, // See comment for wxButton::SetDefault | |
91 | NULL); | |
92 | ||
93 | m_mainWidget = (WXWidget) buttonWidget; | |
94 | ||
4b5f3fe6 JS |
95 | m_windowFont = parent->GetFont(); |
96 | ChangeFont(FALSE); | |
97 | ||
321db4b6 | 98 | ChangeBackgroundColour (); |
a4294b78 | 99 | |
321db4b6 | 100 | DoSetBitmap(); |
a4294b78 JS |
101 | |
102 | XtAddCallback (buttonWidget, XmNactivateCallback, (XtCallbackProc) wxButtonCallback, | |
103 | (XtPointer) this); | |
104 | ||
105 | SetCanAddEventHandler(TRUE); | |
106 | AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y); | |
107 | ||
a4294b78 JS |
108 | return TRUE; |
109 | } | |
110 | ||
111 | wxBitmapButton::~wxBitmapButton() | |
112 | { | |
113 | SetBitmapLabel(wxNullBitmap); | |
114 | ||
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; |
a4294b78 | 123 | |
321db4b6 JS |
124 | DoSetBitmap(); |
125 | } | |
126 | ||
127 | void wxBitmapButton::SetBitmapSelected(const wxBitmap& sel) | |
128 | { | |
129 | m_buttonBitmapSelected = sel; | |
130 | m_buttonBitmapSelectedOriginal = sel; | |
131 | ||
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; | |
145 | ||
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; | |
a4294b78 | 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, | |
163 | NULL); | |
164 | ||
165 | wxColour col; | |
166 | col.SetPixel(backgroundPixel); | |
167 | ||
168 | wxBitmap newBitmap = wxCreateMaskedBitmap(m_buttonBitmapOriginal, col); | |
169 | m_buttonBitmap = newBitmap; | |
a4294b78 | 170 | |
321db4b6 JS |
171 | pixmap = (Pixmap) m_buttonBitmap.GetPixmap(); |
172 | } | |
a4294b78 | 173 | else |
321db4b6 | 174 | pixmap = (Pixmap) m_buttonBitmap.GetLabelPixmap(m_mainWidget); |
a4294b78 | 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, | |
182 | NULL); | |
183 | ||
184 | wxColour col; | |
185 | col.SetPixel(backgroundPixel); | |
186 | ||
187 | wxBitmap newBitmap = wxCreateMaskedBitmap(m_buttonBitmapDisabledOriginal, col); | |
188 | m_buttonBitmapDisabled = newBitmap; | |
189 | ||
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); |
a4294b78 | 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, | |
205 | NULL); | |
206 | ||
207 | wxColour col; | |
208 | col.SetPixel(backgroundPixel); | |
209 | ||
210 | wxBitmap newBitmap = wxCreateMaskedBitmap(m_buttonBitmapSelectedOriginal, col); | |
211 | m_buttonBitmapSelected = newBitmap; | |
212 | ||
213 | armPixmap = (Pixmap) m_buttonBitmapSelected.GetPixmap(); | |
214 | } | |
215 | else | |
216 | armPixmap = (Pixmap) m_buttonBitmap.GetArmPixmap(m_mainWidget); | |
217 | } | |
218 | else | |
219 | armPixmap = (Pixmap) m_buttonBitmap.GetArmPixmap(m_mainWidget); | |
220 | ||
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 | } | |
227 | ||
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, |
321db4b6 JS |
242 | XmNlabelInsensitivePixmap, XmUNSPECIFIED_PIXMAP, |
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); |
a4294b78 | 251 | |
321db4b6 JS |
252 | // Must reset the bitmaps since the colours have changed. |
253 | DoSetBitmap(); | |
254 | } |