⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 indexeditorview.cpp

📁 VC开发环境下
💻 CPP
字号:
// IndexEditorView.cpp : implementation of the CIndexEditorView class
//

#include "stdafx.h"
#include "IndexEditor.h"

#include "IndexEditorDoc.h"
#include "IndexEditorView.h"
#include ".\indexeditorview.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CIndexEditorView

IMPLEMENT_DYNCREATE(CIndexEditorView, CFormView)

BEGIN_MESSAGE_MAP(CIndexEditorView, CFormView)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_NOTIFY(GVN_ENDLABELEDIT, ID_INDEX_GRID, OnGridEndEdit)
	ON_NOTIFY(NM_RCLICK, ID_INDEX_GRID, OnGridRClick)
	ON_COMMAND(ID_GRID_ADD_ROW, OnGridAddRow)
	ON_COMMAND(ID_GRID_DEL_ROW, OnGridDelRow)
	ON_COMMAND(ID_GRID_REFRESH, OnGridRefresh)
	ON_UPDATE_COMMAND_UI(ID_GRID_DEL_ROW, OnUpdateGridDelRow)
	ON_COMMAND(ID_GRID_DELETE_ALL, OnGridDeleteAll)
	ON_UPDATE_COMMAND_UI(ID_GRID_DELETE_ALL, OnUpdateGridDeleteAll)
//	ON_COMMAND(ID_GRID_INSERT, OnGridInsert)
//	ON_UPDATE_COMMAND_UI(ID_GRID_INSERT, OnUpdateGridInsert)
ON_COMMAND(ID_CANCEL_EDIT_SRVR, OnCancelEditSrvr)
END_MESSAGE_MAP()

// CIndexEditorView construction/destruction

CIndexEditorView::CIndexEditorView()
	: CFormView(CIndexEditorView::IDD)
{
	// TODO: add construction code here

}

CIndexEditorView::~CIndexEditorView()
{
}

void CIndexEditorView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
}

BOOL CIndexEditorView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CIndexEditorView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	ResizeParentToFit();
}


// CIndexEditorView diagnostics

#ifdef _DEBUG
void CIndexEditorView::AssertValid() const
{
	CFormView::AssertValid();
}

void CIndexEditorView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

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


// CIndexEditorView message handlers

int CIndexEditorView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFormView::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  Add your specialized creation code here
	if(!m_gridIndex.Create(CRect(0,0,0,0),this,ID_INDEX_GRID,WS_CHILD|WS_TABSTOP|WS_VISIBLE))
		return FALSE;

	InitGrid();

	return 0;
}

void CIndexEditorView::InitGrid()
{
	m_gridIndex.SetColumnCount(4);
	m_gridIndex.SetRowCount(1);
	m_gridIndex.SetFixedRowCount();
	m_gridIndex.SetFixedColumnCount();
	m_gridIndex.SetItemText(0,1,_T("分类名"));
	m_gridIndex.SetItemText(0,2,_T("量化值"));
	m_gridIndex.SetItemText(0,3,_T("描述"));
	m_gridIndex.AutoSize();
}


void CIndexEditorView::OnSize(UINT nType, int cx, int cy)
{
	CFormView::OnSize(nType, cx, cy);

	// TODO: Add your message handler code here
	if (m_gridIndex) 
		m_gridIndex.SetWindowPos(NULL,0,0,cx,cy,NULL);
}

void CIndexEditorView::OnUpdate(CView* /*pSender*/, LPARAM /*lHint*/, CObject* /*pHint*/)
{
}


//void CIndexEditorView::ShowIndex(int offset)
//{
//	IGHIndexPtr cpIndex;
//	GetDocument()->m_cpIndexSystem->get_Item(offset,&cpIndex);
//
//	ShowIndex(cpIndex);
//}

void CIndexEditorView::ShowIndex(IGHIndex *pGHIndex)
{	
	m_cpCurrentIndex=pGHIndex;

	if (pGHIndex==NULL)
	{
		m_gridIndex.SetRowCount(1);
		return;
	}

	ASSERT(pGHIndex);
	
	LONG lCount;
	m_cpCurrentIndex->get_Count(&lCount);
	m_gridIndex.SetRowCount(1+lCount);

	for (LONG i=0;i<lCount;i++)
	{
		IGHClassifyPtr cpClassify;
		m_cpCurrentIndex->get_Item(i,&cpClassify);
		assert(cpClassify!=NULL);

		CString str;
		str.Format(_T("%d"),i+1);
		m_gridIndex.SetItemText(i+1,0,str);

		//Name
		CComBSTR bstrName;
		cpClassify->get_Name(&bstrName);
		m_gridIndex.SetItemText(i+1,1,CString(bstrName));

		//Value
		FLOAT fValue;
		cpClassify->get_Value(&fValue);
		str.Format(_T("%f"),fValue);
		m_gridIndex.SetItemText(i+1,2,str);

		//Description
		CComBSTR bstrDes;
		cpClassify->get_Description(&bstrDes);
		m_gridIndex.SetItemText(i+1,3,CString(bstrDes));
	}

	m_gridIndex.AutoSize();
}

