home *** CD-ROM | disk | FTP | other *** search
/ Using Visual Basic 5 (Platinum Edition) / vb5.iso / ACTIVEX / SRDVID / DATA.1 / svdoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-10  |  5.7 KB  |  228 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 __SVVIEWER_H__
  24.     #include "SVViewer.h"
  25. #endif
  26.  
  27. #ifndef __SVVIEW_H__
  28.     #include "SVView.h"
  29. #endif
  30.  
  31. #ifndef __SVDOC_H__
  32.     #include "SVDoc.h"
  33. #endif
  34.  
  35. #ifndef __CASTRING_H__
  36.     #include "CAString.h"
  37. #endif
  38.  
  39. #ifdef _DEBUG
  40. #undef THIS_FILE
  41. static char BASED_CODE THIS_FILE[] = __FILE__;
  42. #endif
  43.  
  44. /////////////////////////////////////////////////////////////////////////////
  45. // CSVViewerDoc
  46.  
  47. IMPLEMENT_DYNCREATE(CSVViewerDoc, CDocument)
  48.  
  49. BEGIN_MESSAGE_MAP(CSVViewerDoc, CDocument)
  50.     //{{AFX_MSG_MAP(CSVViewerDoc)
  51.     //}}AFX_MSG_MAP
  52. END_MESSAGE_MAP()
  53.  
  54. /////////////////////////////////////////////////////////////////////////////
  55. // CSVViewerDoc construction/destruction
  56.  
  57. CSVViewerDoc::CSVViewerDoc()
  58. {
  59.     m_pISurround = NULL;
  60.     m_pIStorage = NULL;
  61. }
  62.  
  63. BOOL CSVViewerDoc::OnOpenDocument(LPCTSTR lpszPathName)
  64. {
  65.     HRESULT          hr;
  66.     IStorage         *pIStorage = NULL;
  67.     IStream            *pStream = NULL;
  68.     BITMAPINFOHEADER *pDib = NULL;
  69.     BYTE            *pvBits = NULL;
  70.     CAnyString        fileName = lpszPathName;
  71.     CAnyString        storageName = CSVDOC_SURROUNDIMAGE;
  72.  
  73.     // free up the current surround video image
  74.     DeleteContents();
  75.     SetModifiedFlag( FALSE );
  76.  
  77.     TRY
  78.     {
  79.         // See if it's a compound file...
  80.         hr = StgIsStorageFile( fileName );
  81.         if( hr == STG_E_FILENOTFOUND )
  82.         {
  83.             AfxThrowFileException(0);
  84.         }
  85.         // ...it's not a compound file. See if it's a DIB...
  86.         else if( hr != S_OK )
  87.         {
  88.             pDib = (BITMAPINFOHEADER*)GlobalAllocPtr( GHND, sizeof(BITMAPINFOHEADER) +
  89.                                                       256*sizeof(RGBQUAD) );
  90.             if( pDib == NULL )
  91.                 AfxThrowMemoryException();
  92.  
  93.             // See if it's a Dib we can treat as an Surround Video Image
  94.             if( ReadDib( fileName, (BITMAPINFO*)pDib, &pvBits ) != DIB_OK )
  95.                 AfxThrowFileException(0);
  96.  
  97.             // Create a Surround from the Dib 
  98.             hr = PanoramicSurroundFromDIB( pDib,
  99.                                            DibColors(pDib),
  100.                                            pvBits,
  101.                                            DibHeight(pDib) / 2,
  102.                                            MAX_ARCSECONDS,
  103.                                            &m_pISurround);
  104.  
  105.             if( FAILED(hr) ) AfxThrowFileException(0);
  106.  
  107.             return m_pISurround != NULL;
  108.         }
  109.  
  110. // NOTE WELL: The following section shows two ways of creating an ISurround from a file.
  111. //              The first uses PanoramicSurroundFromFile() which just takes a file name.
  112. //              The second uses PanoramicSurroundFromStream() which uses a stream.
  113.  
  114. //#define USE_PanoramicSurroundFromFile // Comment this line out to use PanoramicSurroundFromStream()
  115.  
  116. #ifdef USE_PanoramicSurroundFromFile
  117.  
  118.         // Get an ISurround using the highest color resolution we can based
  119.         // on the display depth.
  120.         HDC hdc = GetDC(NULL);
  121.         hr = -1;
  122.         if( GetDeviceCaps( hdc, BITSPIXEL ) > 8 )
  123.                 hr = PanoramicSurroundFromFile( fileName, 24, &m_pISurround );
  124.         if( FAILED(hr) )
  125.                 hr = PanoramicSurroundFromFile( fileName, 8, &m_pISurround );
  126.         ReleaseDC(NULL, hdc);
  127.  
  128. #else
  129.         // open the storage with the image filename    
  130.         hr = StgOpenStorage( fileName, NULL, STGM_READ|STGM_SHARE_EXCLUSIVE, 
  131.                              0, 0, &pIStorage);
  132.         
  133.         if( FAILED(hr) )
  134.             AfxThrowFileException(0);
  135.  
  136.         hr = pIStorage->OpenStream ( storageName, NULL, STGM_READ|STGM_SHARE_EXCLUSIVE,
  137.                                      0, &pStream );
  138.         if( FAILED(hr) )
  139.             AfxThrowFileException(0);
  140.  
  141.          // Get an ISurround using the highest color resolution we can based
  142.          // on the display depth.
  143.         HDC hdc = GetDC(NULL);
  144.         hr = -1;
  145.         if( GetDeviceCaps( hdc, BITSPIXEL ) > 8 )
  146.                 hr = PanoramicSurroundFromStream( pStream, 24, &m_pISurround );
  147.         if( FAILED(hr) )
  148.                 hr = PanoramicSurroundFromStream( pStream, 8, &m_pISurround );
  149.         ReleaseDC(NULL, hdc);
  150.  
  151. #endif // #ifdef USE_PanoramicSurroundFromFile
  152.         
  153.         if( FAILED(hr) )
  154.             AfxThrowFileException(0);
  155.     }
  156.     CATCH_ALL(e)
  157.     {
  158.         if( pDib != NULL )
  159.             GlobalFreePtr( pDib);
  160.         
  161.         if( pvBits != NULL )
  162.             GlobalFreePtr( pvBits );
  163.  
  164.         AfxMessageBox( AFX_MSG_CANTREADFILE );
  165.     }
  166.     END_CATCH_ALL
  167.  
  168.     m_pIStorage = pIStorage;
  169.  
  170.     if (pStream)
  171.         pStream->Release();
  172.  
  173.     return m_pISurround != NULL;
  174. }
  175.  
  176. /////////////////////////////////////////////////////////////////////////////
  177. // CSVViewerDoc diagnostics
  178.  
  179. #ifdef _DEBUG
  180. void CSVViewerDoc::AssertValid() const
  181. {
  182.     CDocument::AssertValid();
  183. }
  184.  
  185. void CSVViewerDoc::Dump(CDumpContext& dc) const
  186. {
  187.     CDocument::Dump(dc);
  188. }
  189. #endif //_DEBUG
  190.  
  191.  
  192. CView* CSVViewerDoc::GetSVView(void)
  193. {
  194.     // Loop through views looking for CSVViewerView,
  195.     POSITION pos = GetFirstViewPosition();
  196.     while(pos != NULL)
  197.     {
  198.         CView* pView = GetNextView(pos);
  199.         if(pView->IsKindOf(RUNTIME_CLASS(CSVViewerView)))
  200.             return pView;
  201.     }
  202.  
  203.     return NULL;
  204. }
  205.  
  206. void CSVViewerDoc::DeleteContents()
  207. {
  208.     // Reset the CSVViewerView before attempting to reset myself
  209.     CSVViewerView* pSVViewerView = (CSVViewerView*)GetSVView();
  210.     if(NULL != pSVViewerView)
  211.         pSVViewerView->Reset();
  212.  
  213.     // release our interfaces
  214.     if (m_pISurround)
  215.     {
  216.         m_pISurround->Release();
  217.         m_pISurround = NULL;
  218.     }
  219.  
  220.     if (m_pIStorage)
  221.     {
  222.         m_pIStorage->Release();
  223.         m_pIStorage = NULL;
  224.     }
  225. }
  226.  
  227.  
  228.