home *** CD-ROM | disk | FTP | other *** search
/ Troubleshooting Netware Systems / CSTRIAL0196.BIN / attach / msj / v10n04 / olecont.exe / CONTVIEW.CPP < prev    next >
C/C++ Source or Header  |  1995-04-01  |  10KB  |  383 lines

  1. // contview.cpp : implementation of the CContainerView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "contain.h"
  6.  
  7. #include "cntlinfo.h"
  8. #include "contdoc.h"
  9. #include "cntlitem.h"
  10. #include "contitem.h"
  11. #include "contview.h"
  12. #include "inctldlg.h"
  13.  
  14. #ifdef _DEBUG
  15. #undef THIS_FILE
  16. static char BASED_CODE THIS_FILE[] = __FILE__;
  17. #endif
  18.  
  19.  
  20. /////////////////////////////////////////////////////////////////////////////
  21. // definitions
  22.  
  23. #define MARGIN_PIXELS    6
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CContainerView
  27.  
  28. IMPLEMENT_DYNCREATE(CContainerView, CView)
  29.  
  30. BEGIN_MESSAGE_MAP(CContainerView, CView)
  31.     //{{AFX_MSG_MAP(CContainerView)
  32.     ON_WM_SETFOCUS()
  33.     ON_WM_SIZE()
  34.     ON_COMMAND(ID_OLE_INSERT_NEW, InsertObject)
  35.     ON_COMMAND(ID_CANCEL_EDIT_CNTR, OnCancelEdit)
  36.     ON_WM_LBUTTONDBLCLK()
  37.     ON_WM_LBUTTONDOWN()
  38.     ON_WM_SETCURSOR()
  39.     ON_WM_DESTROY()
  40.     //}}AFX_MSG_MAP
  41.     // Standard printing commands
  42.     ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  43.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  44. END_MESSAGE_MAP()
  45.  
  46. /////////////////////////////////////////////////////////////////////////////
  47. // CContainerView construction/destruction
  48.  
  49. CContainerView::CContainerView()
  50. {
  51.     m_pSelection = NULL;
  52. }
  53.  
  54. CContainerView::~CContainerView()
  55. {
  56. }
  57.  
  58. /////////////////////////////////////////////////////////////////////////////
  59. // CContainerView drawing
  60.            
  61. void CContainerView::SetupTracker(CRectTracker* pTracker, CContainerItem* pItem)
  62. {
  63.     ASSERT(pTracker != NULL);
  64.     ASSERT(pItem != NULL);
  65.  
  66.     pTracker->m_nHandleSize = MARGIN_PIXELS;
  67.     pTracker->m_rect = pItem->m_rect;
  68.     pTracker->m_rect.InflateRect(MARGIN_PIXELS, MARGIN_PIXELS);
  69.     
  70.     if (pItem == m_pSelection)
  71.         pTracker->m_nStyle |= CRectTracker::resizeInside;
  72.  
  73.     if (pItem->GetType() == OT_LINK)
  74.         pTracker->m_nStyle |= CRectTracker::dottedLine;
  75.     else
  76.         pTracker->m_nStyle |= CRectTracker::solidLine;
  77.  
  78.     if (pItem->GetItemState() == COleClientItem::openState ||
  79.         pItem->GetItemState() == COleClientItem::activeUIState)
  80.     {
  81.         pTracker->m_nStyle |= CRectTracker::hatchInside;
  82.     }
  83. }
  84.                         
  85. void CContainerView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
  86. {
  87.     CView::OnPrepareDC(pDC, pInfo);
  88. }                        
  89.                         
  90. void CContainerView::OnDraw(CDC* pDC)
  91. {
  92.     CContainerDoc* pDoc = GetDocument();
  93.     ASSERT_VALID(pDoc);
  94.  
  95.     // Draw all CContainerItems
  96.     POSITION pos = pDoc->GetStartPosition();
  97.     while (pos != NULL)
  98.     {
  99.         CContainerItem* pItem = (CContainerItem*)pDoc->GetNextItem(pos);
  100.         pItem->Draw(pDC, pItem->m_rect);
  101.  
  102.         // draw the tracker
  103.         CRectTracker tracker;
  104.         SetupTracker(&tracker, pItem);
  105.         tracker.Draw(pDC);
  106.     }
  107. }
  108.  
  109. void CContainerView::OnInitialUpdate()
  110. {
  111.     CView::OnInitialUpdate();
  112.  
  113.     // TODO: remove this code when final selection model code is written
  114.     m_pSelection = NULL;    // initialize selection
  115.  
  116. }
  117.   
  118. void CContainerView::InvalidateItem(CContainerItem* pItem)
  119. {
  120.     pItem->Invalidate();
  121. }
  122.   
  123. /////////////////////////////////////////////////////////////////////////////
  124. // CContainerView printing
  125.  
  126. BOOL CContainerView::OnPreparePrinting(CPrintInfo* pInfo)
  127. {
  128.     // default preparation
  129.     return DoPreparePrinting(pInfo);
  130. }
  131.  
  132. void CContainerView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  133. {
  134.     // TODO: add extra initialization before printing
  135. }
  136.  
  137. void CContainerView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  138. {
  139.     // TODO: add cleanup after printing
  140. }
  141.  
  142. /////////////////////////////////////////////////////////////////////////////
  143. // OLE Client support and commands
  144.  
  145. BOOL CContainerView::IsSelected(const CObject* pDocItem) const
  146. {
  147.     return pDocItem == m_pSelection;
  148. }                             
  149.                                 
  150.  
  151. CContainerItem* CContainerView::HitTestItems(CPoint point)
  152. {
  153.     CContainerDoc* pDoc = GetDocument();
  154.     CContainerItem* pCtlHit = NULL;
  155.     POSITION pos = pDoc->GetStartPosition();
  156.     while (pos != NULL)
  157.     {
  158.         CContainerItem* pCtl = (CContainerItem*)pDoc->GetNextItem(pos);
  159.         if (pCtl->IsKindOf(RUNTIME_CLASS(CContainerItem)))
  160.         {
  161.             CRectTracker tracker;
  162.             SetupTracker(&tracker, pCtl);
  163.             if (tracker.HitTest(point) >= 0)
  164.             {
  165.                 pCtlHit = pCtl;
  166.                 // items later in the list are drawn on top - so keep looking
  167.             }
  168.         }
  169.     }
  170.     return pCtlHit;    // return top item at point
  171. }
  172.                                 
  173. void CContainerView::SetSelection(CContainerItem* pNewSel)
  174. {
  175.     if (pNewSel != NULL && pNewSel == m_pSelection)
  176.         return;
  177.  
  178.     // deactivate any in-place active item on this view!
  179.     COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
  180.     if (pActiveItem != NULL && pNewSel != pActiveItem)
  181.     {
  182.         // if we found one, deactivate it
  183.         pActiveItem->Close();
  184.         ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
  185.     }
  186.     if (m_pSelection != NULL) // invalidate the old item
  187.         InvalidateItem(m_pSelection);
  188.     if ((m_pSelection = pNewSel) != NULL) // invalidate the new item
  189.         InvalidateItem(m_pSelection);
  190. }
  191.  
  192. void CContainerView::InsertObject()
  193. {
  194.     // Invoke the standard Insert Object dialog box to obtain information
  195.     //  for new CContainerItem object.
  196.     COleInsertCtlDlg dlg(this);
  197.     if (dlg.DoModal() != IDOK)
  198.         return;
  199.  
  200.     BeginWaitCursor();
  201.  
  202.     CContainerItem* pItem = NULL;
  203.     TRY
  204.     {
  205.         // Create new item connected to this document.
  206.         CContainerDoc* pDoc = GetDocument();
  207.         ASSERT_VALID(pDoc);
  208.         pItem = new CContainerItem(pDoc);
  209.         ASSERT_VALID(pItem);
  210.  
  211.         // Initialize the item from the dialog data.
  212.         if (!dlg.CreateItem(pItem))
  213.             AfxThrowMemoryException();  // any exception will do
  214.         ASSERT_VALID(pItem);
  215.  
  216.         // If item created from class list (not from file) then launch
  217.         //  the server to edit the item.
  218.         if (dlg.GetSelectionType() == COleInsertDialog::createNewItem)
  219.         {
  220.             pItem->UpdateLink();
  221.             pItem->UpdateExtent();
  222.             pItem->DoVerb(OLEIVERB_SHOW, this);
  223.         }
  224.  
  225.         ASSERT_VALID(pItem);
  226.  
  227.         // As an arbitrary user interface design, this sets the selection
  228.         //  to the last item inserted.
  229.  
  230.         // TODO: reimplement selection as appropriate for your application
  231.  
  232.         m_pSelection = pItem;   // set selection to last inserted item
  233.         pDoc->UpdateAllViews(NULL);
  234.     }
  235.     CATCH(CException, e)
  236.     {
  237.         if (pItem != NULL)
  238.         {
  239.             ASSERT_VALID(pItem);
  240.             pItem->Delete();
  241.         }
  242.         AfxMessageBox(IDP_FAILED_TO_CREATE);
  243.     }
  244.     END_CATCH
  245.  
  246.     EndWaitCursor();
  247. }
  248.  
  249. // The following command handler provides the standard keyboard
  250. //  user interface to cancel an in-place editing session.
  251. void CContainerView::OnCancelEdit()
  252. {
  253.     // Close any in-place active item on this view.
  254.     COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
  255.     if (pActiveItem != NULL)
  256.     {
  257.         pActiveItem->Close();
  258.     }
  259.     ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
  260. }
  261.  
  262. // Special handling of OnSetFocus and OnSize are required for a container
  263. //  when an object is being edited in-place.
  264. void CContainerView::OnSetFocus(CWnd* pOldWnd)
  265. {
  266.     COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
  267.     if (pActiveItem != NULL &&
  268.         pActiveItem->GetItemState() == COleClientItem::activeUIState)
  269.     {
  270.         // need to set focus to this item if it is in the same view
  271.         CWnd* pWnd = pActiveItem->GetInPlaceWindow();
  272.         if (pWnd != NULL)
  273.         {
  274.             pWnd->SetFocus();   // don't call the base class
  275.             return;
  276.         }
  277.     }
  278.  
  279.     CView::OnSetFocus(pOldWnd);
  280. }
  281.  
  282. void CContainerView::OnSize(UINT nType, int cx, int cy)
  283. {
  284.     CView::OnSize(nType, cx, cy);
  285.     COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
  286.     if (pActiveItem != NULL)
  287.         pActiveItem->SetItemRects();
  288. }
  289.  
  290. /////////////////////////////////////////////////////////////////////////////
  291. // CContainerView diagnostics
  292.  
  293. #ifdef _DEBUG
  294. void CContainerView::AssertValid() const
  295. {
  296.     CView::AssertValid();
  297. }
  298.  
  299. void CContainerView::Dump(CDumpContext& dc) const
  300. {
  301.     CView::Dump(dc);
  302. }
  303.  
  304. CContainerDoc* CContainerView::GetDocument() // non-debug version is inline
  305. {
  306.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CContainerDoc)));
  307.     return (CContainerDoc*)m_pDocument;
  308. }
  309. #endif //_DEBUG
  310.  
  311. /////////////////////////////////////////////////////////////////////////////
  312. // CContainerView message handlers
  313.                
  314.                
  315. void CContainerView::OnLButtonDblClk(UINT nFlags, CPoint point) 
  316. {
  317.     OnLButtonDown(nFlags, point);
  318.  
  319.     if (m_pSelection != NULL)
  320.     {
  321.         m_pSelection->DoVerb(GetKeyState(VK_CONTROL) < 0 ?
  322.             OLEIVERB_OPEN : OLEIVERB_PRIMARY, this);
  323.     }
  324.  
  325.     CView::OnLButtonDblClk(nFlags, point);
  326. }
  327.  
  328. void CContainerView::OnLButtonDown(UINT nFlags, CPoint point) 
  329. {
  330.     CContainerItem* pItemHit = HitTestItems(point);
  331.     SetSelection(pItemHit);
  332.  
  333.     if (pItemHit != NULL)
  334.     {
  335.         CRectTracker tracker;
  336.         SetupTracker(&tracker, pItemHit);
  337.  
  338.         UpdateWindow();
  339.         if (tracker.Track(this, point))
  340.         {
  341.             pItemHit->Invalidate();    
  342.             tracker.m_rect.InflateRect(-MARGIN_PIXELS, -MARGIN_PIXELS);
  343.             pItemHit->Move(tracker.m_rect);
  344.             pItemHit->Invalidate();
  345.             GetDocument()->SetModifiedFlag();
  346.         }
  347.     }
  348.  
  349.     CView::OnLButtonDown(nFlags, point);
  350. }
  351.  
  352. BOOL CContainerView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
  353. {
  354.     if (pWnd == this && m_pSelection != NULL)
  355.     {
  356.         // give the tracker for the selection a chance
  357.         CRectTracker tracker;
  358.         SetupTracker(&tracker, m_pSelection);
  359.         if (tracker.SetCursor(this, nHitTest))
  360.             return TRUE;
  361.     }
  362.     
  363.     return CView::OnSetCursor(pWnd, nHitTest, message);
  364. }
  365.  
  366. void CContainerView::OnDestroy() 
  367. {
  368.     CView::OnDestroy();
  369.     
  370.     // Deactivate the in place active item on this view
  371.  
  372.     // Note:  To handle multiple inplace items, you will need to
  373.     //        iterate through all of the items and deactivate those that
  374.     //        apply.
  375.  
  376.     COleClientItem* pItem = GetDocument()->GetInPlaceActiveItem(this);
  377.     if (pItem != NULL && pItem->GetActiveView() == this)
  378.     {
  379.         pItem->Deactivate();
  380.         ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
  381.     }
  382. }
  383.