summaryrefslogtreecommitdiff
path: root/plugins/IEView/IEView.cpp
blob: 9d698401e7ce06513ddfdae51bcbf774f1817e96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
/*

IEView Plugin for Miranda IM
Copyright (C) 2005-2010  Piotr Piastucki

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
#include "IEView.h"
#include "resource.h"
#include "Options.h"
#include "Utils.h"

//#include <Urlmon.h>


#define WM_WAITWHILEBUSY (WM_USER+600)
//#define GECKO
#define DISPID_BEFORENAVIGATE2      250   // hyperlink clicked on
#define DISPID_NAVIGATECOMPLETE2    252   // UIActivate new document
#define DISPID_DOCUMENTCOMPLETE     259   // new document goes ReadyState_Complete

IEView * IEView::list = NULL;
CRITICAL_SECTION IEView::mutex;
bool IEView::isInited = false;

struct MM_INTERFACE   mmi;


static LRESULT CALLBACK IEViewServerWindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
	IEView *view = IEView::get(GetParent(GetParent(hwnd)));
	if (view != NULL) {
		switch (message) {
		case WM_KEYUP:
			if (LOWORD(wParam) == VK_ESCAPE && !(GetKeyState(VK_SHIFT) & 0x8000)
			&& !(GetKeyState(VK_CONTROL) & 0x8000) && !(GetKeyState(VK_MENU) & 0x8000)) {
				//if (view->getBuilder() != NULL) {
					SendMessage(GetParent(GetParent(GetParent(hwnd))), WM_COMMAND, IDCANCEL, 0);
//				} else {

	//			}
			}
			break;
		case WM_KEYDOWN:
			view->translateAccelerator(message, wParam, lParam);
			break;
		case WM_SETFOCUS:
			{
				RECT rcWindow;
				POINT cursor;
				GetWindowRect(hwnd, &rcWindow);
				GetCursorPos(&cursor);
				if (cursor.y > rcWindow.bottom || cursor.y < rcWindow.top ||
					cursor.x > rcWindow.right || cursor.x < rcWindow.left) {
				} else {
					view->mouseActivate();
				}
				if (view->setFocus((HWND)wParam)) {
					return TRUE;
				}
			}
			break;
		case WM_LBUTTONDOWN:
			POINT pt;
			pt.x = LOWORD(lParam);
			pt.y = HIWORD(lParam);
			if (view->mouseClick(pt)) {
				return TRUE;
			}
			break;
		}
		return CallWindowProc(view->getServerWndProc(), hwnd, message, wParam, lParam);
	}
	return DefWindowProc (hwnd, message, wParam, lParam);
}

static LRESULT CALLBACK IEViewDocWindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
	IEView *view = IEView::get(GetParent(hwnd));
	if (view!=NULL) {
		WNDPROC oldWndProc = view->getDocWndProc();
		if (message == WM_PARENTNOTIFY && wParam == WM_CREATE) {
			SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR) oldWndProc);
			view->setServerWndProc((WNDPROC) SetWindowLongPtr((HWND)lParam, GWLP_WNDPROC, (LONG_PTR) IEViewServerWindowProcedure));
		}
		return CallWindowProc(oldWndProc, hwnd, message, wParam, lParam);
	}
	return DefWindowProc (hwnd, message, wParam, lParam);
}

static LRESULT CALLBACK IEViewWindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
	IEView *view = IEView::get(hwnd);
	if (view!=NULL) {
		WNDPROC oldWndProc = view->getMainWndProc();
		if (message == WM_PARENTNOTIFY && wParam == WM_CREATE) {
			SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR) oldWndProc);
			view->setDocWndProc((WNDPROC) SetWindowLongPtr((HWND)lParam, GWLP_WNDPROC, (LONG_PTR) IEViewDocWindowProcedure));
		}
		return CallWindowProc(oldWndProc, hwnd, message, wParam, lParam);
	}
	return DefWindowProc (hwnd, message, wParam, lParam);
}

IEViewSink::IEViewSink(IEView *smptr) {
	ieWindow = smptr;
}

IEViewSink::~IEViewSink() {}

STDMETHODIMP IEViewSink::QueryInterface(REFIID riid, PVOID *ppv) {
	*ppv=NULL;
	if (IID_IUnknown==riid) {
		*ppv=(IUnknown *)this;
	}
	if (IID_IDispatch==riid) {
		*ppv=(IDispatch *)this;
	}
	if (DIID_DWebBrowserEvents2==riid) {
		*ppv=(DWebBrowserEvents2*)this;
	}
	if (NULL!=*ppv) {
		((LPUNKNOWN)*ppv)->AddRef();
		return NOERROR;
	}
	return E_NOINTERFACE;
}

STDMETHODIMP_(ULONG) IEViewSink::AddRef(void) {
	++m_cRef;
	return m_cRef;
}

STDMETHODIMP_(ULONG) IEViewSink::Release(void) {
	--m_cRef;
	return m_cRef;
}

STDMETHODIMP IEViewSink::GetTypeInfoCount(UINT *ptr) { return E_NOTIMPL; }
STDMETHODIMP IEViewSink::GetTypeInfo(UINT iTInfo, LCID lcid, LPTYPEINFO* ppTInfo) { return S_OK; }
STDMETHODIMP IEViewSink::GetIDsOfNames(REFIID riid, LPOLESTR* rgszNames, UINT cNames, LCID lcid, DISPID* rgDispId) { return S_OK; }

STDMETHODIMP IEViewSink::Invoke(DISPID dispIdMember, REFIID riid, LCID lcid , WORD wFlags,
							DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO*pExcepInfo, UINT*puArgErr) {
	if (!pDispParams) return E_INVALIDARG;
 	switch (dispIdMember) {
		case DISPID_BEFORENAVIGATE2:
			BeforeNavigate2(pDispParams->rgvarg[6].pdispVal,
							pDispParams->rgvarg[5].pvarVal,
							pDispParams->rgvarg[4].pvarVal,
							pDispParams->rgvarg[3].pvarVal,
							pDispParams->rgvarg[2].pvarVal,
							pDispParams->rgvarg[1].pvarVal,
							pDispParams->rgvarg[0].pboolVal);
			return S_OK;
	}
	return DISP_E_MEMBERNOTFOUND;
}
// DWebBrowserEvents2

void IEViewSink::StatusTextChange(BSTR text) {}
void IEViewSink::ProgressChange(long progress, long progressMax) {}
void IEViewSink::CommandStateChange(long command, VARIANT_BOOL enable) {}
void IEViewSink::DownloadBegin() {}
void IEViewSink::DownloadComplete() {}
void IEViewSink::TitleChange(BSTR text) {}
void IEViewSink::PropertyChange(BSTR text) {}
void IEViewSink::BeforeNavigate2(IDispatch* pDisp,VARIANT* url,VARIANT* flags, VARIANT* targetFrameName,
								VARIANT* postData, VARIANT* headers, VARIANT_BOOL* cancel) {
	int i = (int)wcslen(url->bstrVal);
	char *tTemp = new char[i+1];
	WideCharToMultiByte(CP_ACP, 0, url->bstrVal, -1, tTemp, i+1, NULL, NULL);
#ifndef GECKO
	if (strcmp(tTemp, "about:blank")) {
//		if (smileyWindow==NULL) {
			CallService(MS_UTILS_OPENURL, (WPARAM) 1, (LPARAM) tTemp);
//		} else {
	//		smileyWindow->choose(tTemp);
	//	}
		*cancel = VARIANT_TRUE;
	}
#endif
	delete tTemp;
}

void IEViewSink::NewWindow2(IDispatch** ppDisp, VARIANT_BOOL* cancel) {}
void IEViewSink::NavigateComplete(IDispatch* pDisp, VARIANT* url) {}
void IEViewSink::DocumentComplete(IDispatch* pDisp, VARIANT* url) {}
void IEViewSink::OnQuit() {}
void IEViewSink::OnVisible(VARIANT_BOOL visible) {}
void IEViewSink::OnToolBar(VARIANT_BOOL visible) {}
void IEViewSink::OnMenuBar(VARIANT_BOOL visible) {}
void IEViewSink::OnStatusBar(VARIANT_BOOL visible) {}
void IEViewSink::OnFullScreen(VARIANT_BOOL visible) {}
void IEViewSink::OnTheaterMode(VARIANT_BOOL visible) {}
void IEViewSink::WindowSetResizable(VARIANT_BOOL visible) {}
void IEViewSink::WindowSetLeft(long val) {}
void IEViewSink::WindowSetTop(long val) {}
void IEViewSink::WindowSetWidth(long val) {}
void IEViewSink::WindowSetHeight(long val) {}
void IEViewSink::WindowClosing(VARIANT_BOOL isChildWindow, VARIANT_BOOL* cancel) {}
void IEViewSink::ClientToHostWindow(long *cx, long *cy) {}
void IEViewSink::SetSecureLockIcon(long val) {}
void IEViewSink::FileDownload(VARIANT_BOOL* cancel) {}


#ifdef GECKO

static void __cdecl StartThread(void *vptr) {
	IEView *iev = (IEView *) vptr;
	iev->waitWhileBusy();
	return;
}
#endif

void IEView::waitWhileBusy() {
	VARIANT_BOOL busy;
	pWebBrowser->get_Busy(&busy);
	while (busy == VARIANT_TRUE) {
		Sleep(10);
		pWebBrowser->get_Busy(&busy);
	}
	PostMessage(hwnd, WM_WAITWHILEBUSY, 0, 0);
}

void IEView::setBorder() {
	LONG style = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
	LONG oldStyle = style;
	if (Options::getGeneralFlags() & Options::GENERAL_NO_BORDER) {
#ifndef GECKO
		style &= ~(WS_EX_STATICEDGE);
#endif
	} else {
		style |= (WS_EX_STATICEDGE);
	}
	if (oldStyle != style) {
		SetWindowLongPtr(hwnd,GWL_EXSTYLE,style);
		SetWindowPos(getHWND(), NULL, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
	}
//	RedrawWindow(hwnd, NULL, NULL, RDW_INVALIDATE | RDW_FRAME | RDW_UPDATENOW);
}

IEView::IEView(HWND parent, HTMLBuilder* builder, int x, int y, int cx, int cy) {
	MSG msg;
	IOleObject*   pOleObject = NULL;
	IOleInPlaceObject *pOleInPlace;
	isContactSet = false;
	this->parent = parent;
	this->builder = builder;
	prev = next = NULL;
	hwnd = NULL;
	sink = NULL;
	pWebBrowser = NULL;
	m_pConnectionPoint = NULL;
	m_cRef = 0;
	selectedText = NULL;
	getFocus = false;
	rcClient.left = x;
	rcClient.top = y;
	rcClient.right = x + cx;
	rcClient.bottom = y + cy;
#ifdef GECKO
	if (SUCCEEDED(CoCreateInstance(CLSID_MozillaBrowser, NULL, CLSCTX_INPROC, IID_IWebBrowser2, (LPVOID*)&pWebBrowser))) {
#else
	if (SUCCEEDED(CoCreateInstance(CLSID_WebBrowser, NULL, CLSCTX_INPROC, IID_IWebBrowser2, (LPVOID*)&pWebBrowser))) {
#endif
//		pWebBrowser->put_RegisterAsBrowser(VARIANT_FALSE);
		if (SUCCEEDED(pWebBrowser->QueryInterface(IID_IOleObject, (void**)&pOleObject))) {
			pOleObject->SetClientSite(this);
			pOleObject->DoVerb(OLEIVERB_INPLACEACTIVATE, &msg, this, 0, this->parent, &rcClient);
			pOleObject->Release();
		} else {
			MessageBoxA(NULL,"IID_IOleObject failed.","RESULT",MB_OK);
		}

		if (SUCCEEDED(pWebBrowser->QueryInterface(IID_IOleInPlaceObject, (void**)&pOleInPlace))) {
			pOleInPlace->GetWindow(&hwnd);
			pOleInPlace->Release();
		} else {
			MessageBoxA(NULL,"IID_IOleInPlaceObject failed.","RESULT",MB_OK);
		}

		setBorder();
		IConnectionPointContainer* pCPContainer;
		// Step 1: Get a pointer to the connection point container.
		if (SUCCEEDED(pWebBrowser->QueryInterface(IID_IConnectionPointContainer,
											(void**)&pCPContainer))) {
		// m_pConnectionPoint is defined like this:
		// Step 2: Find the connection point.
			if (SUCCEEDED(pCPContainer->FindConnectionPoint(DIID_DWebBrowserEvents2,
												 &m_pConnectionPoint))) {
			 // Step 3: Advise the connection point that you
			 // want to sink its events.
				sink = new IEViewSink(this);
			 	if (FAILED(m_pConnectionPoint->Advise((IUnknown *)sink, &m_dwCookie)))	 {
					MessageBoxA(NULL, "Failed to Advise", "C++ Event Sink", MB_OK);
			 	}
			}
			pCPContainer->Release();
		}
#ifndef GECKO
		setMainWndProc((WNDPROC)SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR) IEViewWindowProcedure));
#else
//		setMainWndProc((WNDPROC)SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR) MozillaWindowProcedure));
#endif
	}
	EnterCriticalSection(&mutex);
	next = list;
	if (next != NULL) {
		next->prev = this;
	}
	list = this;
	LeaveCriticalSection(&mutex);
//	clear();
	pWebBrowser->put_RegisterAsDropTarget(VARIANT_FALSE);
}

IEView::~IEView() {
	IOleObject*   pOleObject = NULL;
	EnterCriticalSection(&mutex);
	if (list == this) {
		list = next;
	} else if (prev!=NULL) {
		prev->next = next;
	}
	if (next != NULL) {
		next->prev = prev;
	}
	prev = NULL;
	next = NULL;
	LeaveCriticalSection(&mutex);
	if (SUCCEEDED(pWebBrowser->QueryInterface(IID_IOleObject, (void**)&pOleObject))) {
		pOleObject->SetClientSite(NULL);
		pOleObject->Release();
	} else {
		MessageBoxA(NULL,"IID_IOleObject failed.","RESULT",MB_OK);
	}
	if (builder != NULL) {
		delete builder;
		builder = NULL;
	}
	if (m_pConnectionPoint != NULL) {
		m_pConnectionPoint->Unadvise(m_dwCookie);
		m_pConnectionPoint->Release();
	}
	if (sink != NULL) {
		delete sink;
	}
	if (selectedText != NULL) {
		delete 	selectedText;
	}
#ifndef GECKO
	pWebBrowser->Release();
#endif
	DestroyWindow(hwnd);
}

void IEView::init() {
	if (isInited) return;
	isInited = true;
	InitializeCriticalSection(&mutex);
	if (FAILED(OleInitialize(NULL))) {
		MessageBoxA(NULL,"OleInitialize failed.","ERROR",MB_OK);
	}
}

void IEView::release() {
	EnterCriticalSection(&mutex);
	while (list != NULL) {
		delete list;
	}
	LeaveCriticalSection(&mutex);
	DeleteCriticalSection(&mutex);
}

IEView* IEView::get(HWND hwnd) {
	IEView *ptr;
	if (list == NULL) return NULL;
	EnterCriticalSection(&mutex);
	for (ptr = list; ptr !=NULL; ptr=ptr->next) {
		if (ptr->hwnd == hwnd) {
			break;
		}
	}
	LeaveCriticalSection(&mutex);
	return ptr;
}

void IEView::setMainWndProc(WNDPROC wndProc) {
	mainWndProc = wndProc;
}

WNDPROC IEView::getMainWndProc() {
	return mainWndProc;
}

void IEView::setDocWndProc(WNDPROC wndProc) {
	docWndProc = wndProc;
}

WNDPROC IEView::getDocWndProc() {
	return docWndProc;
}

void IEView::setServerWndProc(WNDPROC wndProc) {
	serverWndProc = wndProc;
}

WNDPROC IEView::getServerWndProc() {
	return serverWndProc;
}

// IUnknown
STDMETHODIMP IEView::QueryInterface(REFIID riid, PVOID *ppv) {
	*ppv=NULL;
	if (IID_IUnknown==riid)
		*ppv=this;
	if (IID_IOleClientSite==riid)
		*ppv=(IOleClientSite*)this;//Unknown)m_pIOleClientSite;
	if (IID_IOleWindow==riid || IID_IOleInPlaceSite==riid)
		*ppv=(IOleInPlaceSite*)this;//m_pIOleIPSite;
	if (IID_IDocHostUIHandler==riid)
		*ppv=(IDocHostUIHandler*)this;//m_pIOleIPSite;
	if (IID_IInternetSecurityManager==riid)
		*ppv=(IInternetSecurityManager*)this;
	if (IID_IServiceProvider==riid) {
		*ppv=(IServiceProvider*)this;
	}
	if (NULL!=*ppv) {
		((LPUNKNOWN)*ppv)->AddRef();
		return NOERROR;
	}
	return E_NOINTERFACE;
}

STDMETHODIMP_(ULONG) IEView::AddRef(void) {
	++m_cRef;
	return m_cRef;
}

STDMETHODIMP_(ULONG) IEView::Release(void) {
	--m_cRef;
	return m_cRef;
}

// IDispatch
STDMETHODIMP IEView::GetTypeInfoCount(UINT *ptr) { return E_NOTIMPL; }
STDMETHODIMP IEView::GetTypeInfo(UINT iTInfo, LCID lcid, LPTYPEINFO* ppTInfo) { return S_OK; }
STDMETHODIMP IEView::GetIDsOfNames(REFIID riid, LPOLESTR* rgszNames, UINT cNames, LCID lcid, DISPID* rgDispId) { return S_OK; }

STDMETHODIMP IEView::Invoke(DISPID dispIdMember, REFIID riid, LCID lcid , WORD wFlags,
							DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO*pExcepInfo, UINT*puArgErr) {
 	switch (dispIdMember) {
		case  DISPID_AMBIENT_DLCONTROL:
			break;
	}
	return DISP_E_MEMBERNOTFOUND;
}

// IOleWindow
STDMETHODIMP IEView::GetWindow(HWND *phwnd) {
	*phwnd = parent;
	return S_OK;
}

STDMETHODIMP IEView::ContextSensitiveHelp(BOOL fEnterMode) {
	return E_NOTIMPL;
}

// IOleInPlace
STDMETHODIMP IEView::CanInPlaceActivate(void) {
	return S_OK;
}

STDMETHODIMP IEView::OnInPlaceActivate(void) {
	m_bInPlaceActive = TRUE;
	return S_OK;
}

STDMETHODIMP IEView::OnUIActivate(void) {
	return E_NOTIMPL;
}

STDMETHODIMP IEView::GetWindowContext(IOleInPlaceFrame **ppFrame, IOleInPlaceUIWindow **ppDoc,
							LPRECT lprcPosRect, LPRECT lprcClipRect,
							LPOLEINPLACEFRAMEINFO lpFrameInfo) {

	lprcPosRect->left = rcClient.left;
	lprcPosRect->top = rcClient.top;
	lprcPosRect->right = rcClient.right;
	lprcPosRect->bottom = rcClient.bottom;
	lprcClipRect->left = rcClient.left;
	lprcClipRect->top = rcClient.top;
	lprcClipRect->right = rcClient.right;
	lprcClipRect->bottom = rcClient.bottom;
	return S_OK;
}

STDMETHODIMP IEView::Scroll(SIZE scrollExtant) {
	return E_NOTIMPL;
}

STDMETHODIMP IEView::OnUIDeactivate(BOOL fUndoable) {
	return E_NOTIMPL;
}

STDMETHODIMP IEView::OnInPlaceDeactivate( void) {
	m_bInPlaceActive = FALSE;
	return S_OK;
}

STDMETHODIMP IEView::DiscardUndoState( void) {
	return E_NOTIMPL;
}

STDMETHODIMP IEView::DeactivateAndUndo( void) {
	return E_NOTIMPL;
}

STDMETHODIMP IEView::OnPosRectChange(LPCRECT lprcPosRect) {
	return E_NOTIMPL;
}

// IOleClientSite
STDMETHODIMP IEView::SaveObject(void) {
	return E_NOTIMPL;
}
STDMETHODIMP IEView::GetMoniker(DWORD dwAssign, DWORD dwWhichMoniker, IMoniker **ppmk) {
	return E_NOTIMPL;
}
STDMETHODIMP IEView::GetContainer(IOleContainer **ppContainer) {
	return E_NOTIMPL;
}
STDMETHODIMP IEView::ShowObject(void) {
	return E_NOTIMPL;
}
STDMETHODIMP IEView::OnShowWindow(BOOL fShow) {
	return E_NOTIMPL;
}
STDMETHODIMP IEView::RequestNewObjectLayout(void) {
	return E_NOTIMPL;
}
// IDocHostUIHandler
STDMETHODIMP IEView::ShowContextMenu(DWORD dwID, POINT *ppt, IUnknown *pcmdTarget, IDispatch *pdispReserved) {
	IOleCommandTarget * pOleCommandTarget;
	IOleWindow * pOleWindow;
	HWND hSPWnd;
	if (builder == NULL) {
	//	return S_OK;
	}
#ifdef GECKO
	{
		return E_NOTIMPL;
/*
		HMENU hMenu = GetSubMenu(LoadMenu(hInstance, MAKEINTRESOURCE(IDR_CONTEXTMENU)),0);
		TranslateMenu(hMenu);
		if (dwID == 6) { // anchor
			EnableMenuItem(hMenu, ID_MENU_COPYLINK, MF_BYCOMMAND | MF_ENABLED);
		} else if (dwID == 5) { // text select
			EnableMenuItem(hMenu, ID_MENU_COPY, MF_BYCOMMAND | MF_ENABLED);
		} else if (dwID == 1) { // control (image)
			EnableMenuItem(hMenu, ID_MENU_SAVEIMAGE, MF_BYCOMMAND | MF_ENABLED);
		}
		if (builder!=NULL) {

		}
		int iSelection = TrackPopupMenu(hMenu,
										TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD,
										ppt->x,
										ppt->y,
										0,
										hwnd,
										(RECT*)NULL);
		DestroyMenu(hMenu);
		if (iSelection == ID_MENU_CLEARLOG) {
			clear(NULL);
		} else {
			SendMessage(hSPWnd, WM_COMMAND, iSelection, (LPARAM) NULL);
		}
*/
	}
