1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/bmpbuttn.cpp
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"
15 #include "wx/bmpbuttn.h"
18 #pragma message disable nosimpint
20 #include <Xm/PushBG.h>
23 #pragma message enable nosimpint
26 #include "wx/motif/private.h"
28 // Implemented in button.cpp
29 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer ptr
);
31 // Pixmap XCreateInsensitivePixmap( Display *display, Pixmap pixmap );
33 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton
, wxButton
)
35 wxBitmapButton::wxBitmapButton()
37 m_marginX
= m_marginY
= wxDEFAULT_BUTTON_MARGIN
;
38 m_insensPixmap
= (WXPixmap
) 0;
41 bool wxBitmapButton::Create(wxWindow
*parent
, wxWindowID id
,
42 const wxBitmap
& bitmap
,
44 const wxSize
& size
, long style
,
45 const wxValidator
& validator
,
48 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
52 m_bmpNormal
= m_bmpNormalOriginal
= bitmap
;
53 m_bmpSelected
= m_bmpSelectedOriginal
= bitmap
;
55 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
58 * Patch Note (important)
59 * There is no major reason to put a defaultButtonThickness here.
60 * Not requesting it give the ability to put wxButton with a spacing
61 * as small as requested. However, if some button become a DefaultButton,
62 * other buttons are no more aligned -- This is why we set
63 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
64 * in the ::SetDefaultButton method.
66 Widget buttonWidget
= XtVaCreateManagedWidget ("button",
68 // Gadget causes problems for default button operation.
70 xmPushButtonGadgetClass
, parentWidget
,
72 xmPushButtonWidgetClass
, parentWidget
,
74 // See comment for wxButton::SetDefault
75 // XmNdefaultButtonShadowThickness, 1,
76 XmNrecomputeSize
, False
,
79 m_mainWidget
= (WXWidget
) buttonWidget
;
81 XtAddCallback (buttonWidget
,
82 XmNactivateCallback
, (XtCallbackProc
) wxButtonCallback
,
85 wxSize best
= m_bmpNormal
.Ok() ? GetBestSize() : wxSize(30, 30);
86 if( size
.x
!= -1 ) best
.x
= size
.x
;
87 if( size
.y
!= -1 ) best
.y
= size
.y
;
92 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
93 pos
.x
, pos
.y
, best
.x
, best
.y
);
98 wxBitmapButton::~wxBitmapButton()
100 SetBitmapLabel(wxNullBitmap
);
103 XmDestroyPixmap (DefaultScreenOfDisplay ((Display
*) GetXDisplay()),
104 (Pixmap
) m_insensPixmap
);
107 void wxBitmapButton::SetBitmapLabel(const wxBitmap
& bitmap
)
109 m_bmpNormalOriginal
= bitmap
;
110 m_bmpNormal
= bitmap
;
115 void wxBitmapButton::SetBitmapSelected(const wxBitmap
& sel
)
118 m_bmpSelectedOriginal
= sel
;
123 void wxBitmapButton::SetBitmapFocus(const wxBitmap
& focus
)
129 void wxBitmapButton::SetBitmapDisabled(const wxBitmap
& disabled
)
131 m_bmpDisabled
= disabled
;
132 m_bmpDisabledOriginal
= disabled
;
137 void wxBitmapButton::DoSetBitmap()
139 if (m_bmpNormalOriginal
.Ok())
142 Pixmap insensPixmap
= 0;
143 Pixmap armPixmap
= 0;
145 // Must re-make the bitmap to have its transparent areas drawn
146 // in the current widget background colour.
147 if (m_bmpNormalOriginal
.GetMask())
149 WXPixel backgroundPixel
;
150 XtVaGetValues((Widget
) m_mainWidget
,
151 XmNbackground
, &backgroundPixel
,
155 col
.SetPixel(backgroundPixel
);
158 wxCreateMaskedBitmap(m_bmpNormalOriginal
, col
);
159 m_bmpNormal
= newBitmap
;
160 m_bitmapCache
.SetBitmap( m_bmpNormal
);
162 pixmap
= (Pixmap
) m_bmpNormal
.GetDrawable();
166 m_bitmapCache
.SetBitmap( m_bmpNormal
);
167 pixmap
= (Pixmap
) m_bitmapCache
.GetLabelPixmap(m_mainWidget
);
170 if (m_bmpDisabledOriginal
.Ok())
172 if (m_bmpDisabledOriginal
.GetMask())
174 WXPixel backgroundPixel
;
175 XtVaGetValues((Widget
) m_mainWidget
,
176 XmNbackground
, &backgroundPixel
,
180 col
.SetPixel(backgroundPixel
);
183 wxCreateMaskedBitmap(m_bmpDisabledOriginal
, col
);
184 m_bmpDisabled
= newBitmap
;
186 insensPixmap
= (Pixmap
) m_bmpDisabled
.GetDrawable();
189 insensPixmap
= (Pixmap
) m_bitmapCache
.GetInsensPixmap(m_mainWidget
);
192 insensPixmap
= (Pixmap
) m_bitmapCache
.GetInsensPixmap(m_mainWidget
);
194 // Now make the bitmap representing the armed state
195 if (m_bmpSelectedOriginal
.Ok())
197 if (m_bmpSelectedOriginal
.GetMask())
199 WXPixel backgroundPixel
;
200 XtVaGetValues((Widget
) m_mainWidget
,
201 XmNarmColor
, &backgroundPixel
,
205 col
.SetPixel(backgroundPixel
);
208 wxCreateMaskedBitmap(m_bmpSelectedOriginal
, col
);
209 m_bmpSelected
= newBitmap
;
211 armPixmap
= (Pixmap
) m_bmpSelected
.GetDrawable();
214 armPixmap
= (Pixmap
) m_bitmapCache
.GetArmPixmap(m_mainWidget
);
217 armPixmap
= (Pixmap
) m_bitmapCache
.GetArmPixmap(m_mainWidget
);
219 XtVaSetValues ((Widget
) m_mainWidget
,
220 XmNlabelPixmap
, pixmap
,
221 XmNlabelInsensitivePixmap
, insensPixmap
,
222 XmNarmPixmap
, armPixmap
,
223 XmNlabelType
, XmPIXMAP
,
228 // Null bitmap: must not use current pixmap
229 // since it is no longer valid.
230 XtVaSetValues ((Widget
) m_mainWidget
,
231 XmNlabelType
, XmSTRING
,
232 XmNlabelPixmap
, XmUNSPECIFIED_PIXMAP
,
233 XmNlabelInsensitivePixmap
, XmUNSPECIFIED_PIXMAP
,
234 XmNarmPixmap
, XmUNSPECIFIED_PIXMAP
,
239 void wxBitmapButton::ChangeBackgroundColour()
241 wxDoChangeBackgroundColour(m_mainWidget
, m_backgroundColour
, true);
243 // Must reset the bitmaps since the colours have changed.
247 wxSize
wxBitmapButton::DoGetBestSize() const
251 if (m_bmpNormal
.Ok())
253 int border
= (GetWindowStyle() & wxNO_BORDER
) ? 4 : 10;
254 ret
.x
= m_bmpNormal
.GetWidth()+border
;
255 ret
.y
= m_bmpNormal
.GetHeight()+border
;