]> git.saurik.com Git - wxWidgets.git/blob - src/unix/displayx11.cpp
Add wx.ListBox.HitTest
[wxWidgets.git] / src / unix / displayx11.cpp
1 ///////////////////////////////////////////////////////////////////////////
2 // Name: displayx11.cpp
3 // Purpose: Unix/X11 implementation of wxDisplay class
4 // Author: Brian Victor
5 // Modified by:
6 // Created: 12/05/02
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #include "wx/display.h"
20 #include "wx/intl.h"
21 #include "wx/log.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/dynarray.h"
25 #include "wx/gdicmn.h"
26 #include "wx/string.h"
27 #include "wx/utils.h"
28 #endif /* WX_PRECOMP */
29
30 #if wxUSE_DISPLAY
31
32 /* These must be included after the wx files. Otherwise the Data macro in
33 * Xlibint.h conflicts with a function declaration in wx/list.h. */
34 extern "C" {
35 #include <X11/Xlib.h>
36 #ifndef __VMS
37 #include <X11/Xlibint.h>
38 # include <X11/extensions/Xinerama.h>
39 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
40 #include <X11/extensions/xf86vmode.h>
41 #endif
42 #endif
43 }
44
45 class wxDisplayUnixPriv
46 {
47 public:
48 wxRect m_rect;
49 int m_depth;
50 };
51
52 size_t wxDisplayBase::GetCount()
53 {
54 Display *disp = (Display*)wxGetDisplay();
55
56 #ifndef __VMS
57 if ( XineramaIsActive(disp) )
58 {
59 XineramaScreenInfo *screenarr;
60 int numscreens;
61 screenarr = XineramaQueryScreens(disp, &numscreens);
62 XFree(screenarr);
63 return numscreens;
64 }
65 else
66 #endif
67 {
68 return 1;
69 }
70 }
71
72 int wxDisplayBase::GetFromPoint(const wxPoint &p)
73 {
74 Display *disp = (Display*)wxGetDisplay();
75
76 #ifndef __VMS
77 if ( XineramaIsActive(disp) )
78 {
79 int which_screen = -1;
80 XineramaScreenInfo *screenarr;
81 int numscreens;
82 screenarr = XineramaQueryScreens(disp, &numscreens);
83
84 int i;
85 for (i = 0; i < numscreens; ++i)
86 {
87 if (p.x >= screenarr[i].x_org &&
88 p.x < screenarr[i].x_org + screenarr[i].width &&
89 p.y >= screenarr[i].y_org &&
90 p.y < screenarr[i].y_org + screenarr[i].height)
91 {
92 which_screen = i;
93 }
94 }
95
96 XFree(screenarr);
97 return which_screen;
98 }
99 else
100 #endif
101 {
102 wxSize size = wxGetDisplaySize();
103 if (p.x >= 0 &&
104 p.x < size.GetWidth() &&
105 p.y >= 0 &&
106 p.y < size.GetHeight())
107 {
108 return 0;
109 }
110
111 return -1;
112 }
113 }
114
115 wxDisplay::wxDisplay(size_t index) : wxDisplayBase ( index ), m_priv( new wxDisplayUnixPriv )
116 {
117 Display *disp = (Display*)wxGetDisplay();
118
119 #ifndef __VMS
120 if ( XineramaIsActive(disp) )
121 {
122 XineramaScreenInfo *screenarr;
123 int numscreens;
124 screenarr = XineramaQueryScreens(disp, &numscreens);
125
126 m_priv->m_rect = wxRect(screenarr[index].x_org, screenarr[index].y_org,
127 screenarr[index].width, screenarr[index].height);
128 m_priv->m_depth = DefaultDepth(disp, DefaultScreen(disp));
129 XFree(screenarr);
130 }
131 else
132 #endif
133 {
134 wxSize size = wxGetDisplaySize();
135 m_priv->m_rect = wxRect(0, 0, size.GetWidth(), size.GetHeight());
136 m_priv->m_depth = wxDisplayDepth();
137 }
138 }
139
140 wxDisplay::~wxDisplay()
141 {
142 delete m_priv;
143 }
144
145 wxRect wxDisplay::GetGeometry() const
146 {
147 return m_priv->m_rect;
148 }
149
150 int wxDisplay::GetDepth() const
151 {
152 return m_priv->m_depth;
153 }
154
155 wxString wxDisplay::GetName() const
156 {
157 return wxEmptyString;
158 }
159
160
161 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
162
163 //
164 // See (http://www.xfree86.org/4.2.0/XF86VidModeDeleteModeLine.3.html) for more
165 // info about xf86 video mode extensions
166 //
167
168 //free private data common to x (usually s3) servers
169 #define wxClearXVM(vm) if(vm.privsize) XFree(vm.c_private)
170
171 // Correct res rate from GLFW
172 #define wxCRR2(v,dc) (int) (((1000.0f * (float) dc) /*PIXELS PER SECOND */) / ((float) v.htotal * v.vtotal /*PIXELS PER FRAME*/) + 0.5f)
173 #define wxCRR(v) wxCRR2(v,v.dotclock)
174 #define wxCVM2(v, dc) wxVideoMode(v.hdisplay, v.vdisplay, DefaultDepth((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay())), wxCRR2(v,dc))
175 #define wxCVM(v) wxCVM2(v, v.dotclock)
176
177 wxArrayVideoModes wxDisplay::GetModes(const wxVideoMode& mode) const
178 {
179 //Convenience...
180 Display* pDisplay = (Display*) wxGetDisplay(); //default display
181 int nScreen = DefaultScreen(pDisplay); //default screen of (default) display...
182
183 //Some variables..
184 XF86VidModeModeInfo** ppXModes; //Enumerated Modes (Don't forget XFree() :))
185 int nNumModes; //Number of modes enumerated....
186
187 wxArrayVideoModes Modes; //modes to return...
188
189 if (XF86VidModeGetAllModeLines(pDisplay, nScreen, &nNumModes, &ppXModes) == TRUE)
190 {
191 for (int i = 0; i < nNumModes; ++i)
192 {
193 if (mode == wxDefaultVideoMode || //According to display.h All modes valid if dafault mode...
194 mode.Matches(wxCVM((*ppXModes[i]))) ) //...?
195 {
196 Modes.Add(wxCVM((*ppXModes[i])));
197 }
198 wxClearXVM((*ppXModes[i]));
199 // XFree(ppXModes[i]); //supposed to free?
200 }
201 XFree(ppXModes);
202 }
203 else //OOPS!
204 {
205 wxLogSysError(_("Failed to enumerate video modes"));
206 }
207
208 return Modes;
209 }
210
211 wxVideoMode wxDisplay::GetCurrentMode() const
212 {
213 XF86VidModeModeLine VM;
214 int nDotClock;
215 XF86VidModeGetModeLine((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay()),
216 &nDotClock, &VM);
217 wxClearXVM(VM);
218 return wxCVM2(VM, nDotClock);
219 }
220
221 bool wxDisplay::ChangeMode(const wxVideoMode& mode)
222 {
223 XF86VidModeModeInfo** ppXModes; //Enumerated Modes (Don't forget XFree() :))
224 int nNumModes; //Number of modes enumerated....
225
226 if( !XF86VidModeGetAllModeLines((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay()), &nNumModes, &ppXModes) )
227 {
228 wxLogSysError(_("Failed to change video mode"));
229 return false;
230 }
231
232 bool bRet = false;
233 if (mode == wxDefaultVideoMode)
234 {
235 bRet = XF86VidModeSwitchToMode((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay()),
236 ppXModes[0]) == TRUE;
237
238 for (int i = 0; i < nNumModes; ++i)
239 {
240 wxClearXVM((*ppXModes[i]));
241 // XFree(ppXModes[i]); //supposed to free?
242 }
243 }
244 else
245 {
246 for (int i = 0; i < nNumModes; ++i)
247 {
248 if (!bRet &&
249 ppXModes[i]->hdisplay == mode.w &&
250 ppXModes[i]->vdisplay == mode.h &&
251 wxCRR((*ppXModes[i])) == mode.refresh)
252 {
253 //switch!
254 bRet = XF86VidModeSwitchToMode((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay()),
255 ppXModes[i]) == TRUE;
256 }
257 wxClearXVM((*ppXModes[i]));
258 // XFree(ppXModes[i]); //supposed to free?
259 }
260 }
261
262 XFree(ppXModes);
263
264 return bRet;
265 }
266
267
268 #else // !HAVE_X11_EXTENSIONS_XF86VMODE_H
269
270 wxArrayVideoModes wxDisplay::GetModes(const wxVideoMode& mode) const
271 {
272 Display *disp = (Display*)wxGetDisplay();
273 int count_return;
274 int* depths = XListDepths(disp, 0, &count_return);
275 wxArrayVideoModes modes;
276 if (depths)
277 {
278 int x;
279 for (x = 0; x < count_return; ++x)
280 {
281 modes.Add(wxVideoMode(m_priv->m_rect.GetWidth(), m_priv->m_rect.GetHeight(), depths[x]));
282 }
283 }
284 return modes;
285 }
286
287 wxVideoMode wxDisplay::GetCurrentMode() const
288 {
289 // Not implemented
290 return wxVideoMode();
291 }
292
293 bool wxDisplay::ChangeMode(const wxVideoMode& mode)
294 {
295 // Not implemented
296 return false;
297 }
298
299 #endif // !HAVE_X11_EXTENSIONS_XF86VMODE_H
300
301 #endif /* wxUSE_DISPLAY */