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"
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 wxBitmapButtonBase::wxBitmapButtonBase()
52 wxBitmapButton::wxBitmapButton()
54 m_marginX
= m_marginY
= wxDEFAULT_BUTTON_MARGIN
;
55 m_insensPixmap
= (WXPixmap
) 0;
58 bool wxBitmapButton::Create(wxWindow
*parent
, wxWindowID id
,
59 const wxBitmap
& bitmap
,
61 const wxSize
& size
, long style
,
62 const wxValidator
& validator
,
65 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
68 m_bmpNormal
= m_bmpNormalOriginal
= bitmap
;
69 m_bmpSelected
= m_bmpSelectedOriginal
= bitmap
;
71 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
74 * Patch Note (important)
75 * There is no major reason to put a defaultButtonThickness here.
76 * Not requesting it give the ability to put wxButton with a spacing
77 * as small as requested. However, if some button become a DefaultButton,
78 * other buttons are no more aligned -- This is why we set
79 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
80 * in the ::SetDefaultButton method.
82 Widget buttonWidget
= XtVaCreateManagedWidget ("button",
84 // Gadget causes problems for default button operation.
86 xmPushButtonGadgetClass
, parentWidget
,
88 xmPushButtonWidgetClass
, parentWidget
,
90 // See comment for wxButton::SetDefault
91 // XmNdefaultButtonShadowThickness, 1,
92 XmNrecomputeSize
, False
,
95 m_mainWidget
= (WXWidget
) buttonWidget
;
99 ChangeBackgroundColour ();
103 XtAddCallback (buttonWidget
,
104 XmNactivateCallback
, (XtCallbackProc
) wxButtonCallback
,
107 wxSize best
= m_bmpNormal
.Ok() ? GetBestSize() : wxSize(30, 30);
108 if( size
.x
!= -1 ) best
.x
= size
.x
;
109 if( size
.y
!= -1 ) best
.y
= size
.y
;
111 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
112 pos
.x
, pos
.y
, best
.x
, best
.y
);
117 wxBitmapButton::~wxBitmapButton()
119 SetBitmapLabel(wxNullBitmap
);
122 XmDestroyPixmap (DefaultScreenOfDisplay ((Display
*) GetXDisplay()),
123 (Pixmap
) m_insensPixmap
);
126 void wxBitmapButton::SetBitmapLabel(const wxBitmap
& bitmap
)
128 m_bmpNormalOriginal
= bitmap
;
129 m_bmpNormal
= bitmap
;
134 void wxBitmapButton::SetBitmapSelected(const wxBitmap
& sel
)
137 m_bmpSelectedOriginal
= sel
;
142 void wxBitmapButton::SetBitmapFocus(const wxBitmap
& focus
)
148 void wxBitmapButton::SetBitmapDisabled(const wxBitmap
& disabled
)
150 m_bmpDisabled
= disabled
;
151 m_bmpDisabledOriginal
= disabled
;
156 void wxBitmapButton::DoSetBitmap()
158 if (m_bmpNormalOriginal
.Ok())
161 Pixmap insensPixmap
= 0;
162 Pixmap armPixmap
= 0;
164 // Must re-make the bitmap to have its transparent areas drawn
165 // in the current widget background colour.
166 if (m_bmpNormalOriginal
.GetMask())
169 XtVaGetValues((Widget
) m_mainWidget
,
170 XmNbackground
, &backgroundPixel
,
174 col
.SetPixel(backgroundPixel
);
177 wxCreateMaskedBitmap(m_bmpNormalOriginal
, col
);
178 m_bmpNormal
= newBitmap
;
179 m_bitmapCache
.SetBitmap( m_bmpNormal
);
181 pixmap
= (Pixmap
) m_bmpNormal
.GetDrawable();
185 m_bitmapCache
.SetBitmap( m_bmpNormal
);
186 pixmap
= (Pixmap
) m_bitmapCache
.GetLabelPixmap(m_mainWidget
);
189 if (m_bmpDisabledOriginal
.Ok())
191 if (m_bmpDisabledOriginal
.GetMask())
194 XtVaGetValues((Widget
) m_mainWidget
,
195 XmNbackground
, &backgroundPixel
,
199 col
.SetPixel(backgroundPixel
);
202 wxCreateMaskedBitmap(m_bmpDisabledOriginal
, col
);
203 m_bmpDisabled
= newBitmap
;
205 insensPixmap
= (Pixmap
) m_bmpDisabled
.GetDrawable();
208 insensPixmap
= (Pixmap
) m_bitmapCache
.GetInsensPixmap(m_mainWidget
);
211 insensPixmap
= (Pixmap
) m_bitmapCache
.GetInsensPixmap(m_mainWidget
);
213 // Now make the bitmap representing the armed state
214 if (m_bmpSelectedOriginal
.Ok())
216 if (m_bmpSelectedOriginal
.GetMask())
219 XtVaGetValues((Widget
) m_mainWidget
,
220 XmNarmColor
, &backgroundPixel
,
224 col
.SetPixel(backgroundPixel
);
227 wxCreateMaskedBitmap(m_bmpSelectedOriginal
, col
);
228 m_bmpSelected
= newBitmap
;
230 armPixmap
= (Pixmap
) m_bmpSelected
.GetDrawable();
233 armPixmap
= (Pixmap
) m_bitmapCache
.GetArmPixmap(m_mainWidget
);
236 armPixmap
= (Pixmap
) m_bitmapCache
.GetArmPixmap(m_mainWidget
);
238 XtVaSetValues ((Widget
) m_mainWidget
,
239 XmNlabelPixmap
, pixmap
,
240 XmNlabelInsensitivePixmap
, insensPixmap
,
241 XmNarmPixmap
, armPixmap
,
242 XmNlabelType
, XmPIXMAP
,
247 // Null bitmap: must not use current pixmap
248 // since it is no longer valid.
249 XtVaSetValues ((Widget
) m_mainWidget
,
250 XmNlabelType
, XmSTRING
,
251 XmNlabelPixmap
, XmUNSPECIFIED_PIXMAP
,
252 XmNlabelInsensitivePixmap
, XmUNSPECIFIED_PIXMAP
,
253 XmNarmPixmap
, XmUNSPECIFIED_PIXMAP
,
258 void wxBitmapButton::ChangeBackgroundColour()
260 wxDoChangeBackgroundColour(m_mainWidget
, m_backgroundColour
, TRUE
);
262 // Must reset the bitmaps since the colours have changed.
266 wxSize
wxBitmapButton::DoGetBestSize() const
270 if (m_bmpNormal
.Ok())
272 int border
= (GetWindowStyle() & wxNO_BORDER
) ? 4 : 10;
273 ret
.x
= m_bmpNormal
.GetWidth()+border
;
274 ret
.y
= m_bmpNormal
.GetHeight()+border
;