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
/*
 * PROJECT:         ReactOS Console Configuration DLL
 * LICENSE:         GPL - See COPYING in the top level directory
 * FILE:            dll/cpl/console/font.c
 * PURPOSE:         Font dialog
 * PROGRAMMERS:     Johannes Anderwald (johannes.anderwald@reactos.org)
 *                  Hermes Belusca-Maito (hermes.belusca@sfr.fr)
 *                  Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com)
 */

#include "console.h"

#define NDEBUG
#include <debug.h>


/*
 * Current active font, corresponding to the active console font,
 * and used for painting the text samples.
 */
FONT_PREVIEW FontPreview = {NULL, 0, 0};


/*
 * Standard font pixel/point heights for TrueType fonts
 */
static const SHORT TrueTypePoints[] =
{
    5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72
};

typedef struct _FONTSIZE_LIST_CTL
{
    LIST_CTL RasterSizeList;    // ListBox for Raster font sizes; needs to handle bisection.
    HWND hWndTTSizeList;        // ComboBox for TrueType font sizes.
    BOOL bIsTTSizeDirty;        // TRUE or FALSE depending on whether we have edited the edit zone.
    BOOL UseRasterOrTTList;     // TRUE: Use the Raster size list; FALSE: Use the TrueType size list.
    BOOL TTSizePixelUnit;       // TRUE: Size in pixels (default); FALSE: Size in points.
    LONG CurrentRasterSize;
    LONG CurrentTTSize;         // In whatever unit (pixels or points) currently selected.
} FONTSIZE_LIST_CTL, *PFONTSIZE_LIST_CTL;

/* Used by FontTypeChange() only */
static INT   CurrentSelFont  = LB_ERR;
static DWORD CurrentFontType = (DWORD)-1;   // Invalid font type


VOID
RefreshFontPreview(
    IN FONT_PREVIEW* Preview,
    IN PCONSOLE_STATE_INFO pConInfo)
{
    if (Preview->hFont) DeleteObject(Preview->hFont);
    Preview->hFont = CreateConsoleFont(pConInfo);
    if (Preview->hFont == NULL)
        DPRINT1("RefreshFontPreview: CreateConsoleFont() failed\n");
    GetFontCellSize(NULL, Preview->hFont, &Preview->CharHeight, &Preview->CharWidth);
}

VOID
UpdateFontPreview(
    IN FONT_PREVIEW* Preview,
    IN HFONT hFont,
    IN UINT  CharWidth,
    IN UINT  CharHeight)
{
    if (Preview->hFont) DeleteObject(Preview->hFont);
    Preview->hFont = hFont;
    Preview->CharWidth  = CharWidth;
    Preview->CharHeight = CharHeight;
}

// PLIST_GETCOUNT
static INT
RasterSizeList_GetCount(
    IN PLIST_CTL ListCtl)
{
    return (INT)SendMessageW(ListCtl->hWndList, LB_GETCOUNT, 0, 0);
}

// PLIST_GETDATA
static ULONG_PTR
RasterSizeList_GetData(
    IN PLIST_CTL ListCtl,
    IN INT Index)
{
    return (ULONG_PTR)SendMessageW(ListCtl->hWndList, LB_GETITEMDATA, (WPARAM)Index, 0);
}


INT
LogicalSizeToPointSize(
    IN HDC hDC OPTIONAL,
    IN UINT LogicalSize)
{
    INT PointSize;
    HDC hOrgDC = hDC;

    if (!hDC)
        hDC = GetDC(NULL);

    // LogicalSize = tm.tmHeight - tm.tmInternalLeading;
    PointSize = MulDiv(LogicalSize, 72, GetDeviceCaps(hDC, LOGPIXELSY));

    if (!hOrgDC)
        ReleaseDC(NULL, hDC);

    return PointSize;
}

INT
PointSizeToLogicalSize(
    IN HDC hDC OPTIONAL,
    IN INT PointSize)
{
    INT LogicalSize;
    HDC hOrgDC = hDC;

    if (!hDC)
        hDC = GetDC(NULL);

    LogicalSize = MulDiv(PointSize, GetDeviceCaps(hDC, LOGPIXELSY), 72);

    if (!hOrgDC)
        ReleaseDC(NULL, hDC);

    return LogicalSize;
}


