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"
15 #include "wx/bmpbuttn.h"
18 #pragma message disable nosimpint
20 #include <Xm/PushBG.h>
23 #pragma message enable nosimpint
26 #include "wx/motif/private.h"
28 // Implemented in button.cpp
29 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer ptr
);
31 // Pixmap XCreateInsensitivePixmap( Display *display, Pixmap pixmap );
33 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton
, wxButton
)
35 wxBitmapButton::wxBitmapButton()
37 m_marginX
= m_marginY
= wxDEFAULT_BUTTON_MARGIN
;
38 m_insensPixmap
= (WXPixmap
) 0;
41 bool wxBitmapButton::Create(wxWindow
*parent
, wxWindowID id
,
42 const wxBitmap
& bitmap
,
44 const wxSize
& size
, long style
,
45 const wxValidator
& validator
,
48 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
52 m_bitmaps
[State_Normal
] =
53 m_bitmapsOriginal
[State_Normal
] = bitmap
;
54 m_bitmaps
[State_Pressed
] =
55 m_bitmapsOriginal
[State_Pressed
] = bitmap
;
57 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
60 * Patch Note (important)
61 * There is no major reason to put a defaultButtonThickness here.
62 * Not requesting it give the ability to put wxButton with a spacing
63 * as small as requested. However, if some button become a DefaultButton,
64 * other buttons are no more aligned -- This is why we set
65 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
66 * in the ::SetDefaultButton method.
68 Widget buttonWidget
= XtVaCreateManagedWidget ("button",
70 // Gadget causes problems for default button operation.
72 xmPushButtonGadgetClass
, parentWidget
,
74 xmPushButtonWidgetClass
, parentWidget
,
76 // See comment for wxButton::SetDefault
77 // XmNdefaultButtonShadowThickness, 1,
78 XmNrecomputeSize
, False
,
81 m_mainWidget
= (WXWidget
) buttonWidget
;
83 XtAddCallback (buttonWidget
,
84 XmNactivateCallback
, (XtCallbackProc
) wxButtonCallback
,
87 wxSize best
= GetBitmapLabel().IsOk() ? GetBestSize() : wxSize(30, 30);
88 if( size
.x
!= -1 ) best
.x
= size
.x
;
89 if( size
.y
!= -1 ) best
.y
= size
.y
;
94 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
95 pos
.x
, pos
.y
, best
.x
, best
.y
);
100 wxBitmapButton::~wxBitmapButton()
102 SetBitmapLabel(wxNullBitmap
);
105 XmDestroyPixmap (DefaultScreenOfDisplay ((Display
*) GetXDisplay()),
106 (Pixmap
) m_insensPixmap
);
109 void wxBitmapButton::DoSetBitmap(const wxBitmap
& bitmap
, State which
)
111 m_bitmapsOriginal
[which
] = bitmap
;
113 wxBitmapButtonBase::DoSetBitmap(bitmap
, which
);
116 void wxBitmapButton::OnSetBitmap()
118 wxBitmapButtonBase::OnSetBitmap();
120 if ( m_bitmapsOriginal
[State_Normal
].IsOk() )
123 Pixmap insensPixmap
= 0;
124 Pixmap armPixmap
= 0;
126 // Must re-make the bitmap to have its transparent areas drawn
127 // in the current widget background colour.
128 if ( m_bitmapsOriginal
[State_Normal
].GetMask() )
130 WXPixel backgroundPixel
;
131 XtVaGetValues((Widget
) m_mainWidget
,
132 XmNbackground
, &backgroundPixel
,
136 col
.SetPixel(backgroundPixel
);
139 wxCreateMaskedBitmap(m_bitmapsOriginal
[State_Normal
], col
);
140 m_bitmaps
[State_Normal
] = newBitmap
;
141 m_bitmapCache
.SetBitmap( m_bitmaps
[State_Normal
] );
143 pixmap
= (Pixmap
) m_bitmaps
[State_Normal
].GetDrawable();
147 m_bitmapCache
.SetBitmap( m_bitmaps
[State_Normal
] );
148 pixmap
= (Pixmap
) m_bitmapCache
.GetLabelPixmap(m_mainWidget
);
151 if (m_bitmapsOriginal
[State_Disabled
].IsOk())
153 if (m_bitmapsOriginal
[State_Disabled
].GetMask())
155 WXPixel backgroundPixel
;
156 XtVaGetValues((Widget
) m_mainWidget
,
157 XmNbackground
, &backgroundPixel
,
161 col
.SetPixel(backgroundPixel
);
164 wxCreateMaskedBitmap(m_bitmapsOriginal
[State_Disabled
], col
);
165 m_bitmaps
[State_Disabled
] = newBitmap
;
167 insensPixmap
= (Pixmap
) m_bitmaps
[State_Disabled
].GetDrawable();
170 insensPixmap
= (Pixmap
) m_bitmapCache
.GetInsensPixmap(m_mainWidget
);
173 insensPixmap
= (Pixmap
) m_bitmapCache
.GetInsensPixmap(m_mainWidget
);
175 // Now make the bitmap representing the armed state
176 if (m_bitmapsOriginal
[State_Pressed
].IsOk())
178 if (m_bitmapsOriginal
[State_Pressed
].GetMask())
180 WXPixel backgroundPixel
;
181 XtVaGetValues((Widget
) m_mainWidget
,
182 XmNarmColor
, &backgroundPixel
,
186 col
.SetPixel(backgroundPixel
);
189 wxCreateMaskedBitmap(m_bitmapsOriginal
[State_Pressed
], col
);
190 m_bitmaps
[State_Pressed
] = newBitmap
;
192 armPixmap
= (Pixmap
) m_bitmaps
[State_Pressed
].GetDrawable();
195 armPixmap
= (Pixmap
) m_bitmapCache
.GetArmPixmap(m_mainWidget
);
198 armPixmap
= (Pixmap
) m_bitmapCache
.GetArmPixmap(m_mainWidget
);
200 XtVaSetValues ((Widget
) m_mainWidget
,
201 XmNlabelPixmap
, pixmap
,
202 XmNlabelInsensitivePixmap
, insensPixmap
,
203 XmNarmPixmap
, armPixmap
,
204 XmNlabelType
, XmPIXMAP
,
209 // Null bitmap: must not use current pixmap
210 // since it is no longer valid.
211 XtVaSetValues ((Widget
) m_mainWidget
,
212 XmNlabelType
, XmSTRING
,
213 XmNlabelPixmap
, XmUNSPECIFIED_PIXMAP
,
214 XmNlabelInsensitivePixmap
, XmUNSPECIFIED_PIXMAP
,
215 XmNarmPixmap
, XmUNSPECIFIED_PIXMAP
,
220 void wxBitmapButton::ChangeBackgroundColour()
222 wxDoChangeBackgroundColour(m_mainWidget
, m_backgroundColour
, true);
224 // Must reset the bitmaps since the colours have changed.
228 wxSize
wxBitmapButton::DoGetBestSize() const
232 if (GetBitmapLabel().IsOk())
234 int border
= HasFlag(wxNO_BORDER
) ? 4 : 10;
238 ret
+= GetBitmapLabel().GetSize();