// ErrView.cpp : implementation file
//

#include "stdafx.h"
#include "skoko.h"
#include "MainFrm.h"
#include "ErrView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CErrView

IMPLEMENT_DYNCREATE(CErrView, CListView)

CErrView::CErrView() : m_firstUpdate(TRUE)
{
}

CErrView::~CErrView()
{
}


BEGIN_MESSAGE_MAP(CErrView, CListView)
  //{{AFX_MSG_MAP(CErrView)
  ON_NOTIFY_REFLECT(LVN_GETDISPINFO, OnGetdispinfo)
  //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CErrView drawing

void CErrView::OnDraw(CDC* pDC)
{
  CDocument* pDoc = GetDocument();
  // TODO: add draw code here
}

/////////////////////////////////////////////////////////////////////////////
// CErrView diagnostics

#ifdef _DEBUG
void CErrView::AssertValid() const
{
  CListView::AssertValid();
}

void CErrView::Dump(CDumpContext& dc) const
{
  CListView::Dump(dc);
}

CSKOKODoc* CErrView::GetDocument() // non-debug version is inline
{
  ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSKOKODoc)));
  return (CSKOKODoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CErrView message handlers

void CErrView::OnInitialUpdate() 
{
  CListView::OnInitialUpdate();
  
  if (m_firstUpdate)
  {
    Init();
    m_firstUpdate = FALSE;
  }
}

void CErrView::Init( void )
{
  CListCtrl& ctrl = GetListCtrl();
  const DWORD dwAddLvStyle = LVS_ALIGNLEFT | LVS_REPORT | LVS_SHOWSELALWAYS;
  const DWORD dwRemoveLvStyle = LVS_SORTASCENDING | LVS_SORTDESCENDING;
  ctrl.ModifyStyle(dwRemoveLvStyle, dwAddLvStyle, 0);
  ctrl.InsertColumn(0, _T("Объект"), LVCFMT_LEFT, 300, 0);
  ctrl.InsertColumn(1, _T("Ошибка"), LVCFMT_LEFT, 120, 0);
  ctrl.SetImageList(&theApp.m_imageList, LVSIL_SMALL);
}

void CErrView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
  switch (lHint)
  {
    case 10: // Очистка списка
      Reset();
      break;
    case 11: // Обновление списка
      RefreshListCtrl();
      break;
    default:
      Invalidate();
  }

  UpdateCountIndicator();
}

void CErrView::UpdateCountIndicator( void )
{
  theApp.FrameWnd()->SetErrIndicator(GetListCtrl().GetItemCount());
}

void CErrView::RefreshListCtrl( void )
{
  CWaitCursor wc;
  
  CListCtrl& ctrl = GetListCtrl();
  const CSKOKODoc& doc = *GetDocument();

  ctrl.DeleteAllItems();
  ctrl.SetItemCount(doc.GetErrorCount());
  for (int i = 0; i < doc.GetErrorCount(); ++i)
  {
    int item = ctrl.InsertItem(i, LPSTR_TEXTCALLBACK, CSKOKOApp::ICON_ERROR);
    ctrl.SetItemText(item, 1, LPSTR_TEXTCALLBACK);
    
    // LPARAM каждого элемента равен его номеру в журнале!
    ctrl.SetItemData(item, i);
  }
}

void CErrView::Reset( void )
{
  GetListCtrl().DeleteAllItems();
}

void CErrView::OnGetdispinfo(NMHDR* pNMHDR, LRESULT* pResult) 
{
  LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
  
  if (pDispInfo->item.mask & LVIF_TEXT)
  {
    const CSKOKODoc* pDoc = GetDocument();
    static CFileInfo fi;
    pDoc->GetErrorInfo(pDispInfo->item.lParam, &fi);
    LPCTSTR pText;
    if (pDispInfo->item.iSubItem == 0)
      pText = fi.filePath;
    else
      pText = fi.ownerName;
    pDispInfo->item.pszText = (LPTSTR)pText;
  }
  
  *pResult = 0;
}

void CErrView::Refresh()
{
  Reset();
  RefreshListCtrl();
  UpdateCountIndicator();
}