static VOID
FontSizeList_SelectFontSize(
    IN PFONTSIZE_LIST_CTL SizeList,
    IN ULONG FontSize)
{
    INT nSel;
    WCHAR szFontSize[100];

    //
    // FIXME: Check whether FontSize == 0
    // (or in the case of raster font maybe, whether HIWORD(FontSize) == Height == 0) ??
    //

    /* Find and select the best font size in the list corresponding to the current size */
    if (SizeList->UseRasterOrTTList)
    {
        INT idx;

        /* Raster font size (in pixels) */
        SizeList->CurrentRasterSize = FontSize;

        nSel = BisectListSortedByValue(&SizeList->RasterSizeList, FontSize, NULL, FALSE);
        idx  = (INT)SendMessageW(SizeList->RasterSizeList.hWndList, LB_GETCOUNT, 0, 0);
        if (nSel == LB_ERR)
        {
            /* Not found, select the first element of the list */
            nSel = 0;
        }
        else if (nSel >= idx)
        {
            /*
             * We got an index beyond the end of the list (as per Bisect* functionality),
             * so instead, select the last element of the list.
             */
            nSel = idx-1;
        }
        SendMessageW(SizeList->RasterSizeList.hWndList, LB_SETCURSEL, (WPARAM)nSel, 0);
    }
    else
    {
        /* TrueType font size (in pixels or points) */
        SizeList->CurrentTTSize = FontSize;

        // _ultow(szFontSize, FontSize, 10);
        StringCchPrintfW(szFontSize, ARRAYSIZE(szFontSize), L"%d", FontSize);

        /* Find the font size in the list, or add it both in the ComboBox list, sorted by size value (string), and its edit box */
        nSel = SendMessageW(SizeList->hWndTTSizeList, CB_FINDSTRINGEXACT, 0, (LPARAM)szFontSize);
        if (nSel == CB_ERR)
        {
            nSel = (UINT)SendMessageW(SizeList->hWndTTSizeList, CB_ADDSTRING, -1, (LPARAM)szFontSize);
            // ComboBox_SetText(...)
            SetWindowTextW(SizeList->hWndTTSizeList, szFontSize);
            SizeList->bIsTTSizeDirty = TRUE;
        }
        SendMessageW(SizeList->hWndTTSizeList, CB_SETCURSEL, (WPARAM)nSel, 0);
    }
}

static LONG
FontSizeList_GetSelectedFontSize(
    IN PFONTSIZE_LIST_CTL SizeList)
{
    INT nSel;
    LONG FontSize;
    PWCHAR pszNext = NULL;
    WCHAR szFontSize[100];

    if (SizeList->UseRasterOrTTList)
    {
        /* Raster font size (in pixels) */

        nSel = (INT)SendMessageW(SizeList->RasterSizeList.hWndList, LB_GETCURSEL, 0, 0);
        if (nSel == LB_ERR) return 0;

        FontSize = (LONG)SizeList->RasterSizeList.GetData(&SizeList->RasterSizeList, nSel);
        if (FontSize == LB_ERR) return 0;

        SizeList->CurrentRasterSize = FontSize;
    }
    else
    {
        /* TrueType font size (in pixels or points) */

        if (!SizeList->bIsTTSizeDirty)
        {
            /*
             * The user just selected an existing size, read the ComboBox selection.
             *
             * See: https://support.microsoft.com/en-us/help/66365/how-to-process-a-cbn-selchange-notification-message
             * for more details.
             */
            nSel = SendMessageW(SizeList->hWndTTSizeList, CB_GETCURSEL, 0, 0);
            SendMessageW(SizeList->hWndTTSizeList, CB_GETLBTEXT, nSel, (LPARAM)szFontSize);

            /* Validate the font size */
            FontSize = wcstoul(szFontSize, &pszNext, 10);
            if ((FontSize == 0) || (*pszNext))
                return 0;
        }
        else
        {
            /* Read the ComboBox edit string, as the user has entered a custom size */
            // ComboBox_GetText(...)
            GetWindowTextW(SizeList->hWndTTSizeList, szFontSize, ARRAYSIZE(szFontSize));

            /* Validate the font size */
            FontSize = wcstoul(szFontSize, &pszNext, 10);
            if ((FontSize == 0) || (*pszNext))
                return 0;

            /* Find if the font size already exists in the list; if not, add it */
            nSel = SendMessageW(SizeList->hWndTTSizeList, CB_FINDSTRINGEXACT, 0, (LPARAM)szFontSize);
            if (nSel == CB_ERR)
            {
                nSel = (UINT)SendMessageW(SizeList->hWndTTSizeList, CB_ADDSTRING, -1, (LPARAM)szFontSize);
                //// ComboBox_SetText(...)
                //SetWindowTextW(SizeList->hWndTTSizeList, szFontSize);
                //SizeList->bIsTTSizeDirty = TRUE;
            }
            SendMessageW(SizeList->hWndTTSizeList, CB_SETCURSEL, (WPARAM)nSel, 0);
        }

        SizeList->bIsTTSizeDirty = FALSE;

        SizeList->CurrentTTSize = FontSize;

        /*
         * If the font size is given in points, instead of pixels,
         * convert it into logical size.
         */
        if (!SizeList->TTSizePixelUnit)
            FontSize = -PointSizeToLogicalSize(NULL, FontSize);
    }

    return FontSize;
}


