VirtualBox

source: vbox/trunk/src/VBox/GuestHost/SharedClipboard/ClipboardEnumFormatEtcImpl-win.cpp@ 78583

Last change on this file since 78583 was 78443, checked in by vboxsync, 5 years ago

Shared Clipboard/URI: SCM fixes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.1 KB
Line 
1/* $Id: ClipboardEnumFormatEtcImpl-win.cpp 78443 2019-05-07 18:00:52Z 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
33VBoxClipboardWinEnumFormatEtc::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
63VBoxClipboardWinEnumFormatEtc::~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
84STDMETHODIMP_(ULONG) VBoxClipboardWinEnumFormatEtc::AddRef(void)
85{
86 return InterlockedIncrement(&m_lRefCount);
87}
88
89STDMETHODIMP_(ULONG) VBoxClipboardWinEnumFormatEtc::Release(void)
90{
91 LONG lCount = InterlockedDecrement(&m_lRefCount);
92 if (lCount == 0)
93 {
94 delete this;
95 return 0;
96 }
97
98 return lCount;
99}
100
101STDMETHODIMP VBoxClipboardWinEnumFormatEtc::QueryInterface(REFIID iid, void **ppvObject)
102{
103 if ( iid == IID_IEnumFORMATETC
104 || iid == IID_IUnknown)
105 {
106 AddRef();
107 *ppvObject = this;
108 return S_OK;
109 }
110
111 *ppvObject = 0;
112 return E_NOINTERFACE;
113}
114
115STDMETHODIMP VBoxClipboardWinEnumFormatEtc::Next(ULONG cFormats, LPFORMATETC pFormatEtc, ULONG *pcFetched)
116{
117 ULONG ulCopied = 0;
118
119 if(cFormats == 0 || pFormatEtc == 0)
120 return E_INVALIDARG;
121
122 while ( m_nIndex < m_nNumFormats
123 && ulCopied < cFormats)
124 {
125 VBoxClipboardWinEnumFormatEtc::CopyFormat(&pFormatEtc[ulCopied],
126 &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
137STDMETHODIMP VBoxClipboardWinEnumFormatEtc::Skip(ULONG cFormats)
138{
139 m_nIndex += cFormats;
140 return (m_nIndex <= m_nNumFormats) ? S_OK : S_FALSE;
141}
142
143STDMETHODIMP VBoxClipboardWinEnumFormatEtc::Reset(void)
144{
145 m_nIndex = 0;
146 return S_OK;
147}
148
149STDMETHODIMP VBoxClipboardWinEnumFormatEtc::Clone(IEnumFORMATETC **ppEnumFormatEtc)
150{
151 HRESULT hResult =
152 CreateEnumFormatEtc(m_nNumFormats, m_pFormatEtc, ppEnumFormatEtc);
153
154 if (hResult == S_OK)
155 ((VBoxClipboardWinEnumFormatEtc *) *ppEnumFormatEtc)->m_nIndex = m_nIndex;
156
157 return hResult;
158}
159
160/* static */
161void VBoxClipboardWinEnumFormatEtc::CopyFormat(LPFORMATETC pDest, LPFORMATETC pSource)
162{
163 AssertPtrReturnVoid(pDest);
164 AssertPtrReturnVoid(pSource);
165
166 *pDest = *pSource;
167
168 if (pSource->ptd)
169 {
170 pDest->ptd = (DVTARGETDEVICE*)CoTaskMemAlloc(sizeof(DVTARGETDEVICE));
171 *(pDest->ptd) = *(pSource->ptd);
172 }
173}
174
175/* static */
176HRESULT VBoxClipboardWinEnumFormatEtc::CreateEnumFormatEtc(UINT nNumFormats, LPFORMATETC pFormatEtc, IEnumFORMATETC **ppEnumFormatEtc)
177{
178 AssertReturn(nNumFormats, E_INVALIDARG);
179 AssertPtrReturn(pFormatEtc, E_INVALIDARG);
180 AssertPtrReturn(ppEnumFormatEtc, E_INVALIDARG);
181
182 HRESULT hr;
183 try
184 {
185 *ppEnumFormatEtc = new VBoxClipboardWinEnumFormatEtc(pFormatEtc, nNumFormats);
186 hr = S_OK;
187 }
188 catch(std::bad_alloc &)
189 {
190 hr = E_OUTOFMEMORY;
191 }
192
193 return hr;
194}
195
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use