1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
/*
 * COPYRIGHT:       See COPYING in the top level directory
 * PROJECT:         ReactOS Display Control Panel
 * FILE:            dll/cpl/desk/settings.c
 * PURPOSE:         Settings property page
 *
 * PROGRAMMERS:     Trevor McCort (lycan359@gmail.com)
 *                  Hervé Poussineau (hpoussin@reactos.org)
 */

#include "desk.h"

typedef struct _SETTINGS_DATA
{
    PDISPLAY_DEVICE_ENTRY DisplayDeviceList;
    PDISPLAY_DEVICE_ENTRY CurrentDisplayDevice;
    HBITMAP hSpectrumBitmaps[NUM_SPECTRUM_BITMAPS];
    int cxSource[NUM_SPECTRUM_BITMAPS];
    int cySource[NUM_SPECTRUM_BITMAPS];
} SETTINGS_DATA, *PSETTINGS_DATA;

typedef struct _TIMEOUTDATA
{
    TCHAR szRawBuffer[256];
    TCHAR szCookedBuffer[256];
    INT nTimeout;
} TIMEOUTDATA, *PTIMEOUTDATA;

static VOID
UpdateDisplay(IN HWND hwndDlg, PSETTINGS_DATA pData, IN BOOL bUpdateThumb)
{
    TCHAR Buffer[64];
    TCHAR Pixel[64];
    DWORD index;
    HWND hwndMonSel;
    MONSL_MONINFO info;

    LoadString(hApplet, IDS_PIXEL, Pixel, sizeof(Pixel) / sizeof(TCHAR));
    _stprintf(Buffer, Pixel, pData->CurrentDisplayDevice->CurrentSettings->dmPelsWidth, pData->CurrentDisplayDevice->CurrentSettings->dmPelsHeight, Pixel);
    SendDlgItemMessage(hwndDlg, IDC_SETTINGS_RESOLUTION_TEXT, WM_SETTEXT, 0, (LPARAM)Buffer);

    for (index = 0; index < pData->CurrentDisplayDevice->ResolutionsCount; index++)
    {
        if (pData->CurrentDisplayDevice->Resolutions[index].dmPelsWidth == pData->CurrentDisplayDevice->CurrentSettings->dmPelsWidth &&
            pData->CurrentDisplayDevice->Resolutions[index].dmPelsHeight == pData->CurrentDisplayDevice->CurrentSettings->dmPelsHeight)
        {
            if (bUpdateThumb)
                SendDlgItemMessage(hwndDlg, IDC_SETTINGS_RESOLUTION, TBM_SETPOS, TRUE, index);
            break;
        }
    }
    if (LoadString(hApplet, (2900 + pData->CurrentDisplayDevice->CurrentSettings->dmBitsPerPel), Buffer, sizeof(Buffer) / sizeof(TCHAR)))
        SendDlgItemMessage(hwndDlg, IDC_SETTINGS_BPP, CB_SELECTSTRING, (WPARAM)-1, (LPARAM)Buffer);

    hwndMonSel = GetDlgItem(hwndDlg, IDC_SETTINGS_MONSEL);
    index = (INT)SendMessage(hwndMonSel, MSLM_GETCURSEL, 0, 0);
    if (index != (DWORD)-1 && SendMessage(hwndMonSel, MSLM_GETMONITORINFO, index, (LPARAM)&info))
    {
        info.Size.cx = pData->CurrentDisplayDevice->CurrentSettings->dmPelsWidth;
        info.Size.cy = pData->CurrentDisplayDevice->CurrentSettings->dmPelsHeight;
        SendMessage(hwndMonSel, MSLM_SETMONITORINFO, index, (LPARAM)&info);
    }
}

static int
CompareSettings(PSETTINGS_ENTRY Entry, DWORD dmPelsWidth, DWORD dmPelsHeight,
                DWORD dmBitsPerPel, DWORD dmDisplayFrequency)
{
    if (Entry->dmPelsWidth  == dmPelsWidth  &&
        Entry->dmPelsHeight == dmPelsHeight &&
        Entry->dmBitsPerPel == dmBitsPerPel &&
        Entry->dmDisplayFrequency == dmDisplayFrequency)
    {
        return 0;
    }
    else
    if ((Entry->dmPelsWidth  < dmPelsWidth) ||
        (Entry->dmPelsWidth == dmPelsWidth && Entry->dmPelsHeight < dmPelsHeight) ||
        (Entry->dmPelsWidth == dmPelsWidth && Entry->dmPelsHeight == dmPelsHeight &&
         Entry->dmBitsPerPel < dmBitsPerPel))
    {
        return 1;
    }
    return -1;
}

