]> git.sesse.net Git - vlc/blob - activex/utils.h
4514880ae7293d3d2719b382f3bedc286dae04dc
[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 // enumeration
47 template<class T> class VLCEnum : IUnknown
48 {
49
50 public:
51
52     VLCEnum(REFIID riid, std::vector<T> &);
53     VLCEnum(const VLCEnum<T> &);
54     virtual ~VLCEnum() {};
55
56     VLCEnum<T>& operator=(const VLCEnum<T> &t);
57
58     // IUnknown methods
59     STDMETHODIMP QueryInterface(REFIID riid, void **);
60     STDMETHODIMP_(ULONG) AddRef(void);
61     STDMETHODIMP_(ULONG) Release(void);
62
63     // IEnumXXXX methods
64     STDMETHODIMP Next(ULONG, T *, ULONG *);
65     STDMETHODIMP Skip(ULONG);
66     STDMETHODIMP Reset(void);
67     // cloning is implemented by subclasses and must use copy constructor
68     //STDMETHODIMP Clone(VLCEnum<T> **);
69
70     typedef void (*retainer)(T);
71
72     void setRetainOperation(retainer retain) { _retain = retain; };
73
74 private:
75
76     LONG                                _refcount;
77     std::vector<T>                      _v;
78     typename std::vector<T>::iterator   _i;
79     REFIID                              _riid;
80     retainer                            _retain;
81 };
82
83 template<class T>
84 VLCEnum<T>::VLCEnum(REFIID riid, std::vector<T> &v) :
85     _refcount(1),
86     _v(v),
87     _riid(riid),
88     _retain(NULL)
89 {
90     _i= v.begin();
91 };
92
93 template<class T>
94 VLCEnum<T>::VLCEnum(const VLCEnum<T> &e) :
95     IUnknown(),
96     _refcount(1),
97     _v(e._v),
98     _riid(e._riid)
99 {
100 };
101
102 template<class T>
103 VLCEnum<T>& VLCEnum<T>::operator=(const VLCEnum<T> &e)
104 {
105     this->_refcount = 1;
106     this->_riid = e._riid;
107     this->_v    = e._v;
108     this->_i    = e._i;
109     return this;
110 };
111
112 template<class T>
113 STDMETHODIMP VLCEnum<T>::QueryInterface(REFIID riid, void **ppv)
114 {
115     if( NULL == ppv )
116         return E_POINTER;
117     if( (IID_IUnknown == riid) 
118      || (_riid == riid) )
119     {
120         AddRef();
121         *ppv = reinterpret_cast<LPVOID>(this);
122         return NOERROR;
123     }
124     // standalone object
125     return E_NOINTERFACE;
126 };
127
128 template<class T>
129 STDMETHODIMP_(ULONG) VLCEnum<T>::AddRef(void)
130 {
131     return InterlockedIncrement(&_refcount);
132 };
133
134 template<class T>
135 STDMETHODIMP_(ULONG) VLCEnum<T>::Release(void)
136 {
137     ULONG refcount = InterlockedDecrement(&_refcount);
138     if( 0 == refcount )
139     {
140         delete this;
141         return 0;
142     }
143     return refcount;
144 };
145
146 template<class T>
147 STDMETHODIMP VLCEnum<T>::Next(ULONG celt, T *rgelt, ULONG *pceltFetched)
148 {
149     if( NULL == rgelt )
150         return E_POINTER;
151
152     if( (celt > 1) && (NULL == pceltFetched) )
153         return E_INVALIDARG;
154
155     ULONG c = 0;
156     typename std::vector<T>::iterator end = _v.end();
157
158     while( (c < celt) && (_i != end) )
159     {
160         rgelt[c] = *_i;
161         if( NULL != _retain ) _retain(rgelt[c]);
162         ++_i;
163         ++c;
164     }
165
166     if( NULL != pceltFetched )
167         *pceltFetched = c;
168
169     return (c == celt) ? S_OK : S_FALSE;
170 };
171
172 template<class T>
173 STDMETHODIMP VLCEnum<T>::Skip(ULONG celt)
174 {
175     ULONG c = 0;
176     typename std::vector<T>::iterator end = _v.end();
177
178     while( (c < celt) && (_i != end) )
179     {
180         ++_i;
181         ++c;
182     }
183     return (c == celt) ? S_OK : S_FALSE;
184 };
185
186 template<class T>
187 STDMETHODIMP VLCEnum<T>::Reset(void)
188 {
189     _i= _v.begin();
190     return S_OK;
191 };
192
193 #endif
194