home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / cxicon.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  6.4 KB  |  250 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. #include "stdafx.h"
  20. #include "cxicon.h"
  21. #include "feimage.h"
  22. #include "winproto.h"
  23.  
  24. HBITMAP NSNavCenterImage::m_hBadImageBitmap = NULL;
  25. int NSNavCenterImage::refCount = 0;
  26.  
  27. NSNavCenterImage::NSNavCenterImage(char * url, CIconCallbackInfo* iconCallbackInfo)
  28. {
  29.     pUrl = _strdup( url);
  30.     bmpInfo = NULL;
  31.     m_bCompletelyLoaded = FALSE;
  32.     bits = 0;
  33.     maskbits = 0;
  34.     m_BadImage = FALSE;
  35.     resourceList.AddHead(iconCallbackInfo);  // list to store all the resources waiting on this image
  36.     NSNavCenterImage::refCount++;
  37.     iconContext = NULL;
  38.     ProcessIcon();
  39. }
  40.  
  41. NSNavCenterImage::~NSNavCenterImage()
  42. {
  43.     XP_FREE( bmpInfo);
  44.     CDCCX::HugeFree(bits);
  45.     CDCCX::HugeFree(maskbits);
  46.     free(pUrl);
  47.     
  48.     NSNavCenterImage::refCount--;
  49.     if (refCount == 0)
  50.     {
  51.         if (NSNavCenterImage::m_hBadImageBitmap)
  52.             VERIFY(::DeleteObject(NSNavCenterImage::m_hBadImageBitmap));
  53.     }
  54. }
  55.  
  56. void NSNavCenterImage::DestroyContext()
  57. {
  58.     iconContext->DeleteContextDC();
  59.     iconContext->DestroyContext(); // now destroy self.
  60.     iconContext = NULL;
  61. }
  62.  
  63. BOOL NSNavCenterImage::CompletelyLoaded()
  64. {
  65.     if (m_bCompletelyLoaded)
  66.     {
  67.         if (iconContext)
  68.             DestroyContext();
  69.         return TRUE;
  70.     }
  71.  
  72.     return FALSE;
  73. }
  74.  
  75. void Icon_GetUrlExitRoutine(URL_Struct *pUrl, int iStatus, MWContext *pContext)  
  76. {
  77.     //    Report any errors.
  78.     if(iStatus < 0 && pUrl->error_msg != NULL)    
  79.     {
  80.         void* pData;
  81.         NSNavCenterImage* theImage = NULL;
  82.         if (CHTFEData::m_CustomURLCache.Lookup(pUrl->address, pData))
  83.             theImage = (NSNavCenterImage*)pData;
  84.  
  85.         if (theImage) 
  86.         {  // Since we cannot load this url, replace it with a bad image.
  87.             theImage->maskbits = 0;
  88.             theImage->bits = 0;
  89.             theImage->m_BadImage = TRUE;
  90.             if (!NSNavCenterImage::m_hBadImageBitmap)
  91.                 NSNavCenterImage::m_hBadImageBitmap = ::LoadBitmap(AfxGetResourceHandle(), MAKEINTRESOURCE(IDB_IMAGE_BAD));
  92.             theImage->CompleteCallback();
  93.         }
  94.     }
  95. }
  96.  
  97. static BOOL ValidNSBitmapFormat(char* extension)
  98. {
  99.     BOOL val = FALSE;
  100.     CString theFormat = "image/";
  101.     if(!extension)
  102.         return val;
  103.     theFormat += &extension[1];
  104.     if (theFormat.CompareNoCase(IMAGE_GIF) == 0)
  105.         val = TRUE;
  106.     else if (theFormat.CompareNoCase(IMAGE_JPG) == 0)
  107.         val = TRUE;
  108.     else if (theFormat.CompareNoCase(IMAGE_PJPG) == 0)
  109.         val = TRUE;
  110.     else if (theFormat.CompareNoCase(IMAGE_PPM) == 0)
  111.         val = TRUE;
  112.     else if (theFormat.CompareNoCase(IMAGE_PNG) == 0)
  113.         val = TRUE;
  114.     else if (theFormat.CompareNoCase(IMAGE_XBM) == 0)
  115.         val = TRUE;
  116.     else if (theFormat.CompareNoCase(IMAGE_XBM2) == 0)
  117.         val = TRUE;
  118.     else if (theFormat.CompareNoCase(IMAGE_XBM3) == 0)
  119.         val = TRUE;
  120.     return val;
  121. }
  122.  
  123. void NSNavCenterImage::ProcessIcon() 
  124. {
  125.     char *ext = FE_FindFileExt(pUrl);
  126.     if (ValidNSBitmapFormat(ext)) 
  127.     {
  128.         // If there is no context, create one for processing the image.
  129.         hSubDC = ::CreateCompatibleDC(NULL);
  130.  
  131.         if (iconContext == NULL)
  132.         {
  133.             iconContext = new CXIcon(this);
  134.             iconContext->SubstituteDC(hSubDC);
  135.             iconContext->Initialize(FALSE, NULL, FALSE);
  136.             iconContext->SetUseDibPalColors(FALSE);
  137.         }
  138.  
  139.         //  Ask for this via client pull.
  140.         //  We may be in the call stack of the image lib, and doing
  141.         //  lots of fast small get urls causes it to barf due
  142.         //  to list management not being reentrant.
  143.         FEU_ClientPull(iconContext->GetContext(), 0, NET_CreateURLStruct(pUrl, NET_DONT_RELOAD), FO_CACHE_AND_PRESENT, FALSE);
  144.     }
  145.     else 
  146.     { // handle window internal format BMP
  147.         CString extension = ext;
  148.         if (extension.CompareNoCase(".bmp")) 
  149.         {
  150.         }
  151.         else  
  152.         {
  153.             // TODO: Error handling here, unknow bitmap format.
  154.         }
  155.     
  156.     }
  157. }
  158.  
  159. void NSNavCenterImage::CompleteCallback()
  160. {
  161.     m_bCompletelyLoaded = TRUE;
  162.     while (!resourceList.IsEmpty())
  163.     {
  164.         CIconCallbackInfo* callback = (CIconCallbackInfo*)(resourceList.RemoveHead());
  165.         callback->pObject->LoadComplete(callback->pResource);
  166.         delete callback;
  167.     }
  168. }
  169.  
  170. CXIcon::CXIcon(NSNavCenterImage* theImage)
  171. {
  172.     MWContext *pContext = GetContext();
  173.     m_cxType = IconCX;
  174.     pContext->type = MWContextIcon;
  175.     m_MM = MM_TEXT;
  176.     m_hDC = 0;
  177.     m_icon = theImage;
  178.     m_image = NULL;
  179.     m_mask = NULL;
  180. }
  181.  
  182. CXIcon::~CXIcon()
  183. {
  184. }
  185.  
  186. BITMAPINFO * CXIcon::NewPixmap(NI_Pixmap *pImage, BOOL isMask)
  187. {
  188.     // remember which bitmap we have so we can get the bits later in imageComplete.
  189.     if (isMask)
  190.         m_mask = pImage;
  191.     else m_image = pImage;
  192.     
  193.     return CDCCX::NewPixmap(pImage, isMask);
  194. }
  195.  
  196.  
  197. void CXIcon::ImageComplete(NI_Pixmap* image)
  198. {
  199.     FEBitmapInfo *imageInfo;
  200.     imageInfo = (FEBitmapInfo*) image->client_data;
  201.     BITMAPINFOHEADER* header = (BITMAPINFOHEADER*)imageInfo->bmpInfo;
  202.     if (image == m_image)
  203.     {
  204.         int nColorTable;
  205.         if (GetBitsPerPixel() == 16 || GetBitsPerPixel() == 32)
  206.             nColorTable = 3;
  207.         else if (GetBitsPerPixel() < 16)
  208.             nColorTable = 1 << GetBitsPerPixel();
  209.         else {
  210.             ASSERT(GetBitsPerPixel() == 24);
  211.             nColorTable = 0;
  212.         }
  213.  
  214.         m_icon->bmpInfo = FillBitmapInfoHeader(image);
  215.         m_icon->bits = HugeAlloc(header->biSizeImage, 1);
  216.         memcpy( m_icon->bits, image->bits, header->biSizeImage );
  217.     }
  218.     else 
  219.     {
  220.         m_icon->maskbits = HugeAlloc(header->biSizeImage, 1);
  221.         memcpy( m_icon->maskbits, image->bits, header->biSizeImage );
  222.     }
  223.  
  224.     delete imageInfo;
  225.     image->client_data = 0;
  226.     if (m_image && m_mask) 
  227.     {
  228.         if (m_icon->maskbits && m_icon->bits) 
  229.         {
  230.             CDCCX::HugeFree(m_image->bits);
  231.             m_image->bits = 0;
  232.             CDCCX::HugeFree(m_mask->bits);
  233.             m_mask->bits = 0;
  234.             m_icon->CompleteCallback();
  235.         }
  236.     }
  237.     else 
  238.     {
  239.         // We do not have mask, so we don't need to wait for mask.
  240.         CDCCX::HugeFree(m_image->bits);
  241.         m_image->bits = 0;
  242.         m_icon->CompleteCallback();
  243.     }
  244. }
  245.     //    Don't display partial images.
  246. void CXIcon::AllConnectionsComplete(MWContext *pContext)
  247. {
  248.     CDCCX::AllConnectionsComplete(pContext);
  249. }
  250.