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"
15 #include <X11/extensions/shape.h>
16 #include "wx/region.h"
17 #include "wx/bitmap.h"
18 #include "wx/dcmemory.h"
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
27 bool wxDoSetShape( Display
* xdisplay
,
29 const wxRegion
& region
)
33 if( !XShapeQueryExtension( xdisplay
, &dummy1
, &dummy2
) )
36 if( region
.IsEmpty() )
38 XShapeCombineMask( xdisplay
, xwindow
, ShapeBounding
, 0, 0,
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);
49 dc
.SetBackground(*wxBLACK_BRUSH
);
51 dc
.SetClippingRegion(region
);
52 dc
.SetBackground(*wxWHITE_BRUSH
);
54 dc
.SelectObject(wxNullBitmap
);
56 XShapeCombineMask( xdisplay
, xwindow
, ShapeBounding
, 0, 0,
57 (Pixmap
)bmp
.GetDrawable(), ShapeSet
);
65 bool wxDoSetShape( Display
* WXUNUSED(xdisplay
),
66 Window
WXUNUSED(xwindow
),
67 const wxRegion
& WXUNUSED(region
) )
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
80 bool wxFillXVisualInfo( wxXVisualInfo
* vi
, Display
* dpy
)
82 int xscreen
= DefaultScreen( dpy
);
83 Visual
* vis
= DefaultVisual( dpy
, xscreen
);
84 int bpp
= DefaultDepth( dpy
, xscreen
);
86 XVisualInfo vinfo_template
;
89 vinfo_template
.visual
= vis
;
90 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
91 vinfo_template
.depth
= bpp
;
94 vinfo
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
,
95 &vinfo_template
, &nitem
);
97 wxCHECK_MSG( vinfo
, false, wxT("no visual") );
99 vi
->Init( dpy
, vinfo
);
106 inline int ABS(int x
) { return x
< 0 ? -x
: x
; }
108 static void wxCalcPrecAndShift( unsigned long mask
, int *shift
, int *prec
)
113 while (!(mask
& 0x1))
126 wxXVisualInfo::wxXVisualInfo()
128 m_visualColormap
= NULL
;
132 wxXVisualInfo::~wxXVisualInfo()
137 if (m_visualColormap
)
138 delete [] (XColor
*)m_visualColormap
;
141 void wxXVisualInfo::Init( Display
* dpy
, XVisualInfo
* vi
)
143 m_visualType
= vi
->visual
->c_class
;
144 m_visualScreen
= vi
->screen
;
146 m_visualRedMask
= vi
->red_mask
;
147 m_visualGreenMask
= vi
->green_mask
;
148 m_visualBlueMask
= vi
->blue_mask
;
150 if (m_visualType
!= GrayScale
&& m_visualType
!= PseudoColor
)
152 wxCalcPrecAndShift( m_visualRedMask
, &m_visualRedShift
,
154 wxCalcPrecAndShift( m_visualGreenMask
, &m_visualGreenShift
,
155 &m_visualGreenPrec
);
156 wxCalcPrecAndShift( m_visualBlueMask
, &m_visualBlueShift
,
160 m_visualDepth
= vi
->depth
;
162 vi
->depth
= m_visualRedPrec
+ m_visualGreenPrec
+ m_visualBluePrec
;
164 m_visualColormapSize
= vi
->colormap_size
;
166 if (m_visualDepth
> 8)
169 m_visualColormap
= new XColor
[m_visualColormapSize
];
170 XColor
* colors
= (XColor
*) m_visualColormap
;
172 for (int i
= 0; i
< m_visualColormapSize
; i
++)
175 XQueryColors( dpy
, DefaultColormap(dpy
, vi
->screen
),
176 colors
, m_visualColormapSize
);
178 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
180 for (int r
= 0; r
< 32; r
++)
182 for (int g
= 0; g
< 32; g
++)
184 for (int b
= 0; b
< 32; b
++)
186 int rr
= (r
<< 3) | (r
>> 2);
187 int gg
= (g
<< 3) | (g
>> 2);
188 int bb
= (b
<< 3) | (b
>> 2);
196 for (int i
= 0; i
< m_visualColormapSize
; i
++)
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
);
204 index
= i
; max
= sum
;
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
;
215 m_colorCube
[ (r
*1024) + (g
*32) + b
] = index
;
221 #endif // !wxUSE_NANOX