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