Skip to content

Commit

Permalink
Windows clipboard implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
kovacsv committed May 31, 2020
1 parent 681f56b commit 1ad9277
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 1 deletion.
94 changes: 94 additions & 0 deletions Sources/WindowsAppSupport/WAS_ClipboardHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include "WAS_ClipboardHandler.hpp"
#include "NE_Debug.hpp"
#include "NUIE_VersionInfo.hpp"

namespace WAS
{

ClipboardHandler::ClipboardHandler () :
NUIE::ClipboardHandler (),
formatId (0)
{
formatId = RegisterClipboardFormat (VSE_APP_NAME);
}

ClipboardHandler::~ClipboardHandler ()
{

}

NUIE::Version ClipboardHandler::GetCurrentVersion () const
{
return NUIE::GetCurrentVersion ();
}

bool ClipboardHandler::IsCompatibleVersion (const NUIE::Version& version) const
{
return NUIE::IsCompatibleVersion (version);
}

bool ClipboardHandler::HasClipboardContent () const
{
if (DBGERROR (!OpenClipboard (NULL))) {
return false;
}

HGLOBAL clipboardHandle = GetClipboardData (formatId);
bool hasClipboardContent = (clipboardHandle != NULL);
CloseClipboard ();

return hasClipboardContent;
}

bool ClipboardHandler::GetClipboardContent (std::vector<char>& content) const
{
if (DBGERROR (!OpenClipboard (NULL))) {
return false;
}
HGLOBAL clipboardHandle = GetClipboardData (formatId);
if (clipboardHandle == NULL) {
CloseClipboard ();
return false;
}

SIZE_T handleSize = GlobalSize (clipboardHandle);
if (handleSize == 0) {
return false;
}

char* pointer = (char*) GlobalLock (clipboardHandle);
content.assign (pointer, pointer + handleSize);
GlobalUnlock (clipboardHandle);
CloseClipboard ();

return true;
}

bool ClipboardHandler::SetClipboardContent (const std::vector<char>& content)
{
if (DBGERROR (content.empty ())) {
return false;
}

if (DBGERROR (!OpenClipboard (NULL))) {
return false;
}

EmptyClipboard ();

HGLOBAL clipboardHandle = GlobalAlloc (GMEM_MOVEABLE, content.size ());
LPVOID pointer = GlobalLock (clipboardHandle);
memcpy (pointer, &content[0], content.size ());
GlobalUnlock (clipboardHandle);

HANDLE resultData = SetClipboardData (formatId, clipboardHandle);
if (DBGERROR (resultData == NULL)) {
CloseClipboard ();
return false;
}

CloseClipboard ();
return true;
}

}
29 changes: 29 additions & 0 deletions Sources/WindowsAppSupport/WAS_ClipboardHandler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef WAS_CLIPBOARDHANDLER_HPP
#define WAS_CLIPBOARDHANDLER_HPP

#include "WAS_IncludeWindowsHeaders.hpp"
#include "NUIE_ClipboardHandler.hpp"

namespace WAS
{

class ClipboardHandler : public NUIE::ClipboardHandler
{
public:
ClipboardHandler ();
virtual ~ClipboardHandler ();

virtual NUIE::Version GetCurrentVersion () const override;
virtual bool IsCompatibleVersion (const NUIE::Version& version) const override;

virtual bool HasClipboardContent () const override;
virtual bool GetClipboardContent (std::vector<char>& content) const override;
virtual bool SetClipboardContent (const std::vector<char>& content) override;

private:
UINT formatId;
};

}

#endif
3 changes: 2 additions & 1 deletion Sources/WindowsEmbeddingDemo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "WAS_BitmapContextGdi.hpp"
#include "WAS_WindowsAppUtils.hpp"
#include "WAS_HwndEventHandler.hpp"
#include "WAS_ClipboardHandler.hpp"
#include "WAS_NodeEditorHwndControl.hpp"
#include "WAS_ParameterDialog.hpp"

Expand Down Expand Up @@ -211,7 +212,7 @@ class MyNodeUIEnvironment : public NUIE::NodeUIEnvironment
NE::BasicStringConverter stringConverter;
NUIE::BasicSkinParams skinParams;
WAS::HwndEventHandler eventHandler;
NUIE::MemoryClipboardHandler clipboardHandler;
WAS::ClipboardHandler clipboardHandler;
NE::EvaluationEnv evaluationEnv;
WAS::NodeEditorNodeTreeHwndControl nodeEditorControl;
};
Expand Down

0 comments on commit 1ad9277

Please sign in to comment.