1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxBitmapButton
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
16 #define XtScreen XTSCREEN
21 #include "wx/bmpbuttn.h"
24 #pragma message disable nosimpint
26 #include <Xm/PushBG.h>
29 #pragma message enable nosimpint
32 #include "wx/motif/private.h"
34 // Implemented in button.cpp
35 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer ptr
);
37 // Pixmap XCreateInsensitivePixmap( Display *display, Pixmap pixmap );
39 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton
, wxButton
)
41 wxBitmapButton::wxBitmapButton()
43 m_marginX
= m_marginY
= wxDEFAULT_BUTTON_MARGIN
;
44 m_insensPixmap
= (WXPixmap
) 0;
47 bool wxBitmapButton::Create(wxWindow
*parent
, wxWindowID id
,
48 const wxBitmap
& bitmap
,
50 const wxSize
& size
, long style
,
51 const wxValidator
& validator
,
54 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
57 m_bmpNormal
= m_bmpNormalOriginal
= bitmap
;
58 m_bmpSelected
= m_bmpSelectedOriginal
= bitmap
;
60 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
63 * Patch Note (important)
64 * There is no major reason to put a defaultButtonThickness here.
65 * Not requesting it give the ability to put wxButton with a spacing
66 * as small as requested. However, if some button become a DefaultButton,
67 * other buttons are no more aligned -- This is why we set
68 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
69 * in the ::SetDefaultButton method.
71 Widget buttonWidget
= XtVaCreateManagedWidget ("button",
73 // Gadget causes problems for default button operation.
75 xmPushButtonGadgetClass
, parentWidget
,
77 xmPushButtonWidgetClass
, parentWidget
,
79 // See comment for wxButton::SetDefault
80 // XmNdefaultButtonShadowThickness, 1,
81 XmNrecomputeSize
, False
,
84 m_mainWidget
= (WXWidget
) buttonWidget
;
88 ChangeBackgroundColour ();
92 XtAddCallback (buttonWidget
,
93 XmNactivateCallback
, (XtCallbackProc
) wxButtonCallback
,
96 wxSize best
= m_bmpNormal
.Ok() ? GetBestSize() : wxSize(30, 30);
97 if( size
.x
!= -1 ) best
.x
= size
.x
;
98 if( size
.y
!= -1 ) best
.y
= size
.y
;
100 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
101 pos
.x
, pos
.y
, best
.x
, best
.y
);
106 wxBitmapButton::~wxBitmapButton()
108 SetBitmapLabel(wxNullBitmap
);
111 XmDestroyPixmap (DefaultScreenOfDisplay ((Display
*) GetXDisplay()),
112 (Pixmap
) m_insensPixmap
);
115 void wxBitmapButton::SetBitmapLabel(const wxBitmap
& bitmap
)
117 m_bmpNormalOriginal
= bitmap
;
118 m_bmpNormal
= bitmap
;
123 void wxBitmapButton::SetBitmapSelected(const wxBitmap
& sel
)
126 m_bmpSelectedOriginal
= sel
;
131 void wxBitmapButton::SetBitmapFocus(const wxBitmap
& focus
)
137 void wxBitmapButton::SetBitmapDisabled(const wxBitmap
& disabled
)
139 m_bmpDisabled
= disabled
;
140 m_bmpDisabledOriginal
= disabled
;
145 void wxBitmapButton::DoSetBitmap()
147 if (m_bmpNormalOriginal
.Ok())
150 Pixmap insensPixmap
= 0;
151 Pixmap armPixmap
= 0;
153 // Must re-make the bitmap to have its transparent areas drawn
154 // in the current widget background colour.
155 if (m_bmpNormalOriginal
.GetMask())
158 XtVaGetValues((Widget
) m_mainWidget
,
159 XmNbackground
, &backgroundPixel
,
163 col
.SetPixel(backgroundPixel
);
166 wxCreateMaskedBitmap(m_bmpNormalOriginal
, col
);
167 m_bmpNormal
= newBitmap
;
168 m_bitmapCache
.SetBitmap( m_bmpNormal
);
170 pixmap
= (Pixmap
) m_bmpNormal
.GetDrawable();
174 m_bitmapCache
.SetBitmap( m_bmpNormal
);
175 pixmap
= (Pixmap
) m_bitmapCache
.GetLabelPixmap(m_mainWidget
);
178 if (m_bmpDisabledOriginal
.Ok())
180 if (m_bmpDisabledOriginal
.GetMask())
183 XtVaGetValues((Widget
) m_mainWidget
,
184 XmNbackground
, &backgroundPixel
,
188 col
.SetPixel(backgroundPixel
);
191 wxCreateMaskedBitmap(m_bmpDisabledOriginal
, col
);
192 m_bmpDisabled
= newBitmap
;
194 insensPixmap
= (Pixmap
) m_bmpDisabled
.GetDrawable();
197 insensPixmap
= (Pixmap
) m_bitmapCache
.GetInsensPixmap(m_mainWidget
);
200 insensPixmap
= (Pixmap
) m_bitmapCache
.GetInsensPixmap(m_mainWidget
);
202 // Now make the bitmap representing the armed state
203 if (m_bmpSelectedOriginal
.Ok())
205 if (m_bmpSelectedOriginal
.GetMask())
208 XtVaGetValues((Widget
) m_mainWidget
,
209 XmNarmColor
, &backgroundPixel
,
213 col
.SetPixel(backgroundPixel
);
216 wxCreateMaskedBitmap(m_bmpSelectedOriginal
, col
);
217 m_bmpSelected
= newBitmap
;
219 armPixmap
= (Pixmap
) m_bmpSelected
.GetDrawable();
222 armPixmap
= (Pixmap
) m_bitmapCache
.GetArmPixmap(m_mainWidget
);
225 armPixmap
= (Pixmap
) m_bitmapCache
.GetArmPixmap(m_mainWidget
);
227 XtVaSetValues ((Widget
) m_mainWidget
,
228 XmNlabelPixmap
, pixmap
,
229 XmNlabelInsensitivePixmap
, insensPixmap
,
230 XmNarmPixmap
, armPixmap
,
231 XmNlabelType
, XmPIXMAP
,
236 // Null bitmap: must not use current pixmap
237 // since it is no longer valid.
238 XtVaSetValues ((Widget
) m_mainWidget
,
239 XmNlabelType
, XmSTRING
,
240 XmNlabelPixmap
, XmUNSPECIFIED_PIXMAP
,
241 XmNlabelInsensitivePixmap
, XmUNSPECIFIED_PIXMAP
,
242 XmNarmPixmap
, XmUNSPECIFIED_PIXMAP
,
247 void wxBitmapButton::ChangeBackgroundColour()
249 wxDoChangeBackgroundColour(m_mainWidget
, m_backgroundColour
, true);
251 // Must reset the bitmaps since the colours have changed.
255 wxSize
wxBitmapButton::DoGetBestSize() const
259 if (m_bmpNormal
.Ok())
261 int border
= (GetWindowStyle() & wxNO_BORDER
) ? 4 : 10;
262 ret
.x
= m_bmpNormal
.GetWidth()+border
;
263 ret
.y
= m_bmpNormal
.GetHeight()+border
;