static VOID
AddFontToList(
    IN HWND hWndList,
    IN LPCWSTR pszFaceName,
    IN DWORD FontType)
{
    INT iItem;

    /* Make sure the font doesn't already exist in the list */
    if (SendMessageW(hWndList, LB_FINDSTRINGEXACT, 0, (LPARAM)pszFaceName) != LB_ERR)
        return;

    /* Add the font */
    iItem = (INT)SendMessageW(hWndList, LB_ADDSTRING, 0, (LPARAM)pszFaceName);
    if (iItem == LB_ERR)
    {
        DPRINT1("Failed to add font '%S'\n", pszFaceName);
        return;
    }

    DPRINT1("Add font '%S'\n", pszFaceName);

    /* Store this information in the list-item's userdata area */
    // SendMessageW(hWndList, LB_SETITEMDATA, idx, MAKELPARAM(fFixed, fTrueType));
    SendMessageW(hWndList, LB_SETITEMDATA, iItem, (LPARAM)FontType);
}

typedef struct _FACE_NAMES_PROC_PARAM
{
    HWND hWndList;
    UINT CodePage;
} FACE_NAMES_PROC_PARAM, *PFACE_NAMES_PROC_PARAM;

static BOOL CALLBACK
EnumFaceNamesProc(
    IN PLOGFONTW lplf,
    IN PNEWTEXTMETRICW lpntm,
    IN DWORD  FontType,
    IN LPARAM lParam)
{
    PFACE_NAMES_PROC_PARAM Param = (PFACE_NAMES_PROC_PARAM)lParam;

    if (IsValidConsoleFont2(lplf, lpntm, FontType, Param->CodePage))
    {
        /* Add the font to the list */
        AddFontToList(Param->hWndList, lplf->lfFaceName, FontType);
    }

    /* Continue the font enumeration */
    return TRUE;
}

static BOOL CALLBACK
EnumFontSizesProc(
    IN PLOGFONTW lplf,
    IN PNEWTEXTMETRICW lpntm,
    IN DWORD  FontType,
    IN LPARAM lParam)
{
    PFONTSIZE_LIST_CTL SizeList = (PFONTSIZE_LIST_CTL)lParam;
    UINT iItem, iDupItem;
    WCHAR szFontSize[100];

    if (FontType != TRUETYPE_FONTTYPE)
    {
        WPARAM FontSize;

        /*
         * Format:
         * Width  = FontSize.X = LOWORD(FontSize);
         * Height = FontSize.Y = HIWORD(FontSize);
         */

        StringCchPrintfW(szFontSize, ARRAYSIZE(szFontSize), L"%d x %d", lplf->lfWidth, lplf->lfHeight);
        FontSize = MAKEWPARAM(lplf->lfWidth, lplf->lfHeight);

        /* Add the font size into the list, sorted by size value. Avoid any duplicates. */
        /* Store this information in the list-item's userdata area */
        iDupItem = LB_ERR;
        iItem = BisectListSortedByValue(&SizeList->RasterSizeList, FontSize, &iDupItem, TRUE);
        if (iItem == LB_ERR)
            iItem = 0;
        if (iDupItem == LB_ERR)
        {
            iItem = (UINT)SendMessageW(SizeList->RasterSizeList.hWndList, LB_INSERTSTRING, iItem, (LPARAM)szFontSize);
            if (iItem != LB_ERR && iItem != LB_ERRSPACE)
                iItem = SendMessageW(SizeList->RasterSizeList.hWndList, LB_SETITEMDATA, iItem, FontSize);
        }

        return TRUE;
    }
    else
    {
        /* TrueType or vectored font: list all the hardcoded font points */
        ULONG i;
        for (i = 0; i < ARRAYSIZE(TrueTypePoints); ++i)
        {
            // _ultow(szFontSize, TrueTypePoints[i], 10);
            StringCchPrintfW(szFontSize, ARRAYSIZE(szFontSize), L"%d", TrueTypePoints[i]);

            /* Add the font size into the list, sorted by size value (string). Avoid any duplicates. */
            if (SendMessageW(SizeList->hWndTTSizeList, CB_FINDSTRINGEXACT, 0, (LPARAM)szFontSize) == CB_ERR)
                iItem = (UINT)SendMessageW(SizeList->hWndTTSizeList, CB_INSERTSTRING, -1, (LPARAM)szFontSize);
        }

        /* Stop the enumeration now */
        return FALSE;
    }
}

