Click here to Skip to main content
15,881,875 members
Articles / Programming Languages / C++

Project Line Counter Add-In v2.10 for VS.NET and VC6

Rate me:
Please Sign up or sign in to vote.
4.92/5 (38 votes)
29 Jun 2003 447.5K   5.3K   142  
Get statistics about your source code with a click of a button
/***************************************************************************/
/* NOTE:                                                                   */
/* This document is copyright (c) by Oz Solomonovich, and is bound by the  */
/* MIT open source license (www.opensource.org/licenses/mit-license.html). */
/* See License.txt for more information.                                   */
/***************************************************************************/

// GalleryDlg.cpp : implementation file
//

#include "stdafx.h"
#include "linecount.h"
#include "GalleryDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

const int WM_GOT_GALLERY_INFO = (WM_USER + 1);


// utility functions
CString GetResourceAsString(HMODULE hModule, LPCTSTR lpName, LPCTSTR lpType)
{
    HRSRC hRsrc = FindResource(hModule, lpName, lpType);
    if (hRsrc != NULL)
    {
        HGLOBAL hGlob = LoadResource(hModule, hRsrc);
        if (hGlob != NULL)
        {
            LPVOID pRes = LockResource(hGlob);
            if (pRes != NULL)
            {
                return CString((char *)pRes, SizeofResource(hModule, hRsrc));
            }
        }
    }
    ASSERT(!"Resource not found");
    return "";
}

bool LoadXMLDocument(LPCWSTR src, IXMLDOMDocumentPtr& outPtr, CString& outErr)
{
	IUnknownPtr object;
	CLSID clsid;
    outPtr = NULL;

	if (SUCCEEDED(CLSIDFromProgID(L"MSXML.DOMDocument", &clsid)))
	{
        object.CreateInstance(clsid);
        outPtr = object;
	}

    if (outPtr == NULL)
    {
        outErr = "Failed to create MSXML Object.  "
            "You need to install the MSXML library.";
        return false;
    }

    HRESULT hr = outPtr->put_async(VARIANT_FALSE);
    if (SUCCEEDED(hr))
    {
       hr = outPtr->put_validateOnParse(VARIANT_FALSE);
    }
    if (SUCCEEDED(hr))
    {
       hr = outPtr->put_resolveExternals(VARIANT_FALSE);
    }

    if (src == NULL) return true;

    CComBSTR bStr;
    VARIANT_BOOL bSuccess;
    CComVariant v;
    v = bStr;
    outPtr->load(CComVariant(src), &bSuccess);

    if (bSuccess != VARIANT_TRUE)
    {
        CComBSTR bStr;
        IXMLDOMParseErrorPtr pObjError;
        hr = outPtr->get_parseError(&pObjError);
        hr = pObjError->get_reason(&bStr);
        outErr.Format(
            "There was an error downloading the XML data: %S", bStr);
        return false;
    }

    outErr = "";
    return true;
}



/////////////////////////////////////////////////////////////////////////////
// CGalleryDlg dialog


CGalleryDlg::CGalleryDlg(CWnd* pParent /*=NULL*/)
	: CResizableDialog(CGalleryDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGalleryDlg)
	m_sMessage = _T("");
	//}}AFX_DATA_INIT

    m_sMessage = "Downloading information...";
}


void CGalleryDlg::DoDataExchange(CDataExchange* pDX)
{
	CResizableDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGalleryDlg)
	DDX_Control(pDX, IDC_MESSAGE, m_ctlMessage);
	DDX_Text(pDX, IDC_MESSAGE, m_sMessage);
	DDX_Control(pDX, IDC_HTML, m_ctlWebBrowser);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CGalleryDlg, CResizableDialog)
	//{{AFX_MSG_MAP(CGalleryDlg)
	//}}AFX_MSG_MAP
    ON_MESSAGE(WM_GOT_GALLERY_INFO, OnGotGalleryInfo)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGalleryDlg message handlers

