Add context menu with "Copy link" ability

Close #2435, close #9154
This commit is contained in:
Udo Hoffmann 2020-11-14 19:52:35 +01:00 committed by Don HO
parent b7d148f493
commit d155f0326a
No known key found for this signature in database
GPG Key ID: 6C429F1D8D84F46E
9 changed files with 57 additions and 8 deletions

View File

@ -160,6 +160,7 @@ The comments are here for explanation, it's not necessary to translate them.
<Item id="42048" name="Copy Binary Content"/>
<Item id="42049" name="Cut Binary Content"/>
<Item id="42050" name="Paste Binary Content"/>
<Item id="42082" name="Copy link"/>
<Item id="42037" name="Column Mode..."/>
<Item id="42034" name="Column Editor..."/>
<Item id="42051" name="Character Panel"/>

View File

@ -153,6 +153,7 @@
<Item id="42048" name="Copy Binary Content"/>
<Item id="42049" name="Cut Binary Content"/>
<Item id="42050" name="Paste Binary Content"/>
<Item id="42082" name="Copy link"/>
<Item id="42037" name="Column Mode..."/>
<Item id="42034" name="Column Editor..."/>
<Item id="42051" name="Character Panel"/>

View File

@ -1516,7 +1516,8 @@ LRESULT Notepad_plus::process(HWND hwnd, UINT message, WPARAM wParam, LPARAM lPa
::GetCursorPos(&p);
ContextMenu scintillaContextmenu;
std::vector<MenuItemUnit>& tmp = nppParam.getContextMenuItems();
scintillaContextmenu.create(hwnd, tmp, _mainMenuHandle);
bool copyLink = (_pEditView->getSelectedTextCount() == 0) && _pEditView->getIndicatorRange(URL_INDIC);
scintillaContextmenu.create(hwnd, tmp, _mainMenuHandle, copyLink);
scintillaContextmenu.display(p);
return TRUE;
}

View File

@ -305,6 +305,19 @@ void Notepad_plus::command(int id)
checkClipboard();
break;
case IDM_EDIT_COPY_LINK:
{
int startPos = 0, endPos = 0, curPos = 0;
if (_pEditView->getIndicatorRange(URL_INDIC, &startPos, &endPos, &curPos))
{
_pEditView->execute(SCI_SETSEL, startPos, endPos);
_pEditView->execute(WM_COPY);
checkClipboard();
_pEditView->execute(SCI_SETSEL, curPos, curPos);
break;
}
}
case IDM_EDIT_COPY_BINARY:
case IDM_EDIT_CUT_BINARY:
{

View File

@ -820,11 +820,9 @@ BOOL Notepad_plus::notify(SCNotification *notification)
else
{ // Double click with no modifiers
// Check wether cursor is within URL
auto indicMsk = notifyView->execute(SCI_INDICATORALLONFOR, notification->position);
if (!(indicMsk & (1 << URL_INDIC))) break;
auto startPos = notifyView->execute(SCI_INDICATORSTART, URL_INDIC, notification->position);
auto endPos = notifyView->execute(SCI_INDICATOREND, URL_INDIC, notification->position);
if ((notification->position < startPos) || (notification->position > endPos)) break;
int startPos = 0, endPos = 0;
if (!notifyView->getIndicatorRange(URL_INDIC, &startPos, &endPos))
break;
// WM_LBUTTONUP goes to opening browser instead of Scintilla here, because the mouse is not captured.
// The missing message causes mouse cursor flicker as soon as the mouse cursor is moved to a position outside the text editing area.

View File

@ -560,6 +560,21 @@ public:
execute(SCI_INDICATORCLEARRANGE, docStart, docEnd-docStart);
};
bool getIndicatorRange(int indicatorNumber, int *from = NULL, int *to = NULL, int *cur = NULL) {
int curPos = static_cast<int>(execute(SCI_GETCURRENTPOS));
int indicMsk = static_cast<int>(execute(SCI_INDICATORALLONFOR, curPos));
if (!(indicMsk & (1 << indicatorNumber)))
return false;
int startPos = static_cast<int>(execute(SCI_INDICATORSTART, indicatorNumber, curPos));
int endPos = static_cast<int>(execute(SCI_INDICATOREND, indicatorNumber, curPos));
if ((curPos < startPos) || (curPos > endPos))
return false;
if (from) *from = startPos;
if (to) *to = endPos;
if (cur) *cur = curPos;
return true;
};
static LanguageName langNames[L_EXTERNAL+1];
void bufferUpdated(Buffer * buffer, int mask);

View File

@ -27,6 +27,9 @@
#include "ContextMenu.h"
#include "menuCmdID.h"
#include "Parameters.h"
#include "localization.h"
MenuItemUnit::MenuItemUnit(unsigned long cmdID, const TCHAR *itemName, const TCHAR *parentFolderName) : _cmdID(cmdID)
{
@ -53,7 +56,7 @@ ContextMenu::~ContextMenu()
}
void ContextMenu::create(HWND hParent, const std::vector<MenuItemUnit> & menuItemArray, const HMENU mainMenuHandle)
void ContextMenu::create(HWND hParent, const std::vector<MenuItemUnit> & menuItemArray, const HMENU mainMenuHandle, bool copyLink)
{
_hParent = hParent;
_hMenu = ::CreatePopupMenu();
@ -127,5 +130,21 @@ void ContextMenu::create(HWND hParent, const std::vector<MenuItemUnit> & menuIte
GetMenuItemInfo(mainMenuHandle, item._cmdID, FALSE, &mii);
SetMenuItemInfo(_hMenu, item._cmdID, FALSE, &mii);
}
if (copyLink && (item._cmdID == IDM_EDIT_COPY))
{
NativeLangSpeaker* nativeLangSpeaker = NppParameters::getInstance().getNativeLangSpeaker();
generic_string localized = nativeLangSpeaker->getNativeLangMenuString(IDM_EDIT_COPY_LINK);
if (localized.length() == 0)
localized = L"Copy link";
memset(&mii, 0, sizeof(mii));
mii.cbSize = sizeof(MENUITEMINFO);
mii.fMask = MIIM_ID | MIIM_STRING | MIIM_STATE;
mii.wID = IDM_EDIT_COPY_LINK;
mii.dwTypeData = (TCHAR*) localized.c_str();
mii.fState = MFS_ENABLED;
int c = GetMenuItemCount(_hMenu);
SetMenuItemInfo(_hMenu, c - 1, TRUE, & mii);
}
}
}

View File

@ -47,7 +47,7 @@ class ContextMenu final
public:
~ContextMenu();
void create(HWND hParent, const std::vector<MenuItemUnit> & menuItemArray, const HMENU mainMenuHandle = NULL);
void create(HWND hParent, const std::vector<MenuItemUnit> & menuItemArray, const HMENU mainMenuHandle = NULL, bool copyLink = false);
bool isCreated() const {return _hMenu != NULL;}
void display(const POINT & p) const {

View File

@ -174,6 +174,7 @@
#define IDM_EDIT_REMOVE_ANY_DUP_LINES (IDM_EDIT + 79)
#define IDM_EDIT_SORTLINES_LEXICO_CASE_INSENS_ASCENDING (IDM_EDIT + 80)
#define IDM_EDIT_SORTLINES_LEXICO_CASE_INSENS_DESCENDING (IDM_EDIT + 81)
#define IDM_EDIT_COPY_LINK (IDM_EDIT + 82)
#define IDM_EDIT_AUTOCOMPLETE (50000 + 0)
#define IDM_EDIT_AUTOCOMPLETE_CURRENTFILE (50000 + 1)