static PSETTINGS_ENTRY
GetPossibleSettings(IN LPCTSTR DeviceName, OUT DWORD* pSettingsCount, OUT PSETTINGS_ENTRY* CurrentSettings)
{
    DEVMODE devmode;
    DWORD NbSettings = 0;
    DWORD iMode = 0;
    DWORD dwFlags = 0;
    PSETTINGS_ENTRY Settings = NULL;
    HDC hDC;
    PSETTINGS_ENTRY Current;
    DWORD bpp, xres, yres;
    DWORD curDispFreq;

    /* Get current settings */
    *CurrentSettings = NULL;
    hDC = CreateIC(NULL, DeviceName, NULL, NULL);
    bpp = GetDeviceCaps(hDC, PLANES);
    bpp *= GetDeviceCaps(hDC, BITSPIXEL);
    xres = GetDeviceCaps(hDC, HORZRES);
    yres = GetDeviceCaps(hDC, VERTRES);
    DeleteDC(hDC);

    /* List all settings */
    devmode.dmSize = (WORD)sizeof(devmode);
    devmode.dmDriverExtra = 0;

    if (!EnumDisplaySettingsEx(DeviceName, ENUM_CURRENT_SETTINGS, &devmode, dwFlags))
        return NULL;

    curDispFreq = devmode.dmDisplayFrequency;

    while (EnumDisplaySettingsEx(DeviceName, iMode, &devmode, dwFlags))
    {
        iMode++;

        if (devmode.dmPelsWidth < 640 ||
            devmode.dmPelsHeight < 480 ||
            devmode.dmDisplayFrequency != curDispFreq ||
            (devmode.dmBitsPerPel != 4 &&
             devmode.dmBitsPerPel != 8 &&
             devmode.dmBitsPerPel != 16 &&
             devmode.dmBitsPerPel != 24 &&
             devmode.dmBitsPerPel != 32))
        {
            continue;
        }

        Current = HeapAlloc(GetProcessHeap(), 0, sizeof(SETTINGS_ENTRY));
        if (Current != NULL)
        {
            /* Sort resolutions by increasing height, and BPP */
            PSETTINGS_ENTRY Previous = NULL;
            PSETTINGS_ENTRY Next = Settings;
            Current->dmPelsWidth = devmode.dmPelsWidth;
            Current->dmPelsHeight = devmode.dmPelsHeight;
            Current->dmBitsPerPel = devmode.dmBitsPerPel;
            Current->dmDisplayFrequency = devmode.dmDisplayFrequency;
            while (Next != NULL &&
                   CompareSettings(Next, devmode.dmPelsWidth,
                                   devmode.dmPelsHeight, devmode.dmBitsPerPel,
                                   devmode.dmDisplayFrequency) > 0)
            {
                Previous = Next;
                Next = Next->Flink;
            }
            Current->Blink = Previous;
            Current->Flink = Next;
            if (Previous == NULL)
                Settings = Current;
            else
                Previous->Flink = Current;
            if (Next != NULL)
                Next->Blink = Current;
            if (devmode.dmPelsWidth == xres && devmode.dmPelsHeight == yres && devmode.dmBitsPerPel == bpp)
            {
                *CurrentSettings = Current;
            }
            NbSettings++;
        }
    }

    *pSettingsCount = NbSettings;
    return Settings;
}

static BOOL
AddDisplayDevice(IN PSETTINGS_DATA pData, IN const DISPLAY_DEVICE *DisplayDevice)
{
    PDISPLAY_DEVICE_ENTRY newEntry = NULL;
    LPTSTR description = NULL;
    LPTSTR name = NULL;
    LPTSTR key = NULL;
    LPTSTR devid = NULL;
    SIZE_T descriptionSize, nameSize, keySize, devidSize;
    PSETTINGS_ENTRY Current;
    DWORD ResolutionsCount = 1;
    DWORD i;

    newEntry = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DISPLAY_DEVICE_ENTRY));
    if (!newEntry) goto ByeBye;

    newEntry->Settings = GetPossibleSettings(DisplayDevice->DeviceName, &newEntry->SettingsCount, &newEntry->CurrentSettings);
    if (!newEntry->Settings) goto ByeBye;

    newEntry->InitialSettings.dmPelsWidth = newEntry->CurrentSettings->dmPelsWidth;
    newEntry->InitialSettings.dmPelsHeight = newEntry->CurrentSettings->dmPelsHeight;
    newEntry->InitialSettings.dmBitsPerPel = newEntry->CurrentSettings->dmBitsPerPel;

    /* Count different resolutions */
    for (Current = newEntry->Settings; Current != NULL; Current = Current->Flink)
    {
        if (Current->Flink != NULL &&
            ((Current->dmPelsWidth != Current->Flink->dmPelsWidth) ||
             (Current->dmPelsHeight != Current->Flink->dmPelsHeight)))
        {
            ResolutionsCount++;
        }
    }

    newEntry->Resolutions = HeapAlloc(GetProcessHeap(), 0, ResolutionsCount * sizeof(RESOLUTION_INFO));
    if (!newEntry->Resolutions) goto ByeBye;

    newEntry->ResolutionsCount = ResolutionsCount;

    /* Fill resolutions infos */
    for (Current = newEntry->Settings, i = 0; Current != NULL; Current = Current->Flink)
    {
        if (Current->Flink == NULL ||
            (Current->Flink != NULL &&
            ((Current->dmPelsWidth != Current->Flink->dmPelsWidth) ||
             (Current->dmPelsHeight != Current->Flink->dmPelsHeight))))
        {
            newEntry->Resolutions[i].dmPelsWidth = Current->dmPelsWidth;
            newEntry->Resolutions[i].dmPelsHeight = Current->dmPelsHeight;
            i++;
        }
    }
    descriptionSize = (_tcslen(DisplayDevice->DeviceString) + 1) * sizeof(TCHAR);
    description = HeapAlloc(GetProcessHeap(), 0, descriptionSize);
    if (!description) goto ByeBye;

    nameSize = (_tcslen(DisplayDevice->DeviceName) + 1) * sizeof(TCHAR);
    name = HeapAlloc(GetProcessHeap(), 0, nameSize);
    if (!name) goto ByeBye;

    keySize = (_tcslen(DisplayDevice->DeviceKey) + 1) * sizeof(TCHAR);
    key = HeapAlloc(GetProcessHeap(), 0, keySize);
    if (!key) goto ByeBye;

    devidSize = (_tcslen(DisplayDevice->DeviceID) + 1) * sizeof(TCHAR);
    devid = HeapAlloc(GetProcessHeap(), 0, devidSize);
    if (!devid) goto ByeBye;

    memcpy(description, DisplayDevice->DeviceString, descriptionSize);
    memcpy(name, DisplayDevice->DeviceName, nameSize);
    memcpy(key, DisplayDevice->DeviceKey, keySize);
    memcpy(devid, DisplayDevice->DeviceID, devidSize);
    newEntry->DeviceDescription = description;
    newEntry->DeviceName = name;
    newEntry->DeviceKey = key;
    newEntry->DeviceID = devid;
    newEntry->DeviceStateFlags = DisplayDevice->StateFlags;
    newEntry->Flink = pData->DisplayDeviceList;
    pData->DisplayDeviceList = newEntry;
    return TRUE;

