1 #ifdef _WIN32
2 
3 #include <CtrlLib/CtrlLib.h>
4 #include "ActiveX.h"
5 
6 
7 namespace Upp {
8 
BSTRSet(const String str,BSTR & bstr)9 bool BSTRSet(const String str, BSTR &bstr) {
10 	wchar_t *buffer;
11 	DWORD size = MultiByteToWideChar(CP_UTF8, 0, str, -1, 0, 0);
12 	if (!(buffer = (wchar_t *)GlobalAlloc(GMEM_FIXED, sizeof(wchar_t) * size)))
13 		return false;
14 
15 	MultiByteToWideChar(CP_UTF8, 0, str, -1, buffer, size);
16 	bstr = SysAllocString(buffer);
17 	GlobalFree(buffer);
18 	if (!bstr)
19 		return false;
20 	return true;
21 }
22 
BSTRGet(BSTR & bstr)23 String BSTRGet(BSTR &bstr) {
24 	if (!bstr)
25 		return Null;
26 
27 	char *buffer;
28 	DWORD size = SysStringLen(bstr);
29 	if (!(buffer = (char *)GlobalAlloc(GMEM_FIXED, sizeof(wchar_t) * size)))
30 		return Null;
31 
32 	size_t i = wcstombs(buffer, bstr, size);
33 	buffer[i] = 0;
34 
35 	String ret = buffer;
36 	GlobalFree(buffer);
37 	return ret;
38 }
39 
OleStr()40 OleStr::OleStr() {
41     VariantInit(&var);
42     VariantClear(&var);
43 }
44 
~OleStr()45 OleStr::~OleStr() {
46   	SysFreeString(var.bstrVal);
47 }
48 
Set(const String str)49 bool OleStr::Set(const String str) {
50 	var.vt = VT_BSTR;
51 	return BSTRSet(str, var.bstrVal);
52 }
53 
~CBSTR()54 CBSTR::~CBSTR() {
55 	SysFreeString(bstr);
56 }
57 
Set(const String str)58 bool CBSTR::Set(const String str) {
59 	return BSTRSet(str, bstr);
60 }
61 
DHCtrlActiveX(CLSID clsid,const String name,bool status)62 DHCtrlActiveX::DHCtrlActiveX(CLSID clsid, const String name, bool status) :
63 						clsid(clsid), name(name), status(status), oleObj(0), pClientSite(0) {}
64 
~DHCtrlActiveX(void)65 DHCtrlActiveX::~DHCtrlActiveX(void) {
66 	Detach();
67 }
68 
SetStatus(bool _status)69 DHCtrlActiveX &DHCtrlActiveX::SetStatus(bool _status) {
70 	status = _status;
71 	if (status) {
72 		Attach(GetHWND());
73 		EnableWindow(GetHWND(), true);
74 		BackPaint(NOBACKPAINT);
75 	} else {
76 		Detach();
77 		BackPaint(FULLBACKPAINT);
78 	}
79 	return *this;
80 }
81 
WindowProc(UINT message,WPARAM wParam,LPARAM lParam)82 LRESULT DHCtrlActiveX::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) {
83 	if (status) {
84 		switch (message) {
85 		case WM_SIZE: 	DoResize();
86 						break;
87 		case WM_CREATE:	Attach(GetHWND());
88 						EnableWindow(GetHWND(), true);
89 						break;
90 		}
91 	}
92 	return DHCtrl::WindowProc(message, wParam, lParam);
93 }
94 
QueryInterface(const IID iid)95 void *DHCtrlActiveX::QueryInterface(const IID iid) {
96 	void *ret;
97 	if (!oleObj || !status)
98 		return NULL;
99 	if (S_OK != oleObj->QueryInterface(iid, &ret))
100 		return NULL;
101 	return ret;
102 }
103 
Detach()104 void DHCtrlActiveX::Detach() {
105 	if(!pClientSite.hwnd)
106 		return;
107 	if (!oleObj)
108 		return;
109 
110 	oleObj->Close(OLECLOSE_NOSAVE);
111 	oleObj->Release();
112 	oleObj = 0;
113 }
114 
DoResize()115 void DHCtrlActiveX::DoResize() {
116 	if(!pClientSite.hwnd)
117 		return;
118 	if (!oleObj || !status)
119 		return;
120 
121 	RECT rect;
122 	GetClientRect(pClientSite.hwnd, &rect);
123 
124 	IOleInPlaceObject *inplace = (IOleInPlaceObject *)QueryInterface(IID_IOleInPlaceObject);
125 	if (inplace) {
126 		inplace->SetObjectRects(&rect, &rect);
127 		inplace->Release();
128 	}
129 }
130 
Attach(HWND hwnd)131 bool DHCtrlActiveX::Attach(HWND hwnd) {
132 	if (hwnd == (HWND)-1 || hwnd == 0 || !status)
133 		return false;
134 	if(pClientSite.hwnd && pClientSite.hwnd != hwnd)
135 		Detach();
136 	if(!hwnd)
137 		return false;
138 
139 	RECT rect;
140 	AXStorage storage;
141 	pClientSite.hwnd = hwnd;
142 
143 	if (OleCreate(clsid, IID_IOleObject, OLERENDER_DRAW, 0, (IOleClientSite *)&pClientSite, &storage, (void**)&oleObj))
144 		return false;
145 
146 	oleObj->SetHostNames(name.ToWString(), 0);
147 	GetClientRect(hwnd, &rect);
148 	if (!OleSetContainedObject((IUnknown *)oleObj, TRUE)) {
149 		if (!oleObj->DoVerb(OLEIVERB_INPLACEACTIVATE, NULL, (IOleClientSite *)&pClientSite, -1, hwnd, &rect))
150 			return true;
151 	}
152 	Detach();
153 	return false;
154 }
155 
QueryInterface(REFIID riid,void __RPC_FAR * __RPC_FAR * ppvObject)156 HRESULT STDMETHODCALLTYPE AXStorage::QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject) {
157 	throw Exc(t_("Function not implemented"));
158 }
159 
AddRef(void)160 ULONG STDMETHODCALLTYPE AXStorage::AddRef( void) {
161 	return 1;
162 }
163 
Release(void)164 ULONG STDMETHODCALLTYPE AXStorage::Release( void) {
165 	return 0;
166 }
167 
CreateStream(const OLECHAR * pwcsName,DWORD grfMode,DWORD reserved1,DWORD reserved2,IStream ** ppstm)168 HRESULT STDMETHODCALLTYPE AXStorage::CreateStream(const OLECHAR *pwcsName, DWORD grfMode, DWORD reserved1, DWORD reserved2, IStream **ppstm) {
169 	throw Exc(t_("Function not implemented"));
170 }
171 
OpenStream(const OLECHAR * pwcsName,void * reserved1,DWORD grfMode,DWORD reserved2,IStream ** ppstm)172 HRESULT STDMETHODCALLTYPE AXStorage::OpenStream(const OLECHAR *pwcsName, void *reserved1, DWORD grfMode, DWORD reserved2, IStream **ppstm) {
173 	throw Exc(t_("Function not implemented"));
174 }
175 
CreateStorage(const OLECHAR * pwcsName,DWORD grfMode,DWORD reserved1,DWORD reserved2,IStorage ** ppstg)176 HRESULT STDMETHODCALLTYPE AXStorage::CreateStorage(const OLECHAR *pwcsName, DWORD grfMode, DWORD reserved1, DWORD reserved2, IStorage **ppstg) {
177 	throw Exc(t_("Function not implemented"));
178 }
179 
OpenStorage(const OLECHAR * pwcsName,IStorage * pstgPriority,DWORD grfMode,SNB snbExclude,DWORD reserved,IStorage ** ppstg)180 HRESULT STDMETHODCALLTYPE AXStorage::OpenStorage(const OLECHAR *pwcsName, IStorage *pstgPriority, DWORD grfMode, SNB snbExclude, DWORD reserved, IStorage **ppstg) {
181 	throw Exc(t_("Function not implemented"));
182 }
183 
CopyTo(DWORD ciidExclude,const IID * rgiidExclude,SNB snbExclude,IStorage * pstgDest)184 HRESULT STDMETHODCALLTYPE AXStorage::CopyTo(DWORD ciidExclude, const IID *rgiidExclude, SNB snbExclude, IStorage *pstgDest) {
185 	throw Exc(t_("Function not implemented"));
186 }
187 
MoveElementTo(const OLECHAR * pwcsName,IStorage * pstgDest,const OLECHAR * pwcsNewName,DWORD grfFlags)188 HRESULT STDMETHODCALLTYPE AXStorage::MoveElementTo(const OLECHAR *pwcsName, IStorage *pstgDest, const OLECHAR *pwcsNewName, DWORD grfFlags) {
189 	throw Exc(t_("Function not implemented"));
190 }
191 
Commit(DWORD grfCommitFlags)192 HRESULT STDMETHODCALLTYPE AXStorage::Commit(DWORD grfCommitFlags) {
193 	throw Exc(t_("Function not implemented"));
194 }
195 
Revert()196 HRESULT STDMETHODCALLTYPE AXStorage::Revert() {
197 	throw Exc(t_("Function not implemented"));
198 }
199 
EnumElements(DWORD reserved1,void * reserved2,DWORD reserved3,IEnumSTATSTG ** ppenum)200 HRESULT STDMETHODCALLTYPE AXStorage::EnumElements(DWORD reserved1, void *reserved2, DWORD reserved3, IEnumSTATSTG **ppenum) {
201 	throw Exc(t_("Function not implemented"));
202 }
203 
DestroyElement(const OLECHAR * pwcsName)204 HRESULT STDMETHODCALLTYPE AXStorage::DestroyElement(const OLECHAR *pwcsName) {
205 	throw Exc(t_("Function not implemented"));
206 }
207 
RenameElement(const OLECHAR * pwcsOldName,const OLECHAR * pwcsNewName)208 HRESULT STDMETHODCALLTYPE AXStorage::RenameElement(const OLECHAR *pwcsOldName, const OLECHAR *pwcsNewName) {
209 	throw Exc(t_("Function not implemented"));
210 }
211 
SetElementTimes(const OLECHAR * pwcsName,const FILETIME * pctime,const FILETIME * patime,const FILETIME * pmtime)212 HRESULT STDMETHODCALLTYPE AXStorage::SetElementTimes(const OLECHAR *pwcsName, const FILETIME *pctime, const FILETIME *patime, const FILETIME *pmtime) {
213 	throw Exc(t_("Function not implemented"));
214 }
215 
SetClass(REFCLSID clsid)216 HRESULT STDMETHODCALLTYPE AXStorage::SetClass(REFCLSID clsid) {
217 	return(S_OK);
218 }
219 
SetStateBits(DWORD grfStateBits,DWORD grfMask)220 HRESULT STDMETHODCALLTYPE AXStorage::SetStateBits(DWORD grfStateBits, DWORD grfMask) {
221 	throw Exc(t_("Function not implemented"));
222 }
223 
Stat(STATSTG * pstatstg,DWORD grfStatFlag)224 HRESULT STDMETHODCALLTYPE AXStorage::Stat(STATSTG *pstatstg, DWORD grfStatFlag) {
225 	throw Exc(t_("Function not implemented"));
226 }
227 
QueryInterface(REFIID riid,void __RPC_FAR * __RPC_FAR * ppvObject)228 HRESULT STDMETHODCALLTYPE AXClientSite::QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject) {
229 	if (!memcmp(&riid, &IID_IUnknown, sizeof(GUID)))
230 		*ppvObject = reinterpret_cast<IUnknown *> (this);
231 	else if (!memcmp(&riid, &IID_IOleClientSite, sizeof(GUID)))
232 		*ppvObject = (IOleClientSite *)this;
233 	else if (!memcmp(&riid, &IID_IOleInPlaceSite, sizeof(GUID)))
234 		*ppvObject = (IOleInPlaceSite *)this;
235 	else {
236 		*ppvObject = 0;
237 		return(E_NOINTERFACE);
238 	}
239 	return S_OK;
240 }
241 
AddRef()242 ULONG STDMETHODCALLTYPE AXClientSite::AddRef() {
243 	return 1;
244 }
245 
Release()246 ULONG STDMETHODCALLTYPE AXClientSite::Release() {
247 	return 0;
248 }
249 
SaveObject()250 HRESULT STDMETHODCALLTYPE AXClientSite::SaveObject() {
251 	throw Exc(t_("Function not implemented"));
252 }
253 
GetMoniker(DWORD dwAssign,DWORD dwWhichMoniker,IMoniker ** ppmk)254 HRESULT STDMETHODCALLTYPE AXClientSite::GetMoniker(DWORD dwAssign, DWORD dwWhichMoniker, IMoniker **ppmk) {
255 	throw Exc(t_("Function not implemented"));
256 }
257 
GetContainer(IOleContainer ** ppContainer)258 HRESULT STDMETHODCALLTYPE AXClientSite::GetContainer(IOleContainer **ppContainer) {
259 	*ppContainer = 0;
260 	return E_NOINTERFACE;
261 }
262 
ShowObject()263 HRESULT STDMETHODCALLTYPE AXClientSite::ShowObject() {
264 	return NOERROR;
265 }
266 
OnShowWindow(BOOL fShow)267 HRESULT STDMETHODCALLTYPE AXClientSite::OnShowWindow(BOOL fShow) {
268 	return NOERROR;
269 }
270 
RequestNewObjectLayout()271 HRESULT STDMETHODCALLTYPE AXClientSite::RequestNewObjectLayout() {
272 	throw Exc(t_("Function not implemented"));
273 }
274 
275 
GetWindow(HWND * phwnd)276 HRESULT STDMETHODCALLTYPE AXClientSite::GetWindow(HWND *phwnd) {
277 	*phwnd = hwnd;
278 	return S_OK;
279 }
280 
ContextSensitiveHelp(BOOL fEnterMode)281 HRESULT STDMETHODCALLTYPE AXClientSite::ContextSensitiveHelp(BOOL fEnterMode) {
282 	throw Exc(t_("Function not implemented"));
283 }
284 
GetBorder(LPRECT lprectBorder)285 HRESULT STDMETHODCALLTYPE AXClientSite::GetBorder(LPRECT lprectBorder) {
286 	throw Exc(t_("Function not implemented"));
287 }
288 
RequestBorderSpace(LPCBORDERWIDTHS pborderwidths)289 HRESULT STDMETHODCALLTYPE AXClientSite::RequestBorderSpace(LPCBORDERWIDTHS pborderwidths) {
290 	throw Exc(t_("Function not implemented"));
291 }
292 
SetBorderSpace(LPCBORDERWIDTHS pborderwidths)293 HRESULT STDMETHODCALLTYPE AXClientSite::SetBorderSpace(LPCBORDERWIDTHS pborderwidths) {
294 	throw Exc(t_("Function not implemented"));
295 }
296 
SetActiveObject(IOleInPlaceActiveObject * pActiveObject,LPCOLESTR pszObjName)297 HRESULT STDMETHODCALLTYPE AXClientSite::SetActiveObject(IOleInPlaceActiveObject *pActiveObject, LPCOLESTR pszObjName) {
298 	return S_OK;
299 }
300 
InsertMenus(HMENU hmenuShared,LPOLEMENUGROUPWIDTHS lpMenuWidths)301 HRESULT STDMETHODCALLTYPE AXClientSite::InsertMenus(HMENU hmenuShared, LPOLEMENUGROUPWIDTHS lpMenuWidths) {
302 	throw Exc(t_("Function not implemented"));
303 }
304 
SetMenu(HMENU hmenuShared,HOLEMENU holemenu,HWND hwndActiveObject)305 HRESULT STDMETHODCALLTYPE AXClientSite::SetMenu(HMENU hmenuShared, HOLEMENU holemenu, HWND hwndActiveObject) {
306 	return S_OK;
307 }
308 
RemoveMenus(HMENU hmenuShared)309 HRESULT STDMETHODCALLTYPE AXClientSite::RemoveMenus(HMENU hmenuShared) {
310 	throw Exc(t_("Function not implemented"));
311 }
312 
SetStatusText(LPCOLESTR pszStatusText)313 HRESULT STDMETHODCALLTYPE AXClientSite::SetStatusText(LPCOLESTR pszStatusText) {
314 	return S_OK;
315 }
316 
EnableModeless(BOOL fEnable)317 HRESULT STDMETHODCALLTYPE AXClientSite::EnableModeless(BOOL fEnable) {
318 	return S_OK;
319 }
320 
TranslateAccelerator(LPMSG lpmsg,WORD wID)321 HRESULT STDMETHODCALLTYPE AXClientSite::TranslateAccelerator(LPMSG lpmsg, WORD wID) {
322 	throw Exc(t_("Function not implemented"));
323 }
324 
CanInPlaceActivate()325 HRESULT STDMETHODCALLTYPE AXClientSite::CanInPlaceActivate() {
326 	return S_OK;
327 }
328 
OnInPlaceActivate()329 HRESULT STDMETHODCALLTYPE AXClientSite::OnInPlaceActivate() {
330 	return S_OK;
331 }
332 
OnUIActivate()333 HRESULT STDMETHODCALLTYPE AXClientSite::OnUIActivate() {
334 	return S_OK;
335 }
336 
GetWindowContext(IOleInPlaceFrame ** ppFrame,IOleInPlaceUIWindow ** ppDoc,LPRECT lprcPosRect,LPRECT lprcClipRect,LPOLEINPLACEFRAMEINFO lpFrameInfo)337 HRESULT STDMETHODCALLTYPE AXClientSite::GetWindowContext(IOleInPlaceFrame **ppFrame, IOleInPlaceUIWindow **ppDoc, LPRECT lprcPosRect, LPRECT lprcClipRect, LPOLEINPLACEFRAMEINFO lpFrameInfo) {
338 	*ppFrame = (IOleInPlaceFrame *)this;
339 
340 	*ppDoc = 0;
341 
342 	lpFrameInfo->fMDIApp = FALSE;
343 	lpFrameInfo->hwndFrame = hwnd;
344 	lpFrameInfo->haccel = 0;
345 	lpFrameInfo->cAccelEntries = 0;
346 
347 	GetClientRect(lpFrameInfo->hwndFrame, lprcPosRect);
348 	GetClientRect(lpFrameInfo->hwndFrame, lprcClipRect);
349 
350 	return S_OK;
351 }
352 
Scroll(SIZE scrollExtant)353 HRESULT STDMETHODCALLTYPE AXClientSite::Scroll(SIZE scrollExtant) {
354 	throw Exc(t_("Function not implemented"));
355 }
356 
OnUIDeactivate(BOOL fUndoable)357 HRESULT STDMETHODCALLTYPE AXClientSite::OnUIDeactivate(BOOL fUndoable) {
358 	return S_OK;
359 }
360 
OnInPlaceDeactivate()361 HRESULT STDMETHODCALLTYPE AXClientSite::OnInPlaceDeactivate() {
362 	return S_OK;
363 }
364 
DiscardUndoState()365 HRESULT STDMETHODCALLTYPE AXClientSite::DiscardUndoState() {
366 	throw Exc(t_("Function not implemented"));
367 }
368 
DeactivateAndUndo()369 HRESULT STDMETHODCALLTYPE AXClientSite::DeactivateAndUndo() {
370 	throw Exc(t_("Function not implemented"));
371 }
372 
OnPosRectChange(LPCRECT lprcPosRect)373 HRESULT STDMETHODCALLTYPE AXClientSite::OnPosRectChange(LPCRECT lprcPosRect) {
374 	return S_OK;
375 }
376 
377 }
378 
379 #endif
380