]> git.saurik.com Git - wxWidgets.git/blob - src/mac/display.cpp
Compilation fix for !WXWIN_COMPATIBILITY_2_4.
[wxWidgets.git] / src / mac / display.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: display.cpp
3 // Purpose: Mac implementation of wxDisplay class
4 // Author: Brian Victor
5 // Modified by: Royce Mitchell III & Ryan Norton
6 // Created: 06/21/02
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWindows team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "display.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #if wxUSE_DISPLAY
24
25 #ifndef WX_PRECOMP
26 #include "wx/dynarray.h"
27 #include "wx/log.h"
28 #endif
29
30 #ifdef __DARWIN__
31 #include <Carbon/Carbon.h>
32 #else
33 #include <Displays.h>
34 #include <Quickdraw.h>
35 #include <Video.h> //for VDSwitchInfoRec
36 #endif
37
38 #include "wx/display.h"
39 #include "wx/gdicmn.h"
40 #include "wx/string.h"
41
42 // ----------------------------------------------------------------------------
43 // private classes
44 // ----------------------------------------------------------------------------
45
46 class wxDisplayMacPriv
47 {
48 public:
49 GDHandle m_hndl;
50 };
51
52 size_t wxDisplayBase::GetCount()
53 {
54 GDHandle hndl;
55 size_t num = 0;
56 hndl = DMGetFirstScreenDevice(true);
57 while(hndl)
58 {
59 num++;
60 hndl = DMGetNextScreenDevice(hndl, true);
61 }
62 return num;
63 }
64
65 int wxDisplayBase::GetFromPoint(const wxPoint &p)
66 {
67 GDHandle hndl;
68 size_t num = 0;
69 hndl = DMGetFirstScreenDevice(true);
70 while(hndl)
71 {
72 Rect screenrect = (*hndl)->gdRect;
73 if (p.x >= screenrect.left &&
74 p.x <= screenrect.right &&
75 p.y >= screenrect.top &&
76 p.y <= screenrect.bottom)
77 {
78 return num;
79 }
80 num++;
81 hndl = DMGetNextScreenDevice(hndl, true);
82 }
83 return -1;
84 }
85
86 wxDisplay::wxDisplay(size_t index) : wxDisplayBase ( index ),
87 m_priv ( new wxDisplayMacPriv() )
88 {
89 GDHandle hndl;
90 hndl = DMGetFirstScreenDevice(true);
91 m_priv->m_hndl = NULL;
92 while(hndl)
93 {
94 if (index == 0)
95 {
96 m_priv->m_hndl = hndl;
97 }
98 index--;
99 hndl = DMGetNextScreenDevice(hndl, true);
100 }
101 }
102
103 wxRect wxDisplay::GetGeometry() const
104 {
105 if (!(m_priv)) return wxRect(0, 0, 0, 0);
106 if (!(m_priv->m_hndl)) return wxRect(0, 0, 0, 0);
107 Rect screenrect = (*(m_priv->m_hndl))->gdRect;
108 return wxRect( screenrect.left, screenrect.top,
109 screenrect.right - screenrect.left, screenrect.bottom - screenrect.top);
110 }
111
112 int wxDisplay::GetDepth() const
113 {
114 if (!(m_priv)) return 0;
115 if (!(m_priv->m_hndl)) return 0;
116
117 // This cryptic looking code is based on Apple's sample code:
118 // http://developer.apple.com/samplecode/Sample_Code/Graphics_2D/GDevVideo/Gen.cp.htm
119
120 //RN - according to the docs
121 //gdPMap is a bitmap-type representation of the GDevice, and all
122 //0x0000FFFF does is get the lower 16 bits of pixelSize. However,
123 //since pixelSize is only 16 bits (a short)...
124 return ((*(*(m_priv->m_hndl))->gdPMap)->pixelSize) & 0x0000FFFF;
125 }
126
127 wxString wxDisplay::GetName() const
128 {
129 // Macs don't name their displays...
130 return wxEmptyString;
131 }
132
133 struct DMModeIteratorRec
134 {
135 wxArrayVideoModes* pModes;
136 const wxVideoMode* pMatchMode;
137 };
138
139 pascal void DMModeListIteratorProc ( void* pData,
140 DMListIndexType nIndex,
141 DMDisplayModeListEntryPtr pInfo)
142 {
143 DMModeIteratorRec* pInfoData = (DMModeIteratorRec*) pData;
144
145 //Note that in testing the refresh rate is always 0 on my ibook - RN
146 int refresh = (int) Fix2Long(pInfo->displayModeResolutionInfo->csRefreshRate);
147
148 for(unsigned long i = 0; i < pInfo->displayModeDepthBlockInfo->depthBlockCount; ++i)
149 {
150 #define pDBI pInfo->displayModeDepthBlockInfo->depthVPBlock[i].depthVPBlock
151
152 if (wxVideoMode((int) pInfo->displayModeResolutionInfo->csHorizontalPixels,
153 (int) pInfo->displayModeResolutionInfo->csVerticalLines,
154 (int) pDBI->vpPixelSize,
155 refresh).Matches(*pInfoData->pMatchMode) )
156 {
157 pInfoData->pModes->Add(wxVideoMode((int) pInfo->displayModeResolutionInfo->csHorizontalPixels,
158 (int) pInfo->displayModeResolutionInfo->csVerticalLines,
159 (int) pDBI->vpPixelSize,
160 refresh));
161 }
162 #undef pDBI
163 }
164 }
165
166 struct DMModeInfoRec
167 {
168 const wxVideoMode* pMode;
169 VDSwitchInfoRec sMode;
170 bool bMatched;
171 };
172
173 pascal void DMModeInfoProc ( void* pData,
174 DMListIndexType nIndex,
175 DMDisplayModeListEntryPtr pInfo)
176 {
177 DMModeInfoRec* pInfoData = (DMModeInfoRec*) pData;
178 Fixed refresh = Long2Fix(pInfoData->pMode->refresh);
179
180 for(unsigned long i = 0; i < pInfo->displayModeDepthBlockInfo->depthBlockCount; ++i)
181 {
182 #define pDBI pInfo->displayModeDepthBlockInfo->depthVPBlock[i].depthVPBlock
183 if (pInfoData->pMode->w == (int&) pInfo->displayModeResolutionInfo->csHorizontalPixels &&
184 pInfoData->pMode->h == (int&) pInfo->displayModeResolutionInfo->csVerticalLines &&
185 pInfoData->pMode->bpp == (int) pDBI->vpPixelSize &&
186 refresh == pInfo->displayModeResolutionInfo->csRefreshRate)
187 {
188 memcpy(&pInfoData->sMode, pInfo->displayModeDepthBlockInfo->depthVPBlock[i].depthSwitchInfo,
189 sizeof(VDSwitchInfoRec));
190 pInfoData->sMode.csMode = pDBI->vpPixelSize;
191 pInfoData->bMatched = true;
192 break;
193 }
194 #undef pDBI
195 }
196 }
197
198 struct DMModeTransRec
199 {
200 wxVideoMode Mode;
201 const VDSwitchInfoRec* psMode;
202 bool bMatched;
203 };
204
205 pascal void DMModeTransProc ( void* pData,
206 DMListIndexType nIndex,
207 DMDisplayModeListEntryPtr pInfo)
208 {
209 DMModeTransRec* pInfoData = (DMModeTransRec*) pData;
210
211 for(unsigned long i = 0; i < pInfo->displayModeDepthBlockInfo->depthBlockCount; ++i)
212 {
213 #define pDBI pInfo->displayModeDepthBlockInfo->depthVPBlock[i].depthVPBlock
214 if (pInfoData->psMode->csData == pInfo->displayModeDepthBlockInfo->depthVPBlock[i].depthSwitchInfo->csData)
215 {
216 pInfoData->Mode = wxVideoMode((int) pInfo->displayModeResolutionInfo->csHorizontalPixels,
217 (int) pInfo->displayModeResolutionInfo->csVerticalLines,
218 (int) pDBI->vpPixelSize,
219 (int) Fix2Long(pInfo->displayModeResolutionInfo->csRefreshRate) );
220 pInfoData->bMatched = true;
221 break;
222 }
223 #undef pDBI
224 }
225 }
226
227 wxArrayVideoModes
228 wxDisplay::GetModes(const wxVideoMode& mode) const
229 {
230
231 wxArrayVideoModes Modes;
232
233 unsigned long dwDMVer;
234 Gestalt(gestaltDisplayMgrVers, (long*) &dwDMVer);
235
236 //Check DM version (for backward compatibility only - 7.5.3+ use 2.0)
237 if (dwDMVer >= 0x020000) //version 2?
238 {
239
240 DMListIndexType nNumModes;
241 DMListType pModes;
242 DMDisplayModeListIteratorUPP uppMLI;
243 DisplayIDType nDisplayID;
244
245 wxASSERT(DMGetDisplayIDByGDevice(m_priv->m_hndl, &nDisplayID, false) == noErr);
246 //Create a new list...
247 wxASSERT_MSG(DMNewDisplayModeList(nDisplayID, NULL, NULL, &nNumModes, &pModes) == noErr, wxT("Could not create a new display mode list") );
248
249 uppMLI = NewDMDisplayModeListIteratorUPP(DMModeListIteratorProc);
250 wxASSERT(uppMLI);
251
252 DMModeIteratorRec sModeInfo;
253 sModeInfo.pModes = &Modes;
254 sModeInfo.pMatchMode = &mode;
255 for (DMListIndexType i = 0; i < nNumModes; ++i)
256 {
257 wxASSERT(DMGetIndexedDisplayModeFromList(pModes, i, NULL,
258 uppMLI, &sModeInfo) == noErr);
259 }
260 DisposeDMDisplayModeListIteratorUPP(uppMLI);
261 wxASSERT(DMDisposeList(pModes) == noErr);
262 }
263 else //DM 1.0, 1.2, 1.x
264 {
265 wxLogSysError(wxString::Format(wxT("Display Manager Version %u Not Supported! Present? %s"),
266 (unsigned int) dwDMVer / 0x10000,
267 (dwDMVer & (1 << gestaltDisplayMgrPresent) ? wxT("Yes") : wxT("No")) )
268 );
269 }
270
271 return Modes;
272 }
273
274 wxVideoMode wxDisplay::GetCurrentMode() const
275 {
276 unsigned long dwDMVer;
277 wxVideoMode RetMode;
278
279 Gestalt(gestaltDisplayMgrVers, (long*) &dwDMVer);
280 //Check DM version (for backward compatibility only - 7.5.3+ use 2.0)
281 if (dwDMVer >= 0x020000) //version 2?
282 {
283 VDSwitchInfoRec sMode; //Note - csMode member also contains the bit depth
284 if (DMGetDisplayMode(m_priv->m_hndl, &sMode) == noErr)
285 {
286 DMListIndexType nNumModes;
287 DMListType pModes;
288 DMDisplayModeListIteratorUPP uppMLI;
289 DisplayIDType nDisplayID;
290
291 wxASSERT(DMGetDisplayIDByGDevice(m_priv->m_hndl, &nDisplayID, false) == noErr);
292 //Create a new list...
293 wxASSERT_MSG(DMNewDisplayModeList(nDisplayID, NULL, NULL, &nNumModes, &pModes) == noErr,
294 wxT("Could not create a new display mode list") );
295
296 uppMLI = NewDMDisplayModeListIteratorUPP(DMModeTransProc);
297 wxASSERT(uppMLI);
298
299 DMModeTransRec sModeInfo;
300 sModeInfo.bMatched = false;
301 sModeInfo.psMode = &sMode;
302 for (DMListIndexType i = 0; i < nNumModes; ++i)
303 {
304 wxASSERT(DMGetIndexedDisplayModeFromList(pModes, i, NULL,
305 uppMLI, &sModeInfo) == noErr);
306
307 if ( sModeInfo.bMatched == true )
308 {
309 RetMode = sModeInfo.Mode;
310 break;
311 }
312 }
313
314 DisposeDMDisplayModeListIteratorUPP(uppMLI);
315 wxASSERT(DMDisposeList(pModes) == noErr);
316 }
317 else //Can't get current mode?
318 {
319 wxLogSysError(wxString::Format(wxT("Couldn't obtain current display mode!!!\ndwDMVer:%u"),
320 (unsigned int) dwDMVer));
321 }
322 }
323 else //DM ver 1
324 {
325 wxLogSysError(wxString::Format(wxT("Display Manager Version %u Not Supported! Present? %s"),
326 (unsigned int) dwDMVer / 0x10000,
327 (dwDMVer & (1 << gestaltDisplayMgrPresent) ? wxT("Yes") : wxT("No")) )
328 );
329 }
330
331 return RetMode;
332 }
333
334 bool wxDisplay::ChangeMode(const wxVideoMode& mode)
335 {
336 unsigned long dwDMVer;
337 Gestalt(gestaltDisplayMgrVers, (long*)&dwDMVer);
338 if (GetCount() == 1 || dwDMVer >= 0x020000)
339 {
340 if (mode == wxDefaultVideoMode)
341 {
342 //#ifndef __DARWIN__
343 // Handle hDisplayState;
344 // if (DMBeginConfigureDisplays(&hDisplayState) != noErr)
345 // {
346 // wxLogSysError(wxT("Could not lock display for display mode changing!"));
347 // return false;
348 // }
349 // wxASSERT( DMUseScreenPrefs(true, hDisplayState) == noErr);
350 // DMEndConfigureDisplays(hDisplayState);
351 // return true;
352 //#else
353 //hmmmmm....
354 return true;
355 //#endif
356 }
357
358 //0 & NULL for params 2 & 3 of DMSetVideoMode signal it to use defaults (current mode)
359 //DM 2.0+ doesn't use params 2 & 3 of DMSetDisplayMode
360 //so we have to use this icky structure
361 VDSwitchInfoRec sMode;
362 memset(&sMode, 0, sizeof(VDSwitchInfoRec) );
363
364 DMListIndexType nNumModes;
365 DMListType pModes;
366 DMDisplayModeListIteratorUPP uppMLI;
367 DisplayIDType nDisplayID;
368
369 wxASSERT(DMGetDisplayIDByGDevice(m_priv->m_hndl, &nDisplayID, false) == noErr);
370 //Create a new list...
371 wxASSERT_MSG(DMNewDisplayModeList(nDisplayID, NULL, NULL, &nNumModes, &pModes) == noErr,
372 wxT("Could not create a new display mode list") );
373
374 uppMLI = NewDMDisplayModeListIteratorUPP(DMModeInfoProc);
375 wxASSERT(uppMLI);
376
377 DMModeInfoRec sModeInfo;
378 sModeInfo.bMatched = false;
379 sModeInfo.pMode = &mode;
380 unsigned int i;
381 for(i = 0; i < nNumModes; ++i)
382 {
383 wxASSERT(DMGetIndexedDisplayModeFromList(pModes, i, NULL,
384 uppMLI, &sModeInfo) == noErr);
385 if (sModeInfo.bMatched == true)
386 {
387 sMode = sModeInfo.sMode;
388 break;
389 }
390 }
391 if(i == nNumModes)
392 return false;
393
394 DisposeDMDisplayModeListIteratorUPP(uppMLI);
395 wxASSERT(DMDisposeList(pModes) == noErr);
396
397 // For the really paranoid -
398 // unsigned long flags;
399 // Boolean bok;
400 // wxASSERT(noErr == DMCheckDisplayMode(m_priv->m_hndl, sMode.csData,
401 // sMode.csMode, &flags, NULL, &bok));
402 // wxASSERT(bok);
403
404 Handle hDisplayState;
405 if (DMBeginConfigureDisplays(&hDisplayState) != noErr)
406 {
407 wxLogSysError(wxT("Could not lock display for display mode changing!"));
408 return false;
409 }
410
411 unsigned long dwBPP = (unsigned long) mode.bpp;
412 if (DMSetDisplayMode(m_priv->m_hndl, sMode.csData,
413 (unsigned long*) &(dwBPP), NULL
414 //(unsigned long) &sMode
415 , hDisplayState
416 ) != noErr)
417 {
418 DMEndConfigureDisplays(hDisplayState);
419 wxMessageBox(wxString::Format(wxT("Could not set the display mode")));
420 return false;
421 }
422 DMEndConfigureDisplays(hDisplayState);
423 }
424 else //DM 1.0, 1.2, 1.x
425 {
426 wxLogSysError(wxString::Format(wxT("Monitor gravitation not supported yet. dwDMVer:%u"),
427 (unsigned int) dwDMVer));
428 return false;
429 }
430
431 return true;
432 }
433
434 wxDisplay::~wxDisplay()
435 {
436 if ( m_priv )
437 {
438 delete m_priv;
439 m_priv = 0;
440 }
441 }
442
443 #endif // wxUSE_DISPLAY