/*****************************************************************************
* Copyright (c) 1990-98 Digi International Inc., All Rights Reserved
* 
* This software contains proprietary and confidential information of Digi
* International Inc.  By accepting transfer of this copy, Recipient agrees
* to retain this software in confidence, to prevent disclosure to others,
* and to make no use of this software other than that for which it was
* delivered.  This is an unpublished copyrighted work of Digi International
* Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly
* prohibited.
* 
* Restricted Rights Legend
*
* Use, duplication, or disclosure by the Government is subject to
* restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in
* Technical Data and Computer Software clause at DFARS 252.227-7031 or
* subparagraphs (c)(1) and (2) of the Commercial Computer Software -
* Restricted Rights at 48 CFR 52.227-19, as applicable.
*
* Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
*
*****************************************************************************/


// PageMSN.cpp : implementation file
//

#include "stdafx.h"
#include "PageMSN.h"

#include "dxbtypes.h"
#include "mgmthlc.h"

#include "Resource.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPageMSN property page

IMPLEMENT_DYNAMIC(CPageMSN, CConfigWizPage)

CPageMSN::CPageMSN(UINT const nIDTemplate) : CConfigWizPage(nIDTemplate)
{
	//{{AFX_DATA_INIT(CPageMSN)
	//}}AFX_DATA_INIT

   m_fAdding = FALSE;
}

CPageMSN::~CPageMSN()
{
}

void CPageMSN::DoDataExchange(CDataExchange* pDX)
{
	CConfigWizPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPageMSN)
	DDX_Control(pDX, IDC_MSNEditButton, m_ctrlEditButton);
	DDX_Control(pDX, IDC_MSNList, m_ctrlMSNList);
	DDX_Control(pDX, IDC_MSNRemoveButton, m_ctrlRemoveButton);
	DDX_Control(pDX, IDC_MSNAddButton, m_ctrlAddButton);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPageMSN, CConfigWizPage)
	//{{AFX_MSG_MAP(CPageMSN)
	ON_BN_CLICKED(IDC_MSNAddButton, OnMSNAddButton)
	ON_BN_CLICKED(IDC_MSNRemoveButton, OnMSNRemoveButton)
	ON_NOTIFY(LVN_ENDLABELEDIT, IDC_MSNList, OnEndLabelEditMSNList)
	ON_NOTIFY(LVN_BEGINLABELEDIT, IDC_MSNList, OnBeginLabelEditMSNList)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_MSNList, OnItemChangedMSNList)
	ON_BN_CLICKED(IDC_MSNEditButton, OnMSNEditButton)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