void CIndexEditorView::ShowIndex(const CString& IndexName)
{
//	assert(m_cpIndexSystem);

	LONG lCount;

	IGHIndexsPtr cpIndexs;
	cpIndexs=GetDocument()->m_cpIndexSystem;
	cpIndexs->get_Count(&lCount);

	CComBSTR	 bstrName;
	IGHIndexPtr  cpIndex;
	for (LONG i=0;i<lCount;i++)
	{
		bstrName.Empty();
		cpIndex=0;

		cpIndexs->get_Item(i,&cpIndex);
		assert(cpIndex!=NULL);

		cpIndex->get_Name(&bstrName);
		if (IndexName==bstrName)
		{
			ShowIndex(cpIndex);
			break;
		}
	}
}


void CIndexEditorView::OnGridEndEdit(NMHDR *pNotifyStruct,LRESULT* pResult)
{
	NM_GRIDVIEW* pItem = (NM_GRIDVIEW*) pNotifyStruct;

	ASSERT(m_cpCurrentIndex!=NULL);

	CString str=m_gridIndex.GetItemText(pItem->iRow,pItem->iColumn);
	IGHClassifyPtr cpClassify;
	m_cpCurrentIndex->get_Item(pItem->iRow-1,&cpClassify);
	
	ASSERT(cpClassify!=NULL);

	switch(pItem->iColumn)   
	{
	case 1:                     //Classify Name
		if (IsExist(str,pItem->iRow)||str.IsEmpty())
		{
			AfxMessageBox(str.IsEmpty()?IDS_GRID_ERROR_CANNOT_EMPTY:IDS_GRID_ERROR_CANNOT_REPEAT,MB_ICONWARNING|MB_OK);
			*pResult=-1;
			return;
		}
		cpClassify->put_Name(CComBSTR(str));  
		break;
	case 2:                                     //Classify Value
		{
			FLOAT fValue=(FLOAT)_tstof(str);
			CString strNew;
			strNew.Format(_T("%f"),fValue);
			if (strNew!=str)
			{
				m_gridIndex.SetItemText(pItem->iRow,pItem->iColumn,strNew);
			}
			cpClassify->put_Value(fValue);
			break;
		}
	case 3:                              //Classify Description
		cpClassify->put_Description(CComBSTR(str));
		break;
	default:
		ASSERT(FALSE);
		break;
	}
	
	m_gridIndex.AutoSize();

	GetDocument()->SetModifiedFlag();
	GetDocument()->UpdateTitle();

	*pResult = 0;
}

void CIndexEditorView::OnGridRClick(NMHDR *pNotifyStruct,LRESULT* pResult)
{
	NM_GRIDVIEW* pItem = (NM_GRIDVIEW*) pNotifyStruct;
	if (m_cpCurrentIndex)
	{

		CPoint point;
		GetCursorPos (&point);
		
		ShowContextMenu(point);
	
	}
	*pResult=0;
}

void CIndexEditorView::ShowContextMenu(CPoint point)
{
	CMenu contextMenu;
	contextMenu.LoadMenu(IDR_MENU_GRID);

	CBCGPPopupMenu *pPopupMenu=new CBCGPPopupMenu();
	if(pPopupMenu->Create(this,point.x,point.y+2,contextMenu.GetSubMenu(0)->m_hMenu,FALSE,TRUE))
		return;

	((CBCGPMDIFrameWnd*)AfxGetMainWnd())->OnShowPopupMenu(pPopupMenu);
	UpdateDialogControls(this,FALSE);
}


void CIndexEditorView::OnGridAddRow()
{

	ASSERT(m_cpCurrentIndex!=NULL);

	//Add a new Classify
	IGHClassifyPtr cpClassify;
	HRESULT hr;
	hr=cpClassify.CreateInstance(CLSID_GHClassify);	
	ASSERT(hr==S_OK);

	CString strClassifyName;
	GetSafeName(strClassifyName);
	cpClassify->put_Name(CComBSTR(strClassifyName));

	m_cpCurrentIndex->Add(cpClassify);
	
	//Add a new row in the grid
	int nItem=m_gridIndex.GetRowCount();
	m_gridIndex.SetRowCount(nItem+1);

	//Set Classify name
	m_gridIndex.SetItemText(nItem,1,strClassifyName); 
	//Set Classify Value
	CString strValue;
	strValue.Format(_T("%f"),0.0);
	m_gridIndex.SetItemText(nItem,2,strValue);

	//Set head column's text
	CString str;	
	str.Format(_T("%d"),nItem);
	m_gridIndex.SetItemText(nItem,0,str);

	m_gridIndex.AutoSize();

	GetDocument()->SetModifiedFlag();
	GetDocument()->UpdateTitle();
	
}

