[CONSOLE.CPL] Fix the console screen preview when selecting TrueType fonts.
authorHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Sat, 4 May 2019 23:10:39 +0000 (01:10 +0200)
committerHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Sat, 4 May 2019 23:20:19 +0000 (01:20 +0200)
CORE-13182 CORE-13196

- Use the correct character height & width.
- Additions: use StringCch*() when initializing the dialog title.

[CONSRV:CONCFG] Minor fixes.

- When retrieving font characteristics in ConCfgReadUserSettings(),
  check for NULL/zero values that indicate that we should use default
  ones instead.
- Rename 'dwNumSubKeys' into 'dwNumValues'.

dll/cpl/console/console.c
dll/cpl/console/console.h
dll/cpl/console/font.c
dll/cpl/console/layout.c
win32ss/user/winsrv/concfg/settings.c

index d39253a..89e9ac5 100644 (file)
@@ -228,25 +228,20 @@ InitApplet(HANDLE hSectionOrWnd)
     }
 
     /* Initialize the font support */
-    hCurrentFont = CreateConsoleFont(ConInfo);
-    if (hCurrentFont == NULL)
-        DPRINT1("InitApplet: CreateConsoleFont failed\n");
+    FontPreview.hFont = CreateConsoleFont(ConInfo);
+    if (FontPreview.hFont == NULL)
+        DPRINT1("InitApplet: CreateConsoleFont() failed\n");
+    GetFontCellSize(NULL, FontPreview.hFont, &FontPreview.CharHeight, &FontPreview.CharWidth);
 
     /* Initialize the property sheet structure */
     ZeroMemory(&psh, sizeof(psh));
     psh.dwSize = sizeof(psh);
-    psh.dwFlags = PSH_PROPSHEETPAGE | PSH_PROPTITLE | /* PSH_USEHICON */ PSH_USEICONID | PSH_NOAPPLYNOW | PSH_USECALLBACK;
+    psh.dwFlags = PSH_PROPSHEETPAGE | PSH_PROPTITLE | /* PSH_USEHICON */ PSH_USEICONID | PSH_NOAPPLYNOW | PSH_USECALLBACK;
 
     if (ConInfo->ConsoleTitle[0] != UNICODE_NULL)
-    {
-        wcsncpy(szTitle, L"\"", MAX_PATH);
-        wcsncat(szTitle, ConInfo->ConsoleTitle, MAX_PATH - wcslen(szTitle));
-        wcsncat(szTitle, L"\"", MAX_PATH - wcslen(szTitle));
-    }
+        StringCchPrintfW(szTitle, ARRAYSIZE(szTitle), L"\"%s\"", ConInfo->ConsoleTitle);
     else
-    {
-        wcscpy(szTitle, L"ReactOS Console");
-    }
+        StringCchCopyW(szTitle, ARRAYSIZE(szTitle), L"ReactOS Console");
     psh.pszCaption = szTitle;
 
     if (pSharedInfo != NULL)
@@ -278,9 +273,9 @@ InitApplet(HANDLE hSectionOrWnd)
     Result = PropertySheetW(&psh);
     UnRegisterWinPrevClass(hApplet);
 
-    /* First cleanup */
-    if (hCurrentFont) DeleteObject(hCurrentFont);
-    hCurrentFont = NULL;
+    /* Clear the font support */
+    if (FontPreview.hFont) DeleteObject(FontPreview.hFont);
+    FontPreview.hFont = NULL;
 
     /* Save the console settings */
     if (SetConsoleInfo)
index 04508bf..0f7b592 100644 (file)
@@ -35,10 +35,17 @@ typedef enum _TEXT_TYPE
     Popup
 } TEXT_TYPE;
 
+typedef struct _FONT_PREVIEW
+{
+    HFONT hFont;
+    UINT  CharWidth;
+    UINT  CharHeight;
+} FONT_PREVIEW;
+
 /* Globals */
 extern HINSTANCE hApplet;
 extern PCONSOLE_STATE_INFO ConInfo;
-extern HFONT hCurrentFont;
+extern FONT_PREVIEW FontPreview;
 
 VOID ApplyConsoleInfo(HWND hwndDlg);
 
index 45a2612..66c9cc6 100644 (file)
@@ -18,7 +18,7 @@
  * Current active font, corresponding to the active console font,
  * and used for painting the text samples.
  */
