1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Mac implementation of wxDisplay class
4 // Author: Brian Victor
5 // Modified by: Royce Mitchell III & Ryan Norton
8 // Copyright: (c) wxWindows team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "display.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
26 #include "wx/dynarray.h"
31 #include <Carbon/Carbon.h>
34 #include <Quickdraw.h>
35 #include <Video.h> //for VDSwitchInfoRec
38 #include "wx/display.h"
39 #include "wx/gdicmn.h"
40 #include "wx/string.h"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 class wxDisplayMacPriv
52 size_t wxDisplayBase::GetCount()
56 hndl
= DMGetFirstScreenDevice(true);
60 hndl
= DMGetNextScreenDevice(hndl
, true);
65 int wxDisplayBase::GetFromPoint(const wxPoint
&p
)
69 hndl
= DMGetFirstScreenDevice(true);
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
)
81 hndl
= DMGetNextScreenDevice(hndl
, true);
86 wxDisplay::wxDisplay(size_t index
) : wxDisplayBase ( index
),
87 m_priv ( new wxDisplayMacPriv() )
90 hndl
= DMGetFirstScreenDevice(true);
91 m_priv
->m_hndl
= NULL
;
96 m_priv
->m_hndl
= hndl
;
99 hndl
= DMGetNextScreenDevice(hndl
, true);
103 wxRect
wxDisplay::GetGeometry() const
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
);
112 int wxDisplay::GetDepth() const
114 if (!(m_priv
)) return 0;
115 if (!(m_priv
->m_hndl
)) return 0;
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
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;
127 wxString
wxDisplay::GetName() const
129 // Macs don't name their displays...
130 return wxEmptyString
;
133 struct DMModeIteratorRec
135 wxArrayVideoModes
* pModes
;
136 const wxVideoMode
* pMatchMode
;
139 pascal void DMModeListIteratorProc ( void* pData
,
140 DMListIndexType nIndex
,
141 DMDisplayModeListEntryPtr pInfo
)
143 DMModeIteratorRec
* pInfoData
= (DMModeIteratorRec
*) pData
;
145 //Note that in testing the refresh rate is always 0 on my ibook - RN
146 int refresh
= (int) Fix2Long(pInfo
->displayModeResolutionInfo
->csRefreshRate
);
148 for(unsigned long i
= 0; i
< pInfo
->displayModeDepthBlockInfo
->depthBlockCount
; ++i
)
150 #define pDBI pInfo->displayModeDepthBlockInfo->depthVPBlock[i].depthVPBlock
152 if (wxVideoMode((int) pInfo
->displayModeResolutionInfo
->csHorizontalPixels
,
153 (int) pInfo
->displayModeResolutionInfo
->csVerticalLines
,
154 (int) pDBI
->vpPixelSize
,
155 refresh
).Matches(*pInfoData
->pMatchMode
) )
157 pInfoData
->pModes
->Add(wxVideoMode((int) pInfo
->displayModeResolutionInfo
->csHorizontalPixels
,
158 (int) pInfo
->displayModeResolutionInfo
->csVerticalLines
,
159 (int) pDBI
->vpPixelSize
,
168 const wxVideoMode
* pMode
;
169 VDSwitchInfoRec sMode
;
173 pascal void DMModeInfoProc ( void* pData
,
174 DMListIndexType nIndex
,
175 DMDisplayModeListEntryPtr pInfo
)
177 DMModeInfoRec
* pInfoData
= (DMModeInfoRec
*) pData
;
178 Fixed refresh
= Long2Fix(pInfoData
->pMode
->refresh
);
180 for(unsigned long i
= 0; i
< pInfo
->displayModeDepthBlockInfo
->depthBlockCount
; ++i
)
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
)
188 memcpy(&pInfoData
->sMode
, pInfo
->displayModeDepthBlockInfo
->depthVPBlock
[i
].depthSwitchInfo
,
189 sizeof(VDSwitchInfoRec
));
190 pInfoData
->sMode
.csMode
= pDBI
->vpPixelSize
;
191 pInfoData
->bMatched
= true;
198 struct DMModeTransRec
201 const VDSwitchInfoRec
* psMode
;
205 pascal void DMModeTransProc ( void* pData
,
206 DMListIndexType nIndex
,
207 DMDisplayModeListEntryPtr pInfo
)
209 DMModeTransRec
* pInfoData
= (DMModeTransRec
*) pData
;
211 for(unsigned long i
= 0; i
< pInfo
->displayModeDepthBlockInfo
->depthBlockCount
; ++i
)
213 #define pDBI pInfo->displayModeDepthBlockInfo->depthVPBlock[i].depthVPBlock
214 if (pInfoData
->psMode
->csData
== pInfo
->displayModeDepthBlockInfo
->depthVPBlock
[i
].depthSwitchInfo
->csData
)
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;
228 wxDisplay::GetModes(const wxVideoMode
& mode
) const
231 wxArrayVideoModes Modes
;
233 unsigned long dwDMVer
;
234 Gestalt(gestaltDisplayMgrVers
, (long*) &dwDMVer
);
236 //Check DM version (for backward compatibility only - 7.5.3+ use 2.0)
237 if (dwDMVer
>= 0x020000) //version 2?
240 DMListIndexType nNumModes
;
242 DMDisplayModeListIteratorUPP uppMLI
;
243 DisplayIDType nDisplayID
;
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") );
249 uppMLI
= NewDMDisplayModeListIteratorUPP(DMModeListIteratorProc
);
252 DMModeIteratorRec sModeInfo
;
253 sModeInfo
.pModes
= &Modes
;
254 sModeInfo
.pMatchMode
= &mode
;
255 for (DMListIndexType i
= 0; i
< nNumModes
; ++i
)
257 wxASSERT(DMGetIndexedDisplayModeFromList(pModes
, i
, NULL
,
258 uppMLI
, &sModeInfo
) == noErr
);
260 DisposeDMDisplayModeListIteratorUPP(uppMLI
);
261 wxASSERT(DMDisposeList(pModes
) == noErr
);
263 else //DM 1.0, 1.2, 1.x
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")) )
274 wxVideoMode
wxDisplay::GetCurrentMode() const
276 unsigned long dwDMVer
;
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?
283 VDSwitchInfoRec sMode
; //Note - csMode member also contains the bit depth
284 if (DMGetDisplayMode(m_priv
->m_hndl
, &sMode
) == noErr
)
286 DMListIndexType nNumModes
;
288 DMDisplayModeListIteratorUPP uppMLI
;
289 DisplayIDType nDisplayID
;
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") );
296 uppMLI
= NewDMDisplayModeListIteratorUPP(DMModeTransProc
);
299 DMModeTransRec sModeInfo
;
300 sModeInfo
.bMatched
= false;
301 sModeInfo
.psMode
= &sMode
;
302 for (DMListIndexType i
= 0; i
< nNumModes
; ++i
)
304 wxASSERT(DMGetIndexedDisplayModeFromList(pModes
, i
, NULL
,
305 uppMLI
, &sModeInfo
) == noErr
);
307 if ( sModeInfo
.bMatched
== true )
309 RetMode
= sModeInfo
.Mode
;
314 DisposeDMDisplayModeListIteratorUPP(uppMLI
);
315 wxASSERT(DMDisposeList(pModes
) == noErr
);
317 else //Can't get current mode?
319 wxLogSysError(wxString::Format(wxT("Couldn't obtain current display mode!!!\ndwDMVer:%u"),
320 (unsigned int) dwDMVer
));
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")) )
334 bool wxDisplay::ChangeMode(const wxVideoMode
& mode
)
336 unsigned long dwDMVer
;
337 Gestalt(gestaltDisplayMgrVers
, (long*)&dwDMVer
);
338 if (GetCount() == 1 || dwDMVer
>= 0x020000)
340 if (mode
== wxDefaultVideoMode
)
343 // Handle hDisplayState;
344 // if (DMBeginConfigureDisplays(&hDisplayState) != noErr)
346 // wxLogSysError(wxT("Could not lock display for display mode changing!"));
349 // wxASSERT( DMUseScreenPrefs(true, hDisplayState) == noErr);
350 // DMEndConfigureDisplays(hDisplayState);
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
) );
364 DMListIndexType nNumModes
;
366 DMDisplayModeListIteratorUPP uppMLI
;
367 DisplayIDType nDisplayID
;
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") );
374 uppMLI
= NewDMDisplayModeListIteratorUPP(DMModeInfoProc
);
377 DMModeInfoRec sModeInfo
;
378 sModeInfo
.bMatched
= false;
379 sModeInfo
.pMode
= &mode
;
381 for(i
= 0; i
< nNumModes
; ++i
)
383 wxASSERT(DMGetIndexedDisplayModeFromList(pModes
, i
, NULL
,
384 uppMLI
, &sModeInfo
) == noErr
);
385 if (sModeInfo
.bMatched
== true)
387 sMode
= sModeInfo
.sMode
;
394 DisposeDMDisplayModeListIteratorUPP(uppMLI
);
395 wxASSERT(DMDisposeList(pModes
) == noErr
);
397 // For the really paranoid -
398 // unsigned long flags;
400 // wxASSERT(noErr == DMCheckDisplayMode(m_priv->m_hndl, sMode.csData,
401 // sMode.csMode, &flags, NULL, &bok));
404 Handle hDisplayState
;
405 if (DMBeginConfigureDisplays(&hDisplayState
) != noErr
)
407 wxLogSysError(wxT("Could not lock display for display mode changing!"));
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
418 DMEndConfigureDisplays(hDisplayState
);
419 wxMessageBox(wxString::Format(wxT("Could not set the display mode")));
422 DMEndConfigureDisplays(hDisplayState
);
424 else //DM 1.0, 1.2, 1.x
426 wxLogSysError(wxString::Format(wxT("Monitor gravitation not supported yet. dwDMVer:%u"),
427 (unsigned int) dwDMVer
));
434 wxDisplay::~wxDisplay()
443 #endif // wxUSE_DISPLAY