Author: hbelusca Date: Mon Apr 24 17:01:07 2017 New Revision: 74405
URL: http://svn.reactos.org/svn/reactos?rev=74405&view=rev Log: [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.
Modified: trunk/reactos/dll/cpl/console/console.h trunk/reactos/dll/cpl/console/font.c
Modified: trunk/reactos/dll/cpl/console/console.h URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/cpl/console/console.h?r... ============================================================================== --- trunk/reactos/dll/cpl/console/console.h [iso-8859-1] (original) +++ trunk/reactos/dll/cpl/console/console.h [iso-8859-1] Mon Apr 24 17:01:07 2017 @@ -2,6 +2,7 @@ #define CONSOLE_H__
#include <stdio.h> +#include <wchar.h>
#define WIN32_NO_STATUS
@@ -12,9 +13,10 @@ #include <winreg.h> #include <winuser.h> #include <wincon.h> -#include <wchar.h> #include <commctrl.h> #include <cpl.h> + +#include <strsafe.h>
#include "resource.h"
Modified: trunk/reactos/dll/cpl/console/font.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/cpl/console/font.c?rev=... ============================================================================== --- trunk/reactos/dll/cpl/console/font.c [iso-8859-1] (original) +++ trunk/reactos/dll/cpl/console/font.c [iso-8859-1] Mon Apr 24 17:01:07 2017 @@ -112,6 +112,34 @@ return CharInfo.ciCharset; else 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 @@ -226,18 +254,8 @@ 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 @@ 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 @@ (Length + 1) * sizeof(WCHAR)); if (FaceName == NULL) return;
- Length = (INT)SendDlgItemMessageW(hwndDlg, IDC_LBOX_FONTTYPE, - LB_GETTEXT, nSel, (LPARAM)FaceName); - FaceName[Length] = '\0'; - - Length = min(Length/*wcslen(FaceName) + 1*/, LF_FACESIZE - 1); // wcsnlen - wcsncpy(pConInfo->FaceName, FaceName, LF_FACESIZE); - pConInfo->FaceName[Length] = L'\0'; + Length = (INT)SendMessageW(hListBox, LB_GETTEXT, nSel, (LPARAM)FaceName); + 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 @@ 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 @@ * 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 @@ 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 @@ { case WM_INITDIALOG: { + HWND hListBox = GetDlgItem(hwndDlg, IDC_LBOX_FONTTYPE); HDC hDC; LOGFONTW lf; INT idx; @@ -418,44 +430,24 @@ // 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);