ResultView.cpp
资源名称:ISQL_src.zip [点击查看]
上传用户:jsxglz
上传日期:2007-01-03
资源大小:117k
文件大小:14k
源码类别:
SQL Server
开发平台:
Visual C++
- // ResultView.cpp : implementation file
- //
- #include "stdafx.h"
- #include "InteractiveSQL.h"
- #include "ResultView.h"
- #include "InteractiveSQLDoc.h"
- #include "resource.h"
- #include "MainFrm.h"
- #include "SQLView.h"
- #include "ExportDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CResultView
- IMPLEMENT_DYNCREATE(CResultView, CRichEditView)
- CResultView::CResultView()
- {
- m_nWordWrap = WrapNone;
- // Edit font
- LOGFONT logFont;
- memset(&logFont, 0, sizeof(logFont));
- logFont.lfHeight = -15;
- logFont.lfWeight = 400;
- strcpy(logFont.lfFaceName, "Courier");
- if(!m_font.CreateFontIndirect(&logFont))
- TRACE("Could Not create font.n");
- m_pGridCtrl = NULL;
- }
- CResultView::~CResultView()
- {
- if(m_font.m_hObject)
- {
- m_font.Detach();
- m_font.m_hObject = NULL;
- }
- }
- BEGIN_MESSAGE_MAP(CResultView, CRichEditView)
- //{{AFX_MSG_MAP(CResultView)
- ON_WM_SIZE()
- ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
- ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
- ON_WM_CREATE()
- ON_UPDATE_COMMAND_UI(ID_FILE_NEW, OnUpdateFileNew)
- ON_UPDATE_COMMAND_UI(ID_FILE_OPEN, OnUpdateFileOpen)
- ON_UPDATE_COMMAND_UI(ID_FILE_SAVE, OnUpdateFileSave)
- ON_UPDATE_COMMAND_UI(ID_FILE_SAVE_AS, OnUpdateFileSaveAs)
- ON_COMMAND(ID_FILE_PRINT, OnFilePrint)
- ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
- ON_WM_MOUSEACTIVATE()
- ON_UPDATE_COMMAND_UI(ID_FILE_PRINT, OnUpdateFilePrint)
- ON_COMMAND(ID_VIEW_WRAP_WORD, OnViewWrapWord)
- ON_UPDATE_COMMAND_UI(ID_VIEW_WRAP_WORD, OnUpdateViewWrapWord)
- ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
- ON_WM_SETFOCUS()
- ON_COMMAND(ID_GRID_SAVE_SELECTION, OnGridSaveSelection)
- ON_COMMAND(ID_EDIT_SELECT_ALL, OnEditSelectAll)
- ON_UPDATE_COMMAND_UI(ID_EDIT_SELECT_ALL, OnUpdateEditSelectAll)
- //}}AFX_MSG_MAP
- ON_COMMAND(ID_FILE_PRINT, CRichEditView::OnFilePrint)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CResultView diagnostics
- #ifdef _DEBUG
- void CResultView::AssertValid() const
- {
- CRichEditView::AssertValid();
- }
- void CResultView::Dump(CDumpContext& dc) const
- {
- CRichEditView::Dump(dc);
- }
- CInteractiveSQLDoc* CResultView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CInteractiveSQLDoc)));
- return (CInteractiveSQLDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CResultView message handlers
- void CResultView::OnSize(UINT nType, int cx, int cy)
- {
- CRichEditView::OnSize(nType, cx, cy);
- if(m_pGridCtrl && m_pGridCtrl->m_hWnd != NULL)
- m_pGridCtrl->MoveWindow(0, 0, cx, cy);
- }
- void CResultView::OnEditCopy()
- {
- CWaitCursor wait;
- bool bCopyGrid = m_pGridCtrl && ::IsWindow(m_pGridCtrl->m_hWnd) &&
- m_pGridCtrl->IsWindowVisible();
- if(bCopyGrid)
- CopyGridData();
- else
- GetRichEditCtrl().Copy();
- }
- void CResultView::CopyGridData()
- {
- CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
- ASSERT(pFrame);
- pFrame->m_wndStatusBar.SetPaneText(0, "Copying...");
- long lCol = m_pGridCtrl->GetCol();
- long lRow = m_pGridCtrl->GetRow();
- long lColSel = m_pGridCtrl->GetColSel();
- long lRowSel = m_pGridCtrl->GetRowSel();
- CSharedFile memFile(GMEM_MOVEABLE | GMEM_DDESHARE | GMEM_ZEROINIT);
- CString sBuff;
- USES_CONVERSION;
- for(long lR = lRow; lR <= lRowSel; lR++)
- {
- for(long lC = lCol; lC <= lColSel; lC++)
- {
- sBuff = m_pGridCtrl->GetTextMatrix(lR, lC);
- memFile.Write(T2A(const_cast<LPTSTR>((LPCTSTR)sBuff)), sBuff.GetLength());
- if(lC < lColSel)
- memFile.Write("t", strlen("t"));
- }
- if(lR < lRowSel)
- memFile.Write("n", strlen("n"));
- }
- COleDataSource* pDataSource = new COleDataSource;
- ASSERT(pDataSource);
- pDataSource->CacheGlobalData(CF_TEXT, memFile.Detach());
- pDataSource->SetClipboard();
- pFrame->m_wndStatusBar.SetPaneText(0, "");
- }
- void CResultView::OnUpdateEditCopy(CCmdUI* pCmdUI)
- {
- bool bEnable = (m_pGridCtrl && ::IsWindow(m_pGridCtrl->m_hWnd) &&
- m_pGridCtrl->IsWindowVisible()) || GetRichEditCtrl().GetTextLength();
- pCmdUI->Enable(bEnable);
- }
- int CResultView::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if(CRichEditView::OnCreate(lpCreateStruct) == -1)
- return -1;
- if(m_font.m_hObject)
- GetRichEditCtrl().SetFont(&m_font);
- return 0;
- }
- void CResultView::OnUpdateFileNew(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(FALSE);
- }
- void CResultView::OnUpdateFileOpen(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(FALSE);
- }
- void CResultView::OnUpdateFileSave(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(FALSE);
- }
- void CResultView::OnUpdateFileSaveAs(CCmdUI* pCmdUI)
- {
- bool bEnable = (m_pGridCtrl && ::IsWindow(m_pGridCtrl->m_hWnd) &&
- m_pGridCtrl->IsWindowVisible());
- pCmdUI->Enable(bEnable || GetRichEditCtrl().GetTextLength());
- }
- void CResultView::OnFilePrint()
- {
- CWaitCursor wait;
- CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
- ASSERT(pFrame);
- CDocument* pDoc = pFrame->GetActiveDocument();
- ASSERT(pDoc);
- CString sTitle = pDoc->GetTitle();
- if(sTitle.IsEmpty())
- sTitle = "Untitled.";
- if(m_strObjName.IsEmpty())
- m_strObjName = "Untitled";
- pFrame->LockWindowUpdate();
- pDoc->SetTitle(m_strObjName);
- CRichEditView::OnFilePrint();
- pDoc->SetTitle(sTitle);
- pFrame->UnlockWindowUpdate();
- }
- void CResultView::OnGridSaveSelection()
- {
- if(!ExportResults(true))
- TRACE("Error exporting results.n");
- }
- void CResultView::OnFileSaveAs()
- {
- CWaitCursor wait;
- bool bExport = m_pGridCtrl && ::IsWindow(m_pGridCtrl->m_hWnd) &&
- m_pGridCtrl->IsWindowVisible();
- if(bExport)
- {
- if(!ExportResults())
- TRACE("Error exporting results.n");
- }
- else
- {
- CStringEx sBuff;
- GetRichEditCtrl().GetWindowText(sBuff);
- CString strFilter, sDefaultExt;
- if(!m_strObjName.IsEmpty() && sBuff.FindNoCase(m_strObjName) != -1)
- {
- sDefaultExt = _T("sql");
- strFilter = _T("Procedure Files (*.sql)|*.sql|All Files (*.*)|*.*|");
- }
- else
- {
- sDefaultExt = _T("txt");
- strFilter = _T("Text Files (*.txt)|*.txt|All Files(*.*)|*.*|");
- m_strObjName = _T("Untitled"); // Emptied in <CMainFrame::OnExceptionClear>
- }
- CFileDialog dlg(false, sDefaultExt, m_strObjName, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
- strFilter, this);
- if(dlg.DoModal() == IDOK)
- {
- CWaitCursor wait;
- CString sBuff;
- CString sPathName = dlg.GetPathName();
- CStdioFile file;
- CFileException fileException;
- if(!file.Open(sPathName, CFile::typeText | CFile::modeCreate |
- CFile::modeWrite, &fileException))
- {
- sBuff.Format("File Path Name: <%s> ", sPathName);
- sBuff += CHelpers::GetFileExceptionError(fileException.m_cause);
- AfxMessageBox(sBuff);
- }
- else
- {
- GetRichEditCtrl().GetWindowText(sBuff);
- file.WriteString(sBuff);
- }
- }
- }
- }
- int CResultView::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message)
- {
- const MSG* pMsg = GetCurrentMessage();
- GetParent()->SendMessage(WM_MOUSEACTIVATE, pMsg->wParam, pMsg->lParam);
- return CRichEditView::OnMouseActivate(pDesktopWnd, nHitTest, message);
- }
- HMENU CResultView::GetContextMenu(WORD /*wSelType*/, LPOLEOBJECT /*lpOleObj*/,
- CHARRANGE* /*lpChrg*/)
- {
- bool bOkToInvokeGridMenu = m_pGridCtrl && ::IsWindow(m_pGridCtrl->m_hWnd) &&
- m_pGridCtrl->IsWindowVisible();
- if(bOkToInvokeGridMenu)
- InvokeGridMenu();
- else
- {
- CMenu menu;
- if(menu.LoadMenu(IDR_RCLICK))
- {
- CMenu* pMenu = menu.GetSubMenu(2);
- ASSERT(pMenu);
- int nTextLength = GetRichEditCtrl().GetTextLength();
- if(!nTextLength)
- {
- pMenu->EnableMenuItem(ID_EDIT_SELECT_ALL, MF_BYCOMMAND | MF_GRAYED);
- pMenu->EnableMenuItem(ID_FILE_SAVE_AS, MF_BYCOMMAND | MF_GRAYED);
- pMenu->EnableMenuItem(ID_FILE_PRINT, MF_BYCOMMAND | MF_GRAYED);
- pMenu->EnableMenuItem(ID_EDIT_CLEAR, MF_BYCOMMAND | MF_GRAYED);
- }
- if(GetRichEditCtrl().GetSelText().IsEmpty())
- {
- pMenu->EnableMenuItem(ID_EDIT_CUT, MF_BYCOMMAND | MF_GRAYED);
- pMenu->EnableMenuItem(ID_EDIT_COPY, MF_BYCOMMAND | MF_GRAYED);
- }
- if(!GetRichEditCtrl().CanPaste())
- pMenu->EnableMenuItem(ID_EDIT_PASTE, MF_BYCOMMAND | MF_GRAYED);
- if(!GetRichEditCtrl().CanUndo())
- pMenu->EnableMenuItem(ID_EDIT_UNDO, MF_BYCOMMAND | MF_GRAYED);
- CPoint point;
- ::GetCursorPos(&point);
- pMenu->TrackPopupMenu(0, point.x, point.y, this);
- }
- }
- return NULL;
- }
- void CResultView::OnUpdateFilePrint(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(GetRichEditCtrl().GetTextLength());
- }
- void CResultView::OnViewWrapWord()
- {
- m_nWordWrap = (m_nWordWrap == WrapNone) ? WrapToWindow : WrapNone;
- WrapChanged();
- }
- void CResultView::OnUpdateViewWrapWord(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(GetRichEditCtrl().GetTextLength());
- pCmdUI->SetCheck(m_nWordWrap == WrapToWindow);
- }
- void CResultView::SetWrapNone()
- {
- if(m_nWordWrap == WrapToWindow)
- {
- m_nWordWrap = WrapNone;
- WrapChanged();
- }
- }
- void CResultView::SetWrapToWindow()
- {
- if(m_nWordWrap == WrapNone)
- {
- m_nWordWrap = WrapToWindow;
- WrapChanged();
- }
- }
- void CResultView::OnUpdateEditPaste(CCmdUI* pCmdUI)
- {
- bool bEnable = m_pGridCtrl && ::IsWindow(m_pGridCtrl->m_hWnd) &&
- m_pGridCtrl->IsWindowVisible();
- pCmdUI->Enable(!bEnable);
- }
- void CResultView::OnSetFocus(CWnd* pOldWnd)
- {
- bool bFocus = m_pGridCtrl && ::IsWindow(m_pGridCtrl->m_hWnd) &&
- m_pGridCtrl->IsWindowVisible();
- if(bFocus)
- m_pGridCtrl->SetFocus();
- else
- CRichEditView::OnSetFocus(pOldWnd);
- }
- BOOL CResultView::ExportResults(const bool& bSaveSelection)
- {
- CWaitCursor wait;
- BOOL bRet = FALSE;
- CString strFilter = _T("Export Files (*.csv)|*.csv|All Files(*.*)|*.*|");
- CExportDlg dlg(false, _T("csv"), "Untitled", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
- strFilter, this);
- dlg.m_bSaveSelection = bSaveSelection;
- if(bSaveSelection)
- dlg.m_ofn.lpstrTitle = "Save Selection";
- else
- dlg.m_ofn.lpstrTitle = "Save Grid Results";
- if(dlg.DoModal() != IDOK)
- return FALSE;
- CString sBuff;
- CString sPathName = dlg.GetPathName();
- CStdioFile file;
- CFileException fileException;
- bRet = file.Open(sPathName, CFile::typeText | CFile::modeCreate |
- CFile::modeWrite, &fileException);
- if(!bRet)
- {
- sBuff.Format("File Path Name: <%s> ", sPathName);
- sBuff += CHelpers::GetFileExceptionError(fileException.m_cause);
- AfxMessageBox(sBuff);
- }
- else
- {
- if(!ExportToFile(&file, dlg.m_strDelimiter, dlg.m_bColumnNames, bSaveSelection))
- {
- TRACE("Error exporting to file.n");
- file.Close();
- file.Remove(sPathName);
- bRet = FALSE;
- }
- }
- return bRet;
- }
- void CResultView::InvokeGridMenu()
- {
- CWaitCursor wait;
- CMenu menu;
- if(menu.LoadMenu(IDR_RCLICK))
- {
- CMenu* pMenu = menu.GetSubMenu(1);
- if(pMenu)
- {
- CPoint point;
- ::GetCursorPos(&point);
- pMenu->TrackPopupMenu(0, point.x, point.y, this);
- }
- }
- }
- BOOL CResultView::ExportToFile(CStdioFile* pFile, LPCTSTR lpszDelimiter,
- const BOOL& bColumnNames, const bool& bSaveSelection)
- {
- CWaitCursor wait;
- CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
- ASSERT(pFrame);
- BOOL bRet = TRUE;
- ASSERT(pFile && pFile->m_pStream);
- if(bSaveSelection)
- {
- pFrame->m_wndStatusBar.SetPaneText(0, "Saving selection...");
- long lCol = m_pGridCtrl->GetCol();
- long lRow = m_pGridCtrl->GetRow();
- long lColSel = m_pGridCtrl->GetColSel();
- long lRowSel = m_pGridCtrl->GetRowSel();
- for(long lR = lRow; lR <= lRowSel; lR++)
- {
- for(long lC = lCol; lC <= lColSel; lC++)
- {
- pFile->WriteString(m_pGridCtrl->GetTextMatrix(lR, lC));
- if(lC < lColSel)
- pFile->WriteString(lpszDelimiter);
- }
- if(lR < lRowSel)
- pFile->WriteString("n");
- }
- }
- else
- {
- pFrame->m_wndStatusBar.SetPaneText(0, "Saving grid results...");
- CMSFlexGrid* pGrid = NULL;
- long lCols = 0;
- long lRows = 0;
- for(POSITION pos = pFrame->m_GridList.GetHeadPosition(); pos != NULL;)
- {
- pGrid = (CMSFlexGrid*)pFrame->m_GridList.GetNext(pos);
- ASSERT(pGrid);
- lCols = pGrid->GetCols();
- lRows = pGrid->GetRows();
- for(long lR = (bColumnNames ? 0 : 1); lR < lRows; lR++)
- {
- for(long lC = 0; lC < lCols; lC++)
- {
- pFile->WriteString(pGrid->GetTextMatrix(lR, lC));
- if(lC < lCols-1)
- pFile->WriteString(lpszDelimiter);
- }
- if(lR < lRows)
- pFile->WriteString("n");
- }
- pFile->WriteString("n");
- }
- }
- pFile->Close();
- pFrame->m_wndStatusBar.SetPaneText(0, "");
- return bRet;
- }
- void CResultView::OnInitialUpdate()
- {
- CRichEditView::OnInitialUpdate();
- SetMargins(CRect(720, 720, 720, 720));
- }
- BOOL CResultView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- return DoPreparePrinting(pInfo);
- }
- BOOL CResultView::PreTranslateMessage(MSG* pMsg)
- {
- if(m_pGridCtrl && ::IsWindow(m_pGridCtrl->m_hWnd) &&
- m_pGridCtrl->IsWindowVisible())
- {
- if(pMsg->message == WM_KEYDOWN)
- {
- if(pMsg->wParam != VK_RETURN)
- return m_pGridCtrl->PreTranslateMessage(pMsg);
- else
- {
- ::MessageBeep(MB_ICONEXCLAMATION);
- return TRUE;
- }
- }
- }
- return CRichEditView::PreTranslateMessage(pMsg);
- }
- void CResultView::OnEditSelectAll()
- {
- bool bGridSelectAll = m_pGridCtrl && ::IsWindow(m_pGridCtrl->m_hWnd) &&
- m_pGridCtrl->IsWindowVisible();
- if(!bGridSelectAll)
- GetRichEditCtrl().SetSel(0, GetRichEditCtrl().GetTextLength());
- else
- {
- m_pGridCtrl->SetRow(1);
- m_pGridCtrl->SetCol(0);
- m_pGridCtrl->SetRowSel(m_pGridCtrl->GetRows()-1);
- m_pGridCtrl->SetColSel(m_pGridCtrl->GetCols()-1);
- }
- }
- void CResultView::OnUpdateEditSelectAll(CCmdUI* pCmdUI)
- {
- bool bEnable = (m_pGridCtrl && ::IsWindow(m_pGridCtrl->m_hWnd) &&
- m_pGridCtrl->IsWindowVisible() && m_pGridCtrl->GetRows() > 1) ||
- GetRichEditCtrl().GetTextLength();
- pCmdUI->Enable(bEnable);
- }