1 | /* $Id: ClipboardEnumFormatEtcImpl-win.cpp 100204 2023-06-19 09:11:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * ClipboardEnumFormatEtcImpl-win.cpp - Shared Clipboard IEnumFORMATETC ("Format et cetera") implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #include <new> /* For bad_alloc. */
|
---|
33 |
|
---|
34 | #define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
|
---|
35 | #include <VBox/GuestHost/SharedClipboard-win.h>
|
---|
36 |
|
---|
37 | #include <iprt/win/windows.h>
|
---|
38 |
|
---|
39 | #include <VBox/log.h>
|
---|
40 |
|
---|
41 |
|
---|
42 |
|
---|
43 | SharedClipboardWinEnumFormatEtc::SharedClipboardWinEnumFormatEtc(void)
|
---|
44 | : m_lRefCount(1),
|
---|
45 | m_nIndex(0)
|
---|
46 | {
|
---|
47 | }
|
---|
48 |
|
---|
49 | SharedClipboardWinEnumFormatEtc::~SharedClipboardWinEnumFormatEtc(void)
|
---|
50 | {
|
---|
51 | Destroy();
|
---|
52 |
|
---|
53 | LogFlowFunc(("m_lRefCount=%RI32\n", m_lRefCount));
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Initializes an IEnumFORMATETC instance.
|
---|
58 | *
|
---|
59 | * @returns VBox status code.
|
---|
60 | * @param pFormatEtc Array of formats to use for initialization.
|
---|
61 | * @param cFormats Number of formats in \a pFormatEtc.
|
---|
62 | */
|
---|
63 | int SharedClipboardWinEnumFormatEtc::Init(LPFORMATETC pFormatEtc, ULONG cFormats)
|
---|
64 | {
|
---|
65 | LogFlowFunc(("pFormatEtc=%p, cFormats=%RU32\n", pFormatEtc, cFormats));
|
---|
66 | m_pFormatEtc = new FORMATETC[cFormats];
|
---|
67 | AssertPtrReturn(m_pFormatEtc, VERR_NO_MEMORY);
|
---|
68 |
|
---|
69 | for (ULONG i = 0; i < cFormats; i++)
|
---|
70 | {
|
---|
71 | LogFlowFunc(("Format %RU32: cfFormat=%RI16, tyMed=%RU32, dwAspect=%RU32\n",
|
---|
72 | i, pFormatEtc[i].cfFormat, pFormatEtc[i].tymed, pFormatEtc[i].dwAspect));
|
---|
73 |
|
---|
74 | SharedClipboardWinDataObject::logFormat(pFormatEtc[i].cfFormat);
|
---|
75 |
|
---|
76 | SharedClipboardWinEnumFormatEtc::CopyFormat(&m_pFormatEtc[i], &pFormatEtc[i]);
|
---|
77 | }
|
---|
78 |
|
---|
79 | m_nNumFormats = cFormats;
|
---|
80 |
|
---|
81 | return VINF_SUCCESS;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Destroys an IEnumFORMATETC instance.
|
---|
86 | */
|
---|
87 | void SharedClipboardWinEnumFormatEtc::Destroy(void)
|
---|
88 | {
|
---|
89 | if (m_pFormatEtc)
|
---|
90 | {
|
---|
91 | for (ULONG i = 0; i < m_nNumFormats; i++)
|
---|
92 | {
|
---|
93 | if(m_pFormatEtc[i].ptd)
|
---|
94 | CoTaskMemFree(m_pFormatEtc[i].ptd);
|
---|
95 | }
|
---|
96 |
|
---|
97 | delete[] m_pFormatEtc;
|
---|
98 | m_pFormatEtc = NULL;
|
---|
99 | }
|
---|
100 |
|
---|
101 | m_nNumFormats = 0;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /*
|
---|
105 | * IUnknown methods.
|
---|
106 | */
|
---|
107 |
|
---|
108 | STDMETHODIMP_(ULONG) SharedClipboardWinEnumFormatEtc::AddRef(void)
|
---|
109 | {
|
---|
110 | return InterlockedIncrement(&m_lRefCount);
|
---|
111 | }
|
---|
112 |
|
---|
113 | STDMETHODIMP_(ULONG) SharedClipboardWinEnumFormatEtc::Release(void)
|
---|
114 | {
|
---|
115 | LONG lCount = InterlockedDecrement(&m_lRefCount);
|
---|
116 | if (lCount == 0)
|
---|
117 | {
|
---|
118 | LogFlowFunc(("Delete\n"));
|
---|
119 | delete this;
|
---|
120 | return 0;
|
---|
121 | }
|
---|
122 |
|
---|
123 | return lCount;
|
---|
124 | }
|
---|
125 |
|
---|
126 | STDMETHODIMP SharedClipboardWinEnumFormatEtc::QueryInterface(REFIID iid, void **ppvObject)
|
---|
127 | {
|
---|
128 | if ( iid == IID_IEnumFORMATETC
|
---|
129 | || iid == IID_IUnknown)
|
---|
130 | {
|
---|
131 | AddRef();
|
---|
132 | *ppvObject = this;
|
---|
133 | return S_OK;
|
---|
134 | }
|
---|
135 |
|
---|
136 | *ppvObject = 0;
|
---|
137 | return E_NOINTERFACE;
|
---|
138 | }
|
---|
139 |
|
---|
140 | STDMETHODIMP SharedClipboardWinEnumFormatEtc::Next(ULONG cFormats, LPFORMATETC pFormatEtc, ULONG *pcFetched)
|
---|
141 | {
|
---|
142 | ULONG ulCopied = 0;
|
---|
143 |
|
---|
144 | if(cFormats == 0 || pFormatEtc == 0)
|
---|
145 | return E_INVALIDARG;
|
---|
146 |
|
---|
147 | while ( m_nIndex < m_nNumFormats
|
---|
148 | && ulCopied < cFormats)
|
---|
149 | {
|
---|
150 | SharedClipboardWinEnumFormatEtc::CopyFormat(&pFormatEtc[ulCopied], &m_pFormatEtc[m_nIndex]);
|
---|
151 | ulCopied++;
|
---|
152 | m_nIndex++;
|
---|
153 | }
|
---|
154 |
|
---|
155 | if (pcFetched)
|
---|
156 | *pcFetched = ulCopied;
|
---|
157 |
|
---|
158 | return (ulCopied == cFormats) ? S_OK : S_FALSE;
|
---|
159 | }
|
---|
160 |
|
---|
161 | STDMETHODIMP SharedClipboardWinEnumFormatEtc::Skip(ULONG cFormats)
|
---|
162 | {
|
---|
163 | m_nIndex += cFormats;
|
---|
164 | return (m_nIndex <= m_nNumFormats) ? S_OK : S_FALSE;
|
---|
165 | }
|
---|
166 |
|
---|
167 | STDMETHODIMP SharedClipboardWinEnumFormatEtc::Reset(void)
|
---|
168 | {
|
---|
169 | m_nIndex = 0;
|
---|
170 | return S_OK;
|
---|
171 | }
|
---|
172 |
|
---|
173 | STDMETHODIMP SharedClipboardWinEnumFormatEtc::Clone(IEnumFORMATETC **ppEnumFormatEtc)
|
---|
174 | {
|
---|
175 | HRESULT hResult = CreateEnumFormatEtc(m_nNumFormats, m_pFormatEtc, ppEnumFormatEtc);
|
---|
176 | if (hResult == S_OK)
|
---|
177 | ((SharedClipboardWinEnumFormatEtc *) *ppEnumFormatEtc)->m_nIndex = m_nIndex;
|
---|
178 |
|
---|
179 | return hResult;
|
---|
180 | }
|
---|
181 |
|
---|
182 | /* static */
|
---|
183 | void SharedClipboardWinEnumFormatEtc::CopyFormat(LPFORMATETC pDest, LPFORMATETC pSource)
|
---|
184 | {
|
---|
185 | AssertPtrReturnVoid(pDest);
|
---|
186 | AssertPtrReturnVoid(pSource);
|
---|
187 |
|
---|
188 | *pDest = *pSource;
|
---|
189 |
|
---|
190 | if (pSource->ptd)
|
---|
191 | {
|
---|
192 | pDest->ptd = (DVTARGETDEVICE*)CoTaskMemAlloc(sizeof(DVTARGETDEVICE));
|
---|
193 | *(pDest->ptd) = *(pSource->ptd);
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | /* static */
|
---|
198 | HRESULT SharedClipboardWinEnumFormatEtc::CreateEnumFormatEtc(UINT nNumFormats, LPFORMATETC pFormatEtc, IEnumFORMATETC **ppEnumFormatEtc)
|
---|
199 | {
|
---|
200 | AssertReturn(nNumFormats, E_INVALIDARG);
|
---|
201 | AssertPtrReturn(pFormatEtc, E_INVALIDARG);
|
---|
202 | AssertPtrReturn(ppEnumFormatEtc, E_INVALIDARG);
|
---|
203 |
|
---|
204 | HRESULT hr;
|
---|
205 |
|
---|
206 | SharedClipboardWinEnumFormatEtc *pEnumFormatEtc = new SharedClipboardWinEnumFormatEtc();
|
---|
207 | if (pEnumFormatEtc)
|
---|
208 | {
|
---|
209 | hr = pEnumFormatEtc->Init(pFormatEtc, nNumFormats);
|
---|
210 | if (SUCCEEDED(hr))
|
---|
211 | *ppEnumFormatEtc = pEnumFormatEtc;
|
---|
212 | }
|
---|
213 | else
|
---|
214 | hr = E_OUTOFMEMORY;
|
---|
215 |
|
---|
216 | return hr;
|
---|
217 | }
|
---|
218 |
|
---|