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.
pull/150/head
Andreas Jönsson 10 years ago
parent d6081a5f37
commit 933aae4fc2

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

@ -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);
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

@ -1933,7 +1933,7 @@ void Notepad_plus::copyMarkedLines()
globalStr = currentStr;
}
}
str2Cliboard(globalStr.c_str());
str2Cliboard(globalStr);
}
void Notepad_plus::cutMarkedLines()
@ -1953,7 +1953,7 @@ void Notepad_plus::cutMarkedLines()
}
}
_pEditView->execute(SCI_ENDUNDOACTION);
str2Cliboard(globalStr.c_str());
str2Cliboard(globalStr);
}
void Notepad_plus::deleteMarkedLines(bool isMarked)
@ -4555,7 +4555,7 @@ void Notepad_plus::getCurrentOpenedFiles(Session & session, bool includUntitledD
_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());
}

@ -599,7 +599,7 @@ private:
void doSynScorll(HWND hW);
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 getIntegralDockingData(tTbData & dockData, int & iCont, bool & isVisible);

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

Loading…
Cancel
Save