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 wxBitmapButton::wxBitmapButton()
44 m_marginX
= m_marginY
= wxDEFAULT_BUTTON_MARGIN
;
45 m_insensPixmap
= (WXPixmap
) 0;
48 bool wxBitmapButton::Create(wxWindow
*parent
, wxWindowID id
,
49 const wxBitmap
& bitmap
,
51 const wxSize
& size
, long style
,
52 const wxValidator
& validator
,
55 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
58 m_bmpNormal
= m_bmpNormalOriginal
= bitmap
;
59 m_bmpSelected
= m_bmpSelectedOriginal
= bitmap
;
61 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
64 * Patch Note (important)
65 * There is no major reason to put a defaultButtonThickness here.
66 * Not requesting it give the ability to put wxButton with a spacing
67 * as small as requested. However, if some button become a DefaultButton,
68 * other buttons are no more aligned -- This is why we set
69 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
70 * in the ::SetDefaultButton method.
72 Widget buttonWidget
= XtVaCreateManagedWidget ("button",
74 // Gadget causes problems for default button operation.
76 xmPushButtonGadgetClass
, parentWidget
,
78 xmPushButtonWidgetClass
, parentWidget
,
80 // See comment for wxButton::SetDefault
81 // XmNdefaultButtonShadowThickness, 1,
82 XmNrecomputeSize
, False
,
85 m_mainWidget
= (WXWidget
) buttonWidget
;
89 ChangeBackgroundColour ();
93 XtAddCallback (buttonWidget
,
94 XmNactivateCallback
, (XtCallbackProc
) wxButtonCallback
,
97 wxSize best
= m_bmpNormal
.Ok() ? GetBestSize() : wxSize(30, 30);
98 if( size
.x
!= -1 ) best
.x
= size
.x
;
99 if( size
.y
!= -1 ) best
.y
= size
.y
;
101 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
102 pos
.x
, pos
.y
, best
.x
, best
.y
);
107 wxBitmapButton::~wxBitmapButton()
109 SetBitmapLabel(wxNullBitmap
);
112 XmDestroyPixmap (DefaultScreenOfDisplay ((Display
*) GetXDisplay()),
113 (Pixmap
) m_insensPixmap
);
116 void wxBitmapButton::SetBitmapLabel(const wxBitmap
& bitmap
)
118 m_bmpNormalOriginal
= bitmap
;
119 m_bmpNormal
= bitmap
;
124 void wxBitmapButton::SetBitmapSelected(const wxBitmap
& sel
)
127 m_bmpSelectedOriginal
= sel
;
132 void wxBitmapButton::SetBitmapFocus(const wxBitmap
& focus
)
138 void wxBitmapButton::SetBitmapDisabled(const wxBitmap
& disabled
)
140 m_bmpDisabled
= disabled
;
141 m_bmpDisabledOriginal
= disabled
;
146 void wxBitmapButton::DoSetBitmap()
148 if (m_bmpNormalOriginal
.Ok())
151 Pixmap insensPixmap
= 0;
152 Pixmap armPixmap
= 0;
154 // Must re-make the bitmap to have its transparent areas drawn
155 // in the current widget background colour.
156 if (m_bmpNormalOriginal
.GetMask())
159 XtVaGetValues((Widget
) m_mainWidget
,
160 XmNbackground
, &backgroundPixel
,
164 col
.SetPixel(backgroundPixel
);
167 wxCreateMaskedBitmap(m_bmpNormalOriginal
, col
);
168 m_bmpNormal
= newBitmap
;
169 m_bitmapCache
.SetBitmap( m_bmpNormal
);
171 pixmap
= (Pixmap
) m_bmpNormal
.GetDrawable();
175 m_bitmapCache
.SetBitmap( m_bmpNormal
);
176 pixmap
= (Pixmap
) m_bitmapCache
.GetLabelPixmap(m_mainWidget
);
179 if (m_bmpDisabledOriginal
.Ok())
181 if (m_bmpDisabledOriginal
.GetMask())
184 XtVaGetValues((Widget
) m_mainWidget
,
185 XmNbackground
, &backgroundPixel
,
189 col
.SetPixel(backgroundPixel
);
192 wxCreateMaskedBitmap(m_bmpDisabledOriginal
, col
);
193 m_bmpDisabled
= newBitmap
;
195 insensPixmap
= (Pixmap
) m_bmpDisabled
.GetDrawable();
198 insensPixmap
= (Pixmap
) m_bitmapCache
.GetInsensPixmap(m_mainWidget
);
201 insensPixmap
= (Pixmap
) m_bitmapCache
.GetInsensPixmap(m_mainWidget
);
203 // Now make the bitmap representing the armed state
204 if (m_bmpSelectedOriginal
.Ok())
206 if (m_bmpSelectedOriginal
.GetMask())
209 XtVaGetValues((Widget
) m_mainWidget
,
210 XmNarmColor
, &backgroundPixel
,
214 col
.SetPixel(backgroundPixel
);
217 wxCreateMaskedBitmap(m_bmpSelectedOriginal
, col
);
218 m_bmpSelected
= newBitmap
;
220 armPixmap
= (Pixmap
) m_bmpSelected
.GetDrawable();
223 armPixmap
= (Pixmap
) m_bitmapCache
.GetArmPixmap(m_mainWidget
);
226 armPixmap
= (Pixmap
) m_bitmapCache
.GetArmPixmap(m_mainWidget
);
228 XtVaSetValues ((Widget
) m_mainWidget
,
229 XmNlabelPixmap
, pixmap
,
230 XmNlabelInsensitivePixmap
, insensPixmap
,
231 XmNarmPixmap
, armPixmap
,
232 XmNlabelType
, XmPIXMAP
,
237 // Null bitmap: must not use current pixmap
238 // since it is no longer valid.
239 XtVaSetValues ((Widget
) m_mainWidget
,
240 XmNlabelType
, XmSTRING
,
241 XmNlabelPixmap
, XmUNSPECIFIED_PIXMAP
,
242 XmNlabelInsensitivePixmap
, XmUNSPECIFIED_PIXMAP
,
243 XmNarmPixmap
, XmUNSPECIFIED_PIXMAP
,
248 void wxBitmapButton::ChangeBackgroundColour()
250 wxDoChangeBackgroundColour(m_mainWidget
, m_backgroundColour
, TRUE
);
252 // Must reset the bitmaps since the colours have changed.
256 wxSize
wxBitmapButton::DoGetBestSize() const
260 if (m_bmpNormal
.Ok())
262 int border
= (GetWindowStyle() & wxNO_BORDER
) ? 4 : 10;
263 ret
.x
= m_bmpNormal
.GetWidth()+border
;
264 ret
.y
= m_bmpNormal
.GetHeight()+border
;