1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxBitmapButton
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "bmpbuttn.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
20 #define XtScreen XTSCREEN
25 #include "wx/bmpbuttn.h"
28 #pragma message disable nosimpint
30 #include <Xm/PushBG.h>
33 #pragma message enable nosimpint
36 #include "wx/motif/private.h"
38 // Implemented in button.cpp
39 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer ptr
);
41 // Pixmap XCreateInsensitivePixmap( Display *display, Pixmap pixmap );
43 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton
, wxButton
)
45 wxBitmapButton::wxBitmapButton()
47 m_marginX
= m_marginY
= wxDEFAULT_BUTTON_MARGIN
;
48 m_insensPixmap
= (WXPixmap
) 0;
51 bool wxBitmapButton::Create(wxWindow
*parent
, wxWindowID id
,
52 const wxBitmap
& bitmap
,
54 const wxSize
& size
, long style
,
55 const wxValidator
& validator
,
58 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
61 m_bmpNormal
= m_bmpNormalOriginal
= bitmap
;
62 m_bmpSelected
= m_bmpSelectedOriginal
= bitmap
;
64 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
67 * Patch Note (important)
68 * There is no major reason to put a defaultButtonThickness here.
69 * Not requesting it give the ability to put wxButton with a spacing
70 * as small as requested. However, if some button become a DefaultButton,
71 * other buttons are no more aligned -- This is why we set
72 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
73 * in the ::SetDefaultButton method.
75 Widget buttonWidget
= XtVaCreateManagedWidget ("button",
77 // Gadget causes problems for default button operation.
79 xmPushButtonGadgetClass
, parentWidget
,
81 xmPushButtonWidgetClass
, parentWidget
,
83 // See comment for wxButton::SetDefault
84 // XmNdefaultButtonShadowThickness, 1,
85 XmNrecomputeSize
, False
,
88 m_mainWidget
= (WXWidget
) buttonWidget
;
92 ChangeBackgroundColour ();
96 XtAddCallback (buttonWidget
,
97 XmNactivateCallback
, (XtCallbackProc
) wxButtonCallback
,
100 wxSize best
= m_bmpNormal
.Ok() ? GetBestSize() : wxSize(30, 30);
101 if( size
.x
!= -1 ) best
.x
= size
.x
;
102 if( size
.y
!= -1 ) best
.y
= size
.y
;
104 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
105 pos
.x
, pos
.y
, best
.x
, best
.y
);
110 wxBitmapButton::~wxBitmapButton()
112 SetBitmapLabel(wxNullBitmap
);
115 XmDestroyPixmap (DefaultScreenOfDisplay ((Display
*) GetXDisplay()),
116 (Pixmap
) m_insensPixmap
);
119 void wxBitmapButton::SetBitmapLabel(const wxBitmap
& bitmap
)
121 m_bmpNormalOriginal
= bitmap
;
122 m_bmpNormal
= bitmap
;
127 void wxBitmapButton::SetBitmapSelected(const wxBitmap
& sel
)
130 m_bmpSelectedOriginal
= sel
;
135 void wxBitmapButton::SetBitmapFocus(const wxBitmap
& focus
)
141 void wxBitmapButton::SetBitmapDisabled(const wxBitmap
& disabled
)
143 m_bmpDisabled
= disabled
;
144 m_bmpDisabledOriginal
= disabled
;
149 void wxBitmapButton::DoSetBitmap()
151 if (m_bmpNormalOriginal
.Ok())
154 Pixmap insensPixmap
= 0;
155 Pixmap armPixmap
= 0;
157 // Must re-make the bitmap to have its transparent areas drawn
158 // in the current widget background colour.
159 if (m_bmpNormalOriginal
.GetMask())
162 XtVaGetValues((Widget
) m_mainWidget
,
163 XmNbackground
, &backgroundPixel
,
167 col
.SetPixel(backgroundPixel
);
170 wxCreateMaskedBitmap(m_bmpNormalOriginal
, col
);
171 m_bmpNormal
= newBitmap
;
172 m_bitmapCache
.SetBitmap( m_bmpNormal
);
174 pixmap
= (Pixmap
) m_bmpNormal
.GetDrawable();
178 m_bitmapCache
.SetBitmap( m_bmpNormal
);
179 pixmap
= (Pixmap
) m_bitmapCache
.GetLabelPixmap(m_mainWidget
);
182 if (m_bmpDisabledOriginal
.Ok())
184 if (m_bmpDisabledOriginal
.GetMask())
187 XtVaGetValues((Widget
) m_mainWidget
,
188 XmNbackground
, &backgroundPixel
,
192 col
.SetPixel(backgroundPixel
);
195 wxCreateMaskedBitmap(m_bmpDisabledOriginal
, col
);
196 m_bmpDisabled
= newBitmap
;
198 insensPixmap
= (Pixmap
) m_bmpDisabled
.GetDrawable();
201 insensPixmap
= (Pixmap
) m_bitmapCache
.GetInsensPixmap(m_mainWidget
);
204 insensPixmap
= (Pixmap
) m_bitmapCache
.GetInsensPixmap(m_mainWidget
);
206 // Now make the bitmap representing the armed state
207 if (m_bmpSelectedOriginal
.Ok())
209 if (m_bmpSelectedOriginal
.GetMask())
212 XtVaGetValues((Widget
) m_mainWidget
,
213 XmNarmColor
, &backgroundPixel
,
217 col
.SetPixel(backgroundPixel
);
220 wxCreateMaskedBitmap(m_bmpSelectedOriginal
, col
);
221 m_bmpSelected
= newBitmap
;
223 armPixmap
= (Pixmap
) m_bmpSelected
.GetDrawable();
226 armPixmap
= (Pixmap
) m_bitmapCache
.GetArmPixmap(m_mainWidget
);
229 armPixmap
= (Pixmap
) m_bitmapCache
.GetArmPixmap(m_mainWidget
);
231 XtVaSetValues ((Widget
) m_mainWidget
,
232 XmNlabelPixmap
, pixmap
,
233 XmNlabelInsensitivePixmap
, insensPixmap
,
234 XmNarmPixmap
, armPixmap
,
235 XmNlabelType
, XmPIXMAP
,
240 // Null bitmap: must not use current pixmap
241 // since it is no longer valid.
242 XtVaSetValues ((Widget
) m_mainWidget
,
243 XmNlabelType
, XmSTRING
,
244 XmNlabelPixmap
, XmUNSPECIFIED_PIXMAP
,
245 XmNlabelInsensitivePixmap
, XmUNSPECIFIED_PIXMAP
,
246 XmNarmPixmap
, XmUNSPECIFIED_PIXMAP
,
251 void wxBitmapButton::ChangeBackgroundColour()
253 wxDoChangeBackgroundColour(m_mainWidget
, m_backgroundColour
, true);
255 // Must reset the bitmaps since the colours have changed.
259 wxSize
wxBitmapButton::DoGetBestSize() const
263 if (m_bmpNormal
.Ok())
265 int border
= (GetWindowStyle() & wxNO_BORDER
) ? 4 : 10;
266 ret
.x
= m_bmpNormal
.GetWidth()+border
;
267 ret
.y
= m_bmpNormal
.GetHeight()+border
;