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