1 | /* $Id: ClipboardEnumFormatEtcImpl-win.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * ClipboardEnumFormatEtcImpl-win.cpp - Shared Clipboard IEnumFORMATETC ("Format et cetera") implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2022 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(LPFORMATETC pFormatEtc, ULONG cFormats)
|
---|
44 | : m_lRefCount(1),
|
---|
45 | m_nIndex(0)
|
---|
46 | {
|
---|
47 | HRESULT hr;
|
---|
48 |
|
---|
49 | try
|
---|
50 | {
|
---|
51 | LogFlowFunc(("pFormatEtc=%p, cFormats=%RU32\n", pFormatEtc, cFormats));
|
---|
52 | m_pFormatEtc = new FORMATETC[cFormats];
|
---|
53 |
|
---|
54 | for (ULONG i = 0; i < cFormats; i++)
|
---|
55 | {
|
---|
56 | LogFlowFunc(("Format %RU32: cfFormat=%RI16, tyMed=%RU32, dwAspect=%RU32\n",
|
---|
57 | i, pFormatEtc[i].cfFormat, pFormatEtc[i].tymed, pFormatEtc[i].dwAspect));
|
---|
58 |
|
---|
59 | SharedClipboardWinDataObject::logFormat(pFormatEtc[i].cfFormat);
|
---|
60 |
|
---|
61 | SharedClipboardWinEnumFormatEtc::CopyFormat(&m_pFormatEtc[i], &pFormatEtc[i]);
|
---|
62 | }
|
---|
63 |
|
---|
64 | m_nNumFormats = cFormats;
|
---|
65 | hr = S_OK;
|
---|
66 | }
|
---|
67 | catch (std::bad_alloc &)
|
---|
68 | {
|
---|
69 | hr = E_OUTOFMEMORY;
|
---|
70 | }
|
---|
71 |
|
---|
72 | LogFlowFunc(("hr=%Rhrc\n", hr));
|
---|
73 | }
|
---|
74 |
|
---|
75 | SharedClipboardWinEnumFormatEtc::~SharedClipboardWinEnumFormatEtc(void)
|
---|
76 | {
|
---|
77 | if (m_pFormatEtc)
|
---|
78 | {
|
---|
79 | for (ULONG i = 0; i < m_nNumFormats; i++)
|
---|
80 | {
|
---|
81 | if(m_pFormatEtc[i].ptd)
|
---|
82 | CoTaskMemFree(m_pFormatEtc[i].ptd);
|
---|
83 | }
|
---|
84 |
|
---|
85 | delete[] m_pFormatEtc;
|
---|
86 | m_pFormatEtc = NULL;
|
---|
87 | }
|
---|
88 |
|
---|
89 | LogFlowFunc(("m_lRefCount=%RI32\n", m_lRefCount));
|
---|
90 | }
|
---|
91 |
|
---|
92 | /*
|
---|
93 | * IUnknown methods.
|
---|
94 | */
|
---|
95 |
|
---|
96 | STDMETHODIMP_(ULONG) SharedClipboardWinEnumFormatEtc::AddRef(void)
|
---|
97 | {
|
---|
98 | return InterlockedIncrement(&m_lRefCount);
|
---|
99 | }
|
---|
100 |
|
---|
101 | STDMETHODIMP_(ULONG) SharedClipboardWinEnumFormatEtc::Release(void)
|
---|
102 | {
|
---|
103 | LONG lCount = InterlockedDecrement(&m_lRefCount);
|
---|
104 | if (lCount == 0)
|
---|
105 | {
|
---|
106 | LogFlowFunc(("Delete\n"));
|
---|
107 | delete this;
|
---|
108 | return 0;
|
---|
109 | }
|
---|
110 |
|
---|
111 | return lCount;
|
---|
112 | }
|
---|
113 |
|
---|
114 | STDMETHODIMP SharedClipboardWinEnumFormatEtc::QueryInterface(REFIID iid, void **ppvObject)
|
---|
115 | {
|
---|
116 | if ( iid == IID_IEnumFORMATETC
|
---|
117 | || iid == IID_IUnknown)
|
---|
118 | {
|
---|
119 | AddRef();
|
---|
120 | *ppvObject = this;
|
---|
121 | return S_OK;
|
---|
122 | }
|
---|
123 |
|
---|
124 | *ppvObject = 0;
|
---|
125 | return E_NOINTERFACE;
|
---|
126 | }
|
---|
127 |
|
---|
128 | STDMETHODIMP SharedClipboardWinEnumFormatEtc::Next(ULONG cFormats, LPFORMATETC pFormatEtc, ULONG *pcFetched)
|
---|
129 | {
|
---|
130 | ULONG ulCopied = 0;
|
---|
131 |
|
---|
132 | if(cFormats == 0 || pFormatEtc == 0)
|
---|
133 | return E_INVALIDARG;
|
---|
134 |
|
---|
135 | while ( m_nIndex < m_nNumFormats
|
---|
136 | && ulCopied < cFormats)
|
---|
137 | {
|
---|
138 | SharedClipboardWinEnumFormatEtc::CopyFormat(&pFormatEtc[ulCopied], &m_pFormatEtc[m_nIndex]);
|
---|
139 | ulCopied++;
|
---|
140 | m_nIndex++;
|
---|
141 | }
|
---|
142 |
|
---|
143 | if (pcFetched)
|
---|
144 | *pcFetched = ulCopied;
|
---|
145 |
|
---|
146 | return (ulCopied == cFormats) ? S_OK : S_FALSE;
|
---|
147 | }
|
---|
148 |
|
---|
149 | STDMETHODIMP SharedClipboardWinEnumFormatEtc::Skip(ULONG cFormats)
|
---|
150 | {
|
---|
151 | m_nIndex += cFormats;
|
---|
152 | return (m_nIndex <= m_nNumFormats) ? S_OK : S_FALSE;
|
---|
153 | }
|
---|
154 |
|
---|
155 | STDMETHODIMP SharedClipboardWinEnumFormatEtc::Reset(void)
|
---|
156 | {
|
---|
157 | m_nIndex = 0;
|
---|
158 | return S_OK;
|
---|
159 | }
|
---|
160 |
|
---|
161 | STDMETHODIMP SharedClipboardWinEnumFormatEtc::Clone(IEnumFORMATETC **ppEnumFormatEtc)
|
---|
162 | {
|
---|
163 | HRESULT hResult = CreateEnumFormatEtc(m_nNumFormats, m_pFormatEtc, ppEnumFormatEtc);
|
---|
164 | if (hResult == S_OK)
|
---|
165 | ((SharedClipboardWinEnumFormatEtc *) *ppEnumFormatEtc)->m_nIndex = m_nIndex;
|
---|
166 |
|
---|
167 | return hResult;
|
---|
168 | }
|
---|
169 |
|
---|
170 | /* static */
|
---|
171 | void SharedClipboardWinEnumFormatEtc::CopyFormat(LPFORMATETC pDest, LPFORMATETC pSource)
|
---|
172 | {
|
---|
173 | AssertPtrReturnVoid(pDest);
|
---|
174 | AssertPtrReturnVoid(pSource);
|
---|
175 |
|
---|
176 | *pDest = *pSource;
|
---|
177 |
|
---|
178 | if (pSource->ptd)
|
---|
179 | {
|
---|
180 | pDest->ptd = (DVTARGETDEVICE*)CoTaskMemAlloc(sizeof(DVTARGETDEVICE));
|
---|
181 | *(pDest->ptd) = *(pSource->ptd);
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | /* static */
|
---|
186 | HRESULT SharedClipboardWinEnumFormatEtc::CreateEnumFormatEtc(UINT nNumFormats, LPFORMATETC pFormatEtc, IEnumFORMATETC **ppEnumFormatEtc)
|
---|
187 | {
|
---|
188 | AssertReturn(nNumFormats, E_INVALIDARG);
|
---|
189 | AssertPtrReturn(pFormatEtc, E_INVALIDARG);
|
---|
190 | AssertPtrReturn(ppEnumFormatEtc, E_INVALIDARG);
|
---|
191 |
|
---|
192 | HRESULT hr;
|
---|
193 | try
|
---|
194 | {
|
---|
195 | *ppEnumFormatEtc = new SharedClipboardWinEnumFormatEtc(pFormatEtc, nNumFormats);
|
---|
196 | hr = S_OK;
|
---|
197 | }
|
---|
198 | catch(std::bad_alloc &)
|
---|
199 | {
|
---|
200 | hr = E_OUTOFMEMORY;
|
---|
201 | }
|
---|
202 |
|
---|
203 | return hr;
|
---|
204 | }
|
---|
205 |
|
---|