/* * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or * its licensors. * * For complete copyright and license terms please see the LICENSE at the root of this * distribution (the "License"). All use of this software is governed by the License, * or, if provided, by the license below or the license accompanying this file. Do not * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * */ // UiClipboard is responsible setting and getting clipboard data for the UI elements in a platform-independent way. #include "LyShine_precompiled.h" #include "UiClipboard.h" #include #include bool UiClipboard::SetText(const AZStd::string& text) { bool success = false; if (OpenClipboard(nullptr)) { if (EmptyClipboard()) { if (text.length() > 0) { auto wstr = CryStringUtils::UTF8ToWStr(text.c_str()); const SIZE_T buffSize = (wstr.size() + 1) * sizeof(WCHAR); if (HGLOBAL hBuffer = GlobalAlloc(GMEM_MOVEABLE, buffSize)) { auto buffer = static_cast(GlobalLock(hBuffer)); memcpy_s(buffer, buffSize, wstr.data(), wstr.size() * sizeof(wchar_t)); buffer[wstr.size()] = WCHAR(0); GlobalUnlock(hBuffer); SetClipboardData(CF_UNICODETEXT, hBuffer); // Clipboard now owns the hBuffer success = true; } } } CloseClipboard(); } return success; } AZStd::string UiClipboard::GetText() { AZStd::string outText; if (OpenClipboard(nullptr)) { if (HANDLE hText = GetClipboardData(CF_UNICODETEXT)) { const WCHAR* text = static_cast(GlobalLock(hText)); outText = CryStringUtils::WStrToUTF8(text); GlobalUnlock(hText); } CloseClipboard(); } return outText; }