// WebDialog.cpp : implementation file
//

#include "stdafx.h"
#include "error demo.h"
#include "WebDialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CWebDialog dialog

CWebDialog::CWebDialog(CWnd* pParent /*=NULL*/)
	: CDialog(CWebDialog::IDD, pParent)
{
	EnableAutomation();

	//{{AFX_DATA_INIT(CWebDialog)
	//}}AFX_DATA_INIT
}

void CWebDialog::OnFinalRelease()
{
	// When the last reference for an automation object is released
	// OnFinalRelease is called.  The base class will automatically
	// deletes the object.  Add additional cleanup required for your
	// object before calling the base class.

	CDialog::OnFinalRelease();
}

void CWebDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWebDialog)
	DDX_Control(pDX, IDC_URL_LIST, m_cbURL);
	DDX_Control(pDX, IDC_EXPLORER1, m_cWebBrowser);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWebDialog, CDialog)
	//{{AFX_MSG_MAP(CWebDialog)
	ON_BN_CLICKED(IDC_GO_BACK, OnGoBack)
	ON_BN_CLICKED(IDC_GO_FORWARD, OnGoForward)
	ON_CBN_SELCHANGE(IDC_URL_LIST, OnSelchangeUrlList)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BEGIN_DISPATCH_MAP(CWebDialog, CDialog)
	//{{AFX_DISPATCH_MAP(CWebDialog)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()

// Note: we add support for IID_IWebDialog to support typesafe binding
//  from VBA.  This IID must match the GUID that is attached to the 
//  dispinterface in the .ODL file.

// {A48CAF07-560A-11D1-AC25-509148C10000}
static const IID IID_IWebDialog =
{ 0xa48caf07, 0x560a, 0x11d1, { 0xac, 0x25, 0x50, 0x91, 0x48, 0xc1, 0x0, 0x0 } };

BEGIN_INTERFACE_MAP(CWebDialog, CDialog)
	INTERFACE_PART(CWebDialog, IID_IWebDialog, Dispatch)
END_INTERFACE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWebDialog message handlers

BEGIN_EVENTSINK_MAP(CWebDialog, CDialog)
    //{{AFX_EVENTSINK_MAP(CWebDialog)
	ON_EVENT(CWebDialog, IDC_EXPLORER1, 252 /* NavigateComplete2 */, OnNavigateComplete2Explorer1, VTS_DISPATCH VTS_PVARIANT)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

BOOL CWebDialog::OnInitDialog() 
{
	CDialog::OnInitDialog();
	COleVariant noArg;

	m_cWebBrowser.SetMenuBar( TRUE );
	m_cWebBrowser.SetToolBar( TRUE );
	m_cWebBrowser.Navigate( m_csURL, &noArg, &noArg, &noArg, &noArg );
	m_cbURL.SetCurSel(0);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CWebDialog::OnNavigateComplete2Explorer1(LPDISPATCH pDisp, VARIANT FAR* URL) 
{
	m_csURL = m_cWebBrowser.GetLocationURL();
			// see if this entry is already in the list
	if( m_cbURL.FindStringExact( -1, m_csURL ) == CB_ERR )
			// if not then add it to the list
		m_cbURL.SetCurSel( m_cbURL.AddString( m_cWebBrowser.GetLocationURL() ) );
			// and update the list and selection fields
	UpdateData( FALSE );
}

void CWebDialog::OnGoBack() 
{
	m_cWebBrowser.GoBack();
}

void CWebDialog::OnGoForward() 
{
	m_cWebBrowser.GoForward();
}

void CWebDialog::OnSelchangeUrlList() 
{
	COleVariant noArg;

	UpdateData( TRUE );
	m_cbURL.GetLBText( m_cbURL.GetCurSel(), m_csURL );
			// get the selected URL from the combo list box
	m_cWebBrowser.Navigate( m_csURL, &noArg, &noArg, &noArg, &noArg );
			// and call the Navigate method to display the location
}