#else
	if (SUCCEEDED(pcmdTarget->QueryInterface(IID_IOleCommandTarget, (void**)&pOleCommandTarget))) {
		if (SUCCEEDED(pOleCommandTarget->QueryInterface(IID_IOleWindow, (void**)&pOleWindow))) {
			pOleWindow->GetWindow(&hSPWnd);
			HMENU hMenu = GetSubMenu(LoadMenu(hInstance, MAKEINTRESOURCE(IDR_CONTEXTMENU)),0);
		 	TranslateMenu(hMenu);
			if (dwID == 5) { // anchor
				EnableMenuItem(hMenu, ID_MENU_COPYLINK, MF_BYCOMMAND | MF_ENABLED);
			} else if (dwID == 4) { // text select
				EnableMenuItem(hMenu, ID_MENU_COPY, MF_BYCOMMAND | MF_ENABLED);
			} else if (dwID == 1) { // control (image)
				EnableMenuItem(hMenu, ID_MENU_SAVEIMAGE, MF_BYCOMMAND | MF_ENABLED);
			}
			if (builder!=NULL) {

			}
		 	int iSelection = TrackPopupMenu(hMenu,
											TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD,
											ppt->x,
											ppt->y,
											0,
											hwnd,
											(RECT*)NULL);
			DestroyMenu(hMenu);
			if (iSelection == ID_MENU_CLEARLOG) {
				clear(NULL);
			} else {
				SendMessage(hSPWnd, WM_COMMAND, iSelection, (LPARAM) NULL);
			}
			pOleWindow->Release();
		}
		pOleCommandTarget->Release();
	}
