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
) )
56 m_bmpNormal
= m_bmpNormalOriginal
= bitmap
;
57 m_bmpSelected
= m_bmpSelectedOriginal
= bitmap
;
59 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
62 * Patch Note (important)
63 * There is no major reason to put a defaultButtonThickness here.
64 * Not requesting it give the ability to put wxButton with a spacing
65 * as small as requested. However, if some button become a DefaultButton,
66 * other buttons are no more aligned -- This is why we set
67 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
68 * in the ::SetDefaultButton method.
70 Widget buttonWidget
= XtVaCreateManagedWidget ("button",
72 // Gadget causes problems for default button operation.
74 xmPushButtonGadgetClass
, parentWidget
,
76 xmPushButtonWidgetClass
, parentWidget
,
78 // See comment for wxButton::SetDefault
79 // XmNdefaultButtonShadowThickness, 1,
80 XmNrecomputeSize
, False
,
83 m_mainWidget
= (WXWidget
) buttonWidget
;
85 XtAddCallback (buttonWidget
,
86 XmNactivateCallback
, (XtCallbackProc
) wxButtonCallback
,
89 wxSize best
= m_bmpNormal
.Ok() ? GetBestSize() : wxSize(30, 30);
90 if( size
.x
!= -1 ) best
.x
= size
.x
;
91 if( size
.y
!= -1 ) best
.y
= size
.y
;
96 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
97 pos
.x
, pos
.y
, best
.x
, best
.y
);
102 wxBitmapButton::~wxBitmapButton()
104 SetBitmapLabel(wxNullBitmap
);
107 XmDestroyPixmap (DefaultScreenOfDisplay ((Display
*) GetXDisplay()),
108 (Pixmap
) m_insensPixmap
);
111 void wxBitmapButton::SetBitmapLabel(const wxBitmap
& bitmap
)
113 m_bmpNormalOriginal
= bitmap
;
114 m_bmpNormal
= bitmap
;
119 void wxBitmapButton::SetBitmapSelected(const wxBitmap
& sel
)
122 m_bmpSelectedOriginal
= sel
;
127 void wxBitmapButton::SetBitmapFocus(const wxBitmap
& focus
)
133 void wxBitmapButton::SetBitmapDisabled(const wxBitmap
& disabled
)
135 m_bmpDisabled
= disabled
;
136 m_bmpDisabledOriginal
= disabled
;
141 void wxBitmapButton::DoSetBitmap()
143 if (m_bmpNormalOriginal
.Ok())
146 Pixmap insensPixmap
= 0;
147 Pixmap armPixmap
= 0;
149 // Must re-make the bitmap to have its transparent areas drawn
150 // in the current widget background colour.
151 if (m_bmpNormalOriginal
.GetMask())
153 WXPixel backgroundPixel
;
154 XtVaGetValues((Widget
) m_mainWidget
,
155 XmNbackground
, &backgroundPixel
,
159 col
.SetPixel(backgroundPixel
);
162 wxCreateMaskedBitmap(m_bmpNormalOriginal
, col
);
163 m_bmpNormal
= newBitmap
;
164 m_bitmapCache
.SetBitmap( m_bmpNormal
);
166 pixmap
= (Pixmap
) m_bmpNormal
.GetDrawable();
170 m_bitmapCache
.SetBitmap( m_bmpNormal
);
171 pixmap
= (Pixmap
) m_bitmapCache
.GetLabelPixmap(m_mainWidget
);
174 if (m_bmpDisabledOriginal
.Ok())
176 if (m_bmpDisabledOriginal
.GetMask())
178 WXPixel backgroundPixel
;
179 XtVaGetValues((Widget
) m_mainWidget
,
180 XmNbackground
, &backgroundPixel
,
184 col
.SetPixel(backgroundPixel
);
187 wxCreateMaskedBitmap(m_bmpDisabledOriginal
, col
);
188 m_bmpDisabled
= newBitmap
;
190 insensPixmap
= (Pixmap
) m_bmpDisabled
.GetDrawable();
193 insensPixmap
= (Pixmap
) m_bitmapCache
.GetInsensPixmap(m_mainWidget
);
196 insensPixmap
= (Pixmap
) m_bitmapCache
.GetInsensPixmap(m_mainWidget
);
198 // Now make the bitmap representing the armed state
199 if (m_bmpSelectedOriginal
.Ok())
201 if (m_bmpSelectedOriginal
.GetMask())
203 WXPixel backgroundPixel
;
204 XtVaGetValues((Widget
) m_mainWidget
,
205 XmNarmColor
, &backgroundPixel
,
209 col
.SetPixel(backgroundPixel
);
212 wxCreateMaskedBitmap(m_bmpSelectedOriginal
, col
);
213 m_bmpSelected
= newBitmap
;
215 armPixmap
= (Pixmap
) m_bmpSelected
.GetDrawable();
218 armPixmap
= (Pixmap
) m_bitmapCache
.GetArmPixmap(m_mainWidget
);
221 armPixmap
= (Pixmap
) m_bitmapCache
.GetArmPixmap(m_mainWidget
);
223 XtVaSetValues ((Widget
) m_mainWidget
,
224 XmNlabelPixmap
, pixmap
,
225 XmNlabelInsensitivePixmap
, insensPixmap
,
226 XmNarmPixmap
, armPixmap
,
227 XmNlabelType
, XmPIXMAP
,
232 // Null bitmap: must not use current pixmap
233 // since it is no longer valid.
234 XtVaSetValues ((Widget
) m_mainWidget
,
235 XmNlabelType
, XmSTRING
,
236 XmNlabelPixmap
, XmUNSPECIFIED_PIXMAP
,
237 XmNlabelInsensitivePixmap
, XmUNSPECIFIED_PIXMAP
,
238 XmNarmPixmap
, XmUNSPECIFIED_PIXMAP
,
243 void wxBitmapButton::ChangeBackgroundColour()
245 wxDoChangeBackgroundColour(m_mainWidget
, m_backgroundColour
, true);
247 // Must reset the bitmaps since the colours have changed.
251 wxSize
wxBitmapButton::DoGetBestSize() const
255 if (m_bmpNormal
.Ok())
257 int border
= (GetWindowStyle() & wxNO_BORDER
) ? 4 : 10;
258 ret
.x
= m_bmpNormal
.GetWidth()+border
;
259 ret
.y
= m_bmpNormal
.GetHeight()+border
;