ByeBye:
    if (newEntry != NULL)
    {
        if (newEntry->Settings != NULL)
        {
            Current = newEntry->Settings;
            while (Current != NULL)
            {
                PSETTINGS_ENTRY Next = Current->Flink;
                HeapFree(GetProcessHeap(), 0, Current);
                Current = Next;
            }
        }
        if (newEntry->Resolutions != NULL)
            HeapFree(GetProcessHeap(), 0, newEntry->Resolutions);
        HeapFree(GetProcessHeap(), 0, newEntry);
    }
    if (description != NULL)
        HeapFree(GetProcessHeap(), 0, description);
    if (name != NULL)
        HeapFree(GetProcessHeap(), 0, name);
    if (key != NULL)
        HeapFree(GetProcessHeap(), 0, key);
    return FALSE;
}

static VOID
OnDisplayDeviceChanged(IN HWND hwndDlg, IN PSETTINGS_DATA pData, IN PDISPLAY_DEVICE_ENTRY pDeviceEntry)
{
    PSETTINGS_ENTRY Current;
    DWORD index;

    pData->CurrentDisplayDevice = pDeviceEntry; /* Update variable */

    /* Fill color depths combo box */
    SendDlgItemMessage(hwndDlg, IDC_SETTINGS_BPP, CB_RESETCONTENT, 0, 0);
    for (Current = pDeviceEntry->Settings; Current != NULL; Current = Current->Flink)
    {
        TCHAR Buffer[64];
        if (LoadString(hApplet, (2900 + Current->dmBitsPerPel), Buffer, sizeof(Buffer) / sizeof(TCHAR)))
        {
            index = (DWORD) SendDlgItemMessage(hwndDlg, IDC_SETTINGS_BPP, CB_FINDSTRINGEXACT, (WPARAM)-1, (LPARAM)Buffer);
            if (index == (DWORD)CB_ERR)
            {
                index = (DWORD) SendDlgItemMessage(hwndDlg, IDC_SETTINGS_BPP, CB_ADDSTRING, 0, (LPARAM)Buffer);
                SendDlgItemMessage(hwndDlg, IDC_SETTINGS_BPP, CB_SETITEMDATA, index, Current->dmBitsPerPel);
            }
        }
    }

    /* Fill resolutions slider */
    SendDlgItemMessage(hwndDlg, IDC_SETTINGS_RESOLUTION, TBM_CLEARTICS, TRUE, 0);
    SendDlgItemMessage(hwndDlg, IDC_SETTINGS_RESOLUTION, TBM_SETRANGE, TRUE, MAKELONG(0, pDeviceEntry->ResolutionsCount - 1));

    UpdateDisplay(hwndDlg, pData, TRUE);
}