#endif
	return S_OK;
}
STDMETHODIMP IEView::GetHostInfo(DOCHOSTUIINFO *pInfo) {
 	pInfo->dwFlags = DOCHOSTUIFLAG_NO3DBORDER;// | DOCHOSTUIFLAG_DISABLE_SCRIPT_INACTIVE;
	if (builder == NULL) {
		pInfo->dwFlags |= DOCHOSTUIFLAG_DIALOG;
	}
 	return S_OK;
}
STDMETHODIMP IEView::ShowUI(DWORD dwID, IOleInPlaceActiveObject *pActiveObject, IOleCommandTarget *pCommandTarget,
							IOleInPlaceFrame *pFrame, IOleInPlaceUIWindow *pDoc) {
 	return S_OK;
}

STDMETHODIMP IEView::HideUI(void) {return S_OK;}
STDMETHODIMP IEView::UpdateUI(void) {return S_OK;}
STDMETHODIMP IEView::EnableModeless(BOOL fEnable) { return E_NOTIMPL; }
STDMETHODIMP IEView::OnDocWindowActivate(BOOL fEnable) { return E_NOTIMPL; }
STDMETHODIMP IEView::OnFrameWindowActivate(BOOL fEnable) { return E_NOTIMPL; }
STDMETHODIMP IEView::ResizeBorder(LPCRECT prcBorder, IOleInPlaceUIWindow *pUIWindow, BOOL fRameWindow) {return E_NOTIMPL;}
STDMETHODIMP IEView::TranslateAccelerator(LPMSG lpMsg, const GUID *pguidCmdGroup, DWORD nCmdID) { return S_FALSE;}
STDMETHODIMP IEView::GetOptionKeyPath(LPOLESTR *pchKey, DWORD dw) { return E_NOTIMPL; }
STDMETHODIMP IEView::GetDropTarget(IDropTarget *pDropTarget, IDropTarget **ppDropTarget) {
	*ppDropTarget = NULL;
	return S_OK;
//	return E_NOTIMPL;
}

