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"
16 #define XtScreen XTSCREEN
19 #include "wx/bmpbuttn.h"
22 #pragma message disable nosimpint
24 #include <Xm/PushBG.h>
27 #pragma message enable nosimpint
30 #include "wx/motif/private.h"
32 // Implemented in button.cpp
33 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer ptr
);
35 // Pixmap XCreateInsensitivePixmap( Display *display, Pixmap pixmap );
37 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton
, wxButton
)
39 wxBitmapButton::wxBitmapButton()
41 m_marginX
= m_marginY
= wxDEFAULT_BUTTON_MARGIN
;
42 m_insensPixmap
= (WXPixmap
) 0;
45 bool wxBitmapButton::Create(wxWindow
*parent
, wxWindowID id
,
46 const wxBitmap
& bitmap
,
48 const wxSize
& size
, long style
,
49 const wxValidator
& validator
,
52 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
55 m_bmpNormal
= m_bmpNormalOriginal
= bitmap
;
56 m_bmpSelected
= m_bmpSelectedOriginal
= bitmap
;
58 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
61 * Patch Note (important)
62 * There is no major reason to put a defaultButtonThickness here.
63 * Not requesting it give the ability to put wxButton with a spacing
64 * as small as requested. However, if some button become a DefaultButton,
65 * other buttons are no more aligned -- This is why we set
66 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
67 * in the ::SetDefaultButton method.
69 Widget buttonWidget
= XtVaCreateManagedWidget ("button",
71 // Gadget causes problems for default button operation.
73 xmPushButtonGadgetClass
, parentWidget
,
75 xmPushButtonWidgetClass
, parentWidget
,
77 // See comment for wxButton::SetDefault
78 // XmNdefaultButtonShadowThickness, 1,
79 XmNrecomputeSize
, False
,
82 m_mainWidget
= (WXWidget
) buttonWidget
;
86 ChangeBackgroundColour ();
90 XtAddCallback (buttonWidget
,
91 XmNactivateCallback
, (XtCallbackProc
) wxButtonCallback
,
94 wxSize best
= m_bmpNormal
.Ok() ? GetBestSize() : wxSize(30, 30);
95 if( size
.x
!= -1 ) best
.x
= size
.x
;
96 if( size
.y
!= -1 ) best
.y
= size
.y
;
98 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
99 pos
.x
, pos
.y
, best
.x
, best
.y
);
104 wxBitmapButton::~wxBitmapButton()
106 SetBitmapLabel(wxNullBitmap
);
109 XmDestroyPixmap (DefaultScreenOfDisplay ((Display
*) GetXDisplay()),
110 (Pixmap
) m_insensPixmap
);
113 void wxBitmapButton::SetBitmapLabel(const wxBitmap
& bitmap
)
115 m_bmpNormalOriginal
= bitmap
;
116 m_bmpNormal
= bitmap
;
121 void wxBitmapButton::SetBitmapSelected(const wxBitmap
& sel
)
124 m_bmpSelectedOriginal
= sel
;
129 void wxBitmapButton::SetBitmapFocus(const wxBitmap
& focus
)
135 void wxBitmapButton::SetBitmapDisabled(const wxBitmap
& disabled
)
137 m_bmpDisabled
= disabled
;
138 m_bmpDisabledOriginal
= disabled
;
143 void wxBitmapButton::DoSetBitmap()
145 if (m_bmpNormalOriginal
.Ok())
148 Pixmap insensPixmap
= 0;
149 Pixmap armPixmap
= 0;
151 // Must re-make the bitmap to have its transparent areas drawn
152 // in the current widget background colour.
153 if (m_bmpNormalOriginal
.GetMask())
155 WXPixel backgroundPixel
;
156 XtVaGetValues((Widget
) m_mainWidget
,
157 XmNbackground
, &backgroundPixel
,
161 col
.SetPixel(backgroundPixel
);
164 wxCreateMaskedBitmap(m_bmpNormalOriginal
, col
);
165 m_bmpNormal
= newBitmap
;
166 m_bitmapCache
.SetBitmap( m_bmpNormal
);
168 pixmap
= (Pixmap
) m_bmpNormal
.GetDrawable();
172 m_bitmapCache
.SetBitmap( m_bmpNormal
);
173 pixmap
= (Pixmap
) m_bitmapCache
.GetLabelPixmap(m_mainWidget
);
176 if (m_bmpDisabledOriginal
.Ok())
178 if (m_bmpDisabledOriginal
.GetMask())
180 WXPixel backgroundPixel
;
181 XtVaGetValues((Widget
) m_mainWidget
,
182 XmNbackground
, &backgroundPixel
,
186 col
.SetPixel(backgroundPixel
);
189 wxCreateMaskedBitmap(m_bmpDisabledOriginal
, col
);
190 m_bmpDisabled
= newBitmap
;
192 insensPixmap
= (Pixmap
) m_bmpDisabled
.GetDrawable();
195 insensPixmap
= (Pixmap
) m_bitmapCache
.GetInsensPixmap(m_mainWidget
);
198 insensPixmap
= (Pixmap
) m_bitmapCache
.GetInsensPixmap(m_mainWidget
);
200 // Now make the bitmap representing the armed state
201 if (m_bmpSelectedOriginal
.Ok())
203 if (m_bmpSelectedOriginal
.GetMask())
205 WXPixel backgroundPixel
;
206 XtVaGetValues((Widget
) m_mainWidget
,
207 XmNarmColor
, &backgroundPixel
,
211 col
.SetPixel(backgroundPixel
);
214 wxCreateMaskedBitmap(m_bmpSelectedOriginal
, col
);
215 m_bmpSelected
= newBitmap
;
217 armPixmap
= (Pixmap
) m_bmpSelected
.GetDrawable();
220 armPixmap
= (Pixmap
) m_bitmapCache
.GetArmPixmap(m_mainWidget
);
223 armPixmap
= (Pixmap
) m_bitmapCache
.GetArmPixmap(m_mainWidget
);
225 XtVaSetValues ((Widget
) m_mainWidget
,
226 XmNlabelPixmap
, pixmap
,
227 XmNlabelInsensitivePixmap
, insensPixmap
,
228 XmNarmPixmap
, armPixmap
,
229 XmNlabelType
, XmPIXMAP
,
234 // Null bitmap: must not use current pixmap
235 // since it is no longer valid.
236 XtVaSetValues ((Widget
) m_mainWidget
,
237 XmNlabelType
, XmSTRING
,
238 XmNlabelPixmap
, XmUNSPECIFIED_PIXMAP
,
239 XmNlabelInsensitivePixmap
, XmUNSPECIFIED_PIXMAP
,
240 XmNarmPixmap
, XmUNSPECIFIED_PIXMAP
,
245 void wxBitmapButton::ChangeBackgroundColour()
247 wxDoChangeBackgroundColour(m_mainWidget
, m_backgroundColour
, true);
249 // Must reset the bitmaps since the colours have changed.
253 wxSize
wxBitmapButton::DoGetBestSize() const
257 if (m_bmpNormal
.Ok())
259 int border
= (GetWindowStyle() & wxNO_BORDER
) ? 4 : 10;
260 ret
.x
= m_bmpNormal
.GetWidth()+border
;
261 ret
.y
= m_bmpNormal
.GetHeight()+border
;