1 | /* $Id: VBoxDnDEnumFormat.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxDnDEnumFormat.cpp - IEnumFORMATETC ("Format et cetera") implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2022 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 | #include <iprt/win/windows.h>
|
---|
19 | #include <new> /* For bad_alloc. */
|
---|
20 |
|
---|
21 | #include "VBoxTray.h"
|
---|
22 | #include "VBoxHelpers.h"
|
---|
23 | #include "VBoxDnD.h"
|
---|
24 |
|
---|
25 | #ifdef DEBUG
|
---|
26 | # define LOG_ENABLED
|
---|
27 | # define LOG_GROUP LOG_GROUP_DEFAULT
|
---|
28 | #endif
|
---|
29 | #include <VBox/log.h>
|
---|
30 |
|
---|
31 |
|
---|
32 |
|
---|
33 | VBoxDnDEnumFormatEtc::VBoxDnDEnumFormatEtc(LPFORMATETC pFormatEtc, ULONG cFormats)
|
---|
34 | : m_cRefs(1),
|
---|
35 | m_uIdxCur(0)
|
---|
36 | {
|
---|
37 | HRESULT hr;
|
---|
38 |
|
---|
39 | try
|
---|
40 | {
|
---|
41 | LogFlowFunc(("pFormatEtc=%p, cFormats=%RU32\n", pFormatEtc, cFormats));
|
---|
42 | m_paFormatEtc = 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, VBoxDnDDataObject::ClipboardFormatToString(pFormatEtc[i].cfFormat),
|
---|
48 | pFormatEtc[i].tymed, pFormatEtc[i].dwAspect));
|
---|
49 | VBoxDnDEnumFormatEtc::CopyFormat(&m_paFormatEtc[i], &pFormatEtc[i]);
|
---|
50 | }
|
---|
51 |
|
---|
52 | m_cFormats = 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 | VBoxDnDEnumFormatEtc::~VBoxDnDEnumFormatEtc(void)
|
---|
64 | {
|
---|
65 | if (m_paFormatEtc)
|
---|
66 | {
|
---|
67 | for (ULONG i = 0; i < m_cFormats; i++)
|
---|
68 | {
|
---|
69 | if(m_paFormatEtc[i].ptd)
|
---|
70 | CoTaskMemFree(m_paFormatEtc[i].ptd);
|
---|
71 | }
|
---|
72 |
|
---|
73 | delete[] m_paFormatEtc;
|
---|
74 | m_paFormatEtc = NULL;
|
---|
75 | }
|
---|
76 |
|
---|
77 | LogFlowFunc(("m_lRefCount=%RI32\n", m_cRefs));
|
---|
78 | }
|
---|
79 |
|
---|
80 | /*
|
---|
81 | * IUnknown methods.
|
---|
82 | */
|
---|
83 |
|
---|
84 | STDMETHODIMP_(ULONG) VBoxDnDEnumFormatEtc::AddRef(void)
|
---|
85 | {
|
---|
86 | return InterlockedIncrement(&m_cRefs);
|
---|
87 | }
|
---|
88 |
|
---|
89 | STDMETHODIMP_(ULONG) VBoxDnDEnumFormatEtc::Release(void)
|
---|
90 | {
|
---|
91 | LONG lCount = InterlockedDecrement(&m_cRefs);
|
---|
92 | if (lCount == 0)
|
---|
93 | {
|
---|
94 | delete this;
|
---|
95 | return 0;
|
---|
96 | }
|
---|
97 |
|
---|
98 | return lCount;
|
---|
99 | }
|
---|
100 |
|
---|
101 | STDMETHODIMP VBoxDnDEnumFormatEtc::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 |
|
---|
115 | STDMETHODIMP VBoxDnDEnumFormatEtc::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_uIdxCur < m_cFormats
|
---|
123 | && ulCopied < cFormats)
|
---|
124 | {
|
---|
125 | VBoxDnDEnumFormatEtc::CopyFormat(&pFormatEtc[ulCopied],
|
---|
126 | &m_paFormatEtc[m_uIdxCur]);
|
---|
127 | ulCopied++;
|
---|
128 | m_uIdxCur++;
|
---|
129 | }
|
---|
130 |
|
---|
131 | if (pcFetched)
|
---|
132 | *pcFetched = ulCopied;
|
---|
133 |
|
---|
134 | return (ulCopied == cFormats) ? S_OK : S_FALSE;
|
---|
135 | }
|
---|
136 |
|
---|
137 | STDMETHODIMP VBoxDnDEnumFormatEtc::Skip(ULONG cFormats)
|
---|
138 | {
|
---|
139 | m_uIdxCur += cFormats;
|
---|
140 | return (m_uIdxCur <= m_cFormats) ? S_OK : S_FALSE;
|
---|
141 | }
|
---|
142 |
|
---|
143 | STDMETHODIMP VBoxDnDEnumFormatEtc::Reset(void)
|
---|
144 | {
|
---|
145 | m_uIdxCur = 0;
|
---|
146 | return S_OK;
|
---|
147 | }
|
---|
148 |
|
---|
149 | STDMETHODIMP VBoxDnDEnumFormatEtc::Clone(IEnumFORMATETC **ppEnumFormatEtc)
|
---|
150 | {
|
---|
151 | HRESULT hResult =
|
---|
152 | CreateEnumFormatEtc(m_cFormats, m_paFormatEtc, ppEnumFormatEtc);
|
---|
153 |
|
---|
154 | if (hResult == S_OK)
|
---|
155 | ((VBoxDnDEnumFormatEtc *) *ppEnumFormatEtc)->m_uIdxCur = m_uIdxCur;
|
---|
156 |
|
---|
157 | return hResult;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /* static */
|
---|
161 | void VBoxDnDEnumFormatEtc::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 */
|
---|
176 | HRESULT VBoxDnDEnumFormatEtc::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 VBoxDnDEnumFormatEtc(pFormatEtc, nNumFormats);
|
---|
186 | hr = S_OK;
|
---|
187 | }
|
---|
188 | catch(std::bad_alloc &)
|
---|
189 | {
|
---|
190 | hr = E_OUTOFMEMORY;
|
---|
191 | }
|
---|
192 |
|
---|
193 | return hr;
|
---|
194 | }
|
---|
195 |
|
---|