static VOID
FaceNameList_Initialize(
    IN HWND hWndList,
    IN UINT CodePage)
{
    FACE_NAMES_PROC_PARAM Param;
    HDC hDC;
    LOGFONTW lf;
    INT idx;

    Param.hWndList = hWndList;
    Param.CodePage = CodePage;

    ZeroMemory(&lf, sizeof(lf));
    lf.lfCharSet = DEFAULT_CHARSET; // CodePageToCharSet(CodePage);
    // lf.lfPitchAndFamily = FIXED_PITCH | FF_MODERN;

    hDC = GetDC(NULL);
    EnumFontFamiliesExW(hDC, &lf, (FONTENUMPROCW)EnumFaceNamesProc, (LPARAM)&Param, 0);
    ReleaseDC(NULL, hDC);

    idx = (INT)SendMessageW(hWndList, LB_GETCOUNT, 0, 0);
    if (idx != LB_ERR && idx != 0)
    {
        /* We have found some fonts and filled the list, we are fine! */
        return;
    }

    /* No fonts were found. Manually add default ones into the list. */
    DPRINT1("The ideal console fonts were not found; manually add default ones.\n");

    AddFontToList(hWndList, L"Terminal", RASTER_FONTTYPE);
    AddFontToList(hWndList, L"Lucida Console", TRUETYPE_FONTTYPE);
    if (CodePageToCharSet(CodePage) != DEFAULT_CHARSET)
        AddFontToList(hWndList, L"Droid Sans Fallback", TRUETYPE_FONTTYPE);
}

static VOID
FaceNameList_SelectFont(
    IN HWND hDlg,
    IN HWND hWndList,
    IN PFONTSIZE_LIST_CTL SizeList,
    IN LPCWSTR FaceName,
    IN ULONG FontFamily,
    IN ULONG FontWeight,
    IN COORD FontSize)
{
    INT iItem;

    iItem = (INT)SendMessageW(hWndList, LB_FINDSTRINGEXACT, 0, (LPARAM)FaceName);
    if (iItem == LB_ERR)
        iItem = (INT)SendMessageW(hWndList, LB_FINDSTRINGEXACT, 0, (LPARAM)L"Terminal");
    if (iItem == LB_ERR)
        iItem = 0;
    SendMessageW(hWndList, LB_SETCURSEL, (WPARAM)iItem, 0);

    if (FontWeight >= FW_BOLD)
        CheckDlgButton(hDlg, IDC_CHECK_BOLD_FONTS, BST_CHECKED);
    else
        CheckDlgButton(hDlg, IDC_CHECK_BOLD_FONTS, BST_UNCHECKED);

    /* Select the current font size */
    /*
     * Format:
     * Width  = FontSize.X = LOWORD(FontSize);
     * Height = FontSize.Y = HIWORD(FontSize);
     */
    SizeList->CurrentRasterSize = MAKELONG(FontSize.X, FontSize.Y);
    SizeList->CurrentTTSize = FontSize.Y;
    // FontSizeList_SelectFontSize(SizeList, SizeList->CurrentRasterSize);

    // return iItem;
}

