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

  1. // svrdoc.cpp : implementation of the CServerDoc 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 "hiersvr.h"
  16.  
  17. #include "svrdoc.h"
  18. #include "svritem.h"
  19. #include "svrview.h"
  20.  
  21. #ifdef _DEBUG
  22. #undef THIS_FILE
  23. static char BASED_CODE THIS_FILE[] = __FILE__;
  24. #endif
  25.  
  26. CLIPFORMAT NEAR CServerDoc::m_cfPrivate = NULL;
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CServerDoc
  30.  
  31. IMPLEMENT_DYNCREATE(CServerDoc, COleServerDoc)
  32.  
  33. BEGIN_MESSAGE_MAP(CServerDoc, COleServerDoc)
  34.     //{{AFX_MSG_MAP(CServerDoc)
  35.     ON_COMMAND(ID_OPTIONS_FONT, OnOptionsFont)
  36.     ON_COMMAND(ID_CANCEL_INPLACE, OnCancelInplace)
  37.     //}}AFX_MSG_MAP
  38. END_MESSAGE_MAP()
  39.  
  40. /////////////////////////////////////////////////////////////////////////////
  41. // CServerDoc construction/destruction
  42.  
  43. CServerDoc::CServerDoc()
  44. {
  45.     m_pRoot = CServerNode::CreateRootNode(this);
  46.  
  47.     // determine default font for document
  48.     memset(&m_logfont, 0, sizeof m_logfont);
  49.     m_logfont.lfHeight = -10;
  50.     lstrcpy(m_logfont.lfFaceName, _T("Arial"));
  51.     m_logfont.lfOutPrecision = OUT_TT_PRECIS;
  52.     m_logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  53.     m_logfont.lfQuality = PROOF_QUALITY;
  54.     m_logfont.lfPitchAndFamily = FF_SWISS | VARIABLE_PITCH;
  55.  
  56.     // use default window text color
  57.     m_crText = COLOR_WINDOWTEXT+1;
  58.  
  59.     if (m_cfPrivate == NULL)
  60.     {
  61.         m_cfPrivate = (CLIPFORMAT)
  62.             ::RegisterClipboardFormat(_T("MFC HierSvr Sample"));
  63.     }
  64. }
  65.  
  66. CServerDoc::~CServerDoc()
  67. {
  68.     delete m_pRoot;     // delete kills child nodes
  69. }
  70.  
  71. void CServerDoc::DeleteContents()
  72. {
  73.     COleServerDoc::DeleteContents();
  74.  
  75.     if (m_pRoot != NULL)
  76.         m_pRoot->InitRootNode();
  77. }
  78.  
  79. BOOL CServerDoc::OnNewDocument()
  80. {
  81.     if (!COleServerDoc::OnNewDocument())
  82.         return FALSE;
  83.  
  84.     // Note: m_pRoot is created in the constructor
  85.     return (m_pRoot != NULL);
  86. }
  87.  
  88. COleServerItem* CServerDoc::OnGetEmbeddedItem()
  89. {
  90.     ASSERT_VALID(m_pRoot);
  91.  
  92.     // allocate embedded item first time requested
  93.     if (m_pRoot->m_pServerItem == NULL)
  94.         m_pRoot->m_pServerItem = new CServerItem(this, m_pRoot);
  95.  
  96.     // and return it
  97.     ASSERT_VALID(m_pRoot->m_pServerItem);
  98.     return m_pRoot->m_pServerItem;
  99. }
  100.  
  101. COleServerItem* CServerDoc::OnGetLinkedItem(LPCTSTR lpszItemName)
  102. {
  103.     ASSERT_VALID(m_pRoot);
  104.  
  105.     // look in current list first
  106.     COleServerItem* pItem = COleServerDoc::OnGetLinkedItem(lpszItemName);
  107.     if (pItem != NULL)
  108.         return pItem;
  109.  
  110.     // look in document itself for an item with that link name
  111.     CString strItemName = lpszItemName;
  112.     CServerNode* pNode = m_pRoot->FindNode(strItemName);
  113.     if (pNode == NULL)
  114.         return NULL;    // node does not exist
  115.  
  116.     ASSERT(pNode->m_pServerItem == NULL);   // should not be connected
  117.     pItem = new CServerItem(this, pNode);
  118.     ASSERT_VALID(pItem);
  119.  
  120.     // return new item that matches lpszItemName
  121.     return pItem;
  122. }
  123.  
  124. CFont* CServerDoc::SelectDocFont(CDC* pDC, CFont& font)
  125. {
  126.     // convert points in m_logfont.lfHeight to logical
  127.     LOGFONT logfont = m_logfont;
  128.     logfont.lfHeight = -::MulDiv(-logfont.lfHeight,
  129.         pDC->GetDeviceCaps(LOGPIXELSY), 72);
  130.  
  131.     // set the text color as appropriate
  132.     COLORREF cr = m_crText;
  133.     if (cr == COLOR_WINDOW+1)
  134.         cr = GetSysColor(COLOR_WINDOW);
  135.     pDC->SetTextColor(m_crText);
  136.  
  137.     // create the font object
  138.     if (!font.CreateFontIndirect(&logfont))
  139.         return NULL;
  140.  
  141.     // select the font
  142.     return pDC->SelectObject(&font);
  143. }
  144.  
  145. int CServerDoc::DrawTree(CDC* pDC, CPoint ptStart, CServerNode* pNodeSel,
  146.     CServerNode* pRoot)
  147. {
  148.     if (pRoot == NULL)
  149.         pRoot = m_pRoot;
  150.     // select correct font for the document
  151.     CFont font;
  152.     CFont* pOldFont = SelectDocFont(pDC, font);
  153.     if (pOldFont == NULL)
  154.         return -1;
  155.  
  156.     // draw the hierachy list
  157.     int iResult = pRoot->DrawTree(pDC, ptStart, pNodeSel);
  158.  
  159.     // restore state of the dc
  160.     pDC->SelectObject(pOldFont);
  161.     return iResult;
  162. }
  163.  
  164. void CServerDoc::CalcBounding(CDC* pDC, CServerNode* pNodeStart,
  165.     CPoint ptStart, CSize& sizeMax)
  166. {
  167.     ASSERT_VALID(pNodeStart);
  168.  
  169.     // select correct font for the document
  170.     CFont font;
  171.     CFont* pOldFont = SelectDocFont(pDC, font);
  172.  
  173.     // calculate the bounding rect
  174.     pNodeStart->CalcBounding(pDC, ptStart, sizeMax);
  175.  
  176.     // restore state of the dc
  177.     if (pOldFont != NULL)
  178.         pDC->SelectObject(pOldFont);
  179. }
  180.  
  181. /////////////////////////////////////////////////////////////////////////////
  182. // CServerDoc serialization
  183.  
  184. // this serializes the OLE Server document as a stand-alone file
  185. void CServerDoc::Serialize(CArchive& ar)
  186. {
  187.     ASSERT(m_pRoot != NULL);
  188.     SerializeFontInfo(ar);
  189.     m_pRoot->Serialize(ar);
  190. }
  191.  
  192. void CServerDoc::SerializeFontInfo(CArchive& ar)
  193. {
  194.     if (ar.IsStoring())
  195.     {
  196.         ar.Write(&m_logfont, sizeof(m_logfont) - sizeof(m_logfont.lfFaceName));
  197.         // lfFaceName is stored as CString so it is UNICODE/ANSI independent
  198.         ar << CString(m_logfont.lfFaceName);
  199.         ar << m_crText;
  200.     }
  201.     else
  202.     {
  203.         ar.Read(&m_logfont, sizeof(m_logfont) - sizeof(m_logfont.lfFaceName));
  204.         // lfFaceName must be read as a CString
  205.         CString strFaceName;
  206.         ar >> strFaceName;
  207.         lstrcpy(m_logfont.lfFaceName, strFaceName);
  208.         ar >> m_crText;
  209.     }
  210. }
  211.  
  212. /////////////////////////////////////////////////////////////////////////////
  213. // CServerDoc in-place editing
  214.  
  215. void CServerDoc::OnSetItemRects(LPCRECT lpPosRect, LPCRECT lpClipRect)
  216. {
  217.     // get first view of document
  218.     POSITION pos = GetFirstViewPosition();
  219.     ASSERT(pos != NULL);
  220.     CServerView* pView = (CServerView*)GetNextView(pos);
  221.     ASSERT_KINDOF(CServerView, pView);
  222.     ASSERT_VALID(pView);
  223.  
  224.     CSize sizeNum(lpPosRect->right - lpPosRect->left,
  225.         lpPosRect->bottom - lpPosRect->top);
  226.     // for denom -- get extent in device
  227.     // create a view dc
  228.     CServerDC dc(pView);
  229.     // set zoom to 100%
  230.     dc.SetViewportExt(CSize(1,1));
  231.     dc.SetWindowExt(CSize(1,1));
  232.     // get extents in device
  233.     CSize sizeDenom = pView->CalcActualItemSize(m_pRoot, &dc);
  234.  
  235.     // notify first view of potential zoom factor change!
  236.     pView->SetZoomFactor(sizeNum, sizeDenom);
  237.     // resize the window
  238.     COleServerDoc::OnSetItemRects(lpPosRect, lpClipRect);
  239.     // set scrollbar state (if necessary)
  240.     pView->SetScrollInfo();
  241. }
  242.  
  243. void CServerDoc::OnOptionsFont()
  244. {
  245.     CClientDC dc(NULL);
  246.     LOGFONT lf = m_logfont;
  247.     lf.lfHeight = -::MulDiv(-lf.lfHeight, dc.GetDeviceCaps(LOGPIXELSY), 72);
  248.     CFontDialog dlg(&lf);
  249.     dlg.m_cf.rgbColors = m_crText;
  250.     if (dlg.DoModal() == IDOK)
  251.     {
  252.         lf.lfHeight = -::MulDiv(-lf.lfHeight, 72, dc.GetDeviceCaps(LOGPIXELSY));
  253.         m_crText = dlg.GetColor();
  254.         m_logfont = lf;
  255.         SetModifiedFlag();
  256.         UpdateAllItems(NULL);
  257.         UpdateAllViews(NULL);
  258.     }
  259. }
  260.  
  261. // Note: both the server and the container should have a keyboard method
  262. //  of deactivating an active in-place item.
  263.  
  264. void CServerDoc::OnCancelInplace()
  265. {
  266.     if (IsInPlaceActive())
  267.         OnDeactivateUI(FALSE);
  268. }
  269.  
  270. /////////////////////////////////////////////////////////////////////////////
  271. // CServerDoc diagnostics
  272.  
  273. #ifdef _DEBUG
  274. void CServerDoc::AssertValid() const
  275. {
  276.     COleServerDoc::AssertValid();
  277. }
  278.  
  279. void CServerDoc::Dump(CDumpContext& dc) const
  280. {
  281.     COleServerDoc::Dump(dc);
  282. }
  283. #endif //_DEBUG
  284.  
  285. /////////////////////////////////////////////////////////////////////////////
  286.