]> git.saurik.com Git - wxWidgets.git/blob - src/x11/utilsx.cpp
Add GetDrawable convenience function that returns m_bitmap
[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 // ----------------------------------------------------------------------------
15 // wxXVisualInfo
16 // ----------------------------------------------------------------------------
17
18 #if !wxUSE_NANOX
19
20 bool wxFillXVisualInfo( wxXVisualInfo* vi, Display* dpy )
21 {
22 int xscreen = DefaultScreen( dpy );
23 Visual* vis = DefaultVisual( dpy, xscreen );
24 int bpp = DefaultDepth( dpy, xscreen );
25
26 XVisualInfo vinfo_template;
27 XVisualInfo *vinfo;
28
29 vinfo_template.visual = vis;
30 vinfo_template.visualid = XVisualIDFromVisual( vis );
31 vinfo_template.depth = bpp;
32 int nitem = 0;
33
34 vinfo = XGetVisualInfo( dpy, VisualIDMask|VisualDepthMask,
35 &vinfo_template, &nitem );
36
37 wxCHECK_MSG( vinfo, false, wxT("no visual") );
38
39 vi->Init( dpy, vinfo );
40
41 XFree(vinfo);
42
43 return true;
44 }
45
46 inline int ABS(int x) { return x < 0 ? -x : x; }
47
48 static void wxCalcPrecAndShift( unsigned long mask, int *shift, int *prec )
49 {
50 *shift = 0;
51 *prec = 0;
52
53 while (!(mask & 0x1))
54 {
55 (*shift)++;
56 mask >>= 1;
57 }
58
59 while (mask & 0x1)
60 {
61 (*prec)++;
62 mask >>= 1;
63 }
64 }
65
66 wxXVisualInfo::wxXVisualInfo()
67 {
68 m_visualColormap = NULL;
69 m_colorCube = NULL;
70 }
71
72 wxXVisualInfo::~wxXVisualInfo()
73 {
74 if (m_colorCube)
75 free( m_colorCube );
76
77 if (m_visualColormap)
78 delete [] (XColor*)m_visualColormap;
79 }
80
81 void wxXVisualInfo::Init( Display* dpy, XVisualInfo* vi )
82 {
83 m_visualType = vi->visual->c_class;
84 m_visualScreen = vi->screen;
85
86 m_visualRedMask = vi->red_mask;
87 m_visualGreenMask = vi->green_mask;
88 m_visualBlueMask = vi->blue_mask;
89
90 if (m_visualType != GrayScale && m_visualType != PseudoColor)
91 {
92 wxCalcPrecAndShift( m_visualRedMask, &m_visualRedShift,
93 &m_visualRedPrec );
94 wxCalcPrecAndShift( m_visualGreenMask, &m_visualGreenShift,
95 &m_visualGreenPrec );
96 wxCalcPrecAndShift( m_visualBlueMask, &m_visualBlueShift,
97 &m_visualBluePrec );
98 }
99
100 m_visualDepth = vi->depth;
101 if (vi->depth == 16)
102 vi->depth = m_visualRedPrec + m_visualGreenPrec + m_visualBluePrec;
103
104 m_visualColormapSize = vi->colormap_size;
105
106 if (m_visualDepth > 8)
107 return;
108
109 m_visualColormap = new XColor[m_visualColormapSize];
110 XColor* colors = (XColor*) m_visualColormap;
111
112 for (int i = 0; i < m_visualColormapSize; i++)
113 colors[i].pixel = i;
114
115 XQueryColors( dpy, DefaultColormap(dpy, vi->screen),
116 colors, m_visualColormapSize );
117
118 m_colorCube = (unsigned char*)malloc(32 * 32 * 32);
119
120 for (int r = 0; r < 32; r++)
121 {
122 for (int g = 0; g < 32; g++)
123 {
124 for (int b = 0; b < 32; b++)
125 {
126 int rr = (r << 3) | (r >> 2);
127 int gg = (g << 3) | (g >> 2);
128 int bb = (b << 3) | (b >> 2);
129
130 int index = -1;
131
132 if (colors)
133 {
134 int max = 3 * 65536;
135
136 for (int i = 0; i < m_visualColormapSize; i++)
137 {
138 int rdiff = ((rr << 8) - colors[i].red);
139 int gdiff = ((gg << 8) - colors[i].green);
140 int bdiff = ((bb << 8) - colors[i].blue);
141 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
142 if (sum < max)
143 {
144 index = i; max = sum;
145 }
146 }
147 }
148 else
149 {
150 // assume 8-bit true or static colors. this really exists
151 index = (r >> (5 - m_visualRedPrec)) << m_visualRedShift;
152 index |= (g >> (5 - m_visualGreenPrec)) << m_visualGreenShift;
153 index |= (b >> (5 - m_visualBluePrec)) << m_visualBlueShift;
154 }
155 m_colorCube[ (r*1024) + (g*32) + b ] = index;
156 }
157 }
158 }
159 }
160
161 #endif // !wxUSE_NANOX