// GridCtrlDlg.cpp : implementation file
//

#include "stdafx.h"
#include "GridCtrl.h"
#include "GridCtrlDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
        CAboutDlg();

// Dialog Data
        //{{AFX_DATA(CAboutDlg)
        enum { IDD = IDD_ABOUTBOX };
        //}}AFX_DATA

        // ClassWizard generated virtual function overrides
        //{{AFX_VIRTUAL(CAboutDlg)
        protected:
        virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
        //}}AFX_VIRTUAL

// Implementation
protected:
        //{{AFX_MSG(CAboutDlg)
        //}}AFX_MSG
        DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
        //{{AFX_DATA_INIT(CAboutDlg)
        //}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
        CDialog::DoDataExchange(pDX);
        //{{AFX_DATA_MAP(CAboutDlg)
        //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
        //{{AFX_MSG_MAP(CAboutDlg)
                // No message handlers
        //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGridCtrlDlg dialog

CGridCtrlDlg::CGridCtrlDlg(CWnd* pParent /*=NULL*/)
        : CDialog(CGridCtrlDlg::IDD, pParent)
{
        //{{AFX_DATA_INIT(CGridCtrlDlg)
                // NOTE: the ClassWizard will add member initialization here
        //}}AFX_DATA_INIT
        // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
        m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGridCtrlDlg::DoDataExchange(CDataExchange* pDX)
{
        CDialog::DoDataExchange(pDX);
        //{{AFX_DATA_MAP(CGridCtrlDlg)
        DDX_Control(pDX, IDC_EDIT, m_edit);
        DDX_Control(pDX, IDC_MSFLEXGRID1, m_grid);
        //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGridCtrlDlg, CDialog)
        //{{AFX_MSG_MAP(CGridCtrlDlg)
        ON_WM_SYSCOMMAND()
        ON_WM_PAINT()
        ON_WM_QUERYDRAGICON()
        ON_BN_CLICKED(IDC_CALC, OnCalc)
        //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGridCtrlDlg message handlers

BOOL CGridCtrlDlg::OnInitDialog()
{
        CDialog::OnInitDialog();

        // Add "About..." menu item to system menu.

        // IDM_ABOUTBOX must be in the system command range.
        ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
        ASSERT(IDM_ABOUTBOX < 0xF000);

        CMenu* pSysMenu = GetSystemMenu(FALSE);
        if (pSysMenu != NULL)
        {
                CString strAboutMenu;
                strAboutMenu.LoadString(IDS_ABOUTBOX);
                if (!strAboutMenu.IsEmpty())
                {
                        pSysMenu->AppendMenu(MF_SEPARATOR);
                        pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
                }
        }

        // Set the icon for this dialog.  The framework does this automatically
        //  when the application's main window is not a dialog
        SetIcon(m_hIcon, TRUE);                 // Set big icon
        SetIcon(m_hIcon, FALSE);                // Set small icon
        
        // TODO: Add extra initialization here
        m_bEditing=FALSE;
        m_nRow=1;
        m_nCol=1;
        char* arCols[4] = {"Jan", "Feb", "Mar", "Apr"};
        char* arRows[4] = {"Gas", "Phone", "Electric", "Total"};
        m_grid.SetRow(0);       // fill in column headings
        for (int c=0; c<4; c++)
        {
                m_grid.SetCol(c+1);
                m_grid.SetText(arCols[c]);
        }
        m_grid.SetCol(0);       // fill in row headings
        for (int r=0; r<4; r++)
        {
                m_grid.SetRow(r+1);
                m_grid.SetText(arRows[r]);
        }

        
        return TRUE;  // return TRUE  unless you set the focus to a control
}

void CGridCtrlDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
        if ((nID & 0xFFF0) == IDM_ABOUTBOX)
        {
                CAboutDlg dlgAbout;
                dlgAbout.DoModal();
        }
        else
        {
                CDialog::OnSysCommand(nID, lParam);
        }
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CGridCtrlDlg::OnPaint() 
{
        if (IsIconic())
        {
                CPaintDC dc(this); // device context for painting

                SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

                // Center icon in client rectangle
                int cxIcon = GetSystemMetrics(SM_CXICON);
                int cyIcon = GetSystemMetrics(SM_CYICON);
                CRect rect;
                GetClientRect(&rect);
                int x = (rect.Width() - cxIcon + 1) / 2;
                int y = (rect.Height() - cyIcon + 1) / 2;

                // Draw the icon
                dc.DrawIcon(x, y, m_hIcon);
        }
        else
        {
                CDialog::OnPaint();
        }
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CGridCtrlDlg::OnQueryDragIcon()
{
        return (HCURSOR) m_hIcon;
}

BEGIN_EVENTSINK_MAP(CGridCtrlDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CGridCtrlDlg)
        ON_EVENT(CGridCtrlDlg, IDC_MSFLEXGRID1, -600 /* Click */, OnClickMsflexgrid1, VTS_NONE)
        //}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CGridCtrlDlg::OnClickMsflexgrid1() 
{
        // TODO: Add your control notification handler code here
        CString szText = m_grid.GetText();
        if (m_bEditing == FALSE)
        {
                // Save the current grid position and set the edit flag
                m_nRow = m_grid.GetRow();
                m_nCol = m_grid.GetCol();
                m_bEditing = TRUE;
                // Get the current grid text & display it in the edit control
                szText = m_grid.GetText();
                m_edit.SetWindowText(szText);
                m_edit.ShowWindow(SW_SHOW);
                m_edit.SetFocus();
                m_edit.SetSel(0,-1);  // (0,-1) means select all text in edit control
        }
        else
        {
                // Roll up edit control & update previous grid position.
                // Must save current position, go back to old position,
                // & return to current position
                int nCurrentRow = m_grid.GetRow();
                int nCurrentCol = m_grid.GetCol();
                m_grid.SetRow(m_nRow);
                m_grid.SetCol(m_nCol);
                m_grid.SetFocus();

                CString szEntry;
                m_edit.GetWindowText(szText);
                szEntry.Format("%01.2f", atof(szText));

                m_edit.ShowWindow(SW_HIDE);
                m_grid.SetText(szEntry);
                m_bEditing = FALSE;
                m_grid.SetRow(nCurrentRow);
                m_grid.SetCol(nCurrentCol);
        }

        
}

void CGridCtrlDlg::OnCalc() 
{
        // TODO: Add your control notification handler code here
        //Close current editing job, if any
        if (m_bEditing != FALSE)
        {
                CString szEntry,szText;
                m_edit.GetWindowText(szText);
                szEntry.Format("%01.2f", atof(szText));
                m_edit.ShowWindow(SW_HIDE);
                m_grid.SetText(szEntry);
                m_bEditing = FALSE;
        }
        for (int c=1; c<5; c++)
        {
                double dTotal = 0.0;
                m_grid.SetCol(c);
                for (int r=1; r<4; r++)
                {
                        m_grid.SetRow(r);
                        CString szCell = m_grid.GetText();
                        dTotal += atof(szCell);
                }
                CString szTotal;
                szTotal.Format("%01.2f", dTotal);
                m_grid.SetRow(4);
                m_grid.SetText(szTotal);
        }

        
}