DatabaseView.cpp
上传用户:y440e3
上传日期:2010-03-08
资源大小:200k
文件大小:12k
源码类别:

GIS编程

开发平台:

Visual C++

  1. // DatabaseView.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "draw.h"
  5. #include "drawdoc.h"
  6. #include "selectdatadlg.h"
  7. #include "DatabaseView.h"
  8. #include "data1dlg.h"
  9. #include "sortdlg.h"
  10. #include "linkdlg.h"
  11. #include "filterdlg.h"
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CDatabaseView
  19. IMPLEMENT_DYNCREATE(CDatabaseView, CScrollView)
  20. CDatabaseView::CDatabaseView()
  21. {
  22. nIndex=-1;
  23. sizeTotal.cx=sizeTotal.cy=300;
  24. sizePage.cx=sizePage.cy=300;
  25. sizeLine.cx=sizeLine.cy=10;
  26. nSelectData=0;
  27. }
  28. CDatabaseView::~CDatabaseView()
  29. {
  30. }
  31. BEGIN_MESSAGE_MAP(CDatabaseView, CScrollView)
  32. //{{AFX_MSG_MAP(CDatabaseView)
  33. ON_WM_SIZE()
  34. ON_WM_LBUTTONDOWN()
  35. ON_COMMAND(ID_RECORD_ADD, OnRecordAdd)
  36. ON_COMMAND(ID_RECORD_MODIFY, OnRecordModify)
  37. ON_COMMAND(ID_RECORD_FILTER, OnRecordFilter)
  38. ON_COMMAND(ID_RECORD_SORT, OnRecordSort)
  39. ON_COMMAND(ID_RECORD_DELETE, OnRecordDelete)
  40. ON_COMMAND(ID_RECORD_LINK, OnRecordLink)
  41. ON_COMMAND(ID_RECORD_DELETE_ALL, OnRecordDeleteAll)
  42. ON_COMMAND(ID_FILE_SELECT, OnFileSelect)
  43. //}}AFX_MSG_MAP
  44. END_MESSAGE_MAP()
  45. /////////////////////////////////////////////////////////////////////////////
  46. // CDatabaseView drawing
  47. void CDatabaseView::OnInitialUpdate()
  48. {
  49. CDrawDoc* pDoc = GetDocument();
  50. m_pSet1=&pDoc->m_data1;
  51. m_pSet=pDoc->GetDataSet(nSelectData);
  52. m_pSet=&pDoc->m_data1;
  53. // m_pSet2=(CRecordSet *)&pDoc->m_data1;
  54. m_pLinkSet1=&pDoc->m_linkdata1;
  55. Init1();
  56. Init2();
  57. CScrollView::OnInitialUpdate();
  58. SetScrollSizes(MM_TEXT, sizeTotal,sizePage,sizeLine);
  59. //CSize sizeTotal;
  60. }
  61. void CDatabaseView::Init1()
  62. {
  63. TEXTMETRIC tm;
  64. CDC* dc=GetDC();
  65. dc->GetTextMetrics(&tm);
  66. ReleaseDC(dc);
  67. nCharHeight=tm.tmHeight+2;
  68. nCharWidth=tm.tmAveCharWidth+2;
  69. if(m_pSet->IsOpen())
  70. m_pSet->Close();
  71. m_pSet->Open();
  72. nField=m_pSet->GetODBCFieldCount();
  73. CODBCFieldInfo fieldinfo1;
  74. CODBCFieldInfo& fieldinfo=fieldinfo1;
  75. for(int i=0;i<nField;i++)
  76. {
  77. m_pSet->GetODBCFieldInfo(i,fieldinfo);
  78. nFieldWide[i]=(short)fieldinfo1.m_nPrecision;
  79. }
  80. nScreenWide=0;
  81. nFieldStart[0]=0;
  82. for(i=0;i<nField;i++)
  83. {
  84. nScreenWide=nScreenWide+nFieldWide[i]*nCharWidth;
  85. nFieldStart[i+1]=nFieldStart[i]+nFieldWide[i]*nCharWidth;
  86. }
  87. sizeTotal.cx=nScreenWide;
  88. sizeLine.cx=nCharWidth*5;
  89. }
  90. void CDatabaseView::Init2()
  91. {
  92. if(m_pSet->IsOpen())
  93. m_pSet->Close();
  94. m_pSet->Open();
  95. if(m_pSet->IsBOF())
  96. nRecord=0;
  97. else
  98. {
  99. m_pSet->MoveFirst();
  100. nRecord=0;
  101. while(!m_pSet->IsEOF())
  102. {
  103. m_pSet->MoveNext();
  104. nRecord++;
  105. }
  106. }
  107. nScreenHeight=nRecord*nCharHeight;
  108. if(nScreenHeight==0)
  109. nScreenHeight=20;
  110. sizeTotal.cy=nScreenHeight;
  111. sizeLine.cy=nCharHeight+2;
  112. if(nRecord>0)
  113. {
  114. if(nIndex<1||nIndex>nRecord)
  115. nIndex=1;
  116. }
  117. else 
  118. nIndex=-1;
  119. // SetScrollSizes(MM_TEXT, sizeTotal,sizePage,sizeLine);
  120. }
  121. /*void CDatabaseView::Init()
  122. {
  123. TEXTMETRIC tm;
  124. CDC* dc=GetDC();
  125. dc->GetTextMetrics(&tm);
  126. ReleaseDC(dc);
  127. nCharHeight=tm.tmHeight+2;
  128. nCharWidth=tm.tmAveCharWidth+2;
  129. if(m_pSet1->IsOpen())
  130. m_pSet1->Close();
  131. // m_pSet1->m_strFilter="only_id>0";
  132. m_pSet1->Open();
  133. if(m_pSet1->IsBOF())
  134. nRecord=0;
  135. else
  136. {
  137. m_pSet1->MoveFirst();
  138. nRecord=0;
  139. while(!m_pSet1->IsEOF())
  140. {
  141. m_pSet1->MoveNext();
  142. nRecord++;
  143. }
  144. }
  145. nField=m_pSet1->GetODBCFieldCount();
  146. CODBCFieldInfo fieldinfo1;
  147. CODBCFieldInfo& fieldinfo=fieldinfo1;
  148. //nRecord=m_pSet1->GetRecordCount();
  149. for(int i=0;i<nField;i++)
  150. {
  151. m_pSet1->GetODBCFieldInfo(i,fieldinfo);
  152. nFieldWide[i]=(short)fieldinfo1.m_nPrecision;
  153. }
  154. nScreenHeight=nRecord*nCharHeight;
  155. nScreenWide=0;
  156. nFieldStart[0]=0;
  157. for(i=0;i<nField;i++)
  158. {
  159. nScreenWide=nScreenWide+nFieldWide[i]*nCharWidth;
  160. nFieldStart[i+1]=nFieldStart[i]+nFieldWide[i]*nCharWidth;
  161. }
  162. if(nScreenHeight==0)
  163. nScreenHeight=20;
  164. if(nRecord>0)
  165. nIndex=1;
  166. else
  167. nIndex=-1;
  168. sizeTotal.cx=nScreenWide;
  169. sizeTotal.cy=nScreenHeight;
  170. // TODO: calculate the total size of this view
  171. sizeLine.cx=nCharWidth*5;
  172. sizeLine.cy=nCharHeight+2;
  173. SetScrollSizes(MM_TEXT, sizeTotal,sizePage,sizeLine);
  174. }
  175. */
  176. void CDatabaseView::OnDraw(CDC* pDC)
  177. {
  178. CRect r1;
  179. CDrawDoc* pDoc = GetDocument();
  180. CPen pen1(0,1,RGB(255,0,0));
  181. // CPen* pOldPen=ht.SelectObject(&pen1);
  182. pDC->SetTextColor(RGB(255,0,0));
  183. CString c1;
  184. CString& c2=c1;
  185. pDC->SetMapMode(1);
  186. m_pSet->MoveFirst();
  187. for(int i=0;i<nRecord;i++)
  188. {
  189. for(int j=0;j<nField;j++)
  190. {
  191. m_pSet->GetFieldValue(j,c2);
  192. pDC->TextOut(nFieldStart[j],i*nCharHeight,c1);
  193. }
  194. m_pSet->MoveNext();
  195. }
  196. if(nIndex>0)
  197. {
  198. GetRect(nIndex,&r1);
  199. pDC->InvertRect(&r1);
  200. }
  201. // pDC->SelectObject(pOldPen);
  202. }
  203. /////////////////////////////////////////////////////////////////////////////
  204. // CDatabaseView diagnostics
  205. #ifdef _DEBUG
  206. void CDatabaseView::AssertValid() const
  207. {
  208. CScrollView::AssertValid();
  209. }
  210. void CDatabaseView::Dump(CDumpContext& dc) const
  211. {
  212. CScrollView::Dump(dc);
  213. }
  214. CDrawDoc* CDatabaseView::GetDocument() // non-debug version is inline
  215. {
  216. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDrawDoc)));
  217. return (CDrawDoc*)m_pDocument;
  218. }
  219. #endif //_DEBUG
  220. /////////////////////////////////////////////////////////////////////////////
  221. // CDatabaseView message handlers
  222. void CDatabaseView::OnSize(UINT nType, int cx, int cy) 
  223. {
  224. CScrollView::OnSize(nType, cx, cy);
  225. // TODO: Add your message handler code here
  226. sizePage.cx=cx;
  227. sizePage.cy=cy;
  228. SetScrollSizes(MM_TEXT, sizeTotal,sizePage,sizeLine);
  229. }
  230. void CDatabaseView::OnLButtonDown(UINT nFlags, CPoint point) 
  231. {
  232. // TODO: Add your message handler code here and/or call default
  233. CRect r1;
  234. CClientDC ht(this);
  235. OnPrepareDC(&ht);
  236. ht.DPtoLP(&point);
  237. int nIndex1=point.y/nCharHeight+1;
  238. if(nIndex1!=nIndex&&nIndex1>0&&nIndex1<=nRecord)
  239. {
  240. if(nIndex>0)
  241. {
  242. GetRect(nIndex,&r1);
  243. ht.InvertRect(&r1);
  244. }
  245. GetRect(nIndex1,&r1);
  246. ht.InvertRect(&r1);
  247. nIndex=nIndex1;
  248. }
  249. CScrollView::OnLButtonDown(nFlags, point);
  250. }
  251. void CDatabaseView::GetRect(int nRow, CRect *r1)
  252. {
  253. r1->left=0;
  254. r1->right=nFieldStart[nField-1]+nFieldWide[nField-1]*nCharWidth;
  255. r1->top=(nRow-1)*nCharHeight;
  256. r1->bottom=r1->top+nCharHeight;
  257. }
  258. void CDatabaseView::OnRecordAdd() 
  259. {
  260. // TODO: Add your command handler code here
  261. CDrawDoc* pDoc=GetDocument();
  262. if(m_pSet->IsOpen())
  263. m_pSet->Close();
  264. int ID=pDoc->DataInfo[nSelectData].only_ID;
  265. if(ID==1)
  266. {
  267. CData1Dlg dlg(m_pSet1,1,GetOnlyID());
  268. if(dlg.DoModal()==IDOK)
  269. {
  270. Init2();
  271. SetScrollSizes(MM_TEXT, sizeTotal,sizePage,sizeLine);
  272. UpdateWindow();
  273. Invalidate();
  274. }
  275. }
  276. else if(ID==2)
  277. {
  278. // CData2Dlg dlg(m_pSet2,1,GetOnlyID());
  279. CData1Dlg dlg(m_pSet1,1,GetOnlyID());
  280. if(dlg.DoModal()==IDOK)
  281. {
  282. Init2();
  283. SetScrollSizes(MM_TEXT, sizeTotal,sizePage,sizeLine);
  284. UpdateWindow();
  285. Invalidate();
  286. }
  287. }
  288. }
  289. /*int CDatabaseView::GetOnlyID()
  290. {
  291. int *Index1,id;
  292. Index1= GetDocument()->m_Index;
  293. CString m_str1=m_pSet1->m_strFilter;
  294. m_pSet1->m_strFilter="";
  295. if(m_pSet1->IsOpen())
  296. m_pSet1->Close();
  297. m_pSet1->Open();
  298. for(int i=0;i<20000;i++)
  299. Index1[i]=0;
  300. while(!m_pSet1->IsEOF())
  301. {
  302. Index1[m_pSet1->m_only_id]=1;
  303. m_pSet1->MoveNext();
  304. }
  305. for(i=0;i<20000;i++)
  306. {
  307. if(Index1[i]==0)
  308. {
  309. id=i;
  310. break;
  311. }
  312. }
  313. m_pSet1->Close();
  314. m_pSet1->m_strFilter=m_str1;
  315. m_pSet1->Open();
  316. return id;
  317. }
  318. */
  319. /*
  320. int CDatabaseView::GetOnlyID()
  321. {
  322. int *Index1,id;
  323. Index1= GetDocument()->m_Index;
  324. CString m_str1=m_pSet->m_strFilter;
  325. m_pSet->m_strFilter="";
  326. if(m_pSet->IsOpen())
  327. m_pSet->Close();
  328. m_pSet->Open();
  329. for(int i=0;i<20000;i++)
  330. Index1[i]=0;
  331. while(!m_pSet->IsEOF())
  332. {
  333. Index1[m_pSet->GetID()]=1;
  334. m_pSet->MoveNext();
  335. }
  336. for(i=0;i<20000;i++)
  337. {
  338. if(Index1[i]==0)
  339. {
  340. id=i;
  341. break;
  342. }
  343. }
  344. m_pSet->Close();
  345. m_pSet->m_strFilter=m_str1;
  346. m_pSet->Open();
  347. return id;
  348. }
  349. */
  350. int CDatabaseView::GetOnlyID()
  351. {
  352. int *Index1,id;
  353. Index1= GetDocument()->m_Index;
  354. CString m_str1=m_pSet->m_strFilter;
  355. m_pSet->m_strFilter="";
  356. if(m_pSet->IsOpen())
  357. m_pSet->Close();
  358. m_pSet->Open();
  359. for(int i=0;i<20000;i++)
  360. Index1[i]=0;
  361. CString c1;
  362. CString& c2=c1;
  363. i=0;
  364. m_pSet->GetFieldValue(i,c2);
  365. while(!m_pSet->IsEOF())
  366. {
  367. Index1[atoi(c1)]=1;
  368. m_pSet->MoveNext();
  369. }
  370. for(i=0;i<20000;i++)
  371. {
  372. if(Index1[i]==0)
  373. {
  374. id=i;
  375. break;
  376. }
  377. }
  378. m_pSet->Close();
  379. m_pSet->m_strFilter=m_str1;
  380. m_pSet->Open();
  381. return id;
  382. }
  383. void CDatabaseView::OnRecordModify() 
  384. {
  385. // TODO: Add your command handler code here
  386. CDrawDoc* pDoc=GetDocument();
  387. if(nIndex<=0&&nIndex>nRecord)
  388. return;
  389. CString m_str1=m_pSet->m_strFilter;
  390. CString m_str2=m_pSet->m_strSort;
  391. if(m_pSet->IsOpen())
  392. m_pSet->Close();
  393. int ID=pDoc->DataInfo[nSelectData].only_ID;
  394. if(ID==1)
  395. {
  396. if(m_pSet1->IsOpen())
  397. m_pSet1->Close();
  398. m_pSet1->m_strFilter=m_str1;
  399. m_pSet1->m_strSort=m_str2;
  400. m_pSet1->Open();
  401. m_pSet1->MoveFirst();
  402. m_pSet1->Move(nIndex-1);
  403. CData1Dlg dlg(m_pSet1,0,0);
  404. if(dlg.DoModal())
  405. {
  406. Init2();
  407. SetScrollSizes(MM_TEXT, sizeTotal,sizePage,sizeLine);
  408. Invalidate();
  409. }
  410. }
  411. else if(ID==2)
  412. {
  413. /* if(m_pSet2->IsOpen())
  414. m_pSet2->Close();
  415. m_pSet2->m_strFilter=m_str1;
  416. m_pSet2->m_strSort=m_str2;
  417. m_pSet2->Open();
  418. m_pSet2->MoveFirst();
  419. m_pSet2->Move(nIndex-1);
  420. CData2Dlg dlg(m_pSet2,0,0);*/
  421. CData1Dlg dlg(m_pSet1,0,0);
  422. if(dlg.DoModal())
  423. {
  424. Init2();
  425. SetScrollSizes(MM_TEXT, sizeTotal,sizePage,sizeLine);
  426. Invalidate();
  427. }
  428. }
  429. }
  430. void CDatabaseView::OnRecordFilter() 
  431. {
  432. // TODO: Add your command handler code here
  433. CFilterDlg dlg(m_pSet);
  434. if(dlg.DoModal()==IDOK)
  435. {
  436. m_pSet->m_strFilter=dlg.m_strFilter;
  437. Init2();
  438. SetScrollSizes(MM_TEXT, sizeTotal,sizePage,sizeLine);
  439. Invalidate();
  440. }
  441. }
  442. void CDatabaseView::OnRecordSort() 
  443. {
  444. // TODO: Add your command handler code here
  445. CSortDlg pDg1(m_pSet);
  446. if(pDg1.DoModal()==IDOK)
  447. {
  448. if(m_pSet->IsOpen())
  449. m_pSet->Close();
  450. m_pSet->m_strSort=pDg1.m_strSort;
  451. m_pSet->Open();
  452. Invalidate();
  453. }
  454. }
  455. void CDatabaseView::OnRecordDelete() 
  456. {
  457. // TODO: Add your command handler code here
  458. if(nIndex<1||nIndex>nRecord)
  459. return;
  460. if(!m_pSet->IsOpen())
  461. m_pSet->Open();
  462. m_pSet->MoveFirst();
  463. m_pSet->Move(nIndex-1);
  464. m_pSet->Delete();
  465. CPoint p1;
  466. p1=GetScrollPosition();
  467. sizeTotal.cy-=nCharHeight;
  468. if(nIndex==nRecord)
  469. nIndex--;
  470. Init2();
  471. SetScrollSizes(MM_TEXT, sizeTotal,sizePage,sizeLine);
  472. Invalidate();
  473. ScrollToPosition(p1);
  474. }
  475. void CDatabaseView::OnRecordLink() 
  476. {
  477. // TODO: Add your command handler code here
  478. CDrawDoc* pDoc=GetDocument();
  479. m_pSet1->MoveFirst();
  480. m_pSet1->Move(nIndex-1);
  481. CString m_str;
  482. m_str="(database_id=";
  483. short lb=pDoc->GetDataID(nSelectData);
  484. if(lb<0)
  485. return;
  486. char p1[9];
  487. sprintf(p1,"%d",lb);
  488. m_str+=p1;
  489. m_str+=") AND (record_id=";
  490. int ID=m_pSet->GetID();
  491. sprintf(p1,"%d",ID);
  492. m_str+=p1;
  493. m_str+=")";
  494. m_pLinkSet1->m_strFilter=m_str;
  495. CLinkDlg pDlg1(GetDocument(),m_pLinkSet1,1,lb,ID);
  496. pDlg1.DoModal();
  497. }
  498. void CDatabaseView::OnRecordDeleteAll() 
  499. {
  500. if(!m_pSet->IsOpen())
  501. m_pSet->Open();
  502. for(int i=nRecord-1;i>=0;i--)
  503. {
  504. m_pSet->MoveFirst();
  505. m_pSet->Move(i);
  506. m_pSet->Delete();
  507. }
  508. nIndex=-1;
  509. Init2();
  510. SetScrollSizes(MM_TEXT, sizeTotal,sizePage,sizeLine);
  511. Invalidate();
  512. }
  513. void CDatabaseView::OnFileSelect() 
  514. {
  515. // TODO: Add your command handler code here
  516. CDrawDoc* pDoc = GetDocument();
  517. CSelectDataDlg pDlg1(pDoc,nSelectData);
  518. if(pDlg1.DoModal()==IDOK)
  519. {
  520. int nn=pDlg1.m_Index;
  521. if(nn!=nSelectData)
  522. {
  523. if(m_pSet->IsOpen())
  524. m_pSet->Close();
  525. nSelectData=nn;
  526. m_pSet=pDoc->GetDataSet(nSelectData);
  527. m_pSet->Open();
  528. nIndex=-1;
  529. Init1();
  530. Init2();
  531. SetScrollSizes(MM_TEXT, sizeTotal,sizePage,sizeLine);
  532. Invalidate();
  533. }
  534. }
  535. }