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 /////////////////////////////////////////////////////////////////////////////
13 #define XShapeQueryExtension XSHAPEQUERYEXTENSION
14 #define XtDisplay XTDISPLAY
17 #include "wx/x11/privx.h"
21 # include <X11/shape.h>
23 # include <X11/extensions/shape.h>
25 #include "wx/region.h"
26 #include "wx/bitmap.h"
27 #include "wx/dcmemory.h"
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
36 bool wxDoSetShape( Display
* xdisplay
,
38 const wxRegion
& region
)
42 if( !XShapeQueryExtension( xdisplay
, &dummy1
, &dummy2
) )
45 if( region
.IsEmpty() )
47 XShapeCombineMask( xdisplay
, xwindow
, ShapeBounding
, 0, 0,
52 // wxRegion::ConvertToBitmap gives us the wrong Pixmap:
53 // polichrome and with black and whire reversed
54 wxRect box
= region
.GetBox();
55 wxBitmap
bmp(box
.GetRight(), box
.GetBottom(), 1);
58 dc
.SetBackground(*wxBLACK_BRUSH
);
60 dc
.SetClippingRegion(region
);
61 dc
.SetBackground(*wxWHITE_BRUSH
);
63 dc
.SelectObject(wxNullBitmap
);
65 XShapeCombineMask( xdisplay
, xwindow
, ShapeBounding
, 0, 0,
66 (Pixmap
)bmp
.GetDrawable(), ShapeSet
);
74 bool wxDoSetShape( Display
* WXUNUSED(xdisplay
),
75 Window
WXUNUSED(xwindow
),
76 const wxRegion
& WXUNUSED(region
) )
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
89 bool wxFillXVisualInfo( wxXVisualInfo
* vi
, Display
* dpy
)
91 int xscreen
= DefaultScreen( dpy
);
92 Visual
* vis
= DefaultVisual( dpy
, xscreen
);
93 int bpp
= DefaultDepth( dpy
, xscreen
);
95 XVisualInfo vinfo_template
;
98 vinfo_template
.visual
= vis
;
99 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
100 vinfo_template
.depth
= bpp
;
103 vinfo
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
,
104 &vinfo_template
, &nitem
);
106 wxCHECK_MSG( vinfo
, false, wxT("no visual") );
108 vi
->Init( dpy
, vinfo
);
115 inline int ABS(int x
) { return x
< 0 ? -x
: x
; }
117 static void wxCalcPrecAndShift( unsigned long mask
, int *shift
, int *prec
)
122 while (!(mask
& 0x1))
135 wxXVisualInfo::wxXVisualInfo()
137 m_visualColormap
= NULL
;
141 wxXVisualInfo::~wxXVisualInfo()
146 if (m_visualColormap
)
147 delete [] (XColor
*)m_visualColormap
;
150 void wxXVisualInfo::Init( Display
* dpy
, XVisualInfo
* vi
)
152 m_visualType
= vi
->visual
->c_class
;
153 m_visualScreen
= vi
->screen
;
155 m_visualRedMask
= vi
->red_mask
;
156 m_visualGreenMask
= vi
->green_mask
;
157 m_visualBlueMask
= vi
->blue_mask
;
159 if (m_visualType
!= GrayScale
&& m_visualType
!= PseudoColor
)
161 wxCalcPrecAndShift( m_visualRedMask
, &m_visualRedShift
,
163 wxCalcPrecAndShift( m_visualGreenMask
, &m_visualGreenShift
,
164 &m_visualGreenPrec
);
165 wxCalcPrecAndShift( m_visualBlueMask
, &m_visualBlueShift
,
169 m_visualDepth
= vi
->depth
;
171 vi
->depth
= m_visualRedPrec
+ m_visualGreenPrec
+ m_visualBluePrec
;
173 m_visualColormapSize
= vi
->colormap_size
;
175 if (m_visualDepth
> 8)
178 m_visualColormap
= new XColor
[m_visualColormapSize
];
179 XColor
* colors
= (XColor
*) m_visualColormap
;
181 for (int i
= 0; i
< m_visualColormapSize
; i
++)
184 XQueryColors( dpy
, DefaultColormap(dpy
, vi
->screen
),
185 colors
, m_visualColormapSize
);
187 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
189 for (int r
= 0; r
< 32; r
++)
191 for (int g
= 0; g
< 32; g
++)
193 for (int b
= 0; b
< 32; b
++)
195 int rr
= (r
<< 3) | (r
>> 2);
196 int gg
= (g
<< 3) | (g
>> 2);
197 int bb
= (b
<< 3) | (b
>> 2);
205 for (int i
= 0; i
< m_visualColormapSize
; i
++)
207 int rdiff
= ((rr
<< 8) - colors
[i
].red
);
208 int gdiff
= ((gg
<< 8) - colors
[i
].green
);
209 int bdiff
= ((bb
<< 8) - colors
[i
].blue
);
210 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
213 index
= i
; max
= sum
;
219 // assume 8-bit true or static colors. this really exists
220 index
= (r
>> (5 - m_visualRedPrec
)) << m_visualRedShift
;
221 index
|= (g
>> (5 - m_visualGreenPrec
)) << m_visualGreenShift
;
222 index
|= (b
>> (5 - m_visualBluePrec
)) << m_visualBlueShift
;
224 m_colorCube
[ (r
*1024) + (g
*32) + b
] = index
;
230 #endif // !wxUSE_NANOX