// mymfc29EView.cpp : implementation of the CMymfc29EView class
//

#include "stdafx.h"
#include "mymfc29E.h"

#include "mymfc29EDoc.h"
#include "mymfc29EView.h"
#include "AlarmDialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMymfc29EView

IMPLEMENT_DYNCREATE(CMymfc29EView, CView)

BEGIN_MESSAGE_MAP(CMymfc29EView, CView)
	//{{AFX_MSG_MAP(CMymfc29EView)
	ON_COMMAND(ID_BANKOLE_LOAD, OnBankoleLoad)
	ON_UPDATE_COMMAND_UI(ID_BANKOLE_LOAD, OnUpdateBankoleLoad)
	ON_COMMAND(ID_BANKOLE_TEST, OnBankoleTest)
	ON_UPDATE_COMMAND_UI(ID_BANKOLE_TEST, OnUpdateBankoleTest)
	ON_COMMAND(ID_BANKOLE_UNLOAD, OnBankoleUnload)
	ON_UPDATE_COMMAND_UI(ID_BANKOLE_UNLOAD, OnUpdateBankoleUnload)
	ON_COMMAND(ID_CLOCKOLE_CREATEALARM, OnClockoleCreatealarm)
	ON_UPDATE_COMMAND_UI(ID_CLOCKOLE_CREATEALARM, OnUpdateClockoleCreatealarm)
	ON_COMMAND(ID_CLOCKOLE_LOAD, OnClockoleLoad)
	ON_UPDATE_COMMAND_UI(ID_CLOCKOLE_LOAD, OnUpdateClockoleLoad)
	ON_COMMAND(ID_CLOCKOLE_REFRESHTIME, OnClockoleRefreshtime)
	ON_UPDATE_COMMAND_UI(ID_CLOCKOLE_REFRESHTIME, OnUpdateClockoleRefreshtime)
	ON_COMMAND(ID_CLOCKOLE_UNLOAD, OnClockoleUnload)
	ON_UPDATE_COMMAND_UI(ID_CLOCKOLE_UNLOAD, OnUpdateClockoleUnload)
	ON_COMMAND(ID_DLLOLE_GETDATA, OnDlloleGetdata)
	ON_UPDATE_COMMAND_UI(ID_DLLOLE_GETDATA, OnUpdateDlloleGetdata)
	ON_COMMAND(ID_DLLOLE_LOAD, OnDlloleLoad)
	ON_UPDATE_COMMAND_UI(ID_DLLOLE_LOAD, OnUpdateDlloleLoad)
	ON_COMMAND(ID_DLLOLE_UNLOAD, OnDlloleUnload)
	ON_UPDATE_COMMAND_UI(ID_DLLOLE_UNLOAD, OnUpdateDlloleUnload)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMymfc29EView construction/destruction

CMymfc29EView::CMymfc29EView()
{
	// TODO: add construction code here

}

CMymfc29EView::~CMymfc29EView()
{
}

BOOL CMymfc29EView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMymfc29EView drawing

void CMymfc29EView::OnDraw(CDC* pDC)
{
    CMymfc29EDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    pDC->TextOut(100, 0, "--COleDispatchDriver usage, mymfc29E--");
    pDC->TextOut(10, 25, "Run this program from the debugger to see test output.");
    pDC->TextOut(10, 50, "The MYMFC29A, MYMFC29B and MYMFC29C components...");
    pDC->TextOut(10, 75, "...must be built and registered prior to loading this crap...");
    pDC->TextOut(10, 100, "Originally for VC++ 6 + Excel 97 but here VC++ 6 + Excel 2003...");
}

/////////////////////////////////////////////////////////////////////////////
// CMymfc29EView diagnostics

#ifdef _DEBUG
void CMymfc29EView::AssertValid() const
{
	CView::AssertValid();
}

