xref: /reactos/dll/cpl/desk/settings.c (revision 7b1049c8)
1 /*
2  * COPYRIGHT:       See COPYING in the top level directory
3  * PROJECT:         ReactOS Display Control Panel
4  * FILE:            dll/cpl/desk/settings.c
5  * PURPOSE:         Settings property page
6  *
7  * PROGRAMMERS:     Trevor McCort (lycan359@gmail.com)
8  *                  Hervé Poussineau (hpoussin@reactos.org)
9  */
10 
11 #include "desk.h"
12 
13 typedef struct _SETTINGS_DATA
14 {
15     PDISPLAY_DEVICE_ENTRY DisplayDeviceList;
16     PDISPLAY_DEVICE_ENTRY CurrentDisplayDevice;
17     HBITMAP hSpectrumBitmaps[NUM_SPECTRUM_BITMAPS];
18     int cxSource[NUM_SPECTRUM_BITMAPS];
19     int cySource[NUM_SPECTRUM_BITMAPS];
20 } SETTINGS_DATA, *PSETTINGS_DATA;
21 
22 typedef struct _TIMEOUTDATA
23 {
24     TCHAR szRawBuffer[256];
25     TCHAR szCookedBuffer[256];
26     INT nTimeout;
27 } TIMEOUTDATA, *PTIMEOUTDATA;
28 
29 static VOID
30 UpdateDisplay(IN HWND hwndDlg, PSETTINGS_DATA pData, IN BOOL bUpdateThumb)
31 {
32     TCHAR Buffer[64];
33     TCHAR Pixel[64];
34     DWORD index;
35     HWND hwndMonSel;
36     MONSL_MONINFO info;
37 
38     LoadString(hApplet, IDS_PIXEL, Pixel, sizeof(Pixel) / sizeof(TCHAR));
39     _stprintf(Buffer, Pixel, pData->CurrentDisplayDevice->CurrentSettings->dmPelsWidth, pData->CurrentDisplayDevice->CurrentSettings->dmPelsHeight, Pixel);
40     SendDlgItemMessage(hwndDlg, IDC_SETTINGS_RESOLUTION_TEXT, WM_SETTEXT, 0, (LPARAM)Buffer);
41 
42     for (index = 0; index < pData->CurrentDisplayDevice->ResolutionsCount; index++)
43     {
44         if (pData->CurrentDisplayDevice->Resolutions[index].dmPelsWidth == pData->CurrentDisplayDevice->CurrentSettings->dmPelsWidth &&
45             pData->CurrentDisplayDevice->Resolutions[index].dmPelsHeight == pData->CurrentDisplayDevice->CurrentSettings->dmPelsHeight)
46         {
47             if (bUpdateThumb)
48                 SendDlgItemMessage(hwndDlg, IDC_SETTINGS_RESOLUTION, TBM_SETPOS, TRUE, index);
49             break;
50         }
51     }
52     if (LoadString(hApplet, (2900 + pData->CurrentDisplayDevice->CurrentSettings->dmBitsPerPel), Buffer, sizeof(Buffer) / sizeof(TCHAR)))
53         SendDlgItemMessage(hwndDlg, IDC_SETTINGS_BPP, CB_SELECTSTRING, (WPARAM)-1, (LPARAM)Buffer);
54 
55     hwndMonSel = GetDlgItem(hwndDlg, IDC_SETTINGS_MONSEL);
56     index = (INT)SendMessage(hwndMonSel, MSLM_GETCURSEL, 0, 0);
57     if (index != (DWORD)-1 && SendMessage(hwndMonSel, MSLM_GETMONITORINFO, index, (LPARAM)&info))
58     {
59         info.Size.cx = pData->CurrentDisplayDevice->CurrentSettings->dmPelsWidth;
60         info.Size.cy = pData->CurrentDisplayDevice->CurrentSettings->dmPelsHeight;
61         SendMessage(hwndMonSel, MSLM_SETMONITORINFO, index, (LPARAM)&info);
62     }
63 }
64 
65 static int
66 CompareSettings(PSETTINGS_ENTRY Entry, DWORD dmPelsWidth, DWORD dmPelsHeight,
67                 DWORD dmBitsPerPel, DWORD dmDisplayFrequency)
68 {
69     if (Entry->dmPelsWidth  == dmPelsWidth  &&
70         Entry->dmPelsHeight == dmPelsHeight &&
71         Entry->dmBitsPerPel == dmBitsPerPel &&
72         Entry->dmDisplayFrequency == dmDisplayFrequency)
73     {
74         return 0;
75     }
76     else
77     if ((Entry->dmPelsWidth  < dmPelsWidth) ||
78         (Entry->dmPelsWidth == dmPelsWidth && Entry->dmPelsHeight < dmPelsHeight) ||
79         (Entry->dmPelsWidth == dmPelsWidth && Entry->dmPelsHeight == dmPelsHeight &&
80          Entry->dmBitsPerPel < dmBitsPerPel))
81     {
82         return 1;
83     }
84     return -1;
85 }
86 
87 static PSETTINGS_ENTRY
88 GetPossibleSettings(IN LPCTSTR DeviceName, OUT DWORD* pSettingsCount, OUT PSETTINGS_ENTRY* CurrentSettings)
89 {
90     DEVMODE devmode;
91     DWORD NbSettings = 0;
92     DWORD iMode = 0;
93     DWORD dwFlags = 0;
94     PSETTINGS_ENTRY Settings = NULL;
95     HDC hDC;
96     PSETTINGS_ENTRY Current;
97     DWORD bpp, xres, yres;
98     DWORD curDispFreq;
99 
100     /* Get current settings */
101     *CurrentSettings = NULL;
102     hDC = CreateIC(NULL, DeviceName, NULL, NULL);
103     bpp = GetDeviceCaps(hDC, PLANES);
104     bpp *= GetDeviceCaps(hDC, BITSPIXEL);
105     xres = GetDeviceCaps(hDC, HORZRES);
106     yres = GetDeviceCaps(hDC, VERTRES);
107     DeleteDC(hDC);
108 
109     /* List all settings */
110     devmode.dmSize = (WORD)sizeof(devmode);
111     devmode.dmDriverExtra = 0;
112 
113     if (!EnumDisplaySettingsEx(DeviceName, ENUM_CURRENT_SETTINGS, &devmode, dwFlags))
114         return NULL;
115 
116     curDispFreq = devmode.dmDisplayFrequency;
117 
118     while (EnumDisplaySettingsEx(DeviceName, iMode, &devmode, dwFlags))
119     {
120         iMode++;
121 
122         if (devmode.dmPelsWidth < 640 ||
123             devmode.dmPelsHeight < 480 ||
124             devmode.dmDisplayFrequency != curDispFreq ||
125             (devmode.dmBitsPerPel != 4 &&
126              devmode.dmBitsPerPel != 8 &&
127              devmode.dmBitsPerPel != 16 &&
128              devmode.dmBitsPerPel != 24 &&
129              devmode.dmBitsPerPel != 32))
130         {
131             continue;
132         }
133 
134         Current = HeapAlloc(GetProcessHeap(), 0, sizeof(SETTINGS_ENTRY));
135         if (Current != NULL)
136         {
137             /* Sort resolutions by increasing height, and BPP */
138             PSETTINGS_ENTRY Previous = NULL;
139             PSETTINGS_ENTRY Next = Settings;
140             Current->dmPelsWidth = devmode.dmPelsWidth;
141             Current->dmPelsHeight = devmode.dmPelsHeight;
142             Current->dmBitsPerPel = devmode.dmBitsPerPel;
143             Current->dmDisplayFrequency = devmode.dmDisplayFrequency;
144             while (Next != NULL &&
145                    CompareSettings(Next, devmode.dmPelsWidth,
146                                    devmode.dmPelsHeight, devmode.dmBitsPerPel,
147                                    devmode.dmDisplayFrequency) > 0)
148             {
149                 Previous = Next;
150                 Next = Next->Flink;
151             }
152             Current->Blink = Previous;
153             Current->Flink = Next;
154             if (Previous == NULL)
155                 Settings = Current;
156             else
157                 Previous->Flink = Current;
158             if (Next != NULL)
159                 Next->Blink = Current;
160             if (devmode.dmPelsWidth == xres && devmode.dmPelsHeight == yres && devmode.dmBitsPerPel == bpp)
161             {
162                 *CurrentSettings = Current;
163             }
164             NbSettings++;
165         }
166     }
167 
168     *pSettingsCount = NbSettings;
169     return Settings;
170 }
171 
172 static BOOL
173 AddDisplayDevice(IN PSETTINGS_DATA pData, IN const DISPLAY_DEVICE *DisplayDevice)
174 {
175     PDISPLAY_DEVICE_ENTRY newEntry = NULL;
176     LPTSTR description = NULL;
177     LPTSTR name = NULL;
178     LPTSTR key = NULL;
179     LPTSTR devid = NULL;
180     SIZE_T descriptionSize, nameSize, keySize, devidSize;
181     PSETTINGS_ENTRY Current;
182     DWORD ResolutionsCount = 1;
183     DWORD i;
184 
185     newEntry = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DISPLAY_DEVICE_ENTRY));
186     if (!newEntry) goto ByeBye;
187 
188     newEntry->Settings = GetPossibleSettings(DisplayDevice->DeviceName, &newEntry->SettingsCount, &newEntry->CurrentSettings);
189     if (!newEntry->Settings) goto ByeBye;
190 
191     newEntry->InitialSettings.dmPelsWidth = newEntry->CurrentSettings->dmPelsWidth;
192     newEntry->InitialSettings.dmPelsHeight = newEntry->CurrentSettings->dmPelsHeight;
193     newEntry->InitialSettings.dmBitsPerPel = newEntry->CurrentSettings->dmBitsPerPel;
194 
195     /* Count different resolutions */
196     for (Current = newEntry->Settings; Current != NULL; Current = Current->Flink)
197     {
198         if (Current->Flink != NULL &&
199             ((Current->dmPelsWidth != Current->Flink->dmPelsWidth) ||
200              (Current->dmPelsHeight != Current->Flink->dmPelsHeight)))
201         {
202             ResolutionsCount++;
203         }
204     }
205 
206     newEntry->Resolutions = HeapAlloc(GetProcessHeap(), 0, ResolutionsCount * sizeof(RESOLUTION_INFO));
207     if (!newEntry->Resolutions) goto ByeBye;
208 
209     newEntry->ResolutionsCount = ResolutionsCount;
210 
211     /* Fill resolutions infos */
212     for (Current = newEntry->Settings, i = 0; Current != NULL; Current = Current->Flink)
213     {
214         if (Current->Flink == NULL ||
215             (Current->Flink != NULL &&
216             ((Current->dmPelsWidth != Current->Flink->dmPelsWidth) ||
217              (Current->dmPelsHeight != Current->Flink->dmPelsHeight))))
218         {
219             newEntry->Resolutions[i].dmPelsWidth = Current->dmPelsWidth;
220             newEntry->Resolutions[i].dmPelsHeight = Current->dmPelsHeight;
221             i++;
222         }
223     }
224     descriptionSize = (_tcslen(DisplayDevice->DeviceString) + 1) * sizeof(TCHAR);
225     description = HeapAlloc(GetProcessHeap(), 0, descriptionSize);
226     if (!description) goto ByeBye;
227 
228     nameSize = (_tcslen(DisplayDevice->DeviceName) + 1) * sizeof(TCHAR);
229     name = HeapAlloc(GetProcessHeap(), 0, nameSize);
230     if (!name) goto ByeBye;
231 
232     keySize = (_tcslen(DisplayDevice->DeviceKey) + 1) * sizeof(TCHAR);
233     key = HeapAlloc(GetProcessHeap(), 0, keySize);
234     if (!key) goto ByeBye;
235 
236     devidSize = (_tcslen(DisplayDevice->DeviceID) + 1) * sizeof(TCHAR);
237     devid = HeapAlloc(GetProcessHeap(), 0, devidSize);
238     if (!devid) goto ByeBye;
239 
240     memcpy(description, DisplayDevice->DeviceString, descriptionSize);
241     memcpy(name, DisplayDevice->DeviceName, nameSize);
242     memcpy(key, DisplayDevice->DeviceKey, keySize);
243     memcpy(devid, DisplayDevice->DeviceID, devidSize);
244     newEntry->DeviceDescription = description;
245     newEntry->DeviceName = name;
246     newEntry->DeviceKey = key;
247     newEntry->DeviceID = devid;
248     newEntry->DeviceStateFlags = DisplayDevice->StateFlags;
249     newEntry->Flink = pData->DisplayDeviceList;
250     pData->DisplayDeviceList = newEntry;
251     return TRUE;
252 
253 ByeBye:
254     if (newEntry != NULL)
255     {
256         if (newEntry->Settings != NULL)
257         {
258             Current = newEntry->Settings;
259             while (Current != NULL)
260             {
261                 PSETTINGS_ENTRY Next = Current->Flink;
262                 HeapFree(GetProcessHeap(), 0, Current);
263                 Current = Next;
264             }
265         }
266         if (newEntry->Resolutions != NULL)
267             HeapFree(GetProcessHeap(), 0, newEntry->Resolutions);
268         HeapFree(GetProcessHeap(), 0, newEntry);
269     }
270     if (description != NULL)
271         HeapFree(GetProcessHeap(), 0, description);
272     if (name != NULL)
273         HeapFree(GetProcessHeap(), 0, name);
274     if (key != NULL)
275         HeapFree(GetProcessHeap(), 0, key);
276     return FALSE;
277 }
278 
279 static VOID
280 OnDisplayDeviceChanged(IN HWND hwndDlg, IN PSETTINGS_DATA pData, IN PDISPLAY_DEVICE_ENTRY pDeviceEntry)
281 {
282     PSETTINGS_ENTRY Current;
283     DWORD index;
284 
285     pData->CurrentDisplayDevice = pDeviceEntry; /* Update variable */
286 
287     /* Fill color depths combo box */
288     SendDlgItemMessage(hwndDlg, IDC_SETTINGS_BPP, CB_RESETCONTENT, 0, 0);
289     for (Current = pDeviceEntry->Settings; Current != NULL; Current = Current->Flink)
290     {
291         TCHAR Buffer[64];
292         if (LoadString(hApplet, (2900 + Current->dmBitsPerPel), Buffer, sizeof(Buffer) / sizeof(TCHAR)))
293         {
294             index = (DWORD) SendDlgItemMessage(hwndDlg, IDC_SETTINGS_BPP, CB_FINDSTRINGEXACT, (WPARAM)-1, (LPARAM)Buffer);
295             if (index == (DWORD)CB_ERR)
296             {
297                 index = (DWORD) SendDlgItemMessage(hwndDlg, IDC_SETTINGS_BPP, CB_ADDSTRING, 0, (LPARAM)Buffer);
298                 SendDlgItemMessage(hwndDlg, IDC_SETTINGS_BPP, CB_SETITEMDATA, index, Current->dmBitsPerPel);
299             }
300         }
301     }
302 
303     /* Fill resolutions slider */
304     SendDlgItemMessage(hwndDlg, IDC_SETTINGS_RESOLUTION, TBM_CLEARTICS, TRUE, 0);
305     SendDlgItemMessage(hwndDlg, IDC_SETTINGS_RESOLUTION, TBM_SETRANGE, TRUE, MAKELONG(0, pDeviceEntry->ResolutionsCount - 1));
306 
307     UpdateDisplay(hwndDlg, pData, TRUE);
308 }
309 
310 static VOID
311 SettingsOnInitDialog(IN HWND hwndDlg)
312 {
313     BITMAP bitmap;
314     DWORD Result = 0;
315     DWORD iDevNum = 0;
316     DWORD i;
317     DISPLAY_DEVICE displayDevice;
318     PSETTINGS_DATA pData;
319 
320     pData = HeapAlloc(GetProcessHeap(), 0, sizeof(SETTINGS_DATA));
321     if (pData == NULL)
322         return;
323 
324     SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)pData);
325 
326     /* Get video cards list */
327     pData->DisplayDeviceList = NULL;
328     displayDevice.cb = sizeof(displayDevice);
329     while (EnumDisplayDevices(NULL, iDevNum, &displayDevice, 0x1))
330     {
331         if ((displayDevice.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) != 0)
332         {
333             if (AddDisplayDevice(pData, &displayDevice))
334                 Result++;
335         }
336         iDevNum++;
337     }
338 
339     if (Result == 0)
340     {
341         /* No adapter found */
342         EnableWindow(GetDlgItem(hwndDlg, IDC_SETTINGS_BPP), FALSE);
343         EnableWindow(GetDlgItem(hwndDlg, IDC_SETTINGS_RESOLUTION), FALSE);
344         EnableWindow(GetDlgItem(hwndDlg, IDC_SETTINGS_RESOLUTION_TEXT), FALSE);
345         EnableWindow(GetDlgItem(hwndDlg, IDC_SETTINGS_ADVANCED), FALSE);
346         ShowWindow(GetDlgItem(hwndDlg, IDC_SETTINGS_SPECTRUM), SW_HIDE);
347 
348         /* Do not initialize the color spectrum bitmaps */
349         memset(pData->hSpectrumBitmaps, 0, sizeof(pData->hSpectrumBitmaps));
350         return;
351     }
352     else if (Result == 1)
353     {
354         MONSL_MONINFO monitors;
355 
356         /* Single video adapter */
357         SendDlgItemMessage(hwndDlg, IDC_SETTINGS_DEVICE, WM_SETTEXT, 0, (LPARAM)pData->DisplayDeviceList->DeviceDescription);
358         OnDisplayDeviceChanged(hwndDlg, pData, pData->DisplayDeviceList);
359 
360         monitors.Position.x = monitors.Position.y = 0;
361         monitors.Size.cx = pData->CurrentDisplayDevice->CurrentSettings->dmPelsWidth;
362         monitors.Size.cy = pData->CurrentDisplayDevice->CurrentSettings->dmPelsHeight;
363         monitors.Flags = 0;
364         SendDlgItemMessage(hwndDlg,
365                    IDC_SETTINGS_MONSEL,
366                    MSLM_SETMONITORSINFO,
367                    1,
368                    (LPARAM)&monitors);
369     }
370     else /* FIXME: Incomplete! */
371     {
372         PMONSL_MONINFO pMonitors;
373         DWORD i;
374 
375         SendDlgItemMessage(hwndDlg, IDC_SETTINGS_DEVICE, WM_SETTEXT, 0, (LPARAM)pData->DisplayDeviceList->DeviceDescription);
376         OnDisplayDeviceChanged(hwndDlg, pData, pData->DisplayDeviceList);
377 
378         pMonitors = (PMONSL_MONINFO)HeapAlloc(GetProcessHeap(), 0, sizeof(MONSL_MONINFO) * Result);
379         if (pMonitors)
380         {
381             DWORD hack = 1280;
382             for (i = 0; i < Result; i++)
383             {
384                 pMonitors[i].Position.x = hack * i;
385                 pMonitors[i].Position.y = 0;
386                 pMonitors[i].Size.cx = pData->DisplayDeviceList->CurrentSettings->dmPelsWidth;
387                 pMonitors[i].Size.cy = pData->DisplayDeviceList->CurrentSettings->dmPelsHeight;
388                 pMonitors[i].Flags = 0;
389             }
390 
391             SendDlgItemMessage(hwndDlg,
392                        IDC_SETTINGS_MONSEL,
393                        MSLM_SETMONITORSINFO,
394                        Result,
395                        (LPARAM)pMonitors);
396 
397             HeapFree(GetProcessHeap(), 0, pMonitors);
398         }
399     }
400 
401     /* Initialize the color spectrum bitmaps */
402     for (i = 0; i < NUM_SPECTRUM_BITMAPS; i++)
403     {
404         pData->hSpectrumBitmaps[i] = LoadImageW(hApplet, MAKEINTRESOURCEW(IDB_SPECTRUM_4 + i), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
405 
406         if (pData->hSpectrumBitmaps[i] != NULL)
407         {
408             if (GetObjectW(pData->hSpectrumBitmaps[i], sizeof(BITMAP), &bitmap) != 0)
409             {
410                 pData->cxSource[i] = bitmap.bmWidth;
411                 pData->cySource[i] = bitmap.bmHeight;
412             }
413             else
414             {
415                 pData->cxSource[i] = 0;
416                 pData->cySource[i] = 0;
417             }
418         }
419     }
420 }
421 
422 /* Get the ID for SETTINGS_DATA::hSpectrumBitmaps */
423 static VOID
424 ShowColorSpectrum(IN HDC hDC, IN LPRECT client, IN DWORD BitsPerPel, IN PSETTINGS_DATA pData)
425 {
426     HDC hdcMem;
427     INT iBitmap;
428 
429     hdcMem = CreateCompatibleDC(hDC);
430 
431     if (!hdcMem)
432         return;
433 
434     switch(BitsPerPel)
435     {
436         case 4:  iBitmap = 0; break;
437         case 8:  iBitmap = 1; break;
438         default: iBitmap = 2;
439     }
440 
441     if (SelectObject(hdcMem, pData->hSpectrumBitmaps[iBitmap]))
442     {
443         StretchBlt(hDC,
444                client->left, client->top,
445                client->right - client->left,
446                client->bottom - client->top,
447                hdcMem, 0, 0,
448                pData->cxSource[iBitmap],
449                pData->cySource[iBitmap], SRCCOPY);
450     }
451 
452     DeleteDC(hdcMem);
453 }
454 
455 static VOID
456 OnBPPChanged(IN HWND hwndDlg, IN PSETTINGS_DATA pData)
457 {
458     /* If new BPP is not compatible with resolution:
459      * 1) try to find the nearest smaller matching resolution
460      * 2) otherwise, get the nearest bigger resolution
461      */
462     PSETTINGS_ENTRY Current;
463     DWORD dmNewBitsPerPel;
464     DWORD index;
465     HDC hSpectrumDC;
466     HWND hSpectrumControl;
467     RECT client;
468 
469     index = (DWORD) SendDlgItemMessage(hwndDlg, IDC_SETTINGS_BPP, CB_GETCURSEL, 0, 0);
470     dmNewBitsPerPel = (DWORD) SendDlgItemMessage(hwndDlg, IDC_SETTINGS_BPP, CB_GETITEMDATA, index, 0);
471 
472     /* Show a new spectrum bitmap */
473     hSpectrumControl = GetDlgItem(hwndDlg, IDC_SETTINGS_SPECTRUM);
474     hSpectrumDC = GetDC(hSpectrumControl);
475     if (hSpectrumDC == NULL)
476         return;
477 
478     GetClientRect(hSpectrumControl, &client);
479     ShowColorSpectrum(hSpectrumDC, &client, dmNewBitsPerPel, pData);
480     ReleaseDC(hSpectrumControl, hSpectrumDC);
481 
482     /* Find if new parameters are valid */
483     Current = pData->CurrentDisplayDevice->CurrentSettings;
484     if (dmNewBitsPerPel == Current->dmBitsPerPel)
485     {
486         /* No change */
487         return;
488     }
489 
490     PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
491 
492     if (dmNewBitsPerPel < Current->dmBitsPerPel)
493     {
494         Current = Current->Blink;
495         while (Current != NULL)
496         {
497             if (Current->dmBitsPerPel == dmNewBitsPerPel
498              && Current->dmPelsHeight == pData->CurrentDisplayDevice->CurrentSettings->dmPelsHeight
499              && Current->dmPelsWidth == pData->CurrentDisplayDevice->CurrentSettings->dmPelsWidth)
500             {
501                 pData->CurrentDisplayDevice->CurrentSettings = Current;
502                 UpdateDisplay(hwndDlg, pData, TRUE);
503                 return;
504             }
505             Current = Current->Blink;
506         }
507     }
508     else
509     {
510         Current = Current->Flink;
511         while (Current != NULL)
512         {
513             if (Current->dmBitsPerPel == dmNewBitsPerPel
514              && Current->dmPelsHeight == pData->CurrentDisplayDevice->CurrentSettings->dmPelsHeight
515              && Current->dmPelsWidth == pData->CurrentDisplayDevice->CurrentSettings->dmPelsWidth)
516             {
517                 pData->CurrentDisplayDevice->CurrentSettings = Current;
518                 UpdateDisplay(hwndDlg, pData, TRUE);
519                 return;
520             }
521             Current = Current->Flink;
522         }
523     }
524 
525     /* Search smaller resolution compatible with current color depth */
526     Current = pData->CurrentDisplayDevice->CurrentSettings->Blink;
527     while (Current != NULL)
528     {
529         if (Current->dmBitsPerPel == dmNewBitsPerPel)
530         {
531             pData->CurrentDisplayDevice->CurrentSettings = Current;
532             UpdateDisplay(hwndDlg, pData, TRUE);
533             return;
534         }
535         Current = Current->Blink;
536     }
537 
538     /* Search bigger resolution compatible with current color depth */
539     Current = pData->CurrentDisplayDevice->CurrentSettings->Flink;
540     while (Current != NULL)
541     {
542         if (Current->dmBitsPerPel == dmNewBitsPerPel)
543         {
544             pData->CurrentDisplayDevice->CurrentSettings = Current;
545             UpdateDisplay(hwndDlg, pData, TRUE);
546             return;
547         }
548         Current = Current->Flink;
549     }
550 
551     /* We shouldn't go there */
552 }
553 
554 static VOID
555 OnResolutionChanged(IN HWND hwndDlg, IN PSETTINGS_DATA pData, IN DWORD NewPosition,
556                     IN BOOL bUpdateThumb)
557 {
558     /* If new resolution is not compatible with color depth:
559      * 1) try to find the nearest bigger matching color depth
560      * 2) otherwise, get the nearest smaller color depth
561      */
562     PSETTINGS_ENTRY Current;
563     DWORD dmNewPelsHeight = pData->CurrentDisplayDevice->Resolutions[NewPosition].dmPelsHeight;
564     DWORD dmNewPelsWidth = pData->CurrentDisplayDevice->Resolutions[NewPosition].dmPelsWidth;
565     DWORD dmBitsPerPel;
566     DWORD dmDisplayFrequency;
567 
568     /* Find if new parameters are valid */
569     Current = pData->CurrentDisplayDevice->CurrentSettings;
570     if (dmNewPelsHeight == Current->dmPelsHeight && dmNewPelsWidth == Current->dmPelsWidth)
571     {
572         /* No change */
573         return;
574     }
575 
576     PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
577 
578     dmBitsPerPel = Current->dmBitsPerPel;
579     dmDisplayFrequency = Current->dmDisplayFrequency;
580 
581     if (CompareSettings(Current, dmNewPelsWidth,
582                         dmNewPelsHeight, dmBitsPerPel,
583                         dmDisplayFrequency) < 0)
584     {
585         Current = Current->Blink;
586         while (Current != NULL)
587         {
588             if (Current->dmPelsHeight == dmNewPelsHeight
589              && Current->dmPelsWidth  == dmNewPelsWidth
590              && Current->dmBitsPerPel == dmBitsPerPel)
591             {
592                 pData->CurrentDisplayDevice->CurrentSettings = Current;
593                 UpdateDisplay(hwndDlg, pData, bUpdateThumb);
594                 return;
595             }
596             Current = Current->Blink;
597         }
598     }
599     else
600     {
601         Current = Current->Flink;
602         while (Current != NULL)
603         {
604             if (Current->dmPelsHeight == dmNewPelsHeight
605              && Current->dmPelsWidth  == dmNewPelsWidth
606              && Current->dmBitsPerPel == dmBitsPerPel)
607             {
608                 pData->CurrentDisplayDevice->CurrentSettings = Current;
609                 UpdateDisplay(hwndDlg, pData, bUpdateThumb);
610                 return;
611             }
612             Current = Current->Flink;
613         }
614     }
615 
616     /* Search bigger color depth compatible with current resolution */
617     Current = pData->CurrentDisplayDevice->CurrentSettings->Flink;
618     while (Current != NULL)
619     {
620         if (dmNewPelsHeight == Current->dmPelsHeight && dmNewPelsWidth == Current->dmPelsWidth)
621         {
622             pData->CurrentDisplayDevice->CurrentSettings = Current;
623             UpdateDisplay(hwndDlg, pData, bUpdateThumb);
624             return;
625         }
626         Current = Current->Flink;
627     }
628 
629     /* Search smaller color depth compatible with current resolution */
630     Current = pData->CurrentDisplayDevice->CurrentSettings->Blink;
631     while (Current != NULL)
632     {
633         if (dmNewPelsHeight == Current->dmPelsHeight && dmNewPelsWidth == Current->dmPelsWidth)
634         {
635             pData->CurrentDisplayDevice->CurrentSettings = Current;
636             UpdateDisplay(hwndDlg, pData, bUpdateThumb);
637             return;
638         }
639         Current = Current->Blink;
640     }
641 
642     /* We shouldn't go there */
643 }
644 
645 /* Property sheet page callback */
646 UINT CALLBACK
647 SettingsPageCallbackProc(HWND hwnd, UINT uMsg, LPPROPSHEETPAGE ppsp)
648 {
649     UINT Ret = 0;
650 
651     switch (uMsg)
652     {
653         case PSPCB_CREATE:
654             Ret = RegisterMonitorSelectionControl(hApplet);
655             break;
656 
657         case PSPCB_RELEASE:
658             UnregisterMonitorSelectionControl(hApplet);
659             break;
660     }
661 
662     return Ret;
663 }
664 
665 static INT_PTR CALLBACK
666 ConfirmDlgProc(IN HWND hwndDlg, IN UINT uMsg, IN WPARAM wParam, IN LPARAM lParam)
667 {
668     PTIMEOUTDATA pData;
669 
670     pData = (PTIMEOUTDATA)GetWindowLongPtr(hwndDlg, DWLP_USER);
671 
672     switch(uMsg)
673     {
674         case WM_INITDIALOG:
675             /* Allocate the local dialog data */
676             pData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(TIMEOUTDATA));
677             if (pData == NULL)
678                 return FALSE;
679 
680             /* Link the dialog data to the dialog */
681             SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)pData);
682 
683             /* Timeout in seconds */
684             pData->nTimeout = 15;
685 
686             /* Load the raw timeout string */
687             LoadString(hApplet, IDS_TIMEOUTTEXT, pData->szRawBuffer, ARRAYSIZE(pData->szRawBuffer));
688 
689             /* Cook the timeout string and show it */
690             _stprintf(pData->szCookedBuffer, pData->szRawBuffer, pData->nTimeout);
691             SetDlgItemText(hwndDlg, IDC_TIMEOUTTEXT, pData->szCookedBuffer);
692 
693             /* Start the timer (ticks every second)*/
694             SetTimer(hwndDlg, 1, 1000, NULL);
695             break;
696 
697         case WM_TIMER:
698             /* Update the timepout value */
699             pData->nTimeout--;
700 
701             /* Update the timeout text */
702             _stprintf(pData->szCookedBuffer, pData->szRawBuffer, pData->nTimeout);
703             SetDlgItemText(hwndDlg, IDC_TIMEOUTTEXT, pData->szCookedBuffer);
704 
705             /* Kill the timer and return a 'No', if we ran out of time */
706             if (pData->nTimeout == 0)
707             {
708                 KillTimer(hwndDlg, 1);
709                 EndDialog(hwndDlg, IDNO);
710             }
711             break;
712 
713         case WM_COMMAND:
714             /* Kill the timer and return the clicked button id */
715             KillTimer(hwndDlg, 1);
716             EndDialog(hwndDlg, LOWORD(wParam));
717             break;
718 
719         case WM_DESTROY:
720             /* Free the local dialog data */
721             HeapFree(GetProcessHeap(), 0, pData);
722             break;
723     }
724 
725     return FALSE;
726 }
727 
728 static VOID
729 ApplyDisplaySettings(HWND hwndDlg, PSETTINGS_DATA pData)
730 {
731     TCHAR Message[1024], Title[256];
732     DEVMODE devmode;
733     LONG rc;
734 
735     RtlZeroMemory(&devmode, sizeof(devmode));
736     devmode.dmSize = (WORD)sizeof(devmode);
737     devmode.dmPelsWidth = pData->CurrentDisplayDevice->CurrentSettings->dmPelsWidth;
738     devmode.dmPelsHeight = pData->CurrentDisplayDevice->CurrentSettings->dmPelsHeight;
739     devmode.dmBitsPerPel = pData->CurrentDisplayDevice->CurrentSettings->dmBitsPerPel;
740     devmode.dmDisplayFrequency = pData->CurrentDisplayDevice->CurrentSettings->dmDisplayFrequency;
741     devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
742 
743     rc = ChangeDisplaySettingsEx(pData->CurrentDisplayDevice->DeviceName,
744                                  &devmode,
745                                  NULL,
746                                  CDS_UPDATEREGISTRY,
747                                  NULL);
748     switch (rc)
749     {
750         case DISP_CHANGE_SUCCESSFUL:
751             break;
752 
753         case DISP_CHANGE_RESTART:
754             LoadString(hApplet, IDS_DISPLAY_SETTINGS, Title, sizeof(Title) / sizeof(TCHAR));
755             LoadString(hApplet, IDS_APPLY_NEEDS_RESTART, Message, sizeof(Message) / sizeof (TCHAR));
756             MessageBox(hwndDlg, Message, Title, MB_OK | MB_ICONINFORMATION);
757             return;
758 
759         case DISP_CHANGE_FAILED:
760         default:
761             LoadString(hApplet, IDS_DISPLAY_SETTINGS, Title, sizeof(Title) / sizeof(TCHAR));
762             LoadString(hApplet, IDS_APPLY_FAILED, Message, sizeof(Message) / sizeof (TCHAR));
763             MessageBox(hwndDlg, Message, Title, MB_OK | MB_ICONSTOP);
764             return;
765     }
766 
767     if (DialogBox(hApplet, MAKEINTRESOURCE(IDD_CONFIRMSETTINGS), hwndDlg, ConfirmDlgProc) == IDYES)
768     {
769         pData->CurrentDisplayDevice->InitialSettings.dmPelsWidth = pData->CurrentDisplayDevice->CurrentSettings->dmPelsWidth;
770         pData->CurrentDisplayDevice->InitialSettings.dmPelsHeight = pData->CurrentDisplayDevice->CurrentSettings->dmPelsHeight;
771         pData->CurrentDisplayDevice->InitialSettings.dmBitsPerPel = pData->CurrentDisplayDevice->CurrentSettings->dmBitsPerPel;
772     }
773     else
774     {
775         devmode.dmPelsWidth = pData->CurrentDisplayDevice->InitialSettings.dmPelsWidth;
776         devmode.dmPelsHeight = pData->CurrentDisplayDevice->InitialSettings.dmPelsHeight;
777         devmode.dmBitsPerPel = pData->CurrentDisplayDevice->InitialSettings.dmBitsPerPel;
778         devmode.dmDisplayFrequency = pData->CurrentDisplayDevice->InitialSettings.dmDisplayFrequency;
779 
780         rc = ChangeDisplaySettingsEx(pData->CurrentDisplayDevice->DeviceName,
781                                      &devmode,
782                                      NULL,
783                                      CDS_UPDATEREGISTRY,
784                                      NULL);
785         switch (rc)
786         {
787             case DISP_CHANGE_SUCCESSFUL:
788                 pData->CurrentDisplayDevice->CurrentSettings->dmPelsWidth = pData->CurrentDisplayDevice->InitialSettings.dmPelsWidth;
789                 pData->CurrentDisplayDevice->CurrentSettings->dmPelsHeight = pData->CurrentDisplayDevice->InitialSettings.dmPelsHeight;
790                 pData->CurrentDisplayDevice->CurrentSettings->dmBitsPerPel = pData->CurrentDisplayDevice->InitialSettings.dmBitsPerPel;
791                 UpdateDisplay(hwndDlg, pData, TRUE);
792                 break;
793 
794             case DISP_CHANGE_RESTART:
795                 LoadString(hApplet, IDS_DISPLAY_SETTINGS, Title, sizeof(Title) / sizeof(TCHAR));
796                 LoadString(hApplet, IDS_APPLY_NEEDS_RESTART, Message, sizeof(Message) / sizeof (TCHAR));
797                 MessageBox(hwndDlg, Message, Title, MB_OK | MB_ICONINFORMATION);
798                 return;
799 
800             case DISP_CHANGE_FAILED:
801             default:
802                 LoadString(hApplet, IDS_DISPLAY_SETTINGS, Title, sizeof(Title) / sizeof(TCHAR));
803                 LoadString(hApplet, IDS_APPLY_FAILED, Message, sizeof(Message) / sizeof (TCHAR));
804                 MessageBox(hwndDlg, Message, Title, MB_OK | MB_ICONSTOP);
805                 return;
806         }
807     }
808 }
809 
810 /* Property page dialog callback */
811 INT_PTR CALLBACK
812 SettingsPageProc(IN HWND hwndDlg, IN UINT uMsg, IN WPARAM wParam, IN LPARAM lParam)
813 {
814     PSETTINGS_DATA pData;
815 
816     pData = (PSETTINGS_DATA)GetWindowLongPtr(hwndDlg, DWLP_USER);
817 
818     switch(uMsg)
819     {
820         case WM_INITDIALOG:
821         {
822             SettingsOnInitDialog(hwndDlg);
823             break;
824         }
825         case WM_DRAWITEM:
826         {
827             LPDRAWITEMSTRUCT lpDrawItem;
828             lpDrawItem = (LPDRAWITEMSTRUCT) lParam;
829 
830             if (lpDrawItem->CtlID == IDC_SETTINGS_SPECTRUM)
831                 ShowColorSpectrum(lpDrawItem->hDC, &lpDrawItem->rcItem, pData->CurrentDisplayDevice->CurrentSettings->dmBitsPerPel, pData);
832             break;
833         }
834         case WM_COMMAND:
835         {
836             DWORD controlId = LOWORD(wParam);
837             DWORD command   = HIWORD(wParam);
838 
839             if (controlId == IDC_SETTINGS_ADVANCED && command == BN_CLICKED)
840                 DisplayAdvancedSettings(hwndDlg, pData->CurrentDisplayDevice);
841             else if (controlId == IDC_SETTINGS_BPP && command == CBN_SELCHANGE)
842                 OnBPPChanged(hwndDlg, pData);
843             break;
844         }
845         case WM_HSCROLL:
846         {
847             switch (LOWORD(wParam))
848             {
849                 case TB_LINEUP:
850                 case TB_LINEDOWN:
851                 case TB_PAGEUP:
852                 case TB_PAGEDOWN:
853                 case TB_TOP:
854                 case TB_BOTTOM:
855                 case TB_ENDTRACK:
856                 {
857                     DWORD newPosition = (DWORD) SendDlgItemMessage(hwndDlg, IDC_SETTINGS_RESOLUTION, TBM_GETPOS, 0, 0);
858                     OnResolutionChanged(hwndDlg, pData, newPosition, TRUE);
859                     break;
860                 }
861 
862                 case TB_THUMBTRACK:
863                     OnResolutionChanged(hwndDlg, pData, HIWORD(wParam), FALSE);
864                     break;
865             }
866             break;
867         }
868         case WM_NOTIFY:
869         {
870             LPNMHDR lpnm = (LPNMHDR)lParam;
871             if (lpnm->code == (UINT)PSN_APPLY)
872             {
873                 if (pData->CurrentDisplayDevice->CurrentSettings->dmPelsWidth != pData->CurrentDisplayDevice->InitialSettings.dmPelsWidth
874                  || pData->CurrentDisplayDevice->CurrentSettings->dmPelsHeight != pData->CurrentDisplayDevice->InitialSettings.dmPelsHeight
875                  || pData->CurrentDisplayDevice->CurrentSettings->dmBitsPerPel != pData->CurrentDisplayDevice->InitialSettings.dmBitsPerPel)
876                 {
877                     /* Apply new settings */
878                     ApplyDisplaySettings(hwndDlg, pData);
879                 }
880             }
881             break;
882         }
883 
884         case WM_CONTEXTMENU:
885         {
886             HWND hwndMonSel;
887             HMENU hPopup;
888             UINT uiCmd;
889             POINT pt, ptClient;
890             INT Index;
891 
892             pt.x = (SHORT)LOWORD(lParam);
893             pt.y = (SHORT)HIWORD(lParam);
894 
895             hwndMonSel = GetDlgItem(hwndDlg,
896                                     IDC_SETTINGS_MONSEL);
897             if ((HWND)wParam == hwndMonSel)
898             {
899                 if (pt.x == -1 && pt.y == -1)
900                 {
901                     RECT rcMon;
902 
903                     Index = (INT)SendMessage(hwndMonSel,
904                                              MSLM_GETCURSEL,
905                                              0,
906                                              0);
907 
908                     if (Index >= 0 &&
909                         (INT)SendMessage(hwndMonSel,
910                                          MSLM_GETMONITORRECT,
911                                          Index,
912                                          (LPARAM)&rcMon) > 0)
913                     {
914                         pt.x = rcMon.left + ((rcMon.right - rcMon.left) / 2);
915                         pt.y = rcMon.top + ((rcMon.bottom - rcMon.top) / 2);
916                     }
917                     else
918                         pt.x = pt.y = 0;
919 
920                     MapWindowPoints(hwndMonSel,
921                                     NULL,
922                                     &pt,
923                                     1);
924                 }
925                 else
926                 {
927                     ptClient = pt;
928                     MapWindowPoints(NULL,
929                                     hwndMonSel,
930                                     &ptClient,
931                                     1);
932 
933                     Index = (INT)SendMessage(hwndMonSel,
934                                              MSLM_HITTEST,
935                                              (WPARAM)&ptClient,
936                                              0);
937                 }
938 
939                 if (Index >= 0)
940                 {
941                     hPopup = LoadPopupMenu(hApplet,
942                                            MAKEINTRESOURCE(IDM_MONITOR_MENU));
943                     if (hPopup != NULL)
944                     {
945                         /* FIXME: Enable/Disable menu items */
946                         EnableMenuItem(hPopup,
947                                        ID_MENU_ATTACHED,
948                                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
949                         EnableMenuItem(hPopup,
950                                        ID_MENU_PRIMARY,
951                                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
952                         EnableMenuItem(hPopup,
953                                        ID_MENU_IDENTIFY,
954                                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
955                         EnableMenuItem(hPopup,
956                                        ID_MENU_PROPERTIES,
957                                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
958 
959                         uiCmd = (UINT)TrackPopupMenu(hPopup,
960                                                      TPM_RETURNCMD | TPM_RIGHTBUTTON,
961                                                      pt.x,
962                                                      pt.y,
963                                                      0,
964                                                      hwndDlg,
965                                                      NULL);
966 
967                         switch (uiCmd)
968                         {
969                             case ID_MENU_ATTACHED:
970                             case ID_MENU_PRIMARY:
971                             case ID_MENU_IDENTIFY:
972                             case ID_MENU_PROPERTIES:
973                                 /* FIXME: Implement */
974                                 break;
975                         }
976 
977                         DestroyMenu(hPopup);
978                     }
979                 }
980             }
981             break;
982         }
983 
984         case WM_DESTROY:
985         {
986             DWORD i;
987             PDISPLAY_DEVICE_ENTRY Current = pData->DisplayDeviceList;
988 
989             while (Current != NULL)
990             {
991                 PDISPLAY_DEVICE_ENTRY Next = Current->Flink;
992                 PSETTINGS_ENTRY CurrentSettings = Current->Settings;
993                 while (CurrentSettings != NULL)
994                 {
995                     PSETTINGS_ENTRY NextSettings = CurrentSettings->Flink;
996                     HeapFree(GetProcessHeap(), 0, CurrentSettings);
997                     CurrentSettings = NextSettings;
998                 }
999                 HeapFree(GetProcessHeap(), 0, Current);
1000                 Current = Next;
1001             }
1002 
1003             for (i = 0; i < NUM_SPECTRUM_BITMAPS; i++)
1004             {
1005                 if (pData->hSpectrumBitmaps[i])
1006                     DeleteObject(pData->hSpectrumBitmaps[i]);
1007             }
1008 
1009             HeapFree(GetProcessHeap(), 0, pData);
1010         }
1011     }
1012     return FALSE;
1013 }
1014