From 8b7e0b76c42c0ac4e92a50d38e28ff1c851c2e9d Mon Sep 17 00:00:00 2001 From: Don Ho Date: Tue, 15 Mar 2011 01:36:55 +0000 Subject: [PATCH] [NEW_FEATURE] Add Copy/Cut/Paste Binary Content feature. git-svn-id: svn://svn.tuxfamily.org/svnroot/notepadplus/repository/trunk@741 f5eea248-9336-0410-98b8-ebc06183d4e3 --- PowerEditor/src/Notepad_plus.cpp | 3 + PowerEditor/src/Notepad_plus.rc | 4 ++ PowerEditor/src/NppCommands.cpp | 106 +++++++++++++++++++++++++++++++ PowerEditor/src/menuCmdID.h | 4 +- 4 files changed, 116 insertions(+), 1 deletion(-) diff --git a/PowerEditor/src/Notepad_plus.cpp b/PowerEditor/src/Notepad_plus.cpp index a791d34c..c84462fe 100644 --- a/PowerEditor/src/Notepad_plus.cpp +++ b/PowerEditor/src/Notepad_plus.cpp @@ -1396,7 +1396,10 @@ void Notepad_plus::checkClipboard() bool canPaste = (_pEditView->execute(SCI_CANPASTE) != 0); enableCommand(IDM_EDIT_CUT, hasSelection, MENU | TOOLBAR); enableCommand(IDM_EDIT_COPY, hasSelection, MENU | TOOLBAR); + enableCommand(IDM_EDIT_PASTE, canPaste, MENU | TOOLBAR); + //enableCommand(IDM_EDIT_PASTE, true, MENU | TOOLBAR); + enableCommand(IDM_EDIT_DELETE, hasSelection, MENU | TOOLBAR); enableCommand(IDM_EDIT_UPPERCASE, hasSelection, MENU); enableCommand(IDM_EDIT_LOWERCASE, hasSelection, MENU); diff --git a/PowerEditor/src/Notepad_plus.rc b/PowerEditor/src/Notepad_plus.rc index 19f03d9a..814f043e 100644 --- a/PowerEditor/src/Notepad_plus.rc +++ b/PowerEditor/src/Notepad_plus.rc @@ -267,6 +267,10 @@ BEGIN BEGIN MENUITEM "Paste HTML Content", IDM_EDIT_PASTE_AS_HTML MENUITEM "Paste RTF Content", IDM_EDIT_PASTE_AS_RTF + MENUITEM SEPARATOR + MENUITEM "Copy Binary Content", IDM_EDIT_COPY_BINARY + MENUITEM "Cut Binary Content", IDM_EDIT_CUT_BINARY + MENUITEM "Paste Binary Content", IDM_EDIT_PASTE_BINARY END MENUITEM SEPARATOR MENUITEM "Column Mode...", IDM_EDIT_COLUMNMODETIP diff --git a/PowerEditor/src/NppCommands.cpp b/PowerEditor/src/NppCommands.cpp index 143b5b7e..91d84bbf 100644 --- a/PowerEditor/src/NppCommands.cpp +++ b/PowerEditor/src/NppCommands.cpp @@ -23,7 +23,9 @@ #define CF_HTML TEXT("HTML Format") #define CF_RTF TEXT("Rich Text Format") +#define CF_NPPTEXTLEN TEXT("Notepad++ Binary Text Length") +static int copyDataLen = 0; void Notepad_plus::macroPlayback(Macro macro) { @@ -138,6 +140,65 @@ void Notepad_plus::command(int id) checkClipboard(); break; + case IDM_EDIT_COPY_BINARY: + case IDM_EDIT_CUT_BINARY: + { + int textLen = _pEditView->execute(SCI_GETSELTEXT, 0, 0) - 1; + if (!textLen) + return; + + char *pBinText = new char[textLen + 1]; + _pEditView->getSelectedText(pBinText, textLen + 1); + + // Open the clipboard, and empty it. + if (!OpenClipboard(NULL)) + return; + EmptyClipboard(); + + // Allocate a global memory object for the text. + HGLOBAL hglbCopy = GlobalAlloc(GMEM_MOVEABLE, (textLen + 1) * sizeof(unsigned char)); + if (hglbCopy == NULL) + { + CloseClipboard(); + return; + } + + // Lock the handle and copy the text to the buffer. + unsigned char *lpucharCopy = (unsigned char *)GlobalLock(hglbCopy); + memcpy(lpucharCopy, pBinText, textLen * sizeof(unsigned char)); + lpucharCopy[textLen] = 0; // null character + + GlobalUnlock(hglbCopy); + + // Place the handle on the clipboard. + SetClipboardData(CF_TEXT, hglbCopy); + + + // Allocate a global memory object for the text length. + HGLOBAL hglbLenCopy = GlobalAlloc(GMEM_MOVEABLE, sizeof(unsigned long)); + if (hglbLenCopy == NULL) + { + CloseClipboard(); + return; + } + + // Lock the handle and copy the text to the buffer. + unsigned long *lpLenCopy = (unsigned long *)GlobalLock(hglbLenCopy); + *lpLenCopy = textLen; + + GlobalUnlock(hglbLenCopy); + + // Place the handle on the clipboard. + UINT f = RegisterClipboardFormat(CF_NPPTEXTLEN); + SetClipboardData(f, hglbLenCopy); + + CloseClipboard(); + + if (id == IDM_EDIT_CUT_BINARY) + _pEditView->execute(SCI_REPLACESEL, 0, (LPARAM)""); + } + break; + case IDM_EDIT_PASTE: { int eolMode = int(_pEditView->execute(SCI_GETEOLMODE)); @@ -146,6 +207,51 @@ void Notepad_plus::command(int id) } break; + case IDM_EDIT_PASTE_BINARY: + { + if (!IsClipboardFormatAvailable(CF_TEXT)) + return; + + if (!OpenClipboard(NULL)) + return; + + HGLOBAL hglb = GetClipboardData(CF_TEXT); + if (hglb != NULL) + { + char *lpchar = (char *)GlobalLock(hglb); + if (lpchar != NULL) + { + UINT cf_nppTextLen = RegisterClipboardFormat(CF_NPPTEXTLEN); + if (IsClipboardFormatAvailable(cf_nppTextLen)) + { + HGLOBAL hglbLen = GetClipboardData(cf_nppTextLen); + if (hglbLen != NULL) + { + unsigned long *lpLen = (unsigned long *)GlobalLock(hglbLen); + if (lpLen != NULL) + { + _pEditView->execute(SCI_REPLACESEL, 0, (LPARAM)""); + _pEditView->execute(SCI_ADDTEXT, *lpLen, (LPARAM)lpchar); + + GlobalUnlock(hglb); + } + } + _pEditView->execute(SCI_REPLACESEL, 0, (LPARAM)""); + _pEditView->execute(SCI_ADDTEXT, copyDataLen, (LPARAM)lpchar); + } + else + { + + _pEditView->execute(SCI_REPLACESEL, 0, (LPARAM)lpchar); + } + GlobalUnlock(hglb); + } + } + CloseClipboard(); + + } + break; + case IDM_EDIT_PASTE_AS_RTF: case IDM_EDIT_PASTE_AS_HTML: { diff --git a/PowerEditor/src/menuCmdID.h b/PowerEditor/src/menuCmdID.h index cfa3b715..c23cf0ae 100644 --- a/PowerEditor/src/menuCmdID.h +++ b/PowerEditor/src/menuCmdID.h @@ -101,7 +101,9 @@ #define IDM_EDIT_COLUMNMODETIP (IDM_EDIT + 37) #define IDM_EDIT_PASTE_AS_HTML (IDM_EDIT + 38) #define IDM_EDIT_PASTE_AS_RTF (IDM_EDIT + 39) - + #define IDM_EDIT_COPY_BINARY (IDM_EDIT + 48) + #define IDM_EDIT_CUT_BINARY (IDM_EDIT + 49) + #define IDM_EDIT_PASTE_BINARY (IDM_EDIT + 50) #define IDM_EDIT_AUTOCOMPLETE (50000 + 0) #define IDM_EDIT_AUTOCOMPLETE_CURRENTFILE (50000 + 1)