1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/bmpbuttn.cpp
3 // Purpose: wxBitmapButton
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
14 #include "wx/bmpbuttn.h"
17 #pragma message disable nosimpint
19 #include <Xm/PushBG.h>
22 #pragma message enable nosimpint
25 #include "wx/motif/private.h"
27 // Implemented in button.cpp
28 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer ptr
);
30 // Pixmap XCreateInsensitivePixmap( Display *display, Pixmap pixmap );
32 wxBitmapButton::wxBitmapButton()
34 m_marginX
= m_marginY
= wxDEFAULT_BUTTON_MARGIN
;
35 m_insensPixmap
= (WXPixmap
) 0;
38 bool wxBitmapButton::Create(wxWindow
*parent
, wxWindowID id
,
39 const wxBitmap
& bitmap
,
41 const wxSize
& size
, long style
,
42 const wxValidator
& validator
,
45 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
49 m_bitmaps
[State_Normal
] =
50 m_bitmapsOriginal
[State_Normal
] = bitmap
;
51 m_bitmaps
[State_Pressed
] =
52 m_bitmapsOriginal
[State_Pressed
] = bitmap
;
54 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
57 * Patch Note (important)
58 * There is no major reason to put a defaultButtonThickness here.
59 * Not requesting it give the ability to put wxButton with a spacing
60 * as small as requested. However, if some button become a DefaultButton,
61 * other buttons are no more aligned -- This is why we set
62 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
63 * in the ::SetDefaultButton method.
65 Widget buttonWidget
= XtVaCreateManagedWidget ("button",
67 // Gadget causes problems for default button operation.
69 xmPushButtonGadgetClass
, parentWidget
,
71 xmPushButtonWidgetClass
, parentWidget
,
73 // See comment for wxButton::SetDefault
74 // XmNdefaultButtonShadowThickness, 1,
75 XmNrecomputeSize
, False
,
78 m_mainWidget
= (WXWidget
) buttonWidget
;
80 XtAddCallback (buttonWidget
,
81 XmNactivateCallback
, (XtCallbackProc
) wxButtonCallback
,
84 wxSize best
= GetBitmapLabel().IsOk() ? GetBestSize() : wxSize(30, 30);
85 if( size
.x
!= -1 ) best
.x
= size
.x
;
86 if( size
.y
!= -1 ) best
.y
= size
.y
;
91 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
92 pos
.x
, pos
.y
, best
.x
, best
.y
);
97 wxBitmapButton::~wxBitmapButton()
99 SetBitmapLabel(wxNullBitmap
);
102 XmDestroyPixmap (DefaultScreenOfDisplay ((Display
*) GetXDisplay()),
103 (Pixmap
) m_insensPixmap
);
106 void wxBitmapButton::DoSetBitmap(const wxBitmap
& bitmap
, State which
)
108 m_bitmapsOriginal
[which
] = bitmap
;
110 wxBitmapButtonBase::DoSetBitmap(bitmap
, which
);
113 void wxBitmapButton::OnSetBitmap()
115 wxBitmapButtonBase::OnSetBitmap();
117 if ( m_bitmapsOriginal
[State_Normal
].IsOk() )
120 Pixmap insensPixmap
= 0;
121 Pixmap armPixmap
= 0;
123 // Must re-make the bitmap to have its transparent areas drawn
124 // in the current widget background colour.
125 if ( m_bitmapsOriginal
[State_Normal
].GetMask() )
127 WXPixel backgroundPixel
;
128 XtVaGetValues((Widget
) m_mainWidget
,
129 XmNbackground
, &backgroundPixel
,
133 col
.SetPixel(backgroundPixel
);
136 wxCreateMaskedBitmap(m_bitmapsOriginal
[State_Normal
], col
);
137 m_bitmaps
[State_Normal
] = newBitmap
;
138 m_bitmapCache
.SetBitmap( m_bitmaps
[State_Normal
] );
140 pixmap
= (Pixmap
) m_bitmaps
[State_Normal
].GetDrawable();
144 m_bitmapCache
.SetBitmap( m_bitmaps
[State_Normal
] );
145 pixmap
= (Pixmap
) m_bitmapCache
.GetLabelPixmap(m_mainWidget
);
148 if (m_bitmapsOriginal
[State_Disabled
].IsOk())
150 if (m_bitmapsOriginal
[State_Disabled
].GetMask())
152 WXPixel backgroundPixel
;
153 XtVaGetValues((Widget
) m_mainWidget
,
154 XmNbackground
, &backgroundPixel
,
158 col
.SetPixel(backgroundPixel
);
161 wxCreateMaskedBitmap(m_bitmapsOriginal
[State_Disabled
], col
);
162 m_bitmaps
[State_Disabled
] = newBitmap
;
164 insensPixmap
= (Pixmap
) m_bitmaps
[State_Disabled
].GetDrawable();
167 insensPixmap
= (Pixmap
) m_bitmapCache
.GetInsensPixmap(m_mainWidget
);
170 insensPixmap
= (Pixmap
) m_bitmapCache
.GetInsensPixmap(m_mainWidget
);
172 // Now make the bitmap representing the armed state
173 if (m_bitmapsOriginal
[State_Pressed
].IsOk())
175 if (m_bitmapsOriginal
[State_Pressed
].GetMask())
177 WXPixel backgroundPixel
;
178 XtVaGetValues((Widget
) m_mainWidget
,
179 XmNarmColor
, &backgroundPixel
,
183 col
.SetPixel(backgroundPixel
);
186 wxCreateMaskedBitmap(m_bitmapsOriginal
[State_Pressed
], col
);
187 m_bitmaps
[State_Pressed
] = newBitmap
;
189 armPixmap
= (Pixmap
) m_bitmaps
[State_Pressed
].GetDrawable();
192 armPixmap
= (Pixmap
) m_bitmapCache
.GetArmPixmap(m_mainWidget
);
195 armPixmap
= (Pixmap
) m_bitmapCache
.GetArmPixmap(m_mainWidget
);
197 XtVaSetValues ((Widget
) m_mainWidget
,
198 XmNlabelPixmap
, pixmap
,
199 XmNlabelInsensitivePixmap
, insensPixmap
,
200 XmNarmPixmap
, armPixmap
,
201 XmNlabelType
, XmPIXMAP
,
206 // Null bitmap: must not use current pixmap
207 // since it is no longer valid.
208 XtVaSetValues ((Widget
) m_mainWidget
,
209 XmNlabelType
, XmSTRING
,
210 XmNlabelPixmap
, XmUNSPECIFIED_PIXMAP
,
211 XmNlabelInsensitivePixmap
, XmUNSPECIFIED_PIXMAP
,
212 XmNarmPixmap
, XmUNSPECIFIED_PIXMAP
,
217 void wxBitmapButton::ChangeBackgroundColour()
219 wxDoChangeBackgroundColour(m_mainWidget
, m_backgroundColour
, true);
221 // Must reset the bitmaps since the colours have changed.
225 wxSize
wxBitmapButton::DoGetBestSize() const
229 if (GetBitmapLabel().IsOk())
231 int border
= HasFlag(wxNO_BORDER
) ? 4 : 10;
235 ret
+= GetBitmapLabel().GetSize();