MSW: fix DisableScreensaver and RestoreScreensaver definitions
[vlc/asuraparaju-public.git] / projects / activex / utils.h
blob6234ab3cebd08643457ad7097f5864e9ad48a666
1 /*****************************************************************************
2 * utils.h: ActiveX control for VLC
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
6 * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
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.
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.
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 *****************************************************************************/
23 #ifndef __UTILS_H__
24 #define __UTILS_H__
26 #include <ole2.h>
28 #include <vector>
30 // utilities
31 extern char *CStrFromWSTR(UINT codePage, LPCWSTR wstr, UINT len);
32 extern char *CStrFromBSTR(UINT codePage, BSTR bstr);
33 extern BSTR BSTRFromCStr(UINT codePage, LPCSTR s);
35 // properties
36 extern HRESULT GetObjectProperty(LPUNKNOWN object, DISPID dispID, VARIANT& v);
38 // properties
39 extern HDC CreateDevDC(DVTARGETDEVICE *ptd);
40 extern void DPFromHimetric(HDC hdc, LPPOINT pt, int count);
41 extern void HimetricFromDP(HDC hdc, LPPOINT pt, int count);
43 // URL
44 extern LPWSTR CombineURL(LPCWSTR baseUrl, LPCWSTR url);
46 /**************************************************************************************************/
48 /* this function object is used to dereference the iterator into a value */
49 template <typename T, class Iterator>
50 struct VLCDereference
52 T operator()(const Iterator& i) const
54 return *i;
58 template<REFIID EnumeratorIID, class Enumerator, typename T, class Iterator, typename Dereference = VLCDereference<T, Iterator> >
59 class VLCEnumIterator : public Enumerator
62 public:
64 VLCEnumIterator(const Iterator& from, const Iterator& to) :
65 _refcount(1),
66 _begin(from),
67 _curr(from),
68 _end(to)
69 {};
71 VLCEnumIterator(const VLCEnumIterator& e) :
72 Enumerator(),
73 _refcount(e._refcount),
74 _begin(e._begin),
75 _curr(e._curr),
76 _end(e._end)
77 {};
79 virtual ~VLCEnumIterator()
80 {};
82 // IUnknown methods
83 STDMETHODIMP QueryInterface(REFIID riid, void **ppv)
85 if( NULL == ppv )
86 return E_POINTER;
87 if( (IID_IUnknown == riid)
88 || (EnumeratorIID == riid) )
90 AddRef();
91 *ppv = reinterpret_cast<LPVOID>(this);
92 return NOERROR;
94 // standalone object
95 return E_NOINTERFACE;
98 STDMETHODIMP_(ULONG) AddRef(void)
100 return InterlockedIncrement(&_refcount);
103 STDMETHODIMP_(ULONG) Release(void)
105 ULONG refcount = InterlockedDecrement(&_refcount);
106 if( 0 == refcount )
108 delete this;
109 return 0;
111 return refcount;
115 // IEnumXXXX methods
116 STDMETHODIMP Next(ULONG celt, T *rgelt, ULONG *pceltFetched)
118 if( NULL == rgelt )
119 return E_POINTER;
121 if( (celt > 1) && (NULL == pceltFetched) )
122 return E_INVALIDARG;
124 ULONG c = 0;
126 while( (c < celt) && (_curr != _end) )
128 rgelt[c] = dereference(_curr);
129 ++_curr;
130 ++c;
133 if( NULL != pceltFetched )
134 *pceltFetched = c;
136 return (c == celt) ? S_OK : S_FALSE;
139 STDMETHODIMP Skip(ULONG celt)
141 ULONG c = 0;
143 while( (c < celt) && (_curr != _end) )
145 ++_curr;
146 ++c;
148 return (c == celt) ? S_OK : S_FALSE;
151 STDMETHODIMP Reset(void)
153 _curr = _begin;
154 return S_OK;
157 STDMETHODIMP Clone(Enumerator **ppEnum)
159 if( NULL == ppEnum )
160 return E_POINTER;
161 *ppEnum = dynamic_cast<Enumerator *>(new VLCEnumIterator(*this));
162 return (NULL != *ppEnum ) ? S_OK : E_OUTOFMEMORY;
165 private:
167 LONG _refcount;
168 Iterator _begin, _curr, _end;
170 Dereference dereference;
174 #endif