From dc6bfedb904f19b3b6fa09b793d82f5019e7e3de Mon Sep 17 00:00:00 2001 From: Don Ho Date: Mon, 6 Nov 2023 00:40:38 +0100 Subject: [PATCH] Fix Incomplete styler.xml crashes Notepad++ issue And do some refactoring. Fix #12101 --- PowerEditor/src/Parameters.h | 3 +- .../WinControls/ColourPicker/WordStyleDlg.cpp | 98 ++++++++++++++++++ .../WinControls/ColourPicker/WordStyleDlg.h | 99 ++----------------- 3 files changed, 110 insertions(+), 90 deletions(-) diff --git a/PowerEditor/src/Parameters.h b/PowerEditor/src/Parameters.h index f30ccfa15..f727c5c7a 100644 --- a/PowerEditor/src/Parameters.h +++ b/PowerEditor/src/Parameters.h @@ -443,7 +443,8 @@ struct StyleArray void clear() { _styleVect.clear(); }; Style& getStyler(size_t index) { - assert(index < _styleVect.size()); + if (index >= _styleVect.size()) + throw std::out_of_range("Styler index out of range"); return _styleVect[index]; }; diff --git a/PowerEditor/src/WinControls/ColourPicker/WordStyleDlg.cpp b/PowerEditor/src/WinControls/ColourPicker/WordStyleDlg.cpp index 21b84e5a6..25f5d441f 100644 --- a/PowerEditor/src/WinControls/ColourPicker/WordStyleDlg.cpp +++ b/PowerEditor/src/WinControls/ColourPicker/WordStyleDlg.cpp @@ -1162,6 +1162,42 @@ void WordStyleDlg::create(int dialogID, bool isRTL, bool msgDestParent) } } +void WordStyleDlg::doDialog(bool isRTL) +{ + if (!isCreated()) + { + create(IDD_STYLER_DLG, isRTL); + prepare2Cancel(); + } + + if (!::IsWindowVisible(_hSelf)) + { + prepare2Cancel(); + } + display(); +} + +void WordStyleDlg::prepare2Cancel() +{ + _styles2restored = (NppParameters::getInstance()).getLStylerArray(); + _gstyles2restored = (NppParameters::getInstance()).getGlobalStylers(); + _gOverride2restored = (NppParameters::getInstance()).getGlobalOverrideStyle(); +} + +void WordStyleDlg::redraw(bool forceUpdate) const +{ + _pFgColour->redraw(forceUpdate); + _pBgColour->redraw(forceUpdate); + ::InvalidateRect(_hStyleInfoStaticText, NULL, TRUE); + ::UpdateWindow(_hStyleInfoStaticText); +} + +void WordStyleDlg::restoreGlobalOverrideValues() +{ + GlobalOverride& gOverride = (NppParameters::getInstance()).getGlobalOverrideStyle(); + gOverride = _gOverride2restored; +} + void WordStyleDlg::apply() { @@ -1175,3 +1211,65 @@ void WordStyleDlg::apply() ::SendMessage(_hParent, WM_UPDATESCINTILLAS, 0, 0); ::SendMessage(_hParent, WM_UPDATEMAINMENUBITMAPS, 0, 0); } + +void WordStyleDlg::addLastThemeEntry() +{ + NppParameters& nppParamInst = NppParameters::getInstance(); + ThemeSwitcher& themeSwitcher = nppParamInst.getThemeSwitcher(); + std::pair& themeInfo = themeSwitcher.getElementFromIndex(themeSwitcher.size() - 1); + ::SendMessage(_hSwitch2ThemeCombo, CB_ADDSTRING, 0, reinterpret_cast(themeInfo.first.c_str())); +} + +Style& WordStyleDlg::getCurrentStyler() +{ + int32_t styleIndex = static_cast(::SendDlgItemMessage(_hSelf, IDC_STYLES_LIST, LB_GETCURSEL, 0, 0)); + if (styleIndex == LB_ERR) + styleIndex = 0; + + try { + if (_currentLexerIndex == 0) + { + return _globalStyles.getStyler(styleIndex); + } + else + { + LexerStyler& lexerStyler = _lsArray.getLexerFromIndex(_currentLexerIndex - 1); + return lexerStyler.getStyler(styleIndex); + } + } + catch (...) + { + return _globalStyles.getStyler(0); + } +} + +void WordStyleDlg::enableFontStyle(bool isEnable) +{ + ::EnableWindow(_hCheckBold, isEnable); + ::EnableWindow(_hCheckItalic, isEnable); + ::EnableWindow(_hCheckUnderline, isEnable); +} + +long WordStyleDlg::notifyDataModified() +{ + _isDirty = true; + _isThemeDirty = true; + ::EnableWindow(::GetDlgItem(_hSelf, IDC_SAVECLOSE_BUTTON), TRUE); + return TRUE; +} + +void WordStyleDlg::showGlobalOverrideCtrls(bool show) +{ + if (show) + { + updateGlobalOverrideCtrls(); + } + ::ShowWindow(::GetDlgItem(_hSelf, IDC_GLOBAL_FG_CHECK), show ? SW_SHOW : SW_HIDE); + ::ShowWindow(::GetDlgItem(_hSelf, IDC_GLOBAL_BG_CHECK), show ? SW_SHOW : SW_HIDE); + ::ShowWindow(::GetDlgItem(_hSelf, IDC_GLOBAL_FONT_CHECK), show ? SW_SHOW : SW_HIDE); + ::ShowWindow(::GetDlgItem(_hSelf, IDC_GLOBAL_FONTSIZE_CHECK), show ? SW_SHOW : SW_HIDE); + ::ShowWindow(::GetDlgItem(_hSelf, IDC_GLOBAL_BOLD_CHECK), show ? SW_SHOW : SW_HIDE); + ::ShowWindow(::GetDlgItem(_hSelf, IDC_GLOBAL_ITALIC_CHECK), show ? SW_SHOW : SW_HIDE); + ::ShowWindow(::GetDlgItem(_hSelf, IDC_GLOBAL_UNDERLINE_CHECK), show ? SW_SHOW : SW_HIDE); + _isShownGOCtrls = show; +} diff --git a/PowerEditor/src/WinControls/ColourPicker/WordStyleDlg.h b/PowerEditor/src/WinControls/ColourPicker/WordStyleDlg.h index 5ecbed0b3..9529f0c59 100644 --- a/PowerEditor/src/WinControls/ColourPicker/WordStyleDlg.h +++ b/PowerEditor/src/WinControls/ColourPicker/WordStyleDlg.h @@ -62,50 +62,13 @@ public : WordStyleDlg() = default; void create(int dialogID, bool isRTL = false, bool msgDestParent = true) override; - - void doDialog(bool isRTL = false) { - if (!isCreated()) - { - create(IDD_STYLER_DLG, isRTL); - prepare2Cancel(); - } - - if (!::IsWindowVisible(_hSelf)) - { - prepare2Cancel(); - } - display(); - }; - - void prepare2Cancel() { - _styles2restored = (NppParameters::getInstance()).getLStylerArray(); - _gstyles2restored = (NppParameters::getInstance()).getGlobalStylers(); - _gOverride2restored = (NppParameters::getInstance()).getGlobalOverrideStyle(); - }; - - void redraw(bool forceUpdate = false) const override { - _pFgColour->redraw(forceUpdate); - _pBgColour->redraw(forceUpdate); - ::InvalidateRect(_hStyleInfoStaticText, NULL, TRUE); - ::UpdateWindow(_hStyleInfoStaticText); - }; - - void restoreGlobalOverrideValues() { - GlobalOverride & gOverride = (NppParameters::getInstance()).getGlobalOverrideStyle(); - gOverride = _gOverride2restored; - }; - + void doDialog(bool isRTL = false); + void prepare2Cancel(); + void redraw(bool forceUpdate = false) const override; + void restoreGlobalOverrideValues(); void apply(); - - void addLastThemeEntry() { - NppParameters& nppParamInst = NppParameters::getInstance(); - ThemeSwitcher & themeSwitcher = nppParamInst.getThemeSwitcher(); - std::pair & themeInfo = themeSwitcher.getElementFromIndex(themeSwitcher.size() - 1); - ::SendMessage(_hSwitch2ThemeCombo, CB_ADDSTRING, 0, reinterpret_cast(themeInfo.first.c_str())); - }; - + void addLastThemeEntry(); bool selectThemeByName(const TCHAR* themeName); - bool goToSection(const TCHAR* sectionNames); // sectionNames is formed as following: "Language name:Style name" // ex: "Global Styles:EOL custom color" will set Language on "Global Styles", then set Style on "EOL custom color" if both are found. @@ -146,25 +109,11 @@ private : bool _isThemeDirty = false; bool _isShownGOCtrls = false; + std::pair goToPreferencesSettings(); intptr_t CALLBACK run_dlgProc(UINT Message, WPARAM wParam, LPARAM lParam) override; - - Style & getCurrentStyler() { - int32_t styleIndex = static_cast(::SendDlgItemMessage(_hSelf, IDC_STYLES_LIST, LB_GETCURSEL, 0, 0)); - if (styleIndex == LB_ERR) - styleIndex = 0; - - if (_currentLexerIndex == 0) - { - return _globalStyles.getStyler(styleIndex); - } - else - { - LexerStyler & lexerStyler = _lsArray.getLexerFromIndex(_currentLexerIndex - 1); - return lexerStyler.getStyler(styleIndex); - } - }; + Style& getCurrentStyler(); bool getStyleName(TCHAR *styleName, const size_t styleNameLen); @@ -179,40 +128,12 @@ private : void updateUserKeywords(); void switchToTheme(); void updateThemeName(const generic_string& themeName); - std::pair goToPreferencesSettings(); - void loadLangListFromNppParam(); - - void enableFontStyle(bool isEnable) { - ::EnableWindow(_hCheckBold, isEnable); - ::EnableWindow(_hCheckItalic, isEnable); - ::EnableWindow(_hCheckUnderline, isEnable); - }; - long notifyDataModified() { - _isDirty = true; - _isThemeDirty = true; - ::EnableWindow(::GetDlgItem(_hSelf, IDC_SAVECLOSE_BUTTON), TRUE); - return TRUE; - }; + void enableFontStyle(bool isEnable); + long notifyDataModified(); void setStyleListFromLexer(int index); void setVisualFromStyleList(); - void updateGlobalOverrideCtrls(); - - void showGlobalOverrideCtrls(bool show) { - if (show) - { - updateGlobalOverrideCtrls(); - } - ::ShowWindow(::GetDlgItem(_hSelf, IDC_GLOBAL_FG_CHECK), show?SW_SHOW:SW_HIDE); - ::ShowWindow(::GetDlgItem(_hSelf, IDC_GLOBAL_BG_CHECK), show?SW_SHOW:SW_HIDE); - ::ShowWindow(::GetDlgItem(_hSelf, IDC_GLOBAL_FONT_CHECK), show?SW_SHOW:SW_HIDE); - ::ShowWindow(::GetDlgItem(_hSelf, IDC_GLOBAL_FONTSIZE_CHECK), show?SW_SHOW:SW_HIDE); - ::ShowWindow(::GetDlgItem(_hSelf, IDC_GLOBAL_BOLD_CHECK), show?SW_SHOW:SW_HIDE); - ::ShowWindow(::GetDlgItem(_hSelf, IDC_GLOBAL_ITALIC_CHECK), show?SW_SHOW:SW_HIDE); - ::ShowWindow(::GetDlgItem(_hSelf, IDC_GLOBAL_UNDERLINE_CHECK), show?SW_SHOW:SW_HIDE); - _isShownGOCtrls = show; - }; - + void showGlobalOverrideCtrls(bool show); void applyCurrentSelectedThemeAndUpdateUI(); };