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