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 /////////////////////////////////////////////////////////////////////////////
13 #define XShapeQueryExtension XSHAPEQUERYEXTENSION
14 #define XtDisplay XTDISPLAY
17 #include "wx/x11/privx.h"
21 # include "wx/vms_x_fix.h"
22 # include <X11/shape.h>
24 # include <X11/extensions/shape.h>
26 #include "wx/region.h"
27 #include "wx/bitmap.h"
28 #include "wx/dcmemory.h"
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
37 bool wxDoSetShape( Display
* xdisplay
,
39 const wxRegion
& region
)
43 if( !XShapeQueryExtension( xdisplay
, &dummy1
, &dummy2
) )
46 if( region
.IsEmpty() )
48 XShapeCombineMask( xdisplay
, xwindow
, ShapeBounding
, 0, 0,
53 // wxRegion::ConvertToBitmap gives us the wrong Pixmap:
54 // polichrome and with black and whire reversed
55 wxRect box
= region
.GetBox();
56 wxBitmap
bmp(box
.GetRight(), box
.GetBottom(), 1);
59 dc
.SetBackground(*wxBLACK_BRUSH
);
61 dc
.SetClippingRegion(region
);
62 dc
.SetBackground(*wxWHITE_BRUSH
);
64 dc
.SelectObject(wxNullBitmap
);
66 XShapeCombineMask( xdisplay
, xwindow
, ShapeBounding
, 0, 0,
67 (Pixmap
)bmp
.GetDrawable(), ShapeSet
);
75 bool wxDoSetShape( Display
* WXUNUSED(xdisplay
),
76 Window
WXUNUSED(xwindow
),
77 const wxRegion
& WXUNUSED(region
) )
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
90 bool wxFillXVisualInfo( wxXVisualInfo
* vi
, Display
* dpy
)
92 int xscreen
= DefaultScreen( dpy
);
93 Visual
* vis
= DefaultVisual( dpy
, xscreen
);
94 int bpp
= DefaultDepth( dpy
, xscreen
);
96 XVisualInfo vinfo_template
;
99 vinfo_template
.visual
= vis
;
100 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
101 vinfo_template
.depth
= bpp
;
104 vinfo
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
,
105 &vinfo_template
, &nitem
);
107 wxCHECK_MSG( vinfo
, false, wxT("no visual") );
109 vi
->Init( dpy
, vinfo
);
116 inline int ABS(int x
) { return x
< 0 ? -x
: x
; }
118 static void wxCalcPrecAndShift( unsigned long mask
, int *shift
, int *prec
)
123 while (!(mask
& 0x1))
136 wxXVisualInfo::wxXVisualInfo()
138 m_visualColormap
= NULL
;
142 wxXVisualInfo::~wxXVisualInfo()
147 if (m_visualColormap
)
148 delete [] (XColor
*)m_visualColormap
;
151 void wxXVisualInfo::Init( Display
* dpy
, XVisualInfo
* vi
)
153 m_visualType
= vi
->visual
->c_class
;
154 m_visualScreen
= vi
->screen
;
156 m_visualRedMask
= vi
->red_mask
;
157 m_visualGreenMask
= vi
->green_mask
;
158 m_visualBlueMask
= vi
->blue_mask
;
160 if (m_visualType
!= GrayScale
&& m_visualType
!= PseudoColor
)
162 wxCalcPrecAndShift( m_visualRedMask
, &m_visualRedShift
,
164 wxCalcPrecAndShift( m_visualGreenMask
, &m_visualGreenShift
,
165 &m_visualGreenPrec
);
166 wxCalcPrecAndShift( m_visualBlueMask
, &m_visualBlueShift
,
170 m_visualDepth
= vi
->depth
;
172 vi
->depth
= m_visualRedPrec
+ m_visualGreenPrec
+ m_visualBluePrec
;
174 m_visualColormapSize
= vi
->colormap_size
;
176 if (m_visualDepth
> 8)
179 m_visualColormap
= new XColor
[m_visualColormapSize
];
180 XColor
* colors
= (XColor
*) m_visualColormap
;
182 for (int i
= 0; i
< m_visualColormapSize
; i
++)
185 XQueryColors( dpy
, DefaultColormap(dpy
, vi
->screen
),
186 colors
, m_visualColormapSize
);
188 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
190 for (int r
= 0; r
< 32; r
++)
192 for (int g
= 0; g
< 32; g
++)
194 for (int b
= 0; b
< 32; b
++)
196 int rr
= (r
<< 3) | (r
>> 2);
197 int gg
= (g
<< 3) | (g
>> 2);
198 int bb
= (b
<< 3) | (b
>> 2);
206 for (int i
= 0; i
< m_visualColormapSize
; i
++)
208 int rdiff
= ((rr
<< 8) - colors
[i
].red
);
209 int gdiff
= ((gg
<< 8) - colors
[i
].green
);
210 int bdiff
= ((bb
<< 8) - colors
[i
].blue
);
211 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
214 index
= i
; max
= sum
;
220 // assume 8-bit true or static colors. this really exists
221 index
= (r
>> (5 - m_visualRedPrec
)) << m_visualRedShift
;
222 index
|= (g
>> (5 - m_visualGreenPrec
)) << m_visualGreenShift
;
223 index
|= (b
>> (5 - m_visualBluePrec
)) << m_visualBlueShift
;
225 m_colorCube
[ (r
*1024) + (g
*32) + b
] = index
;
231 #endif // !wxUSE_NANOX