]> git.saurik.com Git - wxWidgets.git/blob - src/unix/displayx11.cpp
Fixed and refactored wxPropertyGridManager decsription text box painting
[wxWidgets.git] / src / unix / displayx11.cpp
1 ///////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/displayx11.cpp
3 // Purpose: Unix/X11 implementation of wxDisplay class
4 // Author: Brian Victor, Vadim Zeitlin
5 // Modified by:
6 // Created: 12/05/02
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_DISPLAY
28
29 #include "wx/display.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/dynarray.h"
33 #include "wx/gdicmn.h"
34 #include "wx/string.h"
35 #include "wx/utils.h"
36 #include "wx/intl.h"
37 #include "wx/log.h"
38 #endif /* WX_PRECOMP */
39
40 #include "wx/display_impl.h"
41
42 /* These must be included after the wx files. Otherwise the Data macro in
43 * Xlibint.h conflicts with a function declaration in wx/list.h. */
44 extern "C"
45 {
46 #include <X11/Xlib.h>
47 #include <X11/Xlibint.h>
48
49 #include <X11/extensions/Xinerama.h>
50 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
51 #include <X11/extensions/xf86vmode.h>
52 #endif
53 }
54
55 // ----------------------------------------------------------------------------
56 // helper class to automatically free XineramaQueryScreens() return value
57 // ----------------------------------------------------------------------------
58
59 class ScreensInfo
60 {
61 public:
62 ScreensInfo()
63 {
64 m_screens = XineramaQueryScreens((Display *)wxGetDisplay(), &m_num);
65 }
66
67 ~ScreensInfo()
68 {
69 XFree(m_screens);
70 }
71
72 operator const XineramaScreenInfo *() const { return m_screens; }
73
74 unsigned GetCount() const { return static_cast<unsigned>(m_num); }
75
76 private:
77 XineramaScreenInfo *m_screens;
78 int m_num;
79 };
80
81 // ----------------------------------------------------------------------------
82 // display and display factory classes
83 // ----------------------------------------------------------------------------
84
85 class WXDLLEXPORT wxDisplayImplX11 : public wxDisplayImpl
86 {
87 public:
88 wxDisplayImplX11(unsigned n, const XineramaScreenInfo& info)
89 : wxDisplayImpl(n),
90 m_rect(info.x_org, info.y_org, info.width, info.height)
91 {
92 }
93
94 virtual wxRect GetGeometry() const { return m_rect; }
95 virtual wxRect GetClientArea() const
96 {
97 // we intentionally don't cache the result here because the client
98 // display area may change (e.g. the user resized or hid a panel) and
99 // we don't currently react to its changes
100 return IsPrimary() ? wxGetClientDisplayRect() : m_rect;
101 }
102
103 virtual wxString GetName() const { return wxString(); }
104
105 virtual wxArrayVideoModes GetModes(const wxVideoMode& mode) const;
106 virtual wxVideoMode GetCurrentMode() const;
107 virtual bool ChangeMode(const wxVideoMode& mode);
108
109 private:
110 wxRect m_rect;
111 int m_depth;
112
113 DECLARE_NO_COPY_CLASS(wxDisplayImplX11)
114 };
115
116 class wxDisplayFactoryX11 : public wxDisplayFactory
117 {
118 public:
119 wxDisplayFactoryX11() { }
120
121 virtual wxDisplayImpl *CreateDisplay(unsigned n);
122 virtual unsigned GetCount();
123 virtual int GetFromPoint(const wxPoint& pt);
124
125 protected:
126 DECLARE_NO_COPY_CLASS(wxDisplayFactoryX11)
127 };
128
129 // ============================================================================
130 // wxDisplayFactoryX11 implementation
131 // ============================================================================
132
133 unsigned wxDisplayFactoryX11::GetCount()
134 {
135 return ScreensInfo().GetCount();
136 }
137
138 int wxDisplayFactoryX11::GetFromPoint(const wxPoint& p)
139 {
140 ScreensInfo screens;
141
142 const unsigned numscreens(screens.GetCount());
143 for ( unsigned i = 0; i < numscreens; ++i )
144 {
145 const XineramaScreenInfo& s = screens[i];
146 if ( p.x >= s.x_org && p.x < s.x_org + s.width &&
147 p.y >= s.y_org && p.y < s.y_org + s.height )
148 {
149 return i;
150 }
151 }
152
153 return wxNOT_FOUND;
154 }
155
156 wxDisplayImpl *wxDisplayFactoryX11::CreateDisplay(unsigned n)
157 {
158 ScreensInfo screens;
159
160 return n < screens.GetCount() ? new wxDisplayImplX11(n, screens[n]) : NULL;
161 }
162
163 // ============================================================================
164 // wxDisplayImplX11 implementation
165 // ============================================================================
166
167 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
168
169 //
170 // See (http://www.xfree86.org/4.2.0/XF86VidModeDeleteModeLine.3.html) for more
171 // info about xf86 video mode extensions
172 //
173
174 //free private data common to x (usually s3) servers
175 #define wxClearXVM(vm) if(vm.privsize) XFree(vm.c_private)
176
177 // Correct res rate from GLFW
178 #define wxCRR2(v,dc) (int) (((1000.0f * (float) dc) /*PIXELS PER SECOND */) / ((float) v.htotal * v.vtotal /*PIXELS PER FRAME*/) + 0.5f)
179 #define wxCRR(v) wxCRR2(v,v.dotclock)
180 #define wxCVM2(v, dc) wxVideoMode(v.hdisplay, v.vdisplay, DefaultDepth((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay())), wxCRR2(v,dc))
181 #define wxCVM(v) wxCVM2(v, v.dotclock)
182
183 wxArrayVideoModes wxDisplayImplX11::GetModes(const wxVideoMode& mode) const
184 {
185 //Convenience...
186 Display* pDisplay = (Display*) wxGetDisplay(); //default display
187 int nScreen = DefaultScreen(pDisplay); //default screen of (default) display...
188
189 //Some variables..
190 XF86VidModeModeInfo** ppXModes; //Enumerated Modes (Don't forget XFree() :))
191 int nNumModes; //Number of modes enumerated....
192
193 wxArrayVideoModes Modes; //modes to return...
194
195 if (XF86VidModeGetAllModeLines(pDisplay, nScreen, &nNumModes, &ppXModes) == TRUE)
196 {
197 for (int i = 0; i < nNumModes; ++i)
198 {
199 if (mode == wxDefaultVideoMode || //According to display.h All modes valid if dafault mode...
200 mode.Matches(wxCVM((*ppXModes[i]))) ) //...?
201 {
202 Modes.Add(wxCVM((*ppXModes[i])));
203 }
204 wxClearXVM((*ppXModes[i]));
205 // XFree(ppXModes[i]); //supposed to free?
206 }
207 XFree(ppXModes);
208 }
209 else //OOPS!
210 {
211 wxLogSysError(_("Failed to enumerate video modes"));
212 }
213
214 return Modes;
215 }
216
217 wxVideoMode wxDisplayImplX11::GetCurrentMode() const
218 {
219 XF86VidModeModeLine VM;
220 int nDotClock;
221 XF86VidModeGetModeLine((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay()),
222 &nDotClock, &VM);
223 wxClearXVM(VM);
224 return wxCVM2(VM, nDotClock);
225 }
226
227 bool wxDisplayImplX11::ChangeMode(const wxVideoMode& mode)
228 {
229 XF86VidModeModeInfo** ppXModes; //Enumerated Modes (Don't forget XFree() :))
230 int nNumModes; //Number of modes enumerated....
231
232 if( !XF86VidModeGetAllModeLines((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay()), &nNumModes, &ppXModes) )
233 {
234 wxLogSysError(_("Failed to change video mode"));
235 return false;
236 }
237
238 bool bRet = false;
239 if (mode == wxDefaultVideoMode)
240 {
241 bRet = XF86VidModeSwitchToMode((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay()),
242 ppXModes[0]) == TRUE;
243
244 for (int i = 0; i < nNumModes; ++i)
245 {
246 wxClearXVM((*ppXModes[i]));
247 // XFree(ppXModes[i]); //supposed to free?
248 }
249 }
250 else
251 {
252 for (int i = 0; i < nNumModes; ++i)
253 {
254 if (!bRet &&
255 ppXModes[i]->hdisplay == mode.GetWidth() &&
256 ppXModes[i]->vdisplay == mode.GetHeight() &&
257 wxCRR((*ppXModes[i])) == mode.GetRefresh())
258 {
259 //switch!
260 bRet = XF86VidModeSwitchToMode((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay()),
261 ppXModes[i]) == TRUE;
262 }
263 wxClearXVM((*ppXModes[i]));
264 // XFree(ppXModes[i]); //supposed to free?
265 }
266 }
267
268 XFree(ppXModes);
269
270 return bRet;
271 }
272
273
274 #else // !HAVE_X11_EXTENSIONS_XF86VMODE_H
275
276 wxArrayVideoModes wxDisplayImplX11::GetModes(const wxVideoMode& modeMatch) const
277 {
278 int count_return;
279 int* depths = XListDepths((Display*)wxGetDisplay(), 0, &count_return);
280 wxArrayVideoModes modes;
281 if ( depths )
282 {
283 for ( int x = 0; x < count_return; ++x )
284 {
285 wxVideoMode mode(m_rect.GetWidth(), m_rect.GetHeight(), depths[x]);
286 if ( mode.Matches(modeMatch) )
287 {
288 modes.Add(modeMatch);
289 }
290 }
291
292 XFree(depths);
293 }
294 return modes;
295 }
296
297 wxVideoMode wxDisplayImplX11::GetCurrentMode() const
298 {
299 // Not implemented
300 return wxVideoMode();
301 }
302
303 bool wxDisplayImplX11::ChangeMode(const wxVideoMode& WXUNUSED(mode))
304 {
305 // Not implemented
306 return false;
307 }
308
309 #endif // !HAVE_X11_EXTENSIONS_XF86VMODE_H
310
311 // ============================================================================
312 // wxDisplay::CreateFactory()
313 // ============================================================================
314
315 /* static */ wxDisplayFactory *wxDisplay::CreateFactory()
316 {
317 if ( XineramaIsActive((Display*)wxGetDisplay()) )
318 {
319 return new wxDisplayFactoryX11;
320 }
321
322 return new wxDisplayFactorySingle;
323 }
324
325 #endif /* wxUSE_DISPLAY */
326
327 #include "wx/utils.h"
328
329 #if wxUSE_LIBHILDON
330
331 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
332 {
333 // TODO: don't hardcode display size
334 if ( x )
335 *x = 0;
336 if ( y )
337 *y = 0;
338 if ( width )
339 *width = 672;
340 if ( height )
341 *height = 396;
342 }
343
344 #else // !wxUSE_LIBHILDON
345
346 #include "wx/log.h"
347
348 #include <X11/Xlib.h>
349 #include <X11/Xatom.h>
350
351 // TODO: make this a full-fledged class and move to a public header
352 class wxX11Ptr
353 {
354 public:
355 wxX11Ptr(void *ptr = NULL) : m_ptr(ptr) { }
356 ~wxX11Ptr() { if ( m_ptr ) XFree(m_ptr); }
357
358 private:
359 void *m_ptr;
360
361 DECLARE_NO_COPY_CLASS(wxX11Ptr)
362 };
363
364 // NB: this function is implemented using X11 and not GDK calls as it's shared
365 // by wxGTK[12], wxX11 and wxMotif ports
366 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
367 {
368 Display * const dpy = wxGetX11Display();
369 wxCHECK_RET( dpy, _T("can't be called before initializing the GUI") );
370
371 const Atom atomWorkArea = XInternAtom(dpy, "_NET_WORKAREA", True);
372 if ( atomWorkArea )
373 {
374 long *workareas = NULL;
375 unsigned long numItems;
376 unsigned long bytesRemaining;
377 Atom actualType;
378 int format;
379
380 if ( XGetWindowProperty
381 (
382 dpy,
383 XDefaultRootWindow(dpy),
384 atomWorkArea,
385 0, // offset of data to retrieve
386 4, // number of items to retrieve
387 False, // don't delete property
388 XA_CARDINAL, // type of the items to get
389 &actualType,
390 &format,
391 &numItems,
392 &bytesRemaining,
393 (unsigned char **)&workareas
394 ) == Success && workareas )
395 {
396 wxX11Ptr x11ptr(workareas); // ensure it will be freed
397
398 // check that we retrieved the property of the expected type and
399 // that we did get back 4 longs (32 is the format for long), as
400 // requested
401 if ( actualType != XA_CARDINAL ||
402 format != 32 ||
403 numItems != 4 )
404 {
405 wxLogDebug(_T("XGetWindowProperty(\"_NET_WORKAREA\") failed"));
406 return;
407 }
408
409 if ( x )
410 *x = workareas[0];
411 if ( y )
412 *y = workareas[1];
413 if ( width )
414 *width = workareas[2];
415 if ( height )
416 *height = workareas[3];
417
418 return;
419 }
420 }
421
422 // if we get here, _NET_WORKAREA is not supported so return the entire
423 // screen size as fall back
424 if (x)
425 *x = 0;
426 if (y)
427 *y = 0;
428 wxDisplaySize(width, height);
429 }
430
431 #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON