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 wxBitmapButton::wxBitmapButton()
35 m_marginX
= m_marginY
= wxDEFAULT_BUTTON_MARGIN
;
36 m_insensPixmap
= (WXPixmap
) 0;
39 bool wxBitmapButton::Create(wxWindow
*parent
, wxWindowID id
,
40 const wxBitmap
& bitmap
,
42 const wxSize
& size
, long style
,
43 const wxValidator
& validator
,
46 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
50 m_bitmaps
[State_Normal
] =
51 m_bitmapsOriginal
[State_Normal
] = bitmap
;
52 m_bitmaps
[State_Pressed
] =
53 m_bitmapsOriginal
[State_Pressed
] = bitmap
;
55 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
58 * Patch Note (important)
59 * There is no major reason to put a defaultButtonThickness here.
60 * Not requesting it give the ability to put wxButton with a spacing
61 * as small as requested. However, if some button become a DefaultButton,
62 * other buttons are no more aligned -- This is why we set
63 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
64 * in the ::SetDefaultButton method.
66 Widget buttonWidget
= XtVaCreateManagedWidget ("button",
68 // Gadget causes problems for default button operation.
70 xmPushButtonGadgetClass
, parentWidget
,
72 xmPushButtonWidgetClass
, parentWidget
,
74 // See comment for wxButton::SetDefault
75 // XmNdefaultButtonShadowThickness, 1,
76 XmNrecomputeSize
, False
,
79 m_mainWidget
= (WXWidget
) buttonWidget
;
81 XtAddCallback (buttonWidget
,
82 XmNactivateCallback
, (XtCallbackProc
) wxButtonCallback
,
85 wxSize best
= GetBitmapLabel().IsOk() ? GetBestSize() : wxSize(30, 30);
86 if( size
.x
!= -1 ) best
.x
= size
.x
;
87 if( size
.y
!= -1 ) best
.y
= size
.y
;
92 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
93 pos
.x
, pos
.y
, best
.x
, best
.y
);
98 wxBitmapButton::~wxBitmapButton()
100 SetBitmapLabel(wxNullBitmap
);
103 XmDestroyPixmap (DefaultScreenOfDisplay ((Display
*) GetXDisplay()),
104 (Pixmap
) m_insensPixmap
);
107 void wxBitmapButton::DoSetBitmap(const wxBitmap
& bitmap
, State which
)
109 m_bitmapsOriginal
[which
] = bitmap
;
111 wxBitmapButtonBase::DoSetBitmap(bitmap
, which
);
114 void wxBitmapButton::OnSetBitmap()
116 wxBitmapButtonBase::OnSetBitmap();
118 if ( m_bitmapsOriginal
[State_Normal
].IsOk() )
121 Pixmap insensPixmap
= 0;
122 Pixmap armPixmap
= 0;
124 // Must re-make the bitmap to have its transparent areas drawn
125 // in the current widget background colour.
126 if ( m_bitmapsOriginal
[State_Normal
].GetMask() )
128 WXPixel backgroundPixel
;
129 XtVaGetValues((Widget
) m_mainWidget
,
130 XmNbackground
, &backgroundPixel
,
134 col
.SetPixel(backgroundPixel
);
137 wxCreateMaskedBitmap(m_bitmapsOriginal
[State_Normal
], col
);
138 m_bitmaps
[State_Normal
] = newBitmap
;
139 m_bitmapCache
.SetBitmap( m_bitmaps
[State_Normal
] );
141 pixmap
= (Pixmap
) m_bitmaps
[State_Normal
].GetDrawable();
145 m_bitmapCache
.SetBitmap( m_bitmaps
[State_Normal
] );
146 pixmap
= (Pixmap
) m_bitmapCache
.GetLabelPixmap(m_mainWidget
);
149 if (m_bitmapsOriginal
[State_Disabled
].IsOk())
151 if (m_bitmapsOriginal
[State_Disabled
].GetMask())
153 WXPixel backgroundPixel
;
154 XtVaGetValues((Widget
) m_mainWidget
,
155 XmNbackground
, &backgroundPixel
,
159 col
.SetPixel(backgroundPixel
);
162 wxCreateMaskedBitmap(m_bitmapsOriginal
[State_Disabled
], col
);
163 m_bitmaps
[State_Disabled
] = newBitmap
;
165 insensPixmap
= (Pixmap
) m_bitmaps
[State_Disabled
].GetDrawable();
168 insensPixmap
= (Pixmap
) m_bitmapCache
.GetInsensPixmap(m_mainWidget
);
171 insensPixmap
= (Pixmap
) m_bitmapCache
.GetInsensPixmap(m_mainWidget
);
173 // Now make the bitmap representing the armed state
174 if (m_bitmapsOriginal
[State_Pressed
].IsOk())
176 if (m_bitmapsOriginal
[State_Pressed
].GetMask())
178 WXPixel backgroundPixel
;
179 XtVaGetValues((Widget
) m_mainWidget
,
180 XmNarmColor
, &backgroundPixel
,
184 col
.SetPixel(backgroundPixel
);
187 wxCreateMaskedBitmap(m_bitmapsOriginal
[State_Pressed
], col
);
188 m_bitmaps
[State_Pressed
] = newBitmap
;
190 armPixmap
= (Pixmap
) m_bitmaps
[State_Pressed
].GetDrawable();
193 armPixmap
= (Pixmap
) m_bitmapCache
.GetArmPixmap(m_mainWidget
);
196 armPixmap
= (Pixmap
) m_bitmapCache
.GetArmPixmap(m_mainWidget
);
198 XtVaSetValues ((Widget
) m_mainWidget
,
199 XmNlabelPixmap
, pixmap
,
200 XmNlabelInsensitivePixmap
, insensPixmap
,
201 XmNarmPixmap
, armPixmap
,
202 XmNlabelType
, XmPIXMAP
,
207 // Null bitmap: must not use current pixmap
208 // since it is no longer valid.
209 XtVaSetValues ((Widget
) m_mainWidget
,
210 XmNlabelType
, XmSTRING
,
211 XmNlabelPixmap
, XmUNSPECIFIED_PIXMAP
,
212 XmNlabelInsensitivePixmap
, XmUNSPECIFIED_PIXMAP
,
213 XmNarmPixmap
, XmUNSPECIFIED_PIXMAP
,
218 void wxBitmapButton::ChangeBackgroundColour()
220 wxDoChangeBackgroundColour(m_mainWidget
, m_backgroundColour
, true);
222 // Must reset the bitmaps since the colours have changed.
226 wxSize
wxBitmapButton::DoGetBestSize() const
230 if (GetBitmapLabel().IsOk())
232 int border
= HasFlag(wxNO_BORDER
) ? 4 : 10;
236 ret
+= GetBitmapLabel().GetSize();