Improve str2Clipboard.

Make it take generic_string instead of TCHAR*, since at most callsites
we already have a generic_string.

Improve error handling. Depending on where we are in the function when
we get an error, we need to free the memory, unlock the memory, or
close the clipboard.

Note that if SetClipboardData succeeds then we should not do anything
more to the memory.
This commit is contained in:
Andreas Jönsson 2015-06-01 18:47:24 +02:00
parent d6081a5f37
commit 933aae4fc2
5 changed files with 39 additions and 25 deletions

View File

@ -779,35 +779,49 @@ double stodLocale(const generic_string& str, _locale_t loc, size_t* idx)
return ans; return ans;
} }
bool str2Clipboard(const TCHAR *str2cpy, HWND hwnd) bool str2Clipboard(const generic_string &str2cpy, HWND hwnd)
{ {
if (!str2cpy) int len2Allocate = (str2cpy.size() + 1) * sizeof(TCHAR);
return false;
int len2Allocate = lstrlen(str2cpy) + 1;
len2Allocate *= sizeof(TCHAR);
unsigned int cilpboardFormat = CF_TEXT;
cilpboardFormat = CF_UNICODETEXT;
HGLOBAL hglbCopy = ::GlobalAlloc(GMEM_MOVEABLE, len2Allocate); HGLOBAL hglbCopy = ::GlobalAlloc(GMEM_MOVEABLE, len2Allocate);
if (hglbCopy == NULL) if (hglbCopy == NULL)
{ {
return false; return false;
} }
if (!::OpenClipboard(hwnd))
if (!::OpenClipboard(hwnd)) //_pPublicInterface->getHSelf())) {
::GlobalFree(hglbCopy);
::CloseClipboard();
return false; return false;
}
::EmptyClipboard(); if (!::EmptyClipboard())
{
::GlobalFree(hglbCopy);
::CloseClipboard();
return false;
}
// Lock the handle and copy the text to the buffer. // Lock the handle and copy the text to the buffer.
TCHAR *pStr = (TCHAR *)::GlobalLock(hglbCopy); TCHAR *pStr = (TCHAR *)::GlobalLock(hglbCopy);
lstrcpy(pStr, str2cpy); if (pStr == NULL)
{
::GlobalUnlock(hglbCopy); ::GlobalUnlock(hglbCopy);
::GlobalFree(hglbCopy);
// Place the handle on the clipboard.
::SetClipboardData(cilpboardFormat, hglbCopy);
::CloseClipboard(); ::CloseClipboard();
return false;
}
_tcscpy_s(pStr, len2Allocate / sizeof(TCHAR), str2cpy.c_str());
::GlobalUnlock(hglbCopy);
// Place the handle on the clipboard.
unsigned int clipBoardFormat = CF_UNICODETEXT;
if (::SetClipboardData(clipBoardFormat, hglbCopy) == NULL)
{
::GlobalUnlock(hglbCopy);
::GlobalFree(hglbCopy);
::CloseClipboard();
return false;
}
if (!::CloseClipboard())
{
return false;
}
return true; return true;
} }

View File

@ -198,6 +198,6 @@ generic_string stringJoin(const std::vector<generic_string>& strings, const gene
generic_string stringTakeWhileAdmissable(const generic_string& input, const generic_string& admissable); generic_string stringTakeWhileAdmissable(const generic_string& input, const generic_string& admissable);
double stodLocale(const generic_string& str, _locale_t loc, size_t* idx = NULL); double stodLocale(const generic_string& str, _locale_t loc, size_t* idx = NULL);
bool str2Clipboard(const TCHAR *str2cpy, HWND hwnd); bool str2Clipboard(const generic_string &str2cpy, HWND hwnd);
#endif //M30_IDE_COMMUN_H #endif //M30_IDE_COMMUN_H

View File

@ -1933,7 +1933,7 @@ void Notepad_plus::copyMarkedLines()
globalStr = currentStr; globalStr = currentStr;
} }
} }
str2Cliboard(globalStr.c_str()); str2Cliboard(globalStr);
} }
void Notepad_plus::cutMarkedLines() void Notepad_plus::cutMarkedLines()
@ -1953,7 +1953,7 @@ void Notepad_plus::cutMarkedLines()
} }
} }
_pEditView->execute(SCI_ENDUNDOACTION); _pEditView->execute(SCI_ENDUNDOACTION);
str2Cliboard(globalStr.c_str()); str2Cliboard(globalStr);
} }
void Notepad_plus::deleteMarkedLines(bool isMarked) void Notepad_plus::deleteMarkedLines(bool isMarked)
@ -4555,7 +4555,7 @@ void Notepad_plus::getCurrentOpenedFiles(Session & session, bool includUntitledD
_invisibleEditView.execute(SCI_SETDOCPOINTER, 0, oldDoc); _invisibleEditView.execute(SCI_SETDOCPOINTER, 0, oldDoc);
} }
bool Notepad_plus::str2Cliboard(const TCHAR *str2cpy) bool Notepad_plus::str2Cliboard(const generic_string & str2cpy)
{ {
return str2Clipboard(str2cpy, _pPublicInterface->getHSelf()); return str2Clipboard(str2cpy, _pPublicInterface->getHSelf());
} }

View File

@ -599,7 +599,7 @@ private:
void doSynScorll(HWND hW); void doSynScorll(HWND hW);
void setWorkingDir(const TCHAR *dir); void setWorkingDir(const TCHAR *dir);
bool str2Cliboard(const TCHAR *str2cpy); bool str2Cliboard(const generic_string & str2cpy);
bool bin2Cliboard(const UCHAR *uchar2cpy, size_t length); bool bin2Cliboard(const UCHAR *uchar2cpy, size_t length);
bool getIntegralDockingData(tTbData & dockData, int & iCont, bool & isVisible); bool getIntegralDockingData(tTbData & dockData, int & iCont, bool & isVisible);

View File

@ -676,7 +676,7 @@ void Notepad_plus::command(int id)
{ {
generic_string dir(buf->getFullPathName()); generic_string dir(buf->getFullPathName());
PathRemoveFileSpec(dir); PathRemoveFileSpec(dir);
str2Cliboard(dir.c_str()); str2Cliboard(dir);
} }
else if (id == IDM_EDIT_FILENAMETOCLIP) else if (id == IDM_EDIT_FILENAMETOCLIP)
{ {