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 #include "wx/x11/private/wrapxkb.h"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
46 bool wxDoSetShape( Display
* xdisplay
,
48 const wxRegion
& region
)
52 if( !XShapeQueryExtension( xdisplay
, &dummy1
, &dummy2
) )
55 if( region
.IsEmpty() )
57 XShapeCombineMask( xdisplay
, xwindow
, ShapeBounding
, 0, 0,
62 // wxRegion::ConvertToBitmap gives us the wrong Pixmap:
63 // polychrome and with black and white reversed
64 wxRect box
= region
.GetBox();
65 wxBitmap
bmp(box
.GetRight(), box
.GetBottom(), 1);
68 dc
.SetBackground(*wxBLACK_BRUSH
);
70 dc
.SetDeviceClippingRegion(region
);
71 dc
.SetBackground(*wxWHITE_BRUSH
);
73 dc
.SelectObject(wxNullBitmap
);
75 XShapeCombineMask( xdisplay
, xwindow
, ShapeBounding
, 0, 0,
76 (Pixmap
)bmp
.GetDrawable(), ShapeSet
);
84 bool wxDoSetShape( Display
* WXUNUSED(xdisplay
),
85 Window
WXUNUSED(xwindow
),
86 const wxRegion
& WXUNUSED(region
) )
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
99 bool wxFillXVisualInfo( wxXVisualInfo
* vi
, Display
* dpy
)
101 int xscreen
= DefaultScreen( dpy
);
102 Visual
* vis
= DefaultVisual( dpy
, xscreen
);
103 int bpp
= DefaultDepth( dpy
, xscreen
);
105 XVisualInfo vinfo_template
;
108 vinfo_template
.visual
= vis
;
109 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
110 vinfo_template
.depth
= bpp
;
113 vinfo
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
,
114 &vinfo_template
, &nitem
);
116 wxCHECK_MSG( vinfo
, false, wxT("no visual") );
118 vi
->Init( dpy
, vinfo
);
125 inline int ABS(int x
) { return x
< 0 ? -x
: x
; }
127 static void wxCalcPrecAndShift( unsigned long mask
, int *shift
, int *prec
)
132 while (!(mask
& 0x1))
145 wxXVisualInfo::wxXVisualInfo()
147 m_visualColormap
= NULL
;
151 wxXVisualInfo::~wxXVisualInfo()
156 if (m_visualColormap
)
157 delete [] (XColor
*)m_visualColormap
;
160 void wxXVisualInfo::Init( Display
* dpy
, XVisualInfo
* vi
)
162 m_visualType
= vi
->visual
->c_class
;
163 m_visualScreen
= vi
->screen
;
165 m_visualRedMask
= vi
->red_mask
;
166 m_visualGreenMask
= vi
->green_mask
;
167 m_visualBlueMask
= vi
->blue_mask
;
169 if (m_visualType
!= GrayScale
&& m_visualType
!= PseudoColor
)
171 wxCalcPrecAndShift( m_visualRedMask
, &m_visualRedShift
,
173 wxCalcPrecAndShift( m_visualGreenMask
, &m_visualGreenShift
,
174 &m_visualGreenPrec
);
175 wxCalcPrecAndShift( m_visualBlueMask
, &m_visualBlueShift
,
179 m_visualDepth
= vi
->depth
;
181 vi
->depth
= m_visualRedPrec
+ m_visualGreenPrec
+ m_visualBluePrec
;
183 m_visualColormapSize
= vi
->colormap_size
;
185 if (m_visualDepth
> 8)
188 m_visualColormap
= new XColor
[m_visualColormapSize
];
189 XColor
* colors
= (XColor
*) m_visualColormap
;
191 for (int i
= 0; i
< m_visualColormapSize
; i
++)
194 XQueryColors( dpy
, DefaultColormap(dpy
, vi
->screen
),
195 colors
, m_visualColormapSize
);
197 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
199 for (int r
= 0; r
< 32; r
++)
201 for (int g
= 0; g
< 32; g
++)
203 for (int b
= 0; b
< 32; b
++)
205 int rr
= (r
<< 3) | (r
>> 2);
206 int gg
= (g
<< 3) | (g
>> 2);
207 int bb
= (b
<< 3) | (b
>> 2);
215 for (int i
= 0; i
< m_visualColormapSize
; i
++)
217 int rdiff
= ((rr
<< 8) - colors
[i
].red
);
218 int gdiff
= ((gg
<< 8) - colors
[i
].green
);
219 int bdiff
= ((bb
<< 8) - colors
[i
].blue
);
220 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
223 index
= i
; max
= sum
;
229 // assume 8-bit true or static colors. this really exists
230 index
= (r
>> (5 - m_visualRedPrec
)) << m_visualRedShift
;
231 index
|= (g
>> (5 - m_visualGreenPrec
)) << m_visualGreenShift
;
232 index
|= (b
>> (5 - m_visualBluePrec
)) << m_visualBlueShift
;
234 m_colorCube
[ (r
*1024) + (g
*32) + b
] = (unsigned char)index
;
240 #endif // !wxUSE_NANOX
242 /* Don't synthesize KeyUp events holding down a key and producing
243 KeyDown events with autorepeat. */
244 bool wxSetDetectableAutoRepeat( bool flag
)
246 #ifdef HAVE_X11_XKBLIB_H
248 XkbSetDetectableAutoRepeat( (Display
*)wxGetDisplay(), flag
, &result
);
249 return result
; /* true if keyboard hardware supports this mode */