static VOID
SettingsOnInitDialog(IN HWND hwndDlg)
{
    BITMAP bitmap;
    DWORD Result = 0;
    DWORD iDevNum = 0;
    DWORD i;
    DISPLAY_DEVICE displayDevice;
    PSETTINGS_DATA pData;

    pData = HeapAlloc(GetProcessHeap(), 0, sizeof(SETTINGS_DATA));
    if (pData == NULL)
        return;

    SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)pData);

    /* Get video cards list */
    pData->DisplayDeviceList = NULL;
    displayDevice.cb = sizeof(displayDevice);
    while (EnumDisplayDevices(NULL, iDevNum, &displayDevice, 0x1))
    {
        if ((displayDevice.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) != 0)
        {
            if (AddDisplayDevice(pData, &displayDevice))
                Result++;
        }
        iDevNum++;
    }

    if (Result == 0)
    {
        /* No adapter found */
        EnableWindow(GetDlgItem(hwndDlg, IDC_SETTINGS_BPP), FALSE);
        EnableWindow(GetDlgItem(hwndDlg, IDC_SETTINGS_RESOLUTION), FALSE);
        EnableWindow(GetDlgItem(hwndDlg, IDC_SETTINGS_RESOLUTION_TEXT), FALSE);
        EnableWindow(GetDlgItem(hwndDlg, IDC_SETTINGS_ADVANCED), FALSE);
        ShowWindow(GetDlgItem(hwndDlg, IDC_SETTINGS_SPECTRUM), SW_HIDE);

        /* Do not initialize the color spectrum bitmaps */
        memset(pData->hSpectrumBitmaps, 0, sizeof(pData->hSpectrumBitmaps));
        return;
    }
    else if (Result == 1)
    {
        MONSL_MONINFO monitors;

        /* Single video adapter */
        SendDlgItemMessage(hwndDlg, IDC_SETTINGS_DEVICE, WM_SETTEXT, 0, (LPARAM)pData->DisplayDeviceList->DeviceDescription);
        OnDisplayDeviceChanged(hwndDlg, pData, pData->DisplayDeviceList);

        monitors.Position.x = monitors.Position.y = 0;
        monitors.Size.cx = pData->CurrentDisplayDevice->CurrentSettings->dmPelsWidth;
        monitors.Size.cy = pData->CurrentDisplayDevice->CurrentSettings->dmPelsHeight;
        monitors.Flags = 0;
        SendDlgItemMessage(hwndDlg,
                   IDC_SETTINGS_MONSEL,
                   MSLM_SETMONITORSINFO,
                   1,
                   (LPARAM)&monitors);
    }
    else /* FIXME: Incomplete! */
    {
        PMONSL_MONINFO pMonitors;
        DWORD i;

        SendDlgItemMessage(hwndDlg, IDC_SETTINGS_DEVICE, WM_SETTEXT, 0, (LPARAM)pData->DisplayDeviceList->DeviceDescription);
        OnDisplayDeviceChanged(hwndDlg, pData, pData->DisplayDeviceList);

        pMonitors = (PMONSL_MONINFO)HeapAlloc(GetProcessHeap(), 0, sizeof(MONSL_MONINFO) * Result);
        if (pMonitors)
        {
            DWORD hack = 1280;
            for (i = 0; i < Result; i++)
            {
                pMonitors[i].Position.x = hack * i;
                pMonitors[i].Position.y = 0;
                pMonitors[i].Size.cx = pData->DisplayDeviceList->CurrentSettings->dmPelsWidth;
                pMonitors[i].Size.cy = pData->DisplayDeviceList->CurrentSettings->dmPelsHeight;
                pMonitors[i].Flags = 0;
            }

            SendDlgItemMessage(hwndDlg,
                       IDC_SETTINGS_MONSEL,
                       MSLM_SETMONITORSINFO,
                       Result,
                       (LPARAM)pMonitors);

            HeapFree(GetProcessHeap(), 0, pMonitors);
        }
    }

    /* Initialize the color spectrum bitmaps */
    for (i = 0; i < NUM_SPECTRUM_BITMAPS; i++)
    {
        pData->hSpectrumBitmaps[i] = LoadImageW(hApplet, MAKEINTRESOURCEW(IDB_SPECTRUM_4 + i), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);

        if (pData->hSpectrumBitmaps[i] != NULL)
        {
            if (GetObjectW(pData->hSpectrumBitmaps[i], sizeof(BITMAP), &bitmap) != 0)
            {
                pData->cxSource[i] = bitmap.bmWidth;
                pData->cySource[i] = bitmap.bmHeight;
            }
            else
            {
                pData->cxSource[i] = 0;
                pData->cySource[i] = 0;
            }
        }
    }
}

/* Get the ID for SETTINGS_DATA::hSpectrumBitmaps */
static VOID
ShowColorSpectrum(IN HDC hDC, IN LPRECT client, IN DWORD BitsPerPel, IN PSETTINGS_DATA pData)
{
    HDC hdcMem;
    INT iBitmap;

    hdcMem = CreateCompatibleDC(hDC);

    if (!hdcMem)
        return;

    switch(BitsPerPel)
    {
        case 4:  iBitmap = 0; break;
        case 8:  iBitmap = 1; break;
        default: iBitmap = 2;
    }

    if (SelectObject(hdcMem, pData->hSpectrumBitmaps[iBitmap]))
    {
        StretchBlt(hDC,
               client->left, client->top,
               client->right - client->left,
               client->bottom - client->top,
               hdcMem, 0, 0,
               pData->cxSource[iBitmap],
               pData->cySource[iBitmap], SRCCOPY);
    }

    DeleteDC(hdcMem);
}

