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