static VOID
UpdateFontSizeList(
    IN HWND hDlg,
    IN PFONTSIZE_LIST_CTL SizeList)
{
    HWND hDlgItem;

    if (SizeList->UseRasterOrTTList)
    {
        /*
         * Raster font: show the Raster size list, and
         * hide the TrueType size list and the units.
         */

        // EnableDlgItem(hDlg, IDC_CHECK_BOLD_FONTS, FALSE);

        hDlgItem = GetDlgItem(hDlg, IDC_RADIO_PIXEL_UNIT);
        ShowWindow(hDlgItem, SW_HIDE);
        EnableWindow(hDlgItem, FALSE);

        hDlgItem = GetDlgItem(hDlg, IDC_RADIO_POINT_UNIT);
        ShowWindow(hDlgItem, SW_HIDE);
        EnableWindow(hDlgItem, FALSE);

        hDlgItem = SizeList->hWndTTSizeList;
        ShowWindow(hDlgItem, SW_HIDE);
        EnableWindow(hDlgItem, FALSE);

        hDlgItem = SizeList->RasterSizeList.hWndList;
        EnableWindow(hDlgItem, TRUE);
        ShowWindow(hDlgItem, SW_SHOW);
    }
    else
    {
        /*
         * TrueType font: show the TrueType size list
         * and the units, and hide the Raster size list.
         */

        // EnableDlgItem(hDlg, IDC_CHECK_BOLD_FONTS, TRUE);

        hDlgItem = SizeList->RasterSizeList.hWndList;
        ShowWindow(hDlgItem, SW_HIDE);
        EnableWindow(hDlgItem, FALSE);

        hDlgItem = SizeList->hWndTTSizeList;
        EnableWindow(hDlgItem, TRUE);
        ShowWindow(hDlgItem, SW_SHOW);

        hDlgItem = GetDlgItem(hDlg, IDC_RADIO_PIXEL_UNIT);
        EnableWindow(hDlgItem, TRUE);
        ShowWindow(hDlgItem, SW_SHOW);

        hDlgItem = GetDlgItem(hDlg, IDC_RADIO_POINT_UNIT);
        EnableWindow(hDlgItem, TRUE);
        ShowWindow(hDlgItem, SW_SHOW);
    }
}

static BOOL
FontSizeChange(
    IN HWND hDlg,
    IN PFONTSIZE_LIST_CTL SizeList,
    IN OUT PCONSOLE_STATE_INFO pConInfo);

static BOOL
FontTypeChange(
    IN HWND hDlg,
    IN PFONTSIZE_LIST_CTL SizeList,
    IN OUT PCONSOLE_STATE_INFO pConInfo)
{
    HWND hListBox = GetDlgItem(hDlg, IDC_LBOX_FONTTYPE);
    INT Length, nSel;
    LPWSTR FaceName;
    DWORD FontType;
    LPCWSTR FontGrpBoxLabelTpl = NULL;<--- Assignment 'FontGrpBoxLabelTpl=NULL', assigned value is 0
    WCHAR FontGrpBoxLabel[260];

    nSel = (INT)SendMessageW(hListBox, LB_GETCURSEL, 0, 0);
    if (nSel == LB_ERR) return FALSE;

    /*
     * This is disabled, because there can be external parameters
     * that may have changed (e.g. ConInfo->FontWeight, code page, ...)
     * and that we don't control here, and that need a font refresh.
     */
#if 0
    /* Check whether the selection has changed */
    if (nSel == CurrentSelFont)
        return FALSE;
#endif

    Length = (INT)SendMessageW(hListBox, LB_GETTEXTLEN, nSel, 0);
    if (Length == LB_ERR) return FALSE;

    FaceName = HeapAlloc(GetProcessHeap(),
                         HEAP_ZERO_MEMORY,
                         (Length + 1) * sizeof(WCHAR));
    if (FaceName == NULL) return FALSE;

    Length = (INT)SendMessageW(hListBox, LB_GETTEXT, nSel, (LPARAM)FaceName);
    FaceName[Length] = L'\0';

    StringCchCopyW(pConInfo->FaceName, ARRAYSIZE(pConInfo->FaceName), FaceName);
    DPRINT("pConInfo->FaceName = '%S'\n", pConInfo->FaceName);

    /*
     * Retrieve the read-only font group box label string template,
     * and set the group box label to the name of the selected font.
     */
    Length = LoadStringW(hApplet, IDS_GROUPBOX_FONT_NAME, (LPWSTR)&FontGrpBoxLabelTpl, 0);
    if (FontGrpBoxLabelTpl && Length > 0)<--- Condition 'FontGrpBoxLabelTpl' is always false
    {
        StringCchCopyNW(FontGrpBoxLabel, ARRAYSIZE(FontGrpBoxLabel), FontGrpBoxLabelTpl, Length);
        StringCchCatW(FontGrpBoxLabel, ARRAYSIZE(FontGrpBoxLabel), FaceName);
        SetDlgItemTextW(hDlg, IDC_GROUPBOX_FONT_NAME, FontGrpBoxLabel);
    }

    HeapFree(GetProcessHeap(), 0, FaceName);

    /*
     * Reset the font size list, only:
     * - if we have changed the type of font, or
     * - if the font type is the same and is RASTER but the font has changed.
     * Otherwise, if the font type is not RASTER and has not changed,
     * we always display the TrueType default sizes and we don't need to
     * recreate the list when we change between different TrueType fonts.
     */
    FontType = SendMessageW(hListBox, LB_GETITEMDATA, nSel, 0);
    if (FontType != LB_ERR)
    {
        SizeList->UseRasterOrTTList = (FontType == RASTER_FONTTYPE);

        /* Display the correct font size list (if needed) */
        if (CurrentFontType != FontType)
            UpdateFontSizeList(hDlg, SizeList);

        /* Enumerate the available sizes for the selected font */
        if ((CurrentFontType != FontType) ||
            (FontType == RASTER_FONTTYPE && CurrentSelFont != nSel))
        {
            LOGFONTW lf;
            HDC hDC;

            if (SizeList->UseRasterOrTTList)
                SendMessageW(SizeList->RasterSizeList.hWndList, LB_RESETCONTENT, 0, 0);
            else
                SendMessageW(SizeList->hWndTTSizeList, CB_RESETCONTENT, 0, 0);

            ZeroMemory(&lf, sizeof(lf));
            lf.lfCharSet = DEFAULT_CHARSET; // CodePageToCharSet(pConInfo->CodePage);
            // lf.lfPitchAndFamily = FIXED_PITCH | FF_MODERN;
            StringCchCopyW(lf.lfFaceName, ARRAYSIZE(lf.lfFaceName), pConInfo->FaceName);

            hDC = GetDC(NULL);
            EnumFontFamiliesExW(hDC, &lf, (FONTENUMPROCW)EnumFontSizesProc, (LPARAM)SizeList, 0);
            ReleaseDC(NULL, hDC);

            /* Re-select the current font size */
            if (SizeList->UseRasterOrTTList)
                FontSizeList_SelectFontSize(SizeList, SizeList->CurrentRasterSize);
            else
                FontSizeList_SelectFontSize(SizeList, SizeList->CurrentTTSize);
        }
    }
    else
    {
        /* We failed, display the raster fonts size list */
        SizeList->UseRasterOrTTList = TRUE;
        UpdateFontSizeList(hDlg, SizeList);
    }
    CurrentFontType = FontType;
    CurrentSelFont  = nSel;

    FontSizeChange(hDlg, SizeList, pConInfo);
    return TRUE;
}

