home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 18.ddi / MFC / SRC / VIEWCORE.CP_ / VIEWCORE.CP
Encoding:
Text File  |  1993-02-08  |  9.9 KB  |  377 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library. 
  2. // Copyright (C) 1992 Microsoft Corporation 
  3. // All rights reserved. 
  4. //  
  5. // This source code is only intended as a supplement to the 
  6. // Microsoft Foundation Classes Reference and Microsoft 
  7. // QuickHelp and/or WinHelp documentation provided with the library. 
  8. // See these sources for detailed information regarding the 
  9. // Microsoft Foundation Classes product. 
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef AFX_CORE2_SEG
  14. #pragma code_seg(AFX_CORE2_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char BASED_CODE THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. /////////////////////////////////////////////////////////////////////////////
  23. // CView
  24.  
  25. // IMPLEMENT_DYNAMIC for CView is in winfrm.cpp for .OBJ granularity reasons
  26.  
  27. BEGIN_MESSAGE_MAP(CView, CWnd)
  28.     //{{AFX_MSG_MAP(CView)
  29.     ON_WM_PAINT()
  30.     ON_WM_MOUSEACTIVATE()
  31.     ON_WM_CREATE()
  32.     ON_WM_DESTROY()
  33.  
  34.     // Standard commands for split pane
  35.     ON_COMMAND_EX(ID_WINDOW_SPLIT, OnSplitCmd)
  36.     ON_UPDATE_COMMAND_UI(ID_WINDOW_SPLIT, OnUpdateSplitCmd)
  37.  
  38.     // Standard commands for next pane
  39.     ON_UPDATE_COMMAND_UI(ID_NEXT_PANE, OnUpdateNextPaneMenu)
  40.     ON_COMMAND_EX(ID_NEXT_PANE, OnNextPaneCmd)
  41.     ON_UPDATE_COMMAND_UI(ID_PREV_PANE, OnUpdateNextPaneMenu)
  42.     ON_COMMAND_EX(ID_PREV_PANE, OnNextPaneCmd)
  43.     //}}AFX_MSG_MAP
  44.     // special command for Initial Update
  45.     ON_MESSAGE_VOID(WM_INITIALUPDATE, OnInitialUpdate)
  46. END_MESSAGE_MAP()
  47.  
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CView construction/destruction
  50.  
  51. CView::CView()
  52. {
  53.     m_pDocument = NULL;
  54. }
  55.  
  56. CView::~CView()
  57. {
  58.     if (m_pDocument != NULL)
  59.         m_pDocument->RemoveView(this);
  60. }
  61.  
  62. /////////////////////////////////////////////////////////////////////////////
  63. // CView second phase construction - bind to document
  64.  
  65. BOOL CView::PreCreateWindow(CREATESTRUCT& cs)
  66. {
  67.     ASSERT(cs.style & WS_CHILD);
  68.     if (cs.lpszClass == NULL)
  69.         cs.lpszClass = _afxWndFrameOrView;  // COLOR_WINDOW background
  70.     return TRUE;
  71. }
  72.  
  73. int CView::OnCreate(LPCREATESTRUCT lpcs)
  74. {
  75.     if (CWnd::OnCreate(lpcs) == -1)
  76.         return -1;
  77.  
  78.     // if ok, wire in the current document
  79.     ASSERT(m_pDocument == NULL);
  80.     CCreateContext* pContext;
  81.     pContext = (CCreateContext*)_AfxGetPtrFromFarPtr(lpcs->lpCreateParams);
  82.  
  83.     // A pane MUST be created in a given context !
  84.     ASSERT(pContext != NULL);
  85.     if (pContext->m_pCurrentDoc != NULL)
  86.     {
  87.         pContext->m_pCurrentDoc->AddView(this);
  88.         ASSERT(m_pDocument != NULL);
  89.     }
  90.     else
  91.     {
  92.         TRACE0("Warning: Creating a pane with no CDocument\n");
  93.     }
  94.  
  95.     return 0;   // ok
  96. }
  97.  
  98. void CView::OnDestroy()
  99. {
  100.     CFrameWnd* pFrame = GetParentFrame();
  101.     if (pFrame != NULL && pFrame->GetActiveView() == this)
  102.         pFrame->SetActiveView(NULL);    // deactivate during death
  103.     CWnd::OnDestroy();
  104. }
  105.  
  106. // self destruction
  107. void CView::PostNcDestroy()
  108. {
  109.     // default for views is to allocate them on the heap
  110.     //  the default post-cleanup is to 'delete this'.
  111.     // never explicitly call 'delete' on a view
  112.     delete this;
  113. }
  114.  
  115. /////////////////////////////////////////////////////////////////////////////
  116. // Command routing
  117.  
  118. BOOL CView::OnCmdMsg(UINT nID, int nCode, void* pExtra,
  119.     AFX_CMDHANDLERINFO* pHandlerInfo)
  120. {
  121.     // first pump through pane
  122.     if (CWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
  123.         return TRUE;
  124.  
  125.     // then pump through document
  126.     BOOL bHandled = FALSE;
  127.     if (m_pDocument != NULL)
  128.     {
  129.         // special state for saving view of 
  130.         CView*  pOldRoutingView = CCmdTarget::pRoutingView;
  131.         CCmdTarget::pRoutingView = this;
  132.         bHandled = m_pDocument->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
  133.         CCmdTarget::pRoutingView = pOldRoutingView;
  134.     }
  135.  
  136.     return bHandled;
  137. }
  138.  
  139. /////////////////////////////////////////////////////////////////////////////
  140. // CView drawing support
  141.  
  142. void CView::OnPaint()
  143. {
  144.     // standard paint routine
  145.     CPaintDC dc(this);
  146.     OnPrepareDC(&dc);
  147.     OnDraw(&dc);
  148. }
  149.  
  150. void CView::OnInitialUpdate()
  151. {
  152.     OnUpdate(NULL, 0, NULL);        // initial update
  153. }
  154.  
  155. void CView::OnUpdate(CView* pSender, LPARAM /*lHint*/, CObject* /*pHint*/)
  156. {
  157.     ASSERT(pSender != this);
  158.  
  159.     // invalidate the entire pane, erase background too
  160.     Invalidate(TRUE);
  161. }
  162.  
  163. void CView::OnPrint(CDC* pDC, CPrintInfo*)
  164. {
  165.     ASSERT_VALID(pDC);
  166.  
  167.     // Override and set printing variables based on page number
  168.     OnDraw(pDC);                    // Call Draw
  169. }
  170.  
  171. /////////////////////////////////////////////////////////////////////////////
  172. // CView selection support
  173.  
  174. BOOL CView::IsSelected(const CObject* pDocItem) const
  175. {
  176.     ASSERT_VALID(pDocItem);
  177.  
  178.     return FALSE;       // not selected as far as I know
  179. }
  180.  
  181. void CView::OnActivateView(BOOL bActivate, CView* pActivateView, CView*)
  182. {
  183.     if (bActivate)
  184.     {
  185.         ASSERT(pActivateView == this);
  186.         SetFocus(); // take the focus if the view/pane is active
  187.     }
  188. }
  189.  
  190. int CView::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message)
  191. {
  192.     int maRet = CWnd::OnMouseActivate(pDesktopWnd, nHitTest, message);
  193.     if (maRet == MA_NOACTIVATE || maRet == MA_NOACTIVATEANDEAT)
  194.         return maRet;   // frame does not want to activate
  195.  
  196.     // eat it if this will cause activation
  197.     CFrameWnd* pParentFrame = GetParentFrame();
  198.     ASSERT(pParentFrame == pDesktopWnd || pDesktopWnd->IsChild(pParentFrame));
  199.     pParentFrame->SetActiveView(this); // activate or re-activate us
  200.  
  201.     return maRet;
  202. }
  203.  
  204.  
  205.  
  206. /////////////////////////////////////////////////////////////////////////////
  207. // CView splitting commands
  208.  
  209. IMPLEMENT_DYNAMIC(CSplitterWnd, CWnd)   // for swap tuning
  210.  
  211. static CSplitterWnd* GetParentSplitter(const CWnd* pWnd, BOOL bAnyState)
  212. {
  213.     CSplitterWnd* pSplitter = (CSplitterWnd*)pWnd->GetParent();
  214.     if (!pSplitter->IsKindOf(RUNTIME_CLASS(CSplitterWnd)))
  215.         return NULL;        // not a splitter
  216.     if (!bAnyState)
  217.     {
  218.         // ignore splitters in minimized (iconic) windows
  219.         while ((pWnd = pWnd->GetParent()) != NULL)
  220.             if (pWnd->IsIconic())
  221.                 return NULL;
  222.     }
  223.     return pSplitter;
  224. }
  225.  
  226. CScrollBar* CView::GetScrollBarCtrl(int nBar) const
  227. {
  228.     ASSERT(nBar == SB_HORZ || nBar == SB_VERT);
  229.     if (GetStyle() & ((nBar == SB_HORZ) ? WS_HSCROLL : WS_VSCROLL))
  230.     {
  231.         // it has a regular windows style scrollbar (no control)
  232.         return NULL;
  233.     }
  234.  
  235.     CWnd* pParent = GetParentSplitter(this, TRUE);
  236.     if (pParent == NULL)
  237.         return NULL;            // no splitter
  238.  
  239.     UINT nID = _AfxGetDlgCtrlID(m_hWnd);
  240.     if (nID < AFX_IDW_PANE_FIRST || nID > AFX_IDW_PANE_LAST)
  241.         return NULL;            // not a standard pane ID
  242.  
  243.     // appropriate PANE id - look for sibling (splitter, or just frame)
  244.     UINT nIDScroll;
  245.     if (nBar == SB_HORZ)
  246.         nIDScroll = AFX_IDW_HSCROLL_FIRST + (nID - AFX_IDW_PANE_FIRST) % 16;
  247.     else
  248.         nIDScroll = AFX_IDW_VSCROLL_FIRST + (nID - AFX_IDW_PANE_FIRST) / 16;
  249.  
  250.     // return shared scroll bars that are immediate children of splitter
  251.     return (CScrollBar*)pParent->GetDlgItem(nIDScroll);
  252. }
  253.  
  254.  
  255. void CView::OnUpdateSplitCmd(CCmdUI* pCmdUI)
  256. {
  257.     CSplitterWnd* pSplitter = GetParentSplitter(this, FALSE);
  258.     pCmdUI->Enable(pSplitter != NULL);
  259. }
  260.  
  261. BOOL CView::OnSplitCmd(UINT)
  262. {
  263.     CSplitterWnd* pSplitter = GetParentSplitter(this, FALSE);
  264.     if (pSplitter == NULL)
  265.         return FALSE;
  266.  
  267.     pSplitter->DoKeyboardSplit();
  268.     return TRUE;    // attempted at least
  269. }
  270.  
  271. void CView::OnUpdateNextPaneMenu(CCmdUI* pCmdUI)
  272. {
  273.     ASSERT(pCmdUI->m_nID == ID_NEXT_PANE ||
  274.         pCmdUI->m_nID == ID_PREV_PANE);
  275.     CSplitterWnd* pSplitter = GetParentSplitter(this, FALSE);
  276.     pCmdUI->Enable(pSplitter != NULL &&
  277.         pSplitter->CanActivateNext(pCmdUI->m_nID == ID_PREV_PANE));
  278. }
  279.  
  280. BOOL CView::OnNextPaneCmd(UINT nID)
  281. {
  282.     CSplitterWnd* pSplitter = GetParentSplitter(this, FALSE);
  283.     if (pSplitter == NULL)
  284.         return FALSE;
  285.  
  286.     ASSERT(nID == ID_NEXT_PANE || nID == ID_PREV_PANE);
  287.     pSplitter->ActivateNext(nID == ID_PREV_PANE);
  288.     return TRUE;
  289. }
  290.  
  291. /////////////////////////////////////////////////////////////////////////////
  292. // Printing support virtual functions (others in viewpr.cpp)
  293.  
  294. void CView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
  295. {
  296.     ASSERT_VALID(pDC);
  297.  
  298.     // Default to one page printing if doc length not known
  299.     if (pInfo != NULL)
  300.         pInfo->m_bContinuePrinting = 
  301.             (pInfo->GetMaxPage() != 0xffff || (pInfo->m_nCurPage == 1));
  302. }
  303.  
  304.  
  305. BOOL CView::OnPreparePrinting(CPrintInfo*)
  306. {
  307.     // Do print DC initialization here
  308.     // override and call DoPreparePrinting (in viewprnt.cpp)
  309.  
  310.     return TRUE;
  311. }
  312.  
  313. void CView::OnBeginPrinting(CDC* pDC, CPrintInfo*)
  314. {
  315.     ASSERT_VALID(pDC);
  316.     // Do printing initialization here
  317. }
  318.  
  319. void CView::OnEndPrinting(CDC* pDC, CPrintInfo*)
  320. {
  321.     ASSERT_VALID(pDC);
  322.     // Do printing cleanup here
  323. }
  324.  
  325.  
  326. // OnEndPrintPreview is here for swap tuning reasons
  327. //  (see viewprev.cpp for complete preview mode implementation)
  328. void CView::OnEndPrintPreview(CDC* pDC, CPrintInfo* pInfo, 
  329.             POINT, CPreviewView* pView)
  330. {
  331.     ASSERT_VALID(pDC);
  332.     ASSERT_VALID(pView);
  333.  
  334.     if (pView->m_pPrintView != NULL)
  335.         pView->m_pPrintView->OnEndPrinting(pDC, pInfo);
  336.  
  337.     CFrameWnd* pParent = (CFrameWnd*) AfxGetApp()->m_pMainWnd;
  338.     ASSERT(pParent->IsKindOf(RUNTIME_CLASS(CFrameWnd)));
  339.  
  340.     // restore the old main window
  341.     pParent->OnSetPreviewMode(FALSE, pView->m_pPreviewState);
  342.  
  343.     // Force active view back to old one
  344.     pParent->SetActiveView(pView->m_pPreviewState->pViewActiveOld);
  345.     if (pParent != GetParentFrame())
  346.         OnActivateView(TRUE, this, this);   // re-activate view in real frame
  347.     pView->DestroyWindow();     // destroy preview view
  348.             // C++ object will be deleted in PostNcDestroy
  349.  
  350.     // restore main frame layout and idle message
  351.     pParent->RecalcLayout();
  352.     pParent->SendMessage(WM_SETMESSAGESTRING, (WPARAM)AFX_IDS_IDLEMESSAGE, 0L);
  353. }
  354.  
  355.  
  356.  
  357. /////////////////////////////////////////////////////////////////////////////
  358. // CView diagnostics
  359.  
  360. #ifdef _DEBUG
  361. void CView::Dump(CDumpContext& dc) const
  362. {
  363.     CWnd::Dump(dc);
  364.     if (m_pDocument != NULL)
  365.         AFX_DUMP1(dc, "\nwith document ", m_pDocument);
  366.     else
  367.         AFX_DUMP0(dc, "\nwith no document");
  368. }
  369.  
  370. void CView::AssertValid() const
  371. {
  372.     CWnd::AssertValid();
  373. }
  374. #endif //_DEBUG
  375.  
  376. /////////////////////////////////////////////////////////////////////////////
  377.