home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / cntritem.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  10.3 KB  |  347 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. // cntritem.cpp : implementation of the CNetscapeCntrItem class
  20. //
  21.  
  22. #include "stdafx.h"
  23.  
  24. #include "cntritem.h"
  25.  
  26. #ifdef _DEBUG
  27. #undef THIS_FILE
  28. static char BASED_CODE THIS_FILE[] = __FILE__;
  29. #endif
  30. #ifndef XP_WIN32
  31. #include "olenls.h"
  32. #define OLESTR(str) str
  33. #endif
  34. /////////////////////////////////////////////////////////////////////////////
  35. // CNetscapeCntrItem implementation
  36.  
  37. #ifndef _AFXDLL
  38. #undef new
  39. #endif
  40. IMPLEMENT_SERIAL(CNetscapeCntrItem, COleClientItem, 0)
  41. #ifndef _AFXDLL
  42. #define new DEBUG_NEW
  43. #endif
  44. extern void  FE_ConvertSpace(char *newName);
  45.  
  46. CNetscapeCntrItem::CNetscapeCntrItem(CGenericDoc* pContainer)
  47.     : COleClientItem(pContainer)
  48. {
  49.     m_bLoading = FALSE; //  Not loading.
  50.     m_bBroken = FALSE;  //  Not broken.
  51.     m_bDelayed = FALSE; //  Not delayed.
  52.     m_iLock = 0;    //  No one is referencing us.
  53.     m_pOriginalItem = NULL;
  54.     m_isFullPage = FALSE;
  55.     m_isDirty = FALSE;
  56.     m_bIsOleItem = FALSE;
  57.     m_bSetExtents = FALSE;
  58.     m_bCanSavedByOLE = FALSE;
  59.     m_idSavedAs =DISPID_UNKNOWN; 
  60.  
  61. }
  62.  
  63. CNetscapeCntrItem::~CNetscapeCntrItem()
  64. {
  65.     //  If we've something loaded, then release it.
  66.     if(m_bLoading == FALSE && m_bBroken == FALSE && m_bDelayed == FALSE)    {
  67.         //  Assuming we're valid.
  68.         Release(OLECLOSE_SAVEIFDIRTY);
  69.     }
  70. }
  71. void CNetscapeCntrItem::OnActivate()
  72. {
  73.     char * pSource = NULL;
  74.     const char* ptr;
  75.     LPDISPATCH pdisp;
  76.     HRESULT hr;
  77.     int _convert;
  78.     if (m_lpObject->QueryInterface(IID_IDispatch, (void**)&pdisp) == S_OK){
  79. #ifdef XP_WIN32
  80.         LPCOLESTR lpOleStr = T2COLE("SaveAs");
  81.         hr = pdisp->GetIDsOfNames(IID_NULL, (unsigned short **)&lpOleStr, 1, LOCALE_USER_DEFAULT, &m_idSavedAs);
  82.         pdisp->Release();
  83.         if (hr == S_OK)    
  84.             m_bCanSavedByOLE = TRUE;
  85.         else
  86.             m_idSavedAs = DISPID_UNKNOWN;
  87. #else
  88.             m_idSavedAs = DISPID_UNKNOWN;
  89. #endif
  90.     }
  91.  
  92.     const char* ptr1 = m_csAddress; 
  93.     if (NET_IsLocalFileURL((char*)ptr1)) {
  94.         XP_ConvertUrlToLocalFile(m_csAddress, &pSource);
  95.         m_csDosName = *pSource;  // pick up the drive name.
  96.         m_csDosName += ":";
  97.         m_csDosName += strchr(pSource, '\\');  // pick up the real file name.
  98.         ptr = m_csDosName;     
  99.         FE_ConvertSpace((char*)ptr);
  100.         XP_FREE(pSource);
  101.     }
  102.  
  103.     // the object does not support ole automation, try to find out if this is a storagefile, so
  104.     // we can use OLESave().
  105.     if (!m_bCanSavedByOLE) {
  106. #ifdef XP_WIN32        // we will only want to handle saving when the object had storage file.
  107.         int _convert;
  108.         LPCOLESTR lpsz = A2CW(m_csDosName);
  109.         if (StgIsStorageFile(lpsz) == S_OK) 
  110.             m_bCanSavedByOLE = TRUE;
  111. #else                       
  112.         HRESULT sc1 = StgIsStorageFile(m_csDosName);
  113.         if (GetScode(sc1) == S_OK) 
  114.             m_bCanSavedByOLE = TRUE;
  115. #endif
  116.         }
  117.  
  118.     CGenericView* pView = GetActiveView();
  119.     CFrameGlue *pFrameGlue = pView->GetFrame();
  120.     if(pFrameGlue != NULL)    {
  121.         m_bLocationBarShowing = pFrameGlue->GetChrome()->GetToolbarVisible(ID_LOCATION_TOOLBAR);
  122.     }
  123.  
  124.     COleClientItem::OnActivate();
  125. }
  126.  
  127. void CNetscapeCntrItem::OnChange(OLE_NOTIFICATION nCode, DWORD dwParam)
  128. {
  129.     COleClientItem::OnChange(nCode, dwParam);
  130.  
  131.     // this is a hack to test if we need to ask user to save the file or not.
  132.     // since I can not get lpPersistStorage->IsDirty() to give me the correct 
  133.     // answer.  
  134.     if (nCode == OLE_CLOSED)
  135.         m_isDirty = FALSE;
  136.  
  137.     if (nCode == OLE_CHANGED && dwParam == DVASPECT_CONTENT)
  138.         m_isDirty = TRUE;
  139.  
  140.     //  Update all references to the item manually.
  141.     MWContext *pContext = GetDocument()->GetContext()->GetContext();
  142.     POSITION rIndex = m_cplElements.GetHeadPosition();
  143.     LO_EmbedStruct *pLayoutData;
  144.     while(rIndex != NULL)   {
  145.         pLayoutData = (LO_EmbedStruct *)m_cplElements.GetNext(rIndex);
  146.         if (pContext->compositor) {
  147.             XP_Rect rect;
  148.             
  149.             CL_GetLayerBbox(pLayoutData->layer, &rect);
  150.             CL_UpdateLayerRect(CL_GetLayerCompositor(pLayoutData->layer),
  151.                                pLayoutData->layer, &rect, PR_FALSE);
  152.         }
  153.         else
  154.             pContext->funcs->DisplayEmbed(pContext, FE_VIEW, pLayoutData);
  155.     }
  156. }
  157.  
  158. BOOL CNetscapeCntrItem::OnChangeItemPosition(const CRect& rectPos)
  159. {
  160.     //  Change the position of the item.
  161.     //  Let this happen for them, we won't change layout, and we'll see how it goes.
  162.     if(FALSE == COleClientItem::OnChangeItemPosition(rectPos))  {
  163.         return(FALSE);
  164.     }
  165.     return(TRUE);
  166. }
  167.  
  168. void CNetscapeCntrItem::OnGetItemPosition(CRect& rectPos)   {
  169.     //  Give them coordinates to the position of the in place activated item.
  170.     //  So, which item was selected?  We need layout data.
  171.     CGenericDoc *pDoc = GetDocument();
  172.     if(!pDoc)   {
  173.         return;
  174.     }
  175.     CDCCX *pContextCX = pDoc->GetContext();
  176.     if(!pContextCX || pContextCX->IsFrameContext() == FALSE)    {
  177.         return;
  178.     }
  179.  
  180.     CWinCX *pWinCX = VOID2CX(pContextCX, CWinCX);
  181.     if(pWinCX->m_pSelected == NULL)   {
  182.         //  Don't do anything, we don't know of any selection.
  183.         return;
  184.     }
  185.  
  186.     //  Okay, we know about the selected item.
  187.     //  Now, we need to figure out where it is located in the view.
  188.     HDC hdc = pWinCX->GetContextDC();
  189.  
  190.     long lLeft = pWinCX->m_pSelected->x + pWinCX->m_pSelected->x_offset - pWinCX->GetOriginX();
  191.     long lRight = lLeft + pWinCX->m_pSelected->width;
  192.  
  193.     long lTop = pWinCX->m_pSelected->y + pWinCX->m_pSelected->y_offset - pWinCX->GetOriginY();
  194.     long lBottom = lTop + pWinCX->m_pSelected->height;
  195.  
  196.     //  Convert our twips into pixels.
  197.     RECT crConvert;
  198.     ::SetRect(&crConvert, CASTINT(lLeft), CASTINT(lTop), CASTINT(lRight), CASTINT(lBottom));
  199.     ::LPtoDP(hdc, (POINT*) &crConvert, 2);
  200.  
  201.     rectPos = crConvert;
  202.  
  203.     pWinCX->ReleaseContextDC(hdc);
  204. }
  205.  
  206. void CNetscapeCntrItem::OnDeactivateUI(BOOL bUndoable)
  207. {
  208.  
  209.     COleClientItem::OnDeactivateUI(bUndoable);
  210.     // Close an in-place active item whenever it removes the user
  211.     //  interface.  The action here should match as closely as possible
  212.     //  to the handling of the escape key in the view.
  213.     if( IsInPlaceActive() == TRUE)   {
  214.         TRY    {
  215.             Deactivate();
  216.  
  217.         }
  218.         CATCH(CException, e)    {
  219.             //    Something went wrong in OLE (other app down).
  220.             //    No complicated handling here, just keep running.
  221.         }
  222.         END_CATCH
  223.     }
  224. }
  225.  
  226. // TODO: We can remove this check once everyone moves to MSVC 4.0
  227. #if _MSC_VER >= 1000
  228. BOOL CNetscapeCntrItem::OnUpdateFrameTitle()    {
  229.     //  Nothing doing.  We stay the same.
  230.     //  This ASSERTs anyhow, if it continues on down.
  231.     return FALSE;
  232. }
  233. #else
  234. void CNetscapeCntrItem::OnUpdateFrameTitle()    {
  235.     //  Nothing doing.  We stay the same.
  236.     //  This ASSERTs anyhow, if it continues on down.
  237.     return;
  238. }
  239. #endif
  240.  
  241. BOOL CNetscapeCntrItem::OnGetWindowContext(CFrameWnd **ppMainFrame, CFrameWnd **ppDocFrame, LPOLEINPLACEFRAMEINFO lpFrameInfo) {
  242.     //  First call the base.
  243.     BOOL bRetval = COleClientItem::OnGetWindowContext(ppMainFrame, ppDocFrame, lpFrameInfo);
  244.  
  245.     //  Now, override the values with ones that make sense to us.
  246.     CGenericDoc *pDoc = GetDocument();
  247.     CDCCX *pCX = pDoc->GetContext();
  248.     if(pCX != NULL && pCX->IsFrameContext() == TRUE)    {
  249.         //    Get the frame from the window context.
  250.         CWinCX *pWinCX = (CWinCX *)pCX;
  251.         *ppMainFrame = pWinCX->GetFrame()->GetFrameWnd();
  252.         *ppDocFrame = NULL;    //    Act like SDI.
  253.  
  254.         //    It's possible that there is no frame.
  255.         //    In which case we should do what?  Fail.
  256.         if(pWinCX->GetFrame()->GetFrameWnd() == NULL)    {
  257.             bRetval = FALSE;
  258.         }
  259.     }
  260.     else    {
  261.         bRetval = FALSE;
  262.         *ppMainFrame = NULL;
  263.         *ppDocFrame = NULL;
  264.     }
  265.  
  266.     return(bRetval);
  267. }
  268.  
  269. BOOL CNetscapeCntrItem::OnShowControlBars(CFrameWnd *pFrameWnd, BOOL bShow)    {
  270.     //  Call the base, they'll at least handle our normal control bar.
  271.     BOOL bToolBarChanged = FALSE;
  272.     //    Get the frame glue.
  273.     CFrameGlue *pFrameGlue = CFrameGlue::GetFrameGlue(pFrameWnd);
  274.     if(pFrameGlue != NULL)    {
  275.         if(bShow == FALSE)  {
  276.             //  Hide our stuff.
  277.             if(m_bLocationBarShowing){
  278.                 pFrameGlue->GetChrome()->ShowToolbar(ID_LOCATION_TOOLBAR, FALSE);
  279.                 bToolBarChanged = TRUE;
  280.             }
  281.         }
  282.         else  {
  283.             //  Show our stuff.
  284.             //  Assuming that we'll always be called to hide before we're called to show.
  285.                if(m_bLocationBarShowing){
  286.                 pFrameGlue->GetChrome()->ShowToolbar(ID_LOCATION_TOOLBAR, m_bLocationBarShowing);
  287.                  bToolBarChanged = TRUE;
  288.             }
  289.         }
  290.     }
  291.     BOOL bToolBarChanged1 = COleClientItem::OnShowControlBars(pFrameWnd, bShow);
  292.     return(bToolBarChanged || bToolBarChanged1);
  293. }
  294.  
  295. void CNetscapeCntrItem::Serialize(CArchive& ar)
  296. {
  297.     ASSERT_VALID(this);
  298.  
  299.     // Call base class first to read in COleClientItem data.
  300.     // Since this sets up the m_pDocument pointer returned from
  301.     //  CNetscapeCntrItem::GetDocument, it is a good idea to call
  302.     //  the base class Serialize first.
  303.     COleClientItem::Serialize(ar);
  304.  
  305.     // now store/retrieve data specific to CNetscapeCntrItem
  306.     if (ar.IsStoring())
  307.     {
  308.     }
  309.     else
  310.     {
  311.     }
  312. }
  313.  
  314. BOOL CNetscapeCntrItem::CanActivate()    {
  315.     //    Can't activate if the document itself is inplace active.
  316.     CGenericDoc *pDoc = GetDocument();
  317.     ASSERT_VALID(pDoc);
  318.     ASSERT(pDoc->IsKindOf(RUNTIME_CLASS(COleServerDoc)));
  319.  
  320.     if(pDoc->IsInPlaceActive())    {
  321.         return(FALSE);
  322.     }
  323.  
  324.     return(COleClientItem::CanActivate());
  325. }
  326.  
  327.  
  328.  
  329.  
  330.  
  331. /////////////////////////////////////////////////////////////////////////////
  332. // CNetscapeCntrItem diagnostics
  333.  
  334. #ifdef _DEBUG
  335. void CNetscapeCntrItem::AssertValid() const
  336. {
  337.     COleClientItem::AssertValid();
  338. }
  339.  
  340. void CNetscapeCntrItem::Dump(CDumpContext& dc) const
  341. {
  342.     COleClientItem::Dump(dc);
  343. }
  344. #endif
  345.  
  346. /////////////////////////////////////////////////////////////////////////////
  347.