// GridFormView.cpp : implementation file // #include "stdafx.h" #include "resource.h" #include "GridView.h" #include "GridFormView.h" #include "GridDocument.h" //#include "FormSetDlg.h" #include "GridFrameWnd.h" #include "dbgroup.h" #include "TgAs.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif static const TCHAR *sMsgPrompt[]={ #include "GridFormView.hpp" }; class XDbContentRec { public: XString m_sTableName, m_sFieldName, m_sFieldDesc; public: XDbContentRec(const TCHAR *tn, const TCHAR *fn, const TCHAR *fd): m_sTableName(tn), m_sFieldName(fn), m_sFieldDesc(fd) {} XDbContentRec() {} }; class XDbContentTable: public DList { public: XDbContentTable(); XDbContentRec * LookUp(const TCHAR *tn, const TCHAR *fn); }; XDbContentTable::XDbContentTable() { XString sFile; if (!FindFile(_T("dbconten.dbf"), sFile)) return; CodeBase db; XDbfTable *pTable=db.Open(sFile); if (!pTable) return; for(pTable->MoveFirst(); !pTable->IsEOF(); pTable->MoveNext()) { // sg modify 100825,解决64位移植后的BUG,强制转换后,数据为空 CString sTableName, sFieldName, sFieldDesc; XDbContentRec *pRec = new XDbContentRec; // Zyu modify on 100721,添加强制转换 //GET_FIELD(pTable, _T("tablename"), pRec->m_sTableName); //GET_FIELD(pTable, _T("fieldname"), pRec->m_sFieldName); //GET_FIELD(pTable, _T("fielddesc"), pRec->m_sFieldDesc); GET_FIELD(pTable, _T("tablename"), sTableName); GET_FIELD(pTable, _T("fieldname"), sFieldName); GET_FIELD(pTable, _T("fielddesc"), sFieldDesc); sTableName = pRec->m_sTableName; sFieldName = pRec->m_sFieldName; sFieldDesc = pRec->m_sFieldDesc; Append(pRec); } } XDbContentRec * XDbContentTable::LookUp(const TCHAR *tn, const TCHAR *fn) { for(int i=0; im_sTableName, tn)==0 && _tcsicmp(pRec->m_sFieldName, fn)==0) return pRec; } return NULL; } // modify by xlc on 090902 消除tg5lib影响 //#if ADS<18 #if 0 ///////////////////////////////////////////////////////////////////////////// // CGridFormView IMPLEMENT_DYNCREATE(CGridFormView, CGridView) CGridFormView::CGridFormView() : CGridView(CGridFormView::IDD) { m_bEnable=TRUE; m_pFormSetDlg=NULL; m_ColorSelect=RGB(0,0,255); double nSize=3.5; //GetTextSize(DS_CHINA); m_nHeadTxtSize=m_nConTxtSize=nSize; m_nTitTxtSize=1.5*nSize; m_nHeadRowHeight=m_nConRowHeight=2*nSize; m_sTitleName=_T(""); //{{AFX_DATA_INIT(CGridFormView) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT } CGridFormView::~CGridFormView() { } void CGridFormView::DoDataExchange(CDataExchange* pDX) { CGridView::DoDataExchange(pDX); //{{AFX_DATA_MAP(CGridFormView) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CGridFormView, CGridView) //{{AFX_MSG_MAP(CGridFormView) ON_WM_SIZE() ON_COMMAND(IDM_INSERTDBF, OnInsertdbf) ON_UPDATE_COMMAND_UI(IDM_INSERTDBF, OnUpdateInsertdbf) ON_WM_DESTROY() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CGridFormView diagnostics #ifdef _DEBUG void CGridFormView::AssertValid() const { CGridView::AssertValid(); } void CGridFormView::Dump(CDumpContext& dc) const { CGridView::Dump(dc); } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CGridFormView message handlers XDbfTable *CGridFormView::OpenDbfTable() // override member from CGridView { XString sFileSp(256); CGridDocument *pDoc=(CGridDocument *)GetDocument(); ASSERT(pDoc); XDbfTable *pData=m_db.Open(pDoc->GetPathName()); if (!pData) return NULL; XString sTableKey; pDoc->GetDbfTableKey(sTableKey); XDbContentTable contentTab; FieldInfo *pFieldArray = pData->GetFieldInfo(); int nColumn = pData->GetFieldNum(); m_lstHeadInfo.Reset(); for(int i=0; im_sFieldDesc; } int nWidthOfHead=max(pFieldArray[i].len, _tcslen(sFieldDesc)); m_lstHeadInfo.Append(new CFieldHead(TString(pFieldArray[i].name), sFieldDesc, min(100, max(30, short(5*nWidthOfHead))))); m_lstAlignMode.Append(new int(dbgLeft)); m_lstIsSelect.Append(new int(0)); m_lstColumnWidth.Append(new double(0)); } FieldInfo::ReleaseArray(pFieldArray); return pData; } void CGridFormView::OnInitialUpdate() { CGridView::OnInitialUpdate(); UpdateColumnWidth(); } void CGridFormView::OnSize(UINT nType, int cx, int cy) { CGridView::OnSize(nType, cx, cy); if ( ::IsWindow(m_grid.m_hWnd) ) m_grid.SetWindowPos( NULL, 0, 0, cx, cy,SWP_SHOWWINDOW); } void CGridFormView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) { GetBookmarks(TRUE); m_grid.ReBind(); } BEGIN_EVENTSINK_MAP(CGridFormView, CGridView) //{{AFX_EVENTSINK_MAP(CGridFormView) ON_EVENT(CGridFormView, IDC_GRID, -603 /* KeyPress */, OnKeyPressGrid, VTS_PI2) ON_EVENT(CGridFormView, IDC_GRID, 10 /* HeadClick */, OnHeadClickGrid, VTS_I2) ON_EVENT(CGridFormView, IDC_GRID, 9 /* ColResize */, OnColResizeGrid, VTS_I2 VTS_PI2) //}}AFX_EVENTSINK_MAP END_EVENTSINK_MAP() void CGridFormView::OnKeyPressGrid(short FAR* KeyAscii) { if (*KeyAscii == VK_RETURN) { m_grid.Update(); } else if (*KeyAscii==VK_DELETE) { } } void CGridFormView::OnHeadClickGrid(short ColIndex) { //本事件功能: 单击列头时,若该列的背景色是蓝色,则背景色切换成白色, //前景色切换成黑色; 否则背景色切换成蓝色,前景色切换成白色 short Cret=GetKeyState(VK_CONTROL); short Sret=GetKeyState(VK_SHIFT); if (Cret==(short)0xff81 || Cret==(short)0xff80) { //CTRL+Left Mouse CColumn Coli=m_grid.GetColumns().GetItem(ColIndex); if (m_ColorSelect==Coli.GetBackColor()) { Coli.SetBackColor(RGB(255,255,255)); Coli.SetForeColor(RGB(0,0,0)); *m_lstIsSelect[ColIndex]=0; } else { Coli.SetBackColor(m_ColorSelect); Coli.SetForeColor(RGB(255,255,255)); *m_lstIsSelect[ColIndex]=1; } double nMinWidth=0,nTotalWidth=0; CalculateWidth(nMinWidth,nTotalWidth); /* if (m_pFormSetDlg) { int nAlignMode=GetAlignMode(); CFormSetDlg* pFormSetDlg=(CFormSetDlg*)m_pFormSetDlg; pFormSetDlg->UpdateDisplay(nMinWidth,nTotalWidth); pFormSetDlg->UpdateAlignMode(nAlignMode); }*/ } else if (Sret==(short)0xff81 || Sret==(short)0xff80) { //SHIFT+Left Mouse int& AlignMode=*m_lstAlignMode[ColIndex]; switch (AlignMode) { case dbgLeft: AlignMode=dbgCenter; break; case dbgCenter: AlignMode=dbgRight; break; case dbgRight: AlignMode=dbgLeft; break; } CColumn Coli=m_grid.GetColumns().GetItem(ColIndex); Coli.SetAlignment(AlignMode); Coli.SetHeadAlignment(dbgCenter); /* if (m_pFormSetDlg) { int nAlignMode=GetAlignMode(); CFormSetDlg* pFormSetDlg=(CFormSetDlg*)m_pFormSetDlg; pFormSetDlg->UpdateAlignMode(nAlignMode); }*/ } else CGridView::OnHeadClickMatgrid(ColIndex); } void CGridFormView::OnColResizeGrid(short ColIndex, short FAR* Cancel) { UpdateColumnWidth(); if (!m_bEnable) { double nMinWidth=0,nTotalWidth=0; CalculateWidth(nMinWidth,nTotalWidth); /* if (m_pFormSetDlg) { CFormSetDlg* pFormSetDlg=(CFormSetDlg*)m_pFormSetDlg; pFormSetDlg->UpdateDisplay(nMinWidth,nTotalWidth); }*/ } } BOOL CGridFormView::PreTranslateMessage(MSG* pMsg) { if (m_grid.IsSpecialKey(pMsg)) return TRUE; else if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_DELETE) { BOOL bRet=FALSE; { CTrueDBGrid50_SelBookmarks bmks = m_grid.GetSelBookmarks(); int nCount=bmks.GetCount(); if (nCount>1) { for( int i=0; i0 ) { m_grid.SetBookmark( COleVariant(*m_lstBookmarks[0]) ); } else { COleVariant vNull; vNull.ChangeType( VT_NULL ); m_grid.SetBookmark( vNull ); } m_grid.ReBind(); return bRet; } } return CGridView::PreTranslateMessage(pMsg); } //extern int ReadDbfAndDraw(const TCHAR *szFile, BOOL bSort, int nSortField, BOOL bAscend); // 在当前图中绘制 void CGridFormView::UpdateAlignMode(int nAlignMode) { BOOL bIsSelect=IsSelect(); int len=m_lstHeadInfo.Length(); for(int i=0;iMoveTo(bookMark); GET_FIELD(m_pTable, m_lstHeadInfo[nColumnIndex]->m_sName, sContent); nHeight=m_nConTxtSize; } else { sContent=m_lstHeadInfo[nColumnIndex]->m_sDesc; nHeight=m_nHeadTxtSize; } mText.setTextStyle(textId); mText.setTextHeight(nHeight); mText.setContents(sContent); nWidth=mText.actualWidth()+0.4*nHeight; if (i==len) nHeadLen=nWidth; if (nMaxLen1) { CalculateTextWidth(i,nMaxLen,nHeadLen); nMinWidth+=nMaxLen; if (fabs(nMaxLen-nHeadLen)<1.0E-6) nHeight=m_nHeadTxtSize; else nHeight=m_nConTxtSize; nWidth=nMaxLen > nw*nHeight/9.0 ? nMaxLen : nw*nHeight/9.0; nTotalWidth+=nWidth; } } } } void CGridFormView::CalculateWidth(DList& lstColumnSize,DList& lstAlignMode, DList& lstHeadInfo,double& nMinWidth, double& nTotalWidth) { double nHeight,nWidth; nMinWidth=nTotalWidth=0; BOOL bIsSelect=IsSelect(); int len=m_lstHeadInfo.Length(); if (lstColumnSize.Length()) lstColumnSize.Reset(); if (lstAlignMode.Length()) lstAlignMode.Reset(); if (lstHeadInfo.Length()) lstHeadInfo.Reset(); for(int i=0;i1) { CalculateTextWidth(i,nMaxLen,nHeadLen); nMinWidth+=nMaxLen; if (fabs(nMaxLen-nHeadLen)<1.0E-6) nHeight=m_nHeadTxtSize; else nHeight=m_nConTxtSize; nWidth=nMaxLen > nw*nHeight/9.0 ? nMaxLen : nw*nHeight/9.0; nTotalWidth+=nWidth; } else nWidth=nw; lstColumnSize.Append(new double(nWidth)); } } } void CGridFormView::ArrangeWidth(DList& lstColumnSize,double nMinWidth, double& nTotalWidth,double nTotal) { if (fabs(nTotal-nTotalWidth) > 1.0E-6) { BOOL bNext=FALSE; BOOL bIsSelect=IsSelect(); double nFactor=nTotal/nTotalWidth; int len=m_lstHeadInfo.Length(); int lenc=lstColumnSize.Length(); for(int i=0,j=0;i1) { CalculateTextWidth(i,nMaxLen,nHeadLen); if (nSize*nFactor < nMaxLen) { // bNext=TRUE; break; } } } } if (bNext) nFactor=nTotal/nMinWidth; for(i=0,j=0;i1) { CalculateTextWidth(i,nMaxLen,nHeadLen); // nSize=nFactor*nMaxLen; } } else nSize*=nFactor; } } nTotalWidth=nTotal; } } void CGridFormView::OnInsertdbf() { acedPostCommand(_T("InsertDbf\n")); } AcDbObjectId CGridFormView::AddToModelSpace(AcDbEntity *pEntity) { AcDbObjectId nId=AcDbObjectId::kNull; XDbBlockTableRec mspace(ACDB_MODEL_SPACE, AcDb::kForWrite); if (!!mspace) { AcDbBlockTableRecord &rec=mspace; rec.appendAcDbEntity(nId,pEntity); pEntity->close(); } return nId; } AcDbObjectId CGridFormView::AddTextToModelSpace(XString sDesc,AcDbObjectId nTextId,double nHeight, AcDb::TextHorzMode nHorzMode,AcGePoint3d pt, AcDbTextStyleTableRecord* pTextStyle) { AcDbText* pText = new AcDbText; pText->setTextString(sDesc); pText->setTextStyle(nTextId); pText->setHeight(nHeight); pText->setPosition(pt); pText->setHorizontalMode(nHorzMode); pText->setAlignmentPoint(pt); if (pTextStyle) { pText->setWidthFactor(pTextStyle->xScale()); pText->setOblique(pTextStyle->obliquingAngle()); } return AddToModelSpace(pText); } void CGridFormView::AddToGroupDict(const TCHAR* sGroupName,AcDbObjectIdArray idArray) { AcDbObjectId nID; int nCount=0,iMax=0; CString MaxItem,pName; int nNum=_tcslen(sGroupName); AcDbDictionary* pGroupDict=NULL; acdbCurDwg()->getGroupDictionary(pGroupDict, AcDb::kForWrite); AcDbDictionaryIterator* pDictIter = pGroupDict->newIterator(); for(; !pDictIter->done(); pDictIter->next()) { pName = pDictIter->name(); if (!pName.Find(sGroupName) && _ttoi(pName.Right(pName.GetLength()-nNum))) { if (MaxItem < pName) MaxItem = pName; } } delete pDictIter; if (pName.GetLength() == (nNum + 1)) iMax = _ttoi(pName.Right(1))*10; else if (!pName.IsEmpty()) iMax = _ttoi(pName.Right(pName.GetLength()-nNum)); pName.Format(_T("%d"),++iMax); pName = sGroupName + pName; AcDbGroup* pGroup=new AcDbGroup; pGroupDict->setAt(pName,pGroup,nID); pGroupDict->close(); int len=idArray.length(); for(int i=0;iappend(idArray[i]); pGroup->setSelectable(Adesk::kTrue); pGroup->close(); } void CGridFormView::EnableOtherGridFrame(BOOL bEnable /*=TRUE*/) { CWnd* pWnd=NULL; CMDIFrameWnd* pFrame=acedGetAcadFrame(); CWnd* pClient=CWnd::FromHandle(pFrame->m_hWndMDIClient); pWnd=pClient->GetWindow(GW_CHILD); while(pWnd->GetSafeHwnd()) { if (pWnd->IsKindOf(RUNTIME_CLASS(CGridFrameWnd))) { if (pWnd->GetSafeHwnd()!=this->GetParentFrame()->GetSafeHwnd()) pWnd->EnableWindow(bEnable); } pWnd=pWnd->GetWindow(GW_HWNDNEXT); } } int CGridFormView::ArrangeWindow() { CRect rect; double nTotalLen=0; int nColLen,nType,nPos; nColLen=m_lstColumnWidth.Length(); for(int i=0;im_hWndMDIClient); pClient->GetWindowRect(rect); if (nTotalLen>0.8*rect.Width()) { nPos=1; nType=MDITILE_HORIZONTAL; } else { nPos=0; nType=MDITILE_VERTICAL; } pFrameWnd->MDITile(nType|MDITILE_SKIPDISABLED); CView *pView=acedGetAcadDwgView(); CFrameWnd *pDwgFrame=pView->GetParentFrame(); pDwgFrame->ActivateFrame(); return nType; } void CGridFormView::Insertdbf() { #if 0 EnableOtherGridFrame(FALSE); int nPos=ArrangeWindow(); TCHAR ch[2]; ch[0]=(TCHAR)'\x1B'; ch[1]=(TCHAR)'\0'; int nRowNum,nColNum,nOutColNum,nAlignMode,i,j; XPoint ptPick,pt,pt1,pt2; nRowNum=m_lstBookmarks.Length(); nColNum=m_lstHeadInfo.Length(); double nMinWidth=0,nTotalWidth=0,nTotalHeight=0,nHeight,w,scale=1; AcDbObjectId textId=InitTextStyle(TS_STANDARD); XDbObject objTextStyle(textId, AcDb::kForRead); AcDbTextStyleTableRecord *pTextStyle = AcDbTextStyleTableRecord::cast(objTextStyle.m_pObject); DList lstAlignMode; DList lstColumnSize; DList lstHeadInfo; CalculateWidth(lstColumnSize,lstAlignMode,lstHeadInfo,nMinWidth,nTotalWidth); nOutColNum=lstHeadInfo.Length(); for(i=0;im_sDesc,textId,m_nHeadTxtSize,AcDb::kTextCenter, (AcGePoint3d)pt,pTextStyle)); pt.x+=*lstColumnSize[i]/2; } double nSpace=0.2*m_nConTxtSize; for(i=0;iMoveTo(bookMark); for(j=0;jm_sName, sContent); idArray.append(AddTextToModelSpace(sContent,textId,m_nConTxtSize,hMode,(AcGePoint3d)pt,pTextStyle)); switch (*lstAlignMode[j]) { case dbgLeft: pt.x-=nSpace; pt.x+=*lstColumnSize[j]; break; case dbgCenter: pt.x+=*lstColumnSize[j]/2; break; case dbgRight: pt.x+=nSpace; break; } } } AddToGroupDict(_T("FORM"),idArray); acedPostCommand(ch); EnableOtherGridFrame(); #endif } void CGridFormView::OnUpdateInsertdbf(CCmdUI* pCmdUI) { pCmdUI->Enable(m_bEnable); } void CGridFormView::OnDestroy() { if (m_pFormSetDlg->GetSafeHwnd()) { m_pFormSetDlg->DestroyWindow(); m_pFormSetDlg=NULL; EnableOtherGridFrame(); } if (!m_bEnable) acedPostCommand(_T("\n")); //终止本命令 CGridView::OnDestroy(); } //extern CUIntArray g_tempFileIndexArray; void CGridFormView::PostNcDestroy() { CGridDocument *pDoc=(CGridDocument*)GetDocument(); ASSERT(pDoc); CString szFileName, szFilePath; szFilePath = pDoc->GetPathName(); int nEnd = szFilePath.ReverseFind('\\'); if (nEnd != -1) szFileName = szFilePath.Right(szFilePath.GetLength() - nEnd - 1); else szFileName = szFilePath; if (szFileName.Find('~') != -1 && szFileName.Find('$') != -1) { if (FindFile(szFilePath)) { m_pTable->Close(); _tremove(szFilePath); /* CString szString = szFileName.Right(7); int nCurIndex = _ttoi(szString.Left(4)) - 2000; int nSize = g_tempFileIndexArray.GetSize(); if (nSize >= 1) { if (nSize > 1) g_tempFileIndexArray.InsertAt(nSize - 1, nCurIndex); else { int nMaxIndex = g_tempFileIndexArray[0]; if (nCurIndex <= nMaxIndex - 1) { if (nCurIndex == nMaxIndex - 1 && nMaxIndex > 1) g_tempFileIndexArray[0]--; else g_tempFileIndexArray.InsertAt(0, nCurIndex); } } } nSize = g_tempFileIndexArray.GetSize(); for (int i = 0, k ; i < nSize ; i++) { k = i; for (int j = i + 1 ; j < nSize ; j++) { if (g_tempFileIndexArray[k] > g_tempFileIndexArray[j]) k = j; } if (k != i) { int temp = g_tempFileIndexArray[k]; g_tempFileIndexArray[k] = g_tempFileIndexArray[i]; g_tempFileIndexArray[i] = temp; } } if (nSize > 1 && nSize == int(g_tempFileIndexArray[nSize - 1])) { g_tempFileIndexArray.SetSize(1); g_tempFileIndexArray[0] = 1; }*/ } } CGridView::PostNcDestroy(); } #endif void InsertDbf() { CWnd* pWnd=NULL; CMDIFrameWnd* pFrame=acedGetAcadFrame(); CWnd* pClient=CWnd::FromHandle(pFrame->m_hWndMDIClient); pWnd=pClient->GetWindow(GW_CHILD); if (pWnd->IsKindOf(RUNTIME_CLASS(CGridFrameWnd))) { pWnd=pWnd->GetWindow(GW_CHILD); } else { while(pWnd=pWnd->GetWindow(GW_HWNDNEXT)) { if (pWnd->IsKindOf(RUNTIME_CLASS(CGridFrameWnd))) { pWnd=pWnd->GetWindow(GW_CHILD); break; } } } // modify by xlc on 090902 消除tg5lib影响 //#if ADS<18 #if 0 if (pWnd && pWnd->IsKindOf(RUNTIME_CLASS(CGridFormView))) { CGridFormView* pFormView=(CGridFormView*)pWnd; pFormView->Insertdbf(); } #else AfxMessageBox(_T("此处有问题,InsertDbf")); #endif }