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