Removed a deprecated wxBitmap constructor, and some
[wxWidgets.git] / src / motif / bmpmotif.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: bitmap.cpp
3 // Purpose: wxBitmap
4 // Author: Julian Smart, originally in bitmap.cpp
5 // Modified by:
6 // Created: 25/03/2003
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "bmpmotif.h"
14 #endif
15
16 #ifdef __VMS
17 #define XtParent XTPARENT
18 #endif
19
20 #include "wx/defs.h"
21 #include "wx/motif/bmpmotif.h"
22
23 #ifdef __VMS__
24 #pragma message disable nosimpint
25 #endif
26 #include <Xm/Xm.h>
27 #ifdef __VMS__
28 #pragma message enable nosimpint
29 #endif
30
31 #include "wx/motif/private.h"
32
33 #if wxHAVE_LIB_XPM
34 #include <X11/xpm.h>
35 #endif
36 #include <math.h>
37
38 Pixmap XCreateInsensitivePixmap( Display *display, Pixmap pixmap );
39
40 wxBitmapCache::~wxBitmapCache()
41 {
42 if( m_display )
43 {
44 Screen* screen = DefaultScreenOfDisplay( (Display*)m_display );
45
46 if( m_labelPixmap )
47 XmDestroyPixmap( screen, (Pixmap)m_labelPixmap );
48
49 if( m_armPixmap )
50 XmDestroyPixmap( screen, (Pixmap)m_armPixmap );
51
52 if( m_insensPixmap )
53 XmDestroyPixmap( screen, (Pixmap)m_insensPixmap );
54
55 }
56
57 if( m_image )
58 {
59 XmUninstallImage( (XImage*)m_image );
60 XtFree( (char*)(XImage*)m_image );
61 }
62 }
63
64 void wxBitmapCache::SetBitmap( const wxBitmap& bitmap )
65 {
66 if( m_bitmap != bitmap )
67 {
68 InvalidateCache();
69 m_bitmap = bitmap;
70
71 if( m_image )
72 {
73 XmUninstallImage( (XImage*)m_image );
74 XtFree( (char*)(XImage*)m_image );
75 m_image = (WXImage*)NULL;
76 }
77 }
78 }
79
80 void wxBitmapCache::InvalidateCache()
81 {
82 m_recalcPixmaps.label = true;
83 m_recalcPixmaps.arm = true;
84 m_recalcPixmaps.insens = true;
85 }
86
87 void wxBitmapCache::SetColoursChanged()
88 {
89 InvalidateCache();
90 }
91
92 void wxBitmapCache::CreateImageIfNeeded( WXWidget w )
93 {
94 if( m_image )
95 return;
96
97 m_display = w ?
98 (WXDisplay*)XtDisplay( (Widget)w ) :
99 (WXDisplay*)wxGetDisplay();
100
101 XImage *ximage = XGetImage( (Display*)m_display,
102 (Drawable)m_bitmap.GetPixmap(),
103 0, 0,
104 m_bitmap.GetWidth(), m_bitmap.GetHeight(),
105 AllPlanes, ZPixmap );
106
107 m_image = (WXImage*)ximage;
108
109 if( m_image )
110 {
111 char tmp[128];
112 sprintf( tmp, "Im%x", (unsigned int)ximage );
113 XmInstallImage( ximage, tmp );
114 }
115 }
116
117 WXPixmap wxBitmapCache::GetLabelPixmap( WXWidget w )
118 {
119 if( m_labelPixmap && !m_recalcPixmaps.label )
120 return m_labelPixmap;
121
122 CreateImageIfNeeded( w );
123
124 Screen* screen = DefaultScreenOfDisplay( (Display*)m_display );
125
126 if( m_labelPixmap )
127 XmDestroyPixmap( screen, (Pixmap)m_labelPixmap );
128
129 if( !m_image )
130 return (WXPixmap)NULL;
131
132 char tmp[128];
133 sprintf( tmp, "Im%x", (unsigned int)m_image );
134
135 Pixel fg, bg;
136 Widget widget = (Widget)w;
137
138 while( XmIsGadget( widget ) )
139 widget = XtParent( widget );
140 XtVaGetValues( widget,
141 XmNbackground, &bg,
142 XmNforeground, &fg,
143 NULL );
144
145 m_labelPixmap = (WXPixmap)XmGetPixmap( screen, tmp, fg, bg );
146
147 m_recalcPixmaps.label = !m_labelPixmap;
148 return m_labelPixmap;
149 }
150
151 WXPixmap wxBitmapCache::GetArmPixmap( WXWidget w )
152 {
153 if( m_armPixmap && !m_recalcPixmaps.arm )
154 return m_armPixmap;
155
156 CreateImageIfNeeded( w );
157
158 Screen* screen = DefaultScreenOfDisplay( (Display*)m_display );
159
160 if( m_armPixmap )
161 XmDestroyPixmap( screen, (Pixmap)m_armPixmap );
162
163 if( !m_image )
164 return (WXPixmap)NULL;
165
166 char tmp[128];
167 sprintf( tmp, "Im%x", (unsigned int)m_image );
168
169 Pixel fg, bg;
170 Widget widget = (Widget) w;
171
172 XtVaGetValues( widget, XmNarmColor, &bg, NULL );
173 while( XmIsGadget( widget ) )
174 widget = XtParent( widget );
175 XtVaGetValues( widget, XmNforeground, &fg, NULL );
176
177 m_armPixmap = (WXPixmap)XmGetPixmap( screen, tmp, fg, bg );
178
179 m_recalcPixmaps.arm = !m_armPixmap;
180 return m_armPixmap;
181 }
182
183 WXPixmap wxBitmapCache::GetInsensPixmap( WXWidget w )
184 {
185 if( m_insensPixmap && !m_recalcPixmaps.insens )
186 return m_insensPixmap;
187
188 CreateImageIfNeeded( w );
189
190 Screen* screen = DefaultScreenOfDisplay( (Display*)m_display );
191
192 if( m_insensPixmap )
193 XmDestroyPixmap( screen, (Pixmap)m_insensPixmap );
194
195 if( !m_image )
196 return (WXPixmap)NULL;
197
198 m_insensPixmap =
199 (WXPixmap)XCreateInsensitivePixmap( (Display*)m_display,
200 (Pixmap)m_bitmap.GetPixmap() );
201
202 m_recalcPixmaps.insens = !m_insensPixmap;
203 return m_insensPixmap;
204 }
205
206 //////////////////////////////////////////////////////////////////////////////
207 // Utility function
208 //////////////////////////////////////////////////////////////////////////////
209
210 /****************************************************************************
211
212 NAME
213 XCreateInsensitivePixmap - create a grayed-out copy of a pixmap
214
215 SYNOPSIS
216 Pixmap XCreateInsensitivePixmap( Display *display, Pixmap pixmap )
217
218 DESCRIPTION
219 This function creates a grayed-out copy of the argument pixmap, suitable
220 for use as a XmLabel's XmNlabelInsensitivePixmap resource.
221
222 RETURN VALUES
223 The return value is the new Pixmap id or zero on error. Errors include
224 a NULL display argument or an invalid Pixmap argument.
225
226 ERRORS
227 If one of the XLib functions fail, it will produce a X error. The
228 default X error handler prints a diagnostic and calls exit().
229
230 SEE ALSO
231 XCopyArea(3), XCreateBitmapFromData(3), XCreateGC(3), XCreatePixmap(3),
232 XFillRectangle(3), exit(2)
233
234 AUTHOR
235 John R Veregge - john@puente.jpl.nasa.gov
236 Advanced Engineering and Prototyping Group (AEG)
237 Information Systems Technology Section (395)
238 Jet Propulsion Lab - Calif Institute of Technology
239
240 *****************************************************************************/
241
242 Pixmap
243 XCreateInsensitivePixmap( Display *display, Pixmap pixmap )
244
245 {
246 static char stipple_data[] =
247 {
248 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA,
249 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA,
250 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA,
251 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA
252 };
253 GC gc;
254 Pixmap ipixmap, stipple;
255 unsigned width, height, depth;
256
257 Window window; /* These return values */
258 unsigned border; /* from XGetGeometry() */
259 int x, y; /* are not needed. */
260
261 ipixmap = 0;
262
263 if ( NULL == display || 0 == pixmap )
264 return ipixmap;
265
266 if ( 0 == XGetGeometry( display, pixmap, &window, &x, &y,
267 &width, &height, &border, &depth )
268 )
269 return ipixmap; /* BadDrawable: probably an invalid pixmap */
270
271 /* Get the stipple pixmap to be used to 'gray-out' the argument pixmap.
272 */
273 stipple = XCreateBitmapFromData( display, pixmap, stipple_data, 16, 16 );
274 if ( 0 != stipple )
275 {
276 gc = XCreateGC( display, pixmap, (XtGCMask)0, (XGCValues*)NULL );
277 if ( NULL != gc )
278 {
279 /* Create an identical copy of the argument pixmap.
280 */
281 ipixmap = XCreatePixmap( display, pixmap, width, height, depth );
282 if ( 0 != ipixmap )
283 {
284 /* Copy the argument pixmap into the new pixmap.
285 */
286 XCopyArea( display, pixmap, ipixmap,
287 gc, 0, 0, width, height, 0, 0 );
288
289 /* Refill the new pixmap using the stipple algorithm/pixmap.
290 */
291 XSetStipple( display, gc, stipple );
292 XSetFillStyle( display, gc, FillStippled );
293 XFillRectangle( display, ipixmap, gc, 0, 0, width, height );
294 }
295 XFreeGC( display, gc );
296 }
297 XFreePixmap( display, stipple );
298 }
299 return ipixmap;
300 }