]> git.saurik.com Git - wxWidgets.git/blame - src/motif/bmpbuttn.cpp
fixed TREE_ITEM_MENU generation from right mouse clicks: don't pass WM_RBUTTONDOWN...
[wxWidgets.git] / src / motif / bmpbuttn.cpp
CommitLineData
4bb6408c
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: bmpbuttn.cpp
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
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
4bb6408c
JS
13#pragma implementation "bmpbuttn.h"
14#endif
15
1248b41f
MB
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
bcd055ae
JJ
19#ifdef __VMS
20#define XtScreen XTSCREEN
21#endif
22
f6045f99
GD
23#include "wx/defs.h"
24
4bb6408c
JS
25#include "wx/bmpbuttn.h"
26
338dd992
JJ
27#ifdef __VMS__
28#pragma message disable nosimpint
29#endif
a4294b78
JS
30#include <Xm/PushBG.h>
31#include <Xm/PushB.h>
338dd992
JJ
32#ifdef __VMS__
33#pragma message enable nosimpint
34#endif
a4294b78
JS
35
36#include "wx/motif/private.h"
37
38// Implemented in button.cpp
39void wxButtonCallback (Widget w, XtPointer clientData, XtPointer ptr);
40
aae91497 41// Pixmap XCreateInsensitivePixmap( Display *display, Pixmap pixmap );
a4294b78 42
4bb6408c 43IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton)
4bb6408c 44
a4294b78
JS
45wxBitmapButton::wxBitmapButton()
46{
ea9868e8 47 m_marginX = m_marginY = wxDEFAULT_BUTTON_MARGIN;
a4294b78
JS
48 m_insensPixmap = (WXPixmap) 0;
49}
50
ea9868e8
MB
51bool wxBitmapButton::Create(wxWindow *parent, wxWindowID id,
52 const wxBitmap& bitmap,
2d120f83
JS
53 const wxPoint& pos,
54 const wxSize& size, long style,
55 const wxValidator& validator,
56 const wxString& name)
4bb6408c 57{
ea9868e8
MB
58 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
59 return false;
dfe1eee3 60
ea9868e8
MB
61 m_bmpNormal = m_bmpNormalOriginal = bitmap;
62 m_bmpSelected = m_bmpSelectedOriginal = bitmap;
dfe1eee3 63
a4294b78 64 Widget parentWidget = (Widget) parent->GetClientWidget();
dfe1eee3 65
2d120f83
JS
66 /*
67 * Patch Note (important)
68 * There is no major reason to put a defaultButtonThickness here.
69 * Not requesting it give the ability to put wxButton with a spacing
70 * as small as requested. However, if some button become a DefaultButton,
71 * other buttons are no more aligned -- This is why we set
72 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
73 * in the ::SetDefaultButton method.
74 */
a4294b78 75 Widget buttonWidget = XtVaCreateManagedWidget ("button",
dfe1eee3 76
2d120f83 77 // Gadget causes problems for default button operation.
a4294b78 78#if wxUSE_GADGETS
2d120f83 79 xmPushButtonGadgetClass, parentWidget,
a4294b78 80#else
2d120f83 81 xmPushButtonWidgetClass, parentWidget,
a4294b78 82#endif
ea9868e8
MB
83 // See comment for wxButton::SetDefault
84 // XmNdefaultButtonShadowThickness, 1,
85 XmNrecomputeSize, False,
2d120f83 86 NULL);
dfe1eee3 87
a4294b78 88 m_mainWidget = (WXWidget) buttonWidget;
dfe1eee3 89
96be256b 90 ChangeFont(false);
dfe1eee3 91
321db4b6 92 ChangeBackgroundColour ();
dfe1eee3 93
321db4b6 94 DoSetBitmap();
dfe1eee3 95
ea9868e8
MB
96 XtAddCallback (buttonWidget,
97 XmNactivateCallback, (XtCallbackProc) wxButtonCallback,
98 (XtPointer) this);
dfe1eee3 99
ea9868e8
MB
100 wxSize best = m_bmpNormal.Ok() ? GetBestSize() : wxSize(30, 30);
101 if( size.x != -1 ) best.x = size.x;
102 if( size.y != -1 ) best.y = size.y;
103
104 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
105 pos.x, pos.y, best.x, best.y);
dfe1eee3 106
96be256b 107 return true;
a4294b78
JS
108}
109
110wxBitmapButton::~wxBitmapButton()
111{
112 SetBitmapLabel(wxNullBitmap);
dfe1eee3 113
a4294b78 114 if (m_insensPixmap)
ea9868e8
MB
115 XmDestroyPixmap (DefaultScreenOfDisplay ((Display*) GetXDisplay()),
116 (Pixmap) m_insensPixmap);
4bb6408c
JS
117}
118
119void wxBitmapButton::SetBitmapLabel(const wxBitmap& bitmap)
120{
ea9868e8
MB
121 m_bmpNormalOriginal = bitmap;
122 m_bmpNormal = bitmap;
dfe1eee3 123
321db4b6
JS
124 DoSetBitmap();
125}
126
127void wxBitmapButton::SetBitmapSelected(const wxBitmap& sel)
128{
ea9868e8
MB
129 m_bmpSelected = sel;
130 m_bmpSelectedOriginal = sel;
dfe1eee3 131
321db4b6
JS
132 DoSetBitmap();
133};
134
135void wxBitmapButton::SetBitmapFocus(const wxBitmap& focus)
136{
ea9868e8 137 m_bmpFocus = focus;
321db4b6
JS
138 // Not used in Motif
139};
140
141void wxBitmapButton::SetBitmapDisabled(const wxBitmap& disabled)
142{
ea9868e8
MB
143 m_bmpDisabled = disabled;
144 m_bmpDisabledOriginal = disabled;
dfe1eee3 145
321db4b6
JS
146 DoSetBitmap();
147};
148
149void wxBitmapButton::DoSetBitmap()
150{
ea9868e8 151 if (m_bmpNormalOriginal.Ok())
a4294b78 152 {
321db4b6
JS
153 Pixmap pixmap = 0;
154 Pixmap insensPixmap = 0;
155 Pixmap armPixmap = 0;
dfe1eee3 156
321db4b6
JS
157 // Must re-make the bitmap to have its transparent areas drawn
158 // in the current widget background colour.
ea9868e8 159 if (m_bmpNormalOriginal.GetMask())
321db4b6
JS
160 {
161 int backgroundPixel;
ea9868e8
MB
162 XtVaGetValues((Widget) m_mainWidget,
163 XmNbackground, &backgroundPixel,
164 NULL);
dfe1eee3 165
321db4b6
JS
166 wxColour col;
167 col.SetPixel(backgroundPixel);
dfe1eee3 168
ea9868e8
MB
169 wxBitmap newBitmap =
170 wxCreateMaskedBitmap(m_bmpNormalOriginal, col);
171 m_bmpNormal = newBitmap;
aae91497 172 m_bitmapCache.SetBitmap( m_bmpNormal );
dfe1eee3 173
aae0472b 174 pixmap = (Pixmap) m_bmpNormal.GetDrawable();
321db4b6 175 }
a4294b78 176 else
aae91497
MB
177 {
178 m_bitmapCache.SetBitmap( m_bmpNormal );
179 pixmap = (Pixmap) m_bitmapCache.GetLabelPixmap(m_mainWidget);
180 }
dfe1eee3 181
ea9868e8 182 if (m_bmpDisabledOriginal.Ok())
321db4b6 183 {
ea9868e8 184 if (m_bmpDisabledOriginal.GetMask())
321db4b6
JS
185 {
186 int backgroundPixel;
ea9868e8
MB
187 XtVaGetValues((Widget) m_mainWidget,
188 XmNbackground, &backgroundPixel,
189 NULL);
dfe1eee3 190
321db4b6
JS
191 wxColour col;
192 col.SetPixel(backgroundPixel);
dfe1eee3 193
ea9868e8
MB
194 wxBitmap newBitmap =
195 wxCreateMaskedBitmap(m_bmpDisabledOriginal, col);
196 m_bmpDisabled = newBitmap;
dfe1eee3 197
aae0472b 198 insensPixmap = (Pixmap) m_bmpDisabled.GetDrawable();
321db4b6
JS
199 }
200 else
aae91497 201 insensPixmap = (Pixmap) m_bitmapCache.GetInsensPixmap(m_mainWidget);
321db4b6 202 }
a4294b78 203 else
aae91497 204 insensPixmap = (Pixmap) m_bitmapCache.GetInsensPixmap(m_mainWidget);
dfe1eee3 205
321db4b6 206 // Now make the bitmap representing the armed state
ea9868e8 207 if (m_bmpSelectedOriginal.Ok())
a4294b78 208 {
ea9868e8 209 if (m_bmpSelectedOriginal.GetMask())
321db4b6
JS
210 {
211 int backgroundPixel;
ea9868e8
MB
212 XtVaGetValues((Widget) m_mainWidget,
213 XmNarmColor, &backgroundPixel,
214 NULL);
dfe1eee3 215
321db4b6
JS
216 wxColour col;
217 col.SetPixel(backgroundPixel);
dfe1eee3 218
ea9868e8
MB
219 wxBitmap newBitmap =
220 wxCreateMaskedBitmap(m_bmpSelectedOriginal, col);
221 m_bmpSelected = newBitmap;
dfe1eee3 222
aae0472b 223 armPixmap = (Pixmap) m_bmpSelected.GetDrawable();
321db4b6
JS
224 }
225 else
aae91497 226 armPixmap = (Pixmap) m_bitmapCache.GetArmPixmap(m_mainWidget);
321db4b6
JS
227 }
228 else
aae91497 229 armPixmap = (Pixmap) m_bitmapCache.GetArmPixmap(m_mainWidget);
dfe1eee3 230
a4294b78 231 XtVaSetValues ((Widget) m_mainWidget,
321db4b6
JS
232 XmNlabelPixmap, pixmap,
233 XmNlabelInsensitivePixmap, insensPixmap,
a4294b78
JS
234 XmNarmPixmap, armPixmap,
235 XmNlabelType, XmPIXMAP,
236 NULL);
237 }
238 else
239 {
240 // Null bitmap: must not use current pixmap
241 // since it is no longer valid.
242 XtVaSetValues ((Widget) m_mainWidget,
243 XmNlabelType, XmSTRING,
1a3ac83f 244 XmNlabelPixmap, XmUNSPECIFIED_PIXMAP,
2d120f83 245 XmNlabelInsensitivePixmap, XmUNSPECIFIED_PIXMAP,
321db4b6 246 XmNarmPixmap, XmUNSPECIFIED_PIXMAP,
a4294b78
JS
247 NULL);
248 }
4bb6408c
JS
249}
250
321db4b6 251void wxBitmapButton::ChangeBackgroundColour()
a4294b78 252{
96be256b 253 wxDoChangeBackgroundColour(m_mainWidget, m_backgroundColour, true);
dfe1eee3 254
321db4b6
JS
255 // Must reset the bitmaps since the colours have changed.
256 DoSetBitmap();
257}
401eb3de
RR
258
259wxSize wxBitmapButton::DoGetBestSize() const
260{
261 wxSize ret( 30,30 );
262
ea9868e8 263 if (m_bmpNormal.Ok())
401eb3de
RR
264 {
265 int border = (GetWindowStyle() & wxNO_BORDER) ? 4 : 10;
ea9868e8
MB
266 ret.x = m_bmpNormal.GetWidth()+border;
267 ret.y = m_bmpNormal.GetHeight()+border;
401eb3de
RR
268 }
269
270 return ret;
271}
272