-HFONT hCurrentFont = NULL;
+FONT_PREVIEW FontPreview = {NULL, 0, 0};
 
 
 /*
@@ -597,8 +597,7 @@ FontSizeChange(
     IN PFONTSIZE_LIST_CTL SizeList,
     IN OUT PCONSOLE_STATE_INFO pConInfo)
 {
-    HDC hDC;
-    LONG CharWidth, CharHeight, FontSize;
+    LONG FontSize, CharWidth, CharHeight;
     WCHAR szFontSize[100];
 
     /*
@@ -614,33 +613,32 @@ FontSizeChange(
     CharHeight = (SizeList->UseRasterOrTTList ? (LONG)HIWORD(FontSize) : FontSize);
     CharWidth  = (SizeList->UseRasterOrTTList ? (LONG)LOWORD(FontSize) : 0);
 
-    if (hCurrentFont) DeleteObject(hCurrentFont);
-    hCurrentFont = CreateConsoleFont2(CharHeight, CharWidth, pConInfo);
-    if (hCurrentFont == NULL)
-        DPRINT1("FontSizeChange: CreateConsoleFont2 failed\n");
+    if (FontPreview.hFont) DeleteObject(FontPreview.hFont);
+    FontPreview.hFont = CreateConsoleFont2(CharHeight, CharWidth, pConInfo);
+    if (FontPreview.hFont == NULL)
+        DPRINT1("FontSizeChange: CreateConsoleFont2() failed\n");
 
     /* Retrieve the real character size in pixels */
-    hDC = GetDC(NULL);
-    GetFontCellSize(hDC, hCurrentFont, (PUINT)&CharHeight, (PUINT)&CharWidth);
-    ReleaseDC(NULL, hDC);
+    GetFontCellSize(NULL, FontPreview.hFont, &FontPreview.CharHeight, &FontPreview.CharWidth);
 
     /*
      * Format:
      * Width  = FontSize.X = LOWORD(FontSize);
      * Height = FontSize.Y = HIWORD(FontSize);
      */
-    pConInfo->FontSize.X = (SHORT)(SizeList->UseRasterOrTTList ? CharWidth : 0);
-    pConInfo->FontSize.Y = (SHORT)CharHeight;
+    pConInfo->FontSize.X = (SHORT)(SizeList->UseRasterOrTTList ? FontPreview.CharWidth : 0);
+    pConInfo->FontSize.Y = (SHORT)FontPreview.CharHeight;
 
     DPRINT1("pConInfo->FontSize = (%d x %d) ; (CharWidth x CharHeight) = (%d x %d)\n",
-            pConInfo->FontSize.X, pConInfo->FontSize.Y, CharWidth, CharHeight);
+            pConInfo->FontSize.X, pConInfo->FontSize.Y,
+            FontPreview.CharWidth, FontPreview.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);
+    StringCchPrintfW(szFontSize, ARRAYSIZE(szFontSize), L"%d", FontPreview.CharWidth);
     SetDlgItemText(hDlg, IDC_FONT_SIZE_X, szFontSize);
-    StringCchPrintfW(szFontSize, ARRAYSIZE(szFontSize), L"%d", CharHeight);
+    StringCchPrintfW(szFontSize, ARRAYSIZE(szFontSize), L"%d", FontPreview.CharHeight);
     SetDlgItemText(hDlg, IDC_FONT_SIZE_Y, szFontSize);
 
     return TRUE;
index 9123a1f..f8d9986 100644 (file)
@@ -205,8 +205,8 @@ WinPrev_OnDraw(
 
     /* We start with the console client area, rescaled for the preview */
     SetRect(&rcWin, 0, 0,
-            pConInfo->WindowSize.X * pConInfo->FontSize.X,
-            pConInfo->WindowSize.Y * pConInfo->FontSize.Y);
+            pConInfo->WindowSize.X * FontPreview.CharWidth,
+            pConInfo->WindowSize.Y * FontPreview.CharHeight);
     RescaleRect(pData, rcWin);
 
     /* Add the scrollbars if needed (does not account for any frame) */
