//  Beta release only.
//
//  Copyright(c) Ivyware Pty Ltd 2007, 2013  (all rights reserved)
//               MELBOURNE, VICTORIA, AUSTRALIA, 3130
//
//  Provided no fee is charged (either by the distributer or user)
//  permission to use, copy, modify, and distribute this software and
//  its documentation for any purpose and without fee is hereby granted,
//  provided that the above copyright notice appear in all copies and
//  that both that copyright notice and this permission notice appear
//  in supporting documentation.
//
//  No waranty or suitablity for purpose is implied.
//
//
//  Implementation for CListCtrl extensions
//

#include "StdAfx.h"

#include "CListCtrl_ext.h"

//
//  Formats item in CListCtrl
//
//
//  Parameters:  CListCtrl& oCListCtrl
//               List control for which cell is to be formatted
//
//               bool bFull
//               Full window copy flag
//
//  Returns:     HTREEITEM
//               Moved item as a copy of hItem
int
CListCtrl_FormatItem ( CListCtrl& oCListCtrl, int nRow, int nColumn
                     , LPCTSTR lpszFormat, ... )
{
    // Introduce the locals
    va_list    ap;
    va_start ( ap, lpszFormat );

    TCHAR szText[128];
    int nSize = vsprintf_s ( szText, sizeof(szText), lpszFormat, ap );
    szText[nSize] = 0;

    oCListCtrl.SetItemText ( nRow, nColumn, szText );
    return 1;
}

void
CListCtrl_ClearTextAllSubItems ( CListCtrl& oCListCtrl, int nItem )
{
    int nCols = oCListCtrl.GetHeaderCtrl()->GetItemCount();
    for ( int i = 0; i < nCols; i++ )
      oCListCtrl.SetItemText(nItem, i, _T("") );
}

void
CListCtrl_DeleteAllColumns ( CListCtrl& oCListCtrl )
{
    oCListCtrl.DeleteAllItems();
    int nCols = oCListCtrl.GetHeaderCtrl()->GetItemCount();
    for ( int i = 0; i < nCols; i++ )
      oCListCtrl.DeleteColumn ( 0 );
}


//
//  Fetches column header label
//
//  Parameters:  CHeaderCtrl *pHeader
//               Header control for which column text is to be retreived
//
//               int nColumn
//               Column, zero based
//
//  Returns:     LPCTSTR
//               Column label
P2PmsgMFC_EXT BOOL
CHeaderCtrl_GetItemText ( CHeaderCtrl *pHeader, int nColumn, CString& strItemText )
{
    TCHAR  lpBuffer[256];

    HDITEM hdi;
    hdi.mask       = HDI_TEXT;
    hdi.pszText    = lpBuffer;
    hdi.cchTextMax = sizeof(lpBuffer)/sizeof(lpBuffer[0]);

    if ( !pHeader -> GetItem(nColumn, &hdi) )
      return FALSE;
    strItemText =  hdi.pszText;
    return TRUE;
}

//
//  Generates list control column for each passed P2PmsgNode item
//
//  Parameters:  ClistCtrl& oCListCtrl
//               List control in which columns are to be generated
//
//               const P3PmsgNode& oNodeColumns
//               P3PmsgNode containing list of columns to be generated
BOOL
CListCtrl_MakeColumns ( CListCtrl& oCListCtrl, const P3PmsgNode& oNodeColumns )
{
    P3PmsgCurs oCurs((P3PmsgNode&)oNodeColumns);
    int        nCol = 0;
    for ( int i = 0; oCurs.Goto(i); i++ )
    {
        if ( !oCurs.IsField() )
          continue;
        oCListCtrl.InsertColumn ( nCol, oCurs.r_field().c_name(), 0, LVSCW_AUTOSIZE_USEHEADER );
        int nWidth = 50;
        switch (oCurs.r_field().r_data().DataType())
        {
          case VBLockData_INT08:  nWidth =  50; break;
          case VBLockData_UINT08: nWidth =  50; break;
          case VBLockData_INT16:  nWidth =  50; break;
          case VBLockData_UINT16: nWidth =  50; break;
          case VBLockData_INT32:  nWidth =  75; break;
          case VBLockData_UINT32: nWidth =  75; break;
          case VBLockData_INT64:  nWidth =  75; break;
          case VBLockData_UINT64: nWidth =  75; break;
          case VBLockData_FLOAT:  nWidth =  75; break;
          case VBLockData_DOUBLE: nWidth =  75; break;
          case VBLockData_BOOL:   nWidth =  75; break;
          case VBLockData_TIME32: nWidth =  75; break;
          case VBLockData_TIME64: nWidth = 125; break;
        }
        //oCListCtrl.SetColumnWidth ( nCol, LVSCW_AUTOSIZE_USEHEADER );
        if ( oCListCtrl.GetColumnWidth(nCol) < nWidth )
          oCListCtrl.SetColumnWidth ( nCol, nWidth );
        nCol++;
    }
    return oCListCtrl.GetHeaderCtrl()->GetItemCount();
}

BOOL
CListCtrl_InsertRow ( CListCtrl& oCListCtrl, const P3PmsgNode& oNodeColumns )
{
    P3PmsgNode    oNode = (P3PmsgNode&)oNodeColumns;
    CHeaderCtrl  *pHeaderCtrl = oCListCtrl.GetHeaderCtrl();
    CString     strColumnText;
    int           nColumns    = pHeaderCtrl->GetItemCount();
    int           nItems      = 0;
    int           nItem       = oCListCtrl.InsertItem ( oCListCtrl.GetItemCount(), _T("") );
    for ( int nSubItem = 0; nSubItem < nColumns; nSubItem++ )
    {
      CHeaderCtrl_GetItemText ( pHeaderCtrl, nSubItem, strColumnText );
      P3PmsgField oField = oNode.SelectObject(strColumnText);
      if ( !oNode )
        continue;
      oCListCtrl.SetItemText ( nItem, nSubItem, oField.r_data().ToString() );
      nItems++;
    }
    return nItems;
}
