]> git.saurik.com Git - wxWidgets.git/blame - src/motif/bmpbuttn.cpp
bug with object being passed through ... corrected
[wxWidgets.git] / src / motif / bmpbuttn.cpp
CommitLineData
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
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
24void wxButtonCallback (Widget w, XtPointer clientData, XtPointer ptr);
25
26Pixmap XCreateInsensitivePixmap( Display *display, Pixmap pixmap );
27
4bb6408c
JS
28#if !USE_SHARED_LIBRARY
29IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton)
30#endif
31
a4294b78
JS
32wxBitmapButton::wxBitmapButton()
33{
34 m_marginX = wxDEFAULT_BUTTON_MARGIN; m_marginY = wxDEFAULT_BUTTON_MARGIN;
35 m_insensPixmap = (WXPixmap) 0;
36}
37
4bb6408c 38bool 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;
dfe1eee3 48
4bb6408c
JS
49 SetName(name);
50 SetValidator(validator);
51 parent->AddChild(this);
dfe1eee3 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;
dfe1eee3 58
2d120f83 59 /*
4bb6408c
JS
60 int x = pos.x;
61 int y = pos.y;
62 int width = size.x;
63 int height = size.y;
2d120f83 64 */
dfe1eee3 65
4bb6408c
JS
66 if (id == -1)
67 m_windowId = NewControlId();
68 else
69 m_windowId = id;
dfe1eee3 70
a4294b78 71 Widget parentWidget = (Widget) parent->GetClientWidget();
dfe1eee3 72
2d120f83
JS
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",
dfe1eee3 83
2d120f83 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);
dfe1eee3 92
a4294b78 93 m_mainWidget = (WXWidget) buttonWidget;
dfe1eee3 94
da175b2c 95 m_font = parent->GetFont();
4b5f3fe6 96 ChangeFont(FALSE);
dfe1eee3 97
321db4b6 98 ChangeBackgroundColour ();
dfe1eee3 99
321db4b6 100 DoSetBitmap();
dfe1eee3 101
a4294b78 102 XtAddCallback (buttonWidget, XmNactivateCallback, (XtCallbackProc) wxButtonCallback,
2d120f83 103 (XtPointer) this);
dfe1eee3 104
a4294b78
JS
105 SetCanAddEventHandler(TRUE);
106 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y);
dfe1eee3 107
a4294b78
JS
108 return TRUE;
109}
110
111wxBitmapButton::~wxBitmapButton()
112{
113 SetBitmapLabel(wxNullBitmap);
dfe1eee3 114
a4294b78
JS
115 if (m_insensPixmap)
116 XmDestroyPixmap (DefaultScreenOfDisplay ((Display*) GetXDisplay()), (Pixmap) m_insensPixmap);
4bb6408c
JS
117}
118
119void wxBitmapButton::SetBitmapLabel(const wxBitmap& bitmap)
120{
321db4b6 121 m_buttonBitmapOriginal = bitmap;
4bb6408c 122 m_buttonBitmap = bitmap;
dfe1eee3 123
321db4b6
JS
124 DoSetBitmap();
125}
126
127void wxBitmapButton::SetBitmapSelected(const wxBitmap& sel)
128{
129 m_buttonBitmapSelected = sel;
130 m_buttonBitmapSelectedOriginal = sel;
dfe1eee3 131
321db4b6
JS
132 DoSetBitmap();
133};
134
135void wxBitmapButton::SetBitmapFocus(const wxBitmap& focus)
136{
137 m_buttonBitmapFocus = focus;
138 // Not used in Motif
139};
140
141void wxBitmapButton::SetBitmapDisabled(const wxBitmap& disabled)
142{
143 m_buttonBitmapDisabled = disabled;
144 m_buttonBitmapDisabledOriginal = disabled;
dfe1eee3 145
321db4b6
JS
146 DoSetBitmap();
147};
148
149void wxBitmapButton::DoSetBitmap()
150{
151 if (m_buttonBitmapOriginal.Ok())
a4294b78 152 {
321db4b6
JS
153 Pixmap pixmap = 0;
154 Pixmap insensPixmap = 0;
155 Pixmap armPixmap = 0;
dfe1eee3 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 163 NULL);
dfe1eee3 164
321db4b6
JS
165 wxColour col;
166 col.SetPixel(backgroundPixel);
dfe1eee3 167
321db4b6
JS
168 wxBitmap newBitmap = wxCreateMaskedBitmap(m_buttonBitmapOriginal, col);
169 m_buttonBitmap = newBitmap;
dfe1eee3 170
321db4b6
JS
171 pixmap = (Pixmap) m_buttonBitmap.GetPixmap();
172 }
a4294b78 173 else
321db4b6 174 pixmap = (Pixmap) m_buttonBitmap.GetLabelPixmap(m_mainWidget);
dfe1eee3 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 182 NULL);
dfe1eee3 183
321db4b6
JS
184 wxColour col;
185 col.SetPixel(backgroundPixel);
dfe1eee3 186
321db4b6
JS
187 wxBitmap newBitmap = wxCreateMaskedBitmap(m_buttonBitmapDisabledOriginal, col);
188 m_buttonBitmapDisabled = newBitmap;
dfe1eee3 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);
dfe1eee3 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 205 NULL);
dfe1eee3 206
321db4b6
JS
207 wxColour col;
208 col.SetPixel(backgroundPixel);
dfe1eee3 209
321db4b6
JS
210 wxBitmap newBitmap = wxCreateMaskedBitmap(m_buttonBitmapSelectedOriginal, col);
211 m_buttonBitmapSelected = newBitmap;
dfe1eee3 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 219 armPixmap = (Pixmap) m_buttonBitmap.GetArmPixmap(m_mainWidget);
dfe1eee3 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 }
dfe1eee3 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 248void wxBitmapButton::ChangeBackgroundColour()
a4294b78 249{
321db4b6 250 DoChangeBackgroundColour(m_mainWidget, m_backgroundColour, TRUE);
dfe1eee3 251
321db4b6
JS
252 // Must reset the bitmaps since the colours have changed.
253 DoSetBitmap();
254}