STDMETHODIMP IEView::GetExternal(IDispatch **ppDispatch) {
	*ppDispatch = NULL;
	return S_FALSE;
}
STDMETHODIMP IEView::TranslateUrl(DWORD dwTranslate, OLECHAR *pchURLIn, OLECHAR **ppchURLOut) { return E_NOTIMPL; }
STDMETHODIMP IEView::FilterDataObject(IDataObject *pDO, IDataObject **ppDORet) { return E_NOTIMPL; }


/* IServiceProvider */
STDMETHODIMP IEView::QueryService(REFGUID guidService, REFIID riid,	void** ppvObject) {
	if (guidService == SID_SInternetSecurityManager && riid == IID_IInternetSecurityManager) {
		return (HRESULT)this->QueryInterface(riid, ppvObject);
	} else {
		*ppvObject = NULL;
	}
	return E_NOINTERFACE;
}

/* IInternetSecurityManager */

STDMETHODIMP IEView::SetSecuritySite(IInternetSecurityMgrSite *pSite) {
	return INET_E_DEFAULT_ACTION;
}

STDMETHODIMP IEView::GetSecuritySite(IInternetSecurityMgrSite **ppSite) {
	return INET_E_DEFAULT_ACTION;
}

STDMETHODIMP IEView::MapUrlToZone(LPCWSTR pwszUrl, DWORD *pdwZone, DWORD dwFlags) {
	if (pdwZone!=NULL && pwszUrl!=NULL && !wcscmp(pwszUrl, L"about:blank")) {
		*pdwZone = URLZONE_LOCAL_MACHINE;
		return S_OK;
	}
	return INET_E_DEFAULT_ACTION;
}

