1 | /* $Id: ClipboardEnumFormatEtcImpl-win.cpp 78809 2019-05-28 10:54:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * ClipboardEnumFormatEtcImpl-win.cpp - Shared Clipboard IEnumFORMATETC ("Format et cetera") implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #include <new> /* For bad_alloc. */
|
---|
23 |
|
---|
24 | #define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
|
---|
25 | #include <VBox/GuestHost/SharedClipboard-win.h>
|
---|
26 |
|
---|
27 | #include <iprt/win/windows.h>
|
---|
28 |
|
---|
29 | #include <VBox/log.h>
|
---|
30 |
|
---|
31 |
|
---|
32 |
|
---|
33 | VBoxClipboardWinEnumFormatEtc::VBoxClipboardWinEnumFormatEtc(LPFORMATETC pFormatEtc, ULONG cFormats)
|
---|
34 | : m_lRefCount(1),
|
---|
35 | m_nIndex(0)
|
---|
36 | {
|
---|
37 | HRESULT hr;
|
---|
38 |
|
---|
39 | try
|
---|
40 | {
|
---|
41 | LogFlowFunc(("pFormatEtc=%p, cFormats=%RU32\n", pFormatEtc, cFormats));
|
---|
42 | m_pFormatEtc = new FORMATETC[cFormats];
|
---|
43 |
|
---|
44 | for (ULONG i = 0; i < cFormats; i++)
|
---|
45 | {
|
---|
46 | LogFlowFunc(("Format %RU32: cfFormat=%RI16, sFormat=%s, tyMed=%RU32, dwAspect=%RU32\n",
|
---|
47 | i, pFormatEtc[i].cfFormat, VBoxClipboardWinDataObject::ClipboardFormatToString(pFormatEtc[i].cfFormat),
|
---|
48 | pFormatEtc[i].tymed, pFormatEtc[i].dwAspect));
|
---|
49 | VBoxClipboardWinEnumFormatEtc::CopyFormat(&m_pFormatEtc[i], &pFormatEtc[i]);
|
---|
50 | }
|
---|
51 |
|
---|
52 | m_nNumFormats = cFormats;
|
---|
53 | hr = S_OK;
|
---|
54 | }
|
---|
55 | catch (std::bad_alloc &)
|
---|
56 | {
|
---|
57 | hr = E_OUTOFMEMORY;
|
---|
58 | }
|
---|
59 |
|
---|
60 | LogFlowFunc(("hr=%Rhrc\n", hr));
|
---|
61 | }
|
---|
62 |
|
---|
63 | VBoxClipboardWinEnumFormatEtc::~VBoxClipboardWinEnumFormatEtc(void)
|
---|
64 | {
|
---|
65 | if (m_pFormatEtc)
|
---|
66 | {
|
---|
67 | for (ULONG i = 0; i < m_nNumFormats; i++)
|
---|
68 | {
|
---|
69 | if(m_pFormatEtc[i].ptd)
|
---|
70 | CoTaskMemFree(m_pFormatEtc[i].ptd);
|
---|
71 | }
|
---|
72 |
|
---|
73 | delete[] m_pFormatEtc;
|
---|
74 | m_pFormatEtc = NULL;
|
---|
75 | }
|
---|
76 |
|
---|
77 | LogFlowFunc(("m_lRefCount=%RI32\n", m_lRefCount));
|
---|
78 | }
|
---|
79 |
|
---|
80 | /*
|
---|
81 | * IUnknown methods.
|
---|
82 | */
|
---|
83 |
|
---|
84 | STDMETHODIMP_(ULONG) VBoxClipboardWinEnumFormatEtc::AddRef(void)
|
---|
85 | {
|
---|
86 | return InterlockedIncrement(&m_lRefCount);
|
---|
87 | }
|
---|
88 |
|
---|
89 | STDMETHODIMP_(ULONG) VBoxClipboardWinEnumFormatEtc::Release(void)
|
---|
90 | {
|
---|
91 | LONG lCount = InterlockedDecrement(&m_lRefCount);
|
---|
92 | if (lCount == 0)
|
---|
93 | {
|
---|
94 | LogFlowFunc(("Delete\n"));
|
---|
95 | delete this;
|
---|
96 | return 0;
|
---|
97 | }
|
---|
98 |
|
---|
99 | return lCount;
|
---|
100 | }
|
---|
101 |
|
---|
102 | STDMETHODIMP VBoxClipboardWinEnumFormatEtc::QueryInterface(REFIID iid, void **ppvObject)
|
---|
103 | {
|
---|
104 | if ( iid == IID_IEnumFORMATETC
|
---|
105 | || iid == IID_IUnknown)
|
---|
106 | {
|
---|
107 | AddRef();
|
---|
108 | *ppvObject = this;
|
---|
109 | return S_OK;
|
---|
110 | }
|
---|
111 |
|
---|
112 | *ppvObject = 0;
|
---|
113 | return E_NOINTERFACE;
|
---|
114 | }
|
---|
115 |
|
---|
116 | STDMETHODIMP VBoxClipboardWinEnumFormatEtc::Next(ULONG cFormats, LPFORMATETC pFormatEtc, ULONG *pcFetched)
|
---|
117 | {
|
---|
118 | ULONG ulCopied = 0;
|
---|
119 |
|
---|
120 | if(cFormats == 0 || pFormatEtc == 0)
|
---|
121 | return E_INVALIDARG;
|
---|
122 |
|
---|
123 | while ( m_nIndex < m_nNumFormats
|
---|
124 | && ulCopied < cFormats)
|
---|
125 | {
|
---|
126 | VBoxClipboardWinEnumFormatEtc::CopyFormat(&pFormatEtc[ulCopied], &m_pFormatEtc[m_nIndex]);
|
---|
127 | ulCopied++;
|
---|
128 | m_nIndex++;
|
---|
129 | }
|
---|
130 |
|
---|
131 | if (pcFetched)
|
---|
132 | *pcFetched = ulCopied;
|
---|
133 |
|
---|
134 | return (ulCopied == cFormats) ? S_OK : S_FALSE;
|
---|
135 | }
|
---|
136 |
|
---|
137 | STDMETHODIMP VBoxClipboardWinEnumFormatEtc::Skip(ULONG cFormats)
|
---|
138 | {
|
---|
139 | m_nIndex += cFormats;
|
---|
140 | return (m_nIndex <= m_nNumFormats) ? S_OK : S_FALSE;
|
---|
141 | }
|
---|
142 |
|
---|
143 | STDMETHODIMP VBoxClipboardWinEnumFormatEtc::Reset(void)
|
---|
144 | {
|
---|
145 | m_nIndex = 0;
|
---|
146 | return S_OK;
|
---|
147 | }
|
---|
148 |
|
---|
149 | STDMETHODIMP VBoxClipboardWinEnumFormatEtc::Clone(IEnumFORMATETC **ppEnumFormatEtc)
|
---|
150 | {
|
---|
151 | HRESULT hResult = CreateEnumFormatEtc(m_nNumFormats, m_pFormatEtc, ppEnumFormatEtc);
|
---|
152 | if (hResult == S_OK)
|
---|
153 | ((VBoxClipboardWinEnumFormatEtc *) *ppEnumFormatEtc)->m_nIndex = m_nIndex;
|
---|
154 |
|
---|
155 | return hResult;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /* static */
|
---|
159 | void VBoxClipboardWinEnumFormatEtc::CopyFormat(LPFORMATETC pDest, LPFORMATETC pSource)
|
---|
160 | {
|
---|
161 | AssertPtrReturnVoid(pDest);
|
---|
162 | AssertPtrReturnVoid(pSource);
|
---|
163 |
|
---|
164 | *pDest = *pSource;
|
---|
165 |
|
---|
166 | if (pSource->ptd)
|
---|
167 | {
|
---|
168 | pDest->ptd = (DVTARGETDEVICE*)CoTaskMemAlloc(sizeof(DVTARGETDEVICE));
|
---|
169 | *(pDest->ptd) = *(pSource->ptd);
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | /* static */
|
---|
174 | HRESULT VBoxClipboardWinEnumFormatEtc::CreateEnumFormatEtc(UINT nNumFormats, LPFORMATETC pFormatEtc, IEnumFORMATETC **ppEnumFormatEtc)
|
---|
175 | {
|
---|
176 | AssertReturn(nNumFormats, E_INVALIDARG);
|
---|
177 | AssertPtrReturn(pFormatEtc, E_INVALIDARG);
|
---|
178 | AssertPtrReturn(ppEnumFormatEtc, E_INVALIDARG);
|
---|
179 |
|
---|
180 | HRESULT hr;
|
---|
181 | try
|
---|
182 | {
|
---|
183 | *ppEnumFormatEtc = new VBoxClipboardWinEnumFormatEtc(pFormatEtc, nNumFormats);
|
---|
184 | hr = S_OK;
|
---|
185 | }
|
---|
186 | catch(std::bad_alloc &)
|
---|
187 | {
|
---|
188 | hr = E_OUTOFMEMORY;
|
---|
189 | }
|
---|
190 |
|
---|
191 | return hr;
|
---|
192 | }
|
---|
193 |
|
---|