void CMymfc29EView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CMymfc29EDoc* CMymfc29EView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMymfc29EDoc)));
	return (CMymfc29EDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMymfc29EView message handlers

void CMymfc29EView::OnBankoleLoad() 
{
    if(m_bank.CreateInstance(__uuidof(Bank)) != S_OK)
    {
        AfxMessageBox("Bank component not found");
        return;
    }
	else
	{
		AfxMessageBox("OK, Bank component found lor!");
	}
}

void CMymfc29EView::OnUpdateBankoleLoad(CCmdUI* pCmdUI) 
{
    pCmdUI->Enable(m_bank.GetInterfacePtr() == NULL);
}

void CMymfc29EView::OnBankoleTest() 
{
    try {
        m_bank->Deposit(20.0);
        m_bank->Withdrawal(15.0);
        TRACE("new balance = %f\n", m_bank->GetBalance());
    } catch(_com_error& e)
	{
        AfxMessageBox(e.ErrorMessage());
    }
}

void CMymfc29EView::OnUpdateBankoleTest(CCmdUI* pCmdUI) 
{
    pCmdUI->Enable(m_bank.GetInterfacePtr() != NULL);
}

void CMymfc29EView::OnBankoleUnload() 
{
    m_bank.Release();
}

void CMymfc29EView::OnUpdateBankoleUnload(CCmdUI* pCmdUI) 
{
    pCmdUI->Enable(m_bank.GetInterfacePtr() != NULL);
}

void CMymfc29EView::OnClockoleCreatealarm() 
{
    CAlarmDialog dlg;
    try
    {
        if (dlg.DoModal() == IDOK)
        {
            COleDateTime dt(2004, 12, 23, dlg.m_nHours, dlg.m_nMinutes, 
                dlg.m_nSeconds);
            LPDISPATCH pAlarm = m_clock->CreateAlarm(dt);
            m_alarm.Attach((IAlarm*) pAlarm);  // releases prior object!
            m_clock->RefreshWin();
        }
    }
    catch(_com_error& e)
    {
        AfxMessageBox(e.ErrorMessage());
    }
}

void CMymfc29EView::OnUpdateClockoleCreatealarm(CCmdUI* pCmdUI) 
{
    pCmdUI->Enable(m_clock.GetInterfacePtr() != NULL);
}

void CMymfc29EView::OnClockoleLoad() 
{
    if(m_clock.CreateInstance(__uuidof(Document)) != S_OK)
    {
        AfxMessageBox("Clock component not found");
        return;
    }
    try
    {
        m_clock->PutFigure(0, COleVariant("XII"));
        m_clock->PutFigure(1, COleVariant("III"));
        m_clock->PutFigure(2, COleVariant("VI"));
        m_clock->PutFigure(3, COleVariant("IX"));
        OnClockoleRefreshtime();
        m_clock->ShowWin();
    }
    catch(_com_error& e)
    {
        AfxMessageBox(e.ErrorMessage());
    }
}

void CMymfc29EView::OnUpdateClockoleLoad(CCmdUI* pCmdUI) 
{
    pCmdUI->Enable(m_clock.GetInterfacePtr() == NULL);
}

void CMymfc29EView::OnClockoleRefreshtime() 
{
    COleDateTime now = COleDateTime::GetCurrentTime();
    try
    {
        m_clock->PutTime(now);
        m_clock->RefreshWin();
    }
    catch(_com_error& e)
    {
        AfxMessageBox(e.ErrorMessage());
    }
}

void CMymfc29EView::OnUpdateClockoleRefreshtime(CCmdUI* pCmdUI) 
{
    pCmdUI->Enable(m_clock.GetInterfacePtr() != NULL);
}

void CMymfc29EView::OnClockoleUnload() 
{
    m_clock.Release();
}

void CMymfc29EView::OnUpdateClockoleUnload(CCmdUI* pCmdUI) 
{
    pCmdUI->Enable(m_clock.GetInterfacePtr() != NULL);
}

void CMymfc29EView::OnDlloleGetdata() 
{
    try {
        m_auto->DisplayDialog();
        COleVariant vaData = m_auto->GetTextData();
        ASSERT(vaData.vt == VT_BSTR);
        CString strTextData = vaData.bstrVal;
        long lData = m_auto->GetLongData();
        TRACE("CMymfc29DView::OnDlloleGetdata-long = %ld, text = %s\n",
              lData, strTextData);
    }
    catch(_com_error& e)
    {
        AfxMessageBox(e.ErrorMessage());
    }
}

void CMymfc29EView::OnUpdateDlloleGetdata(CCmdUI* pCmdUI) 
{
    pCmdUI->Enable(m_auto.GetInterfacePtr() != NULL);
}

void CMymfc29EView::OnDlloleLoad() 
{
    if(m_auto.CreateInstance(__uuidof(Auto)) != S_OK)
    {
        AfxMessageBox("Mymfc29BAuto component not found");
        return;
    }
	else
	{
		AfxMessageBox("Mymfc29BAuto component found lol!");
	}

}

void CMymfc29EView::OnUpdateDlloleLoad(CCmdUI* pCmdUI) 
{
    pCmdUI->Enable(m_auto.GetInterfacePtr() == NULL);
}

void CMymfc29EView::OnDlloleUnload() 
{
    m_auto.Release();
}

void CMymfc29EView::OnUpdateDlloleUnload(CCmdUI* pCmdUI) 
{
    pCmdUI->Enable(m_auto.GetInterfacePtr() != NULL);
}

