]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/display.cpp
Fix overflow warnings (?)
[wxWidgets.git] / src / mac / carbon / display.cpp
CommitLineData
a536e411
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: display.cpp
3// Purpose: Mac implementation of wxDisplay class
cc9d56b0
RN
4// Author: Ryan Norton & Brian Victor
5// Modified by: Royce Mitchell III
a536e411
JS
6// Created: 06/21/02
7// RCS-ID: $Id$
77ffb593 8// Copyright: (c) wxWidgets team
65571936 9// Licence: wxWindows licence
a536e411
JS
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"
1ed1a609 27 #include "wx/log.h"
92094b09 28 #include "wx/msgdlg.h"
a536e411
JS
29#endif
30
31#ifdef __DARWIN__
32 #include <Carbon/Carbon.h>
33#else
7c4a8e41 34 #include <Gestalt.h>
a536e411
JS
35 #include <Displays.h>
36 #include <Quickdraw.h>
1ed1a609 37 #include <Video.h> //for VDSwitchInfoRec
7c4a8e41 38 #include <FixMath.h>
a536e411
JS
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
49class wxDisplayMacPriv
50{
51public:
e40298d5 52 GDHandle m_hndl;
a536e411
JS
53};
54
55size_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
68int 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
89wxDisplay::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
106wxRect 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
115int 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
1ed1a609
SC
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)...
a536e411
JS
127 return ((*(*(m_priv->m_hndl))->gdPMap)->pixelSize) & 0x0000FFFF;
128}
129
130wxString wxDisplay::GetName() const
131{
132 // Macs don't name their displays...
1e1cc1ce
DS
133 return wxEmptyString;
134}
135
1ed1a609
SC
136struct DMModeIteratorRec
137{
138 wxArrayVideoModes* pModes;
139 const wxVideoMode* pMatchMode;
140};
141
142pascal 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
169struct DMModeInfoRec
170{
171 const wxVideoMode* pMode;
172 VDSwitchInfoRec sMode;
173 bool bMatched;
174};
175
176pascal 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
201struct DMModeTransRec
202{
203 wxVideoMode Mode;
204 const VDSwitchInfoRec* psMode;
205 bool bMatched;
206};
207
208pascal 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
230wxArrayVideoModes
1e1cc1ce
DS
231 wxDisplay::GetModes(const wxVideoMode& mode) const
232{
1ed1a609
SC
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;
cc9d56b0 247 OSErr err;
1ed1a609 248
cc9d56b0
RN
249 err = DMGetDisplayIDByGDevice(m_priv->m_hndl, &nDisplayID, false);
250 wxASSERT(err == noErr);
251
1ed1a609 252 //Create a new list...
cc9d56b0
RN
253 err = DMNewDisplayModeList(nDisplayID, NULL, NULL, &nNumModes, &pModes);
254 wxASSERT_MSG(err == noErr, wxT("Could not create a new display mode list") );
1ed1a609
SC
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 {
cc9d56b0
RN
264 err = DMGetIndexedDisplayModeFromList(pModes, i, NULL, uppMLI, &sModeInfo);
265 wxASSERT(err == noErr);
1ed1a609
SC
266 }
267 DisposeDMDisplayModeListIteratorUPP(uppMLI);
cc9d56b0
RN
268
269 err = DMDisposeList(pModes);
270 wxASSERT(err == noErr);
1ed1a609
SC
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;
1e1cc1ce
DS
281}
282
283wxVideoMode wxDisplay::GetCurrentMode() const
284{
1ed1a609
SC
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;
cc9d56b0 299 OSErr err;
1ed1a609 300
cc9d56b0
RN
301 err = DMGetDisplayIDByGDevice(m_priv->m_hndl, &nDisplayID, false);
302 wxASSERT(err == noErr);
303
1ed1a609 304 //Create a new list...
cc9d56b0
RN
305 err = DMNewDisplayModeList(nDisplayID, NULL, NULL, &nNumModes, &pModes);
306 wxASSERT_MSG(err == noErr, wxT("Could not create a new display mode list") );
1ed1a609
SC
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 {
cc9d56b0
RN
316 err = DMGetIndexedDisplayModeFromList(pModes, i, NULL, uppMLI, &sModeInfo);
317 wxASSERT(err == noErr);
1ed1a609
SC
318
319 if ( sModeInfo.bMatched == true )
320 {
321 RetMode = sModeInfo.Mode;
322 break;
323 }
324 }
325
326 DisposeDMDisplayModeListIteratorUPP(uppMLI);
cc9d56b0
RN
327
328 err = DMDisposeList(pModes);
329 wxASSERT(err == noErr);
1ed1a609
SC
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;
1e1cc1ce
DS
346}
347
348bool wxDisplay::ChangeMode(const wxVideoMode& mode)
349{
1ed1a609
SC
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;
cc9d56b0 382 OSErr err;
1ed1a609 383
cc9d56b0
RN
384 err = DMGetDisplayIDByGDevice(m_priv->m_hndl, &nDisplayID, false);
385 wxASSERT(err == noErr);
386
1ed1a609 387 //Create a new list...
cc9d56b0
RN
388 err = DMNewDisplayModeList(nDisplayID, NULL, NULL, &nNumModes, &pModes);
389 wxASSERT_MSG(err == noErr, wxT("Could not create a new display mode list") );
1ed1a609
SC
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 {
cc9d56b0
RN
400 err = DMGetIndexedDisplayModeFromList(pModes, i, NULL, uppMLI, &sModeInfo);
401 wxASSERT(err == noErr);
402
1ed1a609
SC
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);
cc9d56b0
RN
413
414 err = DMDisposeList(pModes);
415 wxASSERT(err == noErr);
1ed1a609
SC
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;
a536e411
JS
452}
453
454wxDisplay::~wxDisplay()
455{
456 if ( m_priv )
457 {
458 delete m_priv;
459 m_priv = 0;
460 }
461}
462
463#endif // wxUSE_DISPLAY