1 | /* $Id: VBoxDnDEnumFormat.cpp 49947 2013-12-17 08:40:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxDnDEnumFormat.cpp - IEnumFORMATETC ("Format et cetera") implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013 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 | #include <windows.h>
|
---|
18 | #include <new> /* For bad_alloc. */
|
---|
19 |
|
---|
20 | #include "VBoxTray.h"
|
---|
21 | #include "VBoxHelpers.h"
|
---|
22 | #include "VBoxDnD.h"
|
---|
23 |
|
---|
24 |
|
---|
25 | VBoxDnDEnumFormatEtc::VBoxDnDEnumFormatEtc(FORMATETC *pFormatEtc, ULONG cFormats)
|
---|
26 | : m_lRefCount(1),
|
---|
27 | m_nIndex(0)
|
---|
28 | {
|
---|
29 | HRESULT hr;
|
---|
30 |
|
---|
31 | try
|
---|
32 | {
|
---|
33 | LogFlowFunc(("pFormatEtc=%p, cFormats=%RU32\n", pFormatEtc, cFormats));
|
---|
34 | m_pFormatEtc = new FORMATETC[cFormats];
|
---|
35 |
|
---|
36 | for (ULONG i = 0; i < cFormats; i++)
|
---|
37 | {
|
---|
38 | LogFlowFunc(("Format %RU32: cfFormat=%RI16, sFormat=%s, tyMed=%RU32, dwAspect=%RU32\n",
|
---|
39 | i, pFormatEtc[i].cfFormat, VBoxDnDDataObject::ClipboardFormatToString(pFormatEtc[i].cfFormat),
|
---|
40 | pFormatEtc[i].tymed, pFormatEtc[i].dwAspect));
|
---|
41 | VBoxDnDEnumFormatEtc::CopyFormat(&m_pFormatEtc[i], &pFormatEtc[i]);
|
---|
42 | }
|
---|
43 |
|
---|
44 | m_nNumFormats = cFormats;
|
---|
45 | hr = S_OK;
|
---|
46 | }
|
---|
47 | catch (std::bad_alloc &)
|
---|
48 | {
|
---|
49 | hr = E_OUTOFMEMORY;
|
---|
50 | }
|
---|
51 |
|
---|
52 | LogFlowFunc(("hr=%Rhrc\n", hr));
|
---|
53 | }
|
---|
54 |
|
---|
55 | VBoxDnDEnumFormatEtc::~VBoxDnDEnumFormatEtc(void)
|
---|
56 | {
|
---|
57 | if (m_pFormatEtc)
|
---|
58 | {
|
---|
59 | for (ULONG i = 0; i < m_nNumFormats; i++)
|
---|
60 | {
|
---|
61 | if(m_pFormatEtc[i].ptd)
|
---|
62 | CoTaskMemFree(m_pFormatEtc[i].ptd);
|
---|
63 | }
|
---|
64 |
|
---|
65 | delete[] m_pFormatEtc;
|
---|
66 | m_pFormatEtc = NULL;
|
---|
67 | }
|
---|
68 |
|
---|
69 | LogFlowFunc(("m_lRefCount=%RI32\n", m_lRefCount));
|
---|
70 | }
|
---|
71 |
|
---|
72 | /*
|
---|
73 | * IUnknown methods.
|
---|
74 | */
|
---|
75 |
|
---|
76 | STDMETHODIMP_(ULONG) VBoxDnDEnumFormatEtc::AddRef(void)
|
---|
77 | {
|
---|
78 | return InterlockedIncrement(&m_lRefCount);
|
---|
79 | }
|
---|
80 |
|
---|
81 | STDMETHODIMP_(ULONG) VBoxDnDEnumFormatEtc::Release(void)
|
---|
82 | {
|
---|
83 | LONG lCount = InterlockedDecrement(&m_lRefCount);
|
---|
84 | if (lCount == 0)
|
---|
85 | {
|
---|
86 | delete this;
|
---|
87 | return 0;
|
---|
88 | }
|
---|
89 |
|
---|
90 | return lCount;
|
---|
91 | }
|
---|
92 |
|
---|
93 | STDMETHODIMP VBoxDnDEnumFormatEtc::QueryInterface(REFIID iid, void **ppvObject)
|
---|
94 | {
|
---|
95 | if ( iid == IID_IEnumFORMATETC
|
---|
96 | || iid == IID_IUnknown)
|
---|
97 | {
|
---|
98 | AddRef();
|
---|
99 | *ppvObject = this;
|
---|
100 | return S_OK;
|
---|
101 | }
|
---|
102 |
|
---|
103 | *ppvObject = 0;
|
---|
104 | return E_NOINTERFACE;
|
---|
105 | }
|
---|
106 |
|
---|
107 | STDMETHODIMP VBoxDnDEnumFormatEtc::Next(ULONG cFormats, FORMATETC *pFormatEtc, ULONG *pcFetched)
|
---|
108 | {
|
---|
109 | ULONG ulCopied = 0;
|
---|
110 |
|
---|
111 | if(cFormats == 0 || pFormatEtc == 0)
|
---|
112 | return E_INVALIDARG;
|
---|
113 |
|
---|
114 | while ( m_nIndex < m_nNumFormats
|
---|
115 | && ulCopied < cFormats)
|
---|
116 | {
|
---|
117 | VBoxDnDEnumFormatEtc::CopyFormat(&pFormatEtc[ulCopied],
|
---|
118 | &m_pFormatEtc[m_nIndex]);
|
---|
119 | ulCopied++;
|
---|
120 | m_nIndex++;
|
---|
121 | }
|
---|
122 |
|
---|
123 | if (pcFetched)
|
---|
124 | *pcFetched = ulCopied;
|
---|
125 |
|
---|
126 | return (ulCopied == cFormats) ? S_OK : S_FALSE;
|
---|
127 | }
|
---|
128 |
|
---|
129 | STDMETHODIMP VBoxDnDEnumFormatEtc::Skip(ULONG cFormats)
|
---|
130 | {
|
---|
131 | m_nIndex += cFormats;
|
---|
132 | return (m_nIndex <= m_nNumFormats) ? S_OK : S_FALSE;
|
---|
133 | }
|
---|
134 |
|
---|
135 | STDMETHODIMP VBoxDnDEnumFormatEtc::Reset(void)
|
---|
136 | {
|
---|
137 | m_nIndex = 0;
|
---|
138 | return S_OK;
|
---|
139 | }
|
---|
140 |
|
---|
141 | STDMETHODIMP VBoxDnDEnumFormatEtc::Clone(IEnumFORMATETC **ppEnumFormatEtc)
|
---|
142 | {
|
---|
143 | HRESULT hResult =
|
---|
144 | CreateEnumFormatEtc(m_nNumFormats, m_pFormatEtc, ppEnumFormatEtc);
|
---|
145 |
|
---|
146 | if (hResult == S_OK)
|
---|
147 | ((VBoxDnDEnumFormatEtc *) *ppEnumFormatEtc)->m_nIndex = m_nIndex;
|
---|
148 |
|
---|
149 | return hResult;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /* static */
|
---|
153 | void VBoxDnDEnumFormatEtc::CopyFormat(FORMATETC *pDest, FORMATETC *pSource)
|
---|
154 | {
|
---|
155 | AssertPtrReturnVoid(pDest);
|
---|
156 | AssertPtrReturnVoid(pSource);
|
---|
157 |
|
---|
158 | *pDest = *pSource;
|
---|
159 |
|
---|
160 | if (pSource->ptd)
|
---|
161 | {
|
---|
162 | pDest->ptd = (DVTARGETDEVICE*)CoTaskMemAlloc(sizeof(DVTARGETDEVICE));
|
---|
163 | *(pDest->ptd) = *(pSource->ptd);
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | /* static */
|
---|
168 | HRESULT VBoxDnDEnumFormatEtc::CreateEnumFormatEtc(UINT nNumFormats, FORMATETC *pFormatEtc, IEnumFORMATETC **ppEnumFormatEtc)
|
---|
169 | {
|
---|
170 | AssertReturn(nNumFormats, E_INVALIDARG);
|
---|
171 | AssertPtrReturn(pFormatEtc, E_INVALIDARG);
|
---|
172 | AssertPtrReturn(ppEnumFormatEtc, E_INVALIDARG);
|
---|
173 |
|
---|
174 | HRESULT hr;
|
---|
175 | try
|
---|
176 | {
|
---|
177 | *ppEnumFormatEtc = new VBoxDnDEnumFormatEtc(pFormatEtc, nNumFormats);
|
---|
178 | hr = S_OK;
|
---|
179 | }
|
---|
180 | catch(std::bad_alloc &)
|
---|
181 | {
|
---|
182 | hr = E_OUTOFMEMORY;
|
---|
183 | }
|
---|
184 |
|
---|
185 | return hr;
|
---|
186 | }
|
---|
187 |
|
---|