diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 000000000..9f7a6dfd3 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,36 @@ +# EditorConfig is awesome: https://EditorConfig.org +# +# VisualStudio: https://docs.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2017 + +# top-most EditorConfig file +root = true + +# Windows-style newlines with a newline ending every file +[*] +charset = utf-8 +end_of_line = crlf +insert_final_newline = true +trim_trailing_whitespace = false + +# Matches multiple files with brace expansion notation +[*.{h,c,cpp,hpp,cxx}] +# Set default charset +charset = utf-8-bom +# 2 space indentation +indent_style = space +indent_size = 2 +tab_width = 2 + +[language/**.h] +charset = utf-8 + +[minipath/language/**.h] +charset = utf-8 + +[scintilla/**.{h,c,cpp,hpp,cxx}] +charset = utf-8 +# tab (w=4) indentation +indent_style = tab +indent_size = 4 +tab_width = 4 + diff --git a/Notepad3.sln b/Notepad3.sln index 25c14befe..7f4fdba71 100644 --- a/Notepad3.sln +++ b/Notepad3.sln @@ -143,6 +143,11 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mp_ru_ru", "minipath\langua {29AB36E1-284E-4E0B-9DF4-F4F84760BD9B} = {29AB36E1-284E-4E0B-9DF4-F4F84760BD9B} EndProjectSection EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{7C0B9717-FC7C-4EEA-BC48-6107418A7D93}" + ProjectSection(SolutionItems) = preProject + .editorconfig = .editorconfig + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 diff --git a/src/ChooseFont/ChooseFont.cpp b/src/ChooseFont/ChooseFont.cpp index e33d74595..faf430ee4 100644 --- a/src/ChooseFont/ChooseFont.cpp +++ b/src/ChooseFont/ChooseFont.cpp @@ -49,39 +49,39 @@ class ChooseFontDialog { public: - explicit ChooseFontDialog(HWND hParent, const WCHAR* localeName, DPI_T dpi, LPCHOOSEFONT lpCFGDI); - virtual ~ChooseFontDialog(); - ChooseFontDialog() = delete; + explicit ChooseFontDialog(HWND hParent, const WCHAR* localeName, DPI_T dpi, LPCHOOSEFONT lpCFGDI); + virtual ~ChooseFontDialog(); + ChooseFontDialog() = delete; - HRESULT GetTextFormat(IDWriteTextFormat** textFormat); - HRESULT GetTextFormat(IDWriteTextFormat* textFormatIn, IDWriteTextFormat** textFormatOut); + HRESULT GetTextFormat(IDWriteTextFormat** textFormat); + HRESULT GetTextFormat(IDWriteTextFormat* textFormatIn, IDWriteTextFormat** textFormatOut); private: - HWND m_parent; - HWND m_dialog; - WCHAR m_localeName[LOCALE_NAME_MAX_LENGTH]; - DPI_T m_currentDPI; - LPCHOOSEFONT m_chooseFontStruct; - IDWriteFontCollection* m_fontCollection; - IDWriteTextFormat* m_currentTextFormat; - IDWriteTextFormat* m_renderTextFormat; - - HRESULT OnFontFamilySelect(); - HRESULT OnFontFaceSelect(); - HRESULT OnFontSizeSelect(); + HWND m_parent; + HWND m_dialog; + WCHAR m_localeName[LOCALE_NAME_MAX_LENGTH]; + DPI_T m_currentDPI; + LPCHOOSEFONT m_chooseFontStruct; + IDWriteFontCollection* m_fontCollection; + IDWriteTextFormat* m_currentTextFormat; + IDWriteTextFormat* m_renderTextFormat; - HRESULT OnFontFamilyNameEdit(HWND hwndFontFamilies); - HRESULT OnFontFaceNameEdit(HWND hwndFontFaces); - HRESULT OnFontSizeNameEdit(HWND hwndFontSizes); + HRESULT OnFontFamilySelect(); + HRESULT OnFontFaceSelect(); + HRESULT OnFontSizeSelect(); - HRESULT DrawSampleText(HDC sampleDC); + HRESULT OnFontFamilyNameEdit(HWND hwndFontFamilies); + HRESULT OnFontFaceNameEdit(HWND hwndFontFaces); + HRESULT OnFontSizeNameEdit(HWND hwndFontSizes); - static INT_PTR CALLBACK CFDialogProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); + HRESULT DrawSampleText(HDC sampleDC); - BOOL OnInitDialog(HWND dialog, HWND hwndFocus, LPARAM lParam); - void OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify); - void OnDrawItem(HWND hwnd, const DRAWITEMSTRUCT* lpDrawItem); + static INT_PTR CALLBACK CFDialogProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); + + BOOL OnInitDialog(HWND dialog, HWND hwndFocus, LPARAM lParam); + void OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify); + void OnDrawItem(HWND hwnd, const DRAWITEMSTRUCT* lpDrawItem); }; @@ -123,9 +123,9 @@ ChooseFontDialog::ChooseFontDialog(HWND hParent, const WCHAR* localeName, const ChooseFontDialog::~ChooseFontDialog() { - SafeRelease(&m_fontCollection); - SafeRelease(&m_currentTextFormat); - SafeRelease(&m_renderTextFormat); + SafeRelease(&m_fontCollection); + SafeRelease(&m_currentTextFormat); + SafeRelease(&m_renderTextFormat); } @@ -139,53 +139,51 @@ ChooseFontDialog::~ChooseFontDialog() HRESULT ChooseFontDialog::GetTextFormat(IDWriteTextFormat** textFormat) { - HRESULT hr = S_OK; + HRESULT hr = S_OK; - *textFormat = nullptr; - - // Default to the system font collection - SafeRelease(&m_fontCollection); - hr = g_dwrite->GetSystemFontCollection(&m_fontCollection); + *textFormat = nullptr; - // Create a default text format - if (SUCCEEDED(hr)) - { - SafeRelease(&m_currentTextFormat); + // Default to the system font collection + SafeRelease(&m_fontCollection); + hr = g_dwrite->GetSystemFontCollection(&m_fontCollection); - const WCHAR* const fontFamilyName = m_chooseFontStruct->lpLogFont->lfFaceName; - float const pointSize = static_cast(m_chooseFontStruct->iPointSize) / 10.0f; - auto const fontWeight = static_cast(m_chooseFontStruct->lpLogFont->lfWeight); // TODO: mapping? - DWRITE_FONT_STYLE const fontStyle = (m_chooseFontStruct->lpLogFont->lfItalic ? - DWRITE_FONT_STYLE_ITALIC : DWRITE_FONT_STYLE_NORMAL); - DWRITE_FONT_STRETCH const fontStretch = DWRITE_FONT_STRETCH_NORMAL; + // Create a default text format + if (SUCCEEDED(hr)) { + SafeRelease(&m_currentTextFormat); - hr = g_dwrite->CreateTextFormat( - fontFamilyName, - m_fontCollection, - fontWeight, - fontStyle, - fontStretch, - pointSize, - m_localeName, - &m_currentTextFormat); + const WCHAR* const fontFamilyName = m_chooseFontStruct->lpLogFont->lfFaceName; + float const pointSize = static_cast(m_chooseFontStruct->iPointSize) / 10.0f; + auto const fontWeight = static_cast(m_chooseFontStruct->lpLogFont->lfWeight); // TODO: mapping? + DWRITE_FONT_STYLE const fontStyle = (m_chooseFontStruct->lpLogFont->lfItalic ? + DWRITE_FONT_STYLE_ITALIC : DWRITE_FONT_STYLE_NORMAL); + DWRITE_FONT_STRETCH const fontStretch = DWRITE_FONT_STRETCH_NORMAL; + + hr = g_dwrite->CreateTextFormat( + fontFamilyName, + m_fontCollection, + fontWeight, + fontStyle, + fontStretch, + pointSize, + m_localeName, + &m_currentTextFormat); + } + + // Open the dialog + if (SUCCEEDED(hr)) { + if (Globals.hLngResContainer) { + hr = static_cast(DialogBoxParam(Globals.hLngResContainer, MAKEINTRESOURCE(IDD_MUI_CHOOSEFONT), m_parent, CFDialogProc, reinterpret_cast(this))); } - - // Open the dialog - if (SUCCEEDED(hr)) - { - if (Globals.hLngResContainer) { - hr = static_cast(DialogBoxParam(Globals.hLngResContainer, MAKEINTRESOURCE(IDD_MUI_CHOOSEFONT), m_parent, CFDialogProc, reinterpret_cast(this))); - } - else { - hr = static_cast(DialogBoxParam(g_hInstanceNP3, MAKEINTRESOURCE(IDD_MUI_CHOOSEFONT), m_parent, CFDialogProc, reinterpret_cast(this))); - } + else { + hr = static_cast(DialogBoxParam(g_hInstanceNP3, MAKEINTRESOURCE(IDD_MUI_CHOOSEFONT), m_parent, CFDialogProc, reinterpret_cast(this))); } + } - // If all went well, and the user didn't cancel, return the new format. - if (hr == S_OK) { - *textFormat = SafeDetach(&m_currentTextFormat); - } - return hr; + // If all went well, and the user didn't cancel, return the new format. + if (hr == S_OK) { + *textFormat = SafeDetach(&m_currentTextFormat); + } + return hr; } @@ -200,37 +198,35 @@ HRESULT ChooseFontDialog::GetTextFormat(IDWriteTextFormat** textFormat) HRESULT ChooseFontDialog::GetTextFormat(IDWriteTextFormat* textFormatIn, IDWriteTextFormat** textFormatOut) { - HRESULT hr = S_OK; + HRESULT hr = S_OK; - *textFormatOut = nullptr; + *textFormatOut = nullptr; - SafeSet(&m_currentTextFormat, textFormatIn); + SafeSet(&m_currentTextFormat, textFormatIn); - // Pull out the input font attributes - SafeRelease(&m_fontCollection); - hr = m_currentTextFormat->GetFontCollection(&m_fontCollection); - - if (SUCCEEDED(hr)) - { - hr = m_currentTextFormat->GetLocaleName(&m_localeName[0], _ARRAYSIZE(m_localeName)); + // Pull out the input font attributes + SafeRelease(&m_fontCollection); + hr = m_currentTextFormat->GetFontCollection(&m_fontCollection); + + if (SUCCEEDED(hr)) { + hr = m_currentTextFormat->GetLocaleName(&m_localeName[0], _ARRAYSIZE(m_localeName)); + } + + // Open the dialog + if (SUCCEEDED(hr)) { + if (Globals.hLngResContainer) { + hr = static_cast(DialogBoxParam(Globals.hLngResContainer, MAKEINTRESOURCE(IDD_MUI_CHOOSEFONT), m_parent, CFDialogProc, reinterpret_cast(this))); } - - // Open the dialog - if (SUCCEEDED(hr)) - { - if (Globals.hLngResContainer) { - hr = static_cast(DialogBoxParam(Globals.hLngResContainer, MAKEINTRESOURCE(IDD_MUI_CHOOSEFONT), m_parent, CFDialogProc, reinterpret_cast(this))); - } - else { - hr = static_cast(DialogBoxParam(g_hInstanceNP3, MAKEINTRESOURCE(IDD_MUI_CHOOSEFONT), m_parent, CFDialogProc, reinterpret_cast(this))); - } + else { + hr = static_cast(DialogBoxParam(g_hInstanceNP3, MAKEINTRESOURCE(IDD_MUI_CHOOSEFONT), m_parent, CFDialogProc, reinterpret_cast(this))); } + } - // If all went well, and the user didn't cancel, return the new format. - if (hr == S_OK) { - *textFormatOut = SafeDetach(&m_currentTextFormat); - } - return hr; + // If all went well, and the user didn't cancel, return the new format. + if (hr == S_OK) { + *textFormatOut = SafeDetach(&m_currentTextFormat); + } + return hr; } @@ -244,84 +240,76 @@ HRESULT ChooseFontDialog::GetTextFormat(IDWriteTextFormat* textFormatIn, IDWrite HRESULT ChooseFontDialog::OnFontFamilySelect() { - HRESULT hr = S_OK; + HRESULT hr = S_OK; - HWND hwndFontFamilyNames = GetDlgItem(m_dialog, IDC_FONT_FAMILY_NAMES); - HWND hwndFontFaceNames = GetDlgItem(m_dialog, IDC_FONT_FACE_NAMES); - int currentSelection = ComboBox_GetCurSel(hwndFontFamilyNames); + HWND hwndFontFamilyNames = GetDlgItem(m_dialog, IDC_FONT_FAMILY_NAMES); + HWND hwndFontFaceNames = GetDlgItem(m_dialog, IDC_FONT_FACE_NAMES); + int currentSelection = ComboBox_GetCurSel(hwndFontFamilyNames); - // Get the font family name - WCHAR fontFamilyName[100]; + // Get the font family name + WCHAR fontFamilyName[100]; - UINT32 fontFamilyNameLength = ComboBox_GetLBTextLen(hwndFontFamilyNames, currentSelection) + 1; - if (fontFamilyNameLength > _ARRAYSIZE(fontFamilyName)) - hr = E_NOT_SUFFICIENT_BUFFER; + UINT32 fontFamilyNameLength = ComboBox_GetLBTextLen(hwndFontFamilyNames, currentSelection) + 1; + if (fontFamilyNameLength > _ARRAYSIZE(fontFamilyName)) + hr = E_NOT_SUFFICIENT_BUFFER; - if (SUCCEEDED(hr)) - { - ComboBox_GetLBText(hwndFontFamilyNames, currentSelection, &fontFamilyName[0]); + if (SUCCEEDED(hr)) { + ComboBox_GetLBText(hwndFontFamilyNames, currentSelection, &fontFamilyName[0]); + } + + // Get the face names for the new font family + IDWriteFontFamily* fontFamily = nullptr; + std::vector fonts; + + // Get the font variants for this family + if (currentSelection != CB_ERR) + hr = GetFonts(m_fontCollection, fontFamilyName, fonts); + + // Initialize the face name list + std::vector fontFaceInfo; + if (SUCCEEDED(hr)) { + ComboBox_ResetContent(hwndFontFaceNames); + GetFontFaceInfo(fonts, m_localeName, fontFaceInfo); + } + + if (SUCCEEDED(hr)) { + for (size_t i = 0; i != fontFaceInfo.size(); ++i) { + int fontFaceIndex = ComboBox_AddString(hwndFontFaceNames, fontFaceInfo[i].fontFaceName); + ComboBox_SetItemData(hwndFontFaceNames, fontFaceIndex, fontFaceInfo[i].PackedFontAttributes()); + } + } + + // Select the best fit font face for the current attributes + if (SUCCEEDED(hr)) { + FontFaceInfo desiredAttributes( + L"", + m_currentTextFormat->GetFontWeight(), + m_currentTextFormat->GetFontStyle(), + m_currentTextFormat->GetFontStretch()); + + int selectedFontFaceName = 0; + ULONG bestFitAttributes = GetBestFontAttributes(m_fontCollection, fontFamilyName, desiredAttributes); + + int fontFaceCount = ComboBox_GetCount(hwndFontFaceNames); + + for (int i = 0; i != fontFaceCount; ++i) { + if (static_cast(ComboBox_GetItemData(hwndFontFaceNames, i)) == bestFitAttributes) { + selectedFontFaceName = i; + break; + } } - // Get the face names for the new font family - IDWriteFontFamily* fontFamily = nullptr; - std::vector fonts; + ComboBox_SetCurSel(hwndFontFaceNames, selectedFontFaceName); + OnFontFaceSelect(); + } - // Get the font variants for this family - if (currentSelection != CB_ERR) - hr = GetFonts(m_fontCollection, fontFamilyName, fonts); + // Release the held font list. + for (auto& font : fonts) { + SafeRelease(&font); + } + SafeRelease(&fontFamily); - // Initialize the face name list - std::vector fontFaceInfo; - if (SUCCEEDED(hr)) - { - ComboBox_ResetContent(hwndFontFaceNames); - GetFontFaceInfo(fonts, m_localeName, fontFaceInfo); - } - - if (SUCCEEDED(hr)) - { - for (size_t i = 0; i != fontFaceInfo.size(); ++i) - { - int fontFaceIndex = ComboBox_AddString(hwndFontFaceNames, fontFaceInfo[i].fontFaceName); - ComboBox_SetItemData(hwndFontFaceNames, fontFaceIndex, fontFaceInfo[i].PackedFontAttributes()); - } - } - - // Select the best fit font face for the current attributes - if (SUCCEEDED(hr)) - { - FontFaceInfo desiredAttributes( - L"", - m_currentTextFormat->GetFontWeight(), - m_currentTextFormat->GetFontStyle(), - m_currentTextFormat->GetFontStretch()); - - int selectedFontFaceName = 0; - ULONG bestFitAttributes = GetBestFontAttributes(m_fontCollection, fontFamilyName, desiredAttributes); - - int fontFaceCount = ComboBox_GetCount(hwndFontFaceNames); - - for (int i = 0; i != fontFaceCount; ++i) - { - if (static_cast(ComboBox_GetItemData(hwndFontFaceNames, i)) == bestFitAttributes) - { - selectedFontFaceName = i; - break; - } - } - - ComboBox_SetCurSel(hwndFontFaceNames, selectedFontFaceName); - OnFontFaceSelect(); - } - - // Release the held font list. - for (auto& font : fonts) - { - SafeRelease(&font); - } - SafeRelease(&fontFamily); - - return hr; + return hr; } @@ -335,12 +323,12 @@ HRESULT ChooseFontDialog::OnFontFamilySelect() HRESULT ChooseFontDialog::OnFontFaceSelect() { - HRESULT hr = S_OK; + HRESULT hr = S_OK; - // Signal the sample text window to redraw itself. - InvalidateRect(GetDlgItem(m_dialog, IDC_SAMPLE_BOX), nullptr, false); + // Signal the sample text window to redraw itself. + InvalidateRect(GetDlgItem(m_dialog, IDC_SAMPLE_BOX), nullptr, false); - return hr; + return hr; } @@ -354,12 +342,12 @@ HRESULT ChooseFontDialog::OnFontFaceSelect() HRESULT ChooseFontDialog::OnFontSizeSelect() { - HRESULT hr = S_OK; + HRESULT hr = S_OK; - // Signal the sample text window to redraw itself. - InvalidateRect(GetDlgItem(m_dialog, IDC_SAMPLE_BOX), nullptr, false); + // Signal the sample text window to redraw itself. + InvalidateRect(GetDlgItem(m_dialog, IDC_SAMPLE_BOX), nullptr, false); - return hr; + return hr; } @@ -378,69 +366,64 @@ HRESULT ChooseFontDialog::OnFontSizeSelect() HRESULT ChooseFontDialog::OnFontFamilyNameEdit(HWND hwndFontFamilies) { - HRESULT hr = S_OK; + HRESULT hr = S_OK; - // Save the state of the edit box selection - DWORD editSelection = ComboBox_GetEditSel(hwndFontFamilies); - int editSelectionBegin = LOWORD(editSelection); - int editSelectionEnd = HIWORD(editSelection); + // Save the state of the edit box selection + DWORD editSelection = ComboBox_GetEditSel(hwndFontFamilies); + int editSelectionBegin = LOWORD(editSelection); + int editSelectionEnd = HIWORD(editSelection); - // Get the text in the edit portion of the combo - WCHAR fontFamilyName[100]; - ComboBox_GetText(hwndFontFamilies, &fontFamilyName[0], _ARRAYSIZE(fontFamilyName)); + // Get the text in the edit portion of the combo + WCHAR fontFamilyName[100]; + ComboBox_GetText(hwndFontFamilies, &fontFamilyName[0], _ARRAYSIZE(fontFamilyName)); - // Try to find an exact match (case-insensitive) - int matchingFontFamily = ComboBox_FindStringExact(hwndFontFamilies, -1, fontFamilyName); - bool usedAltMatch = false; + // Try to find an exact match (case-insensitive) + int matchingFontFamily = ComboBox_FindStringExact(hwndFontFamilies, -1, fontFamilyName); + bool usedAltMatch = false; - if (matchingFontFamily == CB_ERR) - { - // If a match isn't found, scan all for alternate forms in the font - // collection. - IDWriteFontFamily* fontFamily = nullptr; - hr = GetFontFamily(m_fontCollection, fontFamilyName, &fontFamily); + if (matchingFontFamily == CB_ERR) { + // If a match isn't found, scan all for alternate forms in the font + // collection. + IDWriteFontFamily* fontFamily = nullptr; + hr = GetFontFamily(m_fontCollection, fontFamilyName, &fontFamily); - if (hr == S_OK) - { - // If a match is found, get the family name localized to the locale - // we're using in the combo box and match against that. - usedAltMatch = true; + if (hr == S_OK) { + // If a match is found, get the family name localized to the locale + // we're using in the combo box and match against that. + usedAltMatch = true; - std::wstring localFontFamilyName; - hr = GetFontFamilyName(fontFamily, m_localeName, localFontFamilyName); + std::wstring localFontFamilyName; + hr = GetFontFamilyName(fontFamily, m_localeName, localFontFamilyName); - if (SUCCEEDED(hr)) - { - matchingFontFamily = ComboBox_FindStringExact(hwndFontFamilies, -1, localFontFamilyName.c_str()); - } - } - else if (hr == DWRITE_E_NOFONT) - { - // Ignore DWRITE_E_NOFONT errors - hr = S_OK; - } - - SafeRelease(&fontFamily); + if (SUCCEEDED(hr)) { + matchingFontFamily = ComboBox_FindStringExact(hwndFontFamilies, -1, localFontFamilyName.c_str()); + } + } + else if (hr == DWRITE_E_NOFONT) { + // Ignore DWRITE_E_NOFONT errors + hr = S_OK; } - // Process the match, if any - if (SUCCEEDED(hr) && matchingFontFamily != CB_ERR) - { - ComboBox_SetCurSel(hwndFontFamilies, matchingFontFamily); + SafeRelease(&fontFamily); + } - // SetCurSel will update the edit text to match the text of the - // selected item. If we matched against an alternate name put that - // name back. - if (usedAltMatch) - ComboBox_SetText(hwndFontFamilies, fontFamilyName); + // Process the match, if any + if (SUCCEEDED(hr) && matchingFontFamily != CB_ERR) { + ComboBox_SetCurSel(hwndFontFamilies, matchingFontFamily); - // Reset the edit selection to what is was before SetCurSel. - ComboBox_SetEditSel(hwndFontFamilies, editSelectionBegin, editSelectionEnd); + // SetCurSel will update the edit text to match the text of the + // selected item. If we matched against an alternate name put that + // name back. + if (usedAltMatch) + ComboBox_SetText(hwndFontFamilies, fontFamilyName); - hr = OnFontFamilySelect(); - } + // Reset the edit selection to what is was before SetCurSel. + ComboBox_SetEditSel(hwndFontFamilies, editSelectionBegin, editSelectionEnd); - return hr; + hr = OnFontFamilySelect(); + } + + return hr; } @@ -455,29 +438,28 @@ HRESULT ChooseFontDialog::OnFontFamilyNameEdit(HWND hwndFontFamilies) HRESULT ChooseFontDialog::OnFontFaceNameEdit(HWND hwnd) { - HRESULT hr = S_OK; + HRESULT hr = S_OK; - // Save the state of the edit box selection - DWORD editSelection = ComboBox_GetEditSel(hwnd); - int editSelectionBegin = LOWORD(editSelection); - int editSelectionEnd = HIWORD(editSelection); + // Save the state of the edit box selection + DWORD editSelection = ComboBox_GetEditSel(hwnd); + int editSelectionBegin = LOWORD(editSelection); + int editSelectionEnd = HIWORD(editSelection); - // Try to find the currently typed text - WCHAR text[100]; - ComboBox_GetText(hwnd, &text[0], _ARRAYSIZE(text)); + // Try to find the currently typed text + WCHAR text[100]; + ComboBox_GetText(hwnd, &text[0], _ARRAYSIZE(text)); - int selectedItem = ComboBox_FindStringExact(hwnd, -1, text); - if (selectedItem != CB_ERR) - { - // If text is found, select the corresponding list item, put the - // selection state back to what it was originally, and redraw the - // sample text - ComboBox_SetCurSel(hwnd, selectedItem); - ComboBox_SetEditSel(hwnd, editSelectionBegin, editSelectionEnd); - hr = OnFontFaceSelect(); - } + int selectedItem = ComboBox_FindStringExact(hwnd, -1, text); + if (selectedItem != CB_ERR) { + // If text is found, select the corresponding list item, put the + // selection state back to what it was originally, and redraw the + // sample text + ComboBox_SetCurSel(hwnd, selectedItem); + ComboBox_SetEditSel(hwnd, editSelectionBegin, editSelectionEnd); + hr = OnFontFaceSelect(); + } - return hr; + return hr; } @@ -492,29 +474,28 @@ HRESULT ChooseFontDialog::OnFontFaceNameEdit(HWND hwnd) HRESULT ChooseFontDialog::OnFontSizeNameEdit(HWND hwnd) { - HRESULT hr = S_OK; + HRESULT hr = S_OK; - // Save the state of the edit box selection - DWORD editSelection = ComboBox_GetEditSel(hwnd); - int editSelectionBegin = LOWORD(editSelection); - int editSelectionEnd = HIWORD(editSelection); + // Save the state of the edit box selection + DWORD editSelection = ComboBox_GetEditSel(hwnd); + int editSelectionBegin = LOWORD(editSelection); + int editSelectionEnd = HIWORD(editSelection); - // Try to find the currently typed text - WCHAR text[100]; - ComboBox_GetText(hwnd, &text[0], _ARRAYSIZE(text)); + // Try to find the currently typed text + WCHAR text[100]; + ComboBox_GetText(hwnd, &text[0], _ARRAYSIZE(text)); - int selectedItem = ComboBox_FindStringExact(hwnd, -1, text); - if (selectedItem != CB_ERR) - { - // If text is found, select the corresponding list item, put the - // selection state back to what it was originally, and redraw the - // sample text - ComboBox_SetCurSel(hwnd, selectedItem); - ComboBox_SetEditSel(hwnd, editSelectionBegin, editSelectionEnd); - } - hr = OnFontSizeSelect(); + int selectedItem = ComboBox_FindStringExact(hwnd, -1, text); + if (selectedItem != CB_ERR) { + // If text is found, select the corresponding list item, put the + // selection state back to what it was originally, and redraw the + // sample text + ComboBox_SetCurSel(hwnd, selectedItem); + ComboBox_SetEditSel(hwnd, editSelectionBegin, editSelectionEnd); + } + hr = OnFontSizeSelect(); - return hr; + return hr; } @@ -526,124 +507,121 @@ HRESULT ChooseFontDialog::OnFontSizeNameEdit(HWND hwnd) HRESULT ChooseFontDialog::DrawSampleText(HDC sampleDC) { - static WCHAR sampleText[256] = { L'\0' }; - if (sampleText[0] == L'\0') { - LoadLngStringW(IDS_MUI_EXAMPLE_TEXT, sampleText, _ARRAYSIZE(sampleText)); - } + static WCHAR sampleText[256] = { L'\0' }; + if (sampleText[0] == L'\0') { + LoadLngStringW(IDS_MUI_EXAMPLE_TEXT, sampleText, _ARRAYSIZE(sampleText)); + } - HRESULT hr = S_OK; + HRESULT hr = S_OK; - HWND hwndFontFamilies = GetDlgItem(m_dialog, IDC_FONT_FAMILY_NAMES); - HWND hwndFontFaces = GetDlgItem(m_dialog, IDC_FONT_FACE_NAMES); - HWND hwndFontSizes = GetDlgItem(m_dialog, IDC_FONT_SIZE); - HWND hwndSampleBox = GetDlgItem(m_dialog, IDC_SAMPLE_BOX); + HWND hwndFontFamilies = GetDlgItem(m_dialog, IDC_FONT_FAMILY_NAMES); + HWND hwndFontFaces = GetDlgItem(m_dialog, IDC_FONT_FACE_NAMES); + HWND hwndFontSizes = GetDlgItem(m_dialog, IDC_FONT_SIZE); + HWND hwndSampleBox = GetDlgItem(m_dialog, IDC_SAMPLE_BOX); - // Get the currently selected font family. If there isn't one, then just - // don't update the text format and continue to use whatever we had before - int selectedFontFamily = ComboBox_GetCurSel(hwndFontFamilies); + // Get the currently selected font family. If there isn't one, then just + // don't update the text format and continue to use whatever we had before + int selectedFontFamily = ComboBox_GetCurSel(hwndFontFamilies); - if (selectedFontFamily != CB_ERR) - { - // Get the font family name - WCHAR fontFamilyName[100]; - GetWindowText(hwndFontFamilies, &fontFamilyName[0], _ARRAYSIZE(fontFamilyName)); + if (selectedFontFamily != CB_ERR) { + // Get the font family name + WCHAR fontFamilyName[100]; + GetWindowText(hwndFontFamilies, &fontFamilyName[0], _ARRAYSIZE(fontFamilyName)); - // Get the font face attributes - int selectedFontFace = ComboBox_GetCurSel(hwndFontFaces); - auto packedAttributes = static_cast(ComboBox_GetItemData(hwndFontFaces, selectedFontFace)); + // Get the font face attributes + int selectedFontFace = ComboBox_GetCurSel(hwndFontFaces); + auto packedAttributes = static_cast(ComboBox_GetItemData(hwndFontFaces, selectedFontFace)); - // Get the font size - WCHAR fontSizeText[100]; - GetWindowText(hwndFontSizes, &fontSizeText[0], _ARRAYSIZE(fontSizeText)); + // Get the font size + WCHAR fontSizeText[100]; + GetWindowText(hwndFontSizes, &fontSizeText[0], _ARRAYSIZE(fontSizeText)); - auto pointSize = static_cast(wcstod(fontSizeText, nullptr)); - if (pointSize <= 0.0f) { pointSize = 10.0f; } + auto pointSize = static_cast(wcstod(fontSizeText, nullptr)); + if (pointSize <= 0.0f) { pointSize = 10.0f; } - FontFaceInfo fontFaceInfo(fontFamilyName, packedAttributes); + FontFaceInfo fontFaceInfo(fontFamilyName, packedAttributes); - // Recreate current text format object - SafeRelease(&m_currentTextFormat); - hr = g_dwrite->CreateTextFormat( - fontFamilyName, - m_fontCollection, - fontFaceInfo.fontWeight, - fontFaceInfo.fontStyle, - fontFaceInfo.fontStretch, - pointSize, - m_localeName, - &m_currentTextFormat); + // Recreate current text format object + SafeRelease(&m_currentTextFormat); + hr = g_dwrite->CreateTextFormat( + fontFamilyName, + m_fontCollection, + fontFaceInfo.fontWeight, + fontFaceInfo.fontStyle, + fontFaceInfo.fontStretch, + pointSize, + m_localeName, + &m_currentTextFormat); - // Create the rendering text format object - float dipSize = (pointSize * m_currentDPI.y) / 72.0f; + // Create the rendering text format object + float dipSize = (pointSize * m_currentDPI.y) / 72.0f; - SafeRelease(&m_renderTextFormat); - hr = g_dwrite->CreateTextFormat( - fontFamilyName, - m_fontCollection, - fontFaceInfo.fontWeight, - fontFaceInfo.fontStyle, - fontFaceInfo.fontStretch, - dipSize, - m_localeName, - &m_renderTextFormat); - } - // Get the size of the sample box - RECT sampleBounds = {}; - GetClientRect(hwndSampleBox, &sampleBounds); + SafeRelease(&m_renderTextFormat); + hr = g_dwrite->CreateTextFormat( + fontFamilyName, + m_fontCollection, + fontFaceInfo.fontWeight, + fontFaceInfo.fontStyle, + fontFaceInfo.fontStretch, + dipSize, + m_localeName, + &m_renderTextFormat); + } + // Get the size of the sample box + RECT sampleBounds = {}; + GetClientRect(hwndSampleBox, &sampleBounds); - UINT width = sampleBounds.right - sampleBounds.left; - UINT height = sampleBounds.bottom - sampleBounds.top; + UINT width = sampleBounds.right - sampleBounds.left; + UINT height = sampleBounds.bottom - sampleBounds.top; - // Layout the sample text using the text format and UI bounds (converted to DIPs) - IDWriteTextLayout* textLayout = nullptr; - if (SUCCEEDED(hr)) { - hr = g_dwrite->CreateTextLayout( - sampleText, - _ARRAYSIZE(sampleText) - 1, - m_renderTextFormat, - static_cast((width * m_currentDPI.x) / GetDeviceCaps(sampleDC, LOGPIXELSX)), - static_cast((height * m_currentDPI.y) / GetDeviceCaps(sampleDC, LOGPIXELSY)), - &textLayout); - } + // Layout the sample text using the text format and UI bounds (converted to DIPs) + IDWriteTextLayout* textLayout = nullptr; + if (SUCCEEDED(hr)) { + hr = g_dwrite->CreateTextLayout( + sampleText, + _ARRAYSIZE(sampleText) - 1, + m_renderTextFormat, + static_cast((width * m_currentDPI.x) / GetDeviceCaps(sampleDC, LOGPIXELSX)), + static_cast((height * m_currentDPI.y) / GetDeviceCaps(sampleDC, LOGPIXELSY)), + &textLayout); + } - // Create a DWrite surface to render to - GdiTextRenderer* textRenderer = nullptr; - if (SUCCEEDED(hr)) - { - textRenderer = SafeAcquire(new(std::nothrow) GdiTextRenderer()); - if (textRenderer != nullptr) - hr = textRenderer->Initialize(m_dialog, sampleDC, width, height); - else - hr = E_FAIL; - } + // Create a DWrite surface to render to + GdiTextRenderer* textRenderer = nullptr; + if (SUCCEEDED(hr)) { + textRenderer = SafeAcquire(new(std::nothrow) GdiTextRenderer()); + if (textRenderer != nullptr) + hr = textRenderer->Initialize(m_dialog, sampleDC, width, height); + else + hr = E_FAIL; + } - if (SUCCEEDED(hr)) - { - // Fill the DWrite surface with the background color - HDC dwriteDC = textRenderer->GetDC(); - SetDCBrushColor(dwriteDC, GetSysColor(COLOR_BTNFACE)); - FillRect(dwriteDC, &sampleBounds, GetStockBrush(DC_BRUSH)); + if (SUCCEEDED(hr)) { + // Fill the DWrite surface with the background color + HDC dwriteDC = textRenderer->GetDC(); + SetDCBrushColor(dwriteDC, GetSysColor(COLOR_BTNFACE)); + FillRect(dwriteDC, &sampleBounds, GetStockBrush(DC_BRUSH)); - // Draw the text onto the DWrite surface - hr = textLayout->Draw(nullptr, textRenderer, 0, 0); + // Draw the text onto the DWrite surface + hr = textLayout->Draw(nullptr, textRenderer, 0, 0); - // Copy the DWrite surface to the sample on screen - BitBlt( - sampleDC, - 0, - 0, - width, - height, - dwriteDC, - 0, - 0, - SRCCOPY | NOMIRRORBITMAP); - } + // Copy the DWrite surface to the sample on screen + BitBlt( + sampleDC, + 0, + 0, + width, + height, + dwriteDC, + 0, + 0, + SRCCOPY | NOMIRRORBITMAP); + } - SafeRelease(&textRenderer); - SafeRelease(&textLayout); + SafeRelease(&textRenderer); + SafeRelease(&textLayout); - return hr; + return hr; } @@ -657,24 +635,22 @@ HRESULT ChooseFontDialog::DrawSampleText(HDC sampleDC) INT_PTR CALLBACK ChooseFontDialog::CFDialogProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { - if (message == WM_INITDIALOG) { - SetWindowLongPtr(hWnd, GWLP_USERDATA, lParam); + if (message == WM_INITDIALOG) { + SetWindowLongPtr(hWnd, GWLP_USERDATA, lParam); + } + auto this_ = reinterpret_cast(GetWindowLongPtr(hWnd, GWLP_USERDATA)); + if (this_ != nullptr) { + LPCFHOOKPROC hookFct = this_->m_chooseFontStruct->lpfnHook; + if (hookFct) { + (*hookFct)(hWnd, message, wParam, reinterpret_cast(this_->m_chooseFontStruct)); } - auto this_ = reinterpret_cast(GetWindowLongPtr(hWnd, GWLP_USERDATA)); - if (this_ != nullptr) - { - LPCFHOOKPROC hookFct = this_->m_chooseFontStruct->lpfnHook; - if (hookFct) { - (*hookFct)(hWnd, message, wParam, reinterpret_cast(this_->m_chooseFontStruct)); - } - switch (message) - { - HANDLE_MSG(hWnd, WM_INITDIALOG, this_->OnInitDialog); - HANDLE_MSG(hWnd, WM_COMMAND, this_->OnCommand); - HANDLE_MSG(hWnd, WM_DRAWITEM, this_->OnDrawItem); - } + switch (message) { + HANDLE_MSG(hWnd, WM_INITDIALOG, this_->OnInitDialog); + HANDLE_MSG(hWnd, WM_COMMAND, this_->OnCommand); + HANDLE_MSG(hWnd, WM_DRAWITEM, this_->OnDrawItem); } - return FALSE; + } + return FALSE; } @@ -689,64 +665,62 @@ INT_PTR CALLBACK ChooseFontDialog::CFDialogProc(HWND hWnd, UINT message, WPARAM BOOL ChooseFontDialog::OnInitDialog(HWND dialog, HWND hwndFocus, LPARAM lParam) { - m_dialog = dialog; + m_dialog = dialog; - HWND hwndFamilyNames = GetDlgItem(dialog, IDC_FONT_FAMILY_NAMES); - HWND hwndSizes = GetDlgItem(dialog, IDC_FONT_SIZE); + HWND hwndFamilyNames = GetDlgItem(dialog, IDC_FONT_FAMILY_NAMES); + HWND hwndSizes = GetDlgItem(dialog, IDC_FONT_SIZE); - // Fill in the font family name list. + // Fill in the font family name list. - std::vector fontFamilyNames; - if (FAILED(GetFontFamilyNames(m_fontCollection, m_localeName, fontFamilyNames))) - return FALSE; + std::vector fontFamilyNames; + if (FAILED(GetFontFamilyNames(m_fontCollection, m_localeName, fontFamilyNames))) + return FALSE; - for (size_t i = 0; i != fontFamilyNames.size(); ++i) - ComboBox_AddString(hwndFamilyNames, fontFamilyNames[i].c_str()); - - // Fill in the hardcoded font sizes + for (size_t i = 0; i != fontFamilyNames.size(); ++i) + ComboBox_AddString(hwndFamilyNames, fontFamilyNames[i].c_str()); - static const float FontSizes[] = { - 1.5, 2.5, 3.5, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, - 8, 8.5, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.5, - 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, - 26, 28, 30, 36, 48, 60, 72, 84, 96 - }; + // Fill in the hardcoded font sizes - WCHAR sizeName[100]; - sizeName[0] = '\0'; + static const float FontSizes[] = { + 1.5, 2.5, 3.5, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, + 8, 8.5, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.5, + 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, + 26, 28, 30, 36, 48, 60, 72, 84, 96 + }; - for (int i = 0; i != _ARRAYSIZE(FontSizes); ++i) - { - StringCchPrintf(sizeName, _ARRAYSIZE(sizeName), L"%.3G", FontSizes[i]); - ComboBox_AddString(hwndSizes, sizeName); - } + WCHAR sizeName[100]; + sizeName[0] = '\0'; - // Select the current size - FLOAT fCurFontSize = round(m_currentTextFormat->GetFontSize() * 10.0f) / 10.0f; - StringCchPrintf(sizeName, _ARRAYSIZE(sizeName), L"%.3G", fCurFontSize); + for (int i = 0; i != _ARRAYSIZE(FontSizes); ++i) { + StringCchPrintf(sizeName, _ARRAYSIZE(sizeName), L"%.3G", FontSizes[i]); + ComboBox_AddString(hwndSizes, sizeName); + } + // Select the current size + FLOAT fCurFontSize = round(m_currentTextFormat->GetFontSize() * 10.0f) / 10.0f; + StringCchPrintf(sizeName, _ARRAYSIZE(sizeName), L"%.3G", fCurFontSize); + + SetWindowText(hwndSizes, sizeName); + if (CB_ERR == ComboBox_SelectString(hwndSizes, -1, sizeName)) SetWindowText(hwndSizes, sizeName); - if (CB_ERR == ComboBox_SelectString(hwndSizes, -1, sizeName)) - SetWindowText(hwndSizes, sizeName); - // Select the font family specified in the input text format. + // Select the font family specified in the input text format. - int selectedFontFamily = CB_ERR; - std::wstring fontFamilyName; - - if (SUCCEEDED(GetFontFamilyNameFromFormat(m_currentTextFormat, fontFamilyName))) - { - selectedFontFamily = ComboBox_SelectString(hwndFamilyNames, -1, fontFamilyName.c_str()); - } + int selectedFontFamily = CB_ERR; + std::wstring fontFamilyName; - if (selectedFontFamily == CB_ERR) - SetWindowText(hwndFamilyNames, fontFamilyName.c_str()); + if (SUCCEEDED(GetFontFamilyNameFromFormat(m_currentTextFormat, fontFamilyName))) { + selectedFontFamily = ComboBox_SelectString(hwndFamilyNames, -1, fontFamilyName.c_str()); + } - OnFontFamilySelect(); + if (selectedFontFamily == CB_ERR) + SetWindowText(hwndFamilyNames, fontFamilyName.c_str()); - CenterDlgInParent(m_dialog); + OnFontFamilySelect(); - return TRUE; + CenterDlgInParent(m_dialog); + + return TRUE; } @@ -760,29 +734,29 @@ BOOL ChooseFontDialog::OnInitDialog(HWND dialog, HWND hwndFocus, LPARAM lParam) void ChooseFontDialog::OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify) { - if (id == IDCANCEL && codeNotify == BN_CLICKED) - EndDialog(hwnd, S_FALSE); + if (id == IDCANCEL && codeNotify == BN_CLICKED) + EndDialog(hwnd, S_FALSE); - else if (id == IDOK && codeNotify == BN_CLICKED) - EndDialog(hwnd, S_OK); + else if (id == IDOK && codeNotify == BN_CLICKED) + EndDialog(hwnd, S_OK); - else if (id == IDC_FONT_FAMILY_NAMES && codeNotify == CBN_SELCHANGE) - OnFontFamilySelect(); + else if (id == IDC_FONT_FAMILY_NAMES && codeNotify == CBN_SELCHANGE) + OnFontFamilySelect(); - else if (id == IDC_FONT_FAMILY_NAMES && codeNotify == CBN_EDITCHANGE) - OnFontFamilyNameEdit(hwndCtl); + else if (id == IDC_FONT_FAMILY_NAMES && codeNotify == CBN_EDITCHANGE) + OnFontFamilyNameEdit(hwndCtl); - else if (id == IDC_FONT_FACE_NAMES && codeNotify == CBN_SELCHANGE) - OnFontFaceSelect(); + else if (id == IDC_FONT_FACE_NAMES && codeNotify == CBN_SELCHANGE) + OnFontFaceSelect(); - else if (id == IDC_FONT_FACE_NAMES && codeNotify == CBN_EDITCHANGE) - OnFontFaceNameEdit(hwndCtl); + else if (id == IDC_FONT_FACE_NAMES && codeNotify == CBN_EDITCHANGE) + OnFontFaceNameEdit(hwndCtl); - else if (id == IDC_FONT_SIZE && codeNotify == CBN_SELCHANGE) - OnFontSizeSelect(); + else if (id == IDC_FONT_SIZE && codeNotify == CBN_SELCHANGE) + OnFontSizeSelect(); - else if (id == IDC_FONT_SIZE && codeNotify == CBN_EDITCHANGE) - OnFontSizeNameEdit(hwndCtl); + else if (id == IDC_FONT_SIZE && codeNotify == CBN_EDITCHANGE) + OnFontSizeNameEdit(hwndCtl); } @@ -796,7 +770,7 @@ void ChooseFontDialog::OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotif void ChooseFontDialog::OnDrawItem(HWND hwnd, const DRAWITEMSTRUCT* lpDrawItem) { - DrawSampleText(lpDrawItem->hDC); + DrawSampleText(lpDrawItem->hDC); } // ############################################################################ @@ -804,11 +778,10 @@ void ChooseFontDialog::OnDrawItem(HWND hwnd, const DRAWITEMSTRUCT* lpDrawItem) static void SetChosenFontFromTextFormat( - IDWriteTextFormat* textFormat, + IDWriteTextFormat* textFormat, LPCHOOSEFONT lpCF, const DPI_T dpi) { - if (textFormat != nullptr) - { + if (textFormat != nullptr) { WCHAR fontFamilyName[100]; HDC hdc = GetDC(lpCF->hwndOwner); @@ -822,7 +795,7 @@ static void SetChosenFontFromTextFormat( lpCF->iPointSize = static_cast(lround(pointSize * 10.0f)); lpCF->lpLogFont->lfWeight = static_cast(fontWeight); lpCF->lpLogFont->lfItalic = static_cast((((fontStyle == DWRITE_FONT_STYLE_ITALIC) || - (fontStyle == DWRITE_FONT_STYLE_OBLIQUE)) ? TRUE : FALSE)); + (fontStyle == DWRITE_FONT_STYLE_OBLIQUE)) ? TRUE : FALSE)); lpCF->lpLogFont->lfQuality = static_cast(CLEARTYPE_QUALITY); ReleaseDC(lpCF->hwndOwner, hdc); @@ -832,29 +805,29 @@ static void SetChosenFontFromTextFormat( extern "C" bool ChooseFontDirectWrite(HWND hwnd, const WCHAR* localeName, DPI_T dpi, LPCHOOSEFONT lpCFGDI) { - if (!lpCFGDI) { return false; } + if (!lpCFGDI) { return false; } - // The Microsoft Security Development Lifecycle recommends that all - // applications include the following call to ensure that heap corruptions - // do not go unnoticed and therefore do not introduce opportunities - // for security exploits. - HeapSetInformation(nullptr, HeapEnableTerminationOnCorruption, nullptr, 0); + // The Microsoft Security Development Lifecycle recommends that all + // applications include the following call to ensure that heap corruptions + // do not go unnoticed and therefore do not introduce opportunities + // for security exploits. + HeapSetInformation(nullptr, HeapEnableTerminationOnCorruption, nullptr, 0); - if (dpi.x == 0) { dpi.x = dpi.y = USER_DEFAULT_SCREEN_DPI; } + if (dpi.x == 0) { dpi.x = dpi.y = USER_DEFAULT_SCREEN_DPI; } - g_hInstanceNP3 = lpCFGDI->hInstance; + g_hInstanceNP3 = lpCFGDI->hInstance; - DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), (IUnknown **) &g_dwrite); + DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), (IUnknown **)&g_dwrite); - IDWriteTextFormat* textFormatOut = nullptr; - ChooseFontDialog chooseFont(hwnd, localeName, dpi, lpCFGDI); - chooseFont.GetTextFormat(&textFormatOut); + IDWriteTextFormat* textFormatOut = nullptr; + ChooseFontDialog chooseFont(hwnd, localeName, dpi, lpCFGDI); + chooseFont.GetTextFormat(&textFormatOut); - SetChosenFontFromTextFormat(textFormatOut, lpCFGDI, dpi); + SetChosenFontFromTextFormat(textFormatOut, lpCFGDI, dpi); - SafeRelease(&textFormatOut); - SafeRelease(&g_dwrite); - return true; + SafeRelease(&textFormatOut); + SafeRelease(&g_dwrite); + return true; } // ############################################################################