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