// Mymfc18View.cpp : implementation of the CMymfc18View class
//

#include "stdafx.h"
#include "mymfc18.h"

#include "MYMFC18DOC.h"
#include "Mymfc18View.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__ ;
#endif

//////////////////////////////////////////////////////////////////////
// CMymfc18View

IMPLEMENT_DYNCREATE(CMymfc18View, CFormView)
BEGIN_MESSAGE_MAP(CMymfc18View, CFormView)
    //{{AFX_MSG_MAP(CMymfc18View)
    ON_BN_CLICKED(IDC_CLEAR, OnClear)
    ON_COMMAND(ID_STUDENT_HOME, OnStudentHome)
    ON_COMMAND(ID_STUDENT_END, OnStudentEnd)
    ON_COMMAND(ID_STUDENT_PREV, OnStudentPrev)
    ON_COMMAND(ID_STUDENT_NEXT, OnStudentNext)
    ON_COMMAND(ID_STUDENT_INS, OnStudentIns)
    ON_COMMAND(ID_STUDENT_DEL, OnStudentDel)
    ON_UPDATE_COMMAND_UI(ID_STUDENT_HOME, OnUpdateStudentHome)
    ON_UPDATE_COMMAND_UI(ID_STUDENT_END, OnUpdateStudentEnd)
    ON_UPDATE_COMMAND_UI(ID_STUDENT_PREV, OnUpdateStudentHome)
    ON_UPDATE_COMMAND_UI(ID_STUDENT_NEXT, OnUpdateStudentEnd)
    ON_UPDATE_COMMAND_UI(ID_STUDENT_DEL, OnUpdateStudentDel)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

//////////////////////////////////////////////////////////////////////
// CMymfc18View construction/destruction

CMymfc18View::CMymfc18View() : CFormView(CMymfc18View::IDD)
{
    TRACE("Entering CMymfc18View constructor\n");
    //{{AFX_DATA_INIT(CMymfc18View)
    m_nGrade = 0;
    m_strName = _T("");
    //}}AFX_DATA_INIT
    m_position = NULL;
}

CMymfc18View::~CMymfc18View()
{
}

void CMymfc18View::DoDataExchange(CDataExchange* pDX)
{
    CFormView::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CMymfc18View)
    DDX_Text(pDX, IDC_GRADE, m_nGrade);
    DDV_MinMaxInt(pDX, m_nGrade, 0, 100);
    DDX_Text(pDX, IDC_NAME, m_strName);
    DDV_MaxChars(pDX, m_strName, 20);
    //}}AFX_DATA_MAP
}
BOOL CMymfc18View::PreCreateWindow(CREATESTRUCT& cs)
{
    // TODO: Modify the Window class or styles here by modifying
    //  the CREATESTRUCT cs
    return CFormView::PreCreateWindow(cs);
}

void CMymfc18View::OnInitialUpdate() 
{
    TRACE("Entering CMymfc18View::OnInitialUpdate\n");
    m_pList = GetDocument()->GetList();
    CFormView::OnInitialUpdate();
}

//////////////////////////////////////////////////////////////////////
// CMymfc18View diagnostics

#ifdef _DEBUG
void CMymfc18View::AssertValid() const
{
    CFormView::AssertValid();
}

void CMymfc18View::Dump(CDumpContext& dc) const
{
    CFormView::Dump(dc);
}

CMymfc18Doc* CMymfc18View::GetDocument() // non-debug version is inline
{
    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMymfc18Doc)));
    return (CMymfc18Doc*)m_pDocument;
}
#endif //_DEBUG

//////////////////////////////////////////////////////////////////////
// CMymfc18View message handlers

void CMymfc18View::OnClear() 
{
    TRACE("Entering CMymfc18View::OnClear\n");
    ClearEntry();
}

void CMymfc18View::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
    // called by OnInitialUpdate and by UpdateAllViews
    TRACE("Entering CMymfc18View::OnUpdate\n");
    m_position = m_pList->GetHeadPosition();
    GetEntry(m_position); // initial data for view
}