static VOID
OnBPPChanged(IN HWND hwndDlg, IN PSETTINGS_DATA pData)
{
    /* If new BPP is not compatible with resolution:
     * 1) try to find the nearest smaller matching resolution
     * 2) otherwise, get the nearest bigger resolution
     */
    PSETTINGS_ENTRY Current;
    DWORD dmNewBitsPerPel;
    DWORD index;
    HDC hSpectrumDC;
    HWND hSpectrumControl;
    RECT client;

    index = (DWORD) SendDlgItemMessage(hwndDlg, IDC_SETTINGS_BPP, CB_GETCURSEL, 0, 0);
    dmNewBitsPerPel = (DWORD) SendDlgItemMessage(hwndDlg, IDC_SETTINGS_BPP, CB_GETITEMDATA, index, 0);

    /* Show a new spectrum bitmap */
    hSpectrumControl = GetDlgItem(hwndDlg, IDC_SETTINGS_SPECTRUM);
    hSpectrumDC = GetDC(hSpectrumControl);
    if (hSpectrumDC == NULL)
        return;

    GetClientRect(hSpectrumControl, &client);
    ShowColorSpectrum(hSpectrumDC, &client, dmNewBitsPerPel, pData);
    ReleaseDC(hSpectrumControl, hSpectrumDC);

    /* Find if new parameters are valid */
    Current = pData->CurrentDisplayDevice->CurrentSettings;
    if (dmNewBitsPerPel == Current->dmBitsPerPel)
    {
        /* No change */
        return;
    }

    PropSheet_Changed(GetParent(hwndDlg), hwndDlg);

    if (dmNewBitsPerPel < Current->dmBitsPerPel)
    {
        Current = Current->Blink;
        while (Current != NULL)
        {
            if (Current->dmBitsPerPel == dmNewBitsPerPel
             && Current->dmPelsHeight == pData->CurrentDisplayDevice->CurrentSettings->dmPelsHeight
             && Current->dmPelsWidth == pData->CurrentDisplayDevice->CurrentSettings->dmPelsWidth)
            {
                pData->CurrentDisplayDevice->CurrentSettings = Current;
                UpdateDisplay(hwndDlg, pData, TRUE);
                return;
            }
            Current = Current->Blink;
        }
    }
    else
    {
        Current = Current->Flink;
        while (Current != NULL)
        {
            if (Current->dmBitsPerPel == dmNewBitsPerPel
             && Current->dmPelsHeight == pData->CurrentDisplayDevice->CurrentSettings->dmPelsHeight
             && Current->dmPelsWidth == pData->CurrentDisplayDevice->CurrentSettings->dmPelsWidth)
            {
                pData->CurrentDisplayDevice->CurrentSettings = Current;
                UpdateDisplay(hwndDlg, pData, TRUE);
                return;
            }
            Current = Current->Flink;
        }
    }

    /* Search smaller resolution compatible with current color depth */
    Current = pData->CurrentDisplayDevice->CurrentSettings->Blink;
    while (Current != NULL)
    {
        if (Current->dmBitsPerPel == dmNewBitsPerPel)
        {
            pData->CurrentDisplayDevice->CurrentSettings = Current;
            UpdateDisplay(hwndDlg, pData, TRUE);
            return;
        }
        Current = Current->Blink;
    }

    /* Search bigger resolution compatible with current color depth */
    Current = pData->CurrentDisplayDevice->CurrentSettings->Flink;
    while (Current != NULL)
    {
        if (Current->dmBitsPerPel == dmNewBitsPerPel)
        {
            pData->CurrentDisplayDevice->CurrentSettings = Current;
            UpdateDisplay(hwndDlg, pData, TRUE);
            return;
        }
        Current = Current->Flink;
    }

    /* We shouldn't go there */
}

static VOID
OnResolutionChanged(IN HWND hwndDlg, IN PSETTINGS_DATA pData, IN DWORD NewPosition,
                    IN BOOL bUpdateThumb)
{
    /* If new resolution is not compatible with color depth:
     * 1) try to find the nearest bigger matching color depth
     * 2) otherwise, get the nearest smaller color depth
     */
    PSETTINGS_ENTRY Current;
    DWORD dmNewPelsHeight = pData->CurrentDisplayDevice->Resolutions[NewPosition].dmPelsHeight;
    DWORD dmNewPelsWidth = pData->CurrentDisplayDevice->Resolutions[NewPosition].dmPelsWidth;
    DWORD dmBitsPerPel;
    DWORD dmDisplayFrequency;

    /* Find if new parameters are valid */
    Current = pData->CurrentDisplayDevice->CurrentSettings;
    if (dmNewPelsHeight == Current->dmPelsHeight && dmNewPelsWidth == Current->dmPelsWidth)
    {
        /* No change */
        return;
    }

    PropSheet_Changed(GetParent(hwndDlg), hwndDlg);

    dmBitsPerPel = Current->dmBitsPerPel;
    dmDisplayFrequency = Current->dmDisplayFrequency;

    if (CompareSettings(Current, dmNewPelsWidth,
                        dmNewPelsHeight, dmBitsPerPel,
                        dmDisplayFrequency) < 0)
    {
        Current = Current->Blink;
        while (Current != NULL)
        {
            if (Current->dmPelsHeight == dmNewPelsHeight
             && Current->dmPelsWidth  == dmNewPelsWidth
             && Current->dmBitsPerPel == dmBitsPerPel)
            {
                pData->CurrentDisplayDevice->CurrentSettings = Current;
                UpdateDisplay(hwndDlg, pData, bUpdateThumb);
                return;
            }
            Current = Current->Blink;
        }
    }
    else
    {
        Current = Current->Flink;
        while (Current != NULL)
        {
            if (Current->dmPelsHeight == dmNewPelsHeight
             && Current->dmPelsWidth  == dmNewPelsWidth
             && Current->dmBitsPerPel == dmBitsPerPel)
            {
                pData->CurrentDisplayDevice->CurrentSettings = Current;
                UpdateDisplay(hwndDlg, pData, bUpdateThumb);
                return;
            }
            Current = Current->Flink;
        }
    }

    /* Search bigger color depth compatible with current resolution */
    Current = pData->CurrentDisplayDevice->CurrentSettings->Flink;
    while (Current != NULL)
    {
        if (dmNewPelsHeight == Current->dmPelsHeight && dmNewPelsWidth == Current->dmPelsWidth)
        {
            pData->CurrentDisplayDevice->CurrentSettings = Current;
            UpdateDisplay(hwndDlg, pData, bUpdateThumb);
            return;
        }
        Current = Current->Flink;
    }

    /* Search smaller color depth compatible with current resolution */
    Current = pData->CurrentDisplayDevice->CurrentSettings->Blink;
    while (Current != NULL)
    {
        if (dmNewPelsHeight == Current->dmPelsHeight && dmNewPelsWidth == Current->dmPelsWidth)
        {
            pData->CurrentDisplayDevice->CurrentSettings = Current;
            UpdateDisplay(hwndDlg, pData, bUpdateThumb);
            return;
        }
        Current = Current->Blink;
    }

    /* We shouldn't go there */
}