STDMETHODIMP IEView::GetSecurityId(LPCWSTR pwszUrl, BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved) {
	return INET_E_DEFAULT_ACTION;
}

STDMETHODIMP IEView::ProcessUrlAction(LPCWSTR pwszUrl, DWORD dwAction, BYTE *pPolicy, DWORD cbPolicy, BYTE *pContext, DWORD cbContext, DWORD dwFlags, DWORD dwReserved) {
	DWORD dwPolicy=URLPOLICY_ALLOW;
	if (pwszUrl!=NULL && !wcscmp(pwszUrl, L"about:blank")) {
//		char str[1024];
//		sprintf(str, "kod: %08X", dwAction);
//			MessageBox(NULL,str, "Security", MB_OK);
		if (dwAction <= URLACTION_ACTIVEX_MAX && dwAction >= URLACTION_ACTIVEX_MIN) {
			//dwPolicy = URLPOLICY_DISALLOW;
			//dwPolicy = URLPOLICY_ALLOW;
		} else if ((dwAction <= URLACTION_JAVA_MAX && dwAction >= URLACTION_JAVA_MIN) || URLACTION_HTML_JAVA_RUN == dwAction) {
			// dwPolicy = URLPOLICY_JAVA_PROHIBIT;
			return INET_E_DEFAULT_ACTION;
		} else if (dwAction <= URLACTION_SCRIPT_MAX && dwAction >= URLACTION_SCRIPT_MIN) {
			//dwPolicy = URLPOLICY_DISALLOW;
			//dwPolicy = URLPOLICY_ALLOW;
		} else if (dwAction <= URLACTION_HTML_MIN && dwAction >= URLACTION_HTML_MAX) {
			//dwPolicy = URLPOLICY_DISALLOW;
			//dwPolicy = URLPOLICY_ALLOW;
		} else {
			return INET_E_DEFAULT_ACTION;
		}
		if ( cbPolicy >= sizeof (DWORD)) {
			*(DWORD*) pPolicy = dwPolicy;
			return S_OK;
		} else {
			return S_FALSE;
		}
	}
	return INET_E_DEFAULT_ACTION;
}