void CIndexEditorView::OnGridDelRow()
{
	ASSERT(m_gridIndex.GetRowCount()>1);
	ASSERT(m_gridIndex.GetSelectedCount()>0);

	CCellRange cellRange=m_gridIndex.GetSelectedCellRange();
	
	int iMinRow=cellRange.GetMinRow();
	for (int i=cellRange.GetMinRow();i<=cellRange.GetMaxRow();i++) 
	{
		//Delete current focus row in the grid
		m_gridIndex.DeleteRow(iMinRow);

		//Delete response classify
		IGHClassifyPtr cpClassify;
		m_cpCurrentIndex->get_Item(iMinRow-1,&cpClassify);
		ASSERT(cpClassify!=NULL);
		m_cpCurrentIndex->Delete(cpClassify);

	}
	
	UpdateGridRowHead(iMinRow);

	m_gridIndex.Invalidate();
	GetDocument()->SetModifiedFlag();
	GetDocument()->UpdateTitle();

}


void CIndexEditorView::OnUpdateGridDelRow(CCmdUI *pCmdUI)
{
	if (m_gridIndex.GetRowCount()<=1||m_gridIndex.GetSelectedCount()<=0)
		pCmdUI->Enable(FALSE);
	else
		pCmdUI->Enable(TRUE);	
}


void CIndexEditorView::OnGridRefresh()
{
	m_gridIndex.Invalidate();	
	m_gridIndex.AutoSize();
	m_gridIndex.Refresh();
}

BOOL CIndexEditorView::IsExist(const CString& strName,int iRow /* = 0 */)
{
	CString strExistName;
	for (int i=1;i<m_gridIndex.GetRowCount();i++)
	{
		if (i==iRow)
			continue;

		strExistName=m_gridIndex.GetItemText(i,1);
		if (strExistName==strName)
			return TRUE;
	}

	return FALSE;
}

void CIndexEditorView::GetSafeName(CString& strName)
{
	int i=0;
	CString strLoadName;
	CString strAppend;

	strLoadName.LoadString(IDS_GRID_NEW_NAME);
	strName=strLoadName;
	while (IsExist(strName))
	{
		strAppend.Format(_T("%d"),++i);
		strName=strLoadName+strAppend;
	}
}

void CIndexEditorView::OnGridDeleteAll()
{
	m_cpCurrentIndex->Clear();

	m_gridIndex.SetRowCount(1);
	m_gridIndex.AutoSize();
	m_gridIndex.Refresh();
}

void CIndexEditorView::OnUpdateGridDeleteAll(CCmdUI *pCmdUI)
{
	if (m_gridIndex.GetRowCount()<=1)
		pCmdUI->Enable(FALSE);
	else
		pCmdUI->Enable(TRUE);
}

//Command Insert
//void CIndexEditorView::OnGridInsert()
//{
//	ASSERT(m_gridIndex.GetRowCount()>1);
//	ASSERT(m_gridIndex.GetSelectedCount()>0);
//
//	CCellRange cellRange=m_gridIndex.GetSelectedCellRange();
//	int iMinRow=cellRange.GetMinRow();
//
//	ASSERT(iMinRow!=0);
//
//	//Add a new Classify
//	IGHClassifyPtr cpClassify;
//	HRESULT hr;
//	hr=cpClassify.CreateInstance(CLSID_GHClassify);	
//	ASSERT(hr==S_OK);
//
//	CString strClassifyName;
//	GetSafeName(strClassifyName);
//	cpClassify->put_Name(CComBSTR(strClassifyName));
//
//	m_cpCurrentIndex->Add(cpClassify);
//
//	//Insert a new row in the grid
//	m_gridIndex.InsertRow(_T(""),iMinRow);
//
//	//Set Classify name
//	m_gridIndex.SetItemText(nItem,1,strClassifyName);  
//
//	//Set head column's text
//	CString str;	
//	str.Format(_T("%d"),nItem);
//	m_gridIndex.SetItemText(nItem,0,str);
//
//	m_gridIndex.AutoSize();
//
//	GetDocument()->SetModifiedFlag();
//	GetDocument()->UpdateTitle();	
//}

//void CIndexEditorView::OnUpdateGridInsert(CCmdUI *pCmdUI)
//{
//	if (m_gridIndex.GetRowCount()<=1||m_gridIndex.GetSelectedCount()<=0)
//		pCmdUI->Enable(FALSE);
//	else
//		pCmdUI->Enable(TRUE);	
//}

void CIndexEditorView::UpdateGridRowHead(int iStart /* = 1 */)
{
	ASSERT(iStart>0);

	int iCount=m_gridIndex.GetRowCount();
	CString strTitle;
	for (int i=iStart;i<iCount;i++)
	{
		strTitle.Format(_T("%d"),i);
		m_gridIndex.SetItemText(i,0,strTitle);
	}
}

void CIndexEditorView::OnCancelEditSrvr()
{
	GetDocument()->OnDeactivateUI(FALSE);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -