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

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. //    CHotspot.cpp : Implementation of the CHotspot 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.  
  21. #ifndef __CHOTSPOT_H__
  22.     #include "CHotspot.h"
  23. #endif
  24.  
  25. #ifndef __UTILITY_H__
  26.     #include "utility.h"
  27. #endif 
  28.  
  29. #ifdef _DEBUG
  30.     #undef THIS_FILE
  31.     static char BASED_CODE THIS_FILE[] = __FILE__;
  32. #endif
  33.  
  34. /////////////////////////////////////////////////////////////////////////////
  35. // CHotspot
  36.  
  37. CHotspot::CHotspot( int id )
  38. {
  39.     // Save hotspot ID
  40.     m_id = id;
  41.  
  42.     // Set defaults
  43.     m_name.Empty();
  44.     m_location.latitude = 0;
  45.     m_location.longitude = 0;
  46.     m_size.cx = 0;
  47.     m_size.cy = 0;
  48.     m_bLocked = FALSE;
  49. }
  50.     
  51. CHotspot::~CHotspot()
  52. {
  53. }
  54.  
  55. HRESULT CHotspot::Write( LPSTREAM pIStream )
  56. {
  57.     HRESULT hr;
  58.  
  59.     hr = WriteToStream( pIStream, m_name );
  60.     hr = WriteToStream( pIStream, &m_location );
  61.     hr = WriteToStream( pIStream, m_size );
  62.     hr = WriteToStream( pIStream, m_bLocked );
  63.     return hr;
  64. }
  65.  
  66. HRESULT CHotspot::Read( LPSTREAM pIStream )
  67. {
  68.     HRESULT hr;
  69.  
  70.     hr = ReadFromStream( pIStream, m_name );
  71.     hr = ReadFromStream( pIStream, &m_location );
  72.     hr = ReadFromStream( pIStream, m_size );
  73.     hr = ReadFromStream( pIStream, &m_bLocked );
  74.     return hr;
  75. }
  76.  
  77. // CHotspotList
  78.  
  79. CHotspotList::CHotspotList()
  80. {
  81.     m_nextID = 0;
  82. }
  83.  
  84. CHotspotList::~CHotspotList()
  85. {
  86.     ResetContent();
  87. }
  88.  
  89. // Create() - Create a hotspot
  90. CHotspot* CHotspotList::Create( int *id )
  91. {
  92.     CHotspot* pHotspot = new CHotspot( m_nextID++ );
  93.     Add( (CObject*)pHotspot );
  94.     
  95.     // See if they want the ID back
  96.     if( id != NULL )
  97.         *id = pHotspot->GetID();
  98.  
  99.     return pHotspot;
  100. }
  101.  
  102. // Draw() - Draw all Hotspots in the hotspot list
  103. void CHotspotList::Draw( CDC *pDC )
  104. {
  105.     int index;
  106.     CHotspot *pHotspot;
  107.     CRect rect;
  108.  
  109.     for( index = GetUpperBound(); index >= 0; index-- )
  110.     {
  111.         pHotspot = (CHotspot *)GetAt(index);
  112.  
  113.         if( pHotspot != NULL )
  114.         {
  115. //            rect = pHotspot->m_rect;
  116. //            pView->DibToClient( &rect );
  117.             
  118. //            if( pDC->RectVisible( &rect ) )
  119. //                pDC->DrawFocusRect( &rect );
  120.         }
  121.     }
  122. }
  123.  
  124. // PtInHotspot() - Return the hotspot at the given point, if any
  125. CHotspot *CHotspotList::PtInHotspot( CPoint point )
  126. {
  127.     int index;
  128.     CHotspot *pHotspot;
  129.  
  130.     for( index = GetUpperBound(); index >= 0; index-- )
  131.     {
  132.         pHotspot = (CHotspot *)GetAt(index);
  133.  
  134.         if( pHotspot != NULL )
  135.         {
  136. //            if( pHotspot->m_rect.PtInRect( point ) )
  137.                 return pHotspot;
  138.         }
  139.     }
  140.     return (CHotspot *)NULL;
  141. }
  142.  
  143. HRESULT CHotspotList::Write( LPSTREAM pIStream )
  144. {
  145.     int index;
  146.     CHotspot *pHotspot;
  147.     HRESULT hr;
  148.  
  149.     WriteToStream( pIStream, this->GetSize() );
  150.     
  151.     for( index = GetUpperBound(); index >= 0; index-- )
  152.     {
  153.         pHotspot = (CHotspot *)GetAt(index);
  154.  
  155.         if( pHotspot != NULL )
  156.         {
  157.             hr = pHotspot->Write( pIStream );
  158.             if( FAILED(hr) )
  159.                 return hr;
  160.         }
  161.     }
  162.     
  163.     return S_OK;
  164. }
  165.  
  166. HRESULT CHotspotList::Read( LPSTREAM pIStream )
  167. {
  168.     int index, count;
  169.     CHotspot *pHotspot;
  170.     HRESULT hr;
  171.  
  172.     ResetContent();
  173.  
  174.     // Read number of hotspots
  175.     hr = ReadFromStream( pIStream, &count );
  176.     if( FAILED(hr) )
  177.         return hr;
  178.  
  179.     for( index = 0; index < count; index++ )
  180.     {
  181.         pHotspot = Create();
  182.         hr = pHotspot->Read( pIStream );
  183.         if( FAILED(hr) )
  184.         {
  185.             delete pHotspot;
  186.             return hr;
  187.         }
  188.     }
  189.  
  190.     return S_OK;
  191. }
  192. // ResetContent() - Delete all Items in the list
  193. void CHotspotList::ResetContent()
  194. {
  195.     int index;
  196.     CHotspot* pHotspot;
  197.  
  198.     for( index = GetUpperBound(); index >= 0; index-- )
  199.     {
  200.         pHotspot = (CHotspot*)GetAt(index);
  201.  
  202.         if( pHotspot != NULL )
  203.             delete pHotspot;
  204.     }
  205.     RemoveAll();
  206. }
  207.  
  208. // IsValidEntry() - Is the given object in the list
  209. BOOL CHotspotList::IsValidEntry( CHotspot* pHotspot )
  210. {
  211.     int index;
  212.     
  213.     for( index = GetUpperBound(); index >= 0; index-- )
  214.     {
  215.         if( pHotspot == (CHotspot *)GetAt(index) )
  216.             return TRUE;
  217.     }
  218.     
  219.     return FALSE;
  220. }
  221.  
  222. // Delete() - Delete Item given it's ID
  223. //
  224. // NOTE WELL: All indexes in the list change after calling this function
  225. //              But this doesn't matter as we use the ID anyway
  226. //
  227. void CHotspotList::Delete( int id )
  228. {
  229.     int index;
  230.     CHotspot* pHotspot;
  231.  
  232.     if( ( index = GetIndexFromId( id ) ) == -1 )
  233.         return;
  234.     
  235.     pHotspot = (CHotspot *)GetAt(index);
  236.     RemoveAt( index );
  237.     delete pHotspot;
  238. }
  239.  
  240. CHotspot* CHotspotList::GetById( int id )
  241. {
  242.     int index;
  243.     CHotspot* pHotspot;
  244.     
  245.     for( index = GetUpperBound(); index >= 0; index-- )
  246.     {
  247.         pHotspot = (CHotspot*)GetAt(index);
  248.  
  249.         if( pHotspot != NULL )
  250.             if( pHotspot->m_id == id )
  251.                 return pHotspot;
  252.     }
  253.     
  254.     return (CHotspot*)NULL;
  255. }
  256.  
  257. CHotspot* CHotspotList::GetByIndex( int index )
  258. {
  259.     if( index < 0 || index > GetUpperBound() )
  260.         return (CHotspot*)NULL;
  261.     
  262.     return (CHotspot*)GetAt(index);
  263. }
  264.  
  265. int CHotspotList::GetIndexFromId( int id )
  266. {
  267.     int index;
  268.     CHotspot* pHotspot;
  269.     
  270.     for( index = GetUpperBound(); index >= 0; index-- )
  271.     {
  272.         if( ( pHotspot = (CHotspot*)GetAt(index) ) != NULL )
  273.         {
  274.             if( pHotspot->m_id == id )
  275.                 return index;
  276.         }
  277.     }
  278.     
  279.     return -1;
  280. }
  281.  
  282. int CHotspotList::GetIdFromIndex( int index )
  283. {
  284.     CHotspot* pHotspot = GetByIndex( index );
  285.     if( pHotspot != NULL )
  286.         return pHotspot->GetID();
  287.     
  288.     return -1;
  289. }
  290.