STDMETHODIMP IEView::QueryCustomPolicy(LPCWSTR pwszUrl, REFGUID guidKey, BYTE **ppPolicy, DWORD *pcbPolicy, BYTE *pContext, DWORD cbContext, DWORD dwReserved) {
	return INET_E_DEFAULT_ACTION;
}

STDMETHODIMP IEView::SetZoneMapping(DWORD dwZone, LPCWSTR lpszPattern, DWORD dwFlags) {
	return INET_E_DEFAULT_ACTION;
}

STDMETHODIMP IEView::GetZoneMappings(DWORD dwZone, IEnumString **ppenumString, DWORD dwFlags) {
	return INET_E_DEFAULT_ACTION;
}


IHTMLDocument2 *IEView::getDocument() {
	HRESULT hr = S_OK;
	IHTMLDocument2 *document = NULL;
	IDispatch *dispatch = NULL;
	if (SUCCEEDED(pWebBrowser->get_Document(&dispatch)) && (dispatch != NULL)) {
		hr = dispatch->QueryInterface(IID_IHTMLDocument2, (void **)&document);
		dispatch->Release();
	}
	return document;
}

void IEView::setWindowPos(int x, int y, int cx, int cy) {
	rcClient.left = x;
	rcClient.top = y;
	rcClient.right = cx;
	rcClient.bottom = cy;//y + cy;
 	if (builder == NULL) {
		//scrollToTop();
	} else {
//		scrollToBottomSoft();
	}
	SetWindowPos(getHWND(), HWND_TOP, x, y, cx, cy, 0);
	/*
	IOleInPlaceObject * inPlaceObject;
	if (SUCCEEDED(pWebBrowser->QueryInterface(IID_IOleInPlaceObject, (void **)&inPlaceObject))) {
		inPlaceObject->SetObjectRects(&rcClient, &rcClient);
		inPlaceObject->Release();
	}
	*/
	if (builder == NULL) {
		//scrollToTop();
	} else {
//		scrollToBottomSoft();
	}
}

void IEView::scrollToTop() {
	IHTMLDocument2 *document = getDocument();
	if (document != NULL) {
		IHTMLWindow2* pWindow = NULL;
		if (SUCCEEDED(document->get_parentWindow( &pWindow )) && pWindow != NULL) {
			pWindow->scrollBy( -0x01FFFFFF, -0x01FFFFFF );
			pWindow->Release();
		}
		document->Release();
	}
}

void IEView::scrollToBottomSoft() {
	IHTMLDocument2 *document = getDocument();
	if (document != NULL) {
		IHTMLWindow2* pWindow = NULL;
		if (SUCCEEDED(document->get_parentWindow( &pWindow )) && (pWindow != NULL)) {
			pWindow->scrollBy( -0x01FFFFFF, 0x01FFFFFF );
			pWindow->Release();
		}
		document->Release();
	}
}

void IEView::scrollToBottom() {/*
	IHTMLDocument2 *document = getDocument();
	if (document != NULL) {
		wchar_t *p = NULL;
		if (SUCCEEDED(document->get_readyState(&p))) {
			int licznik = 0;
			do {
				if (FAILED(document->get_readyState(&p))) {
					break;
				}
				licznik++;
				if (licznik == 1) break;
				Sleep(10);
			} while (!wcscmp(p, L"loading"));
		}
		IHTMLWindow2* pWindow = NULL;
		if (SUCCEEDED(document->get_parentWindow( &pWindow )) && pWindow != NULL) {
			pWindow->scrollBy( 0, 0x01FFFFFF );
		}
		document->Release();
	}*/

	IHTMLDocument2 *document = getDocument();
	if (document != NULL) {
		IHTMLElementCollection *collection;
		IHTMLElement *element;
		IDispatch *dispatch;
		if (SUCCEEDED(document->get_all(&collection)) && (collection != NULL)) {
			long len;
			if (SUCCEEDED(collection->get_length(&len))) {
				VARIANT	variant;
				variant.vt = VT_I4;
				variant.lVal = len-1;
				if (SUCCEEDED(collection->item(variant, variant, &dispatch)) && (dispatch != NULL)) {
					if (SUCCEEDED(dispatch->QueryInterface(IID_IHTMLElement,(void**)&element)) && (element != NULL)) {
 						variant.vt = VT_BOOL;
						variant.boolVal = VARIANT_FALSE;
						if (SUCCEEDED(element->scrollIntoView(variant))) {
							}
						element->Release();
					}
					dispatch->Release();
				}
			}
			collection->Release();
		}
		IHTMLWindow2* pWindow = NULL;
		if (SUCCEEDED(document->get_parentWindow( &pWindow )) && (pWindow != NULL)) {
			pWindow->scrollBy( -0x0000FFFF, 0x0000FFFF );
			pWindow->Release();
		}
		document->Release();
	}
}

