ResultView.cpp
上传用户:jsxglz
上传日期:2007-01-03
资源大小:117k
文件大小:14k
源码类别:

SQL Server

开发平台:

Visual C++

  1. // ResultView.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "InteractiveSQL.h"
  5. #include "ResultView.h"
  6. #include "InteractiveSQLDoc.h"
  7. #include "resource.h"
  8. #include "MainFrm.h"
  9. #include "SQLView.h"
  10. #include "ExportDlg.h"
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CResultView
  18. IMPLEMENT_DYNCREATE(CResultView, CRichEditView)
  19. CResultView::CResultView()
  20. {
  21. m_nWordWrap = WrapNone;
  22. // Edit font
  23. LOGFONT logFont;
  24. memset(&logFont, 0, sizeof(logFont));
  25. logFont.lfHeight = -15;
  26. logFont.lfWeight = 400;
  27. strcpy(logFont.lfFaceName, "Courier");
  28. if(!m_font.CreateFontIndirect(&logFont))
  29. TRACE("Could Not create font.n");
  30. m_pGridCtrl = NULL;
  31. }
  32. CResultView::~CResultView()
  33. {
  34. if(m_font.m_hObject)
  35. {
  36. m_font.Detach();
  37. m_font.m_hObject = NULL;
  38. }
  39. }
  40. BEGIN_MESSAGE_MAP(CResultView, CRichEditView)
  41. //{{AFX_MSG_MAP(CResultView)
  42. ON_WM_SIZE()
  43. ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
  44. ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
  45. ON_WM_CREATE()
  46. ON_UPDATE_COMMAND_UI(ID_FILE_NEW, OnUpdateFileNew)
  47. ON_UPDATE_COMMAND_UI(ID_FILE_OPEN, OnUpdateFileOpen)
  48. ON_UPDATE_COMMAND_UI(ID_FILE_SAVE, OnUpdateFileSave)
  49. ON_UPDATE_COMMAND_UI(ID_FILE_SAVE_AS, OnUpdateFileSaveAs)
  50. ON_COMMAND(ID_FILE_PRINT, OnFilePrint)
  51. ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
  52. ON_WM_MOUSEACTIVATE()
  53. ON_UPDATE_COMMAND_UI(ID_FILE_PRINT, OnUpdateFilePrint)
  54. ON_COMMAND(ID_VIEW_WRAP_WORD, OnViewWrapWord)
  55. ON_UPDATE_COMMAND_UI(ID_VIEW_WRAP_WORD, OnUpdateViewWrapWord)
  56. ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
  57. ON_WM_SETFOCUS()
  58. ON_COMMAND(ID_GRID_SAVE_SELECTION, OnGridSaveSelection)
  59. ON_COMMAND(ID_EDIT_SELECT_ALL, OnEditSelectAll)
  60. ON_UPDATE_COMMAND_UI(ID_EDIT_SELECT_ALL, OnUpdateEditSelectAll)
  61. //}}AFX_MSG_MAP
  62. ON_COMMAND(ID_FILE_PRINT, CRichEditView::OnFilePrint)
  63. END_MESSAGE_MAP()
  64. /////////////////////////////////////////////////////////////////////////////
  65. // CResultView diagnostics
  66. #ifdef _DEBUG
  67. void CResultView::AssertValid() const
  68. {
  69. CRichEditView::AssertValid();
  70. }
  71. void CResultView::Dump(CDumpContext& dc) const
  72. {
  73. CRichEditView::Dump(dc);
  74. }
  75. CInteractiveSQLDoc* CResultView::GetDocument() // non-debug version is inline
  76. {
  77. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CInteractiveSQLDoc)));
  78. return (CInteractiveSQLDoc*)m_pDocument;
  79. }
  80. #endif //_DEBUG
  81. /////////////////////////////////////////////////////////////////////////////
  82. // CResultView message handlers
  83. void CResultView::OnSize(UINT nType, int cx, int cy) 
  84. {
  85. CRichEditView::OnSize(nType, cx, cy);
  86. if(m_pGridCtrl && m_pGridCtrl->m_hWnd != NULL)
  87. m_pGridCtrl->MoveWindow(0, 0, cx, cy);
  88. }
  89. void CResultView::OnEditCopy()
  90. {
  91. CWaitCursor wait;
  92. bool bCopyGrid = m_pGridCtrl && ::IsWindow(m_pGridCtrl->m_hWnd) &&
  93. m_pGridCtrl->IsWindowVisible();
  94. if(bCopyGrid)
  95. CopyGridData();
  96. else
  97. GetRichEditCtrl().Copy();
  98. }
  99. void CResultView::CopyGridData()
  100. {
  101. CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
  102. ASSERT(pFrame);
  103. pFrame->m_wndStatusBar.SetPaneText(0, "Copying...");
  104. long lCol = m_pGridCtrl->GetCol();
  105. long lRow = m_pGridCtrl->GetRow();
  106. long lColSel = m_pGridCtrl->GetColSel();
  107. long lRowSel = m_pGridCtrl->GetRowSel();
  108. CSharedFile memFile(GMEM_MOVEABLE | GMEM_DDESHARE | GMEM_ZEROINIT);
  109. CString sBuff;
  110. USES_CONVERSION;
  111. for(long lR = lRow; lR <= lRowSel; lR++)
  112. {
  113. for(long lC = lCol; lC <= lColSel; lC++)
  114. {
  115. sBuff = m_pGridCtrl->GetTextMatrix(lR, lC);
  116. memFile.Write(T2A(const_cast<LPTSTR>((LPCTSTR)sBuff)), sBuff.GetLength());
  117. if(lC < lColSel)
  118. memFile.Write("t", strlen("t"));
  119. }
  120. if(lR < lRowSel)
  121. memFile.Write("n", strlen("n"));
  122. }
  123. COleDataSource* pDataSource = new COleDataSource;
  124. ASSERT(pDataSource);
  125. pDataSource->CacheGlobalData(CF_TEXT, memFile.Detach());
  126. pDataSource->SetClipboard();
  127. pFrame->m_wndStatusBar.SetPaneText(0, "");
  128. }
  129. void CResultView::OnUpdateEditCopy(CCmdUI* pCmdUI) 
  130. {
  131. bool bEnable = (m_pGridCtrl && ::IsWindow(m_pGridCtrl->m_hWnd) &&
  132. m_pGridCtrl->IsWindowVisible()) || GetRichEditCtrl().GetTextLength();
  133. pCmdUI->Enable(bEnable);
  134. }
  135. int CResultView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  136. {
  137. if(CRichEditView::OnCreate(lpCreateStruct) == -1)
  138. return -1;
  139. if(m_font.m_hObject)
  140. GetRichEditCtrl().SetFont(&m_font);
  141. return 0;
  142. }
  143. void CResultView::OnUpdateFileNew(CCmdUI* pCmdUI) 
  144. {
  145. pCmdUI->Enable(FALSE);
  146. }
  147. void CResultView::OnUpdateFileOpen(CCmdUI* pCmdUI) 
  148. {
  149. pCmdUI->Enable(FALSE);
  150. }
  151. void CResultView::OnUpdateFileSave(CCmdUI* pCmdUI) 
  152. {
  153. pCmdUI->Enable(FALSE);
  154. }
  155. void CResultView::OnUpdateFileSaveAs(CCmdUI* pCmdUI) 
  156. {
  157. bool bEnable = (m_pGridCtrl && ::IsWindow(m_pGridCtrl->m_hWnd) &&
  158. m_pGridCtrl->IsWindowVisible());
  159. pCmdUI->Enable(bEnable || GetRichEditCtrl().GetTextLength());
  160. }
  161. void CResultView::OnFilePrint() 
  162. {
  163. CWaitCursor wait;
  164. CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
  165. ASSERT(pFrame);
  166. CDocument* pDoc = pFrame->GetActiveDocument();
  167. ASSERT(pDoc);
  168. CString sTitle = pDoc->GetTitle();
  169. if(sTitle.IsEmpty())
  170. sTitle = "Untitled.";
  171. if(m_strObjName.IsEmpty())
  172. m_strObjName = "Untitled";
  173. pFrame->LockWindowUpdate();
  174. pDoc->SetTitle(m_strObjName);
  175. CRichEditView::OnFilePrint();
  176. pDoc->SetTitle(sTitle);
  177. pFrame->UnlockWindowUpdate();
  178. }
  179. void CResultView::OnGridSaveSelection() 
  180. {
  181. if(!ExportResults(true))
  182. TRACE("Error exporting results.n");
  183. }
  184. void CResultView::OnFileSaveAs() 
  185. {
  186. CWaitCursor wait;
  187. bool bExport = m_pGridCtrl && ::IsWindow(m_pGridCtrl->m_hWnd) &&
  188. m_pGridCtrl->IsWindowVisible();
  189. if(bExport)
  190. {
  191. if(!ExportResults())
  192. TRACE("Error exporting results.n");
  193. }
  194. else
  195. {
  196. CStringEx sBuff;
  197. GetRichEditCtrl().GetWindowText(sBuff);
  198. CString strFilter, sDefaultExt;
  199. if(!m_strObjName.IsEmpty() && sBuff.FindNoCase(m_strObjName) != -1)
  200. {
  201. sDefaultExt = _T("sql");
  202. strFilter = _T("Procedure Files (*.sql)|*.sql|All Files (*.*)|*.*|");
  203. }
  204. else
  205. {
  206. sDefaultExt = _T("txt");
  207. strFilter = _T("Text Files (*.txt)|*.txt|All Files(*.*)|*.*|");
  208. m_strObjName = _T("Untitled"); // Emptied in <CMainFrame::OnExceptionClear>
  209. }
  210.     
  211. CFileDialog dlg(false, sDefaultExt, m_strObjName, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
  212. strFilter, this);
  213. if(dlg.DoModal() == IDOK)
  214. {
  215. CWaitCursor wait;
  216. CString sBuff;
  217. CString sPathName = dlg.GetPathName();
  218. CStdioFile file;
  219. CFileException fileException;
  220. if(!file.Open(sPathName, CFile::typeText | CFile::modeCreate |  
  221. CFile::modeWrite, &fileException))
  222. {
  223. sBuff.Format("File Path Name: <%s> ", sPathName);
  224. sBuff += CHelpers::GetFileExceptionError(fileException.m_cause);
  225. AfxMessageBox(sBuff);
  226. }
  227. else
  228. {
  229. GetRichEditCtrl().GetWindowText(sBuff);
  230. file.WriteString(sBuff);
  231. }
  232. }
  233. }
  234. }
  235. int CResultView::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message) 
  236. {
  237. const MSG* pMsg = GetCurrentMessage();
  238. GetParent()->SendMessage(WM_MOUSEACTIVATE, pMsg->wParam, pMsg->lParam);
  239. return CRichEditView::OnMouseActivate(pDesktopWnd, nHitTest, message);
  240. }
  241. HMENU CResultView::GetContextMenu(WORD /*wSelType*/, LPOLEOBJECT /*lpOleObj*/,
  242.    CHARRANGE* /*lpChrg*/)
  243. bool bOkToInvokeGridMenu = m_pGridCtrl && ::IsWindow(m_pGridCtrl->m_hWnd) &&
  244. m_pGridCtrl->IsWindowVisible();
  245. if(bOkToInvokeGridMenu)
  246. InvokeGridMenu();
  247. else
  248. {
  249. CMenu menu;
  250. if(menu.LoadMenu(IDR_RCLICK))
  251. {
  252. CMenu* pMenu = menu.GetSubMenu(2);
  253. ASSERT(pMenu);
  254. int nTextLength = GetRichEditCtrl().GetTextLength();
  255. if(!nTextLength)
  256. {
  257. pMenu->EnableMenuItem(ID_EDIT_SELECT_ALL, MF_BYCOMMAND | MF_GRAYED);
  258. pMenu->EnableMenuItem(ID_FILE_SAVE_AS, MF_BYCOMMAND | MF_GRAYED);
  259. pMenu->EnableMenuItem(ID_FILE_PRINT, MF_BYCOMMAND | MF_GRAYED);
  260. pMenu->EnableMenuItem(ID_EDIT_CLEAR, MF_BYCOMMAND | MF_GRAYED);
  261. }
  262. if(GetRichEditCtrl().GetSelText().IsEmpty())
  263. {
  264. pMenu->EnableMenuItem(ID_EDIT_CUT, MF_BYCOMMAND | MF_GRAYED);
  265. pMenu->EnableMenuItem(ID_EDIT_COPY, MF_BYCOMMAND | MF_GRAYED);
  266. }
  267. if(!GetRichEditCtrl().CanPaste())
  268. pMenu->EnableMenuItem(ID_EDIT_PASTE, MF_BYCOMMAND | MF_GRAYED);
  269. if(!GetRichEditCtrl().CanUndo())
  270. pMenu->EnableMenuItem(ID_EDIT_UNDO, MF_BYCOMMAND | MF_GRAYED);
  271. CPoint point;
  272. ::GetCursorPos(&point);
  273. pMenu->TrackPopupMenu(0, point.x, point.y, this);
  274. }
  275. }
  276. return NULL;
  277. }
  278. void CResultView::OnUpdateFilePrint(CCmdUI* pCmdUI) 
  279. {
  280. pCmdUI->Enable(GetRichEditCtrl().GetTextLength());
  281. }
  282. void CResultView::OnViewWrapWord() 
  283. {
  284. m_nWordWrap = (m_nWordWrap == WrapNone) ? WrapToWindow : WrapNone;
  285. WrapChanged();
  286. }
  287. void CResultView::OnUpdateViewWrapWord(CCmdUI* pCmdUI) 
  288. {
  289. pCmdUI->Enable(GetRichEditCtrl().GetTextLength());
  290. pCmdUI->SetCheck(m_nWordWrap == WrapToWindow);
  291. }
  292. void CResultView::SetWrapNone()
  293. {
  294. if(m_nWordWrap == WrapToWindow)
  295. {
  296. m_nWordWrap = WrapNone;
  297. WrapChanged();
  298. }
  299. }
  300. void CResultView::SetWrapToWindow()
  301. {
  302. if(m_nWordWrap == WrapNone)
  303. {
  304. m_nWordWrap = WrapToWindow;
  305. WrapChanged();
  306. }
  307. }
  308. void CResultView::OnUpdateEditPaste(CCmdUI* pCmdUI) 
  309. {
  310. bool bEnable = m_pGridCtrl && ::IsWindow(m_pGridCtrl->m_hWnd) &&
  311. m_pGridCtrl->IsWindowVisible();
  312. pCmdUI->Enable(!bEnable);
  313. }
  314. void CResultView::OnSetFocus(CWnd* pOldWnd) 
  315. {
  316. bool bFocus = m_pGridCtrl && ::IsWindow(m_pGridCtrl->m_hWnd) &&
  317. m_pGridCtrl->IsWindowVisible();
  318. if(bFocus)
  319. m_pGridCtrl->SetFocus();
  320. else
  321. CRichEditView::OnSetFocus(pOldWnd);
  322. }
  323. BOOL CResultView::ExportResults(const bool& bSaveSelection)
  324. {
  325. CWaitCursor wait;
  326. BOOL bRet = FALSE;
  327. CString strFilter = _T("Export Files (*.csv)|*.csv|All Files(*.*)|*.*|");
  328. CExportDlg dlg(false, _T("csv"), "Untitled", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
  329. strFilter, this);
  330. dlg.m_bSaveSelection = bSaveSelection;
  331. if(bSaveSelection)
  332. dlg.m_ofn.lpstrTitle = "Save Selection";
  333. else
  334. dlg.m_ofn.lpstrTitle = "Save Grid Results";
  335. if(dlg.DoModal() != IDOK)
  336. return FALSE;
  337. CString sBuff;
  338. CString sPathName = dlg.GetPathName();
  339. CStdioFile file;
  340. CFileException fileException;
  341. bRet = file.Open(sPathName, CFile::typeText | CFile::modeCreate |  
  342. CFile::modeWrite, &fileException);
  343. if(!bRet)
  344. {
  345. sBuff.Format("File Path Name: <%s> ", sPathName);
  346. sBuff += CHelpers::GetFileExceptionError(fileException.m_cause);
  347. AfxMessageBox(sBuff);
  348. }
  349. else
  350. {
  351. if(!ExportToFile(&file, dlg.m_strDelimiter, dlg.m_bColumnNames, bSaveSelection))
  352. {
  353. TRACE("Error exporting to file.n");
  354. file.Close();
  355. file.Remove(sPathName);
  356. bRet = FALSE;
  357. }
  358. }
  359. return bRet;
  360. }
  361. void CResultView::InvokeGridMenu()
  362. {
  363. CWaitCursor wait;
  364. CMenu menu;
  365. if(menu.LoadMenu(IDR_RCLICK))
  366. {
  367. CMenu* pMenu = menu.GetSubMenu(1);
  368. if(pMenu)
  369. {
  370. CPoint point;
  371. ::GetCursorPos(&point);
  372. pMenu->TrackPopupMenu(0, point.x, point.y, this);
  373. }
  374. }
  375. }
  376. BOOL CResultView::ExportToFile(CStdioFile* pFile, LPCTSTR lpszDelimiter,
  377. const BOOL& bColumnNames, const bool& bSaveSelection)
  378. {
  379. CWaitCursor wait;
  380. CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
  381. ASSERT(pFrame);
  382. BOOL bRet = TRUE;
  383. ASSERT(pFile && pFile->m_pStream);
  384. if(bSaveSelection)
  385. {
  386. pFrame->m_wndStatusBar.SetPaneText(0, "Saving selection...");
  387. long lCol = m_pGridCtrl->GetCol();
  388. long lRow = m_pGridCtrl->GetRow();
  389. long lColSel = m_pGridCtrl->GetColSel();
  390. long lRowSel = m_pGridCtrl->GetRowSel();
  391. for(long lR = lRow; lR <= lRowSel; lR++)
  392. {
  393. for(long lC = lCol; lC <= lColSel; lC++)
  394. {
  395. pFile->WriteString(m_pGridCtrl->GetTextMatrix(lR, lC));
  396. if(lC < lColSel)
  397. pFile->WriteString(lpszDelimiter);
  398. }
  399. if(lR < lRowSel)
  400. pFile->WriteString("n");
  401. }
  402. }
  403. else
  404. {
  405. pFrame->m_wndStatusBar.SetPaneText(0, "Saving grid results...");
  406. CMSFlexGrid* pGrid = NULL;
  407. long lCols = 0;
  408. long lRows = 0;
  409. for(POSITION pos = pFrame->m_GridList.GetHeadPosition(); pos != NULL;)
  410. {
  411. pGrid = (CMSFlexGrid*)pFrame->m_GridList.GetNext(pos);
  412. ASSERT(pGrid);
  413. lCols = pGrid->GetCols();
  414. lRows = pGrid->GetRows();
  415. for(long lR = (bColumnNames ? 0 : 1); lR < lRows; lR++)
  416. {
  417. for(long lC = 0; lC < lCols; lC++)
  418. {
  419. pFile->WriteString(pGrid->GetTextMatrix(lR, lC));
  420. if(lC < lCols-1)
  421. pFile->WriteString(lpszDelimiter);
  422. }
  423. if(lR < lRows)
  424. pFile->WriteString("n");
  425. }
  426. pFile->WriteString("n");
  427. }
  428. }
  429. pFile->Close();
  430. pFrame->m_wndStatusBar.SetPaneText(0, "");
  431. return bRet;
  432. }
  433. void CResultView::OnInitialUpdate() 
  434. {
  435. CRichEditView::OnInitialUpdate();
  436. SetMargins(CRect(720, 720, 720, 720));
  437. }
  438. BOOL CResultView::OnPreparePrinting(CPrintInfo* pInfo) 
  439. {
  440. return DoPreparePrinting(pInfo);
  441. }
  442. BOOL CResultView::PreTranslateMessage(MSG* pMsg) 
  443. {
  444. if(m_pGridCtrl && ::IsWindow(m_pGridCtrl->m_hWnd) &&
  445. m_pGridCtrl->IsWindowVisible())
  446. {
  447. if(pMsg->message == WM_KEYDOWN)
  448. {
  449. if(pMsg->wParam != VK_RETURN)
  450. return m_pGridCtrl->PreTranslateMessage(pMsg);
  451. else
  452. {
  453. ::MessageBeep(MB_ICONEXCLAMATION);
  454. return TRUE;
  455. }
  456. }
  457. }
  458. return CRichEditView::PreTranslateMessage(pMsg);
  459. }
  460. void CResultView::OnEditSelectAll() 
  461. {
  462. bool bGridSelectAll = m_pGridCtrl && ::IsWindow(m_pGridCtrl->m_hWnd) &&
  463. m_pGridCtrl->IsWindowVisible();
  464. if(!bGridSelectAll)
  465. GetRichEditCtrl().SetSel(0, GetRichEditCtrl().GetTextLength());
  466. else
  467. {
  468. m_pGridCtrl->SetRow(1);
  469. m_pGridCtrl->SetCol(0);
  470. m_pGridCtrl->SetRowSel(m_pGridCtrl->GetRows()-1);
  471. m_pGridCtrl->SetColSel(m_pGridCtrl->GetCols()-1);
  472. }
  473. }
  474. void CResultView::OnUpdateEditSelectAll(CCmdUI* pCmdUI) 
  475. {
  476. bool bEnable = (m_pGridCtrl && ::IsWindow(m_pGridCtrl->m_hWnd) &&
  477. m_pGridCtrl->IsWindowVisible() && m_pGridCtrl->GetRows() > 1) ||
  478. GetRichEditCtrl().GetTextLength();
  479. pCmdUI->Enable(bEnable);
  480. }