// FeaturesDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Features.h"
#include "FeaturesDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CFeaturesDlg dialog

CFeaturesDlg::CFeaturesDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CFeaturesDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CFeaturesDlg)
        // 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 CFeaturesDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CFeaturesDlg)
    DDX_Control(pDX, IDC_SFTBOX1, m_Box1);
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFeaturesDlg, CDialog)
    //{{AFX_MSG_MAP(CFeaturesDlg)
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFeaturesDlg message handlers

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

    // 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
    
    HRESULT hr;
    ISftBoxPtr vBox1 = m_Box1.GetControlUnknown();

    // load pictures
    PICTDESC PictDesc;
    PictDesc.cbSizeofstruct = sizeof(PICTDESC);
    PictDesc.picType = PICTYPE_BITMAP;
    PictDesc.bmp.hbitmap = LoadBitmap(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDB_PICTURE));
    PictDesc.bmp.hpal = NULL;
    hr = OleCreatePictureIndirect(&PictDesc, IID_IPictureDisp, TRUE, (void**)&m_pIPictureDispPicture);
    ASSERT(SUCCEEDED(hr));
    PictDesc.bmp.hbitmap = LoadBitmap(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDB_PIN));
    hr = OleCreatePictureIndirect(&PictDesc, IID_IPictureDisp, TRUE, (void**)&m_pIPictureDispPin);
    ASSERT(SUCCEEDED(hr));
    PictDesc.bmp.hbitmap = LoadBitmap(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDB_SPECIAL));
    hr = OleCreatePictureIndirect(&PictDesc, IID_IPictureDisp, TRUE, (void**)&m_pIPictureDispSpecial);
    ASSERT(SUCCEEDED(hr));

    vBox1->putref_Font(NULL); // use the form's default font
    vBox1->Edit->putref_Font(NULL);
    vBox1->RowHeaders->putref_Font(NULL);
    // make a bold font
    IFontPtr pFont = vBox1->GetFont();;
    IFontPtr pFontNew;
    hr = pFont->Clone(&pFontNew);
    ASSERT(SUCCEEDED(hr));
    hr = pFontNew->QueryInterface(IID_IFontDisp, (void**) &m_pBoldFont);
    ASSERT(SUCCEEDED(hr));
    pFontNew->put_Bold(TRUE);
    // .. and an italic font
    hr = pFont->Clone(&pFontNew);
    ASSERT(SUCCEEDED(hr));
    hr = pFontNew->QueryInterface(IID_IFontDisp, (void**) &m_pItalicFont);
    ASSERT(SUCCEEDED(hr));
    pFontNew->put_Italic(TRUE);


    vBox1->BulkUpdate = VARIANT_TRUE;
    for (long index = 0 ; index < 30 ; ++index) {
        if (index == 4) {
            CString s0, s2;
            s0.Format("Item %ld", index);
            s2.Format("Cell %ld,2", index);
            AddOneItem(s0, "This cell text is very long", s2);
        } else if (index == 7) {
            CString s0, s1;
            s0.Format("Item %ld", index);
            s1.Format("Cell %ld,1", index);
            AddOneItem(s0, s1, "This cell text is also very long");
        } else {
            CString s0, s1, s2;
            s0.Format("Item %ld", index);
            s1.Format("Cell %ld,1", index);
            s2.Format("Cell %ld,2", index);
            AddOneItem(s0, s1, s2);
        }
    }
    vBox1->BulkUpdate = VARIANT_FALSE;

    CRect rect;
    m_Box1.GetClientRect(&rect);
    vBox1->Column[0]->WidthPix = rect.Width() / 4;
    vBox1->Column[1]->WidthPix = rect.Width() / 4;
    vBox1->RowHeaders->MakeOptimal(0);
    vBox1->Column[2]->MakeOptimal(0);
    vBox1->Items->RecalcHorizontalExtent(0);
    
    return TRUE;  // return TRUE  unless you set the focus to a control
}

long CFeaturesDlg::AddOneItem(LPCTSTR lpszCell0, LPCTSTR lpszCell1, LPCTSTR lpszCell2)
{
    ISftBoxPtr vBox1 = m_Box1.GetControlUnknown();

    long index = vBox1->Items->Add(lpszCell0);
    vBox1->Cell[index][1]->Text = lpszCell1;

    if ((index % 4) == 0) {
        vBox1->Cell[index][1]->Image->putref_Picture(m_pIPictureDispSpecial);
        if ((index % 8) == 0)
            vBox1->Cell[index][1]->ImageHAlign = halignSftBoxRight;
    }
    vBox1->Cell[index][2]->Text = lpszCell2;
    if ((index % 5) == 0) {
        vBox1->Cell[index][2]->Image->putref_Picture(m_pIPictureDispPin);
        if ((index % 10) != 0)
            vBox1->Cell[index][2]->ImageHAlign = halignSftBoxRight;
    }
    if ((index % 3) == 0) {
        vBox1->Item[index]->RowHeader->Image->putref_Picture(m_pIPictureDispPicture);
        if ((index % 6) != 0)
            vBox1->Item[index]->RowHeader->ImageHAlign = halignSftBoxRight;
    }
    if ((index % 6) == 0)
        vBox1->Cell[index][0]->putref_Font(m_pBoldFont);
    if ((index % 7) == 0)
        vBox1->Cell[index][0]->putref_Font(m_pItalicFont);

    return index;
}

// 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 CFeaturesDlg::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 CFeaturesDlg::OnQueryDragIcon()
{
    return (HCURSOR) m_hIcon;
}