home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / srvritem.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  11.0 KB  |  389 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. // implementation of the CNetscapeSrvrItem class
  20. //
  21.  
  22. #include "stdafx.h"
  23.  
  24. #include "srvritem.h"
  25. #include "cntritem.h"
  26.  
  27. #include "cxmeta.h"
  28. #include "template.h"
  29. #include "mainfrm.h"
  30. #include "ipframe.h"
  31. #include "netsvw.h"
  32.  
  33. #ifdef _DEBUG
  34. #undef THIS_FILE
  35. static char BASED_CODE THIS_FILE[] = __FILE__;
  36. #endif
  37.  
  38. /////////////////////////////////////////////////////////////////////////////
  39. // CNetscapeSrvrItem implementation
  40.  
  41. IMPLEMENT_DYNAMIC(CNetscapeSrvrItem, COleServerItem)
  42.  
  43. //    Ole menu lock.
  44. int CNetscapeSrvrItem::m_iOLEMenuLock = 0;
  45.  
  46. void CNetscapeSrvrItem::LoadOLEMenus()
  47. {
  48.     if (m_ShowUI) {
  49.         //    Up the count.
  50.         m_iOLEMenuLock++;
  51.  
  52.         //    If it's at 1, then we really need to load them.
  53.         if(m_iOLEMenuLock == 1)    {
  54.             TRACE("Loading OLE Menus\n");
  55.  
  56.             HINSTANCE hInst = AfxGetResourceHandle();
  57.             //    Embedded
  58.             if(theApp.m_ViewTmplate->m_hMenuEmbedding == NULL)    {
  59.                 theApp.m_ViewTmplate->m_hMenuEmbedding = ::LoadMenu(hInst, MAKEINTRESOURCE(IDR_SRVR_EMBEDDED));
  60.             }
  61.             ASSERT(theApp.m_ViewTmplate->m_hMenuEmbedding);
  62.  
  63.             if(theApp.m_ViewTmplate->m_hAccelEmbedding == NULL)    {
  64.                 theApp.m_ViewTmplate->m_hAccelEmbedding = ::LoadAccelerators(hInst, MAKEINTRESOURCE(IDR_SRVR_EMBEDDED));
  65.             }
  66.             ASSERT(theApp.m_ViewTmplate->m_hAccelEmbedding);
  67.             //    Inplace
  68.             if(theApp.m_ViewTmplate->m_hMenuInPlaceServer == NULL)    {
  69.                 theApp.m_ViewTmplate->m_hMenuInPlaceServer = ::LoadMenu(hInst, MAKEINTRESOURCE(IDR_SRVR_INPLACE));
  70.             }
  71.             ASSERT(theApp.m_ViewTmplate->m_hMenuInPlaceServer);
  72.  
  73.             if(theApp.m_ViewTmplate->m_hAccelInPlaceServer == NULL)    {
  74.                 theApp.m_ViewTmplate->m_hAccelInPlaceServer = ::LoadAccelerators(hInst, MAKEINTRESOURCE(IDR_SRVR_INPLACE));
  75.             }
  76.             ASSERT(theApp.m_ViewTmplate->m_hAccelInPlaceServer);
  77.         }
  78.     }
  79. }
  80.  
  81. void CNetscapeSrvrItem::UnloadOLEMenus()
  82. {
  83.     if (m_ShowUI) {
  84.         //    Down the menu lock.
  85.         m_iOLEMenuLock--;
  86.  
  87.         //    If it's at 0, then we really need to get rid of them.
  88.         if(m_iOLEMenuLock == 0)    {
  89.             TRACE("Unloading OLE Menus\n");
  90.  
  91.             //    Embedded
  92.             if(theApp.m_ViewTmplate->m_hMenuEmbedding)    {
  93.                 ::DestroyMenu(theApp.m_ViewTmplate->m_hMenuEmbedding);
  94.                 theApp.m_ViewTmplate->m_hMenuEmbedding = NULL;
  95.             }
  96.             if(theApp.m_ViewTmplate->m_hAccelEmbedding)    {
  97.                 ::FreeResource(theApp.m_ViewTmplate->m_hAccelEmbedding);
  98.                 theApp.m_ViewTmplate->m_hAccelEmbedding = NULL;
  99.             }
  100.  
  101.             //    Inplace
  102.             if(theApp.m_ViewTmplate->m_hMenuInPlaceServer)    {
  103.                 ::DestroyMenu(theApp.m_ViewTmplate->m_hMenuInPlaceServer);
  104.                 theApp.m_ViewTmplate->m_hMenuInPlaceServer = NULL;
  105.             }
  106.             if(theApp.m_ViewTmplate->m_hAccelInPlaceServer)    {
  107.                 ::FreeResource(theApp.m_ViewTmplate->m_hAccelInPlaceServer);
  108.                 theApp.m_ViewTmplate->m_hAccelInPlaceServer = NULL;
  109.             }
  110.         }
  111.     }
  112. }
  113.  
  114. CNetscapeSrvrItem::CNetscapeSrvrItem(CGenericDoc* pContainerDoc)
  115.     : COleServerItem(pContainerDoc, TRUE)
  116. {
  117.     TRACE("Creating CNetscapeSrvrItem %p\n", this);
  118.  
  119.     //    No size information of yet.
  120.     //    However, we set the below as a default.
  121.     m_sizeExtent = CSize(0, 0);
  122.  
  123.     //    Let the context know it's an OLE server.
  124.     if(pContainerDoc->GetContext())    {
  125.         pContainerDoc->GetContext()->EnableOleServer();
  126.     }
  127.     m_ShowUI = TRUE;
  128.  
  129.  
  130.     //    Load up the OLE server menus.
  131.     //    This is a good place for the delayed loading and unloading the menu's only
  132.     //        when needed, as a server item won't exist unless needed via OLE.
  133.     LoadOLEMenus();
  134. }
  135.  
  136. CNetscapeSrvrItem::~CNetscapeSrvrItem()
  137. {
  138.     TRACE("Destroying CNetscapeSrvrItem %p\n", this);
  139.  
  140.     //    Since the server item is now gone, it also cleaned up the view and
  141.     //        frame, however, it did not clean up our extra component, the context.
  142.     CGenericDoc *pDoc = GetDocument();
  143.     if(pDoc != NULL)    {
  144.         CDCCX *pCX = pDoc->GetContext();
  145.         if(pCX != NULL)    {
  146.             TRACE("Destroying document context from server item\n");
  147.             pCX->DestroyContext();
  148.         }
  149. #ifdef DEBUG
  150.         else    {
  151.             TRACE("No document context to destroy from server item\n");
  152.         }
  153. #endif
  154.     }
  155.  
  156.     //    Unload OLE menu's.
  157.     //    This is a good place for the delayed loading and unloading the menu's only
  158.     //        when needed, as a server item won't exist unless needed via OLE.
  159.     UnloadOLEMenus();
  160. }
  161.  
  162. void CNetscapeSrvrItem::Serialize(CArchive& ar)
  163. {
  164.     TRACE("Serializing CNetscapeSrvrItem\n");
  165.  
  166.     // CNetscapeSrvrItem::Serialize will be called by the framework if
  167.     //  the item is copied to the clipboard.  This can happen automatically
  168.     //  through the OLE callback OnGetClipboardData.  A good default for
  169.     //  the embedded item is simply to delegate to the document's Serialize
  170.     //  function.  If you support links, then you will want to serialize
  171.     //  just a portion of the document.
  172.  
  173.     if (!IsLinkedItem())
  174.     {
  175.         CGenericDoc* pDoc = GetDocument();
  176.         ASSERT_VALID(pDoc);
  177.         pDoc->Serialize(ar);
  178.     }
  179. }
  180.  
  181. BOOL CNetscapeSrvrItem::OnGetExtent(DVASPECT dwDrawAspect, CSize& rSize)
  182. {
  183.     TRACE("CNetscapeSrvrItem::OnGetExtent(dwDrawAspect, size %p\n", this);
  184.  
  185.     BOOL bRetval = TRUE;
  186.  
  187.     // Most applications, like this one, only handle drawing the content
  188.     //  aspect of the item.  If you wish to support other aspects, such
  189.     //  as DVASPECT_THUMBNAIL (by overriding OnDrawEx), then this
  190.     //  implementation of OnGetExtent should be modified to handle the
  191.     //  additional aspect(s).
  192.  
  193.     if (dwDrawAspect != DVASPECT_CONTENT)
  194.         return COleServerItem::OnGetExtent(dwDrawAspect, rSize);
  195.  
  196.     // CNetscapeSrvrItem::OnGetExtent is called to get the extent in
  197.     //  HIMETRIC units of the entire item.
  198.     
  199.     //    If we've never sized before, set the m_sizeExtent, and return it.
  200.     if(m_sizeExtent.cx == 0 || m_sizeExtent.cy == 0)    {
  201.         CGenericDoc* pDoc = GetDocument();
  202.         if(pDoc != NULL)    {
  203.             //    Return the size of the normal netscape view.
  204.             //    This will cause the window to come up in a correct size depending on the
  205.             //        area of the item.
  206.             CWinCX *pCX = VOID2CX(pDoc->GetContext(), CWinCX);
  207.             if(pCX != NULL)    {
  208.                 HWND hView = pCX->GetPane();
  209.                 if(hView != NULL)    {
  210.                     CRect crDim;
  211.                     ::GetClientRect(hView, crDim);
  212.  
  213.                     int32 lWidth = crDim.Width();
  214.                     int32 lHeight = crDim.Height();
  215.  
  216.                     if (lWidth == 0) 
  217.                         lWidth = 7000;
  218.                     else
  219.                         lWidth = pCX->Twips2MetricX(lWidth);
  220.                     if (lHeight == 0) 
  221.                         lHeight = 7000;
  222.                     else
  223.                         lHeight = pCX->Twips2MetricY(lHeight);
  224.  
  225.                     m_sizeExtent = CSize(CASTINT(lWidth), CASTINT(lHeight));
  226.                 }
  227.             }
  228.         }
  229.     }
  230.     //    Return the extents set previously, or set by OnSetExtent.
  231.     rSize = m_sizeExtent;
  232.  
  233.     TRACE("GetExtents are %ld,%ld\n", m_sizeExtent.cx, m_sizeExtent.cy);
  234.     ASSERT(bRetval);
  235.     return(bRetval);
  236. }
  237.  
  238. BOOL CNetscapeSrvrItem::OnSetExtent(DVASPECT nDrawAspect, const CSize& size)    {
  239.     TRACE("CNetscapeSrvrItem::OnSetExtent(nDrawAspect, size) %p\n", this);
  240.  
  241.     CGenericDoc *pDoc = GetDocument();
  242.     ASSERT(pDoc);
  243.  
  244.     //    Don't handle any others but direct content for now.
  245.     BOOL bRetval = COleServerItem::OnSetExtent(nDrawAspect, size);
  246.     m_sizeExtent = size;
  247.     if(nDrawAspect == DVASPECT_CONTENT)    {
  248.         //  Set our default extents to be reported by the document.
  249.         pDoc->m_csDocumentExtent = m_sizeExtent;
  250.         bRetval = TRUE;
  251.     }
  252.  
  253.     TRACE("SetExtents are %ld,%ld\n", m_sizeExtent.cx, m_sizeExtent.cy);
  254.     ASSERT(bRetval);
  255.     return(bRetval);
  256. }
  257.  
  258.  
  259. //  Return TRUE in most cases unless serious error.
  260. //  Nothing to draw is OK, but unable to draw is not OK.
  261. BOOL CNetscapeSrvrItem::OnDraw(CDC* pDC, CSize& rSize)
  262. {
  263.     TRACE("CNetscapeSrvrItem::OnDraw %p\n", this);
  264.     ASSERT_VALID(pDC);
  265.  
  266.     CGenericDoc* pDoc = GetDocument();
  267.     ASSERT_VALID(pDoc);
  268.  
  269.     //    Determine the URL we're going to draw.
  270.     CString csDraw;
  271.     pDoc->GetContextHistoryAddress(csDraw);
  272.     if(csDraw.IsEmpty())    {
  273.         TRACE("Nothing to draw.\n");
  274.         return(TRUE);
  275.     }
  276.  
  277.     //    Allocate the url.
  278.     URL_Struct *pUrl = NET_CreateURLStruct((const char *)csDraw, NET_DONT_RELOAD);
  279.     if(pUrl == NULL)    {
  280.         TRACE("Nothing to draw without URL.\n");
  281.         return(TRUE);
  282.     }
  283.  
  284.     //    Before we do this, make sure we know the current size of the view
  285.     //      not the document constant extent.
  286.     CSize csViewport = pDoc->m_csViewExtent;
  287.  
  288.     //    Have a new context draw the item into the metafile.
  289.     //    We have to block until completion.
  290.     DWORD dwBlockID = CMetaFileCX::MetaFileAnchorObject(pDC, csViewport, pUrl);
  291.     if(dwBlockID == 0)    {
  292.         //    Counld not create new meta file context.
  293.         return(FALSE);
  294.     }
  295.  
  296.     //    Block return until the context is destroyed.
  297.     FEU_BlockUntilDestroyed(dwBlockID);
  298.  
  299.     return(TRUE);
  300. }
  301.  
  302. #define NO_UI_EMBEDDING 5
  303.  
  304. void CNetscapeSrvrItem::OnDoVerb(LONG iVerb)
  305. {
  306.     switch (iVerb)
  307.     {
  308.     // open - maps to OnOpen
  309.     case OLEIVERB_OPEN:
  310.     case -OLEIVERB_OPEN-1:  // allows positive OLEIVERB_OPEN-1 in registry
  311.         OnOpen();
  312.         break;
  313.  
  314.     // primary, show, and unknown map to OnShow
  315.     case OLEIVERB_PRIMARY:  // OLEIVERB_PRIMARY is 0 and "Edit" in registry
  316.     case OLEIVERB_SHOW:
  317.     case OLEIVERB_UIACTIVATE: {
  318.         COleServerDoc* pDoc = GetDocument();
  319.         m_ShowUI = TRUE;
  320.         OnShow();
  321.         break;
  322.     }                      
  323.  
  324.     case OLEIVERB_INPLACEACTIVATE: {
  325.         COleServerDoc* pDoc = GetDocument();
  326.         m_ShowUI = FALSE;
  327.         OnShow();
  328.         break;
  329.    }
  330.  
  331.     // hide maps to OnHide
  332.     case OLEIVERB_HIDE:
  333.     case -OLEIVERB_HIDE-1:  // allows positive OLEIVERB_HIDE-1 in registry
  334.         OnHide();
  335.         break;
  336.  
  337.  
  338.     case NO_UI_EMBEDDING:    {
  339.         //Get InPlaceFrame ptr.
  340.         CGenericDoc *pDoc = GetDocument();
  341.         ASSERT(pDoc);
  342.  
  343.         POSITION pos = pDoc->GetFirstViewPosition();
  344.         CView *pView = pDoc->GetNextView(pos);
  345.         ASSERT(pView);
  346.  
  347.         m_ShowUI = FALSE;
  348.  
  349.         OnShow();
  350.  
  351.         CInPlaceFrame *pFrm = (CInPlaceFrame *)pView->GetParent();
  352.         ASSERT(pFrm);
  353.  
  354.         pFrm->DestroyResizeBar();
  355.  
  356.         break;
  357.         }
  358.     default:
  359.         // negative verbs not understood should return E_NOTIMPL
  360.         if (iVerb < 0)
  361.             AfxThrowOleException(E_NOTIMPL);
  362.  
  363.         // positive verb not processed --
  364.         //  according to OLE spec, primary verb should be executed
  365.         //  instead.
  366.         OnDoVerb(OLEIVERB_PRIMARY);
  367.  
  368.         // also, OLEOBJ_S_INVALIDVERB should be returned.
  369.         AfxThrowOleException(OLEOBJ_S_INVALIDVERB);
  370.     }
  371. }
  372.  
  373. /////////////////////////////////////////////////////////////////////////////
  374. // CNetscapeSrvrItem diagnostics
  375.  
  376. #ifdef _DEBUG
  377. void CNetscapeSrvrItem::AssertValid() const
  378. {
  379.     COleServerItem::AssertValid();
  380. }
  381.  
  382. void CNetscapeSrvrItem::Dump(CDumpContext& dc) const
  383. {
  384.     COleServerItem::Dump(dc);
  385. }
  386. #endif
  387.  
  388. /////////////////////////////////////////////////////////////////////////////
  389.