/* Property sheet page callback */
UINT CALLBACK
SettingsPageCallbackProc(HWND hwnd, UINT uMsg, LPPROPSHEETPAGE ppsp)
{
    UINT Ret = 0;

    switch (uMsg)
    {
        case PSPCB_CREATE:
            Ret = RegisterMonitorSelectionControl(hApplet);
            break;

        case PSPCB_RELEASE:
            UnregisterMonitorSelectionControl(hApplet);
            break;
    }

    return Ret;
}

static INT_PTR CALLBACK
ConfirmDlgProc(IN HWND hwndDlg, IN UINT uMsg, IN WPARAM wParam, IN LPARAM lParam)
{
    PTIMEOUTDATA pData;

    pData = (PTIMEOUTDATA)GetWindowLongPtr(hwndDlg, DWLP_USER);

    switch(uMsg)
    {
        case WM_INITDIALOG:
            /* Allocate the local dialog data */
            pData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(TIMEOUTDATA));
            if (pData == NULL)
                return FALSE;

            /* Link the dialog data to the dialog */
            SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)pData);

            /* Timeout in seconds */
            pData->nTimeout = 15;

            /* Load the raw timeout string */
            LoadString(hApplet, IDS_TIMEOUTTEXT, pData->szRawBuffer, ARRAYSIZE(pData->szRawBuffer));

            /* Cook the timeout string and show it */
            _stprintf(pData->szCookedBuffer, pData->szRawBuffer, pData->nTimeout);
            SetDlgItemText(hwndDlg, IDC_TIMEOUTTEXT, pData->szCookedBuffer);

            /* Start the timer (ticks every second)*/
            SetTimer(hwndDlg, 1, 1000, NULL);
            break;

        case WM_TIMER:
            /* Update the timepout value */
            pData->nTimeout--;

            /* Update the timeout text */
            _stprintf(pData->szCookedBuffer, pData->szRawBuffer, pData->nTimeout);
            SetDlgItemText(hwndDlg, IDC_TIMEOUTTEXT, pData->szCookedBuffer);

            /* Kill the timer and return a 'No', if we ran out of time */
            if (pData->nTimeout == 0)
            {
                KillTimer(hwndDlg, 1);
                EndDialog(hwndDlg, IDNO);
            }
            break;

        case WM_COMMAND:
            /* Kill the timer and return the clicked button id */
            KillTimer(hwndDlg, 1);
            EndDialog(hwndDlg, LOWORD(wParam));
            break;

        case WM_DESTROY:
            /* Free the local dialog data */
            HeapFree(GetProcessHeap(), 0, pData);
            break;
    }

    return FALSE;
}