@@ -489,7 +489,7 @@ PaintText(
     hBrush = CreateSolidBrush(nbkColor);
     if (!hBrush) return;
 
-    hOldFont = SelectObject(drawItem->hDC, hCurrentFont);
+    hOldFont = SelectObject(drawItem->hDC, FontPreview.hFont);
     //if (hOldFont == NULL)
     //{
     //    DeleteObject(hBrush);
index fcc66d8..2c7624f 100644 (file)
@@ -120,7 +120,7 @@ ConCfgOpenUserSettings(
     Status = RtlOpenCurrentUser(/*samDesired*/MAXIMUM_ALLOWED, (PHANDLE)&/*CurrentUserKeyHandle*/hKey);
     if (!NT_SUCCESS(Status))
     {
-        DPRINT1("RtlOpenCurrentUser failed, Status = 0x%08lx\n", Status);
+        DPRINT1("RtlOpenCurrentUser() failed, Status = 0x%08lx\n", Status);
         SetLastError(RtlNtStatusToDosError(Status));
         return FALSE;
     }
@@ -170,7 +170,7 @@ ConCfgReadUserSettings(
 {
     BOOLEAN Success = FALSE;
     HKEY  hKey;
-    DWORD dwNumSubKeys = 0;
+    DWORD dwNumValues = 0;
     DWORD dwIndex;
     DWORD dwColorIndex = 0;
     DWORD dwType;
@@ -183,21 +183,21 @@ ConCfgReadUserSettings(
     if (!ConCfgOpenUserSettings(DefaultSettings ? L"" : ConsoleInfo->ConsoleTitle,
                                 &hKey, KEY_READ, FALSE))
     {
-        DPRINT("ConCfgOpenUserSettings failed\n");
+        DPRINT("ConCfgOpenUserSettings() failed\n");
         return FALSE;
     }
 
     if (RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, NULL, NULL,
-                         &dwNumSubKeys, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
+                         &dwNumValues, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
     {
-        DPRINT("ConCfgReadUserSettings: RegQueryInfoKeyW failed\n");
+        DPRINT("ConCfgReadUserSettings: RegQueryInfoKeyW() failed\n");
         RegCloseKey(hKey);
         return FALSE;
     }
 
-    DPRINT("ConCfgReadUserSettings entered dwNumSubKeys %d\n", dwNumSubKeys);
+    DPRINT("ConCfgReadUserSettings() entered, dwNumValues %d\n", dwNumValues);
 
-    for (dwIndex = 0; dwIndex < dwNumSubKeys; dwIndex++)
+    for (dwIndex = 0; dwIndex < dwNumValues; dwIndex++)
     {
         dwValue = sizeof(Value);
         dwValueName = ARRAYSIZE(szValueName);
@@ -238,24 +238,36 @@ ConCfgReadUserSettings(
         }
         else if (!wcscmp(szValueName, L"FaceName"))
         {
-            StringCchCopyNW(ConsoleInfo->FaceName, ARRAYSIZE(ConsoleInfo->FaceName),
-                            szValue, ARRAYSIZE(szValue));
+            /* A NULL value means that the defaults should be used instead */
+            if (*szValue)
+            {
+                StringCchCopyNW(ConsoleInfo->FaceName, ARRAYSIZE(ConsoleInfo->FaceName),
+                                szValue, ARRAYSIZE(szValue));
+            }
             Success = TRUE;
         }
         else if (!wcscmp(szValueName, L"FontFamily"))
         {
-            ConsoleInfo->FontFamily = Value;
+            /* A zero value means that the defaults should be used instead */
+            if (Value)
+                ConsoleInfo->FontFamily = Value;
             Success = TRUE;
         }
         else if (!wcscmp(szValueName, L"FontSize"))
         {
-            ConsoleInfo->FontSize.X = LOWORD(Value); // Width
-            ConsoleInfo->FontSize.Y = HIWORD(Value); // Height
+            /* A zero value means that the defaults should be used instead */
+            if (Value)
+            {
+                ConsoleInfo->FontSize.X = LOWORD(Value); // Width
+                ConsoleInfo->FontSize.Y = HIWORD(Value); // Height
+            }
             Success = TRUE;
         }
         else if (!wcscmp(szValueName, L"FontWeight"))
         {
-            ConsoleInfo->FontWeight = Value;
+            /* A zero value means that the defaults should be used instead */
+            if (Value)
+                ConsoleInfo->FontWeight = Value;
             Success = TRUE;
         }
         else if (!wcscmp(szValueName, L"HistoryBufferSize"))