static BOOL
FontSizeChange(
    IN HWND hDlg,
    IN PFONTSIZE_LIST_CTL SizeList,
    IN OUT PCONSOLE_STATE_INFO pConInfo)
{
    LONG FontSize;
    UINT CharWidth, CharHeight;
    HFONT hFont;
    WCHAR szFontSize[100];

    /*
     * Retrieve the current selected font size.
     * - If SizeList->UseRasterOrTTList is TRUE, or if it is FALSE but
     *   if SizeList->TTSizePixelUnit is TRUE, then the font size is in pixels;
     * - If SizeList->TTSizePixelUnit is FALSE, then the font size is in points.
     */
    FontSize = FontSizeList_GetSelectedFontSize(SizeList);
    if (FontSize == 0)
        return FALSE; // We have got an invalid font size...

    /*
     * For TrueType fonts we set the requested width to zero
     * so as to obtain a default aspect-ratio width.
     */
    CharHeight = (UINT)(SizeList->UseRasterOrTTList ? HIWORD(FontSize) : FontSize);
    CharWidth  = (UINT)(SizeList->UseRasterOrTTList ? LOWORD(FontSize) : 0);

    hFont = CreateConsoleFont2((LONG)CharHeight, (LONG)CharWidth, pConInfo);
    if (hFont == NULL)
        DPRINT1("FontSizeChange: CreateConsoleFont2() failed\n");

    /* Retrieve the real character size in pixels */
    GetFontCellSize(NULL, hFont, &CharHeight, &CharWidth);

    /*
     * Update the font preview as well, and store the font handle. It will be
     * freed at later update or when the font preview is refreshed or reset.
     * For TrueType fonts, the preview will show the actual character width.
     */
    UpdateFontPreview(&FontPreview, hFont, CharWidth, CharHeight);

    /*
     * Format:
     * Width  = FontSize.X = LOWORD(FontSize);
     * Height = FontSize.Y = HIWORD(FontSize);
     */
    pConInfo->FontSize.X = (SHORT)(SizeList->UseRasterOrTTList ? CharWidth : 0);
    pConInfo->FontSize.Y = (SHORT)CharHeight;

    DPRINT("pConInfo->FontSize = (%d x %d) ; (CharWidth x CharHeight) = (%d x %d)\n",
           pConInfo->FontSize.X, pConInfo->FontSize.Y, CharWidth, CharHeight);

    InvalidateRect(GetDlgItem(hDlg, IDC_STATIC_FONT_WINDOW_PREVIEW), NULL, TRUE);
    InvalidateRect(GetDlgItem(hDlg, IDC_STATIC_SELECT_FONT_PREVIEW), NULL, TRUE);

    StringCchPrintfW(szFontSize, ARRAYSIZE(szFontSize), L"%d", CharWidth);
    SetDlgItemText(hDlg, IDC_FONT_SIZE_X, szFontSize);
    StringCchPrintfW(szFontSize, ARRAYSIZE(szFontSize), L"%d", CharHeight);
    SetDlgItemText(hDlg, IDC_FONT_SIZE_Y, szFontSize);

    return TRUE;
}


