home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / ole / mfcbind / binddoc.cpp next >
Encoding:
C/C++ Source or Header  |  1998-04-03  |  8.0 KB  |  316 lines

  1. // BindDoc.cpp : implementation of the CMFCBindDoc 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. #include "stdafx.h"
  14. #include "MFCBind.h"
  15.  
  16. #include "BindDoc.h"
  17. #include "BindView.h"
  18. #include "CntrItem.h"
  19. #include "MainFrm.h"
  20. #include "ObjView.h"
  21.  
  22. #ifdef _DEBUG
  23. #define new DEBUG_NEW
  24. #undef THIS_FILE
  25. static char THIS_FILE[] = __FILE__;
  26. #endif
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CMFCBindDoc
  30.  
  31. IMPLEMENT_DYNCREATE(CMFCBindDoc, COleDocument)
  32.  
  33. BEGIN_MESSAGE_MAP(CMFCBindDoc, COleDocument)
  34.     //{{AFX_MSG_MAP(CMFCBindDoc)
  35.     ON_COMMAND(ID_OLE_INSERT_NEW, OnOleInsertNew)
  36.     ON_COMMAND(ID_SECTION_REMOVE, OnSectionRemove)
  37.     ON_UPDATE_COMMAND_UI(ID_SECTION_REMOVE, OnUpdateSectionRemove)
  38.     ON_COMMAND(ID_FILE_PRINT, OnFilePrint)
  39.     ON_COMMAND(ID_FILE_PRINTALLSECTIONS, OnFilePrintAll)
  40.     //}}AFX_MSG_MAP
  41.     // Enable default OLE container implementation
  42.     ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, COleDocument::OnUpdatePasteMenu)
  43.     ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE_LINK, COleDocument::OnUpdatePasteLinkMenu)
  44.     ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_CONVERT, COleDocument::OnUpdateObjectVerbMenu)
  45.     ON_COMMAND(ID_OLE_EDIT_CONVERT, COleDocument::OnEditConvert)
  46.     ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_LINKS, COleDocument::OnUpdateEditLinksMenu)
  47.     ON_COMMAND(ID_OLE_EDIT_LINKS, COleDocument::OnEditLinks)
  48.     ON_UPDATE_COMMAND_UI(ID_OLE_VERB_FIRST, COleDocument::OnUpdateObjectVerbMenu)
  49. END_MESSAGE_MAP()
  50.  
  51. /////////////////////////////////////////////////////////////////////////////
  52. // CMFCBindDoc construction/destruction
  53.  
  54. CMFCBindDoc::CMFCBindDoc()
  55. {
  56.     // Use OLE compound files
  57.     EnableCompoundFile();
  58. }
  59.  
  60. CMFCBindDoc::~CMFCBindDoc()
  61. {
  62. }
  63.  
  64. BOOL CMFCBindDoc::OnNewDocument()
  65. {
  66.     if (!COleDocument::OnNewDocument())
  67.         return FALSE;
  68.     CMainFrame *pFrame = NULL;
  69.     CObjListView* pView = NULL;
  70.     pFrame = (CMainFrame*)AfxGetApp()->m_pMainWnd;
  71.     if(!pFrame)
  72.         return TRUE;
  73.  
  74.     pView = pFrame->GetObjListView();
  75.     ASSERT(pView != NULL);
  76.  
  77.     if(pView)
  78.         pView->RemoveAllItems();
  79.     CMFCBindCntrItem::m_nDocID=1;
  80.  
  81.     return TRUE;
  82. }
  83.  
  84.  
  85.  
  86. /////////////////////////////////////////////////////////////////////////////
  87. // CMFCBindDoc serialization
  88.  
  89. void CMFCBindDoc::Serialize(CArchive& ar)
  90. {
  91.     if (ar.IsStoring())
  92.     {
  93.         // TODO: add storing code here
  94.     }
  95.     else
  96.     {
  97.         // TODO: add loading code here
  98.     }
  99.  
  100.     // Calling the base class COleDocument enables serialization
  101.     //  of the container document's COleClientItem objects.
  102.     COleDocument::Serialize(ar);
  103. }
  104.  
  105. /////////////////////////////////////////////////////////////////////////////
  106. // CMFCBindDoc diagnostics
  107.  
  108. #ifdef _DEBUG
  109. void CMFCBindDoc::AssertValid() const
  110. {
  111.     COleDocument::AssertValid();
  112. }
  113.  
  114. void CMFCBindDoc::Dump(CDumpContext& dc) const
  115. {
  116.     COleDocument::Dump(dc);
  117. }
  118. #endif //_DEBUG
  119.  
  120. /////////////////////////////////////////////////////////////////////////////
  121. // CMFCBindDoc commands
  122.  
  123. void CMFCBindDoc::OnOleInsertNew()
  124. {
  125.     // Invoke the standard Insert Object dialog box to obtain information
  126.     //  for new CMFCBindCntrItem object.
  127.     COleInsertDialog dlg;
  128.     if (dlg.DoModal(COleInsertDialog::DocObjectsOnly) != IDOK)
  129.         return;
  130.  
  131.     CMainFrame* pFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd;
  132.     ASSERT(pFrame != NULL);
  133.  
  134.     CMFCBindView* pViewObjCont = (CMFCBindView*) pFrame->GetDocView();
  135.     ASSERT(pViewObjCont != NULL);
  136.  
  137.     pViewObjCont->BeginWaitCursor();
  138.  
  139.     CMFCBindCntrItem* pItem = NULL;
  140.     TRY
  141.     {
  142.         // Create new item connected to this document.
  143.         pItem = new CMFCBindCntrItem(this);
  144.         ASSERT_VALID(pItem);
  145.  
  146.         //have the dialog create the item
  147.         if (!dlg.CreateItem(pItem))
  148.         {
  149.             delete pItem;
  150.             return;
  151.         }
  152.         ASSERT_VALID(pItem);
  153.  
  154.         // make sure we deactivate any active items first.
  155.         COleClientItem* pActiveItem = GetInPlaceActiveItem(pViewObjCont);
  156.         if (pActiveItem != NULL)
  157.             pActiveItem->Deactivate();
  158.  
  159.         // If item created from class list (not from file) then launch
  160.         //  the server to edit the item.
  161.         pItem->Activate(OLEIVERB_SHOW, pViewObjCont);
  162.         ASSERT_VALID(pItem);
  163.  
  164.         // As an arbitrary user interface design, this sets the selection
  165.         //  to the last item inserted.
  166.  
  167.         // TODO: reimplement selection as appropriate for your application
  168.  
  169.         // set selection to last inserted item
  170.         pViewObjCont->m_pSelection = (CMFCBindCntrItem*) pItem;
  171.         UpdateAllViews(NULL);
  172.     }
  173.     CATCH(CException, e)
  174.     {
  175.         if (pItem != NULL)
  176.         {
  177.             ASSERT_VALID(pItem);
  178.             pItem->Delete();
  179.  
  180.             CMainFrame *pFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd;
  181.             pFrame->GetObjListView()->RemoveItem(pItem);
  182.         }
  183.         AfxMessageBox(IDP_FAILED_TO_CREATE);
  184.     }
  185.     END_CATCH
  186.  
  187.     pViewObjCont->EndWaitCursor();
  188. }
  189.  
  190. void CMFCBindDoc::OnSectionRemove()
  191. {
  192.     // This is the menu handler for the Remove menu item.  We get
  193.     // the currently selected item, close it, remove it from the document's
  194.     // list of items and remove it's icon from the list box.
  195.  
  196.     CMainFrame *pFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd;
  197.     ASSERT(pFrame != NULL);
  198.  
  199.     CView *pViewObjCont = (CView*) pFrame->GetDocView();
  200.     ASSERT(pViewObjCont != NULL);
  201.  
  202.     COleClientItem* pItem = GetPrimarySelectedItem(pViewObjCont);
  203.  
  204.     if (pItem != NULL)
  205.     {
  206.         // remove the item from the list box.
  207.         pItem->Delete();
  208.         pFrame->GetObjListView()->RemoveItem(pItem);
  209.     }
  210. }
  211.  
  212. void CMFCBindDoc::OnUpdateSectionRemove(CCmdUI* pCmdUI)
  213. {
  214.     // we'll only enable the Remove menu item if there is a selected object
  215.     CMainFrame *pFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd;
  216.     ASSERT(pFrame != NULL);
  217.  
  218.     CView *pObjCont = (CView*) pFrame->GetDocView();
  219.  
  220.     if (pObjCont != NULL)
  221.         pCmdUI->Enable(); //we have an object selected
  222.     else
  223.         pCmdUI->Enable(FALSE);
  224.  
  225. }
  226.  
  227.  
  228. void CMFCBindDoc::OnFilePrint()
  229. {
  230.     CMainFrame *pFrame = (CMainFrame*)AfxGetApp()->m_pMainWnd;
  231.     ASSERT_VALID(pFrame);
  232.  
  233.     CMFCBindView *pView = pFrame->GetDocView();
  234.     ASSERT_VALID(pView);
  235.  
  236.     CPrintInfo pi;
  237.     //Active Document containers must specify whether or not
  238.     //to prompt the user with a print dialog.
  239.     pi.m_dwFlags|=PRINTFLAG_PROMPTUSER;
  240.  
  241.     if(!COleDocObjectItem::OnPreparePrinting(pView,&pi,FALSE))
  242.         return;
  243.  
  244.     if(!pView->DoPreparePrinting(&pi))
  245.         return;
  246.  
  247.     COleDocObjectItem::OnPrint(pView,&pi,FALSE);
  248.  
  249. }
  250.  
  251. void CMFCBindDoc::OnFilePrintAll()
  252. {
  253.     CMainFrame *pFrame = (CMainFrame*)AfxGetApp()->m_pMainWnd;
  254.     ASSERT_VALID(pFrame);
  255.  
  256.     CMFCBindView *pView = pFrame->GetDocView();
  257.     ASSERT_VALID(pView);
  258.  
  259.     CPrintInfo pi;
  260.  
  261.     if(!COleDocObjectItem::OnPreparePrinting(pView,&pi))
  262.         return;
  263.  
  264.     if(!pView->DoPreparePrinting(&pi))
  265.         return;
  266.  
  267.     COleDocObjectItem::OnPrint(pView,&pi);
  268. }
  269.  
  270. BOOL CMFCBindDoc::OnOpenDocument(LPCTSTR lpszPathName)
  271. {
  272.     //Make sure all items are removed from the object list view.
  273.     CMainFrame *pFrame = NULL;
  274.     CObjListView *pObjView=NULL;
  275.     pFrame = (CMainFrame*)AfxGetApp()->m_pMainWnd;
  276.     if(pFrame)
  277.         pObjView = pFrame->GetObjListView();
  278.     if(pObjView)
  279.         pObjView->RemoveAllItems();
  280.  
  281.     //Open the new document
  282.     if (!COleDocument::OnOpenDocument(lpszPathName))
  283.         return FALSE;
  284.  
  285.     POSITION pos = GetStartPosition();
  286.     //activate and set focus to the first item.
  287.     COleClientItem *pActiveItem = GetNextClientItem(pos);
  288.     if(pActiveItem)
  289.     {
  290.         pActiveItem->Activate(OLEIVERB_SHOW,pFrame->GetDocView());
  291.         pActiveItem->GetInPlaceWindow()->SetFocus();
  292.         CMFCBindView *pView = pFrame->GetDocView();
  293.         pView->m_pSelection = (CMFCBindCntrItem*)pActiveItem;
  294.         pFrame->GetObjListView()->m_listbox.SetCurSel(0);
  295.     }
  296.     CMFCBindCntrItem::m_nDocID=1;
  297.  
  298.     return TRUE;
  299. }
  300.  
  301. void CMFCBindDoc::OnCloseDocument()
  302. {
  303.     CMainFrame *pFrame = NULL;
  304.     CObjListView* pView = NULL;
  305.     pFrame = (CMainFrame*)AfxGetApp()->m_pMainWnd;
  306.     ASSERT(pFrame != NULL);
  307.  
  308.     pView = pFrame->GetObjListView();
  309.     ASSERT(pView != NULL);
  310.  
  311.     if(pView)
  312.         pView->RemoveAllItems();
  313.  
  314.     COleDocument::OnCloseDocument();
  315. }
  316.