]> git.sesse.net Git - vlc/blob - activex/utils.h
Initial drag and drop support for Qt
[vlc] / activex / utils.h
1 /*****************************************************************************
2  * utils.h: ActiveX control for VLC
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  *
6  * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 #ifndef __UTILS_H__
24 #define __UTILS_H__
25
26 #include <ole2.h>
27
28 #include <vector>
29
30 // utilities
31 extern char *CStrFromBSTR(UINT codePage, BSTR bstr);
32 extern BSTR BSTRFromCStr(UINT codePage, LPCSTR s);
33
34 extern char *CStrFromGUID(REFGUID clsid);
35
36 // properties
37 extern HRESULT GetObjectProperty(LPUNKNOWN object, DISPID dispID, VARIANT& v);
38
39 // properties
40 extern HDC CreateDevDC(DVTARGETDEVICE *ptd);
41 extern void DPFromHimetric(HDC hdc, LPPOINT pt, int count);
42 extern void HimetricFromDP(HDC hdc, LPPOINT pt, int count);
43
44 /**************************************************************************************************/
45
46 /* this function object is used to dereference the iterator into a value */
47 template <class T, class Iterator>
48 struct VLCDereference
49 {
50     T operator()(const Iterator& i) const
51     {
52         return *i;
53     };
54 };
55
56 template<REFIID EnumeratorIID, class Enumerator, class T, class Iterator, typename Dereference = VLCDereference<T, Iterator> >
57 class VLCEnumIterator : public Enumerator
58 {
59
60 public:
61
62     VLCEnumIterator(const Iterator& from, const Iterator& to) :
63         _refcount(1),
64         _begin(from),
65         _curr(from),
66         _end(to)
67     {};
68
69     VLCEnumIterator(const VLCEnumIterator& e) :
70         Enumerator(),
71         _refcount(e._refcount),
72         _begin(e._begin),
73         _curr(e._curr),
74         _end(e._end)
75     {};
76
77     virtual ~VLCEnumIterator()
78     {};
79
80     // IUnknown methods
81     STDMETHODIMP QueryInterface(REFIID riid, void **ppv)
82     {
83         if( NULL == ppv )
84             return E_POINTER;
85         if( (IID_IUnknown == riid) 
86          || (EnumeratorIID == riid) )
87         {
88             AddRef();
89             *ppv = reinterpret_cast<LPVOID>(this);
90             return NOERROR;
91         }
92         // standalone object
93         return E_NOINTERFACE;
94     };
95
96     STDMETHODIMP_(ULONG) AddRef(void)
97     {
98         return InterlockedIncrement(&_refcount);
99     };
100
101     STDMETHODIMP_(ULONG) Release(void)
102     {
103         ULONG refcount = InterlockedDecrement(&_refcount);
104         if( 0 == refcount )
105         {
106             delete this;
107             return 0;
108         }
109         return refcount;
110     };
111
112
113     // IEnumXXXX methods
114     STDMETHODIMP Next(ULONG celt, T *rgelt, ULONG *pceltFetched)
115     {
116         if( NULL == rgelt )
117             return E_POINTER;
118
119         if( (celt > 1) && (NULL == pceltFetched) )
120             return E_INVALIDARG;
121
122         ULONG c = 0;
123
124         while( (c < celt) && (_curr != _end) )
125         {
126             rgelt[c] = dereference(_curr);
127             ++_curr;
128             ++c;
129         }
130
131         if( NULL != pceltFetched )
132             *pceltFetched = c;
133
134         return (c == celt) ? S_OK : S_FALSE;
135     };
136
137     STDMETHODIMP Skip(ULONG celt)
138     {
139         ULONG c = 0;
140
141         while( (c < celt) && (_curr != _end) )
142         {
143             ++_curr;
144             ++c;
145         }
146         return (c == celt) ? S_OK : S_FALSE;
147     };
148
149     STDMETHODIMP Reset(void)
150     {
151         _curr = _begin;
152         return S_OK;
153     };
154
155     STDMETHODIMP Clone(Enumerator **ppEnum)
156     {
157         if( NULL == ppEnum )
158             return E_POINTER;
159         *ppEnum = dynamic_cast<Enumerator *>(new VLCEnumIterator(*this));
160         return (NULL != *ppEnum ) ? S_OK : E_OUTOFMEMORY;
161     };
162
163 private:
164
165     LONG     _refcount;
166     Iterator _begin, _curr, _end;
167
168     Dereference dereference;
169
170 };
171
172 #endif
173