]> git.saurik.com Git - wxWidgets.git/blob - src/x11/utilsx.cpp
support for wxUSE_PALETTE = 0
[wxWidgets.git] / src / x11 / utilsx.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/utilsx.cpp
3 // Purpose: Private functions common to X11 and Motif ports
4 // Author: Mattia Barbon
5 // Modified by:
6 // Created: 05/04/03
7 // RCS-ID: $Id$
8 // Copyright: (c) Mattia Barbon
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __VMS
13 #define XShapeQueryExtension XSHAPEQUERYEXTENSION
14 #define XtDisplay XTDISPLAY
15 #endif
16
17 #include "wx/x11/privx.h"
18
19 #ifdef HAVE_XSHAPE
20 #ifdef __VMS
21 # include <X11/shape.h>
22 #else
23 # include <X11/extensions/shape.h>
24 #endif
25 #include "wx/region.h"
26 #include "wx/bitmap.h"
27 #include "wx/dcmemory.h"
28 #endif
29
30 // ----------------------------------------------------------------------------
31 // XShape code
32 // ----------------------------------------------------------------------------
33
34 #ifdef HAVE_XSHAPE
35
36 bool wxDoSetShape( Display* xdisplay,
37 Window xwindow,
38 const wxRegion& region )
39 {
40 int dummy1, dummy2;
41
42 if( !XShapeQueryExtension( xdisplay, &dummy1, &dummy2 ) )
43 return false;
44
45 if( region.IsEmpty() )
46 {
47 XShapeCombineMask( xdisplay, xwindow, ShapeBounding, 0, 0,
48 None, ShapeSet );
49 }
50 else
51 {
52 // wxRegion::ConvertToBitmap gives us the wrong Pixmap:
53 // polichrome and with black and whire reversed
54 wxRect box = region.GetBox();
55 wxBitmap bmp(box.GetRight(), box.GetBottom(), 1);
56 wxMemoryDC dc;
57 dc.SelectObject(bmp);
58 dc.SetBackground(*wxBLACK_BRUSH);
59 dc.Clear();
60 dc.SetClippingRegion(region);
61 dc.SetBackground(*wxWHITE_BRUSH);
62 dc.Clear();
63 dc.SelectObject(wxNullBitmap);
64
65 XShapeCombineMask( xdisplay, xwindow, ShapeBounding, 0, 0,
66 (Pixmap)bmp.GetDrawable(), ShapeSet );
67 }
68
69 return true;
70 }
71
72 #else
73
74 bool wxDoSetShape( Display* WXUNUSED(xdisplay),
75 Window WXUNUSED(xwindow),
76 const wxRegion& WXUNUSED(region) )
77 {
78 return false;
79 }
80
81 #endif
82
83 // ----------------------------------------------------------------------------
84 // wxXVisualInfo
85 // ----------------------------------------------------------------------------
86
87 #if !wxUSE_NANOX
88
89 bool wxFillXVisualInfo( wxXVisualInfo* vi, Display* dpy )
90 {
91 int xscreen = DefaultScreen( dpy );
92 Visual* vis = DefaultVisual( dpy, xscreen );
93 int bpp = DefaultDepth( dpy, xscreen );
94
95 XVisualInfo vinfo_template;
96 XVisualInfo *vinfo;
97
98 vinfo_template.visual = vis;
99 vinfo_template.visualid = XVisualIDFromVisual( vis );
100 vinfo_template.depth = bpp;
101 int nitem = 0;
102
103 vinfo = XGetVisualInfo( dpy, VisualIDMask|VisualDepthMask,
104 &vinfo_template, &nitem );
105
106 wxCHECK_MSG( vinfo, false, wxT("no visual") );
107
108 vi->Init( dpy, vinfo );
109
110 XFree(vinfo);
111
112 return true;
113 }
114
115 inline int ABS(int x) { return x < 0 ? -x : x; }
116
117 static void wxCalcPrecAndShift( unsigned long mask, int *shift, int *prec )
118 {
119 *shift = 0;
120 *prec = 0;
121
122 while (!(mask & 0x1))
123 {
124 (*shift)++;
125 mask >>= 1;
126 }
127
128 while (mask & 0x1)
129 {
130 (*prec)++;
131 mask >>= 1;
132 }
133 }
134
135 wxXVisualInfo::wxXVisualInfo()
136 {
137 m_visualColormap = NULL;
138 m_colorCube = NULL;
139 }
140
141 wxXVisualInfo::~wxXVisualInfo()
142 {
143 if (m_colorCube)
144 free( m_colorCube );
145
146 if (m_visualColormap)
147 delete [] (XColor*)m_visualColormap;
148 }
149
150 void wxXVisualInfo::Init( Display* dpy, XVisualInfo* vi )
151 {
152 m_visualType = vi->visual->c_class;
153 m_visualScreen = vi->screen;
154
155 m_visualRedMask = vi->red_mask;
156 m_visualGreenMask = vi->green_mask;
157 m_visualBlueMask = vi->blue_mask;
158
159 if (m_visualType != GrayScale && m_visualType != PseudoColor)
160 {
161 wxCalcPrecAndShift( m_visualRedMask, &m_visualRedShift,
162 &m_visualRedPrec );
163 wxCalcPrecAndShift( m_visualGreenMask, &m_visualGreenShift,
164 &m_visualGreenPrec );
165 wxCalcPrecAndShift( m_visualBlueMask, &m_visualBlueShift,
166 &m_visualBluePrec );
167 }
168
169 m_visualDepth = vi->depth;
170 if (vi->depth == 16)
171 vi->depth = m_visualRedPrec + m_visualGreenPrec + m_visualBluePrec;
172
173 m_visualColormapSize = vi->colormap_size;
174
175 if (m_visualDepth > 8)
176 return;
177
178 m_visualColormap = new XColor[m_visualColormapSize];
179 XColor* colors = (XColor*) m_visualColormap;
180
181 for (int i = 0; i < m_visualColormapSize; i++)
182 colors[i].pixel = i;
183
184 XQueryColors( dpy, DefaultColormap(dpy, vi->screen),
185 colors, m_visualColormapSize );
186
187 m_colorCube = (unsigned char*)malloc(32 * 32 * 32);
188
189 for (int r = 0; r < 32; r++)
190 {
191 for (int g = 0; g < 32; g++)
192 {
193 for (int b = 0; b < 32; b++)
194 {
195 int rr = (r << 3) | (r >> 2);
196 int gg = (g << 3) | (g >> 2);
197 int bb = (b << 3) | (b >> 2);
198
199 int index = -1;
200
201 if (colors)
202 {
203 int max = 3 * 65536;
204
205 for (int i = 0; i < m_visualColormapSize; i++)
206 {
207 int rdiff = ((rr << 8) - colors[i].red);
208 int gdiff = ((gg << 8) - colors[i].green);
209 int bdiff = ((bb << 8) - colors[i].blue);
210 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
211 if (sum < max)
212 {
213 index = i; max = sum;
214 }
215 }
216 }
217 else
218 {
219 // assume 8-bit true or static colors. this really exists
220 index = (r >> (5 - m_visualRedPrec)) << m_visualRedShift;
221 index |= (g >> (5 - m_visualGreenPrec)) << m_visualGreenShift;
222 index |= (b >> (5 - m_visualBluePrec)) << m_visualBlueShift;
223 }
224 m_colorCube[ (r*1024) + (g*32) + b ] = index;
225 }
226 }
227 }
228 }
229
230 #endif // !wxUSE_NANOX