]> git.saurik.com Git - wxWidgets.git/blob - src/mac/classic/display.cpp
there aren't any suitable images in images.py any more...
[wxWidgets.git] / src / mac / classic / 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) 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
248 wxASSERT(DMGetDisplayIDByGDevice(m_priv->m_hndl, &nDisplayID, false) == noErr);
249 //Create a new list...
250 wxASSERT_MSG(DMNewDisplayModeList(nDisplayID, NULL, NULL, &nNumModes, &pModes) == noErr, wxT("Could not create a new display mode list") );
251
252 uppMLI = NewDMDisplayModeListIteratorUPP(DMModeListIteratorProc);
253 wxASSERT(uppMLI);
254
255 DMModeIteratorRec sModeInfo;
256 sModeInfo.pModes = &Modes;
257 sModeInfo.pMatchMode = &mode;
258 for (DMListIndexType i = 0; i < nNumModes; ++i)
259 {
260 wxASSERT(DMGetIndexedDisplayModeFromList(pModes, i, NULL,
261 uppMLI, &sModeInfo) == noErr);
262 }
263 DisposeDMDisplayModeListIteratorUPP(uppMLI);
264 wxASSERT(DMDisposeList(pModes) == noErr);
265 }
266 else //DM 1.0, 1.2, 1.x
267 {
268 wxLogSysError(wxString::Format(wxT("Display Manager Version %u Not Supported! Present? %s"),
269 (unsigned int) dwDMVer / 0x10000,
270 (dwDMVer & (1 << gestaltDisplayMgrPresent) ? wxT("Yes") : wxT("No")) )
271 );
272 }
273
274 return Modes;
275 }
276
277 wxVideoMode wxDisplay::GetCurrentMode() const
278 {
279 unsigned long dwDMVer;
280 wxVideoMode RetMode;
281
282 Gestalt(gestaltDisplayMgrVers, (long*) &dwDMVer);
283 //Check DM version (for backward compatibility only - 7.5.3+ use 2.0)
284 if (dwDMVer >= 0x020000) //version 2?
285 {
286 VDSwitchInfoRec sMode; //Note - csMode member also contains the bit depth
287 if (DMGetDisplayMode(m_priv->m_hndl, &sMode) == noErr)
288 {
289 DMListIndexType nNumModes;
290 DMListType pModes;
291 DMDisplayModeListIteratorUPP uppMLI;
292 DisplayIDType nDisplayID;
293
294 wxASSERT(DMGetDisplayIDByGDevice(m_priv->m_hndl, &nDisplayID, false) == noErr);
295 //Create a new list...
296 wxASSERT_MSG(DMNewDisplayModeList(nDisplayID, NULL, NULL, &nNumModes, &pModes) == noErr,
297 wxT("Could not create a new display mode list") );
298
299 uppMLI = NewDMDisplayModeListIteratorUPP(DMModeTransProc);
300 wxASSERT(uppMLI);
301
302 DMModeTransRec sModeInfo;
303 sModeInfo.bMatched = false;
304 sModeInfo.psMode = &sMode;
305 for (DMListIndexType i = 0; i < nNumModes; ++i)
306 {
307 wxASSERT(DMGetIndexedDisplayModeFromList(pModes, i, NULL,
308 uppMLI, &sModeInfo) == noErr);
309
310 if ( sModeInfo.bMatched == true )
311 {
312 RetMode = sModeInfo.Mode;
313 break;
314 }
315 }
316
317 DisposeDMDisplayModeListIteratorUPP(uppMLI);
318 wxASSERT(DMDisposeList(pModes) == noErr);
319 }
320 else //Can't get current mode?
321 {
322 wxLogSysError(wxString::Format(wxT("Couldn't obtain current display mode!!!\ndwDMVer:%u"),
323 (unsigned int) dwDMVer));
324 }
325 }
326 else //DM ver 1
327 {
328 wxLogSysError(wxString::Format(wxT("Display Manager Version %u Not Supported! Present? %s"),
329 (unsigned int) dwDMVer / 0x10000,
330 (dwDMVer & (1 << gestaltDisplayMgrPresent) ? wxT("Yes") : wxT("No")) )
331 );
332 }
333
334 return RetMode;
335 }
336
337 bool wxDisplay::ChangeMode(const wxVideoMode& mode)
338 {
339 unsigned long dwDMVer;
340 Gestalt(gestaltDisplayMgrVers, (long*)&dwDMVer);
341 if (GetCount() == 1 || dwDMVer >= 0x020000)
342 {
343 if (mode == wxDefaultVideoMode)
344 {
345 //#ifndef __DARWIN__
346 // Handle hDisplayState;
347 // if (DMBeginConfigureDisplays(&hDisplayState) != noErr)
348 // {
349 // wxLogSysError(wxT("Could not lock display for display mode changing!"));
350 // return false;
351 // }
352 // wxASSERT( DMUseScreenPrefs(true, hDisplayState) == noErr);
353 // DMEndConfigureDisplays(hDisplayState);
354 // return true;
355 //#else
356 //hmmmmm....
357 return true;
358 //#endif
359 }
360
361 //0 & NULL for params 2 & 3 of DMSetVideoMode signal it to use defaults (current mode)
362 //DM 2.0+ doesn't use params 2 & 3 of DMSetDisplayMode
363 //so we have to use this icky structure
364 VDSwitchInfoRec sMode;
365 memset(&sMode, 0, sizeof(VDSwitchInfoRec) );
366
367 DMListIndexType nNumModes;
368 DMListType pModes;
369 DMDisplayModeListIteratorUPP uppMLI;
370 DisplayIDType nDisplayID;
371
372 wxASSERT(DMGetDisplayIDByGDevice(m_priv->m_hndl, &nDisplayID, false) == noErr);
373 //Create a new list...
374 wxASSERT_MSG(DMNewDisplayModeList(nDisplayID, NULL, NULL, &nNumModes, &pModes) == noErr,
375 wxT("Could not create a new display mode list") );
376
377 uppMLI = NewDMDisplayModeListIteratorUPP(DMModeInfoProc);
378 wxASSERT(uppMLI);
379
380 DMModeInfoRec sModeInfo;
381 sModeInfo.bMatched = false;
382 sModeInfo.pMode = &mode;
383 unsigned int i;
384 for(i = 0; i < nNumModes; ++i)
385 {
386 wxASSERT(DMGetIndexedDisplayModeFromList(pModes, i, NULL,
387 uppMLI, &sModeInfo) == noErr);
388 if (sModeInfo.bMatched == true)
389 {
390 sMode = sModeInfo.sMode;
391 break;
392 }
393 }
394 if(i == nNumModes)
395 return false;
396
397 DisposeDMDisplayModeListIteratorUPP(uppMLI);
398 wxASSERT(DMDisposeList(pModes) == noErr);
399
400 // For the really paranoid -
401 // unsigned long flags;
402 // Boolean bok;
403 // wxASSERT(noErr == DMCheckDisplayMode(m_priv->m_hndl, sMode.csData,
404 // sMode.csMode, &flags, NULL, &bok));
405 // wxASSERT(bok);
406
407 Handle hDisplayState;
408 if (DMBeginConfigureDisplays(&hDisplayState) != noErr)
409 {
410 wxLogSysError(wxT("Could not lock display for display mode changing!"));
411 return false;
412 }
413
414 unsigned long dwBPP = (unsigned long) mode.bpp;
415 if (DMSetDisplayMode(m_priv->m_hndl, sMode.csData,
416 (unsigned long*) &(dwBPP), NULL
417 //(unsigned long) &sMode
418 , hDisplayState
419 ) != noErr)
420 {
421 DMEndConfigureDisplays(hDisplayState);
422 wxMessageBox(wxString::Format(wxT("Could not set the display mode")));
423 return false;
424 }
425 DMEndConfigureDisplays(hDisplayState);
426 }
427 else //DM 1.0, 1.2, 1.x
428 {
429 wxLogSysError(wxString::Format(wxT("Monitor gravitation not supported yet. dwDMVer:%u"),
430 (unsigned int) dwDMVer));
431 return false;
432 }
433
434 return true;
435 }
436
437 wxDisplay::~wxDisplay()
438 {
439 if ( m_priv )
440 {
441 delete m_priv;
442 m_priv = 0;
443 }
444 }
445
446 #endif // wxUSE_DISPLAY