INT_PTR
CALLBACK
FontProc(HWND hDlg,
         UINT uMsg,
         WPARAM wParam,
         LPARAM lParam)
{
    PFONTSIZE_LIST_CTL SizeList;

    SizeList = (PFONTSIZE_LIST_CTL)GetWindowLongPtrW(hDlg, DWLP_USER);

    switch (uMsg)
    {
        case WM_INITDIALOG:
        {
            HWND hFontList = GetDlgItem(hDlg, IDC_LBOX_FONTTYPE);

            SizeList = (PFONTSIZE_LIST_CTL)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*SizeList));
            if (!SizeList)
            {
                EndDialog(hDlg, 0);
                return (INT_PTR)TRUE;
            }
            SizeList->RasterSizeList.hWndList = GetDlgItem(hDlg, IDC_LBOX_FONTSIZE);
            SizeList->RasterSizeList.GetCount = RasterSizeList_GetCount;
            SizeList->RasterSizeList.GetData  = RasterSizeList_GetData;
            SizeList->hWndTTSizeList = GetDlgItem(hDlg, IDC_CBOX_FONTSIZE);
            SizeList->bIsTTSizeDirty = FALSE;
            SetWindowLongPtrW(hDlg, DWLP_USER, (LONG_PTR)SizeList);

            /* By default show the raster font size list */
            SizeList->UseRasterOrTTList = TRUE;

            /* By default show the font sizes in pixel units */
            CheckRadioButton(hDlg, IDC_RADIO_PIXEL_UNIT, IDC_RADIO_POINT_UNIT, IDC_RADIO_PIXEL_UNIT);
            SizeList->TTSizePixelUnit = TRUE;

            UpdateFontSizeList(hDlg, SizeList);

            /* Initialize the font list */
            FaceNameList_Initialize(hFontList, ConInfo->CodePage);

            /* Select the current font */
            DPRINT1("ConInfo->FaceName = '%S'\n", ConInfo->FaceName);
            FaceNameList_SelectFont(hDlg, hFontList,
                                    SizeList,
                                    ConInfo->FaceName,
                                    ConInfo->FontFamily,
                                    ConInfo->FontWeight,
                                    ConInfo->FontSize);

            /* Refresh everything */
            FontTypeChange(hDlg, SizeList, ConInfo);

            return TRUE;
        }

        case WM_DESTROY:
        {
            if (SizeList)
                HeapFree(GetProcessHeap(), 0, SizeList);
            return (INT_PTR)TRUE;
        }

        case WM_DRAWITEM:
        {
            LPDRAWITEMSTRUCT drawItem = (LPDRAWITEMSTRUCT)lParam;

            if (drawItem->CtlID == IDC_STATIC_SELECT_FONT_PREVIEW)
                PaintText(drawItem, ConInfo, Screen);

            return TRUE;
        }

        case WM_DISPLAYCHANGE:
        {
            /* Retransmit to the preview window */
            SendDlgItemMessageW(hDlg, IDC_STATIC_FONT_WINDOW_PREVIEW,
                                WM_DISPLAYCHANGE, wParam, lParam);
            break;
        }

#if 0
        case PSM_QUERYSIBLINGS:
        {
            /*
             * If this is a notification from the "Options" dialog because we
             * changed the code page, treat it using the WM_FONTCHANGE case,
             * otherwise ignore it.
             */
            if (wParam != IDL_CODEPAGE)
                return FALSE;

            /* Fall through */
        }