BOOL CPageMSN::UpdateData(BOOL bSaveAndValidate /* = TRUE */)
{
   BOOL fSuccess = FALSE;

   try
   {
      if (bSaveAndValidate)
      {
         TRACE("UpdateData(GET);\n");
         m_astrMSN.RemoveAll();
         for (int i = 0; i < m_ctrlMSNList.GetItemCount(); i++)
         {
            m_astrMSN.Add(m_ctrlMSNList.GetItemText(i, 0));
         }
      }
      else
      {
         TRACE("UpdateData(SET);\n");
         m_ctrlMSNList.DeleteAllItems();
         for (int i = 0; i < m_astrMSN.GetSize(); i++)
         {
            m_ctrlMSNList.InsertItem(i, m_astrMSN[i]);
         }
      }

      fSuccess = CConfigWizPage::UpdateData(bSaveAndValidate);
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

BOOL CPageMSN::GetMSN(bri_directory_t * const pDirs, int const cMaxDirs)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(pDirs != NULL);
      AffirmThrow(cMaxDirs > 0);

      if (m_hWnd != NULL) UpdateData();

      AffirmThrow(m_astrMSN.GetSize() <= cMaxDirs);

      memset((void *)pDirs, 0, (sizeof(*pDirs) * cMaxDirs));

      for (int i = 0; i < m_astrMSN.GetSize(); i++)
      {
         strcpy((char *)pDirs[i].number, m_astrMSN[i]);
      }

      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

BOOL CPageMSN::SetMSN(bri_directory_t const * const pDirs, int const cMaxDirs)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(pDirs != NULL);
      AffirmThrow(cMaxDirs > 0);

      m_astrMSN.RemoveAll();
      for (int i = 0; i < cMaxDirs; i++)
      {
         if (pDirs[i].number[0] != '\0') m_astrMSN.Add((char *)pDirs[i].number);
      }

      if (m_hWnd != NULL) UpdateData(FALSE);

      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

/////////////////////////////////////////////////////////////////////////////
// CPageMSN message handlers


int CPageMSN::GetCurSel(void)
{
   int iSel = LB_ERR;

   try
   {
      for (int i = 0; i < m_ctrlMSNList.GetItemCount(); i++)
      {
         LV_ITEM lvi = {0};

         lvi.mask = LVIF_STATE;
         lvi.iItem = i;
         lvi.iSubItem = 0;
         lvi.stateMask = LVIS_FOCUSED | LVIS_SELECTED;

         if (m_ctrlMSNList.GetItem(&lvi))
         {
            TRACE("Item #%d's state is 0x%08x\n", i, lvi.state);
            if (lvi.state & LVIS_SELECTED)
            {
               iSel = lvi.iItem;
               break;
            }
         }
      }
   }
   catch(CAffirmException ae)
   {
   }

   TRACE("CurSel == %d\n", iSel);
   return iSel;
}

void CPageMSN::OnMSNAddButton() 
{
   try
   {
      int cItems = m_ctrlMSNList.GetItemCount();
      AffirmThrow(cItems >= 0);
      AffirmThrow(cItems <= MSN_MAX);

      if (cItems < MSN_MAX)
      {
         m_ctrlMSNList.InsertItem(cItems, TEXT(""));
         m_ctrlMSNList.SetFocus();

         m_fAdding = TRUE;
         m_ctrlMSNList.EditLabel(cItems);
      }
   }
   catch (CAffirmException ae)
   {
   }
}

void CPageMSN::OnMSNEditButton() 
{
   int iItem = GetCurSel();
   if (iItem != LB_ERR)
   {
      m_ctrlMSNList.SetFocus();
      m_ctrlMSNList.EditLabel(iItem);
   }
}

void CPageMSN::OnMSNRemoveButton() 
{
   try
   {
      int iCurSel = GetCurSel();

      if (iCurSel != LB_ERR)
      {
         m_ctrlMSNList.DeleteItem(iCurSel);
         UpdateData();

         UpdateAddButton();
         UpdateEditButton();   
         UpdateRemoveButton();
      }
   }
   catch (CAffirmException ae)
   {
   }
}

void CPageMSN::UpdateAddButton(void)
{
   BOOL fEnable = (m_ctrlMSNList.GetItemCount() < MSN_MAX);
   m_ctrlAddButton.EnableWindow(fEnable);
}

void CPageMSN::UpdateEditButton(void)
{
   BOOL fEnable = (GetCurSel() != LB_ERR);
   m_ctrlEditButton.EnableWindow(fEnable);
}

void CPageMSN::UpdateRemoveButton(void)
{
   BOOL fEnable = (GetCurSel() != LB_ERR);
   m_ctrlRemoveButton.EnableWindow(fEnable);
}

BOOL CPageMSN::OnInitDialog() 
{
	CConfigWizPage::OnInitDialog();

   CRect rc;
   m_ctrlMSNList.GetClientRect(&rc);
   m_ctrlMSNList.SetColumnWidth(0, rc.right);

   UpdateData(FALSE);	

   UpdateAddButton();
   UpdateEditButton();   
   UpdateRemoveButton();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CPageMSN::OnBeginLabelEditMSNList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;

   CEdit * pMSNListEdit = m_ctrlMSNList.GetEditControl();
   Affirm(pMSNListEdit != NULL);
   if (pMSNListEdit != NULL)
   {
      pMSNListEdit->LimitText(20);
      pMSNListEdit->ModifyStyle(0, ES_NUMBER, 0);
   }
	
	*pResult = 0;
}

void CPageMSN::OnEndLabelEditMSNList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;

   if (pDispInfo->item.pszText == NULL)
   {
      if (m_fAdding)
         m_ctrlMSNList.DeleteItem(pDispInfo->item.iItem);   // user was adding but didn't enter a number
   }
   else
   {
      if (pDispInfo->item.pszText[0] == TEXT('\0'))         // user deleted all text
         m_ctrlMSNList.DeleteItem(pDispInfo->item.iItem);
      else
         m_ctrlMSNList.SetItemText(pDispInfo->item.iItem, pDispInfo->item.iSubItem, pDispInfo->item.pszText);

      UpdateData();
   }

   m_fAdding = FALSE;

   UpdateAddButton();
   UpdateEditButton();   
   UpdateRemoveButton();

	*pResult = 0;
}

void CPageMSN::OnItemChangedMSNList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

   if ((pNMListView != NULL) && (pNMListView->uNewState & LVIS_SELECTED))
   {
      UpdateEditButton();   
      UpdateRemoveButton();
   }
	
	*pResult = 0;
}
