home *** CD-ROM | disk | FTP | other *** search
/ Building OCXs / Building_OCXs_Que_1995.iso / code / ch07 / contview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-15  |  5.6 KB  |  213 lines

  1. // contview.cpp : implementation of the CContView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "cont.h"
  6.  
  7. #include "contdoc.h"
  8. #include "cntritem.h"
  9. #include "contview.h"
  10.  
  11. #ifdef _DEBUG
  12. #undef THIS_FILE
  13. static char BASED_CODE THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CContView
  18.  
  19. IMPLEMENT_DYNCREATE(CContView, CView)
  20.  
  21. BEGIN_MESSAGE_MAP(CContView, CView)
  22.     //{{AFX_MSG_MAP(CContView)
  23.         // NOTE - the ClassWizard will add and remove mapping macros here.
  24.         //    DO NOT EDIT what you see in these blocks of generated code!
  25.     ON_WM_SETFOCUS()
  26.     ON_WM_SIZE()
  27.     ON_COMMAND(ID_OLE_INSERT_NEW, OnInsertObject)
  28.     ON_COMMAND(ID_CANCEL_EDIT_CNTR, OnCancelEditCntr)
  29.     //}}AFX_MSG_MAP
  30. END_MESSAGE_MAP()
  31.  
  32. /////////////////////////////////////////////////////////////////////////////
  33. // CContView construction/destruction
  34.  
  35. CContView::CContView()
  36. {
  37.     // TODO: add construction code here
  38.  
  39. }
  40.  
  41. CContView::~CContView()
  42. {
  43. }
  44.  
  45. /////////////////////////////////////////////////////////////////////////////
  46. // CContView drawing
  47.  
  48. void CContView::OnDraw(CDC* pDC)
  49. {
  50.     CContDoc* pDoc = GetDocument();
  51.     ASSERT_VALID(pDoc);
  52.  
  53.     // TODO: add draw code for native data here
  54.     // TODO: also draw all OLE items in the document
  55.  
  56.     // Draw the selection at an arbitrary position.  This code should be
  57.     //  removed once your real drawing code is implemented.  This position
  58.     //  corresponds exactly to the rectangle returned by CContCntrItem,
  59.     //  to give the effect of in-place editing.
  60.  
  61.     // TODO: remove this code when final draw code is complete.
  62.  
  63.     if (m_pSelection == NULL)
  64.     {
  65.         POSITION pos = pDoc->GetStartPosition();
  66.         m_pSelection = (CContCntrItem*)pDoc->GetNextClientItem(pos);
  67.     }
  68.     if (m_pSelection != NULL)
  69.         m_pSelection->Draw(pDC, CRect(10, 10, 210, 210));
  70. }
  71.  
  72. void CContView::OnInitialUpdate()
  73. {
  74.     CView::OnInitialUpdate();
  75.  
  76.     // TODO: remove this code when final selection model code is written
  77.     m_pSelection = NULL;    // initialize selection
  78.  
  79. }
  80.  
  81. /////////////////////////////////////////////////////////////////////////////
  82. // OLE Client support and commands
  83.  
  84. BOOL CContView::IsSelected(const CObject* pDocItem) const
  85. {
  86.     // The implementation below is adequate if your selection consists of
  87.     //  only CContCntrItem objects.  To handle different selection
  88.     //  mechanisms, the implementation here should be replaced.
  89.  
  90.     // TODO: implement this function that tests for a selected OLE client item
  91.  
  92.     return pDocItem == m_pSelection;
  93. }
  94.  
  95. void CContView::OnInsertObject()
  96. {
  97.     // Invoke the standard Insert Object dialog box to obtain information
  98.     //  for new CContCntrItem object.
  99.     COleInsertDialog dlg;
  100.     if (dlg.DoModal() != IDOK)
  101.         return;
  102.  
  103.     BeginWaitCursor();
  104.  
  105.     CContCntrItem* pItem = NULL;
  106.     TRY
  107.     {
  108.         // Create new item connected to this document.
  109.         CContDoc* pDoc = GetDocument();
  110.         ASSERT_VALID(pDoc);
  111.         pItem = new CContCntrItem(pDoc);
  112.         ASSERT_VALID(pItem);
  113.  
  114.         // Initialize the item from the dialog data.
  115.         if (!dlg.CreateItem(pItem))
  116.             AfxThrowMemoryException();  // any exception will do
  117.         ASSERT_VALID(pItem);
  118.  
  119.         // If item created from class list (not from file) then launch
  120.         //  the server to edit the item.
  121.         if (dlg.GetSelectionType() == COleInsertDialog::createNewItem)
  122.             pItem->DoVerb(OLEIVERB_SHOW, this);
  123.  
  124.         ASSERT_VALID(pItem);
  125.  
  126.         // As an arbitrary user interface design, this sets the selection
  127.         //  to the last item inserted.
  128.  
  129.         // TODO: reimplement selection as appropriate for your application
  130.  
  131.         m_pSelection = pItem;   // set selection to last inserted item
  132.         pDoc->UpdateAllViews(NULL);
  133.     }
  134.     CATCH(CException, e)
  135.     {
  136.         if (pItem != NULL)
  137.         {
  138.             ASSERT_VALID(pItem);
  139.             pItem->Delete();
  140.         }
  141.         AfxMessageBox(IDP_FAILED_TO_CREATE);
  142.     }
  143.     END_CATCH
  144.  
  145.     EndWaitCursor();
  146. }
  147.  
  148. // The following command handler provides the standard keyboard
  149. //  user interface to cancel an in-place editing session.  Here,
  150. //  the container (not the server) causes the deactivation.
  151. void CContView::OnCancelEditCntr()
  152. {
  153.     // Close any in-place active item on this view.
  154.     COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
  155.     if (pActiveItem != NULL)
  156.     {
  157.         pActiveItem->Close();
  158.     }
  159.     ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
  160. }
  161.  
  162. // Special handling of OnSetFocus and OnSize are required for a container
  163. //  when an object is being edited in-place.
  164. void CContView::OnSetFocus(CWnd* pOldWnd)
  165. {
  166.     COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
  167.     if (pActiveItem != NULL &&
  168.         pActiveItem->GetItemState() == COleClientItem::activeUIState)
  169.     {
  170.         // need to set focus to this item if it is in the same view
  171.         CWnd* pWnd = pActiveItem->GetInPlaceWindow();
  172.         if (pWnd != NULL)
  173.         {
  174.             pWnd->SetFocus();   // don't call the base class
  175.             return;
  176.         }
  177.     }
  178.  
  179.     CView::OnSetFocus(pOldWnd);
  180. }
  181.  
  182. void CContView::OnSize(UINT nType, int cx, int cy)
  183. {
  184.     CView::OnSize(nType, cx, cy);
  185.     COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
  186.     if (pActiveItem != NULL)
  187.         pActiveItem->SetItemRects();
  188. }
  189.  
  190. /////////////////////////////////////////////////////////////////////////////
  191. // CContView diagnostics
  192.  
  193. #ifdef _DEBUG
  194. void CContView::AssertValid() const
  195. {
  196.     CView::AssertValid();
  197. }
  198.  
  199. void CContView::Dump(CDumpContext& dc) const
  200. {
  201.     CView::Dump(dc);
  202. }
  203.  
  204. CContDoc* CContView::GetDocument() // non-debug version is inline
  205. {
  206.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CContDoc)));
  207.     return (CContDoc*)m_pDocument;
  208. }
  209. #endif //_DEBUG
  210.  
  211. /////////////////////////////////////////////////////////////////////////////
  212. // CContView message handlers
  213.