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