From c28116a04f83965452e0c08808000939cc5a3ed9 Mon Sep 17 00:00:00 2001 From: Scott Sumner <30118311+sasumner@users.noreply.github.com> Date: Wed, 21 Oct 2020 16:50:24 -0400 Subject: [PATCH] Add color samples on menu items for styling features Fix #8983, close #9089 --- PowerEditor/src/Notepad_plus.cpp | 64 +++++++++++++++++++ PowerEditor/src/Notepad_plus.h | 1 + PowerEditor/src/NppBigSwitch.cpp | 6 ++ .../WinControls/ColourPicker/WordStyleDlg.cpp | 3 + .../WinControls/ColourPicker/WordStyleDlg.h | 3 +- .../WinControls/ContextMenu/ContextMenu.cpp | 12 +++- 6 files changed, 85 insertions(+), 4 deletions(-) diff --git a/PowerEditor/src/Notepad_plus.cpp b/PowerEditor/src/Notepad_plus.cpp index a3b5b907..d172c924 100644 --- a/PowerEditor/src/Notepad_plus.cpp +++ b/PowerEditor/src/Notepad_plus.cpp @@ -411,6 +411,8 @@ LRESULT Notepad_plus::init(HWND hwnd) // Menu Section // // ------------ // + setupColorSampleBitmapsOnMainMenuItems(); + // Macro Menu std::vector & macros = nppParam.getMacroList(); HMENU hMacroMenu = ::GetSubMenu(_mainMenuHandle, MENUINDEX_MACRO); @@ -2157,6 +2159,68 @@ void Notepad_plus::checkSyncState() enableCommand(IDM_VIEW_SYNSCROLLH, canDoSync, MENU | TOOLBAR); } +void Notepad_plus::setupColorSampleBitmapsOnMainMenuItems() +{ + // set up bitmaps on menu items + // style-related bitmaps of color samples + + struct + { + int firstOfThisColorMenuId; + int styleIndic; + std::vector sameColorMenuIds; + } + bitmapOnStyleMenuItemsInfo[] = + { + { IDM_SEARCH_GONEXTMARKER5, SCE_UNIVERSAL_FOUND_STYLE_EXT5, { IDM_SEARCH_MARKALLEXT5, IDM_SEARCH_UNMARKALLEXT5, IDM_SEARCH_GOPREVMARKER5, IDM_SEARCH_STYLE5TOCLIP } }, + { IDM_SEARCH_GONEXTMARKER4, SCE_UNIVERSAL_FOUND_STYLE_EXT4, { IDM_SEARCH_MARKALLEXT4, IDM_SEARCH_UNMARKALLEXT4, IDM_SEARCH_GOPREVMARKER4, IDM_SEARCH_STYLE4TOCLIP } }, + { IDM_SEARCH_GONEXTMARKER3, SCE_UNIVERSAL_FOUND_STYLE_EXT3, { IDM_SEARCH_MARKALLEXT3, IDM_SEARCH_UNMARKALLEXT3, IDM_SEARCH_GOPREVMARKER3, IDM_SEARCH_STYLE3TOCLIP } }, + { IDM_SEARCH_GONEXTMARKER2, SCE_UNIVERSAL_FOUND_STYLE_EXT2, { IDM_SEARCH_MARKALLEXT2, IDM_SEARCH_UNMARKALLEXT2, IDM_SEARCH_GOPREVMARKER2, IDM_SEARCH_STYLE2TOCLIP } }, + { IDM_SEARCH_GONEXTMARKER1, SCE_UNIVERSAL_FOUND_STYLE_EXT1, { IDM_SEARCH_MARKALLEXT1, IDM_SEARCH_UNMARKALLEXT1, IDM_SEARCH_GOPREVMARKER1, IDM_SEARCH_STYLE1TOCLIP } }, + { IDM_SEARCH_GONEXTMARKER_DEF, SCE_UNIVERSAL_FOUND_STYLE, { IDM_SEARCH_GOPREVMARKER_DEF, IDM_SEARCH_MARKEDTOCLIP } } + }; + + for (int j = 0; j < sizeof(bitmapOnStyleMenuItemsInfo) / sizeof(bitmapOnStyleMenuItemsInfo[0]); ++j) + { + StyleArray& stylers = NppParameters::getInstance().getMiscStylerArray(); + int iFind = stylers.getStylerIndexByID(bitmapOnStyleMenuItemsInfo[j].styleIndic); + + if (iFind != -1) + { + Style const* pStyle = &(stylers.getStyler(iFind)); + + HDC hDC = GetDC(NULL); + const int bitmapXYsize = 16; + HBITMAP hNewBitmap = CreateCompatibleBitmap(hDC, bitmapXYsize, bitmapXYsize); + HDC hDCn = CreateCompatibleDC(hDC); + HBITMAP hOldBitmap = static_cast(SelectObject(hDCn, hNewBitmap)); + RECT rc = { 0, 0, bitmapXYsize, bitmapXYsize }; + + // paint full-size black square + HBRUSH hBlackBrush = CreateSolidBrush(RGB(0,0,0)); + FillRect(hDCn, &rc, hBlackBrush); + DeleteObject(hBlackBrush); + + // overpaint a slightly smaller colored square + rc.left = rc.top = 1; + rc.right = rc.bottom = bitmapXYsize - 1; + HBRUSH hColorBrush = CreateSolidBrush(pStyle->_bgColor); + FillRect(hDCn, &rc, hColorBrush); + DeleteObject(hColorBrush); + + // restore old bitmap so we can delete it to avoid leak + SelectObject(hDCn, hOldBitmap); + DeleteDC(hDCn); + + SetMenuItemBitmaps(_mainMenuHandle, bitmapOnStyleMenuItemsInfo[j].firstOfThisColorMenuId, MF_BYCOMMAND, hNewBitmap, hNewBitmap); + for (int relatedMenuId : bitmapOnStyleMenuItemsInfo[j].sameColorMenuIds) + { + SetMenuItemBitmaps(_mainMenuHandle, relatedMenuId, MF_BYCOMMAND, hNewBitmap, NULL); + } + } + } +} + bool doCheck(HMENU mainHandle, int id) { MENUITEMINFO mii; diff --git a/PowerEditor/src/Notepad_plus.h b/PowerEditor/src/Notepad_plus.h index c0c0801c..c3fad6da 100644 --- a/PowerEditor/src/Notepad_plus.h +++ b/PowerEditor/src/Notepad_plus.h @@ -469,6 +469,7 @@ private: void checkUndoState(); void checkMacroState(); void checkSyncState(); + void setupColorSampleBitmapsOnMainMenuItems(); void dropFiles(HDROP hdrop); void checkModifiedDocument(bool bCheckOnlyCurrentBuffer); diff --git a/PowerEditor/src/NppBigSwitch.cpp b/PowerEditor/src/NppBigSwitch.cpp index 363bb5af..d2d18fb1 100644 --- a/PowerEditor/src/NppBigSwitch.cpp +++ b/PowerEditor/src/NppBigSwitch.cpp @@ -1750,6 +1750,12 @@ LRESULT Notepad_plus::process(HWND hwnd, UINT message, WPARAM wParam, LPARAM lPa return TRUE; } + case WM_UPDATEMAINMENUBITMAPS: + { + setupColorSampleBitmapsOnMainMenuItems(); + return TRUE; + } + case WM_QUERYENDSESSION: case WM_CLOSE: { diff --git a/PowerEditor/src/WinControls/ColourPicker/WordStyleDlg.cpp b/PowerEditor/src/WinControls/ColourPicker/WordStyleDlg.cpp index 223e6ede..00e135f5 100644 --- a/PowerEditor/src/WinControls/ColourPicker/WordStyleDlg.cpp +++ b/PowerEditor/src/WinControls/ColourPicker/WordStyleDlg.cpp @@ -266,6 +266,7 @@ INT_PTR CALLBACK WordStyleDlg::run_dlgProc(UINT Message, WPARAM wParam, LPARAM l //(nppParamInst.getNppGUI())._themeName ::SendMessage(_hSwitch2ThemeCombo, CB_SETCURSEL, _currentThemeIndex, 0); ::SendMessage(_hParent, WM_UPDATESCINTILLAS, 0, 0); + ::SendMessage(_hParent, WM_UPDATEMAINMENUBITMAPS, 0, 0); } ::EnableWindow(::GetDlgItem(_hSelf, IDC_SAVECLOSE_BUTTON), FALSE/*!_isSync*/); display(false); @@ -293,6 +294,7 @@ INT_PTR CALLBACK WordStyleDlg::run_dlgProc(UINT Message, WPARAM wParam, LPARAM l //_isSync = true; display(false); ::SendMessage(_hParent, WM_UPDATESCINTILLAS, 0, 0); + ::SendMessage(_hParent, WM_UPDATEMAINMENUBITMAPS, 0, 0); return TRUE; } @@ -885,4 +887,5 @@ void WordStyleDlg::apply() ::EnableWindow(::GetDlgItem(_hSelf, IDOK), FALSE); ::SendMessage(_hParent, WM_UPDATESCINTILLAS, 0, 0); + ::SendMessage(_hParent, WM_UPDATEMAINMENUBITMAPS, 0, 0); } diff --git a/PowerEditor/src/WinControls/ColourPicker/WordStyleDlg.h b/PowerEditor/src/WinControls/ColourPicker/WordStyleDlg.h index da192f99..3433b009 100644 --- a/PowerEditor/src/WinControls/ColourPicker/WordStyleDlg.h +++ b/PowerEditor/src/WinControls/ColourPicker/WordStyleDlg.h @@ -33,7 +33,8 @@ #include "Parameters.h" -#define WM_UPDATESCINTILLAS (WORDSTYLE_USER + 1) //GlobalStyleDlg's msg 2 send 2 its parent +#define WM_UPDATESCINTILLAS (WORDSTYLE_USER + 1) //GlobalStyleDlg's msg 2 send 2 its parent +#define WM_UPDATEMAINMENUBITMAPS (WORDSTYLE_USER + 2) enum fontStyleType {BOLD_STATUS, ITALIC_STATUS, UNDERLINE_STATUS}; diff --git a/PowerEditor/src/WinControls/ContextMenu/ContextMenu.cpp b/PowerEditor/src/WinControls/ContextMenu/ContextMenu.cpp index 45ae6214..1232b7d8 100644 --- a/PowerEditor/src/WinControls/ContextMenu/ContextMenu.cpp +++ b/PowerEditor/src/WinControls/ContextMenu/ContextMenu.cpp @@ -61,6 +61,7 @@ void ContextMenu::create(HWND hParent, const std::vector & menuIte HMENU hParentFolder = NULL; generic_string currentParentFolderStr; int j = 0; + MENUITEMINFO mii; for (size_t i = 0, len = menuItemArray.size(); i < len; ++i) { @@ -109,7 +110,6 @@ void ContextMenu::create(HWND hParent, const std::vector & menuIte lastIsSep = true; } - if (mainMenuHandle) { bool isEnabled = (::GetMenuState(mainMenuHandle, item._cmdID, MF_BYCOMMAND)&(MF_DISABLED|MF_GRAYED)) == 0; @@ -118,8 +118,14 @@ void ContextMenu::create(HWND hParent, const std::vector & menuIte enableItem(item._cmdID, isEnabled); if (isChecked) checkItem(item._cmdID, isChecked); - } + // set up any menu item bitmaps in the context menu, using main menu bitmaps + memset(&mii, 0, sizeof(mii)); + mii.cbSize = sizeof(MENUITEMINFO); + mii.fMask = MIIM_CHECKMARKS; + mii.fType = MFT_BITMAP; + GetMenuItemInfo(mainMenuHandle, item._cmdID, FALSE, &mii); + SetMenuItemInfo(_hMenu, item._cmdID, FALSE, &mii); + } } } - \ No newline at end of file