// Mymfc17View.cpp : implementation of the CMymfc17View class
//

#include "stdafx.h"
#include "mymfc17.h"

#include "Mymfc17Doc.h"
#include "Mymfc17View.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__ ;
#endif

//////////////////////////////////////////////////////////////////////
// CMymfc17View

IMPLEMENT_DYNCREATE(CMymfc17View, CFormView)
BEGIN_MESSAGE_MAP(CMymfc17View, CFormView)
    //{{AFX_MSG_MAP(CMymfc17View)
    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()

//////////////////////////////////////////////////////////////////////
// CMymfc17View construction/destruction

CMymfc17View::CMymfc17View() : CFormView(CMymfc17View::IDD)
{
    TRACE("Entering CMymfc17View constructor\n");
    //{{AFX_DATA_INIT(CMymfc17View)
    m_nGrade = 0;
    m_strName = _T("");
    //}}AFX_DATA_INIT
    m_position = NULL;
}

CMymfc17View::~CMymfc17View()
{
}

void CMymfc17View::DoDataExchange(CDataExchange* pDX)
{
    CFormView::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CMymfc17View)
    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 CMymfc17View::PreCreateWindow(CREATESTRUCT& cs)
{
    // TODO: Modify the Window class or styles here by modifying
    //  the CREATESTRUCT cs
    return CFormView::PreCreateWindow(cs);
}

void CMymfc17View::OnInitialUpdate() 
{
    TRACE("Entering CMymfc17View::OnInitialUpdate\n");
    m_pList = GetDocument()->GetList();
    CFormView::OnInitialUpdate();
}

//////////////////////////////////////////////////////////////////////
// CMymfc17View diagnostics

#ifdef _DEBUG
void CMymfc17View::AssertValid() const
{
    CFormView::AssertValid();
}

void CMymfc17View::Dump(CDumpContext& dc) const
{
    CFormView::Dump(dc);
}

CMymfc17Doc* CMymfc17View::GetDocument() // non-debug version is inline
{
    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMymfc17Doc)));
    return (CMymfc17Doc*)m_pDocument;
}
#endif //_DEBUG

//////////////////////////////////////////////////////////////////////
// CMymfc17View message handlers

void CMymfc17View::OnClear() 
{
    TRACE("Entering CMymfc17View::OnClear\n");
    ClearEntry();
}

void CMymfc17View::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
    // called by OnInitialUpdate and by UpdateAllViews
    TRACE("Entering CMymfc17View::OnUpdate\n");
    m_position = m_pList->GetHeadPosition();
    GetEntry(m_position); // initial data for view
}

void CMymfc17View::OnStudentHome() 
{
    TRACE("Entering CMymfc17View::OnStudentHome\n");
    // need to deal with list empty condition
    if (!m_pList->IsEmpty()) {
        m_position = m_pList->GetHeadPosition();
        GetEntry(m_position);
    }
}

void CMymfc17View::OnStudentEnd() 
{
    TRACE("Entering CMymfc17View::OnStudentEnd\n");
    if (!m_pList->IsEmpty()) {
        m_position = m_pList->GetTailPosition();
        GetEntry(m_position);
    }
}

void CMymfc17View::OnStudentPrev() 
{
    POSITION pos;
    TRACE("Entering CMymfc17View::OnStudentPrev\n");
    if ((pos = m_position) != NULL) {
        m_pList->GetPrev(pos);
        if (pos) {
            GetEntry(pos);
      m_position = pos;
        }
    }
}

void CMymfc17View::OnStudentNext() 
{
    POSITION pos;
    TRACE("Entering CMymfc17View::OnStudentNext\n");
    if ((pos = m_position) != NULL) {
        m_pList->GetNext(pos);
        if (pos) {
            GetEntry(pos);
            m_position = pos;
        }
    }
}
void CMymfc17View::OnStudentIns() 
{
    TRACE("Entering CMymfc17View::OnStudentIns\n");
    InsertEntry(m_position);
    GetDocument()->SetModifiedFlag();
    GetDocument()->UpdateAllViews(this);
}

void CMymfc17View::OnStudentDel() 
{
    // deletes current entry and positions to next one or head
    POSITION pos;
    TRACE("Entering CMymfc17View::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 CMymfc17View::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 CMymfc17View::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 CMymfc17View::OnUpdateStudentDel(CCmdUI* pCmdUI) 
{
    // called during idle processing and when Student menu drops down
    pCmdUI->Enable(m_position != NULL);
}

void CMymfc17View::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 CMymfc17View::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 CMymfc17View::ClearEntry()
{
    m_strName = "";
    m_nGrade = 0;
    UpdateData(FALSE);
    ((CDialog*) this)->GotoDlgCtrl(GetDlgItem(IDC_NAME));
}