void IEView::write(const wchar_t *text) {
	IHTMLDocument2 *document = getDocument();
	if (document != NULL) {
		SAFEARRAY *safe_array = SafeArrayCreateVector(VT_VARIANT,0,1);
		if (safe_array != NULL) {
			VARIANT	*variant;
			BSTR bstr;
			SafeArrayAccessData(safe_array,(LPVOID *)&variant);
			variant->vt = VT_BSTR;
			variant->bstrVal = bstr = SysAllocString(text);
			SafeArrayUnaccessData(safe_array);
			document->write(safe_array);
			//SysFreeString(bstr); -> SafeArrayDestroy should be enough
			SafeArrayDestroy(safe_array);
		}
		document->Release();
	}
}

void IEView::write(const char *text) {
	int textLen = (int)strlen(text) + 1;
	wchar_t *wcsTemp = new wchar_t[textLen];
	MultiByteToWideChar(CP_UTF8, 0, text, -1, wcsTemp, textLen);
	write(wcsTemp);
	delete [] wcsTemp;
}

void IEView::writef(const char *fmt, ...) {
	char *str;
	va_list vararg;
	int strsize;
	va_start(vararg, fmt);
	str = (char *) malloc(strsize=2048);
	while (_vsnprintf(str, strsize, fmt, vararg) == -1)
		str = (char *) realloc(str, strsize+=2048);
	va_end(vararg);
	write(str);
	free(str);
}

void IEView::navigate(const char *url) {
	int textLen = (int)strlen(url) + 1;
	WCHAR *tTemp = new WCHAR[textLen];
	MultiByteToWideChar(CP_ACP, 0, url, -1, tTemp, textLen);
	pWebBrowser->Navigate(tTemp, NULL, NULL, NULL, NULL);
	delete tTemp;
}

void IEView::navigate(const wchar_t *url) {
	pWebBrowser->Navigate((WCHAR *)url, NULL, NULL, NULL, NULL);
}

void IEView::documentClose() {

#ifdef GECKO
	/*
	IHTMLDocument2 *document = getDocument();
	if (document != NULL) {
		//write("</body></html>");
		document->close();
		document->Release();
	}
	*/
#endif
}

void IEView::appendEventOld(IEVIEWEVENT *event) {
	if (clearRequired) {
		clear(event);
	}
	if (builder!=NULL) {
		builder->appendEventOld(this, event);
	}
	getFocus = false;
}

void IEView::appendEvent(IEVIEWEVENT *event) {
	if (clearRequired) {
		clear(event);
	}
	if (event->eventData == NULL) {return; }

	if (builder!=NULL) {
		builder->appendEventNew(this, event);
	}
	getFocus = false;
}

void IEView::clear(IEVIEWEVENT *event) {
#ifdef GECKO
	pWebBrowser->Navigate(L"about:blank", NULL, NULL, NULL, NULL);
	Utils::forkThread((void (__cdecl *)(void *))StartThread, 0, (void *) this);
	MSG msg;
	BOOL bRet;
	while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0) {
		if (bRet == -1) {
			// handle the error and possibly exit
		} else {
			if (msg.message == WM_WAITWHILEBUSY) {
				break;
			} else {
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}
	}
	{
		IHTMLDocument2 *document = getDocument();
		if (document != NULL) {
			document->close();
			VARIANT		open_name;
			VARIANT		open_features;
			VARIANT		open_replace;
			IDispatch	*open_window	= NULL;
			VariantInit(&open_name);
			open_name.vt      = VT_BSTR;
			open_name.bstrVal = SysAllocString(L"_self");
			VariantInit(&open_features);
			VariantInit(&open_replace);

			HRESULT hr = document->open(SysAllocString(L"text/html"),
								open_name,
									open_features,
									open_replace,
									&open_window);
			if (hr == S_OK) {
			//	pWebBrowser->Refresh();
			}
			if (open_window != NULL) {
				open_window->Release();
			}
			document->Release();
		}
		if (builder!=NULL) {
			builder->clear(this, event);
		}
		clearRequired = false;
		getFocus = false;

	}
	return;
#endif
	IHTMLDocument2 *document = getDocument();
	if (document == NULL) {
		pWebBrowser->Navigate(L"about:blank", NULL, NULL, NULL, NULL);
		HRESULT hr = S_OK;
		IHTMLDocument2 *document = NULL;
		while ((document == NULL) && (hr == S_OK)) {
			Sleep(0);
			IDispatch *dispatch = NULL;
			if (SUCCEEDED(pWebBrowser->get_Document(&dispatch)) && (dispatch != NULL)) {
				hr = dispatch->QueryInterface(IID_IHTMLDocument2, (void **)&document);
				dispatch->Release();
			}
		}
		if (document != NULL) {
			document->Release();
		}
	} else {
		document->close();
		VARIANT		open_name;
		VARIANT		open_features;
		VARIANT		open_replace;
		IDispatch	*open_window	= NULL;
		VariantInit(&open_name);
		open_name.vt      = VT_BSTR;
		open_name.bstrVal = SysAllocString(L"_self");
		VariantInit(&open_features);
		VariantInit(&open_replace);

		HRESULT hr = document->open(SysAllocString(L"text/html"),
							open_name,
								open_features,
								open_replace,
								&open_window);
		if (hr == S_OK) {
		//	pWebBrowser->Refresh();
		}
		if (open_window != NULL) {
			open_window->Release();
		}
		document->Release();
	}
	if (builder!=NULL) {
		builder->clear(this, event);
	}
	clearRequired = false;
	getFocus = false;
	setBorder();
}

