home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / internet / stockticker / containermfc / containermfcview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  7.8 KB  |  298 lines

  1. // containerMFCView.cpp : implementation of the CContainerMFCView class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13.  
  14. #include "stdafx.h"
  15. #include "containerMFC.h"
  16.  
  17. #include "containerMFCDoc.h"
  18. #include "CntrItem.h"
  19. #include "containerMFCView.h"
  20.  
  21. #ifdef _DEBUG
  22. #define new DEBUG_NEW
  23. #undef THIS_FILE
  24. static char THIS_FILE[] = __FILE__;
  25. #endif
  26.  
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CContainerMFCView
  29.  
  30. IMPLEMENT_DYNCREATE(CContainerMFCView, CView)
  31.  
  32. BEGIN_MESSAGE_MAP(CContainerMFCView, CView)
  33.     //{{AFX_MSG_MAP(CContainerMFCView)
  34.     ON_WM_DESTROY()
  35.     ON_WM_SETFOCUS()
  36.     ON_WM_SIZE()
  37.     ON_COMMAND(ID_OLE_INSERT_NEW, OnInsertObject)
  38.     ON_COMMAND(ID_CANCEL_EDIT_CNTR, OnCancelEditCntr)
  39.     ON_WM_CREATE()
  40.     //}}AFX_MSG_MAP
  41. END_MESSAGE_MAP()
  42.  
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CContainerMFCView construction/destruction
  45.  
  46. CContainerMFCView::CContainerMFCView()
  47. {
  48.     m_pSelection = NULL;
  49.     // TODO: add construction code here
  50. }
  51.  
  52. CContainerMFCView::~CContainerMFCView()
  53. {
  54. }
  55.  
  56. BOOL CContainerMFCView::PreCreateWindow(CREATESTRUCT& cs)
  57. {
  58.     return CView::PreCreateWindow(cs);
  59. }
  60.  
  61. /////////////////////////////////////////////////////////////////////////////
  62. // CContainerMFCView drawing
  63.  
  64. void CContainerMFCView::OnDraw(CDC* pDC)
  65. {
  66.     CContainerMFCDoc* pDoc = GetDocument();
  67.     ASSERT_VALID(pDoc);
  68.  
  69.     // TODO: add draw code for native data here
  70.     // TODO: also draw all OLE items in the document
  71.  
  72.     // Draw the selection at an arbitrary position.  This code should be
  73.     //  removed once your real drawing code is implemented.  This position
  74.     //  corresponds exactly to the rectangle returned by CContainerMFCCntrItem,
  75.     //  to give the effect of in-place editing.
  76.  
  77.     // TODO: remove this code when final draw code is complete.
  78.  
  79.     if (m_pSelection == NULL)
  80.     {
  81.         POSITION pos = pDoc->GetStartPosition();
  82.         m_pSelection = (CContainerMFCCntrItem*)pDoc->GetNextClientItem(pos);
  83.     }
  84.     if (m_pSelection != NULL)
  85.     {
  86.         CRect   r;
  87.         GetClientRect(&r);
  88.         m_pSelection->Draw(pDC, r);
  89.     }
  90. }
  91.  
  92. void CContainerMFCView::OnInitialUpdate()
  93. {
  94.     CView::OnInitialUpdate();
  95.  
  96.     // Add "About..." menu item to system menu.
  97.     // also add "Stay On Top" menu item to system menu.
  98.  
  99.     // ID_APP_ABOUT must be in the system command range.
  100.     ASSERT((ID_APP_ABOUT & 0xFFF0) == ID_APP_ABOUT);
  101.     ASSERT(ID_APP_ABOUT < 0xF000);
  102.  
  103.     CMenu* pSysMenu = AfxGetMainWnd()->GetSystemMenu(FALSE);
  104.     ASSERT(pSysMenu);
  105.     if (pSysMenu)
  106.     {
  107.         CString str;
  108.  
  109.         pSysMenu->AppendMenu(MF_SEPARATOR);
  110.         str.LoadString(IDS_STAY_ON_TOP);
  111.         if (str.IsEmpty())
  112.             str = _T("&Stay On Top");
  113.         pSysMenu->AppendMenu(MF_STRING, ID_STAY_ON_TOP, str);
  114.  
  115.         str.LoadString(IDS_ABOUT_APP);
  116.         if (str.IsEmpty())
  117.             str = _T("&About Stock Ticker..");
  118.         pSysMenu->AppendMenu(MF_STRING, ID_APP_ABOUT, str);
  119.     }
  120.     // request document to create control.
  121.     //
  122.     CRect   r;
  123.     GetClientRect(&r);
  124.     GetDocument()->CreateControl(this, r);
  125. }
  126.  
  127. void CContainerMFCView::OnDestroy()
  128. {
  129.     // Deactivate the item on destruction; this is important
  130.     // when a splitter view is being used.
  131.     GetDocument()->Save();      // save state.
  132.     COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
  133.     if (pActiveItem != NULL && pActiveItem->GetActiveView() == this)
  134.     {
  135.       pActiveItem->Deactivate();
  136.       ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
  137.     }
  138.     CView::OnDestroy();
  139. }
  140.  
  141.  
  142. /////////////////////////////////////////////////////////////////////////////
  143. // OLE Client support and commands
  144.  
  145. BOOL CContainerMFCView::IsSelected(const CObject* pDocItem) const
  146. {
  147.     // The implementation below is adequate if your selection consists of
  148.     //  only CContainerMFCCntrItem objects.  To handle different selection
  149.     //  mechanisms, the implementation here should be replaced.
  150.  
  151.     // TODO: implement this function that tests for a selected OLE client item
  152.  
  153.     return pDocItem == m_pSelection;
  154. }
  155.  
  156. void CContainerMFCView::OnInsertObject()
  157. {
  158.     // Invoke the standard Insert Object dialog box to obtain information
  159.     //  for new CContainerMFCCntrItem object.
  160.     COleInsertDialog dlg;
  161.     if (dlg.DoModal() != IDOK)
  162.         return;
  163.  
  164.     BeginWaitCursor();
  165.  
  166.     CContainerMFCCntrItem* pItem = NULL;
  167.     TRY
  168.     {
  169.         // Create new item connected to this document.
  170.         CContainerMFCDoc* pDoc = GetDocument();
  171.         ASSERT_VALID(pDoc);
  172.         pItem = new CContainerMFCCntrItem(pDoc);
  173.         ASSERT_VALID(pItem);
  174.  
  175.         // Initialize the item from the dialog data.
  176.         if (!dlg.CreateItem(pItem))
  177.             AfxThrowMemoryException();  // any exception will do
  178.         ASSERT_VALID(pItem);
  179.  
  180.         // If item created from class list (not from file) then launch
  181.         //  the server to edit the item.
  182.         if (dlg.GetSelectionType() == COleInsertDialog::createNewItem)
  183.             pItem->DoVerb(OLEIVERB_SHOW, this);
  184.  
  185.         ASSERT_VALID(pItem);
  186.  
  187.         // As an arbitrary user interface design, this sets the selection
  188.         //  to the last item inserted.
  189.  
  190.         // TODO: reimplement selection as appropriate for your application
  191.  
  192.         m_pSelection = pItem;   // set selection to last inserted item
  193.         pDoc->UpdateAllViews(NULL);
  194.     }
  195.     CATCH(CException, e)
  196.     {
  197.         if (pItem != NULL)
  198.         {
  199.             ASSERT_VALID(pItem);
  200.             pItem->Delete();
  201.         }
  202.         AfxMessageBox(IDP_FAILED_TO_CREATE);
  203.     }
  204.     END_CATCH
  205.  
  206.     EndWaitCursor();
  207. }
  208.  
  209. // The following command handler provides the standard keyboard
  210. //  user interface to cancel an in-place editing session.  Here,
  211. //  the container (not the server) causes the deactivation.
  212. void CContainerMFCView::OnCancelEditCntr()
  213. {
  214.     // Close any in-place active item on this view.
  215.     COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
  216.     if (pActiveItem != NULL)
  217.     {
  218.         pActiveItem->Close();
  219.     }
  220.     ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
  221. }
  222.  
  223. // Special handling of OnSetFocus and OnSize are required for a container
  224. //  when an object is being edited in-place.
  225. void CContainerMFCView::OnSetFocus(CWnd* pOldWnd)
  226. {
  227.     COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
  228.     if (pActiveItem != NULL &&
  229.         pActiveItem->GetItemState() == COleClientItem::activeUIState)
  230.     {
  231.         // need to set focus to this item if it is in the same view
  232.         CWnd* pWnd = pActiveItem->GetInPlaceWindow();
  233.         if (pWnd != NULL)
  234.         {
  235.             pWnd->SetFocus();   // don't call the base class
  236.             return;
  237.         }
  238.     }
  239.  
  240.     CView::OnSetFocus(pOldWnd);
  241. }
  242.  
  243. // Resize control to fit client area.
  244. //
  245. void CContainerMFCView::OnSize(UINT nType, int cx, int cy)
  246. {
  247.     CView::OnSize(nType, cx, cy);
  248.     CStockTickerCtrl*   pCtrl = GetDocument()->GetControl();
  249.     if (pCtrl->GetSafeHwnd())
  250.     {
  251.         CRect   r(0, 0, cx, cy);
  252.         pCtrl->MoveWindow(r);
  253.     }
  254. }
  255.  
  256. /////////////////////////////////////////////////////////////////////////////
  257. // CContainerMFCView diagnostics
  258.  
  259. #ifdef _DEBUG
  260. void CContainerMFCView::AssertValid() const
  261. {
  262.     CView::AssertValid();
  263. }
  264.  
  265. void CContainerMFCView::Dump(CDumpContext& dc) const
  266. {
  267.     CView::Dump(dc);
  268. }
  269.  
  270. CContainerMFCDoc* CContainerMFCView::GetDocument() // non-debug version is inline
  271. {
  272.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CContainerMFCDoc)));
  273.     return (CContainerMFCDoc*)m_pDocument;
  274. }
  275. #endif //_DEBUG
  276.  
  277. /////////////////////////////////////////////////////////////////////////////
  278. // CContainerMFCView message handlers
  279.  
  280. int CContainerMFCView::OnCreate(LPCREATESTRUCT lpCreateStruct)
  281. {
  282.     if (CView::OnCreate(lpCreateStruct) == -1)
  283.         return -1;
  284.  
  285.     return 0;
  286. }
  287.  
  288. void CContainerMFCView::Serialize(CArchive& ar)
  289. {
  290.     CView::Serialize(ar);
  291.     if (ar.IsStoring())
  292.     {   // storing code
  293.     }
  294.     else
  295.     {   // loading code
  296.     }
  297. }
  298.