]> git.saurik.com Git - wxWidgets.git/blame - src/motif/bmpbuttn.cpp
correct text drawing with background brush, fixes #14161
[wxWidgets.git] / src / motif / bmpbuttn.cpp
CommitLineData
4bb6408c 1/////////////////////////////////////////////////////////////////////////////
7520f3da 2// Name: src/motif/bmpbuttn.cpp
4bb6408c
JS
3// Purpose: wxBitmapButton
4// Author: Julian Smart
5// Modified by:
6// Created: 17/09/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
4bb6408c
JS
10/////////////////////////////////////////////////////////////////////////////
11
1248b41f
MB
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
4bb6408c
JS
15#include "wx/bmpbuttn.h"
16
338dd992
JJ
17#ifdef __VMS__
18#pragma message disable nosimpint
19#endif
a4294b78
JS
20#include <Xm/PushBG.h>
21#include <Xm/PushB.h>
338dd992
JJ
22#ifdef __VMS__
23#pragma message enable nosimpint
24#endif
a4294b78
JS
25
26#include "wx/motif/private.h"
27
28// Implemented in button.cpp
29void wxButtonCallback (Widget w, XtPointer clientData, XtPointer ptr);
30
aae91497 31// Pixmap XCreateInsensitivePixmap( Display *display, Pixmap pixmap );
a4294b78 32
a4294b78
JS
33wxBitmapButton::wxBitmapButton()
34{
ea9868e8 35 m_marginX = m_marginY = wxDEFAULT_BUTTON_MARGIN;
a4294b78
JS
36 m_insensPixmap = (WXPixmap) 0;
37}
38
ea9868e8
MB
39bool wxBitmapButton::Create(wxWindow *parent, wxWindowID id,
40 const wxBitmap& bitmap,
2d120f83
JS
41 const wxPoint& pos,
42 const wxSize& size, long style,
43 const wxValidator& validator,
44 const wxString& name)
4bb6408c 45{
ea9868e8
MB
46 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
47 return false;
105fbe1f 48 PreCreation();
dfe1eee3 49
2352862a
VZ
50 m_bitmaps[State_Normal] =
51 m_bitmapsOriginal[State_Normal] = bitmap;
52 m_bitmaps[State_Pressed] =
53 m_bitmapsOriginal[State_Pressed] = bitmap;
dfe1eee3 54
a4294b78 55 Widget parentWidget = (Widget) parent->GetClientWidget();
dfe1eee3 56
2d120f83
JS
57 /*
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.
65 */
a4294b78 66 Widget buttonWidget = XtVaCreateManagedWidget ("button",
dfe1eee3 67
2d120f83 68 // Gadget causes problems for default button operation.
a4294b78 69#if wxUSE_GADGETS
2d120f83 70 xmPushButtonGadgetClass, parentWidget,
a4294b78 71#else
2d120f83 72 xmPushButtonWidgetClass, parentWidget,
a4294b78 73#endif
ea9868e8 74 // See comment for wxButton::SetDefault
7520f3da 75 // XmNdefaultButtonShadowThickness, 1,
ea9868e8 76 XmNrecomputeSize, False,
2d120f83 77 NULL);
dfe1eee3 78
a4294b78 79 m_mainWidget = (WXWidget) buttonWidget;
dfe1eee3 80
ea9868e8
MB
81 XtAddCallback (buttonWidget,
82 XmNactivateCallback, (XtCallbackProc) wxButtonCallback,
83 (XtPointer) this);
dfe1eee3 84
2352862a 85 wxSize best = GetBitmapLabel().IsOk() ? GetBestSize() : wxSize(30, 30);
ea9868e8
MB
86 if( size.x != -1 ) best.x = size.x;
87 if( size.y != -1 ) best.y = size.y;
88
105fbe1f 89 PostCreation();
ba8ac2c7 90 OnSetBitmap();
105fbe1f 91
ea9868e8
MB
92 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
93 pos.x, pos.y, best.x, best.y);
dfe1eee3 94
96be256b 95 return true;
a4294b78
JS
96}
97
98wxBitmapButton::~wxBitmapButton()
99{
100 SetBitmapLabel(wxNullBitmap);
dfe1eee3 101
a4294b78 102 if (m_insensPixmap)
ea9868e8
MB
103 XmDestroyPixmap (DefaultScreenOfDisplay ((Display*) GetXDisplay()),
104 (Pixmap) m_insensPixmap);
4bb6408c
JS
105}
106
2352862a 107void wxBitmapButton::DoSetBitmap(const wxBitmap& bitmap, State which)
4bb6408c 108{
2352862a 109 m_bitmapsOriginal[which] = bitmap;
dfe1eee3 110
2352862a 111 wxBitmapButtonBase::DoSetBitmap(bitmap, which);
e933b5bc 112}
321db4b6 113
2352862a 114void wxBitmapButton::OnSetBitmap()
321db4b6 115{
2352862a 116 wxBitmapButtonBase::OnSetBitmap();
dfe1eee3 117
2352862a 118 if ( m_bitmapsOriginal[State_Normal].IsOk() )
a4294b78 119 {
321db4b6
JS
120 Pixmap pixmap = 0;
121 Pixmap insensPixmap = 0;
122 Pixmap armPixmap = 0;
dfe1eee3 123
321db4b6
JS
124 // Must re-make the bitmap to have its transparent areas drawn
125 // in the current widget background colour.
2352862a 126 if ( m_bitmapsOriginal[State_Normal].GetMask() )
321db4b6 127 {
3e0071d9 128 WXPixel backgroundPixel;
ea9868e8
MB
129 XtVaGetValues((Widget) m_mainWidget,
130 XmNbackground, &backgroundPixel,
131 NULL);
dfe1eee3 132
321db4b6
JS
133 wxColour col;
134 col.SetPixel(backgroundPixel);
dfe1eee3 135
ea9868e8 136 wxBitmap newBitmap =
2352862a
VZ
137 wxCreateMaskedBitmap(m_bitmapsOriginal[State_Normal], col);
138 m_bitmaps[State_Normal] = newBitmap;
139 m_bitmapCache.SetBitmap( m_bitmaps[State_Normal] );
dfe1eee3 140
2352862a 141 pixmap = (Pixmap) m_bitmaps[State_Normal].GetDrawable();
321db4b6 142 }
a4294b78 143 else
aae91497 144 {
2352862a 145 m_bitmapCache.SetBitmap( m_bitmaps[State_Normal] );
aae91497
MB
146 pixmap = (Pixmap) m_bitmapCache.GetLabelPixmap(m_mainWidget);
147 }
dfe1eee3 148
2352862a 149 if (m_bitmapsOriginal[State_Disabled].IsOk())
321db4b6 150 {
2352862a 151 if (m_bitmapsOriginal[State_Disabled].GetMask())
321db4b6 152 {
3e0071d9 153 WXPixel backgroundPixel;
ea9868e8
MB
154 XtVaGetValues((Widget) m_mainWidget,
155 XmNbackground, &backgroundPixel,
156 NULL);
dfe1eee3 157
321db4b6
JS
158 wxColour col;
159 col.SetPixel(backgroundPixel);
dfe1eee3 160
ea9868e8 161 wxBitmap newBitmap =
2352862a
VZ
162 wxCreateMaskedBitmap(m_bitmapsOriginal[State_Disabled], col);
163 m_bitmaps[State_Disabled] = newBitmap;
dfe1eee3 164
2352862a 165 insensPixmap = (Pixmap) m_bitmaps[State_Disabled].GetDrawable();
321db4b6
JS
166 }
167 else
aae91497 168 insensPixmap = (Pixmap) m_bitmapCache.GetInsensPixmap(m_mainWidget);
321db4b6 169 }
a4294b78 170 else
aae91497 171 insensPixmap = (Pixmap) m_bitmapCache.GetInsensPixmap(m_mainWidget);
dfe1eee3 172
321db4b6 173 // Now make the bitmap representing the armed state
2352862a 174 if (m_bitmapsOriginal[State_Pressed].IsOk())
a4294b78 175 {
2352862a 176 if (m_bitmapsOriginal[State_Pressed].GetMask())
321db4b6 177 {
3e0071d9 178 WXPixel backgroundPixel;
ea9868e8
MB
179 XtVaGetValues((Widget) m_mainWidget,
180 XmNarmColor, &backgroundPixel,
181 NULL);
dfe1eee3 182
321db4b6
JS
183 wxColour col;
184 col.SetPixel(backgroundPixel);
dfe1eee3 185
ea9868e8 186 wxBitmap newBitmap =
2352862a
VZ
187 wxCreateMaskedBitmap(m_bitmapsOriginal[State_Pressed], col);
188 m_bitmaps[State_Pressed] = newBitmap;
dfe1eee3 189
2352862a 190 armPixmap = (Pixmap) m_bitmaps[State_Pressed].GetDrawable();
321db4b6
JS
191 }
192 else
aae91497 193 armPixmap = (Pixmap) m_bitmapCache.GetArmPixmap(m_mainWidget);
321db4b6
JS
194 }
195 else
aae91497 196 armPixmap = (Pixmap) m_bitmapCache.GetArmPixmap(m_mainWidget);
dfe1eee3 197
a4294b78 198 XtVaSetValues ((Widget) m_mainWidget,
321db4b6
JS
199 XmNlabelPixmap, pixmap,
200 XmNlabelInsensitivePixmap, insensPixmap,
a4294b78
JS
201 XmNarmPixmap, armPixmap,
202 XmNlabelType, XmPIXMAP,
203 NULL);
204 }
205 else
206 {
207 // Null bitmap: must not use current pixmap
208 // since it is no longer valid.
209 XtVaSetValues ((Widget) m_mainWidget,
210 XmNlabelType, XmSTRING,
1a3ac83f 211 XmNlabelPixmap, XmUNSPECIFIED_PIXMAP,
2d120f83 212 XmNlabelInsensitivePixmap, XmUNSPECIFIED_PIXMAP,
321db4b6 213 XmNarmPixmap, XmUNSPECIFIED_PIXMAP,
a4294b78
JS
214 NULL);
215 }
4bb6408c
JS
216}
217
321db4b6 218void wxBitmapButton::ChangeBackgroundColour()
a4294b78 219{
96be256b 220 wxDoChangeBackgroundColour(m_mainWidget, m_backgroundColour, true);
dfe1eee3 221
321db4b6 222 // Must reset the bitmaps since the colours have changed.
ba8ac2c7 223 OnSetBitmap();
321db4b6 224}
401eb3de
RR
225
226wxSize wxBitmapButton::DoGetBestSize() const
227{
228 wxSize ret( 30,30 );
229
2352862a 230 if (GetBitmapLabel().IsOk())
401eb3de 231 {
2352862a
VZ
232 int border = HasFlag(wxNO_BORDER) ? 4 : 10;
233 ret.x += border;
234 ret.y += border;
235
236 ret += GetBitmapLabel().GetSize();
401eb3de
RR
237 }
238
239 return ret;
240}