[CONSOLE.CPL]: Simplify some parts of font.c code.

- Use a helper function "AddFontToList" to add font names into the font list;
- Use string-safe functions where needed.

svn path=/trunk/; revision=74405
This commit is contained in:
Hermès Bélusca-Maïto 2017-04-24 17:01:07 +00:00
parent 384affe098
commit 83e0dd0595
2 changed files with 52 additions and 58 deletions

View file

@ -2,6 +2,7 @@
#define CONSOLE_H__ #define CONSOLE_H__
#include <stdio.h> #include <stdio.h>
#include <wchar.h>
#define WIN32_NO_STATUS #define WIN32_NO_STATUS
@ -12,10 +13,11 @@
#include <winreg.h> #include <winreg.h>
#include <winuser.h> #include <winuser.h>
#include <wincon.h> #include <wincon.h>
#include <wchar.h>
#include <commctrl.h> #include <commctrl.h>
#include <cpl.h> #include <cpl.h>
#include <strsafe.h>
#include "resource.h" #include "resource.h"
#define EnableDlgItem(hDlg, nID, bEnable) \ #define EnableDlgItem(hDlg, nID, bEnable) \

View file

@ -114,6 +114,34 @@ BYTE CodePageToCharSet(UINT CodePage)
return DEFAULT_CHARSET; return DEFAULT_CHARSET;
} }
static VOID
AddFontToList(
IN HWND hWndList,
IN LPCWSTR pszFontName,
IN DWORD FontType)
{
INT idx;
/* Make sure the font doesn't already exist in the list */
if (SendMessageW(hWndList, LB_FINDSTRINGEXACT, 0, (LPARAM)pszFontName) != LB_ERR)
return;
/* Add the font */
idx = (INT)SendMessageW(hWndList, LB_ADDSTRING, 0, (LPARAM)pszFontName);
if (idx == LB_ERR)
{
DPRINT1("Failed to add font '%S'\n", pszFontName);
return;
}
DPRINT1("Add font '%S'\n", pszFontName);
/* Store this information in the list-item's userdata area */
// SendMessageW(hWndList, LB_SETITEMDATA, idx, MAKEWPARAM(fFixed, fTrueType));
SendMessageW(hWndList, LB_SETITEMDATA, idx, (WPARAM)FontType);
}
static BOOL CALLBACK static BOOL CALLBACK
EnumFontNamesProc(PLOGFONTW lplf, EnumFontNamesProc(PLOGFONTW lplf,
PNEWTEXTMETRICW lpntm, PNEWTEXTMETRICW lpntm,
@ -226,18 +254,8 @@ EnumFontNamesProc(PLOGFONTW lplf,
return TRUE; return TRUE;
} }
/* Make sure the font doesn't already exist in the list */ /* Add the font to the list */
if (SendMessageW(hwndCombo, LB_FINDSTRINGEXACT, 0, (LPARAM)pszName) == LB_ERR) AddFontToList(hwndCombo, pszName, /* MAKEWPARAM(fFixed, fTrueType) */ FontType);
{
/* Add the font */
INT idx = (INT)SendMessageW(hwndCombo, LB_ADDSTRING, 0, (LPARAM)pszName);
DPRINT1("Add font '%S' (lfPitchAndFamily = %d)\n", pszName, lplf->lfPitchAndFamily);
/* Store this information in the list-item's userdata area */
// SendMessageW(hwndCombo, LB_SETITEMDATA, idx, MAKEWPARAM(fFixed, fTrueType));
SendMessageW(hwndCombo, LB_SETITEMDATA, idx, (WPARAM)FontType);
}
return TRUE; return TRUE;
} }
@ -312,18 +330,16 @@ static VOID
FontTypeChange(HWND hwndDlg, FontTypeChange(HWND hwndDlg,
PCONSOLE_STATE_INFO pConInfo) PCONSOLE_STATE_INFO pConInfo)
{ {
HWND hListBox = GetDlgItem(hwndDlg, IDC_LBOX_FONTTYPE);
INT Length, nSel; INT Length, nSel;
LPWSTR FaceName; LPWSTR FaceName;
HDC hDC; HDC hDC;
LOGFONTW lf; LOGFONTW lf;
nSel = (INT)SendDlgItemMessageW(hwndDlg, IDC_LBOX_FONTTYPE, nSel = (INT)SendMessageW(hListBox, LB_GETCURSEL, 0, 0);
LB_GETCURSEL, 0, 0);
if (nSel == LB_ERR) return; if (nSel == LB_ERR) return;
Length = (INT)SendDlgItemMessageW(hwndDlg, IDC_LBOX_FONTTYPE, Length = (INT)SendMessageW(hListBox, LB_GETTEXTLEN, nSel, 0);
LB_GETTEXTLEN, nSel, 0);
if (Length == LB_ERR) return; if (Length == LB_ERR) return;
FaceName = HeapAlloc(GetProcessHeap(), FaceName = HeapAlloc(GetProcessHeap(),
@ -331,21 +347,17 @@ FontTypeChange(HWND hwndDlg,
(Length + 1) * sizeof(WCHAR)); (Length + 1) * sizeof(WCHAR));
if (FaceName == NULL) return; if (FaceName == NULL) return;
Length = (INT)SendDlgItemMessageW(hwndDlg, IDC_LBOX_FONTTYPE, Length = (INT)SendMessageW(hListBox, LB_GETTEXT, nSel, (LPARAM)FaceName);
LB_GETTEXT, nSel, (LPARAM)FaceName); FaceName[Length] = L'\0';
FaceName[Length] = '\0';
Length = min(Length/*wcslen(FaceName) + 1*/, LF_FACESIZE - 1); // wcsnlen StringCchCopyW(pConInfo->FaceName, ARRAYSIZE(pConInfo->FaceName), FaceName);
wcsncpy(pConInfo->FaceName, FaceName, LF_FACESIZE);
pConInfo->FaceName[Length] = L'\0';
DPRINT1("pConInfo->FaceName = '%S'\n", pConInfo->FaceName); DPRINT1("pConInfo->FaceName = '%S'\n", pConInfo->FaceName);
/* Enumerate the available sizes for the selected font */ /* Enumerate the available sizes for the selected font */
ZeroMemory(&lf, sizeof(lf)); ZeroMemory(&lf, sizeof(lf));
lf.lfCharSet = DEFAULT_CHARSET; // CodePageToCharSet(pConInfo->CodePage); lf.lfCharSet = DEFAULT_CHARSET; // CodePageToCharSet(pConInfo->CodePage);
// lf.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE; // lf.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
wcsncpy(lf.lfFaceName, FaceName, LF_FACESIZE); StringCchCopyW(lf.lfFaceName, ARRAYSIZE(lf.lfFaceName), FaceName);
lf.lfFaceName[Length] = L'\0';
hDC = GetDC(NULL); hDC = GetDC(NULL);
EnumFontFamiliesExW(hDC, &lf, (FONTENUMPROCW)EnumFontSizesProc, EnumFontFamiliesExW(hDC, &lf, (FONTENUMPROCW)EnumFontSizesProc,
@ -365,12 +377,12 @@ static VOID
FontSizeChange(HWND hwndDlg, FontSizeChange(HWND hwndDlg,
PCONSOLE_STATE_INFO pConInfo) PCONSOLE_STATE_INFO pConInfo)
{ {
HWND hListBox = GetDlgItem(hwndDlg, IDC_LBOX_FONTSIZE);
INT nSel; INT nSel;
ULONG FontSize; ULONG FontSize;
WCHAR FontSizeStr[20]; WCHAR FontSizeStr[20];
nSel = (INT)SendDlgItemMessageW(hwndDlg, IDC_LBOX_FONTSIZE, nSel = (INT)SendMessageW(hListBox, LB_GETCURSEL, 0, 0);
LB_GETCURSEL, 0, 0);
if (nSel == LB_ERR) return; if (nSel == LB_ERR) return;
/* /*
@ -378,8 +390,7 @@ FontSizeChange(HWND hwndDlg,
* Width = FontSize.X = LOWORD(FontSize); * Width = FontSize.X = LOWORD(FontSize);
* Height = FontSize.Y = HIWORD(FontSize); * Height = FontSize.Y = HIWORD(FontSize);
*/ */
FontSize = (ULONG)SendDlgItemMessageW(hwndDlg, IDC_LBOX_FONTSIZE, FontSize = (ULONG)SendMessageW(hListBox, LB_GETITEMDATA, nSel, 0);
LB_GETITEMDATA, nSel, 0);
if (FontSize == LB_ERR) return; if (FontSize == LB_ERR) return;
pConInfo->FontSize.X = LOWORD(FontSize); pConInfo->FontSize.X = LOWORD(FontSize);
@ -390,9 +401,9 @@ FontSizeChange(HWND hwndDlg,
InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_SELECT_FONT_PREVIEW), NULL, TRUE); InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_SELECT_FONT_PREVIEW), NULL, TRUE);
swprintf(FontSizeStr, L"%2d", pConInfo->FontSize.X); swprintf(FontSizeStr, L"%2d", pConInfo->FontSize.X);
SetWindowText(GetDlgItem(hwndDlg, IDC_FONT_SIZE_X), FontSizeStr); SetDlgItemText(hwndDlg, IDC_FONT_SIZE_X, FontSizeStr);
swprintf(FontSizeStr, L"%2d", pConInfo->FontSize.Y); swprintf(FontSizeStr, L"%2d", pConInfo->FontSize.Y);
SetWindowText(GetDlgItem(hwndDlg, IDC_FONT_SIZE_Y), FontSizeStr); SetDlgItemText(hwndDlg, IDC_FONT_SIZE_Y, FontSizeStr);
} }
@ -409,6 +420,7 @@ FontProc(HWND hwndDlg,
{ {
case WM_INITDIALOG: case WM_INITDIALOG:
{ {
HWND hListBox = GetDlgItem(hwndDlg, IDC_LBOX_FONTTYPE);
HDC hDC; HDC hDC;
LOGFONTW lf; LOGFONTW lf;
INT idx; INT idx;
@ -418,44 +430,24 @@ FontProc(HWND hwndDlg,
// lf.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE; // lf.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
hDC = GetDC(NULL); hDC = GetDC(NULL);
EnumFontFamiliesExW(hDC, &lf, (FONTENUMPROCW)EnumFontNamesProc, EnumFontFamiliesExW(hDC, &lf, (FONTENUMPROCW)EnumFontNamesProc, (LPARAM)hListBox, 0);
(LPARAM)GetDlgItem(hwndDlg, IDC_LBOX_FONTTYPE), 0);
ReleaseDC(NULL, hDC); ReleaseDC(NULL, hDC);
idx = (INT)SendDlgItemMessageW(hwndDlg, IDC_LBOX_FONTTYPE, LB_GETCOUNT, 0, 0); idx = (INT)SendMessageW(hListBox, LB_GETCOUNT, 0, 0);
if ((idx == 0) || (idx == LB_ERR)) if ((idx == 0) || (idx == LB_ERR))
{ {
DPRINT1("The ideal console fonts are not found; manually add default ones.\n"); DPRINT1("The ideal console fonts are not found; manually add default ones.\n");
/* This world is not ideal. We have to do it realistically. */ /* This world is not ideal. We have to do it realistically. */
idx = (INT)SendDlgItemMessageW(hwndDlg, IDC_LBOX_FONTTYPE, LB_ADDSTRING, 0, AddFontToList(hListBox, L"Lucida Console", TRUETYPE_FONTTYPE);
(LPARAM)L"Lucida Console");
if (idx != LB_ERR)
{
SendDlgItemMessageW(hwndDlg, IDC_LBOX_FONTTYPE,
LB_SETITEMDATA, idx, TRUETYPE_FONTTYPE);
}
if (CodePageToCharSet(ConInfo->CodePage) != DEFAULT_CHARSET) if (CodePageToCharSet(ConInfo->CodePage) != DEFAULT_CHARSET)
{ AddFontToList(hListBox, L"Droid Sans Fallback", TRUETYPE_FONTTYPE);
idx = (INT)SendDlgItemMessageW(hwndDlg, IDC_LBOX_FONTTYPE, LB_ADDSTRING, 0,
(LPARAM)L"Droid Sans Fallback");
if (idx != LB_ERR)
{
SendDlgItemMessageW(hwndDlg, IDC_LBOX_FONTTYPE,
LB_SETITEMDATA, idx, TRUETYPE_FONTTYPE);
}
}
} }
DPRINT1("ConInfo->FaceName = '%S'\n", ConInfo->FaceName); DPRINT1("ConInfo->FaceName = '%S'\n", ConInfo->FaceName);
idx = (INT)SendDlgItemMessageW(hwndDlg, IDC_LBOX_FONTTYPE, idx = (INT)SendMessageW(hListBox, LB_FINDSTRINGEXACT, 0, (LPARAM)ConInfo->FaceName);
LB_FINDSTRINGEXACT, 0, (LPARAM)ConInfo->FaceName);
if (idx != LB_ERR) if (idx != LB_ERR)
{ SendMessageW(hListBox, LB_SETCURSEL, (WPARAM)idx, 0);
SendDlgItemMessageW(hwndDlg, IDC_LBOX_FONTTYPE,
LB_SETCURSEL, (WPARAM)idx, 0);
}
FontTypeChange(hwndDlg, ConInfo); FontTypeChange(hwndDlg, ConInfo);