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