void* IEView::getSelection(IEVIEWEVENT *event) {
	if (selectedText!=NULL) delete selectedText;
	selectedText = getSelection();
	if (selectedText == NULL || wcslen(selectedText)== 0) return NULL;
	if (event->dwFlags & IEEF_NO_UNICODE) {
		int cp = CP_ACP;
		if (event->cbSize >= IEVIEWEVENT_SIZE_V2) {
			cp = event->codepage;
		}
		char *str = Utils::convertToString(selectedText, cp);
		delete selectedText;
		selectedText = (BSTR) str;
	}
	return (void *)selectedText;
}


HWND IEView::getHWND() {
	return hwnd;
}


void IEView::setContact(HANDLE hContact) {
	this->hContact = hContact;
	isContactSet = true;
}

void IEView::translateAccelerator(UINT uMsg, WPARAM wParam, LPARAM lParam) {
	IOleInPlaceActiveObject* pIOIPAO;
	if (SUCCEEDED(pWebBrowser->QueryInterface(IID_IOleInPlaceActiveObject, (void**)&pIOIPAO))) {
		MSG msg;
		msg.message = uMsg;
		msg.wParam = wParam;
		msg.lParam = lParam;
		pIOIPAO->TranslateAccelerator(&msg);
		pIOIPAO->Release();
	}
}

/**
 * Returns the selected text within the active document
 **/
BSTR IEView::getSelection() {
	BSTR text = NULL;
	IHTMLDocument2 *document = getDocument();
	if (document != NULL) {
		IHTMLSelectionObject *pSelection = NULL;
		if (SUCCEEDED(document->get_selection( &pSelection )) && pSelection != NULL) {
			IDispatch *pDisp = NULL;
			if (SUCCEEDED(pSelection->createRange( &pDisp )) &&  pDisp != NULL) {
				IHTMLTxtRange *pRange = NULL;
				if (SUCCEEDED(pDisp->QueryInterface(IID_IHTMLTxtRange, (void**)&pRange))) {
					if (SUCCEEDED(pRange->get_text(&text))) {
						text = Utils::dupString(text);
					}
					pRange->Release();
				}
				pDisp->Release();
			}
			pSelection->Release();
		}
		document->Release();
	}
	return text;
}


/**
 * Returns the destination url (href) of the given anchor element (or parent anchor element)
 **/
BSTR IEView::getHrefFromAnchor(IHTMLElement *element) {
	if (element != NULL) {
		IHTMLAnchorElement * pAnchor;
		if (SUCCEEDED(element->QueryInterface(IID_IHTMLAnchorElement, (void**)&pAnchor)) && (pAnchor!=NULL)) {
			VARIANT	variant;
			BSTR url;
			if (SUCCEEDED(element->getAttribute(L"href", 2, &variant) && variant.vt == VT_BSTR)) {
				url = Utils::dupString(variant.bstrVal);
				SysFreeString(variant.bstrVal);
			}
			//pAnchor->get_href( &url );
//			if (url!=NULL) {
//			url2 = Utils::dupString(url);
//			SysFreeString(url);
	//			url = url2;
	//		}
			pAnchor->Release();
			return url;
		} else {
			IHTMLElement * parent;
			if (SUCCEEDED(element->get_parentElement(&parent)) && (parent!=NULL)) {
				BSTR url = getHrefFromAnchor(parent);
				parent->Release();
				return url;
			}
		}
	}
	return NULL;
}

bool IEView::mouseActivate() {
	if (GetFocus() != hwnd) {
		getFocus = true;
	}
	return false;
}

bool IEView::mouseClick(POINT pt) {
	bool result = false;
	if (GetFocus() != hwnd) {
		getFocus = true;
	}
	IHTMLDocument2 *document = getDocument();
	if (document != NULL) {
		IHTMLElement *element;
		if (SUCCEEDED(document->elementFromPoint( pt.x, pt.y, &element ))&& element!=NULL) {
//		IHTMLAnchorElement * pAnchor;
//			if (SUCCEEDED(element->QueryInterface(IID_IHTMLAnchorElement, (void**)&pAnchor)) && (pAnchor!=NULL)) {
//				element->click();
//				result = true;
//				pAnchor->Release();
//			}
			BSTR url = getHrefFromAnchor(element);
			if (url != NULL) {
				if ((GetKeyState(VK_SHIFT) & 0x8000) && !(GetKeyState(VK_CONTROL) & 0x8000)
				&& !(GetKeyState(VK_MENU) & 0x8000)) {
					SendMessage(GetParent(hwnd), WM_COMMAND, IDCANCEL, 0);
				}
				int i = (int)wcslen(url);
				char *tTemp = new char[i+1];
				WideCharToMultiByte(CP_ACP, 0, url, -1, tTemp, i+1, NULL, NULL);
				CallService(MS_UTILS_OPENURL, (WPARAM) 1, (LPARAM) tTemp);
				delete tTemp;
				delete url;
				result = true;
			}
			element->Release();
		}
		document->Release();
	}
	return result;
}

bool IEView::setFocus(HWND prevFocus) {
	if (GetFocus() != hwnd && !getFocus) { // && IsChild(prevFocus, hwnd
		SetFocus(GetParent(getHWND()));
//		SetFocus(prevFocus);
		return true;
	}
	getFocus = false;
	return false;
}

void IEView::saveDocument() {
	IHTMLDocument2 *document = getDocument();
	if (document != NULL) {
		BSTR bCmd = SysAllocString(L"SaveAs");
		VARIANT_BOOL vb;
		VARIANT      vValue;
		vValue.vt = VT_BOOL;
		vValue.boolVal = TRUE;
		document->execCommand(bCmd, VARIANT_FALSE, vValue, &vb);
		SysFreeString(bCmd);
		document->Release();
	}
}

void IEView::navigate(IEVIEWNAVIGATE * nav) {
	if (nav->dwFlags & IENF_UNICODE) {
		navigate(nav->urlW);
	} else {
		navigate(nav->url);
	}
}