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