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