| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271 |
- /////////////////////////////////////////////////////////////////////////////
- // SimpleBrowser.cpp: Web browser control
- /////////////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "comdef.h"
- #include "mshtml.h"
- #include "mshtmcid.h"
- #include "mshtmhst.h"
- #include "exdispid.h"
- #include "SimpleBrowser.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define Unused(parameter) parameter // avoid compile warnings
- // about unused parameters
- /////////////////////////////////////////////////////////////////////////////
- // Construction and creation
- /////////////////////////////////////////////////////////////////////////////
- SimpleBrowser::SimpleBrowser()
- : _Browser(NULL),
- _BrowserDispatch(NULL),
- _Ready(false),
- _Content(_T(""))
- {
- }
- SimpleBrowser::~SimpleBrowser()
- {
- // release browser interfaces
- if (_Browser != NULL) {
- _Browser->Release();
- _Browser = NULL;
- }
- if (_BrowserDispatch != NULL) {
- _BrowserDispatch->Release();
- _BrowserDispatch = NULL;
- }
- }
- // Standard create
- BOOL SimpleBrowser::Create(DWORD dwStyle,
- const RECT& rect,
- CWnd* pParentWnd,
- UINT nID)
- {
- BOOL results = TRUE;
- _Ready = false;
-
- _Browser = NULL;
- // create this window
- CWnd *window = this;
- results = window->Create(AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW),
- NULL,
- dwStyle,
- rect,
- pParentWnd,
- nID);
- if (!results) return FALSE;
- // create browser control window as child of this window;
- // this window sinks events from control
- CRect browser_window_rect(0,0,(rect.right - rect.left),(rect.bottom - rect.top));
- results = _BrowserWindow.CreateControl(CLSID_WebBrowser,
- NULL,
- (WS_VISIBLE | WS_TABSTOP),
- browser_window_rect,
- this,
- AFX_IDW_PANE_FIRST);
- if (!results) {
- DestroyWindow();
- return FALSE;
- }
- // get control interfaces
- LPUNKNOWN unknown = _BrowserWindow.GetControlUnknown();
- HRESULT hr = unknown->QueryInterface(IID_IWebBrowser2,(void **)&_Browser);
- if (SUCCEEDED(hr)) {
- hr = unknown->QueryInterface(IID_IDispatch,(void **)&_BrowserDispatch);
- }
- if (!SUCCEEDED(hr)) {
- _BrowserWindow.DestroyWindow();
- DestroyWindow();
- return FALSE;
- }
- // navigate to initial blank page
- if (_Browser != NULL) {
- Navigate(_T("about:blank"));
- }
- return TRUE;
- }
- // Create in place of dialog control
- BOOL SimpleBrowser::CreateFromControl(CWnd *pParentWnd,UINT nID,DWORD dwStyle)
- {
- BOOL result = FALSE;
- ASSERT(pParentWnd != NULL);
- CWnd *control = pParentWnd->GetDlgItem(nID);
- if (control != NULL) {
- // get control location
- CRect rect;
- control->GetWindowRect(&rect);
- pParentWnd->ScreenToClient(&rect);
- // destroy control, since the browser will take its place
- control->DestroyWindow();
- // create browser
- result = Create(dwStyle,
- rect,
- pParentWnd,
- nID);
- }
- return result;
- }
- // Destruction
- void SimpleBrowser::PostNcDestroy()
- {
- // release browser interfaces
- if (_Browser != NULL) {
- _Browser->Release();
- _Browser = NULL;
- }
- if (_BrowserDispatch != NULL) {
- _BrowserDispatch->Release();
- _BrowserDispatch = NULL;
- }
-
- _Ready = false;
- _Content = _T("");
-
- CWnd::PostNcDestroy();
- }
- /////////////////////////////////////////////////////////////////////////////
- // Controls
- /////////////////////////////////////////////////////////////////////////////
- // Navigate to URL
- void SimpleBrowser::Navigate(LPCTSTR URL)
- {
- _Ready = false;
- _Content = _T("");
- if (_Browser != NULL) {
- CString url(URL);
- _variant_t flags(0L,VT_I4);
- _variant_t target_frame_name("");
- _variant_t post_data("");
- _variant_t headers("");
- _Browser->Navigate(url.AllocSysString(),
- &flags,
- &target_frame_name,
- &post_data,
- &headers);
- }
- }
- // Navigate to HTML document in resource
- void SimpleBrowser::NavigateResource(int resource_ID)
- {
- if (_Browser != NULL) {
- CString resource_string;
- // load HTML document from resource
- HRSRC resource_handle = FindResource(AfxGetResourceHandle(),
- MAKEINTRESOURCE(resource_ID),
- RT_HTML);
- if (resource_handle != NULL) {
- HGLOBAL resource = LoadResource(AfxGetResourceHandle(),
- resource_handle);
- if (resource != NULL) {
- LPVOID resource_memory = LockResource(resource);
- if (resource_memory != NULL) {
- DWORD resource_size = SizeofResource(AfxGetResourceHandle(),
- resource_handle);
- // identify the resource document as MBCS (e.g. ANSI)
- // or UNICODE
- bool UNICODE_document = false;
- wchar_t *UNICODE_memory = (wchar_t *)resource_memory;
- int UNICODE_size = resource_size / sizeof(wchar_t);
- if (UNICODE_size >= 1) {
- // check for UNICODE byte order mark
- if (*UNICODE_memory == L'\xFEFF') {
- UNICODE_document = true;
- UNICODE_memory += 1;
- UNICODE_size -= 1;
- }
- // otherwise, check for UNICODE leading tag
- else if (UNICODE_size >= 5) {
- if ((UNICODE_memory[0] == L'<') &&
- (towupper(UNICODE_memory[1]) == L'H') &&
- (towupper(UNICODE_memory[2]) == L'T') &&
- (towupper(UNICODE_memory[3]) == L'M') &&
- (towupper(UNICODE_memory[4]) == L'L')) {
- UNICODE_document = true;
- }
- }
- // Note: This logic assumes that the UNICODE resource document is
- // in little-endian byte order, which would be typical for
- // any HTML document used as a resource in a Windows application.
- }
- // convert resource document if required
- #if !defined(UNICODE)
- if (UNICODE_document) {
- char *MBCS_buffer = resource_string.GetBufferSetLength(resource_size + 1);
- int MBCS_length = ::WideCharToMultiByte(CP_ACP,
- 0,
- UNICODE_memory,
- UNICODE_size,
- MBCS_buffer,
- resource_size + 1,
- NULL,
- NULL);
- resource_string.ReleaseBuffer(MBCS_length);
- }
- else {
- resource_string = CString((char *)resource_memory,resource_size);
- }
- #else
- if (UNICODE_document) {
- resource_string = CString(UNICODE_memory,UNICODE_size);
- }
- else {
- wchar_t *UNICODE_buffer = resource_string.GetBufferSetLength(resource_size + 1);
- int UNICODE_length = ::MultiByteToWideChar(CP_ACP,
- 0,
- (const char *)resource_memory,
- resource_size,
- UNICODE_buffer,
- (resource_size + 1));
- resource_string.ReleaseBuffer(UNICODE_length);
- }
- #endif
- }
- }
- }
- Clear();
- Write(resource_string);
- }
- }
- // Append string to current document
- void SimpleBrowser::Write(LPCTSTR string)
- {
- if (_Browser != NULL) {
- _Content.Append(string);
-
- _ContentWrite();
- }
- }
- void SimpleBrowser::Clear()
- {
- if (_Browser != NULL) {
- _Content = _T("");
- // get document interface
- IHTMLDocument2 *document = GetDocument();
- HRESULT hr = S_OK;
-
- if (document != NULL) {
- // close and re-open document to empty contents
- 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);
-
- hr = document->open(::SysAllocString(L"text/html"),
- open_name,
- open_features,
- open_replace,
- &open_window);
- if (hr == S_OK) {
- Refresh();
- }
- if (open_window != NULL) {
- open_window->Release();
- }
- ::VariantClear(&open_name);
-
- document->Release();
- document = NULL;
-
- }
-
- else {
- Navigate(_T("about:blank"));
- }
-
- }
- }
- // Navigation operations
- void SimpleBrowser::GoBack()
- {
- if (_Browser != NULL) {
- _Browser->GoBack();
- }
- }
- void SimpleBrowser::GoForward()
- {
- if (_Browser != NULL) {
- _Browser->GoForward();
- }
- }
- void SimpleBrowser::GoHome()
- {
- if (_Browser != NULL) {
- _Browser->GoHome();
- }
- }
- void SimpleBrowser::Refresh()
- {
- if (_Browser != NULL) {
- _Browser->Refresh();
- }
- }
- void SimpleBrowser::Stop()
- {
- if (_Browser != NULL) {
- _Browser->Stop();
- }
- }
- // Print contents
- void SimpleBrowser::Print(LPCTSTR header,LPCTSTR footer)
- {
- if (_Browser != NULL) {
- // construct two element SAFEARRAY;
- // first element is header string, second element is footer string
- HRESULT hr;
- VARIANT header_variant;
- VariantInit(&header_variant);
- V_VT(&header_variant) = VT_BSTR;
- V_BSTR(&header_variant) = CString(header).AllocSysString();
- VARIANT footer_variant;
- VariantInit(&footer_variant);
- V_VT(&footer_variant) = VT_BSTR;
- V_BSTR(&footer_variant) = CString(footer).AllocSysString();
- long index;
- SAFEARRAYBOUND parameter_array_bound[1];
- SAFEARRAY *parameter_array = NULL;
- parameter_array_bound[0].cElements = 2;
- parameter_array_bound[0].lLbound = 0;
- parameter_array = SafeArrayCreate(VT_VARIANT,1,parameter_array_bound);
- index = 0;
- hr = SafeArrayPutElement(parameter_array,&index,&header_variant);
- index = 1;
- hr = SafeArrayPutElement(parameter_array,&index,&footer_variant);
- VARIANT parameter;
- VariantInit(¶meter);
- V_VT(¶meter) = VT_ARRAY | VT_BYREF;
- V_ARRAY(¶meter) = parameter_array;
- // start printing browser contents
- hr = _Browser->ExecWB(OLECMDID_PRINT,OLECMDEXECOPT_DODEFAULT,¶meter,NULL);
- // release SAFEARRAY
- if (!SUCCEEDED(hr)) {
- VariantClear(&header_variant);
- VariantClear(&footer_variant);
- if (parameter_array != NULL) {
- SafeArrayDestroy(parameter_array);
- }
- }
- }
- }
- void SimpleBrowser::PrintPreview()
- {
- if (_Browser != NULL) {
- _Browser->ExecWB(OLECMDID_PRINTPREVIEW,OLECMDEXECOPT_DODEFAULT,NULL,NULL);
- }
- }
- // Miscellaneous
- bool SimpleBrowser::GetBusy()
- {
- bool busy = false;
- if (_Browser != NULL) {
- VARIANT_BOOL variant_bool;
- HRESULT hr = _Browser->get_Busy(&variant_bool);
- if (SUCCEEDED(hr)) {
- busy = (variant_bool == VARIANT_TRUE);
- }
- }
- return busy;
- }
- CString SimpleBrowser::GetLocationName()
- {
- CString location_name = _T("");
- if (_Browser != NULL) {
- BSTR location_name_BSTR = NULL;
- HRESULT hr = _Browser->get_LocationName(&location_name_BSTR);
- if (SUCCEEDED(hr)) {
- location_name = location_name_BSTR;
- }
- ::SysFreeString(location_name_BSTR);
- }
- return location_name;
- }
- CString SimpleBrowser::GetLocationURL()
- {
- CString location_URL = _T("");
- if (_Browser != NULL) {
- BSTR location_URL_BSTR = NULL;
- HRESULT hr = _Browser->get_LocationURL(&location_URL_BSTR);
- if (SUCCEEDED(hr)) {
- location_URL = location_URL_BSTR;
- }
- ::SysFreeString(location_URL_BSTR);
- }
- return location_URL;
- }
- READYSTATE SimpleBrowser::GetReadyState()
- {
- READYSTATE readystate = READYSTATE_UNINITIALIZED;
- if (_Browser != NULL) {
- _Browser->get_ReadyState(&readystate);
- }
- return readystate;
- }
- bool SimpleBrowser::GetSilent()
- {
- bool silent = false;
- if (_Browser != NULL) {
- VARIANT_BOOL silent_variant;
- HRESULT hr = _Browser->get_Silent(&silent_variant);
- if (SUCCEEDED(hr)) {
- silent = (silent_variant == VARIANT_TRUE);
- }
- }
- return silent;
- }
- void SimpleBrowser::PutSilent(bool silent)
- {
- if (_Browser != NULL) {
- VARIANT_BOOL silent_variant;
- if (silent) silent_variant = VARIANT_TRUE;
- else silent_variant = VARIANT_FALSE;
- _Browser->put_Silent(silent_variant);
- }
- }
- IHTMLDocument2 *SimpleBrowser::GetDocument()
- {
- IHTMLDocument2 *document = NULL;
-
- if (_Browser != NULL) {
-
- // get browser document's dispatch interface
- IDispatch *document_dispatch = NULL;
- HRESULT hr = _Browser->get_Document(&document_dispatch);
- if (SUCCEEDED(hr) && (document_dispatch != NULL)) {
- // get the actual document interface
- hr = document_dispatch->QueryInterface(IID_IHTMLDocument2,
- (void **)&document);
- // release dispatch interface
-
- document_dispatch->Release();
-
- }
-
- }
-
- return document;
- }
- /////////////////////////////////////////////////////////////////////////////
- // Message handlers
- /////////////////////////////////////////////////////////////////////////////
- BEGIN_MESSAGE_MAP(SimpleBrowser,CWnd)
- //{{AFX_MSG_MAP(SimpleBrowser)
- ON_WM_SIZE()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- BOOL SimpleBrowser::PreTranslateMessage(MSG *pMsg)
- {
- BOOL result = FALSE;
- // translate keys correctly, especially Tab, Del, (Enter?)
-
- if (_Browser != NULL) {
-
- IOleInPlaceActiveObject* OleInPlaceActiveObject = NULL;
- HRESULT hr = _Browser->QueryInterface(IID_IOleInPlaceActiveObject, (void**)&OleInPlaceActiveObject);
- if (SUCCEEDED(hr) && (OleInPlaceActiveObject != NULL)) {
- hr = OleInPlaceActiveObject->TranslateAccelerator(pMsg);
- result = (hr == S_OK ? TRUE : FALSE);
- OleInPlaceActiveObject->Release();
- }
- }
- else {
- result = CWnd::PreTranslateMessage(pMsg);
- }
- return result;
- }
- // Resize control window as this window is resized
- void SimpleBrowser::OnSize(UINT nType, int cx, int cy)
- {
- CWnd::OnSize(nType, cx, cy);
- if (_Browser != NULL) {
- CRect rect(0,0,cx,cy);
- _BrowserWindow.MoveWindow(&rect);
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // Event handlers
- /////////////////////////////////////////////////////////////////////////////
- #ifndef DISPID_PRINTTEMPLATEINSTANTIATION
- #define DISPID_PRINTTEMPLATEINSTANTIATION 225
- #endif
- #ifndef DISPID_PRINTTEMPLATETEARDOWN
- #define DISPID_PRINTTEMPLATETEARDOWN 226
- #endif
- BEGIN_EVENTSINK_MAP(SimpleBrowser,CWnd)
- ON_EVENT(SimpleBrowser,AFX_IDW_PANE_FIRST,
- DISPID_BEFORENAVIGATE2,
- _OnBeforeNavigate2,
- VTS_DISPATCH VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PBOOL)
- ON_EVENT(SimpleBrowser,AFX_IDW_PANE_FIRST,
- DISPID_DOCUMENTCOMPLETE,
- _OnDocumentComplete,
- VTS_DISPATCH VTS_PVARIANT)
- ON_EVENT(SimpleBrowser,AFX_IDW_PANE_FIRST,
- DISPID_DOWNLOADBEGIN,
- _OnDownloadBegin,
- VTS_NONE)
- ON_EVENT(SimpleBrowser,AFX_IDW_PANE_FIRST,
- DISPID_PROGRESSCHANGE,
- _OnProgressChange,
- VTS_I4 VTS_I4)
- ON_EVENT(SimpleBrowser,AFX_IDW_PANE_FIRST,
- DISPID_DOWNLOADCOMPLETE,
- _OnDownloadComplete,
- VTS_NONE)
- ON_EVENT(SimpleBrowser,AFX_IDW_PANE_FIRST,
- DISPID_NAVIGATECOMPLETE2,
- _OnNavigateComplete2,
- VTS_DISPATCH VTS_PVARIANT)
- ON_EVENT(SimpleBrowser,AFX_IDW_PANE_FIRST,
- DISPID_STATUSTEXTCHANGE,
- _OnStatusTextChange,
- VTS_BSTR)
- ON_EVENT(SimpleBrowser,AFX_IDW_PANE_FIRST,
- DISPID_TITLECHANGE,
- _OnTitleChange,
- VTS_BSTR)
- ON_EVENT(SimpleBrowser,AFX_IDW_PANE_FIRST,
- DISPID_PRINTTEMPLATEINSTANTIATION,
- _OnPrintTemplateInstantiation,
- VTS_DISPATCH)
- ON_EVENT(SimpleBrowser,AFX_IDW_PANE_FIRST,
- DISPID_PRINTTEMPLATETEARDOWN,
- _OnPrintTemplateTeardown,
- VTS_DISPATCH)
- END_EVENTSINK_MAP()
- SimpleBrowser::Notification::Notification(HWND hwnd,UINT ID,NotificationType type)
- {
- hdr.hwndFrom = hwnd;
- hdr.idFrom = ID;
- hdr.code = type;
- URL = _T("");
- frame = _T("");
- post_data = NULL;
- post_data_size = 0;
- headers = _T("");
- progress = 0;
- progress_max = 0;
- text = _T("");
- }
- SimpleBrowser::Notification::~Notification()
- {
- }
- // Called before navigation begins; application may cancel if required
- void SimpleBrowser::_OnBeforeNavigate2(LPDISPATCH lpDisp,
- VARIANT FAR *URL,
- VARIANT FAR *Flags,
- VARIANT FAR *TargetFrameName,
- VARIANT FAR *PostData,
- VARIANT FAR *Headers,
- VARIANT_BOOL *Cancel)
- {
- Unused(Flags); // Note: flags value is reserved
- if (lpDisp == _BrowserDispatch) {
- CString URL_string;
- CString frame;
- unsigned char *post_data = NULL;
- int post_data_size = 0;
- CString headers;
- if ((URL != NULL) &&
- (V_VT(URL) == VT_BSTR)) {
- URL_string = V_BSTR(URL);
- }
- if ((TargetFrameName != NULL) &&
- (V_VT(TargetFrameName) == VT_BSTR)) {
- frame = V_BSTR(TargetFrameName);
- }
- if ((PostData != NULL) &&
- (V_VT(PostData) == (VT_VARIANT | VT_BYREF))) {
- VARIANT *PostData_variant = V_VARIANTREF(PostData);
- if ((PostData_variant != NULL) &&
- (V_VT(PostData_variant) != VT_EMPTY)) {
- SAFEARRAY *PostData_safearray = V_ARRAY(PostData_variant);
- if (PostData_safearray != NULL) {
- char *post_data_array = NULL;
- SafeArrayAccessData(PostData_safearray,(void HUGEP **)&post_data_array);
- long lower_bound = 1;
- long upper_bound = 1;
- SafeArrayGetLBound(PostData_safearray,1,&lower_bound);
- SafeArrayGetUBound(PostData_safearray,1,&upper_bound);
- post_data_size = (int)(upper_bound - lower_bound + 1);
- post_data = new unsigned char[post_data_size];
- memcpy(post_data,post_data_array,post_data_size);
- SafeArrayUnaccessData(PostData_safearray);
- }
- }
- }
- if ((Headers != NULL) &&
- (V_VT(Headers) == VT_BSTR)) {
- headers = V_BSTR(Headers);
- }
- bool cancel = OnBeforeNavigate2(URL_string,
- frame,
- post_data,post_data_size,
- headers);
- if (Cancel != NULL) {
- if (cancel) *Cancel = VARIANT_TRUE;
- else *Cancel = VARIANT_FALSE;
- }
- delete []post_data;
- }
- }
- bool SimpleBrowser::OnBeforeNavigate2(CString URL,
- CString frame,
- void *post_data,int post_data_size,
- CString headers)
- {
- bool cancel = false;
-
- CWnd *parent = GetParent();
-
- if (parent != NULL) {
- Notification notification(m_hWnd,GetDlgCtrlID(),BeforeNavigate2);
-
- notification.URL = URL;
- notification.frame = frame;
- notification.post_data = post_data;
- notification.post_data_size = post_data_size;
- notification.headers = headers;
- LRESULT result = parent->SendMessage(WM_NOTIFY,
- notification.hdr.idFrom,
- (LPARAM)¬ification);
-
- if (result) {
- cancel = true;
- }
- }
-
- return cancel;
- }
- bool SimpleBrowser::ParsePostData(CString post_data,
- CStringArray &names,
- CStringArray &values)
- {
- bool result = true;
-
- int size = 1;
-
- names.SetSize(size);
- values.SetSize(size);
-
- int offset = 0;
-
- bool parsing_name = true;
-
- CString hex(_T("0123456789ABCDEF"));
-
- while ((offset < post_data.GetLength()) && result) {
- if (post_data[offset] == _T('%')) {
- if ((offset + 2) < post_data.GetLength()) {
- int digit1 = hex.Find(_totupper(post_data[offset + 1]));
- int digit2 = hex.Find(_totupper(post_data[offset + 2]));
- if ((digit1 >= 0) && (digit2 >= 0)) {
- _TCHAR character = (_TCHAR)((digit1 << 4) + digit2);
- if (parsing_name) names[size - 1].AppendChar(character);
- else values[size - 1].AppendChar(character);
- }
-
- else {
- result = false;
- }
- offset += 2;
- }
- else {
- result = false;
- }
- }
- else if (post_data[offset] == _T('+')) {
- if (parsing_name) names[size - 1].AppendChar(_T(' '));
- else values[size - 1].AppendChar(_T(' '));
- }
- else if (post_data[offset] == _T('=')) {
- if (parsing_name) {
- parsing_name = false;
- }
- else {
- values[size - 1].AppendChar(post_data[offset]);
- }
- }
- else if (post_data[offset] == _T('&')) {
- if (!parsing_name) {
- parsing_name = true;
- size += 1;
- names.SetSize(size);
- values.SetSize(size);
- }
- else {
- values[size - 1].AppendChar(post_data[offset]);
- }
- }
-
- else {
- if (parsing_name) names[size - 1].AppendChar(post_data[offset]);
- else values[size - 1].AppendChar(post_data[offset]);
- }
- offset++;
-
- }
- if ((names[size - 1] == _T("")) &&
- (values[size - 1] == _T(""))) {
- names.SetSize(size - 1);
- values.SetSize(size - 1);
- }
- return result;
- }
- // Document loaded and initialized
- void SimpleBrowser::_OnDocumentComplete(LPDISPATCH lpDisp,VARIANT *URL)
- {
- if (lpDisp == _BrowserDispatch) {
- CString URL_string;
-
- if ((URL != NULL) &&
- (V_VT(URL) == VT_BSTR)) {
- URL_string = CString(V_BSTR(URL));
- }
- OnDocumentComplete(URL_string);
- }
- }
- void SimpleBrowser::OnDocumentComplete(CString URL)
- {
- CWnd *parent = GetParent();
-
- if (parent != NULL) {
- Notification notification(m_hWnd,GetDlgCtrlID(),DocumentComplete);
-
- notification.URL = URL;
- LRESULT result = parent->SendMessage(WM_NOTIFY,
- notification.hdr.idFrom,
- (LPARAM)¬ification);
-
- }
- }
- // Navigation/download progress
- void SimpleBrowser::_OnDownloadBegin()
- {
- OnDownloadBegin();
- }
- void SimpleBrowser::OnDownloadBegin()
- {
- CWnd *parent = GetParent();
-
- if (parent != NULL) {
- Notification notification(m_hWnd,GetDlgCtrlID(),DownloadBegin);
-
- LRESULT result = parent->SendMessage(WM_NOTIFY,
- notification.hdr.idFrom,
- (LPARAM)¬ification);
-
- }
- }
- void SimpleBrowser::_OnProgressChange(long progress,long progress_max)
- {
- OnProgressChange((int)progress,(int)progress_max);
- }
- void SimpleBrowser::OnProgressChange(int progress,int progress_max)
- {
- CWnd *parent = GetParent();
-
- if (parent != NULL) {
- Notification notification(m_hWnd,GetDlgCtrlID(),ProgressChange);
-
- notification.progress = progress;
- notification.progress_max = progress_max;
- LRESULT result = parent->SendMessage(WM_NOTIFY,
- notification.hdr.idFrom,
- (LPARAM)¬ification);
-
- }
- }
- void SimpleBrowser::_OnDownloadComplete()
- {
- OnDownloadComplete();
- }
- void SimpleBrowser::OnDownloadComplete()
- {
- CWnd *parent = GetParent();
-
- if (parent != NULL) {
- Notification notification(m_hWnd,GetDlgCtrlID(),DownloadComplete);
-
- LRESULT result = parent->SendMessage(WM_NOTIFY,
- notification.hdr.idFrom,
- (LPARAM)¬ification);
-
- }
- }
- // Navigation to a link has completed
- void SimpleBrowser::_OnNavigateComplete2(LPDISPATCH lpDisp,VARIANT *URL)
- {
- if (lpDisp == _BrowserDispatch) {
- // signal document ready
- _Ready = true;
- // write current content
-
- _ContentWrite();
-
- // inform user navigation complete
-
- CString URL_string;
-
- if ((URL != NULL) &&
- (V_VT(URL) == VT_BSTR)) {
- URL_string = V_BSTR(URL);
- }
- OnNavigateComplete2(URL_string);
- }
- }
- void SimpleBrowser::OnNavigateComplete2(CString URL)
- {
- CWnd *parent = GetParent();
-
- if (parent != NULL) {
- Notification notification(m_hWnd,GetDlgCtrlID(),NavigateComplete2);
-
- notification.URL = URL;
- LRESULT result = parent->SendMessage(WM_NOTIFY,
- notification.hdr.idFrom,
- (LPARAM)¬ification);
-
- }
- }
- // Status text has changed
- void SimpleBrowser::_OnStatusTextChange(BSTR bstrText)
- {
- CString text;
-
- if (bstrText != NULL) {
- text = (LPCTSTR)bstrText;
- }
- OnStatusTextChange(text);
- }
- void SimpleBrowser::OnStatusTextChange(CString text)
- {
- CWnd *parent = GetParent();
-
- if (parent != NULL) {
- Notification notification(m_hWnd,GetDlgCtrlID(),StatusTextChange);
-
- notification.text = text;
- LRESULT result = parent->SendMessage(WM_NOTIFY,
- notification.hdr.idFrom,
- (LPARAM)¬ification);
-
- }
- }
- // Title text has changed
- void SimpleBrowser::_OnTitleChange(BSTR bstrText)
- {
- CString text;
-
- if (bstrText != NULL) {
- text = (LPCTSTR)bstrText;
- }
- OnTitleChange(text);
- }
- void SimpleBrowser::OnTitleChange(CString text)
- {
- CWnd *parent = GetParent();
-
- if (parent != NULL) {
- Notification notification(m_hWnd,GetDlgCtrlID(),TitleChange);
-
- notification.text = text;
- LRESULT result = parent->SendMessage(WM_NOTIFY,
- notification.hdr.idFrom,
- (LPARAM)¬ification);
-
- }
- }
- // Print template instantiation and teardown
-
- void SimpleBrowser::_OnPrintTemplateInstantiation(LPDISPATCH lpDisp)
- {
- OnPrintTemplateInstantiation();
- };
- void SimpleBrowser::OnPrintTemplateInstantiation()
- {
- CWnd *parent = GetParent();
-
- if (parent != NULL) {
- Notification notification(m_hWnd,GetDlgCtrlID(),PrintTemplateInstantiation);
-
- LRESULT result = parent->SendMessage(WM_NOTIFY,
- notification.hdr.idFrom,
- (LPARAM)¬ification);
-
- }
- }
- void SimpleBrowser::_OnPrintTemplateTeardown(LPDISPATCH lpDisp)
- {
- OnPrintTemplateTeardown();
- };
- void SimpleBrowser::OnPrintTemplateTeardown()
- {
- CWnd *parent = GetParent();
-
- if (parent != NULL) {
- Notification notification(m_hWnd,GetDlgCtrlID(),PrintTemplateTeardown);
-
- LRESULT result = parent->SendMessage(WM_NOTIFY,
- notification.hdr.idFrom,
- (LPARAM)¬ification);
-
- }
- }
- // Write deferred content
- void SimpleBrowser::_ContentWrite()
- {
- if (_Ready && (!_Content.IsEmpty())) {
-
- // get document interface
- IHTMLDocument2 *document = GetDocument();
-
- if (document != NULL) {
- // construct text to be written to browser as SAFEARRAY
- SAFEARRAY *safe_array = SafeArrayCreateVector(VT_VARIANT,0,1);
-
- VARIANT *variant;
-
- SafeArrayAccessData(safe_array,(LPVOID *)&variant);
-
- variant->vt = VT_BSTR;
- variant->bstrVal = _Content.AllocSysString();
-
- SafeArrayUnaccessData(safe_array);
- // write SAFEARRAY to browser document
- document->write(safe_array);
-
- // cleanup
-
- document->Release();
- document = NULL;
- ::SysFreeString(variant->bstrVal);
- variant->bstrVal = NULL;
-
- SafeArrayDestroy(safe_array);
-
- }
- }
- }
|