// MyMdi.cpp: implementation of the CMyMdi class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "LYFZSendMsg.h"
#include "MyMdi.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
BOOL CMyMdi::m_bExitCode = true;
void *CMyMdi::m_pMyMdi = NULL;
CMyMdi::CMyMdi()
{

}

CMyMdi::~CMyMdi()
{

}

////////////////////////////////////////////////////
//pName         �������� 
//pNewViewClass ����RUNTIME_CLASS����
//nIcon         ����ͼ��ID
//pParentWnd    ������ָ��
//���أ�
//    0 ��ʾ�����Ѿ����� 
//    1 �򿪴��ڳɹ�
//   -1 �����ڴ�ʧ��
//   -2 ��������ʧ��
/////////////////////////////////////////////////////
int CMyMdi::Append(char *pName, 
				   CRuntimeClass* pNewViewClass,
				   UINT nIcon,
				   CWnd* pParentWnd,
				   UINT nID)
{ 
  CChildFrame *pView = (CChildFrame*)GetView(pName);
  if (pView)  //�Ѵ���
  {
	pView->MDIActivate();
	return 1;
  }
  MainClose();


  pView=new CChildFrame;
  if (pView == NULL) return -1;
  tabMyMdi *pNewMdi =new tabMyMdi;
  if (pNewMdi == NULL) 
  {
	  delete pView;
	  return -1;
  }
  CCreateContext context;
  context.m_pNewViewClass=pNewViewClass;
//����
  if(!pView->LoadFrame(nIcon,WS_CHILDWINDOW,
	                   pParentWnd,&context))
		return -2;
  pNewMdi->pSubView = NULL;
  pNewMdi->pView = (CWnd *)pView;
  memcpy(pNewMdi->szName, pName, 64);
  pNewMdi->pNext = (tabMyMdi*)m_pMyMdi;
  m_pMyMdi = (void*)pNewMdi;
  pView->InitialUpdateFrame(NULL,true);  //��������
//  pNewMdi->pSubView = pView->GetDlgItem(nID);
  return 0;
}

/////////////////////////////////////////////
//pName ��������
//���ش��ڵ�����
int CMyMdi::GetIndex(char *pName)
/////////////////////////////////////////////
{
  if(m_pMyMdi == NULL) return -1;
  tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi;
  int i = 0;
  while(pCur)
  {
    if(strcmp(pName, pCur->szName) == 0)
      return i;
	pCur = pCur->pNext;
	i++;
  }
  return -1;
}



///////////////////////////////////////////////////
//pView ����ָ��
//���ش�������
char *CMyMdi::GetName(CWnd *pView)
///////////////////////////////////////////////////
{
/*  for (int i = 0; i < m_caView.GetSize(); i++)
  {
     if (m_caView.GetAt(i) == pView)  //���
		 return m_caName.GetAt(i);
  }
  return _T("");
*/
  if(m_pMyMdi == NULL) return NULL;
  tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi;
  while(pCur)
  {
    if(pView == pCur->pView)
      return pCur->szName;
	pCur = pCur->pNext;
  }
  return NULL;
}
///////////////////////////////////////////////////
//pView ����ָ��
//���ش�������
CWnd *CMyMdi::GetView(char *pName)
///////////////////////////////////////////////////
{
  if(m_pMyMdi == NULL) return NULL;
  tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi;
  while(pCur)
  {
    if(strcmp(pName, pCur->szName)==0)
      return pCur->pView;
	pCur = pCur->pNext;
  }
  return NULL;
}
///////////////////////////////////////////////////
//pView ����ָ��
//�����Ӵ���ָ��
CWnd *CMyMdi::GetSubView(CWnd *pView)
///////////////////////////////////////////////////
{
  if(m_pMyMdi == NULL) return NULL;
  tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi;
  while(pCur)
  {
    if(pView == pCur->pView)
      return pCur->pSubView;
	pCur = pCur->pNext;
  }
  return NULL;
}
///////////////////////////////////////////////////
//�����Ӵ���ָ��
//pView ����ָ��
//�Ӵ���ָ��
BOOL CMyMdi::SetSubView(CWnd *pView, CWnd *pDlg)
///////////////////////////////////////////////////
{
  if(m_pMyMdi == NULL) return false;
  tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi;
  while(pCur)
  {
    if(pView == pCur->pView)
	{
      pCur->pSubView = pDlg;
	  return true;
	}
	pCur = pCur->pNext;
  }
  return false;
}

////////////////////////////////////////////////////
//pView ����ָ��
//ɾ���Ӵ���
BOOL CMyMdi::Delete(CWnd *pView)
////////////////////////////////////////////////////
{
  if(m_pMyMdi == NULL) return false;
  tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi;
  tabMyMdi *pBefore = NULL;
  while(pCur)
  {
    if(pView == pCur->pView)
	{
	  if(pBefore == NULL) //�׸�
	  {
		m_pMyMdi = (void*)((tabMyMdi*)m_pMyMdi)->pNext;
	  }
	  else
	  {
		pBefore->pNext = pCur->pNext;
	  }
      delete pCur;
	  return true;
	}
	pBefore = pCur;
	pCur = pCur->pNext;
  }
  return FALSE;
}

//////////////////////////////////////////
//pName ��������
//ɾ�����ڵǼ�
BOOL CMyMdi::Delete(char *pName)
//////////////////////////////////////////
{
  if(m_pMyMdi == NULL) return false;
  tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi;
  tabMyMdi *pBefore = NULL;
  while(pCur)
  {
    if(strcmp(pName,pCur->szName)==0)
	{
	  if(pBefore == NULL) //�׸�
	  {
		m_pMyMdi = (void*)((tabMyMdi*)m_pMyMdi)->pNext;
	  }
	  else
	  {
		pBefore->pNext = pCur->pNext;
	  }
      delete pCur;
	}
	pBefore = pCur;
	pCur = pCur->pNext;
	return true;
  }
  return FALSE;
}
//////////////////////////////////////////
//pName ��������
//ɾ�����ڵǼ�
int CMyMdi::GetCount()
//////////////////////////////////////////
{
  if(m_pMyMdi == NULL) return -1;
  tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi;
  int i = 0;
  while(pCur)
  {
	pCur = pCur->pNext;
	i++;
  }
  return i;
//  return m_caName.GetSize();
}

BOOL CMyMdi::MainClose(BOOL bMsg)
{
  m_bExitCode = true;
  if(m_pMyMdi == NULL)return true;
  tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi;
  CWnd *pView;
  while(pCur)
  {
    pView = pCur->pView;
	pCur = pCur->pNext;	if(pView)
	  pView->SendMessage(WM_CLOSE, 0, 0);
	if (GetExitCode()==false) return false;
  }
  return true;
}
BOOL CMyMdi::ChildClose(CWnd *pView)
{
    m_bExitCode = true;
	CWnd *pSubView = GetSubView(pView);
	if (pSubView)
	   pSubView->SendMessage(WM_CLOSE, 0,0);
 	if(GetExitCode())
	{
	   Delete(pView);
	   return true;
	}
	return false;
}

void CMyMdi::DeleteAll()
{
  if(m_pMyMdi == NULL) return ;
  tabMyMdi *pCur = (tabMyMdi*)m_pMyMdi;
  tabMyMdi *pBefore;
  while(pCur)
  {
	pBefore = pCur;
	pCur = pBefore->pNext;
	delete pBefore;
  }

}

void CMyMdi::CloseAll()
{
  MainClose();
}