#endif

        case WM_FONTCHANGE:
        {
            /* The pool of font resources has changed, re-enumerate the fonts */
            HWND hFontList = GetDlgItem(hDlg, IDC_LBOX_FONTTYPE);

            /* Initialize the font list */
            FaceNameList_Initialize(hFontList, ConInfo->CodePage);

            /* Select the current font */
            FaceNameList_SelectFont(hDlg, hFontList,
                                    SizeList,
                                    ConInfo->FaceName,
                                    ConInfo->FontFamily,
                                    ConInfo->FontWeight,
                                    ConInfo->FontSize);

            /* Refresh everything */
            FontTypeChange(hDlg, SizeList, ConInfo);
            break;
        }

        case WM_NOTIFY:
        {
            switch (((LPNMHDR)lParam)->code)
            {
                case PSN_APPLY:
                {
                    ApplyConsoleInfo(hDlg);
                    return TRUE;
                }
            }

            break;
        }

        case WM_COMMAND:
        {
            if (HIWORD(wParam) == LBN_SELCHANGE /* || CBN_SELCHANGE */)
            {
                switch (LOWORD(wParam))
                {
                    case IDC_LBOX_FONTTYPE:
                    {
                        /* Change the property sheet state only if the font has really changed */
                        if (FontTypeChange(hDlg, SizeList, ConInfo))
                            PropSheet_Changed(GetParent(hDlg), hDlg);
                        break;
                    }

                    case IDC_LBOX_FONTSIZE:
                    case IDC_CBOX_FONTSIZE:
                    {
                        /* Change the property sheet state only if the font has really changed */
                        if (FontSizeChange(hDlg, SizeList, ConInfo))
                            PropSheet_Changed(GetParent(hDlg), hDlg);
                        break;
                    }
                }
            }
            /* NOTE: CBN_EDITUPDATE is sent first, and is followed by CBN_EDITCHANGE */
            else if (HIWORD(wParam) == CBN_EDITUPDATE && LOWORD(wParam) == IDC_CBOX_FONTSIZE)
            {
                ULONG FontSize;
                PWCHAR pszNext = NULL;
                WCHAR szFontSize[100];
                WCHAR szMessage[260];

                /* Read the ComboBox edit string, as the user has entered a custom size */
                GetWindowTextW(SizeList->hWndTTSizeList, szFontSize, ARRAYSIZE(szFontSize));

                /* Validate the font size */
                FontSize = wcstoul(szFontSize, &pszNext, 10);
                if ((FontSize == 0) || (*pszNext))
                {
                    // FIXME: Localize!
                    StringCchPrintfW(szMessage, ARRAYSIZE(szMessage), L"\"%s\" is not a valid font size.", szFontSize);
                    MessageBoxW(hDlg, szMessage, L"Error", MB_ICONINFORMATION | MB_OK);
                }
                /**/SizeList->bIsTTSizeDirty = TRUE;/**/
            }
            else if (HIWORD(wParam) == CBN_KILLFOCUS && LOWORD(wParam) == IDC_CBOX_FONTSIZE)
            {
                /* Change the property sheet state only if the font has really changed */
                if (FontSizeChange(hDlg, SizeList, ConInfo))
                    PropSheet_Changed(GetParent(hDlg), hDlg);
            }
            else
            if (HIWORD(wParam) == BN_CLICKED)
            {
                switch (LOWORD(wParam))
                {
                case IDC_CHECK_BOLD_FONTS:
                {
                    if (IsDlgButtonChecked(hDlg, IDC_CHECK_BOLD_FONTS) == BST_CHECKED)
                        ConInfo->FontWeight = FW_BOLD;
                    else
                        ConInfo->FontWeight = FW_NORMAL;

                    FontTypeChange(hDlg, SizeList, ConInfo);
                    PropSheet_Changed(GetParent(hDlg), hDlg);
                    break;
                }

                case IDC_RADIO_PIXEL_UNIT:
                case IDC_RADIO_POINT_UNIT:
                {
                    SizeList->TTSizePixelUnit = (LOWORD(wParam) == IDC_RADIO_PIXEL_UNIT);

                    /* The call is valid only for TrueType fonts */
                    if (CurrentFontType != TRUETYPE_FONTTYPE)
                        break;

                    /* Change the property sheet state only if the font has really changed */
                    if (FontSizeChange(hDlg, SizeList, ConInfo))
                        PropSheet_Changed(GetParent(hDlg), hDlg);
                    break;
                }
                }
            }

            break;
        }

        default:
            break;
    }

    return FALSE;
}