Ejemplo n.º 1
0
void CHtmlViewer::OnDocumentCompleted()
{
    // disconnect / remove old documents events
    if (this->pDocumentElementEvents)
        delete this->pDocumentElementEvents;

    // attach new documents events
    HRESULT hr;
    IDispatch* pHtmlDocDispatch = NULL;
    IHTMLDocument3* pHtmlDoc = NULL;
    // Retrieve the document object.

    hr = pIWebBrowser->get_Document(&pHtmlDocDispatch);
    if (SUCCEEDED (hr) && (pHtmlDocDispatch != NULL))
    {
        hr = pHtmlDocDispatch->QueryInterface(IID_IHTMLDocument3,(void**)&pHtmlDoc);
        if (SUCCEEDED (hr) && (pHtmlDoc != NULL))
        {

            IHTMLElement* pElem = NULL;
            pHtmlDoc->get_documentElement(&pElem);

            this->pDocumentElementEvents=new CElementEvents(pElem);
            this->pDocumentElementEvents->ConnectEvents();

            this->pDocumentElementEvents->SetElementEventsCallBack(this->ElementEventsCallBack,this->ElementEventsCallBackUserParam);
            this->pDocumentElementEvents->SetElementEventsCallBackEx(this->ElementEventsCallBackEx,this->ElementEventsCallBackExUserParam);

            this->pDocumentElementEvents->EnableSelection(this->bEnableSelection);
            this->pDocumentElementEvents->EnableContextMenu(this->bEnableContextMenu);

            pElem->Release();
            pHtmlDoc->Release();
        }

        pHtmlDocDispatch->Release();
    }
}
Ejemplo n.º 2
0
BOOL CHtmlViewer::SaveAs(TCHAR* FileName)
{
    if (this->pIWebBrowser==NULL)
        return FALSE;

    BOOL bSuccess=FALSE;

    // this->pIWebBrowser->ExecWB(OLECMDID_SAVEAS,...) troubles : 
    // don't get updated content 
    // don't update res:// links)

    // this saving way troubles : don't save remote files only res and file protocols supported

    CHtmlViewerSave_FirstItem FirstItem;

    HRESULT hr;
    HANDLE hFile;
    IDispatch* pHtmlDocDispatch = NULL;
    IHTMLDocument3* pHtmlDoc = NULL;
    // Retrieve the document object.

    hr = pIWebBrowser->get_Document(&pHtmlDocDispatch);
    if (SUCCEEDED (hr) && (pHtmlDocDispatch != NULL))
    {
        hr = pHtmlDocDispatch->QueryInterface(IID_IHTMLDocument3,(void**)&pHtmlDoc);
        if (SUCCEEDED (hr) && (pHtmlDoc != NULL))
        {

            IHTMLElement* pElem = NULL;

            hr = pHtmlDoc->get_documentElement(&pElem);
            if (SUCCEEDED (hr) && (pElem != NULL))
            {

                BSTR bstrDocContent;
                pElem->get_innerHTML(&bstrDocContent);
                TCHAR* pDocContent;
                TCHAR* pszCurrent;
                TCHAR* pszResProtocol;
                TCHAR* pszResProtocolEnd;
                TCHAR* pszFileProtocol;
                TCHAR* pszFileProtocolEnd;
                TCHAR* pszDontSaveBegin;
                TCHAR* pszDontSaveEnd;
                TCHAR* pszMin;
                TCHAR* psz;
                TCHAR Link[MAX_PATH];
                TCHAR ChangedLink[MAX_PATH];
    #if (defined(UNICODE)||defined(_UNICODE))
                pDocContent=bstrDocContent;
    #else
                CAnsiUnicodeConvert::UnicodeToAnsi(bstrDocContent,&pDocContent);
    #endif
                if (CTextFile::CreateTextFile(FileName,&hFile))
                {
                    CTextFile::WriteText(hFile,CHtmlViewer_DOCUMENT_HEADER);

                    pszCurrent=pDocContent;

                    for (;;)
                    {
                        // find next dont save begin
                        pszDontSaveBegin=_tcsstr(pszCurrent,CHtmlViewer_DONT_SAVE_BEGIN);

                        // find next file:// protocol
                        pszFileProtocol=_tcsstr(pszCurrent,CHtmlViewer_FILE_PROTOCOL);

                        // find next res:// protocol (like "res://foo.exe/jpg/105")
                        pszResProtocol=_tcsstr(pszCurrent,CHtmlViewer_RES_PROTOCOL);


                        // if no more res::// protocol, file:// protocol or <!--DONT_SAVE_BEGIN-->
                        if ((pszResProtocol==NULL)
                            && (pszFileProtocol==NULL)
                            && (pszDontSaveBegin==NULL)
                            ) 
                        {
                            CTextFile::WriteText(hFile,pszCurrent);
                            break;
                        }

                        // at least one item is not null
                        // get first not null item
                        if (pszDontSaveBegin)
                            pszMin=pszDontSaveBegin;
                        else
                            pszMin=(TCHAR*)-1;
                        FirstItem=CHtmlViewerSave_FirstItem_DontSave;

                        if ((pszFileProtocol<pszMin)
                            && (pszFileProtocol!=0)
                            )
                        {
                            FirstItem=CHtmlViewerSave_FirstItem_File;
                            pszMin=pszFileProtocol;
                        }

                        if ((pszResProtocol<pszMin)
                            && (pszResProtocol!=0)
                            )
                        {
                            FirstItem=CHtmlViewerSave_FirstItem_Res;
                            pszMin=pszResProtocol;
                        }

                        // do treatment for first found type
                        // (use if instead case to allow breaking for(;;). Else we need a goto)
                        if (FirstItem==CHtmlViewerSave_FirstItem_Res)
                        {
                            // write data before res:// protocol
                            CTextFile::WriteText(hFile,pszCurrent,pszResProtocol-pszCurrent+1);

                            // look for the end of res:// protocol
                            pszResProtocolEnd=_tcschr(pszResProtocol+_tcslen(CHtmlViewer_RES_PROTOCOL),'"');
                            if (!pszResProtocolEnd)
                            {
                                // bad html format, output raw content
                                SetFilePointer(hFile,0,0,FILE_BEGIN);
                                CTextFile::WriteText(hFile,CHtmlViewer_DOCUMENT_HEADER);
                                CTextFile::WriteText(hFile,pDocContent);
                                break;
                            }

                            _tcsncpy(Link,pszResProtocol,pszResProtocolEnd-pszResProtocol+1);
                            Link[pszResProtocolEnd-pszResProtocol+1]=0;

                            // change link from resource one to extracted resource one
                            this->ModifyLinkAndExtractResource(FileName,Link,ChangedLink);
                            // write modified link
                            CTextFile::WriteText(hFile,ChangedLink);
                            // end link by adding '"'
                            CTextFile::WriteText(hFile,_T("\""));

                            // pointer after res protocol
                            pszCurrent=pszResProtocolEnd+1;
                        }
                        else if (FirstItem==CHtmlViewerSave_FirstItem_File)
                        {
                            // write data before file:// protocol
                            CTextFile::WriteText(hFile,pszCurrent,pszFileProtocol-pszCurrent+1);

                            // look for the end of file:// protocol
                            pszFileProtocolEnd=_tcschr(pszFileProtocol+_tcslen(CHtmlViewer_FILE_PROTOCOL),'"');
                            if (!pszFileProtocolEnd)
                            {
                                // bad html format, output raw content
                                SetFilePointer(hFile,0,0,FILE_BEGIN);
                                CTextFile::WriteText(hFile,CHtmlViewer_DOCUMENT_HEADER);
                                CTextFile::WriteText(hFile,pDocContent);
                                break;
                            }
                            pszFileProtocol+=_tcslen(CHtmlViewer_FILE_PROTOCOL);
                            _tcsncpy(Link,pszFileProtocol,pszFileProtocolEnd-pszFileProtocol);
                            Link[pszFileProtocolEnd-pszFileProtocol]=0;
                            

                            // get saving filename
                            _tcscpy(ChangedLink,FileName);
                            // remove file extension of saving file name
                            CStdFileOperations::RemoveFileExt(ChangedLink);
                            // add "_files\" to saving file to create a directory like SavingFileName_files
                            _tcscat(ChangedLink,CHtmlViewer_FILES_DIR_EXT);
                            // add the name of file
                            _tcscat(ChangedLink,CStdFileOperations::GetFileName(Link));
                            // create directory if necessary
                            CStdFileOperations::CreateDirectoryForFile(ChangedLink);
                            // copy file to directory
                            CopyFile(Link,ChangedLink,FALSE);
                           
                            // now make a relative link to file
                            _tcscpy(Link,ChangedLink);
                            psz=_tcsrchr(Link,'\\');
                            if (psz)
                            {
                                *psz='/';
                                psz=_tcsrchr(Link,'\\');
                                if (psz)
                                {
                                    _tcscpy(ChangedLink,psz+1);
                                }
                            }

                            // write modified link
                            CTextFile::WriteText(hFile,ChangedLink);
                            // end link by adding '"'
                            CTextFile::WriteText(hFile,_T("\""));

                            // pointer after file protocol
                            pszCurrent=pszFileProtocolEnd+1;
                        }
                        else if (FirstItem==CHtmlViewerSave_FirstItem_DontSave)
                        {
                            // write data before <!--DONT_SAVE_BEGIN-->
                            CTextFile::WriteText(hFile,pszCurrent,pszDontSaveBegin-pszCurrent);

                            pszDontSaveEnd=_tcsstr(pszDontSaveBegin+_tcslen(CHtmlViewer_DONT_SAVE_BEGIN),CHtmlViewer_DONT_SAVE_END);
                            if (!pszDontSaveEnd)
                            {
                                // bad html format, output raw content
                                SetFilePointer(hFile,0,0,FILE_BEGIN);
                                CTextFile::WriteText(hFile,CHtmlViewer_DOCUMENT_HEADER);
                                CTextFile::WriteText(hFile,pDocContent);
                                break;
                            }
                            // point after <!--DONT_SAVE_END-->
                            pszCurrent=pszDontSaveEnd+_tcslen(CHtmlViewer_DONT_SAVE_END);
                        }
                    }
                    CTextFile::WriteText(hFile,CHtmlViewer_DOCUMENT_FOOTER);
                    CloseHandle(hFile);

                    bSuccess=TRUE;
                }
    #if ((!defined(UNICODE))&& (!defined(_UNICODE)))
                // free allocated string for ansi
                free(pDocContent);
    #endif

                SysFreeString(bstrDocContent);

                pElem->Release();
            }
            pHtmlDoc->Release();
        }
        
        pHtmlDocDispatch->Release();
    }
    return bSuccess;
}