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 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
16 #define XShapeQueryExtension XSHAPEQUERYEXTENSION
17 #define XtDisplay XTDISPLAY
20 #include "wx/x11/privx.h"
24 # include "wx/vms_x_fix.h"
25 # include <X11/shape.h>
27 # include <X11/extensions/shape.h>
29 #include "wx/region.h"
30 #include "wx/bitmap.h"
31 #include "wx/dcmemory.h"
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
40 bool wxDoSetShape( Display
* xdisplay
,
42 const wxRegion
& region
)
46 if( !XShapeQueryExtension( xdisplay
, &dummy1
, &dummy2
) )
49 if( region
.IsEmpty() )
51 XShapeCombineMask( xdisplay
, xwindow
, ShapeBounding
, 0, 0,
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);
62 dc
.SetBackground(*wxBLACK_BRUSH
);
64 dc
.SetClippingRegion(region
);
65 dc
.SetBackground(*wxWHITE_BRUSH
);
67 dc
.SelectObject(wxNullBitmap
);
69 XShapeCombineMask( xdisplay
, xwindow
, ShapeBounding
, 0, 0,
70 (Pixmap
)bmp
.GetDrawable(), ShapeSet
);
78 bool wxDoSetShape( Display
* WXUNUSED(xdisplay
),
79 Window
WXUNUSED(xwindow
),
80 const wxRegion
& WXUNUSED(region
) )
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
93 bool wxFillXVisualInfo( wxXVisualInfo
* vi
, Display
* dpy
)
95 int xscreen
= DefaultScreen( dpy
);
96 Visual
* vis
= DefaultVisual( dpy
, xscreen
);
97 int bpp
= DefaultDepth( dpy
, xscreen
);
99 XVisualInfo vinfo_template
;
102 vinfo_template
.visual
= vis
;
103 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
104 vinfo_template
.depth
= bpp
;
107 vinfo
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
,
108 &vinfo_template
, &nitem
);
110 wxCHECK_MSG( vinfo
, false, wxT("no visual") );
112 vi
->Init( dpy
, vinfo
);
119 inline int ABS(int x
) { return x
< 0 ? -x
: x
; }
121 static void wxCalcPrecAndShift( unsigned long mask
, int *shift
, int *prec
)
126 while (!(mask
& 0x1))
139 wxXVisualInfo::wxXVisualInfo()
141 m_visualColormap
= NULL
;
145 wxXVisualInfo::~wxXVisualInfo()
150 if (m_visualColormap
)
151 delete [] (XColor
*)m_visualColormap
;
154 void wxXVisualInfo::Init( Display
* dpy
, XVisualInfo
* vi
)
156 m_visualType
= vi
->visual
->c_class
;
157 m_visualScreen
= vi
->screen
;
159 m_visualRedMask
= vi
->red_mask
;
160 m_visualGreenMask
= vi
->green_mask
;
161 m_visualBlueMask
= vi
->blue_mask
;
163 if (m_visualType
!= GrayScale
&& m_visualType
!= PseudoColor
)
165 wxCalcPrecAndShift( m_visualRedMask
, &m_visualRedShift
,
167 wxCalcPrecAndShift( m_visualGreenMask
, &m_visualGreenShift
,
168 &m_visualGreenPrec
);
169 wxCalcPrecAndShift( m_visualBlueMask
, &m_visualBlueShift
,
173 m_visualDepth
= vi
->depth
;
175 vi
->depth
= m_visualRedPrec
+ m_visualGreenPrec
+ m_visualBluePrec
;
177 m_visualColormapSize
= vi
->colormap_size
;
179 if (m_visualDepth
> 8)
182 m_visualColormap
= new XColor
[m_visualColormapSize
];
183 XColor
* colors
= (XColor
*) m_visualColormap
;
185 for (int i
= 0; i
< m_visualColormapSize
; i
++)
188 XQueryColors( dpy
, DefaultColormap(dpy
, vi
->screen
),
189 colors
, m_visualColormapSize
);
191 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
193 for (int r
= 0; r
< 32; r
++)
195 for (int g
= 0; g
< 32; g
++)
197 for (int b
= 0; b
< 32; b
++)
199 int rr
= (r
<< 3) | (r
>> 2);
200 int gg
= (g
<< 3) | (g
>> 2);
201 int bb
= (b
<< 3) | (b
>> 2);
209 for (int i
= 0; i
< m_visualColormapSize
; i
++)
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
);
217 index
= i
; max
= sum
;
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
;
228 m_colorCube
[ (r
*1024) + (g
*32) + b
] = (unsigned char)index
;
234 #endif // !wxUSE_NANOX