]> git.saurik.com Git - wxWidgets.git/blob - wxPython/contrib/iewin/wxactivex.h
Ensure that the wxSTC gets the focus when clicked.
[wxWidgets.git] / wxPython / contrib / iewin / wxactivex.h
1 #ifndef WX_ACTIVE_X
2 #define WX_ACTIVE_X
3 #pragma warning( disable : 4101 4786)
4 #pragma warning( disable : 4786)
5
6
7 #include <wx/setup.h>
8 #include <wx/wx.h>
9 #include <oleidl.h>
10 #include <docobj.h>
11 #include <iostream>
12 #include <vector>
13 using namespace std;
14
15 //////////////////////////////////////////
16 // wxAutoOleInterface<Interface>
17 // Template class for smart interface handling
18 // - Automatically dereferences ole interfaces
19 // - Smart Copy Semantics
20 // - Can Create Interfaces
21 // - Can query for other interfaces
22 template <class I> class wxAutoOleInterface
23 {
24 protected:
25 I *m_interface;
26
27 public:
28 // takes ownership of an existing interface
29 // Assumed to already have a AddRef() applied
30 explicit wxAutoOleInterface(I *pInterface = NULL) : m_interface(pInterface) {}
31
32 // queries for an interface
33 wxAutoOleInterface(REFIID riid, IUnknown *pUnk) : m_interface(NULL)
34 {
35 QueryInterface(riid, pUnk);
36 };
37 // queries for an interface
38 wxAutoOleInterface(REFIID riid, IDispatch *pDispatch) : m_interface(NULL)
39 {
40 QueryInterface(riid, pDispatch);
41 };
42
43 // Creates an Interface
44 wxAutoOleInterface(REFCLSID clsid, REFIID riid) : m_interface(NULL)
45 {
46 CreateInstance(clsid, riid);
47 };
48
49 // copy constructor
50 explicit wxAutoOleInterface(const wxAutoOleInterface<I>& ti) : m_interface(NULL)
51 {
52 operator = (ti);
53 }
54
55 // assignment operator
56 wxAutoOleInterface<I>& operator = (const wxAutoOleInterface<I>& ti)
57 {
58 if (ti.m_interface)
59 ti.m_interface->AddRef();
60 Free();
61 m_interface = ti.m_interface;
62 return *this;
63 }
64
65 // takes ownership of an existing interface
66 // Assumed to already have a AddRef() applied
67 wxAutoOleInterface<I>& operator = (I *&ti)
68 {
69 Free();
70 m_interface = ti;
71 return *this;
72 }
73
74 ~wxAutoOleInterface()
75 {
76 Free();
77 };
78
79
80 inline void Free()
81 {
82 if (m_interface)
83 m_interface->Release();
84 m_interface = NULL;
85 };
86
87 // queries for an interface
88 HRESULT QueryInterface(REFIID riid, IUnknown *pUnk)
89 {
90 Free();
91 wxASSERT(pUnk != NULL);
92 return pUnk->QueryInterface(riid, (void **) &m_interface);
93 };
94
95 // Create a Interface instance
96 HRESULT CreateInstance(REFCLSID clsid, REFIID riid)
97 {
98 Free();
99 return CoCreateInstance(clsid, NULL, CLSCTX_ALL, riid, (void **) &m_interface);
100 };
101
102
103
104 inline operator I *() const {return m_interface;}
105 inline I* operator ->() {return m_interface;}
106 inline I** GetRef() {return &m_interface;}
107
108 inline bool Ok() const {return m_interface != NULL;}
109 };
110
111
112 wxString OLEHResultToString(HRESULT hr);
113 wxString GetIIDName(REFIID riid);
114
115 //#define __WXOLEDEBUG
116
117
118 #ifdef __WXOLEDEBUG
119 #define WXOLE_TRACE(str) {OutputDebugString(str);OutputDebugString("\r\n");}
120 #define WXOLE_TRACEOUT(stuff)\
121 {\
122 ostringstream os;\
123 os << stuff << ends;\
124 WXOLE_TRACE(os.str().c_str());\
125 }
126
127 #define WXOLE_WARN(__hr,msg)\
128 {\
129 if (__hr != S_OK)\
130 {\
131 wxString s = "*** ";\
132 s += msg;\
133 s += " : "+ OLEHResultToString(__hr);\
134 WXOLE_TRACE(s.c_str());\
135 }\
136 }
137 #else
138 #define WXOLE_TRACE(str)
139 #define WXOLE_TRACEOUT(stuff)
140 #define WXOLE_WARN(_proc,msg) {_proc;}
141 #endif
142
143 // Auto Initialisation
144 class wxOleInit
145 {
146 public:
147 static IMalloc *GetIMalloc();
148
149 wxOleInit();
150 ~wxOleInit();
151 };
152
153 #define DECLARE_OLE_UNKNOWN(cls)\
154 private:\
155 class TAutoInitInt\
156 {\
157 public:\
158 LONG l;\
159 TAutoInitInt() : l(0) {}\
160 };\
161 TAutoInitInt refCount, lockCount;\
162 wxOleInit oleInit;\
163 static void _GetInterface(cls *self, REFIID iid, void **_interface, const char *&desc);\
164 public:\
165 LONG GetRefCount();\
166 HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void ** ppvObject);\
167 ULONG STDMETHODCALLTYPE AddRef();\
168 ULONG STDMETHODCALLTYPE Release();\
169 ULONG STDMETHODCALLTYPE AddLock();\
170 ULONG STDMETHODCALLTYPE ReleaseLock()
171
172 #define DEFINE_OLE_TABLE(cls)\
173 LONG cls::GetRefCount() {return refCount.l;}\
174 HRESULT STDMETHODCALLTYPE cls::QueryInterface(REFIID iid, void ** ppvObject)\
175 {\
176 if (! ppvObject)\
177 {\
178 WXOLE_TRACE("*** NULL POINTER ***");\
179 return E_FAIL;\
180 };\
181 const char *desc = NULL;\
182 cls::_GetInterface(this, iid, ppvObject, desc);\
183 if (! *ppvObject)\
184 {\
185 WXOLE_TRACEOUT("<" << GetIIDName(iid).c_str() << "> Not Found");\
186 return E_NOINTERFACE;\
187 };\
188 WXOLE_TRACEOUT("QI : <" << desc <<">");\
189 ((IUnknown * )(*ppvObject))->AddRef();\
190 return S_OK;\
191 };\
192 ULONG STDMETHODCALLTYPE cls::AddRef()\
193 {\
194 WXOLE_TRACEOUT(# cls << "::Add ref(" << refCount.l << ")");\
195 InterlockedIncrement(&refCount.l);\
196 return refCount.l;\
197 };\
198 ULONG STDMETHODCALLTYPE cls::Release()\
199 {\
200 if (refCount.l > 0)\
201 {\
202 InterlockedDecrement(&refCount.l);\
203 WXOLE_TRACEOUT(# cls << "::Del ref(" << refCount.l << ")");\
204 if (refCount.l == 0)\
205 {\
206 delete this;\
207 return 0;\
208 };\
209 return refCount.l;\
210 }\
211 else\
212 return 0;\
213 }\
214 ULONG STDMETHODCALLTYPE cls::AddLock()\
215 {\
216 WXOLE_TRACEOUT(# cls << "::Add Lock(" << lockCount.l << ")");\
217 InterlockedIncrement(&lockCount.l);\
218 return lockCount.l;\
219 };\
220 ULONG STDMETHODCALLTYPE cls::ReleaseLock()\
221 {\
222 if (lockCount.l > 0)\
223 {\
224 InterlockedDecrement(&lockCount.l);\
225 WXOLE_TRACEOUT(# cls << "::Del Lock(" << lockCount.l << ")");\
226 return lockCount.l;\
227 }\
228 else\
229 return 0;\
230 }\
231 DEFINE_OLE_BASE(cls)
232
233 #define DEFINE_OLE_BASE(cls)\
234 void cls::_GetInterface(cls *self, REFIID iid, void **_interface, const char *&desc)\
235 {\
236 *_interface = NULL;\
237 desc = NULL;
238
239 #define OLE_INTERFACE(_iid, _type)\
240 if (IsEqualIID(iid, _iid))\
241 {\
242 WXOLE_TRACE("Found Interface <" # _type ">");\
243 *_interface = (IUnknown *) (_type *) self;\
244 desc = # _iid;\
245 return;\
246 }
247
248 #define OLE_IINTERFACE(_face) OLE_INTERFACE(IID_##_face, _face)
249
250 #define OLE_INTERFACE_CUSTOM(func)\
251 if (func(self, iid, _interface, desc))\
252 return
253
254 #define END_OLE_TABLE\
255 }
256
257
258
259 class wxActiveX : public wxWindow {
260 public:
261 wxActiveX(wxWindow * parent, REFCLSID clsid, wxWindowID id = -1,
262 const wxPoint& pos = wxDefaultPosition,
263 const wxSize& size = wxDefaultSize,
264 long style = 0,
265 const wxString& name = wxPanelNameStr);
266 wxActiveX(wxWindow * parent, wxString progId, wxWindowID id = -1,
267 const wxPoint& pos = wxDefaultPosition,
268 const wxSize& size = wxDefaultSize,
269 long style = 0,
270 const wxString& name = wxPanelNameStr);
271
272 virtual ~wxActiveX();
273
274 void CreateActiveX(REFCLSID clsid);
275 void CreateActiveX(LPOLESTR progId);
276
277 HRESULT ConnectAdvise(REFIID riid, IUnknown *eventSink);
278
279 void OnSize(wxSizeEvent&);
280 void OnSetFocus(wxFocusEvent&);
281 void OnKillFocus(wxFocusEvent&);
282
283 DECLARE_EVENT_TABLE();
284
285 protected:
286 friend class FrameSite;
287
288 typedef wxAutoOleInterface<IConnectionPoint> wxOleConnectionPoint;
289 typedef pair<wxOleConnectionPoint, DWORD> wxOleConnection;
290 typedef vector<wxOleConnection> wxOleConnectionArray;
291
292 wxAutoOleInterface<IOleClientSite> m_clientSite;
293 wxAutoOleInterface<IUnknown> m_ActiveX;
294 wxAutoOleInterface<IOleObject> m_oleObject;
295 wxAutoOleInterface<IOleInPlaceObject> m_oleInPlaceObject;
296 wxAutoOleInterface<IOleInPlaceActiveObject>
297
298 m_oleInPlaceActiveObject;
299 wxAutoOleInterface<IOleDocumentView> m_docView;
300 HWND m_oleObjectHWND;
301 bool m_bAmbientUserMode;
302 DWORD m_docAdviseCookie;
303 wxOleConnectionArray m_connections;
304
305 HRESULT AmbientPropertyChanged(DISPID dispid);
306
307 };
308
309 #endif /* _IEHTMLWIN_H_ */