void CMymfc18View::OnStudentHome() 
{
    TRACE("Entering CMymfc18View::OnStudentHome\n");
    // need to deal with list empty condition
    if (!m_pList->IsEmpty()) {
        m_position = m_pList->GetHeadPosition();
        GetEntry(m_position);
    }
}

void CMymfc18View::OnStudentEnd() 
{
    TRACE("Entering CMymfc18View::OnStudentEnd\n");
    if (!m_pList->IsEmpty()) {
        m_position = m_pList->GetTailPosition();
        GetEntry(m_position);
    }
}

void CMymfc18View::OnStudentPrev() 
{
    POSITION pos;
    TRACE("Entering CMymfc18View::OnStudentPrev\n");
    if ((pos = m_position) != NULL) {
        m_pList->GetPrev(pos);
        if (pos) {
            GetEntry(pos);
      m_position = pos;
        }
    }
}

void CMymfc18View::OnStudentNext() 
{
    POSITION pos;
    TRACE("Entering CMymfc18View::OnStudentNext\n");
    if ((pos = m_position) != NULL) {
        m_pList->GetNext(pos);
        if (pos) {
            GetEntry(pos);
            m_position = pos;
        }
    }
}
void CMymfc18View::OnStudentIns() 
{
    TRACE("Entering CMymfc18View::OnStudentIns\n");
    InsertEntry(m_position);
    GetDocument()->SetModifiedFlag();
    GetDocument()->UpdateAllViews(this);
}

void CMymfc18View::OnStudentDel() 
{
    // deletes current entry and positions to next one or head
    POSITION pos;
    TRACE("Entering CMymfc18View::OnStudentDel\n");
    if ((pos = m_position) != NULL) {
        m_pList->GetNext(pos);
        if (pos == NULL) {
            pos = m_pList->GetHeadPosition();
            TRACE("GetHeadPos = %ld\n", pos);
            if (pos == m_position) {
                pos = NULL;
            }
        }
        GetEntry(pos);
        CStudent* ps = m_pList->GetAt(m_position);
        m_pList->RemoveAt(m_position);
        delete ps;
        m_position = pos;
        GetDocument()->SetModifiedFlag();
        GetDocument()->UpdateAllViews(this);
    }
}

void CMymfc18View::OnUpdateStudentHome(CCmdUI* pCmdUI) 
{
    // called during idle processing and when Student menu drops down
    POSITION pos;

    // enables button if list not empty and not at home already
    pos = m_pList->GetHeadPosition();
    pCmdUI->Enable((m_position != NULL) && (pos != m_position));
}

void CMymfc18View::OnUpdateStudentEnd(CCmdUI* pCmdUI) 
{
    // called during idle processing and when Student menu drops down
    POSITION pos;

    // enables button if list not empty and not at end already
    pos = m_pList->GetTailPosition();
    pCmdUI->Enable((m_position != NULL) && (pos != m_position));
}

void CMymfc18View::OnUpdateStudentDel(CCmdUI* pCmdUI) 
{
    // called during idle processing and when Student menu drops down
    pCmdUI->Enable(m_position != NULL);
}

void CMymfc18View::GetEntry(POSITION position)
{
    if (position) {
        CStudent* pStudent = m_pList->GetAt(position);
        m_strName = pStudent->m_strName;
        m_nGrade = pStudent->m_nGrade;
    }
    else {
        ClearEntry();
    }
    UpdateData(FALSE);
}

void CMymfc18View::InsertEntry(POSITION position)
{
    if (UpdateData(TRUE)) {
        // UpdateData returns FALSE if it detects a user error
        CStudent* pStudent = new CStudent;
        pStudent->m_strName = m_strName;
        pStudent->m_nGrade = m_nGrade;
        m_position = m_pList->InsertAfter(m_position, pStudent);
    }
}

void CMymfc18View::ClearEntry()
{
    m_strName = "";
    m_nGrade = 0;
    UpdateData(FALSE);
    ((CDialog*) this)->GotoDlgCtrl(GetDlgItem(IDC_NAME));
}

