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
17 #define XtDisplay XTDISPLAY
20 #include "wx/x11/privx.h"
25 #include "wx/bitmap.h"
26 #include "wx/region.h"
30 #include "wx/vms_x_fix.h"
31 #include <X11/shape.h>
33 #include <X11/extensions/shape.h>
36 #include "wx/dcmemory.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
45 bool wxDoSetShape( Display
* xdisplay
,
47 const wxRegion
& region
)
51 if( !XShapeQueryExtension( xdisplay
, &dummy1
, &dummy2
) )
54 if( region
.IsEmpty() )
56 XShapeCombineMask( xdisplay
, xwindow
, ShapeBounding
, 0, 0,
61 // wxRegion::ConvertToBitmap gives us the wrong Pixmap:
62 // polichrome and with black and whire reversed
63 wxRect box
= region
.GetBox();
64 wxBitmap
bmp(box
.GetRight(), box
.GetBottom(), 1);
67 dc
.SetBackground(*wxBLACK_BRUSH
);
69 dc
.SetClippingRegion(region
);
70 dc
.SetBackground(*wxWHITE_BRUSH
);
72 dc
.SelectObject(wxNullBitmap
);
74 XShapeCombineMask( xdisplay
, xwindow
, ShapeBounding
, 0, 0,
75 (Pixmap
)bmp
.GetDrawable(), ShapeSet
);
83 bool wxDoSetShape( Display
* WXUNUSED(xdisplay
),
84 Window
WXUNUSED(xwindow
),
85 const wxRegion
& WXUNUSED(region
) )
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
98 bool wxFillXVisualInfo( wxXVisualInfo
* vi
, Display
* dpy
)
100 int xscreen
= DefaultScreen( dpy
);
101 Visual
* vis
= DefaultVisual( dpy
, xscreen
);
102 int bpp
= DefaultDepth( dpy
, xscreen
);
104 XVisualInfo vinfo_template
;
107 vinfo_template
.visual
= vis
;
108 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
109 vinfo_template
.depth
= bpp
;
112 vinfo
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
,
113 &vinfo_template
, &nitem
);
115 wxCHECK_MSG( vinfo
, false, wxT("no visual") );
117 vi
->Init( dpy
, vinfo
);
124 inline int ABS(int x
) { return x
< 0 ? -x
: x
; }
126 static void wxCalcPrecAndShift( unsigned long mask
, int *shift
, int *prec
)
131 while (!(mask
& 0x1))
144 wxXVisualInfo::wxXVisualInfo()
146 m_visualColormap
= NULL
;
150 wxXVisualInfo::~wxXVisualInfo()
155 if (m_visualColormap
)
156 delete [] (XColor
*)m_visualColormap
;
159 void wxXVisualInfo::Init( Display
* dpy
, XVisualInfo
* vi
)
161 m_visualType
= vi
->visual
->c_class
;
162 m_visualScreen
= vi
->screen
;
164 m_visualRedMask
= vi
->red_mask
;
165 m_visualGreenMask
= vi
->green_mask
;
166 m_visualBlueMask
= vi
->blue_mask
;
168 if (m_visualType
!= GrayScale
&& m_visualType
!= PseudoColor
)
170 wxCalcPrecAndShift( m_visualRedMask
, &m_visualRedShift
,
172 wxCalcPrecAndShift( m_visualGreenMask
, &m_visualGreenShift
,
173 &m_visualGreenPrec
);
174 wxCalcPrecAndShift( m_visualBlueMask
, &m_visualBlueShift
,
178 m_visualDepth
= vi
->depth
;
180 vi
->depth
= m_visualRedPrec
+ m_visualGreenPrec
+ m_visualBluePrec
;
182 m_visualColormapSize
= vi
->colormap_size
;
184 if (m_visualDepth
> 8)
187 m_visualColormap
= new XColor
[m_visualColormapSize
];
188 XColor
* colors
= (XColor
*) m_visualColormap
;
190 for (int i
= 0; i
< m_visualColormapSize
; i
++)
193 XQueryColors( dpy
, DefaultColormap(dpy
, vi
->screen
),
194 colors
, m_visualColormapSize
);
196 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
198 for (int r
= 0; r
< 32; r
++)
200 for (int g
= 0; g
< 32; g
++)
202 for (int b
= 0; b
< 32; b
++)
204 int rr
= (r
<< 3) | (r
>> 2);
205 int gg
= (g
<< 3) | (g
>> 2);
206 int bb
= (b
<< 3) | (b
>> 2);
214 for (int i
= 0; i
< m_visualColormapSize
; i
++)
216 int rdiff
= ((rr
<< 8) - colors
[i
].red
);
217 int gdiff
= ((gg
<< 8) - colors
[i
].green
);
218 int bdiff
= ((bb
<< 8) - colors
[i
].blue
);
219 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
222 index
= i
; max
= sum
;
228 // assume 8-bit true or static colors. this really exists
229 index
= (r
>> (5 - m_visualRedPrec
)) << m_visualRedShift
;
230 index
|= (g
>> (5 - m_visualGreenPrec
)) << m_visualGreenShift
;
231 index
|= (b
>> (5 - m_visualBluePrec
)) << m_visualBlueShift
;
233 m_colorCube
[ (r
*1024) + (g
*32) + b
] = (unsigned char)index
;
239 #endif // !wxUSE_NANOX