1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/utilsx.cpp
3 // Purpose: Private functions common to X11 and Motif ports
4 // Author: Mattia Barbon
8 // Copyright: (c) Mattia Barbon
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
16 #define XShapeQueryExtension XSHAPEQUERYEXTENSION
19 #include "wx/x11/privx.h"
24 #include "wx/bitmap.h"
25 #include "wx/region.h"
29 #include "wx/vms_x_fix.h"
30 #include <X11/shape.h>
32 #include <X11/extensions/shape.h>
35 #include "wx/dcmemory.h"
38 #ifdef HAVE_X11_XKBLIB_H
39 /* under HP-UX and Solaris 2.6, at least, XKBlib.h defines structures with
40 * field named "explicit" - which is, of course, an error for a C++
41 * compiler. To be on the safe side, just redefine it everywhere. */
42 #define explicit __wx_explicit
44 #include "X11/XKBlib.h"
47 #endif // HAVE_X11_XKBLIB_H
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
56 bool wxDoSetShape( Display
* xdisplay
,
58 const wxRegion
& region
)
62 if( !XShapeQueryExtension( xdisplay
, &dummy1
, &dummy2
) )
65 if( region
.IsEmpty() )
67 XShapeCombineMask( xdisplay
, xwindow
, ShapeBounding
, 0, 0,
72 // wxRegion::ConvertToBitmap gives us the wrong Pixmap:
73 // polychrome and with black and white reversed
74 wxRect box
= region
.GetBox();
75 wxBitmap
bmp(box
.GetRight(), box
.GetBottom(), 1);
78 dc
.SetBackground(*wxBLACK_BRUSH
);
80 dc
.SetDeviceClippingRegion(region
);
81 dc
.SetBackground(*wxWHITE_BRUSH
);
83 dc
.SelectObject(wxNullBitmap
);
85 XShapeCombineMask( xdisplay
, xwindow
, ShapeBounding
, 0, 0,
86 (Pixmap
)bmp
.GetDrawable(), ShapeSet
);
94 bool wxDoSetShape( Display
* WXUNUSED(xdisplay
),
95 Window
WXUNUSED(xwindow
),
96 const wxRegion
& WXUNUSED(region
) )
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
109 bool wxFillXVisualInfo( wxXVisualInfo
* vi
, Display
* dpy
)
111 int xscreen
= DefaultScreen( dpy
);
112 Visual
* vis
= DefaultVisual( dpy
, xscreen
);
113 int bpp
= DefaultDepth( dpy
, xscreen
);
115 XVisualInfo vinfo_template
;
118 vinfo_template
.visual
= vis
;
119 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
120 vinfo_template
.depth
= bpp
;
123 vinfo
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
,
124 &vinfo_template
, &nitem
);
126 wxCHECK_MSG( vinfo
, false, wxT("no visual") );
128 vi
->Init( dpy
, vinfo
);
135 inline int ABS(int x
) { return x
< 0 ? -x
: x
; }
137 static void wxCalcPrecAndShift( unsigned long mask
, int *shift
, int *prec
)
142 while (!(mask
& 0x1))
155 wxXVisualInfo::wxXVisualInfo()
157 m_visualColormap
= NULL
;
161 wxXVisualInfo::~wxXVisualInfo()
166 if (m_visualColormap
)
167 delete [] (XColor
*)m_visualColormap
;
170 void wxXVisualInfo::Init( Display
* dpy
, XVisualInfo
* vi
)
172 m_visualType
= vi
->visual
->c_class
;
173 m_visualScreen
= vi
->screen
;
175 m_visualRedMask
= vi
->red_mask
;
176 m_visualGreenMask
= vi
->green_mask
;
177 m_visualBlueMask
= vi
->blue_mask
;
179 if (m_visualType
!= GrayScale
&& m_visualType
!= PseudoColor
)
181 wxCalcPrecAndShift( m_visualRedMask
, &m_visualRedShift
,
183 wxCalcPrecAndShift( m_visualGreenMask
, &m_visualGreenShift
,
184 &m_visualGreenPrec
);
185 wxCalcPrecAndShift( m_visualBlueMask
, &m_visualBlueShift
,
189 m_visualDepth
= vi
->depth
;
191 vi
->depth
= m_visualRedPrec
+ m_visualGreenPrec
+ m_visualBluePrec
;
193 m_visualColormapSize
= vi
->colormap_size
;
195 if (m_visualDepth
> 8)
198 m_visualColormap
= new XColor
[m_visualColormapSize
];
199 XColor
* colors
= (XColor
*) m_visualColormap
;
201 for (int i
= 0; i
< m_visualColormapSize
; i
++)
204 XQueryColors( dpy
, DefaultColormap(dpy
, vi
->screen
),
205 colors
, m_visualColormapSize
);
207 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
209 for (int r
= 0; r
< 32; r
++)
211 for (int g
= 0; g
< 32; g
++)
213 for (int b
= 0; b
< 32; b
++)
215 int rr
= (r
<< 3) | (r
>> 2);
216 int gg
= (g
<< 3) | (g
>> 2);
217 int bb
= (b
<< 3) | (b
>> 2);
225 for (int i
= 0; i
< m_visualColormapSize
; i
++)
227 int rdiff
= ((rr
<< 8) - colors
[i
].red
);
228 int gdiff
= ((gg
<< 8) - colors
[i
].green
);
229 int bdiff
= ((bb
<< 8) - colors
[i
].blue
);
230 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
233 index
= i
; max
= sum
;
239 // assume 8-bit true or static colors. this really exists
240 index
= (r
>> (5 - m_visualRedPrec
)) << m_visualRedShift
;
241 index
|= (g
>> (5 - m_visualGreenPrec
)) << m_visualGreenShift
;
242 index
|= (b
>> (5 - m_visualBluePrec
)) << m_visualBlueShift
;
244 m_colorCube
[ (r
*1024) + (g
*32) + b
] = (unsigned char)index
;
250 #endif // !wxUSE_NANOX
252 /* Don't synthesize KeyUp events holding down a key and producing
253 KeyDown events with autorepeat. */
254 bool wxSetDetectableAutoRepeat( bool flag
)
256 #ifdef HAVE_X11_XKBLIB_H
258 XkbSetDetectableAutoRepeat( (Display
*)wxGetDisplay(), flag
, &result
);
259 return result
; /* true if keyboard hardware supports this mode */