Qt 4.8
qole_win.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qdnd_p.h"
43 
44 #if !(defined(QT_NO_DRAGANDDROP) && defined(QT_NO_CLIPBOARD))
45 
46 #if defined(Q_OS_WINCE)
47 #include <shlobj.h>
48 #include "qguifunctions_wince.h"
49 #endif
50 
52 
54 {
55  m_isNull = false;
56  m_dwRefs = 1;
57  m_nIndex = 0;
58 
59  for (int idx = 0; idx < fmtetcs.count(); ++idx) {
60  LPFORMATETC destetc = new FORMATETC();
61  if (copyFormatEtc(destetc, (LPFORMATETC)&(fmtetcs.at(idx)))) {
62  m_lpfmtetcs.append(destetc);
63  } else {
64  m_isNull = true;
65  delete destetc;
66  break;
67  }
68  }
69 }
70 
72 {
73  m_isNull = false;
74  m_dwRefs = 1;
75  m_nIndex = 0;
76 
77  for (int idx = 0; idx < lpfmtetcs.count(); ++idx) {
78  LPFORMATETC srcetc = lpfmtetcs.at(idx);
79  LPFORMATETC destetc = new FORMATETC();
80  if (copyFormatEtc(destetc, srcetc)) {
81  m_lpfmtetcs.append(destetc);
82  } else {
83  m_isNull = true;
84  delete destetc;
85  break;
86  }
87  }
88 }
89 
91 {
92  LPMALLOC pmalloc;
93 
94 #if !defined(Q_OS_WINCE)
95  if (CoGetMalloc(MEMCTX_TASK, &pmalloc) == NOERROR) {
96 #else
97  if (SHGetMalloc(&pmalloc) == NOERROR) {
98 #endif
99  for (int idx = 0; idx < m_lpfmtetcs.count(); ++idx) {
100  LPFORMATETC tmpetc = m_lpfmtetcs.at(idx);
101  if (tmpetc->ptd)
102  pmalloc->Free(tmpetc->ptd);
103  delete tmpetc;
104  }
105 
106  pmalloc->Release();
107  }
108  m_lpfmtetcs.clear();
109 }
110 
112 {
113  return m_isNull;
114 }
115 
116 // IUnknown methods
117 STDMETHODIMP
118 QOleEnumFmtEtc::QueryInterface(REFIID riid, void FAR* FAR* ppvObj)
119 {
120  if (riid == IID_IUnknown || riid == IID_IEnumFORMATETC) {
121  *ppvObj = this;
122  AddRef();
123  return NOERROR;
124  }
125  *ppvObj = NULL;
126  return ResultFromScode(E_NOINTERFACE);
127 }
128 
129 STDMETHODIMP_(ULONG)
130 QOleEnumFmtEtc::AddRef(void)
131 {
132  return ++m_dwRefs;
133 }
134 
135 STDMETHODIMP_(ULONG)
137 {
138  if (--m_dwRefs == 0) {
139  delete this;
140  return 0;
141  }
142  return m_dwRefs;
143 }
144 
145 // IEnumFORMATETC methods
146 STDMETHODIMP
147 QOleEnumFmtEtc::Next(ULONG celt, LPFORMATETC rgelt, ULONG FAR* pceltFetched)
148 {
149  ULONG i=0;
150  ULONG nOffset;
151 
152  if (rgelt == NULL)
153  return ResultFromScode(E_INVALIDARG);
154 
155  while (i < celt) {
156  nOffset = m_nIndex + i;
157 
158  if (nOffset < ULONG(m_lpfmtetcs.count())) {
159  copyFormatEtc((LPFORMATETC)&(rgelt[i]), m_lpfmtetcs.at(nOffset));
160  i++;
161  } else {
162  break;
163  }
164  }
165 
166  m_nIndex += (WORD)i;
167 
168  if (pceltFetched != NULL)
169  *pceltFetched = i;
170 
171  if (i != celt)
172  return ResultFromScode(S_FALSE);
173 
174  return NOERROR;
175 }
176 
177 STDMETHODIMP
179 {
180  ULONG i=0;
181  ULONG nOffset;
182 
183  while (i < celt) {
184  nOffset = m_nIndex + i;
185 
186  if (nOffset < ULONG(m_lpfmtetcs.count())) {
187  i++;
188  } else {
189  break;
190  }
191  }
192 
193  m_nIndex += (WORD)i;
194 
195  if (i != celt)
196  return ResultFromScode(S_FALSE);
197 
198  return NOERROR;
199 }
200 
201 STDMETHODIMP
203 {
204  m_nIndex = 0;
205  return NOERROR;
206 }
207 
208 STDMETHODIMP
209 QOleEnumFmtEtc::Clone(LPENUMFORMATETC FAR* newEnum)
210 {
211  if (newEnum == NULL)
212  return ResultFromScode(E_INVALIDARG);
213 
215  result->m_nIndex = m_nIndex;
216 
217  if (result->isNull()) {
218  delete result;
219  return ResultFromScode(E_OUTOFMEMORY);
220  } else {
221  *newEnum = result;
222  }
223 
224  return NOERROR;
225 }
226 
227 bool QOleEnumFmtEtc::copyFormatEtc(LPFORMATETC dest, LPFORMATETC src) const
228 {
229  if (dest == NULL || src == NULL)
230  return false;
231 
232  *dest = *src;
233 
234  if (src->ptd) {
235  LPMALLOC pmalloc;
236 
237 #if !defined(Q_OS_WINCE)
238  if (CoGetMalloc(MEMCTX_TASK, &pmalloc) != NOERROR)
239 #else
240  if (SHGetMalloc(&pmalloc) != NOERROR)
241 #endif
242  return false;
243 
244  pmalloc->Alloc(src->ptd->tdSize);
245  memcpy(dest->ptd, src->ptd, size_t(src->ptd->tdSize));
246 
247  pmalloc->Release();
248  }
249 
250  return true;
251 }
252 
254 #endif // QT_NO_DRAGANDDROP && QT_NO_CLIPBOARD
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
STDMETHOD() Next(ULONG celt, LPFORMATETC rgelt, ULONG FAR *pceltFetched)
Definition: qole_win.cpp:147
ULONG m_nIndex
Definition: qdnd_p.h:163
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
QOleEnumFmtEtc(const QVector< FORMATETC > &fmtetcs)
Definition: qole_win.cpp:53
bool isNull() const
Definition: qole_win.cpp:111
bool copyFormatEtc(LPFORMATETC dest, LPFORMATETC src) const
Definition: qole_win.cpp:227
bool m_isNull
Definition: qdnd_p.h:165
STDMETHOD() Skip(ULONG celt)
Definition: qole_win.cpp:178
struct tagFORMATETC FORMATETC
Definition: qmime.h:66
QVector< LPFORMATETC > m_lpfmtetcs
Definition: qdnd_p.h:164
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
STDMETHOD() Clone(LPENUMFORMATETC FAR *newEnum)
Definition: qole_win.cpp:209
STDMETHOD() QueryInterface(REFIID riid, void FAR *FAR *ppvObj)
Definition: qole_win.cpp:118
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void clear()
Removes all the elements from the vector and releases the memory used by the vector.
Definition: qvector.h:347
void append(const T &t)
Inserts value at the end of the vector.
Definition: qvector.h:573
STDMETHOD() Reset(void)
Definition: qole_win.cpp:202
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
virtual ~QOleEnumFmtEtc()
Definition: qole_win.cpp:90
ULONG m_dwRefs
Definition: qdnd_p.h:162