BOOL CGalleryDlg::OnInitDialog() 
{
	CResizableDialog::OnInitDialog();

    AfxBeginThread(BeginFetchGalleryInfo, (LPVOID)this);

    AddAnchor(m_ctlWebBrowser.m_hWnd, TOP_LEFT, BOTTOM_RIGHT);
    AddAnchor(IDC_MESSAGE,            TOP_LEFT, BOTTOM_RIGHT);
    AddAnchor(IDOK,                   BOTTOM_RIGHT);

    EnableSaveRestore("GalleryDlg");

	return TRUE;
}

LRESULT CGalleryDlg::OnGotGalleryInfo(WPARAM wParam, LPARAM lParam)
{
    if (!m_sGalleryError.IsEmpty())
    {
        m_sMessage = m_sGalleryError;
        UpdateData(FALSE);
    }
    else
    {
        // reset the current document... when done, the event handler will
        // set the pending HTML text.
        m_ctlWebBrowser.Navigate("about:blank", NULL, NULL, NULL, NULL);
    }
    return 1;
}


BEGIN_EVENTSINK_MAP(CGalleryDlg, CResizableDialog)
    //{{AFX_EVENTSINK_MAP(CGalleryDlg)
	ON_EVENT(CGalleryDlg, IDC_HTML, 252 /* NavigateComplete2 */, OnNavigateComplete2Html, VTS_DISPATCH VTS_PVARIANT)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CGalleryDlg::OnNavigateComplete2Html(LPDISPATCH pDisp, VARIANT FAR* URL) 
{
    if (m_sPendingHTML.GetLength() > 0)
    {
        CComQIPtr<IHTMLDocument2> pDoc;

        pDoc = m_ctlWebBrowser.GetDocument();

        if (pDoc != NULL)
        {
            CComQIPtr<IHTMLElement> pBody;
  
            pDoc->get_body(&pBody);
            pBody->put_innerHTML(CComBSTR(m_sPendingHTML));

            m_ctlMessage.ShowWindow(SW_HIDE);
            m_ctlWebBrowser.ShowWindow(SW_SHOW);

            // force resize (otherwise browser comes out wrong size)
            CRect rc;
            GetWindowRect(rc);
            rc.bottom++;
            MoveWindow(rc);
            rc.bottom--;
            MoveWindow(rc);

            m_sPendingHTML = L"";
        }
    }
}

UINT CGalleryDlg::BeginFetchGalleryInfo(LPVOID pContext)
{
    CGalleryDlg * const pThis = (CGalleryDlg *)pContext;

    IXMLDOMDocumentPtr pXMLDoc;
    IXMLDOMDocumentPtr pXSLDoc;

    CString sErr;
    CComBSTR bStr;
    HRESULT hr;
    VARIANT_BOOL bSuccess;

    CoInitialize(NULL);

    if (LoadXMLDocument(L"http://www.wndtabs.com/ftp/PLC-Gallery/gallery.xml", pXMLDoc, sErr) &&
        LoadXMLDocument(NULL, pXSLDoc, sErr))
    {
        CComBSTR bStrXSL = CComBSTR(GetResourceAsString(
            AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_XSL_GALLERY), "XSL"));
        pXSLDoc->loadXML(bStrXSL, &bSuccess);
        if (bSuccess != VARIANT_TRUE)
        {
            IXMLDOMParseErrorPtr pObjError;
            hr = pXSLDoc->get_parseError(&pObjError);
            hr = pObjError->get_reason(&bStr);
            sErr.Format(
                "There was loading the XSL resource: %S", bStr);
        }
        else
        {
            hr = pXMLDoc->transformNode(pXSLDoc, &bStr);

            if (SUCCEEDED(hr))
            {
                sErr.Empty();
                pThis->m_sPendingHTML = bStr;
            }
            else
            {
                sErr = "Could not transform the XML data.";
            }
        }
    }
    pThis->m_sGalleryError = sErr;
    pThis->PostMessage(WM_GOT_GALLERY_INFO);

    CoUninitialize();

    return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Experion
Canada Canada
You may know Oz from his WndTabs days. Oz has long since left client side development to work on web technologies and to consult in the R&D management field.

Comments and Discussions