summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGeorge Hazan <ghazan@miranda.im>2022-09-26 18:23:52 +0300
committerGeorge Hazan <ghazan@miranda.im>2022-09-26 18:23:52 +0300
commit30a9e9e370aa9aff9f64158aa0942b471621a7ef (patch)
tree013f47dfcc748e7a816eb3b59a9ee852d34de0ad /src
parentcb0412427ffb4819f2026906f259f4a2dd3ef177 (diff)
Utils_ClipboardCopy - system-wide helper for writing text to clipboard
Diffstat (limited to 'src')
-rw-r--r--src/core/stdmsg/src/msgdialog.cpp11
-rw-r--r--src/core/stdpopup/src/yapp_history_dlg.cpp50
-rw-r--r--src/mir_app/src/srmm_log_rtf.cpp9
-rw-r--r--src/mir_core/src/Windows/winutil.cpp19
-rw-r--r--src/mir_core/src/mir_core.def1
-rw-r--r--src/mir_core/src/mir_core64.def1
6 files changed, 31 insertions, 60 deletions
diff --git a/src/core/stdmsg/src/msgdialog.cpp b/src/core/stdmsg/src/msgdialog.cpp
index a03d02c70b..63ed7f47fa 100644
--- a/src/core/stdmsg/src/msgdialog.cpp
+++ b/src/core/stdmsg/src/msgdialog.cpp
@@ -756,16 +756,7 @@ INT_PTR CMsgDialog::DlgProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
case IDC_USERMENU:
if (GetKeyState(VK_SHIFT) & 0x8000) { // copy user name
ptrW id(Contact::GetInfo(CNF_UNIQUEID, m_hContact, m_szProto));
- if (id != nullptr && OpenClipboard(m_hwnd)) {
- HGLOBAL hData = GlobalAlloc(GMEM_MOVEABLE, mir_wstrlen(id) * sizeof(wchar_t) + 1);
- if (hData) {
- EmptyClipboard();
- mir_wstrcpy((wchar_t *)GlobalLock(hData), id);
- GlobalUnlock(hData);
- SetClipboardData(CF_UNICODETEXT, hData);
- CloseClipboard();
- }
- }
+ Utils_ClipboardCopy(id);
}
else {
HMENU hMenu = Menu_BuildContactMenu(m_hContact);
diff --git a/src/core/stdpopup/src/yapp_history_dlg.cpp b/src/core/stdpopup/src/yapp_history_dlg.cpp
index 3751bb357a..dc6a5775ad 100644
--- a/src/core/stdpopup/src/yapp_history_dlg.cpp
+++ b/src/core/stdpopup/src/yapp_history_dlg.cpp
@@ -414,52 +414,18 @@ void RefreshPopupHistory(HWND hWnd, int renderer)
void CopyPopupDataToClipboard(HWND hList, int selection)
{
- if (!selection) {
+ if (!selection)
return;
- }
-
- if (!GetOpenClipboardWindow()) {
- if (OpenClipboard(hList)) {
- wchar_t buffer[2048];
- buffer[0] = '\0';
- wchar_t *clipboard;
- int i;
- int found = 0;
- int count = ListView_GetItemCount(hList);
- int textType;
-
- textType = CF_UNICODETEXT;
-
- for (i = 0; i < count; i++) {
- if (ListView_GetItemState(hList, i, LVIS_SELECTED)) {
- ListView_GetItemText(hList, i, selection - 100, buffer, _countof(buffer));
- found = 1;
- break;
- }
- }
- if (found) {
- EmptyClipboard();
- int len = (int)mir_wstrlen(buffer);
-
- HANDLE hData = GlobalAlloc(GMEM_MOVEABLE, (len + 2) * sizeof(wchar_t));
- clipboard = (wchar_t *)GlobalLock(hData);
- wcsncpy(clipboard, buffer, len);
- clipboard[len] = '\0';
- GlobalUnlock(hData);
- if (!SetClipboardData(textType, hData)) {
- PUShowMessage("Could not set clipboard data", SM_WARNING);
- }
- }
- CloseClipboard();
- }
- else {
- PUShowMessage("Could not open clipboard", SM_WARNING);
+ int count = ListView_GetItemCount(hList);
+ for (int i = 0; i < count; i++) {
+ if (ListView_GetItemState(hList, i, LVIS_SELECTED)) {
+ wchar_t buffer[2048]; buffer[0] = '\0';
+ ListView_GetItemText(hList, i, selection - 100, buffer, _countof(buffer));
+ Utils_ClipboardCopy(buffer);
+ break;
}
}
- else {
- PUShowMessage("The clipboard is not available", SM_WARNING);
- }
}
//subclass proc for the list view
diff --git a/src/mir_app/src/srmm_log_rtf.cpp b/src/mir_app/src/srmm_log_rtf.cpp
index b0ed13d3b7..01d8b7c5c8 100644
--- a/src/mir_app/src/srmm_log_rtf.cpp
+++ b/src/mir_app/src/srmm_log_rtf.cpp
@@ -168,14 +168,7 @@ INT_PTR CRtfLogWindow::Notify(WPARAM, LPARAM lParam)
break;
case IDM_COPYLINK:
- if (OpenClipboard(m_pDlg.m_hwnd)) {
- EmptyClipboard();
- HGLOBAL hData = GlobalAlloc(GMEM_MOVEABLE, (wszText.GetLength() + 1) * sizeof(wchar_t));
- mir_wstrcpy((wchar_t *)GlobalLock(hData), wszText);
- GlobalUnlock(hData);
- SetClipboardData(CF_UNICODETEXT, hData);
- CloseClipboard();
- }
+ Utils_ClipboardCopy(wszText);
break;
}
diff --git a/src/mir_core/src/Windows/winutil.cpp b/src/mir_core/src/Windows/winutil.cpp
index b17feca760..27dec01400 100644
--- a/src/mir_core/src/Windows/winutil.cpp
+++ b/src/mir_core/src/Windows/winutil.cpp
@@ -133,3 +133,22 @@ MIR_CORE_DLL(int) Utils_CorrectFontSize(int size)
return size * ncm.lfMessageFont.lfHeight / -12;
}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+
+MIR_CORE_DLL(void) Utils_ClipboardCopy(const wchar_t *pwszText)
+{
+ size_t cbLen = mir_wstrlen(pwszText);
+ if (!::OpenClipboard(nullptr) || !cbLen)
+ return;
+
+ ::EmptyClipboard();
+
+ HGLOBAL hData = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, (cbLen+1) * sizeof(wchar_t));
+ if (hData) {
+ mir_wstrcpy((wchar_t *)::GlobalLock(hData), pwszText);
+ ::GlobalUnlock(hData);
+ ::SetClipboardData(CF_UNICODETEXT, hData);
+ }
+ ::CloseClipboard();
+}
diff --git a/src/mir_core/src/mir_core.def b/src/mir_core/src/mir_core.def
index 619b069b3a..2a7c92d1f5 100644
--- a/src/mir_core/src/mir_core.def
+++ b/src/mir_core/src/mir_core.def
@@ -1543,3 +1543,4 @@ _Utils_CorrectFontSize@4 @1762 NONAME
?OnResize@CDlgBase@@MAEXXZ @1763 NONAME
??0MBinBuffer@@QAE@ABV0@@Z @1764 NONAME
??0MBinBuffer@@QAE@I@Z @1765 NONAME
+_Utils_ClipboardCopy@4 @1766 NONAME
diff --git a/src/mir_core/src/mir_core64.def b/src/mir_core/src/mir_core64.def
index f3b2361862..56c4c541d5 100644
--- a/src/mir_core/src/mir_core64.def
+++ b/src/mir_core/src/mir_core64.def
@@ -1543,3 +1543,4 @@ Utils_CorrectFontSize @1762 NONAME
?OnResize@CDlgBase@@MEAAXXZ @1763 NONAME
??0MBinBuffer@@QEAA@AEBV0@@Z @1764 NONAME
??0MBinBuffer@@QEAA@_K@Z @1765 NONAME
+Utils_ClipboardCopy @1766 NONAME