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 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
44 bool wxDoSetShape( Display
* xdisplay
,
46 const wxRegion
& region
)
50 if( !XShapeQueryExtension( xdisplay
, &dummy1
, &dummy2
) )
53 if( region
.IsEmpty() )
55 XShapeCombineMask( xdisplay
, xwindow
, ShapeBounding
, 0, 0,
60 // wxRegion::ConvertToBitmap gives us the wrong Pixmap:
61 // polychrome and with black and white reversed
62 wxRect box
= region
.GetBox();
63 wxBitmap
bmp(box
.GetRight(), box
.GetBottom(), 1);
66 dc
.SetBackground(*wxBLACK_BRUSH
);
68 dc
.SetDeviceClippingRegion(region
);
69 dc
.SetBackground(*wxWHITE_BRUSH
);
71 dc
.SelectObject(wxNullBitmap
);
73 XShapeCombineMask( xdisplay
, xwindow
, ShapeBounding
, 0, 0,
74 (Pixmap
)bmp
.GetDrawable(), ShapeSet
);
82 bool wxDoSetShape( Display
* WXUNUSED(xdisplay
),
83 Window
WXUNUSED(xwindow
),
84 const wxRegion
& WXUNUSED(region
) )
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
97 bool wxFillXVisualInfo( wxXVisualInfo
* vi
, Display
* dpy
)
99 int xscreen
= DefaultScreen( dpy
);
100 Visual
* vis
= DefaultVisual( dpy
, xscreen
);
101 int bpp
= DefaultDepth( dpy
, xscreen
);
103 XVisualInfo vinfo_template
;
106 vinfo_template
.visual
= vis
;
107 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
108 vinfo_template
.depth
= bpp
;
111 vinfo
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
,
112 &vinfo_template
, &nitem
);
114 wxCHECK_MSG( vinfo
, false, wxT("no visual") );
116 vi
->Init( dpy
, vinfo
);
123 inline int ABS(int x
) { return x
< 0 ? -x
: x
; }
125 static void wxCalcPrecAndShift( unsigned long mask
, int *shift
, int *prec
)
130 while (!(mask
& 0x1))
143 wxXVisualInfo::wxXVisualInfo()
145 m_visualColormap
= NULL
;
149 wxXVisualInfo::~wxXVisualInfo()
154 if (m_visualColormap
)
155 delete [] (XColor
*)m_visualColormap
;
158 void wxXVisualInfo::Init( Display
* dpy
, XVisualInfo
* vi
)
160 m_visualType
= vi
->visual
->c_class
;
161 m_visualScreen
= vi
->screen
;
163 m_visualRedMask
= vi
->red_mask
;
164 m_visualGreenMask
= vi
->green_mask
;
165 m_visualBlueMask
= vi
->blue_mask
;
167 if (m_visualType
!= GrayScale
&& m_visualType
!= PseudoColor
)
169 wxCalcPrecAndShift( m_visualRedMask
, &m_visualRedShift
,
171 wxCalcPrecAndShift( m_visualGreenMask
, &m_visualGreenShift
,
172 &m_visualGreenPrec
);
173 wxCalcPrecAndShift( m_visualBlueMask
, &m_visualBlueShift
,
177 m_visualDepth
= vi
->depth
;
179 vi
->depth
= m_visualRedPrec
+ m_visualGreenPrec
+ m_visualBluePrec
;
181 m_visualColormapSize
= vi
->colormap_size
;
183 if (m_visualDepth
> 8)
186 m_visualColormap
= new XColor
[m_visualColormapSize
];
187 XColor
* colors
= (XColor
*) m_visualColormap
;
189 for (int i
= 0; i
< m_visualColormapSize
; i
++)
192 XQueryColors( dpy
, DefaultColormap(dpy
, vi
->screen
),
193 colors
, m_visualColormapSize
);
195 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
197 for (int r
= 0; r
< 32; r
++)
199 for (int g
= 0; g
< 32; g
++)
201 for (int b
= 0; b
< 32; b
++)
203 int rr
= (r
<< 3) | (r
>> 2);
204 int gg
= (g
<< 3) | (g
>> 2);
205 int bb
= (b
<< 3) | (b
>> 2);
213 for (int i
= 0; i
< m_visualColormapSize
; i
++)
215 int rdiff
= ((rr
<< 8) - colors
[i
].red
);
216 int gdiff
= ((gg
<< 8) - colors
[i
].green
);
217 int bdiff
= ((bb
<< 8) - colors
[i
].blue
);
218 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
221 index
= i
; max
= sum
;
227 // assume 8-bit true or static colors. this really exists
228 index
= (r
>> (5 - m_visualRedPrec
)) << m_visualRedShift
;
229 index
|= (g
>> (5 - m_visualGreenPrec
)) << m_visualGreenShift
;
230 index
|= (b
>> (5 - m_visualBluePrec
)) << m_visualBlueShift
;
232 m_colorCube
[ (r
*1024) + (g
*32) + b
] = (unsigned char)index
;
238 #endif // !wxUSE_NANOX