#include <cstring>

#include <Bull/Core/Log/Log.hpp>
#include <Bull/Core/Support/Win32/Windows.hpp>

#include <Bull/Core/System/Win32/ClipboardImpl.hpp>

namespace Bull
{
    namespace prv
    {
        void ClipboardImpl::flush()
        {
            if(OpenClipboard(nullptr))
            {
                EmptyClipboard();
            }
        }

        void ClipboardImpl::setContent(const String& content)
        {
            flush();

            if(!OpenClipboard(nullptr))
            {
                Log::getInstance()->error("Failed to open clipboard");
            }

            std::size_t size = (content.getSize() + 1) * sizeof(char);
            HANDLE handler = GlobalAlloc(CF_UNICODETEXT, size);

            if(handler)
            {
                std::memcpy(GlobalLock(handler), content.getBuffer(), size);
                GlobalUnlock(handler);
                SetClipboardData(CF_UNICODETEXT, handler);
            }

            CloseClipboard();
        }

        String ClipboardImpl::getContent()
        {
            String text;
            HANDLE clipboard;

            if(!OpenClipboard(nullptr))
            {
                Log::getInstance()->error("Failed to open clipboard");

                return text;
            }

            clipboard = GetClipboardData(CF_UNICODETEXT);

            if(clipboard)
            {
                Log::getInstance()->error("Failed to open clipboard");

                CloseClipboard();

                return text;
            }

            text = String(static_cast<const char*>(GlobalLock(clipboard)));
            GlobalUnlock(clipboard);

            CloseClipboard();

            return text;
        }
    }
}

#include <cstring>

#include <Bull/Core/Exception/Throw.hpp>
#include <Bull/Core/Support/Win32/Win32Error.hpp>
#include <Bull/Core/Support/Win32/Windows.hpp>
#include <Bull/Core/System/Win32/ClipboardImpl.hpp>

namespace Bull
{
    namespace prv
    {
        void ClipboardImpl::flush()
        {
            if(OpenClipboard(nullptr))
            {
                EmptyClipboard();
            }
        }

        void ClipboardImpl::setContent(const String& content)
        {
            flush();

            if(!OpenClipboard(nullptr))
            {
                Throw(Win32Error, "ClipboardImpl::setContent", "Failed to open clipboard");
            }

            std::size_t size = (content.getSize() + 1) * sizeof(char);
            HANDLE handler = GlobalAlloc(CF_UNICODETEXT, size);

            if(handler)
            {
                std::memcpy(GlobalLock(handler), content.getBuffer(), size);
                GlobalUnlock(handler);
                SetClipboardData(CF_UNICODETEXT, handler);
            }

            CloseClipboard();
        }

        String ClipboardImpl::getContent()
        {
            String text;
            HANDLE clipboard;

            if(!OpenClipboard(nullptr))
            {
                Throw(Win32Error, "ClipboardImpl::getContent", "Failed to open clipboard");
            }

            clipboard = GetClipboardData(CF_UNICODETEXT);

            if(clipboard)
            {
                Throw(Win32Error, "ClipboardImpl::getContent", "Failed to get clipboard content");
            }

            text = String(static_cast<const char*>(GlobalLock(clipboard)));
            GlobalUnlock(clipboard);

            CloseClipboard();

            return text;
        }
    }
}

Throw Win32Error in case of failure instead of just writing the log
[Core/Clipboard] Throw Win32Error in case of failure instead of just writing the log
