home *** CD-ROM | disk | FTP | other *** search
/ Using Visual Basic 5 (Platinum Edition) / vb5.iso / ACTIVEX / SRDVID / DATA.Z / svdoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-10  |  8.0 KB  |  358 lines

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. //    svdoc.cpp : Implementation of the CSVViewerDoc class
  4. //
  5. /////////////////////////////////////////////////////////////////////////////
  6. //
  7. //  (C) Copyright Black Diamond Consulting, Inc 1996. All rights reserved.
  8. //
  9. //    You have a royalty-free right to use, modify, reproduce and 
  10. //    distribute the Sample Files (and/or any modified version) in 
  11. //    any way you find useful, provided that you agree that Black 
  12. //    Diamond Consulting has no warranty obligations or liability
  13. //    for any Sample Application Files which are modified. 
  14. //
  15. //    Revision History:
  16. //
  17. /////////////////////////////////////////////////////////////////////////////
  18.  
  19. #include "stdafx.h"
  20. #include <stdlib.h>
  21. #include "dib.h"
  22.  
  23. #ifndef __CASTRING_H__
  24.     #include "CAString.h"
  25. #endif
  26.  
  27. #ifndef __HOTSPOT_H__
  28.     #include "hotspot.h"
  29. #endif
  30.  
  31. #ifndef __SVVIEW_H__
  32.     #include "SVView.h"
  33. #endif
  34.  
  35. #ifndef __SVDOC_H__
  36.     #include "SVDoc.h"
  37. #endif
  38.  
  39. #ifdef _DEBUG
  40. #undef THIS_FILE
  41. static char BASED_CODE THIS_FILE[] = __FILE__;
  42. #endif
  43.  
  44. #ifdef UNICODE
  45.     #define CSVDOC_SURROUNDHOTSPOTS L"Hotspots"
  46. #else
  47.     #define CSVDOC_SURROUNDHOTSPOTS "Hotspots"
  48. #endif
  49.  
  50.  
  51. /////////////////////////////////////////////////////////////////////////////
  52. // CSVViewerDoc
  53.  
  54. IMPLEMENT_DYNCREATE(CSVViewerDoc, CDocument)
  55.  
  56. BEGIN_MESSAGE_MAP(CSVViewerDoc, CDocument)
  57.     //{{AFX_MSG_MAP(CSVViewerDoc)
  58.     //}}AFX_MSG_MAP
  59. END_MESSAGE_MAP()
  60.  
  61. /////////////////////////////////////////////////////////////////////////////
  62. // CSVViewerDoc construction/destruction
  63.  
  64. CSVViewerDoc::CSVViewerDoc()
  65. {
  66.     m_pISurround = NULL;
  67.     m_pIStorage = NULL;
  68. }
  69.  
  70. BOOL CSVViewerDoc::OnOpenDocument(LPCTSTR lpszPathName)
  71. {
  72.     HRESULT          hr;
  73.     IStorage         *pIStorage = NULL;
  74.     IStream            *pStream = NULL;
  75.     IStream            *pHotspotStream = NULL;
  76.     CAnyString        imageStream = CSVDOC_SURROUNDIMAGE;
  77.     CAnyString        hotspotStream = CSVDOC_SURROUNDHOTSPOTS;
  78.  
  79.     // free up the current surround video image
  80.     DeleteContents();
  81.     SetModifiedFlag( FALSE );
  82.     
  83.     // Open the file
  84.     hr = OpenStorage( lpszPathName, &pIStorage, FALSE );
  85.     if( FAILED(hr) )
  86.         return FALSE;
  87.  
  88.     hr = pIStorage->OpenStream( imageStream, NULL, STGM_READ|STGM_SHARE_EXCLUSIVE, 0, &pStream );
  89.     if( FAILED(hr) )
  90.         return FALSE;
  91.  
  92.     // Get an ISurround using the highest color resolution we can based
  93.     // on the display depth.
  94.     HDC hdc = GetDC(NULL);
  95.     hr = -1;
  96.     if( GetDeviceCaps( hdc, BITSPIXEL ) > 8 )
  97.             hr = PanoramicSurroundFromStream( pStream, 24, &m_pISurround );
  98.     if( FAILED(hr) )
  99.             hr = PanoramicSurroundFromStream( pStream, 8, &m_pISurround );
  100.     ReleaseDC(NULL, hdc);
  101.  
  102.     if( FAILED(hr) )
  103.         AfxThrowFileException(0);
  104.  
  105.     // Read in any hotspots
  106.     // NOTE: It's OK if there is not hotspot stream so only complain if we
  107.     //         find one and can't read it
  108.     hr = pIStorage->OpenStream( hotspotStream, NULL, STGM_READ|STGM_SHARE_EXCLUSIVE, 0, &pHotspotStream );
  109.     if( !FAILED(hr) )
  110.     {
  111.         hr = m_hotspots.Read( pHotspotStream );
  112.         if( FAILED(hr) )
  113.             AfxThrowFileException(0);
  114.     }
  115.     else hr = S_OK;
  116.  
  117.     m_pIStorage = pIStorage;
  118.  
  119.     if( pStream )
  120.         pStream->Release();
  121.  
  122.     if( pHotspotStream )
  123.         pHotspotStream->Release();
  124.  
  125.     return m_pISurround != NULL;
  126. }
  127.  
  128. void CSVViewerDoc::OnCloseDocument()
  129. {
  130.     DeleteContents();
  131.     CDocument::OnCloseDocument();
  132. }
  133.  
  134. BOOL CSVViewerDoc::OnSaveDocument( LPCTSTR pszPathName )
  135. {
  136.     HRESULT hr;
  137.     LPSTORAGE pIStorage;
  138.     LPSTREAM pIStream;
  139.     STATSTG stg;
  140.     CAnyString storageName;
  141.     CAnyString pathName = pszPathName;
  142.     CAnyString hotspotStream = CSVDOC_SURROUNDHOTSPOTS;
  143.  
  144.     // We should have a storage at this point
  145.     if( m_pIStorage == NULL )
  146.     {
  147.         AfxMessageBox( AFX_MSG_CANTSAVEEMPTY );
  148.         return FALSE;
  149.     }
  150.  
  151.     // Get status of current storage
  152.     hr = m_pIStorage->Stat( &stg, STATFLAG_DEFAULT );
  153.     if( FAILED(hr) )
  154.         return FALSE;
  155.  
  156.     // If it's not the same file, create a new one and copy to it
  157.     storageName = stg.pwcsName;
  158.     if( stricmp( storageName, pathName ) != NULL )
  159.     {
  160.         if( !IsOverwriteable( pathName ) )
  161.             return FALSE;
  162.  
  163.         hr = OpenStorage( pathName, &pIStorage, TRUE );
  164.         if( FAILED(hr) )
  165.             return FALSE;
  166.  
  167.         // Copy the entire storage to the new file
  168.         // (we will shortly replace the Hotspot stream)
  169.         BeginWaitCursor();
  170.         m_pIStorage->CopyTo( NULL, NULL, NULL, pIStorage );
  171.         EndWaitCursor();
  172.  
  173.         // We are using the new file now, release the old one
  174.         m_pIStorage->Release();
  175.         m_pIStorage = pIStorage;
  176.  
  177.     }
  178.  
  179.     // Create (or Overwrite) hotspot stream
  180.     hr = m_pIStorage->CreateStream( hotspotStream,
  181.                                     STGM_DIRECT|STGM_WRITE|STGM_SHARE_EXCLUSIVE|STGM_CREATE,
  182.                                     0, 0, &pIStream );    
  183.     if( FAILED(hr) )
  184.         return FALSE;
  185.  
  186.     BeginWaitCursor();
  187.     hr = m_hotspots.Write( pIStream );
  188.     pIStream->Release();
  189.     EndWaitCursor();
  190.  
  191.     if( FAILED(hr) )
  192.     {
  193.         AfxMessageBox( AFX_MSG_CANTSAVEDOCUMENT );
  194.         return FALSE;
  195.     }
  196.     else
  197.         this->SetModifiedFlag( FALSE );
  198.     
  199.     return !FAILED(hr);
  200. }
  201.  
  202. HRESULT CSVViewerDoc::OpenStorage( LPCTSTR pszPathName, LPSTORAGE* ppIStorage, BOOL bCreate )
  203. {
  204.     HRESULT hr;
  205.     CLSID clsid;
  206.     CAnyString pathName = pszPathName;
  207.  
  208.     *ppIStorage = NULL;
  209.  
  210.     if( bCreate )
  211.     {
  212.         // Create an empty document file, blow away any old one
  213.         hr = StgCreateDocfile( pathName,
  214.                                STGM_READWRITE|STGM_SHARE_EXCLUSIVE|STGM_CREATE,
  215.                                0, ppIStorage );
  216.         if( FAILED(hr) )
  217.         {
  218.             AfxMessageBox( AFX_MSG_UNABLETOCREATE );
  219.             return hr;
  220.         }
  221.  
  222.         // Write the class ID
  223.         hr = WriteClassStg( *ppIStorage, CLSID_CSVDoc );
  224.     
  225.         if( FAILED(hr) )
  226.             return hr;
  227.     }
  228.  
  229.     else
  230.     {
  231.         // Open the root storage (file)
  232.         hr = StgOpenStorage( pathName, NULL,
  233.                              STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE,
  234.                              NULL, 0, ppIStorage );
  235.         if( FAILED(hr) )
  236.         {
  237.             AfxMessageBox( AFX_MSG_UNABLETOREADFILE );
  238.             return hr;
  239.         }
  240.  
  241.         // Make sure it's one of ours
  242.         hr = ReadClassStg( *ppIStorage, &clsid );
  243.         if( FAILED(hr) )
  244.         {
  245.             AfxMessageBox( AFX_MSG_UNABLETOREADFILE );
  246.             return hr;
  247.         }
  248.         else if( clsid != CLSID_CSVDoc )
  249.         {
  250.             AfxMessageBox( AFX_MSG_INVALIDFILE );
  251.             return STG_E_OLDFORMAT;
  252.         }
  253.     }
  254.     
  255.     return S_OK;
  256. }
  257.  
  258. BOOL CSVViewerDoc::IsOverwriteable( LPCTSTR pszPathName )
  259. {
  260.     HRESULT hr;
  261.     LPSTORAGE pIStorage;
  262.     CLSID clsid;
  263.     CString msg;
  264.     CAnyString pathName = pszPathName;
  265.  
  266.     hr = StgIsStorageFile( pathName );
  267.     if( hr == STG_E_FILENOTFOUND )
  268.     {
  269.         return TRUE;
  270.     }
  271.  
  272.     if( hr == S_OK )
  273.     {
  274.         // Open the root storage (file)
  275.         hr = StgOpenStorage( pathName, NULL,
  276.                              STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE,
  277.                              NULL, 0, &pIStorage );
  278.         if( FAILED(hr) )
  279.         {
  280.             AfxMessageBox( AFX_MSG_UNABLETOREADFILE );
  281.             return FALSE;
  282.         }
  283.  
  284.         // Make sure it's one of ours
  285.         hr = ReadClassStg( pIStorage, &clsid );
  286.         
  287.         // Close it
  288.         pIStorage->Release();
  289.         
  290.         if( FAILED(hr) )
  291.         {
  292.             AfxMessageBox( AFX_MSG_UNABLETOREADFILE );
  293.             return FALSE;
  294.         }
  295.         else if( clsid == CLSID_CSVDoc )
  296.             return TRUE;
  297.      }
  298.  
  299.     AfxFormatString1( msg, AFX_MSG_OVERWRITE, pathName ); 
  300.     return IDOK == AfxMessageBox( msg, MB_ICONEXCLAMATION|MB_OKCANCEL ); 
  301. }
  302.  
  303. /////////////////////////////////////////////////////////////////////////////
  304. // CSVViewerDoc diagnostics
  305.  
  306. #ifdef _DEBUG
  307. void CSVViewerDoc::AssertValid() const
  308. {
  309.     CDocument::AssertValid();
  310. }
  311.  
  312. void CSVViewerDoc::Dump(CDumpContext& dc) const
  313. {
  314.     CDocument::Dump(dc);
  315. }
  316. #endif //_DEBUG
  317.  
  318. CView* CSVViewerDoc::GetSVView(void)
  319. {
  320.     // Loop through views looking for CSVViewerView,
  321.     POSITION pos = GetFirstViewPosition();
  322.     while(pos != NULL)
  323.     {
  324.         CView* pView = GetNextView(pos);
  325.         if(pView->IsKindOf(RUNTIME_CLASS(CSVViewerView)))
  326.             return pView;
  327.     }
  328.  
  329.     return NULL;
  330. }
  331.  
  332. void CSVViewerDoc::DeleteContents()
  333. {
  334.     // Reset the CSVViewerView before attempting to reset myself
  335.     CSVViewerView* pSVViewerView = (CSVViewerView*)GetSVView();
  336.     if(NULL != pSVViewerView)
  337.         pSVViewerView->Reset();
  338.  
  339.     // Delete all hotspots
  340.     m_hotspots.ResetContent();
  341.  
  342.     // release our interfaces
  343.     if (m_pISurround)
  344.     {
  345.         m_pISurround->Release();
  346.         m_pISurround = NULL;
  347.     }
  348.  
  349.     if (m_pIStorage)
  350.     {
  351.         m_pIStorage->Release();
  352.         m_pIStorage = NULL;
  353.     }
  354.  
  355. }
  356.  
  357.  
  358.