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