1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/utilsx.cpp
3 // Purpose: Private functions common to X11 and Motif ports
4 // Author: Mattia Barbon
8 // Copyright: (c) Mattia Barbon
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/x11/privx.h"
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
20 bool wxFillXVisualInfo( wxXVisualInfo
* vi
, Display
* dpy
)
22 int xscreen
= DefaultScreen( dpy
);
23 Visual
* vis
= DefaultVisual( dpy
, xscreen
);
24 int bpp
= DefaultDepth( dpy
, xscreen
);
26 XVisualInfo vinfo_template
;
29 vinfo_template
.visual
= vis
;
30 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
31 vinfo_template
.depth
= bpp
;
34 vinfo
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
,
35 &vinfo_template
, &nitem
);
37 wxCHECK_MSG( vinfo
, false, wxT("no visual") );
39 vi
->Init( dpy
, vinfo
);
46 inline int ABS(int x
) { return x
< 0 ? -x
: x
; }
48 static void wxCalcPrecAndShift( unsigned long mask
, int *shift
, int *prec
)
66 wxXVisualInfo::wxXVisualInfo()
68 m_visualColormap
= NULL
;
72 wxXVisualInfo::~wxXVisualInfo()
78 delete [] (XColor
*)m_visualColormap
;
81 void wxXVisualInfo::Init( Display
* dpy
, XVisualInfo
* vi
)
83 m_visualType
= vi
->visual
->c_class
;
84 m_visualScreen
= vi
->screen
;
86 m_visualRedMask
= vi
->red_mask
;
87 m_visualGreenMask
= vi
->green_mask
;
88 m_visualBlueMask
= vi
->blue_mask
;
90 if (m_visualType
!= GrayScale
&& m_visualType
!= PseudoColor
)
92 wxCalcPrecAndShift( m_visualRedMask
, &m_visualRedShift
,
94 wxCalcPrecAndShift( m_visualGreenMask
, &m_visualGreenShift
,
96 wxCalcPrecAndShift( m_visualBlueMask
, &m_visualBlueShift
,
100 m_visualDepth
= vi
->depth
;
102 vi
->depth
= m_visualRedPrec
+ m_visualGreenPrec
+ m_visualBluePrec
;
104 m_visualColormapSize
= vi
->colormap_size
;
106 if (m_visualDepth
> 8)
109 m_visualColormap
= new XColor
[m_visualColormapSize
];
110 XColor
* colors
= (XColor
*) m_visualColormap
;
112 for (int i
= 0; i
< m_visualColormapSize
; i
++)
115 XQueryColors( dpy
, DefaultColormap(dpy
, vi
->screen
),
116 colors
, m_visualColormapSize
);
118 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
120 for (int r
= 0; r
< 32; r
++)
122 for (int g
= 0; g
< 32; g
++)
124 for (int b
= 0; b
< 32; b
++)
126 int rr
= (r
<< 3) | (r
>> 2);
127 int gg
= (g
<< 3) | (g
>> 2);
128 int bb
= (b
<< 3) | (b
>> 2);
136 for (int i
= 0; i
< m_visualColormapSize
; i
++)
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
);
144 index
= i
; max
= sum
;
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
;
155 m_colorCube
[ (r
*1024) + (g
*32) + b
] = index
;
161 #endif // !wxUSE_NANOX