/ src / modules / MeasureTool / MeasureToolCore / Clipboard.cpp
Clipboard.cpp
 1  #include "pch.h"
 2  
 3  #include "Clipboard.h"
 4  
 5  #include <sstream>
 6  
 7  void SetClipBoardToText(const std::wstring_view text)
 8  {
 9      if (!OpenClipboard(nullptr))
10      {
11          return;
12      }
13  
14      const wil::unique_hglobal handle{ GlobalAlloc(GMEM_MOVEABLE, static_cast<size_t>((text.length() + 1) * sizeof(wchar_t))) };
15      if (!handle)
16      {
17          CloseClipboard();
18          return;
19      }
20  
21      if (auto* bufPtr = static_cast<wchar_t*>(GlobalLock(handle.get())); bufPtr != nullptr)
22      {
23          text.copy(bufPtr, text.length());
24          GlobalUnlock(handle.get());
25      }
26  
27      EmptyClipboard();
28      SetClipboardData(CF_UNICODETEXT, handle.get());
29      CloseClipboard();
30  }
31  
32  void SetClipboardToMeasurements(const std::vector<Measurement>& measurements,
33                                  bool printWidth,
34                                  bool printHeight,
35                                  Measurement::Unit units)
36  {
37      if (measurements.empty())
38      {
39          return;
40      }
41  
42      std::wostringstream stream;
43      bool isFirst = true;
44  
45      for (const auto& measurement : measurements)
46      {
47          measurement.PrintToStream(stream, !isFirst, printWidth, printHeight, units);
48          isFirst = false;
49      }
50  
51      SetClipBoardToText(stream.str());
52  }