static VOID
ApplyDisplaySettings(HWND hwndDlg, PSETTINGS_DATA pData)
{
    TCHAR Message[1024], Title[256];
    DEVMODE devmode;
    LONG rc;

    RtlZeroMemory(&devmode, sizeof(devmode));
    devmode.dmSize = (WORD)sizeof(devmode);
    devmode.dmPelsWidth = pData->CurrentDisplayDevice->CurrentSettings->dmPelsWidth;
    devmode.dmPelsHeight = pData->CurrentDisplayDevice->CurrentSettings->dmPelsHeight;
    devmode.dmBitsPerPel = pData->CurrentDisplayDevice->CurrentSettings->dmBitsPerPel;
    devmode.dmDisplayFrequency = pData->CurrentDisplayDevice->CurrentSettings->dmDisplayFrequency;
    devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;

    rc = ChangeDisplaySettingsEx(pData->CurrentDisplayDevice->DeviceName,
                                 &devmode,
                                 NULL,
                                 CDS_UPDATEREGISTRY,
                                 NULL);
    switch (rc)
    {
        case DISP_CHANGE_SUCCESSFUL:
            break;

        case DISP_CHANGE_RESTART:
            LoadString(hApplet, IDS_DISPLAY_SETTINGS, Title, sizeof(Title) / sizeof(TCHAR));
            LoadString(hApplet, IDS_APPLY_NEEDS_RESTART, Message, sizeof(Message) / sizeof (TCHAR));
            MessageBox(hwndDlg, Message, Title, MB_OK | MB_ICONINFORMATION);
            return;

        case DISP_CHANGE_FAILED:
        default:
            LoadString(hApplet, IDS_DISPLAY_SETTINGS, Title, sizeof(Title) / sizeof(TCHAR));
            LoadString(hApplet, IDS_APPLY_FAILED, Message, sizeof(Message) / sizeof (TCHAR));
            MessageBox(hwndDlg, Message, Title, MB_OK | MB_ICONSTOP);
            return;
    }

    if (DialogBox(hApplet, MAKEINTRESOURCE(IDD_CONFIRMSETTINGS), hwndDlg, ConfirmDlgProc) == IDYES)
    {
        pData->CurrentDisplayDevice->InitialSettings.dmPelsWidth = pData->CurrentDisplayDevice->CurrentSettings->dmPelsWidth;
        pData->CurrentDisplayDevice->InitialSettings.dmPelsHeight = pData->CurrentDisplayDevice->CurrentSettings->dmPelsHeight;
        pData->CurrentDisplayDevice->InitialSettings.dmBitsPerPel = pData->CurrentDisplayDevice->CurrentSettings->dmBitsPerPel;
    }
    else
    {
        devmode.dmPelsWidth = pData->CurrentDisplayDevice->InitialSettings.dmPelsWidth;
        devmode.dmPelsHeight = pData->CurrentDisplayDevice->InitialSettings.dmPelsHeight;
        devmode.dmBitsPerPel = pData->CurrentDisplayDevice->InitialSettings.dmBitsPerPel;
        devmode.dmDisplayFrequency = pData->CurrentDisplayDevice->InitialSettings.dmDisplayFrequency;

        rc = ChangeDisplaySettingsEx(pData->CurrentDisplayDevice->DeviceName,
                                     &devmode,
                                     NULL,
                                     CDS_UPDATEREGISTRY,
                                     NULL);
        switch (rc)
        {
            case DISP_CHANGE_SUCCESSFUL:
                pData->CurrentDisplayDevice->CurrentSettings->dmPelsWidth = pData->CurrentDisplayDevice->InitialSettings.dmPelsWidth;
                pData->CurrentDisplayDevice->CurrentSettings->dmPelsHeight = pData->CurrentDisplayDevice->InitialSettings.dmPelsHeight;
                pData->CurrentDisplayDevice->CurrentSettings->dmBitsPerPel = pData->CurrentDisplayDevice->InitialSettings.dmBitsPerPel;
                UpdateDisplay(hwndDlg, pData, TRUE);
                break;

            case DISP_CHANGE_RESTART:
                LoadString(hApplet, IDS_DISPLAY_SETTINGS, Title, sizeof(Title) / sizeof(TCHAR));
                LoadString(hApplet, IDS_APPLY_NEEDS_RESTART, Message, sizeof(Message) / sizeof (TCHAR));
                MessageBox(hwndDlg, Message, Title, MB_OK | MB_ICONINFORMATION);
                return;

            case DISP_CHANGE_FAILED:
            default:
                LoadString(hApplet, IDS_DISPLAY_SETTINGS, Title, sizeof(Title) / sizeof(TCHAR));
                LoadString(hApplet, IDS_APPLY_FAILED, Message, sizeof(Message) / sizeof (TCHAR));
                MessageBox(hwndDlg, Message, Title, MB_OK | MB_ICONSTOP);
                return;
        }
    }
}

