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