[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__
#include <stdio.h>
#include <wchar.h>
#define WIN32_NO_STATUS
@ -12,10 +13,11 @@
#include <winreg.h>
#include <winuser.h>
#include <wincon.h>
#include <wchar.h>
#include <commctrl.h>
#include <cpl.h>
#include <strsafe.h>
#include "resource.h"
#define EnableDlgItem(hDlg, nID, bEnable) \

View file

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