#include "..\stdafx.h" #include "BaseTreeGrid.h" #include "TListQuene.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif BEGIN_MESSAGE_MAP(CBaseTreeGrid, WDGrid) // ON_COMMAND_RANGE(1, 10000, OnMenuCommand) END_MESSAGE_MAP() CBaseTreeGrid::CBaseTreeGrid(bool bTree) : WDGrid(bTree) { } CBaseTreeGrid::~CBaseTreeGrid() { } int CBaseTreeGrid::GetAutoId(int curId) { int maxId = 0; WDGridTreeRow *pRow; TListQuene quene; for (MoveToFirst(); !IsOut(); MoveToNext()) { if (CurTreeRow()->IsRootRow()) { quene.EnQuene(CurTreeRow()); } } // 同时考虑当前剪切板内容,使剪切、粘贴操作时编号不变化 TPDList& list = GetRowPaste(); for (list.MoveToFirst(); !list.IsOut(); list.MoveToNext()) { quene.EnQuene((WDGridTreeRow*)list.GetCurData()); } int count = 0; while (!quene.IsEmpty()) { quene.DeQuene(pRow); // if (pRow->IsParentRow()) if (NULL != pRow && pRow->IsTreeRow() && pRow->IsParentRow()) { for(pRow->MoveToFirst();!pRow->IsOut();pRow->MoveToNext()) { quene.EnQuene(pRow->CurChild()); } } else { int id = (*pRow)[1]; if (id > maxId) maxId = id; if (id == curId) count++; } } if (count < 2 && curId != 0) return curId; else return maxId+1; } bool CBaseTreeGrid::IsRightLabel(LPCTSTR pszNewLab) { CString strNewLab = pszNewLab; if ( 0 == strNewLab.CompareNoCase(_T("0")) || 0 == strNewLab.CollateNoCase(_T(""))) return false; TListQuene quene; for (MoveToFirst(); !IsOut(); MoveToNext()) { if (CurTreeRow()->IsRootRow()) quene.EnQuene(CurTreeRow()); } // 同时考虑当前剪切板内容,使剪切、粘贴操作时编号不变化 TPDList& list = GetRowPaste(); for (list.MoveToFirst(); !list.IsOut(); list.MoveToNext()) { quene.EnQuene((WDGridTreeRow*)list.GetCurData()); } WDGridTreeRow *pRow; int count = 0; while (!quene.IsEmpty()) { quene.DeQuene(pRow); // if (pRow->IsParentRow()) if (NULL != pRow && pRow->IsTreeRow() && pRow->IsParentRow()) { for (pRow->MoveToFirst(); !pRow->IsOut(); pRow->MoveToNext()) { quene.EnQuene(pRow->CurChild()); } } else if (strNewLab == (*pRow)[1]) return false; } return true; } bool CBaseTreeGrid::OnRefresh(WDGridRow& row) { // if (row.IsParentRow()) if (row.IsTreeRow() && row.IsParentRow()) { row[0].Refresh(); for (long i = 1; i < GetColNum(); i++) { CString temp = (LPCTSTR)row[i]; row[i].SetText(_T("")); row[i].Refresh(); row[i].SetText(temp); } return false; } return true; } bool CBaseTreeGrid::OnBeforeInsertChild(WDGridTreeRow&row,long iChild,WDGridTreeRow*&pNewRow) { if (GetRowSelNum() == 1 && pNewRow == NULL)//非插入目录 { pNewRow = new WDGridTreeRow(*this); // if (!GetSelRow()->IsParentRow()) WDGridRow* pRow = GetSelRow(); if (NULL != pRow && pRow->IsTreeRow() && !pRow->IsParentRow()) *pNewRow = (WDGridTreeRow&)*GetSelRow(); } return true; } bool CBaseTreeGrid::OnBeforeInsertRow(long i,WDGridRow* &pNewRow) { if (GetRowSelNum() == 1 && pNewRow == NULL)//非插入目录 { pNewRow = new WDGridTreeRow(*this); // if (!GetSelRow()->IsParentRow()) WDGridRow* pRow = GetSelRow(); if (NULL != pRow && pRow->IsTreeRow() && !pRow->IsParentRow()) *pNewRow = (WDGridTreeRow&)*GetSelRow(); } return true; } void CBaseTreeGrid::OnInsertRow(WDGridRow &row) { // CString sText; // WDGridTreeRow &treeRow = (WDGridTreeRow&)row; // if (row.IsParentRow()) // 插入目录 // { // OnInsertRow(treeRow.GetChild(0)); // } // else // { // row[1].SetText(_T("1")); // sText.Format(_T("%d"), GetAutoId(1)); // row[1].SetText(sText); // row[1].Refresh(); // } } /* // 右键点击一行弹出的提示语 void CBaseTreeGrid::OnRClickRow(WDGridRow &row, CPoint pt) { if (IsReadOnly()) return; UnSelect(); SelectRow(row.GetIndex(), true); CMenu popup; popup.CreatePopupMenu(); popup.AppendMenu(MF_STRING, ID_WDGRID_INS_ROW, _T("新建行(N)")); popup.AppendMenu(MF_STRING, ID_APPEND_CHILD, _T("新建子行(T)")); popup.AppendMenu(MF_STRING, ID_WDGRID_INS_ROW_CAT, _T("新建类别(Ctrl+N)")); if (row.IsParentRow()) { if (!row.IsDelUnable()) popup.AppendMenu(MF_STRING, ID_WDGRID_DEL_ROW, _T("删除类别(Del)")); } else { if (!row.IsDelUnable()) popup.AppendMenu(MF_STRING, ID_WDGRID_DEL_ROW, _T("删除行(Del)")); } popup.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, pt.x, pt.y, this); } // 右键点击左上角的小方格弹出的提示语 void CBaseTreeGrid::OnRClickQuad(CPoint pt) { if (IsReadOnly()) return; CMenu popup; popup.CreatePopupMenu(); if (!g_bFixMatLib) { newLib.Update(false); HMENU hMenu=newLib.CreateMenu(newLib.bMatLib,false); popup.AppendMenu(MF_POPUP|MF_STRING,(UINT_PTR)hMenu,_T("新建数据库")); openLib.Update(true); hMenu=openLib.CreateMenu(newLib.bMatLib,true); popup.AppendMenu(MF_POPUP|MF_STRING,(UINT_PTR)hMenu,_T("选择数据库")); } if (IsModified()) { popup.AppendMenu(MF_STRING,ID_SAVE,_T("保存数据库")); popup.AppendMenu(MF_STRING,ID_RELOAD,_T("放弃修改")); } if (!IsRowSelected()) { UnSelect(); popup.AppendMenu(MF_STRING,ID_WDGRID_ADD_ROW, _T("新建行(N)")); popup.AppendMenu(MF_STRING,ID_APPEND_CAT,_T("新建类别(Ctrl+N)")); if (!IsEmpty()) popup.AppendMenu(MF_STRING,ID_WDGRID_SELECT_ALL,_T("全选(A)")); } else popup.AppendMenu(MF_STRING,ID_WDGRID_DEL_ROW,_T("删除(Del)")); popup.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, pt.x, pt.y,this); } */ void CBaseTreeGrid::OnMenuCommand(UINT id) { switch(id) { // 以下部分为,一行中的 case ID_WDGRID_INS_ROW: DoInsertRowAfter(); // 新建行 break; // case ID_APPEND_CHILD: // DoAppendChild(); // 新建子行 // break; // case ID_WDGRID_INS_ROW_CAT: // DoInsertTreeAfter(); // 新建类别 // break; case ID_WDGRID_DEL_ROW: DoDelete(); // 删除类别、删除行 break; // 以下部分为,左上角的小方格中的 case ID_WDGRID_ADD_ROW: DoAppendRow(); // 新建行 break; // case ID_APPEND_CAT: // 新建类别 // DoAppendTree(); // break; case ID_WDGRID_SELECT_ALL: SelectAll(); // 全选 break; // break; // case ID_SAVE: // OnSave(); // 保存数据库 // break; // case ID_RELOAD: // OnReload(); // 放弃修改 // break; default: // if (id < 3000) // Open(openLib.GetLibFile(id)); // 选择数据库 // else // New(newLib.GetLibFile(id)); // 新建数据库 break; } } /* void CBaseTreeGrid::OnBeforeRefresh(WDGridCell&cell) { WDGrid::OnBeforeRefresh(cell); if(cell.GetColIndex()==0&&!cell.GetRow().IsParentRow()) { CDC&dc=GetDC(); dc.SetTextColor(COLOR_NAME); } } */ bool CBaseTreeGrid::OnBeforeClip() { TList rows; GetSel(rows); for (rows.MoveToFirst(); !rows.IsOut(); rows.MoveToNext()) { WDGridTreeRow* pRow = (WDGridTreeRow*)(rows.GetCurData()); if (pRow->IsChildRow() && rows.Find(pRow->Parent()) == NOT_FOUND) m_lstpParents.FindOrAddTail(pRow->Parent()); } return true; } void CBaseTreeGrid::OnEndClip() { CString sText; for (m_lstpParents.MoveToFirst(); !m_lstpParents.IsOut(); m_lstpParents.MoveToNext()) { WDGridRow& row = *m_lstpParents.GetCurData(); // if (!row.IsParentRow()) if (row.IsTreeRow() && !row.IsParentRow()) { long newId = GetAutoId(row[1]); sText.Format(_T("%d"),newId); row[1].SetText(sText); } } m_lstpParents.Clear(); } bool CBaseTreeGrid::OnBeforeDeleteRow() { TList rows; GetSel(rows); for (rows.MoveToFirst(); !rows.IsOut(); rows.MoveToNext()) { WDGridTreeRow* pRow = (WDGridTreeRow*)(rows.GetCurData()); if (NULL != pRow) { if (pRow->IsChildRow()&&rows.Find(pRow->Parent()) == NOT_FOUND) m_lstpParents.FindOrAddTail(pRow->Parent()); } } return true; } void CBaseTreeGrid::OnEndDeleteRow() { CString sText; for (m_lstpParents.MoveToFirst(); !m_lstpParents.IsOut(); m_lstpParents.MoveToNext()) { WDGridRow& row = *m_lstpParents.GetCurData(); // if (!row.IsParentRow()) if (row.IsTreeRow() && !row.IsParentRow()) { long newId = GetAutoId(row[1]); sText.Format(_T("%d"), newId); row[1].SetText(sText); } } m_lstpParents.Clear(); // WDGrid::OnEndDeleteRow(); } bool CBaseTreeGrid::OnMoveRow(long i) { if (!OnMovingRow(i)) return false; TList rows; GetSel(rows); for (rows.MoveToFirst(); !rows.IsOut(); rows.MoveToNext()) { WDGridTreeRow* pRow = (WDGridTreeRow*)(rows.GetCurData()); if (pRow->IsChildRow() && rows.Find(pRow->Parent()) == NOT_FOUND) m_lstpParents.FindOrAddTail(pRow->Parent()); } return true; } void CBaseTreeGrid::OnEndMoveRow() { // CString sText; // for (m_lstpParents.MoveToFirst(); !m_lstpParents.IsOut(); m_lstpParents.MoveToNext()) // { // WDGridRow& row = *m_lstpParents.GetCurData(); // if (!row.IsParentRow()) // { // long newId = GetAutoId(row[1]); // sText.Format(_T("%d"),newId); // row[1].SetText(sText); // } // } // mcl changed, 09.3.14, IsParentRow()之前要先确定是树行,否则会崩溃 CString sText; for (m_lstpParents.MoveToFirst(); !m_lstpParents.IsOut(); m_lstpParents.MoveToNext()) { WDGridRow* pRow = m_lstpParents.GetCurData(); if (NULL != pRow && pRow->IsTreeRow() && !pRow->IsParentRow()) { long newId = GetAutoId((*pRow)[1]); sText.Format(_T("%d"),newId); (*pRow)[1].SetText(sText); } } m_lstpParents.Clear(); } void CBaseTreeGrid::OnEndPaste() { TList rows; GetSel(rows); TListQuene quene; for (rows.MoveToFirst(); !rows.IsOut(); rows.MoveToNext()) quene.EnQuene(rows.GetCurData()); CString sText; WDGridRow* pRow; while (!quene.IsEmpty()) { quene.DeQuene(pRow); // if (pRow->IsParentRow()) if (pRow != NULL && pRow->IsTreeRow() && pRow->IsParentRow()) { WDGridTreeRow* pTreeRow = (WDGridTreeRow*)pRow; for (pTreeRow->MoveToFirst(); !pTreeRow->IsOut(); pTreeRow->MoveToNext()) quene.EnQuene(pTreeRow->CurChild()); } else { long newId = GetAutoId((*pRow)[1]); sText.Format(_T("%d"), newId); (*pRow)[1].SetText(sText); } } } // 编辑 bool CBaseTreeGrid::OnModified(WDGridCell &cell, CString &text) { // if (cell.GetColIndex() == 1 && !IsRightLabel(text)) // { // CString sText; // sText.Format(_T("编号 %s 已经被使用,请选择其它编号!"), text); // MessageBox(sText); // AutoEditCell(); // return false; // } return true; } // 获取选中的行 WDGridRow* CBaseTreeGrid::GetSelRow() { if (rowSelList.GetLength() == 1) { return rowList[rowSelList[0]]; } return NULL; } bool CBaseTreeGrid::OnBeforeEdit(WDGridCell&cell) { // if (cell.GetRow().IsParentRow()) WDGridRow &row = cell.GetRow(); if (row.IsTreeRow() && row.IsParentRow()) if (cell.GetColIndex()>0) return false; return true; }