home *** CD-ROM | disk | FTP | other *** search
- /////////////////////////////////////////////////////////////////////////////
- //
- // svdoc.cpp : Implementation of the CSVViewerDoc class
- //
- /////////////////////////////////////////////////////////////////////////////
- //
- // (C) Copyright Black Diamond Consulting, Inc 1996. All rights reserved.
- //
- // You have a royalty-free right to use, modify, reproduce and
- // distribute the Sample Files (and/or any modified version) in
- // any way you find useful, provided that you agree that Black
- // Diamond Consulting has no warranty obligations or liability
- // for any Sample Application Files which are modified.
- //
- // Revision History:
- //
- /////////////////////////////////////////////////////////////////////////////
-
- #include "stdafx.h"
- #include <stdlib.h>
- #include "dib.h"
-
- #ifndef __CASTRING_H__
- #include "CAString.h"
- #endif
-
- #ifndef __HOTSPOT_H__
- #include "hotspot.h"
- #endif
-
- #ifndef __SVVIEW_H__
- #include "SVView.h"
- #endif
-
- #ifndef __SVDOC_H__
- #include "SVDoc.h"
- #endif
-
- #ifdef _DEBUG
- #undef THIS_FILE
- static char BASED_CODE THIS_FILE[] = __FILE__;
- #endif
-
- #ifdef UNICODE
- #define CSVDOC_SURROUNDHOTSPOTS L"Hotspots"
- #else
- #define CSVDOC_SURROUNDHOTSPOTS "Hotspots"
- #endif
-
-
- /////////////////////////////////////////////////////////////////////////////
- // CSVViewerDoc
-
- IMPLEMENT_DYNCREATE(CSVViewerDoc, CDocument)
-
- BEGIN_MESSAGE_MAP(CSVViewerDoc, CDocument)
- //{{AFX_MSG_MAP(CSVViewerDoc)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CSVViewerDoc construction/destruction
-
- CSVViewerDoc::CSVViewerDoc()
- {
- m_pISurround = NULL;
- m_pIStorage = NULL;
- }
-
- BOOL CSVViewerDoc::OnOpenDocument(LPCTSTR lpszPathName)
- {
- HRESULT hr;
- IStorage *pIStorage = NULL;
- IStream *pStream = NULL;
- IStream *pHotspotStream = NULL;
- CAnyString imageStream = CSVDOC_SURROUNDIMAGE;
- CAnyString hotspotStream = CSVDOC_SURROUNDHOTSPOTS;
-
- // free up the current surround video image
- DeleteContents();
- SetModifiedFlag( FALSE );
-
- // Open the file
- hr = OpenStorage( lpszPathName, &pIStorage, FALSE );
- if( FAILED(hr) )
- return FALSE;
-
- hr = pIStorage->OpenStream( imageStream, NULL, STGM_READ|STGM_SHARE_EXCLUSIVE, 0, &pStream );
- if( FAILED(hr) )
- return FALSE;
-
- // Get an ISurround using the highest color resolution we can based
- // on the display depth.
- HDC hdc = GetDC(NULL);
- hr = -1;
- if( GetDeviceCaps( hdc, BITSPIXEL ) > 8 )
- hr = PanoramicSurroundFromStream( pStream, 24, &m_pISurround );
- if( FAILED(hr) )
- hr = PanoramicSurroundFromStream( pStream, 8, &m_pISurround );
- ReleaseDC(NULL, hdc);
-
- if( FAILED(hr) )
- AfxThrowFileException(0);
-
- // Read in any hotspots
- // NOTE: It's OK if there is not hotspot stream so only complain if we
- // find one and can't read it
- hr = pIStorage->OpenStream( hotspotStream, NULL, STGM_READ|STGM_SHARE_EXCLUSIVE, 0, &pHotspotStream );
- if( !FAILED(hr) )
- {
- hr = m_hotspots.Read( pHotspotStream );
- if( FAILED(hr) )
- AfxThrowFileException(0);
- }
- else hr = S_OK;
-
- m_pIStorage = pIStorage;
-
- if( pStream )
- pStream->Release();
-
- if( pHotspotStream )
- pHotspotStream->Release();
-
- return m_pISurround != NULL;
- }
-
- void CSVViewerDoc::OnCloseDocument()
- {
- DeleteContents();
- CDocument::OnCloseDocument();
- }
-
- BOOL CSVViewerDoc::OnSaveDocument( LPCTSTR pszPathName )
- {
- HRESULT hr;
- LPSTORAGE pIStorage;
- LPSTREAM pIStream;
- STATSTG stg;
- CAnyString storageName;
- CAnyString pathName = pszPathName;
- CAnyString hotspotStream = CSVDOC_SURROUNDHOTSPOTS;
-
- // We should have a storage at this point
- if( m_pIStorage == NULL )
- {
- AfxMessageBox( AFX_MSG_CANTSAVEEMPTY );
- return FALSE;
- }
-
- // Get status of current storage
- hr = m_pIStorage->Stat( &stg, STATFLAG_DEFAULT );
- if( FAILED(hr) )
- return FALSE;
-
- // If it's not the same file, create a new one and copy to it
- storageName = stg.pwcsName;
- if( stricmp( storageName, pathName ) != NULL )
- {
- if( !IsOverwriteable( pathName ) )
- return FALSE;
-
- hr = OpenStorage( pathName, &pIStorage, TRUE );
- if( FAILED(hr) )
- return FALSE;
-
- // Copy the entire storage to the new file
- // (we will shortly replace the Hotspot stream)
- BeginWaitCursor();
- m_pIStorage->CopyTo( NULL, NULL, NULL, pIStorage );
- EndWaitCursor();
-
- // We are using the new file now, release the old one
- m_pIStorage->Release();
- m_pIStorage = pIStorage;
-
- }
-
- // Create (or Overwrite) hotspot stream
- hr = m_pIStorage->CreateStream( hotspotStream,
- STGM_DIRECT|STGM_WRITE|STGM_SHARE_EXCLUSIVE|STGM_CREATE,
- 0, 0, &pIStream );
- if( FAILED(hr) )
- return FALSE;
-
- BeginWaitCursor();
- hr = m_hotspots.Write( pIStream );
- pIStream->Release();
- EndWaitCursor();
-
- if( FAILED(hr) )
- {
- AfxMessageBox( AFX_MSG_CANTSAVEDOCUMENT );
- return FALSE;
- }
- else
- this->SetModifiedFlag( FALSE );
-
- return !FAILED(hr);
- }
-
- HRESULT CSVViewerDoc::OpenStorage( LPCTSTR pszPathName, LPSTORAGE* ppIStorage, BOOL bCreate )
- {
- HRESULT hr;
- CLSID clsid;
- CAnyString pathName = pszPathName;
-
- *ppIStorage = NULL;
-
- if( bCreate )
- {
- // Create an empty document file, blow away any old one
- hr = StgCreateDocfile( pathName,
- STGM_READWRITE|STGM_SHARE_EXCLUSIVE|STGM_CREATE,
- 0, ppIStorage );
- if( FAILED(hr) )
- {
- AfxMessageBox( AFX_MSG_UNABLETOCREATE );
- return hr;
- }
-
- // Write the class ID
- hr = WriteClassStg( *ppIStorage, CLSID_CSVDoc );
-
- if( FAILED(hr) )
- return hr;
- }
-
- else
- {
- // Open the root storage (file)
- hr = StgOpenStorage( pathName, NULL,
- STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE,
- NULL, 0, ppIStorage );
- if( FAILED(hr) )
- {
- AfxMessageBox( AFX_MSG_UNABLETOREADFILE );
- return hr;
- }
-
- // Make sure it's one of ours
- hr = ReadClassStg( *ppIStorage, &clsid );
- if( FAILED(hr) )
- {
- AfxMessageBox( AFX_MSG_UNABLETOREADFILE );
- return hr;
- }
- else if( clsid != CLSID_CSVDoc )
- {
- AfxMessageBox( AFX_MSG_INVALIDFILE );
- return STG_E_OLDFORMAT;
- }
- }
-
- return S_OK;
- }
-
- BOOL CSVViewerDoc::IsOverwriteable( LPCTSTR pszPathName )
- {
- HRESULT hr;
- LPSTORAGE pIStorage;
- CLSID clsid;
- CString msg;
- CAnyString pathName = pszPathName;
-
- hr = StgIsStorageFile( pathName );
- if( hr == STG_E_FILENOTFOUND )
- {
- return TRUE;
- }
-
- if( hr == S_OK )
- {
- // Open the root storage (file)
- hr = StgOpenStorage( pathName, NULL,
- STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE,
- NULL, 0, &pIStorage );
- if( FAILED(hr) )
- {
- AfxMessageBox( AFX_MSG_UNABLETOREADFILE );
- return FALSE;
- }
-
- // Make sure it's one of ours
- hr = ReadClassStg( pIStorage, &clsid );
-
- // Close it
- pIStorage->Release();
-
- if( FAILED(hr) )
- {
- AfxMessageBox( AFX_MSG_UNABLETOREADFILE );
- return FALSE;
- }
- else if( clsid == CLSID_CSVDoc )
- return TRUE;
- }
-
- AfxFormatString1( msg, AFX_MSG_OVERWRITE, pathName );
- return IDOK == AfxMessageBox( msg, MB_ICONEXCLAMATION|MB_OKCANCEL );
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CSVViewerDoc diagnostics
-
- #ifdef _DEBUG
- void CSVViewerDoc::AssertValid() const
- {
- CDocument::AssertValid();
- }
-
- void CSVViewerDoc::Dump(CDumpContext& dc) const
- {
- CDocument::Dump(dc);
- }
- #endif //_DEBUG
-
- CView* CSVViewerDoc::GetSVView(void)
- {
- // Loop through views looking for CSVViewerView,
- POSITION pos = GetFirstViewPosition();
- while(pos != NULL)
- {
- CView* pView = GetNextView(pos);
- if(pView->IsKindOf(RUNTIME_CLASS(CSVViewerView)))
- return pView;
- }
-
- return NULL;
- }
-
- void CSVViewerDoc::DeleteContents()
- {
- // Reset the CSVViewerView before attempting to reset myself
- CSVViewerView* pSVViewerView = (CSVViewerView*)GetSVView();
- if(NULL != pSVViewerView)
- pSVViewerView->Reset();
-
- // Delete all hotspots
- m_hotspots.ResetContent();
-
- // release our interfaces
- if (m_pISurround)
- {
- m_pISurround->Release();
- m_pISurround = NULL;
- }
-
- if (m_pIStorage)
- {
- m_pIStorage->Release();
- m_pIStorage = NULL;
- }
-
- }
-
-
-