/* Property page dialog callback */
INT_PTR CALLBACK
SettingsPageProc(IN HWND hwndDlg, IN UINT uMsg, IN WPARAM wParam, IN LPARAM lParam)
{
    PSETTINGS_DATA pData;

    pData = (PSETTINGS_DATA)GetWindowLongPtr(hwndDlg, DWLP_USER);

    switch(uMsg)
    {
        case WM_INITDIALOG:
        {
            SettingsOnInitDialog(hwndDlg);
            break;
        }
        case WM_DRAWITEM:
        {
            LPDRAWITEMSTRUCT lpDrawItem;
            lpDrawItem = (LPDRAWITEMSTRUCT) lParam;

            if (lpDrawItem->CtlID == IDC_SETTINGS_SPECTRUM)
                ShowColorSpectrum(lpDrawItem->hDC, &lpDrawItem->rcItem, pData->CurrentDisplayDevice->CurrentSettings->dmBitsPerPel, pData);
            break;
        }
        case WM_COMMAND:
        {
            DWORD controlId = LOWORD(wParam);
            DWORD command   = HIWORD(wParam);

            if (controlId == IDC_SETTINGS_ADVANCED && command == BN_CLICKED)
                DisplayAdvancedSettings(hwndDlg, pData->CurrentDisplayDevice);
            else if (controlId == IDC_SETTINGS_BPP && command == CBN_SELCHANGE)
                OnBPPChanged(hwndDlg, pData);
            break;
        }
        case WM_HSCROLL:
        {
            switch (LOWORD(wParam))
            {
                case TB_LINEUP:
                case TB_LINEDOWN:
                case TB_PAGEUP:
                case TB_PAGEDOWN:
                case TB_TOP:
                case TB_BOTTOM:
                case TB_ENDTRACK:
                {
                    DWORD newPosition = (DWORD) SendDlgItemMessage(hwndDlg, IDC_SETTINGS_RESOLUTION, TBM_GETPOS, 0, 0);
                    OnResolutionChanged(hwndDlg, pData, newPosition, TRUE);
                    break;
                }

                case TB_THUMBTRACK:
                    OnResolutionChanged(hwndDlg, pData, HIWORD(wParam), FALSE);
                    break;
            }
            break;
        }
        case WM_NOTIFY:
        {
            LPNMHDR lpnm = (LPNMHDR)lParam;
            if (lpnm->code == (UINT)PSN_APPLY)
            {
                if (pData->CurrentDisplayDevice->CurrentSettings->dmPelsWidth != pData->CurrentDisplayDevice->InitialSettings.dmPelsWidth
                 || pData->CurrentDisplayDevice->CurrentSettings->dmPelsHeight != pData->CurrentDisplayDevice->InitialSettings.dmPelsHeight
                 || pData->CurrentDisplayDevice->CurrentSettings->dmBitsPerPel != pData->CurrentDisplayDevice->InitialSettings.dmBitsPerPel)
                {
                    /* Apply new settings */
                    ApplyDisplaySettings(hwndDlg, pData);
                }
            }
            break;
        }

        case WM_CONTEXTMENU:
        {
            HWND hwndMonSel;
            HMENU hPopup;
            UINT uiCmd;
            POINT pt, ptClient;
            INT Index;

            pt.x = (SHORT)LOWORD(lParam);
            pt.y = (SHORT)HIWORD(lParam);

            hwndMonSel = GetDlgItem(hwndDlg,
                                    IDC_SETTINGS_MONSEL);
            if ((HWND)wParam == hwndMonSel)
            {
                if (pt.x == -1 && pt.y == -1)
                {
                    RECT rcMon;

                    Index = (INT)SendMessage(hwndMonSel,
                                             MSLM_GETCURSEL,
                                             0,
                                             0);

                    if (Index >= 0 &&
                        (INT)SendMessage(hwndMonSel,
                                         MSLM_GETMONITORRECT,
                                         Index,
                                         (LPARAM)&rcMon) > 0)
                    {
                        pt.x = rcMon.left + ((rcMon.right - rcMon.left) / 2);
                        pt.y = rcMon.top + ((rcMon.bottom - rcMon.top) / 2);
                    }
                    else
                        pt.x = pt.y = 0;

                    MapWindowPoints(hwndMonSel,
                                    NULL,
                                    &pt,
                                    1);
                }
                else
                {
                    ptClient = pt;
                    MapWindowPoints(NULL,
                                    hwndMonSel,
                                    &ptClient,
                                    1);

                    Index = (INT)SendMessage(hwndMonSel,
                                             MSLM_HITTEST,
                                             (WPARAM)&ptClient,
                                             0);
                }

                if (Index >= 0)
                {
                    hPopup = LoadPopupMenu(hApplet,
                                           MAKEINTRESOURCE(IDM_MONITOR_MENU));
                    if (hPopup != NULL)
                    {
                        /* FIXME: Enable/Disable menu items */
                        EnableMenuItem(hPopup,
                                       ID_MENU_ATTACHED,
                                       MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
                        EnableMenuItem(hPopup,
                                       ID_MENU_PRIMARY,
                                       MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
                        EnableMenuItem(hPopup,
                                       ID_MENU_IDENTIFY,
                                       MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
                        EnableMenuItem(hPopup,
                                       ID_MENU_PROPERTIES,
                                       MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);

                        uiCmd = (UINT)TrackPopupMenu(hPopup,
                                                     TPM_RETURNCMD | TPM_RIGHTBUTTON,
                                                     pt.x,
                                                     pt.y,
                                                     0,
                                                     hwndDlg,
                                                     NULL);

                        switch (uiCmd)
                        {
                            case ID_MENU_ATTACHED:
                            case ID_MENU_PRIMARY:
                            case ID_MENU_IDENTIFY:
                            case ID_MENU_PROPERTIES:
                                /* FIXME: Implement */
                                break;
                        }

                        DestroyMenu(hPopup);
                    }
                }
            }
            break;
        }

        case WM_DESTROY:
        {
            DWORD i;
            PDISPLAY_DEVICE_ENTRY Current = pData->DisplayDeviceList;

            while (Current != NULL)
            {
                PDISPLAY_DEVICE_ENTRY Next = Current->Flink;
                PSETTINGS_ENTRY CurrentSettings = Current->Settings;
                while (CurrentSettings != NULL)
                {
                    PSETTINGS_ENTRY NextSettings = CurrentSettings->Flink;
                    HeapFree(GetProcessHeap(), 0, CurrentSettings);
                    CurrentSettings = NextSettings;
                }
                HeapFree(GetProcessHeap(), 0, Current);
                Current = Next;
            }

            for (i = 0; i < NUM_SPECTRUM_BITMAPS; i++)
            {
                if (pData->hSpectrumBitmaps[i])
                    DeleteObject(pData->hSpectrumBitmaps[i]);
            }

            HeapFree(GetProcessHeap(), 0, pData);
        }
    }
    return FALSE;
}