Back to index

lightning-sunbird  0.9+nobinonly
CBrowseDlg.cpp
Go to the documentation of this file.
00001 // CBrowseDlg.cpp : implementation file
00002 //
00003 
00004 #include "stdafx.h"
00005 
00006 #include "cbrowse.h"
00007 #include "CBrowseDlg.h"
00008 #include "ControlEventSink.h"
00009 #include "..\..\src\control\DHTMLCmdIds.h"
00010 
00011 #ifdef _DEBUG
00012 #define new DEBUG_NEW
00013 #undef THIS_FILE
00014 static char THIS_FILE[] = __FILE__;
00015 #endif
00016 
00017 #include <math.h>
00018 #include <fpieee.h>
00019 #include <float.h>
00020 
00021 #include <signal.h>
00022 
00023 #include <stack>
00024 
00025 void __cdecl fperr(int sig)
00026 {
00027        CString sError;
00028        sError.Format("FP Error %08x", sig);
00029        AfxMessageBox(sError);
00030 }
00031 
00032 TCHAR *aURLs[] =
00033 {
00034        _T("http://whippy/calendar.html"),
00035        _T("http://www.mozilla.org"),
00036        _T("http://www.yahoo.com"),
00037        _T("http://www.netscape.com"),
00038        _T("http://www.microsoft.com")
00039 };
00040 
00041 CBrowseDlg *CBrowseDlg::m_pBrowseDlg = NULL;
00042 
00044 // CBrowseDlg dialog
00045 
00046 CBrowseDlg::CBrowseDlg(CWnd* pParent /*=NULL*/)
00047        : CDialog(CBrowseDlg::IDD, pParent)
00048 {
00049        signal(SIGFPE, fperr);
00050        double x = 0.0;
00051        double y = 1.0/x;
00052 
00053        //{{AFX_DATA_INIT(CBrowseDlg)
00054        //}}AFX_DATA_INIT
00055        // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
00056        m_pBrowseDlg = this;
00057        m_pControlSite = NULL;
00058        m_clsid = CLSID_NULL;
00059        m_bUseCustomPopupMenu = FALSE;
00060        m_bUseCustomDropTarget = FALSE;
00061     m_bEditMode = FALSE;
00062     m_bNewWindow = FALSE;
00063     m_bCanGoBack = FALSE;
00064     m_bCanGoForward = FALSE;
00065 }
00066 
00067 void CBrowseDlg::DoDataExchange(CDataExchange* pDX)
00068 {
00069     CDialog::DoDataExchange(pDX);
00070        //{{AFX_DATA_MAP(CBrowseDlg)
00071        DDX_Control(pDX, IDC_STOP, m_btnStop);
00072        DDX_Control(pDX, IDC_FORWARD, m_btnForward);
00073        DDX_Control(pDX, IDC_BACKWARD, m_btnBack);
00074        DDX_Control(pDX, IDC_URL, m_cmbURLs);
00075        //}}AFX_DATA_MAP
00076 }
00077 
00078 BEGIN_MESSAGE_MAP(CBrowseDlg, CDialog)
00079        //{{AFX_MSG_MAP(CBrowseDlg)
00080        ON_WM_PAINT()
00081        ON_WM_QUERYDRAGICON()
00082        ON_BN_CLICKED(IDC_GO, OnGo)
00083        ON_BN_CLICKED(IDC_BACKWARD, OnBackward)
00084        ON_BN_CLICKED(IDC_FORWARD, OnForward)
00085        ON_WM_CLOSE()
00086        ON_WM_DESTROY()
00087        ON_WM_SIZE()
00088        ON_COMMAND(ID_FILE_EXIT, OnFileExit)
00089        ON_COMMAND(ID_VIEW_GOTO_BACK, OnViewGotoBack)
00090        ON_COMMAND(ID_VIEW_GOTO_FORWARD, OnViewGotoForward)
00091        ON_COMMAND(ID_VIEW_GOTO_HOME, OnViewGotoHome)
00092        ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
00093        ON_COMMAND(ID_EDIT_CUT, OnEditCut)
00094        ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
00095        ON_COMMAND(ID_HELP_ABOUT, OnHelpAbout)
00096        ON_UPDATE_COMMAND_UI(ID_VIEW_GOTO_BACK, OnUpdateViewGotoBack)
00097        ON_UPDATE_COMMAND_UI(ID_VIEW_GOTO_FORWARD, OnUpdateViewGotoForward)
00098        ON_COMMAND(ID_EDIT_SELECTALL, OnEditSelectAll)
00099        ON_COMMAND(ID_VIEW_REFRESH, OnViewRefresh)
00100        ON_COMMAND(ID_VIEW_VIEWSOURCE, OnViewViewSource)
00101        ON_BN_CLICKED(IDC_STOP, OnStop)
00102        ON_COMMAND(ID_FILE_SAVEAS, OnFileSaveAs)
00103        ON_COMMAND(ID_FILE_PRINT, OnFilePrint)
00104        ON_COMMAND(ID_DEBUG_VISIBLE, OnDebugVisible)
00105        ON_UPDATE_COMMAND_UI(ID_DEBUG_VISIBLE, OnUpdateDebugVisible)
00106        ON_COMMAND(ID_DEBUG_POSTDATATEST, OnDebugPostDataTest)
00107        ON_BN_CLICKED(IDC_RELOAD, OnReload)
00108        ON_COMMAND(ID_VIEW_EDITMODE, OnViewEditmode)
00109        ON_COMMAND(ID_VIEW_OPENLINKSINNEWWINDOWS, OnViewOpenInNewWindow)
00110        ON_UPDATE_COMMAND_UI(ID_VIEW_EDITMODE, OnUpdateViewEditmode)
00111        ON_UPDATE_COMMAND_UI(ID_VIEW_OPENLINKSINNEWWINDOWS, OnUpdateViewOpenInNewWindow)
00112        ON_COMMAND(ID_FILE_PAGESETUP, OnFilePagesetup)
00113        //}}AFX_MSG_MAP
00114        ON_COMMAND(IDB_BOLD, OnEditBold)
00115        ON_COMMAND(IDB_ITALIC, OnEditItalic)
00116        ON_COMMAND(IDB_UNDERLINE, OnEditUnderline)
00117 END_MESSAGE_MAP()
00118 
00119 #define IL_CLOSEDFOLDER     0
00120 #define IL_OPENFOLDER       1
00121 #define IL_TEST                    2
00122 #define IL_TESTFAILED       3
00123 #define IL_TESTPASSED       4
00124 #define IL_TESTPARTIAL  5
00125 #define IL_NODE                    IL_TEST
00126 
00128 // CBrowseDlg message handlers
00129 
00130 BOOL CBrowseDlg::OnInitDialog()
00131 {
00132        CDialog::OnInitDialog();
00133 
00134        CWinApp *pApp = AfxGetApp();
00135        m_szTestURL = pApp->GetProfileString(SECTION_TEST, KEY_TESTURL, KEY_TESTURL_DEFAULTVALUE);
00136        m_szTestCGI = pApp->GetProfileString(SECTION_TEST, KEY_TESTCGI, KEY_TESTCGI_DEFAULTVALUE);
00137 
00138        // Set the icon for this dialog.  The framework does this automatically
00139        //  when the application's main window is not a dialog
00140        SetIcon(m_hIcon, TRUE);                   // Set big icon
00141        SetIcon(m_hIcon, FALSE);           // Set small icon
00142 
00143        CRect rcTabMarker;
00144        GetDlgItem(IDC_TAB_MARKER)->GetWindowRect(&rcTabMarker);
00145        ScreenToClient(rcTabMarker);
00146 
00147     m_dlgPropSheet.AddPage(&m_TabMessages);
00148     m_dlgPropSheet.AddPage(&m_TabTests);
00149     m_dlgPropSheet.AddPage(&m_TabDOM);
00150 
00151        m_TabMessages.m_pBrowseDlg = this;
00152        m_TabTests.m_pBrowseDlg = this;
00153        m_TabDOM.m_pBrowseDlg = this;
00154 
00155     m_dlgPropSheet.Create(this, WS_CHILD | WS_VISIBLE, 0);
00156     m_dlgPropSheet.ModifyStyleEx (0, WS_EX_CONTROLPARENT);
00157     m_dlgPropSheet.ModifyStyle( 0, WS_TABSTOP );
00158     m_dlgPropSheet.SetWindowPos( NULL, rcTabMarker.left-7, rcTabMarker.top-7, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE );
00159 
00160        // Image list
00161        m_cImageList.Create(16, 16, ILC_COLOR | ILC_MASK, 0, 10);
00162        m_cImageList.Add(AfxGetApp()->LoadIcon(IDI_CLOSEDFOLDER));
00163        m_cImageList.Add(AfxGetApp()->LoadIcon(IDI_OPENFOLDER));
00164        m_cImageList.Add(AfxGetApp()->LoadIcon(IDI_TEST));
00165        m_cImageList.Add(AfxGetApp()->LoadIcon(IDI_TESTFAILED));
00166        m_cImageList.Add(AfxGetApp()->LoadIcon(IDI_TESTPASSED));
00167        m_cImageList.Add(AfxGetApp()->LoadIcon(IDI_TESTPARTIAL));
00168 
00169        // Set up the editor bar
00170        CRect rcEditBarMarker;
00171        GetDlgItem(IDC_EDITBAR_MARKER)->GetWindowRect(&rcEditBarMarker);
00172        ScreenToClient(rcEditBarMarker);
00173        GetDlgItem(IDC_EDITBAR_MARKER)->DestroyWindow();
00174 
00175        m_EditBar.Create(this);
00176        m_EditBar.LoadToolBar(IDR_DHTMLEDIT);
00177        m_EditBar.SetWindowPos(&wndTop, rcEditBarMarker.left, rcEditBarMarker.top,
00178               rcEditBarMarker.Width(), rcEditBarMarker.Height(), SWP_SHOWWINDOW);
00179 
00180        // Set up some URLs. The first couple are internal
00181        m_cmbURLs.AddString(m_szTestURL);
00182        for (int i = 0; i < sizeof(aURLs) / sizeof(aURLs[0]); i++)
00183        {
00184               m_cmbURLs.AddString(aURLs[i]);
00185        }
00186        m_cmbURLs.SetCurSel(0);
00187 
00188        // Create the contained web browser
00189        CreateWebBrowser();
00190 
00191        // Load the menu
00192        m_menu.LoadMenu(IDR_MAIN);
00193        SetMenu(&m_menu);
00194        m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
00195 
00196        return TRUE;  // return TRUE  unless you set the focus to a control
00197 }
00198 
00199 
00200 // If you add a minimize button to your dialog, you will need the code below
00201 //  to draw the icon.  For MFC applications using the document/view model,
00202 //  this is automatically done for you by the framework.
00203 
00204 void CBrowseDlg::OnPaint() 
00205 {
00206        if (IsIconic())
00207        {
00208               CPaintDC dc(this); // device context for painting
00209 
00210               SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
00211 
00212               // Center icon in client rectangle
00213               int cxIcon = GetSystemMetrics(SM_CXICON);
00214               int cyIcon = GetSystemMetrics(SM_CYICON);
00215               CRect rect;
00216               GetClientRect(&rect);
00217               int x = (rect.Width() - cxIcon + 1) / 2;
00218               int y = (rect.Height() - cyIcon + 1) / 2;
00219 
00220               // Draw the icon
00221               dc.DrawIcon(x, y, m_hIcon);
00222        }
00223        else
00224        {
00225               CPaintDC dc(this);
00226 
00227               m_pControlSite->Draw(dc.m_hDC);
00228        }
00229 }
00230 
00231 // The system calls this to obtain the cursor to display while the user drags
00232 //  the minimized window.
00233 HCURSOR CBrowseDlg::OnQueryDragIcon()
00234 {
00235        return (HCURSOR) m_hIcon;
00236 }
00237 
00238 struct EnumData
00239 {
00240        CBrowseDlg *pBrowseDlg;
00241        CSize sizeDelta;
00242 };
00243 
00244 BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam)
00245 {
00246        EnumData *pData = (EnumData *) lParam;
00247        CBrowseDlg *pThis = pData->pBrowseDlg;
00248 
00249        switch (::GetDlgCtrlID(hwnd))
00250        {
00251        case IDC_BROWSER_MARKER:
00252               {
00253                      CWnd *pMarker = pThis->GetDlgItem(IDC_BROWSER_MARKER);
00254                      CRect rcMarker;
00255                      pMarker->GetWindowRect(&rcMarker);
00256                      pThis->ScreenToClient(rcMarker);
00257 
00258                      rcMarker.right += pData->sizeDelta.cx;
00259                      rcMarker.bottom += pData->sizeDelta.cy;
00260 
00261                      if (rcMarker.Width() > 10 && rcMarker.Height() > 10)
00262                      {
00263                             pMarker->SetWindowPos(&CWnd::wndBottom, 0, 0, rcMarker.Width(), rcMarker.Height(), 
00264                                           SWP_NOMOVE | SWP_NOACTIVATE | SWP_HIDEWINDOW);
00265                             pThis->m_pControlSite->SetPosition(rcMarker);
00266                      }
00267               }
00268               break;
00269        case IDC_TAB_MARKER:
00270               {
00271                      CWnd *pMarker = pThis->GetDlgItem(IDC_TAB_MARKER);
00272                      CRect rcMarker;
00273                      pMarker->GetWindowRect(&rcMarker);
00274                      pThis->ScreenToClient(rcMarker);
00275 
00276                      rcMarker.top += pData->sizeDelta.cy;
00277 
00278                      if (rcMarker.top > 70)
00279                      {
00280                             pMarker->SetWindowPos(&CWnd::wndBottom, rcMarker.left, rcMarker.top, 0, 0, 
00281                                           SWP_NOSIZE | SWP_NOACTIVATE | SWP_HIDEWINDOW);
00282                             pThis->m_dlgPropSheet.SetWindowPos(NULL, rcMarker.left - 7, rcMarker.top - 7, 0, 0, 
00283                                           SWP_NOREPOSITION | SWP_NOSIZE | SWP_NOACTIVATE);
00284                      }
00285               }
00286 
00287        }
00288 
00289        return TRUE;
00290 }
00291 
00292 void CBrowseDlg::OnSize(UINT nType, int cx, int cy) 
00293 {
00294        CDialog::OnSize(nType, cx, cy);
00295 
00296        if (m_hWnd == NULL)
00297        {
00298               return;
00299        }
00300 
00301        static CSize sizeOld(-1, -1);
00302        CSize sizeNew(cx, cy);
00303 
00304        if (sizeOld.cx != -1)
00305        {
00306               EnumData data;
00307               data.pBrowseDlg = this;
00308               data.sizeDelta = sizeNew - sizeOld;
00309               ::EnumChildWindows(GetSafeHwnd(), EnumChildProc, (LPARAM) &data);
00310        }
00311        sizeOld = sizeNew;
00312 }
00313 
00314 HRESULT CBrowseDlg::CreateWebBrowser()
00315 {
00316        // Get the position of the browser marker
00317        CRect rcMarker;
00318        GetDlgItem(IDC_BROWSER_MARKER)->GetWindowRect(&rcMarker);
00319        ScreenToClient(rcMarker);
00320        GetDlgItem(IDC_BROWSER_MARKER)->ShowWindow(FALSE);
00321 
00322 //     GetDlgItem(IDC_BROWSER_MARKER)->DestroyWindow();
00323 
00324        CBrowserCtlSiteInstance::CreateInstance(&m_pControlSite);
00325        if (m_pControlSite == NULL)
00326        {
00327               OutputString(_T("Error: could not create control site"));
00328               return E_OUTOFMEMORY;
00329        }
00330 
00331        CBrowseEventSinkInstance *pEventSink = NULL;
00332        CBrowseEventSinkInstance::CreateInstance(&pEventSink);
00333        if (pEventSink == NULL)
00334        {
00335               m_pControlSite->Release();
00336               m_pControlSite = NULL;
00337               OutputString(_T("Error: could not create event sink"));
00338               return E_OUTOFMEMORY;
00339        }
00340        pEventSink->m_pBrowseDlg = this;
00341 
00342        HRESULT hr;
00343 
00344        PropertyList pl;
00345        m_pControlSite->AddRef();
00346        m_pControlSite->m_bUseCustomPopupMenu = m_bUseCustomPopupMenu;
00347        m_pControlSite->m_bUseCustomDropTarget = m_bUseCustomDropTarget;
00348        m_pControlSite->Create(m_clsid, pl);
00349        hr = m_pControlSite->Attach(GetSafeHwnd(), rcMarker, NULL);
00350        if (hr != S_OK)
00351        {
00352               OutputString(_T("Error: Cannot attach to browser control, hr = 0x%08x"), hr);
00353        }
00354        else
00355        {
00356               OutputString(_T("Sucessfully attached to browser control"));
00357        }
00358        
00359        m_pControlSite->SetPosition(rcMarker);
00360        hr = m_pControlSite->Advise(pEventSink, DIID_DWebBrowserEvents2, &m_dwCookie);
00361        if (hr != S_OK)
00362        {
00363               OutputString(_T("Error: Cannot subscribe to DIID_DWebBrowserEvents2 events, hr = 0x%08x"), hr);
00364        }
00365        else
00366        {
00367               OutputString(_T("Sucessfully subscribed to events"));
00368        }
00369 
00370        CComPtr<IUnknown> spUnkBrowser;
00371        m_pControlSite->GetControlUnknown(&spUnkBrowser);
00372 
00373        CIPtr(IWebBrowser2) spWebBrowser = spUnkBrowser;
00374        if (spWebBrowser)
00375        {
00376               spWebBrowser->put_RegisterAsDropTarget(VARIANT_TRUE);
00377        }
00378 
00379        return S_OK;
00380 }
00381 
00382 
00383 HRESULT CBrowseDlg::DestroyWebBrowser()
00384 {
00385        if (m_pControlSite)
00386        {
00387               m_pControlSite->Unadvise(DIID_DWebBrowserEvents2, m_dwCookie);
00388               m_pControlSite->Detach();
00389               m_pControlSite->Release();
00390               m_pControlSite = NULL;
00391        }
00392 
00393        return S_OK;
00394 }
00395 
00396 void CBrowseDlg::PopulateTests()
00397 {
00398        // Create the test tree
00399        CTreeCtrl &tc = m_TabTests.m_tcTests;
00400 
00401        tc.SetImageList(&m_cImageList, TVSIL_NORMAL);
00402        for (int i = 0; i < nTestSets; i++)
00403        {
00404               TestSet *pTestSet = &aTestSets[i];
00405               HTREEITEM hParent = tc.InsertItem(pTestSet->szName, IL_CLOSEDFOLDER, IL_CLOSEDFOLDER);
00406               m_TabTests.m_tcTests.SetItemData(hParent, (DWORD) pTestSet);
00407 
00408               if (pTestSet->pfnPopulator)
00409               {
00410                      pTestSet->pfnPopulator(pTestSet);
00411               }
00412 
00413               for (int j = 0; j < pTestSet->nTests; j++)
00414               {
00415                      Test *pTest = &pTestSet->aTests[j];
00416                      HTREEITEM hTest = tc.InsertItem(pTest->szName, IL_TEST, IL_TEST, hParent);
00417                      if (hTest)
00418                      {
00419                             tc.SetItemData(hTest, (DWORD) pTest);
00420                      }
00421               }
00422        }
00423 }
00424 
00425 
00426 void CBrowseDlg::UpdateURL()
00427 {
00428     CIPtr(IWebBrowser) spBrowser;
00429 
00430     GetWebBrowser(&spBrowser);
00431     if (spBrowser)
00432     {
00433         USES_CONVERSION;
00434         BSTR szLocation = NULL;
00435         spBrowser->get_LocationURL(&szLocation);
00436         m_cmbURLs.SetWindowText(W2T(szLocation));
00437         SysFreeString(szLocation);
00438     }
00439 }
00440 
00441 
00442 HRESULT CBrowseDlg::GetWebBrowser(IWebBrowser **pWebBrowser)
00443 {
00444        if (pWebBrowser == NULL)
00445        {
00446               return E_INVALIDARG;
00447        }
00448 
00449        *pWebBrowser = NULL;
00450 
00451        if (m_pControlSite)
00452        {
00453               IUnknown *pIUnkBrowser = NULL;
00454               m_pControlSite->GetControlUnknown(&pIUnkBrowser);
00455               if (pIUnkBrowser)
00456               {
00457                      pIUnkBrowser->QueryInterface(IID_IWebBrowser, (void **) pWebBrowser);
00458                      pIUnkBrowser->Release();
00459                      if (*pWebBrowser)
00460                      {
00461                             return S_OK;
00462                      }
00463               }
00464        }
00465 
00466        return E_FAIL;
00467 }
00468 
00469 void CBrowseDlg::OnGo() 
00470 {
00471        UpdateData();
00472 
00473        CComPtr<IWebBrowser> webBrowser;
00474        if (SUCCEEDED(GetWebBrowser(&webBrowser)))
00475        {
00476               CString szURL;
00477               m_cmbURLs.GetWindowText(szURL);
00478               CComVariant vFlags(m_bNewWindow ? navOpenInNewWindow : 0);
00479               BSTR bstrURL = szURL.AllocSysString();
00480               HRESULT hr = webBrowser->Navigate(bstrURL, &vFlags, NULL, NULL, NULL);
00481         if (FAILED(hr))
00482         {
00483             OutputString("Navigate failed (hr=0x%08x)", hr);
00484         }
00485               ::SysFreeString(bstrURL);
00486        }
00487 }
00488 
00489 
00490 void CBrowseDlg::OnStop() 
00491 {
00492        CComPtr<IWebBrowser> webBrowser;
00493        if (SUCCEEDED(GetWebBrowser(&webBrowser)))
00494        {
00495               webBrowser->Stop();
00496        }
00497 }
00498 
00499 void CBrowseDlg::OnReload() 
00500 {
00501        CComPtr<IWebBrowser> webBrowser;
00502        if (SUCCEEDED(GetWebBrowser(&webBrowser)))
00503        {
00504               CComVariant vValue(REFRESH_COMPLETELY);
00505               webBrowser->Refresh2(&vValue);
00506        }
00507 }
00508 
00509 void CBrowseDlg::OnBackward() 
00510 {
00511        CComPtr<IWebBrowser> webBrowser;
00512        if (SUCCEEDED(GetWebBrowser(&webBrowser)))
00513        {
00514               webBrowser->GoBack();
00515        }
00516 }
00517 
00518 void CBrowseDlg::OnForward() 
00519 {
00520        CComPtr<IWebBrowser> webBrowser;
00521        if (SUCCEEDED(GetWebBrowser(&webBrowser)))
00522        {
00523               webBrowser->GoForward();
00524        }
00525 }
00526 
00527 void CBrowseDlg::RunTestSet(TestSet *pTestSet)
00528 {
00529        ASSERT(pTestSet);
00530        if (pTestSet == NULL)
00531        {
00532               return;
00533        }
00534 
00535        for (int j = 0; j < pTestSet->nTests; j++)
00536        {
00537               Test *pTest = &pTestSet->aTests[j];
00538               RunTest(pTest);
00539        }
00540 }
00541 
00542 
00543 TestResult CBrowseDlg::RunTest(Test *pTest)
00544 {
00545        ASSERT(pTest);
00546        TestResult nResult = trFailed;
00547 
00548        CString szMsg;
00549        szMsg.Format(_T("Running test \"%s\""), pTest->szName);
00550        OutputString(szMsg);
00551 
00552        if (pTest && pTest->pfn)
00553        {
00554               BrowserInfo cInfo;
00555 
00556               cInfo.pTest = pTest;
00557               cInfo.clsid = m_clsid;
00558               cInfo.pControlSite = m_pControlSite;
00559               cInfo.pIUnknown = NULL;
00560               cInfo.pBrowseDlg = this;
00561               cInfo.szTestURL = m_szTestURL;
00562               cInfo.szTestCGI = m_szTestCGI;
00563               if (cInfo.pControlSite)
00564               {
00565                      cInfo.pControlSite->GetControlUnknown(&cInfo.pIUnknown);
00566               }
00567               nResult = pTest->pfn(cInfo);
00568               pTest->nLastResult = nResult;
00569               if (cInfo.pIUnknown)
00570               {
00571                      cInfo.pIUnknown->Release();
00572               }
00573        }
00574 
00575        switch (nResult)
00576        {
00577        case trFailed:
00578               OutputString(_T("Test failed"));
00579               break;
00580        case trPassed:
00581               OutputString(_T("Test passed"));
00582               break;
00583        case trPartial:
00584               OutputString(_T("Test partial"));
00585               break;
00586        default:
00587               break;
00588        }
00589 
00590        return nResult;
00591 }
00592 
00593 void CBrowseDlg::OutputString(const TCHAR *szMessage, ...)
00594 {
00595        if (m_pBrowseDlg == NULL)
00596        {
00597               return;
00598        }
00599 
00600        TCHAR szBuffer[256];
00601 
00602        va_list cArgs;
00603        va_start(cArgs, szMessage);
00604        _vstprintf(szBuffer, szMessage, cArgs);
00605        va_end(cArgs);
00606 
00607        CString szOutput;
00608        szOutput.Format(_T("%s"), szBuffer);
00609 
00610        m_TabMessages.m_lbMessages.AddString(szOutput);
00611        m_TabMessages.m_lbMessages.SetTopIndex(m_TabMessages.m_lbMessages.GetCount() - 1);
00612 }
00613 
00614 void CBrowseDlg::UpdateTest(HTREEITEM hItem, TestResult nResult)
00615 {
00616        if (nResult == trPassed)
00617        {
00618               m_TabTests.m_tcTests.SetItemImage(hItem, IL_TESTPASSED, IL_TESTPASSED);
00619        }
00620        else if (nResult == trFailed)
00621        {
00622               m_TabTests.m_tcTests.SetItemImage(hItem, IL_TESTFAILED, IL_TESTFAILED);
00623        }
00624        else if (nResult == trPartial)
00625        {
00626               m_TabTests.m_tcTests.SetItemImage(hItem, IL_TESTPARTIAL, IL_TESTPARTIAL);
00627        }
00628 }
00629 
00630 void CBrowseDlg::UpdateTestSet(HTREEITEM hItem)
00631 {
00632        // Examine the results
00633        HTREEITEM hTest = m_TabTests.m_tcTests.GetNextItem(hItem, TVGN_CHILD);
00634        while (hTest)
00635        {
00636               Test *pTest = (Test *) m_TabTests.m_tcTests.GetItemData(hTest);
00637               UpdateTest(hTest, pTest->nLastResult);
00638               hTest = m_TabTests.m_tcTests.GetNextItem(hTest, TVGN_NEXT);
00639        }
00640 }
00641 
00642 void CBrowseDlg::OnRunTest() 
00643 {
00644        HTREEITEM hItem = m_TabTests.m_tcTests.GetNextItem(NULL, TVGN_FIRSTVISIBLE);
00645        while (hItem)
00646        {
00647               UINT nState = m_TabTests.m_tcTests.GetItemState(hItem, TVIS_SELECTED);
00648               if (!(nState & TVIS_SELECTED))
00649               {
00650                      hItem = m_TabTests.m_tcTests.GetNextItem(hItem, TVGN_NEXTVISIBLE);
00651                      continue;
00652               }
00653 
00654               if (m_TabTests.m_tcTests.ItemHasChildren(hItem))
00655               {
00656                      // Run complete set of tests
00657                      TestSet *pTestSet = (TestSet *) m_TabTests.m_tcTests.GetItemData(hItem);
00658                      RunTestSet(pTestSet);
00659                      UpdateTestSet(hItem);
00660               }
00661               else
00662               {
00663                      // Find the test
00664                      Test *pTest = (Test *) m_TabTests.m_tcTests.GetItemData(hItem);
00665                      TestResult nResult = RunTest(pTest);
00666                      UpdateTest(hItem, nResult);
00667               }
00668 
00669               hItem = m_TabTests.m_tcTests.GetNextItem(hItem, TVGN_NEXTVISIBLE);
00670        }
00671 }
00672 
00673 struct _ElementPos
00674 {
00675        HTREEITEM m_htiParent;
00676        CIPtr(IHTMLElementCollection) m_cpElementCollection;
00677        int m_nIndex;
00678 
00679        _ElementPos(HTREEITEM htiParent, IHTMLElementCollection *pElementCollection, int nIndex)
00680        {
00681               m_htiParent = htiParent;
00682               m_cpElementCollection = pElementCollection;
00683               m_nIndex = nIndex;
00684        }
00685        _ElementPos()
00686        {
00687        }
00688 };
00689 
00690 void CBrowseDlg::OnRefreshDOM() 
00691 {
00692        m_TabDOM.m_tcDOM.DeleteAllItems();
00693 
00694        std::stack<_ElementPos> cStack;
00695 
00696        CComPtr<IUnknown> cpUnkPtr;
00697        m_pControlSite->GetControlUnknown(&cpUnkPtr);
00698        CIPtr(IWebBrowserApp) cpWebBrowser = cpUnkPtr;
00699        if (cpWebBrowser == NULL)
00700        {
00701               return;
00702        }
00703 
00704        CIPtr(IDispatch) cpDispDocument;
00705        cpWebBrowser->get_Document(&cpDispDocument);
00706        if (cpDispDocument == NULL)
00707        {
00708               return;
00709        }
00710 
00711        // Recurse the DOM, building a tree
00712        
00713        CIPtr(IHTMLDocument2) cpDocElement = cpDispDocument;
00714        
00715        CIPtr(IHTMLElementCollection) cpColl;
00716        HRESULT hr = cpDocElement->get_all( &cpColl );
00717 
00718        cStack.push(_ElementPos(NULL, cpColl, 0));
00719        while (!cStack.empty())
00720        {
00721               // Pop next position from stack
00722               _ElementPos pos = cStack.top();
00723               cStack.pop();
00724 
00725               // Iterate through elemenets in collection
00726               LONG nElements = 0;;
00727               pos.m_cpElementCollection->get_length(&nElements);
00728               for (int i = pos.m_nIndex; i < nElements; i++ )
00729               {
00730                      CComVariant vName(i);
00731                      CComVariant vIndex;
00732                      CIPtr(IDispatch) cpDisp;
00733 
00734                      hr = pos.m_cpElementCollection->item( vName, vIndex, &cpDisp );
00735                      if ( hr != S_OK )
00736                      {
00737                             continue;
00738                      }
00739                      CIPtr(IHTMLElement) cpElem = cpDisp;
00740                      if (cpElem == NULL)
00741                      {
00742                             continue;
00743                      }
00744 
00745                      // Get tag name
00746                      BSTR bstrTagName = NULL;
00747                      hr = cpElem->get_tagName(&bstrTagName);
00748                      CString szTagName = bstrTagName;
00749                      SysFreeString(bstrTagName);
00750 
00751                      // Add an icon to the tree
00752                      HTREEITEM htiParent = m_TabDOM.m_tcDOM.InsertItem(szTagName, IL_CLOSEDFOLDER, IL_CLOSEDFOLDER, pos.m_htiParent);
00753 
00754                      CIPtr(IDispatch) cpDispColl;
00755                      hr = cpElem->get_children(&cpDispColl);
00756                      if (hr == S_OK)
00757                      {
00758                             CIPtr(IHTMLElementCollection) cpChildColl = cpDispColl;
00759                             cStack.push(_ElementPos(pos.m_htiParent, pos.m_cpElementCollection, pos.m_nIndex + 1));
00760                             cStack.push(_ElementPos(htiParent, cpChildColl, 0));
00761                             break;
00762                      }
00763               }
00764        }
00765 }
00766 
00767 
00768 void CBrowseDlg::OnClose() 
00769 {
00770        DestroyWebBrowser();
00771        DestroyWindow();
00772 }
00773 
00774 
00775 void CBrowseDlg::OnDestroy() 
00776 {
00777        CDialog::OnDestroy();
00778        delete this;  
00779 }
00780 
00781 void CBrowseDlg::ExecOleCommand(const GUID *pguidGroup, DWORD nCmdId)
00782 {
00783        CComPtr<IUnknown> spUnkBrowser;
00784        m_pControlSite->GetControlUnknown(&spUnkBrowser);
00785 
00786        CIPtr(IOleCommandTarget) spCommandTarget = spUnkBrowser;
00787        if (spCommandTarget)
00788        {
00789               HRESULT hr = spCommandTarget->Exec(&CGID_MSHTML, nCmdId, 0, NULL, NULL);
00790               OutputString(_T("Exec(%d), returned %08x"), (int) nCmdId, hr);
00791        }
00792        else
00793        {
00794               OutputString(_T("Error: Browser does not support IOleCommandTarget"));
00795        }
00796 }
00797 
00798 
00799 void CBrowseDlg::OnEditBold()
00800 {
00801        ExecOleCommand(&CGID_MSHTML, IDM_BOLD);
00802 }
00803 
00804 void CBrowseDlg::OnEditItalic()
00805 {
00806        ExecOleCommand(&CGID_MSHTML, IDM_ITALIC);
00807 }
00808 
00809 void CBrowseDlg::OnEditUnderline()
00810 {
00811        ExecOleCommand(&CGID_MSHTML, IDM_UNDERLINE);
00812 }
00813 
00814 void CBrowseDlg::OnFileExit() 
00815 {
00816        OnClose();
00817 }
00818 
00819 void CBrowseDlg::OnViewRefresh() 
00820 {
00821     OnReload();
00822 }
00823 
00824 void CBrowseDlg::OnViewGotoBack() 
00825 {
00826        OnBackward();
00827 }
00828 
00829 void CBrowseDlg::OnViewGotoForward() 
00830 {
00831        OnForward();
00832 }
00833 
00834 void CBrowseDlg::OnUpdateViewGotoBack(CCmdUI* pCmdUI) 
00835 {
00836     pCmdUI->Enable(m_bCanGoBack);
00837 }
00838 
00839 void CBrowseDlg::OnUpdateViewGotoForward(CCmdUI* pCmdUI) 
00840 {
00841     pCmdUI->Enable(m_bCanGoForward);
00842 }
00843 
00844 void CBrowseDlg::OnViewGotoHome() 
00845 {
00846        CComPtr<IWebBrowser> webBrowser;
00847        if (SUCCEEDED(GetWebBrowser(&webBrowser)))
00848        {
00849               webBrowser->GoHome();
00850        }
00851 }
00852 
00853 void CBrowseDlg::OnEditCopy() 
00854 {
00855        ExecOleCommand(NULL, OLECMDID_COPY);
00856 }
00857 
00858 void CBrowseDlg::OnEditCut() 
00859 {
00860        ExecOleCommand(NULL, OLECMDID_CUT);
00861 }
00862 
00863 void CBrowseDlg::OnEditPaste() 
00864 {
00865        ExecOleCommand(NULL, OLECMDID_PASTE);
00866 }
00867 
00868 void CBrowseDlg::OnEditSelectAll() 
00869 {
00870        ExecOleCommand(NULL, OLECMDID_SELECTALL);
00871 }
00872 
00873 void CBrowseDlg::OnHelpAbout() 
00874 {
00875        AfxMessageBox(_T("CBrowse - Browser Control Test Harness"));
00876 }
00877 
00878 void CBrowseDlg::OnViewViewSource() 
00879 {
00880        ExecOleCommand(&CGID_MSHTML, 3);
00881 }
00882 
00883 void CBrowseDlg::OnFileSaveAs() 
00884 {
00885        ExecOleCommand(NULL, OLECMDID_SAVEAS);
00886 }
00887 
00888 void CBrowseDlg::OnFilePrint() 
00889 {
00890        ExecOleCommand(NULL, OLECMDID_PRINT);
00891 }
00892 
00893 void CBrowseDlg::OnFilePagesetup() 
00894 {
00895        ExecOleCommand(NULL, OLECMDID_PAGESETUP);
00896 }
00897 
00898 void CBrowseDlg::OnDebugVisible() 
00899 {
00900     VARIANT_BOOL visible = VARIANT_TRUE;
00901        CComPtr<IWebBrowser> webBrowser;
00902        if (SUCCEEDED(GetWebBrowser(&webBrowser)))
00903        {
00904               CIPtr(IWebBrowserApp) cpWebBrowser = webBrowser;
00905         cpWebBrowser->get_Visible(&visible);
00906         cpWebBrowser->put_Visible(visible == VARIANT_TRUE ? VARIANT_FALSE : VARIANT_TRUE);
00907        }
00908 }
00909 
00910 void CBrowseDlg::OnUpdateDebugVisible(CCmdUI* pCmdUI) 
00911 {
00912     VARIANT_BOOL visible = VARIANT_TRUE;
00913        CComPtr<IWebBrowser> webBrowser;
00914        if (SUCCEEDED(GetWebBrowser(&webBrowser)))
00915        {
00916               CIPtr(IWebBrowserApp) cpWebBrowser = webBrowser;
00917               cpWebBrowser->get_Visible(&visible);
00918        }
00919 
00920     pCmdUI->SetCheck(visible == VARIANT_TRUE ? 1 : 0);
00921 }
00922 
00923 void CBrowseDlg::OnDebugPostDataTest() 
00924 {
00925        CComPtr<IWebBrowser> webBrowser;
00926        if (SUCCEEDED(GetWebBrowser(&webBrowser)))
00927        {
00928               CIPtr(IWebBrowser2) cpWebBrowser = webBrowser;
00929 
00930         CComVariant vURL(L"http://www.mozilla.org/htdig-cgi/htsearch");
00931         const char *szPostData="config=htdig&restrict=&exclude=&words=embedding&method=and&format=builtin-long";
00932 
00933         size_t nSize = strlen(szPostData);
00934         SAFEARRAY *psa = SafeArrayCreateVector(VT_UI1, 0, nSize);
00935         
00936         LPSTR pPostData;
00937         SafeArrayAccessData(psa, (LPVOID*) &pPostData);
00938         memcpy(pPostData, szPostData, nSize);
00939         SafeArrayUnaccessData(psa);
00940 
00941         CComVariant vPostData;
00942         vPostData.vt = VT_ARRAY | VT_UI1;
00943         vPostData.parray = psa;
00944 
00945         CComVariant vHeaders(L"Content-Type: application/x-www-form-urlencoded\r\n");
00946 
00947         cpWebBrowser->Navigate2(
00948             &vURL,
00949             NULL, // Flags
00950             NULL, // Target
00951             &vPostData,
00952             &vHeaders  // Headers
00953         );
00954        }
00955 }
00956 
00957 void CBrowseDlg::OnViewEditmode() 
00958 {
00959     m_bEditMode = m_bEditMode ? FALSE : TRUE;
00960        DWORD nCmdID = m_bEditMode ? IDM_EDITMODE : IDM_BROWSEMODE;
00961        ExecOleCommand(&CGID_MSHTML, nCmdID);
00962 
00963 //     if (m_pControlSite)
00964 //     {
00965 //            m_pControlSite->SetAmbientUserMode((m_btnEditMode.GetCheck() == 0) ? FALSE : TRUE);
00966 //     }
00967 }
00968 
00969 void CBrowseDlg::OnViewOpenInNewWindow() 
00970 {
00971     m_bNewWindow = m_bNewWindow ? FALSE : TRUE;
00972 }
00973 
00974 void CBrowseDlg::OnUpdateViewEditmode(CCmdUI* pCmdUI) 
00975 {
00976     pCmdUI->SetCheck(m_bEditMode ? 1 : 0);
00977 }
00978 
00979 void CBrowseDlg::OnUpdateViewOpenInNewWindow(CCmdUI* pCmdUI) 
00980 {
00981     pCmdUI->SetCheck(1); //m_bNewWindow ? 1 : 0);
00982 }
00983