1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxBitmapButton
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "bmpbuttn.h"
17 #define XtScreen XTSCREEN
22 #include "wx/bmpbuttn.h"
25 #pragma message disable nosimpint
27 #include <Xm/PushBG.h>
30 #pragma message enable nosimpint
33 #include "wx/motif/private.h"
35 // Implemented in button.cpp
36 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer ptr
);
38 // Pixmap XCreateInsensitivePixmap( Display *display, Pixmap pixmap );
40 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton
, wxButton
)
42 wxBitmapButton::wxBitmapButton()
44 m_marginX
= m_marginY
= wxDEFAULT_BUTTON_MARGIN
;
45 m_insensPixmap
= (WXPixmap
) 0;
48 bool wxBitmapButton::Create(wxWindow
*parent
, wxWindowID id
,
49 const wxBitmap
& bitmap
,
51 const wxSize
& size
, long style
,
52 const wxValidator
& validator
,
55 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
58 m_bmpNormal
= m_bmpNormalOriginal
= bitmap
;
59 m_bmpSelected
= m_bmpSelectedOriginal
= bitmap
;
68 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
71 * Patch Note (important)
72 * There is no major reason to put a defaultButtonThickness here.
73 * Not requesting it give the ability to put wxButton with a spacing
74 * as small as requested. However, if some button become a DefaultButton,
75 * other buttons are no more aligned -- This is why we set
76 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
77 * in the ::SetDefaultButton method.
79 Widget buttonWidget
= XtVaCreateManagedWidget ("button",
81 // Gadget causes problems for default button operation.
83 xmPushButtonGadgetClass
, parentWidget
,
85 xmPushButtonWidgetClass
, parentWidget
,
87 // See comment for wxButton::SetDefault
88 // XmNdefaultButtonShadowThickness, 1,
89 XmNrecomputeSize
, False
,
92 m_mainWidget
= (WXWidget
) buttonWidget
;
96 ChangeBackgroundColour ();
100 XtAddCallback (buttonWidget
,
101 XmNactivateCallback
, (XtCallbackProc
) wxButtonCallback
,
104 SetCanAddEventHandler(TRUE
);
106 wxSize best
= m_bmpNormal
.Ok() ? GetBestSize() : wxSize(30, 30);
107 if( size
.x
!= -1 ) best
.x
= size
.x
;
108 if( size
.y
!= -1 ) best
.y
= size
.y
;
110 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
111 pos
.x
, pos
.y
, best
.x
, best
.y
);
116 wxBitmapButton::~wxBitmapButton()
118 SetBitmapLabel(wxNullBitmap
);
121 XmDestroyPixmap (DefaultScreenOfDisplay ((Display
*) GetXDisplay()),
122 (Pixmap
) m_insensPixmap
);
125 void wxBitmapButton::SetBitmapLabel(const wxBitmap
& bitmap
)
127 m_bmpNormalOriginal
= bitmap
;
128 m_bmpNormal
= bitmap
;
133 void wxBitmapButton::SetBitmapSelected(const wxBitmap
& sel
)
136 m_bmpSelectedOriginal
= sel
;
141 void wxBitmapButton::SetBitmapFocus(const wxBitmap
& focus
)
147 void wxBitmapButton::SetBitmapDisabled(const wxBitmap
& disabled
)
149 m_bmpDisabled
= disabled
;
150 m_bmpDisabledOriginal
= disabled
;
155 void wxBitmapButton::DoSetBitmap()
157 if (m_bmpNormalOriginal
.Ok())
160 Pixmap insensPixmap
= 0;
161 Pixmap armPixmap
= 0;
163 // Must re-make the bitmap to have its transparent areas drawn
164 // in the current widget background colour.
165 if (m_bmpNormalOriginal
.GetMask())
168 XtVaGetValues((Widget
) m_mainWidget
,
169 XmNbackground
, &backgroundPixel
,
173 col
.SetPixel(backgroundPixel
);
176 wxCreateMaskedBitmap(m_bmpNormalOriginal
, col
);
177 m_bmpNormal
= newBitmap
;
178 m_bitmapCache
.SetBitmap( m_bmpNormal
);
180 pixmap
= (Pixmap
) m_bmpNormal
.GetPixmap();
184 m_bitmapCache
.SetBitmap( m_bmpNormal
);
185 pixmap
= (Pixmap
) m_bitmapCache
.GetLabelPixmap(m_mainWidget
);
188 if (m_bmpDisabledOriginal
.Ok())
190 if (m_bmpDisabledOriginal
.GetMask())
193 XtVaGetValues((Widget
) m_mainWidget
,
194 XmNbackground
, &backgroundPixel
,
198 col
.SetPixel(backgroundPixel
);
201 wxCreateMaskedBitmap(m_bmpDisabledOriginal
, col
);
202 m_bmpDisabled
= newBitmap
;
204 insensPixmap
= (Pixmap
) m_bmpDisabled
.GetPixmap();
207 insensPixmap
= (Pixmap
) m_bitmapCache
.GetInsensPixmap(m_mainWidget
);
210 insensPixmap
= (Pixmap
) m_bitmapCache
.GetInsensPixmap(m_mainWidget
);
212 // Now make the bitmap representing the armed state
213 if (m_bmpSelectedOriginal
.Ok())
215 if (m_bmpSelectedOriginal
.GetMask())
218 XtVaGetValues((Widget
) m_mainWidget
,
219 XmNarmColor
, &backgroundPixel
,
223 col
.SetPixel(backgroundPixel
);
226 wxCreateMaskedBitmap(m_bmpSelectedOriginal
, col
);
227 m_bmpSelected
= newBitmap
;
229 armPixmap
= (Pixmap
) m_bmpSelected
.GetPixmap();
232 armPixmap
= (Pixmap
) m_bitmapCache
.GetArmPixmap(m_mainWidget
);
235 armPixmap
= (Pixmap
) m_bitmapCache
.GetArmPixmap(m_mainWidget
);
238 // <- the Get...Pixmap()-functions return the same pixmap!
239 if (insensPixmap
== pixmap
)
242 XCreateInsensitivePixmap(DisplayOfScreen(XtScreen((Widget
) m_mainWidget
)), pixmap
);
243 m_insensPixmap
= (WXPixmap
) insensPixmap
;
247 XtVaSetValues ((Widget
) m_mainWidget
,
248 XmNlabelPixmap
, pixmap
,
249 XmNlabelInsensitivePixmap
, insensPixmap
,
250 XmNarmPixmap
, armPixmap
,
251 XmNlabelType
, XmPIXMAP
,
256 // Null bitmap: must not use current pixmap
257 // since it is no longer valid.
258 XtVaSetValues ((Widget
) m_mainWidget
,
259 XmNlabelType
, XmSTRING
,
260 XmNlabelPixmap
, XmUNSPECIFIED_PIXMAP
,
261 XmNlabelInsensitivePixmap
, XmUNSPECIFIED_PIXMAP
,
262 XmNarmPixmap
, XmUNSPECIFIED_PIXMAP
,
267 void wxBitmapButton::ChangeBackgroundColour()
269 DoChangeBackgroundColour(m_mainWidget
, m_backgroundColour
, TRUE
);
271 // Must reset the bitmaps since the colours have changed.
275 wxSize
wxBitmapButton::DoGetBestSize() const
279 if (m_bmpNormal
.Ok())
281 int border
= (GetWindowStyle() & wxNO_BORDER
) ? 4 : 10;
282 ret
.x
= m_bmpNormal
.GetWidth()+border
;
283 ret
.y
= m_bmpNormal
.GetHeight()+border
;