// mymfc24BView.cpp : implementation of the CMymfc24BView class
//

#include "stdafx.h"
#include "mymfc24B.h"

#include "mymfc24BDoc.h"
#include "mymfc24BView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMymfc24BView

IMPLEMENT_DYNCREATE(CMymfc24BView, CView)

BEGIN_MESSAGE_MAP(CMymfc24BView, CView)
	//{{AFX_MSG_MAP(CMymfc24BView)
	ON_WM_CREATE()
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BEGIN_EVENTSINK_MAP(CMymfc24BView, CView)
    ON_EVENT(CMymfc24BView, ID_BROWSER_SEARCH, 100, OnBeforeNavigateExplorer1, VTS_BSTR VTS_I4 VTS_BSTR VTS_PVARIANT VTS_BSTR VTS_PBOOL)
    ON_EVENT(CMymfc24BView, ID_BROWSER_TARGET, 113,  OnTitleChangeExplorer2, VTS_BSTR)
END_EVENTSINK_MAP()

const char CMymfc24BView::s_engineYahoo[] = "http://www.yahoo.com/";

/////////////////////////////////////////////////////////////////////////////
// CMymfc24BView construction/destruction

CMymfc24BView::CMymfc24BView()
{
	// TODO: add construction code here

}

CMymfc24BView::~CMymfc24BView()
{
}

BOOL CMymfc24BView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMymfc24BView drawing

void CMymfc24BView::OnDraw(CDC* pDC)
{
	CMymfc24BDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CMymfc24BView diagnostics

#ifdef _DEBUG
void CMymfc24BView::AssertValid() const
{
	CView::AssertValid();
}

void CMymfc24BView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CMymfc24BDoc* CMymfc24BView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMymfc24BDoc)));
	return (CMymfc24BDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMymfc24BView message handlers


int CMymfc24BView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
        return -1;
    
    DWORD dwStyle = WS_VISIBLE | WS_CHILD;    
    if (m_search.Create(NULL, dwStyle, CRect(0, 0, 100, 100),
                        this, ID_BROWSER_SEARCH) == 0) {
        AfxMessageBox("Unable to create search control!\n");
        return -1;
    }
    m_search.Navigate(s_engineYahoo, NULL, NULL, NULL, NULL);

    if (m_target.Create(NULL, dwStyle, CRect(0, 0, 100, 100),
                        this, ID_BROWSER_TARGET) == 0) {
        AfxMessageBox("Unable to create target control!\n");
        return -1;
    }
    m_target.GoHome(); // as defined in Internet Explorer 4 options

    return 0;
}

void CMymfc24BView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);

    CRect rectClient;
    GetClientRect(rectClient);
    CRect rectBrowse(rectClient);
    rectBrowse.right = rectClient.right / 2;
    CRect rectSearch(rectClient);
    rectSearch.left = rectClient.right / 2;

    m_target.SetWidth(rectBrowse.right - rectBrowse.left);
    m_target.SetHeight(rectBrowse.bottom - rectBrowse.top);
    m_target.UpdateWindow();

    m_search.SetLeft(rectSearch.left);
    m_search.SetWidth(rectSearch.right - rectSearch.left);
    m_search.SetHeight(rectSearch.bottom - rectSearch.top);
    m_search.UpdateWindow();
}

void CMymfc24BView::OnBeforeNavigateExplorer1(LPCTSTR URL,
    long Flags, LPCTSTR TargetFrameName,
    VARIANT FAR* PostData, LPCTSTR Headers, BOOL FAR* Cancel)
{
    TRACE("CMymfc24BView::OnBeforeNavigateExplorer1 -- URL = %s\n", URL);

    if (!strnicmp(URL, s_engineYahoo, strlen(s_engineYahoo))) {
        return;
    }
    m_target.Navigate(URL, NULL, NULL, PostData, NULL);
    *Cancel = TRUE;
}

void CMymfc24BView::OnTitleChangeExplorer2(LPCTSTR Text)
{
    // Careful!  Event could fire before we're ready.
    CWnd* pWnd = AfxGetApp()->m_pMainWnd;
    if (pWnd != NULL) {
        if (::IsWindow(pWnd->m_hWnd)) {
            pWnd->SetWindowText(Text);
        }
    }
}
