home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / feimages.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  8.5 KB  |  230 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 "edt.h"
  21. #include "prefapi.h"
  22.  
  23. //    Structures
  24. //
  25. // Yea I know pImageURL has no size!
  26. //   Image strings are dynamically allocated starting there
  27. typedef struct _FE_CopyImageData {
  28.     int     iSize;              // size of all data, including variable length strings
  29.     Bool    bIsMap;             // For server side maps
  30.     int32   iWidth;             // Fixed size data correspond to fields in LO_ImageDataStruct
  31.     int32   iHeight;            //   and EDT_ImageData
  32.     int32   iHSpace;
  33.     int32   iVSpace;
  34.     int32   iBorder;
  35.     int     iLowResOffset;      // Offsets into string_data. If 0, string is NULL (not used)
  36.     int     iAltOffset;
  37.     int     iAnchorOffset;      // HREF in image
  38.     int     iExtraHTML_Offset;  // Extra HTML (stored in CImageElement)
  39. #pragma warning( disable: 4200)
  40.     char    pImageURL[];     // Append all variable-length strings starting here
  41. #pragma warning( default: 4200)
  42. } FE_CopyImageData;
  43.  
  44.  
  45. // TODO: Should we include other image_attr fields? (e.g.: alignment, attrmask)
  46.  
  47. void FE_ShiftImage(MWContext *context, LO_ImageStruct *lo_image)    {
  48. }
  49.  
  50. // free the mem using correct conditional call
  51. inline void fe_PtrFreeBits(void XP_HUGE *pBits)
  52. {
  53.     if (pBits) {
  54. #ifdef XP_WIN16
  55.         _hfree(pBits);
  56. #else
  57.         free(pBits);
  58. #endif                           
  59.     }
  60. }
  61.  
  62. // Dragged Image can only be dropped in Composer
  63. #ifdef EDITOR
  64. // Fill our structure with data about an image
  65. //   for drag n drop and copying to clipboard
  66. HGLOBAL WFE_CreateCopyImageData(MWContext *pMWContext, LO_ImageStruct *pImage)
  67. {
  68.     if( pMWContext == NULL ||
  69.         pImage == NULL ||
  70.         pImage->image_url == NULL) {
  71.         return NULL;
  72.     }
  73.  
  74.     char *pImageURL = NULL;
  75.     char *pExtraHTML = EDT_GetExtraHTML_FromImage(pImage);
  76.  
  77.     // Lifted from GetImageHref() in POPUP.CPP:
  78.     //    Check for that funky internal-external-reconnect jazz.
  79.     char *pReconnect = "internal-external-reconnect:";
  80.     // Don't use anything else that starts with this:
  81.     // TODO: CHANGE THIS TO TEST FOR  pImage->bIsInternalImage AFTER lloyd IMPLEMENTS THAT
  82.     char *pInternal = "internal-";
  83.  
  84.     if(strncmp((const char *)pImage->image_url, pReconnect, 28) == 0) {
  85.     //    Need to take the URL off the history.
  86.         //MWContext *pContext = GetContext()->GetContext();
  87.         History_entry *pHistEnt = SHIST_GetCurrent(&pMWContext->hist);
  88.         if ( pHistEnt && pHistEnt->address ){
  89.             pImageURL = pHistEnt->address;
  90.         }
  91.     }
  92.     else if(strncmp((const char *)pImage->image_url, pInternal, 9) == 0) {
  93.         return NULL;
  94.     } else {
  95.         pImageURL = (char *)pImage->image_url;
  96.     }
  97.  
  98.     if(pImageURL && XP_STRLEN(pImageURL) > 0) {
  99.         // We have an URL, the most important part,
  100.         //   but save all the data in our drag structure
  101.         int iLowResOffset = 0;
  102.         int iAltOffset = 0;
  103.         int iAnchorOffset = 0;
  104.         int iExtraHTML_Offset = 0;
  105.  
  106.         // First calculate allocation size and offsets for the strings
  107.         // Offset to pImageURL = 1 past end of struct size,
  108.         //  so offset to next string is 1 past the '\0' of pImageURL
  109.         int iOffset = sizeof( FE_CopyImageData ) + XP_STRLEN(pImageURL) + 2;
  110.  
  111.         if ( pImage->lowres_image_url ){
  112.             iLowResOffset = iOffset;
  113.             // Add size of string + 1 for '\0'
  114.             iOffset += XP_STRLEN((char*)pImage->lowres_image_url) + 1;
  115.         }
  116.         if ( pImage->alt ){
  117.             iAltOffset = iOffset;
  118.             iOffset += XP_STRLEN((char*)pImage->alt) + 1;
  119.         }
  120.         if ( pImage->anchor_href ){
  121.             if ( pImage->anchor_href->anchor ){
  122.                 iAnchorOffset = iOffset;
  123.                 iOffset += XP_STRLEN((char*)pImage->anchor_href->anchor) + 1;
  124.             }
  125.         }
  126.         if ( pExtraHTML ){
  127.             // Get Extra HTML if it exists
  128.             iExtraHTML_Offset = iOffset;
  129.             iOffset += XP_STRLEN(pExtraHTML) + 1;
  130.         }
  131.         // Total size = last offset calculated + extra '\0'
  132.         int iDataSize = iOffset + 1;
  133.  
  134.         // Clipboard data should have GMEM_MOVEABLE,
  135.         //  and it should be OK for drag and drop too?
  136.         HGLOBAL hImageData = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE | GMEM_ZEROINIT, iDataSize);
  137.         if(hImageData){
  138.             FE_CopyImageData * pDragData = (FE_CopyImageData*)GlobalLock(hImageData);
  139.  
  140.             // Not used, but its good form to include size of all data
  141.             pDragData->iSize  = iDataSize;
  142.             pDragData->iWidth = pImage->width;
  143.             pDragData->iHeight = pImage->height;
  144.             pDragData->iHSpace = pImage->border_horiz_space;
  145.             pDragData->iVSpace = pImage->border_vert_space;
  146.             pDragData->iBorder = pImage->border_width;
  147.             XP_STRCPY(pDragData->pImageURL, pImageURL);
  148.  
  149.             if ( pImage->image_attr && (pImage->image_attr->attrmask & LO_ATTR_ISMAP) ){
  150.                 pDragData->bIsMap = TRUE;
  151.             }
  152.  
  153.             if (iLowResOffset){
  154.                 pDragData->iLowResOffset = iLowResOffset;
  155.                 XP_STRCPY( ((char*)pDragData)+iLowResOffset, 
  156.                            (char*)pImage->lowres_image_url );
  157.             }
  158.             if (iAltOffset){
  159.                 pDragData->iAltOffset = iAltOffset; 
  160.                 XP_STRCPY( ((char*)pDragData)+iAltOffset,
  161.                            (char*)pImage->alt );
  162.             }
  163.             if (iAnchorOffset){
  164.                 pDragData->iAnchorOffset = iAnchorOffset; 
  165.                 XP_STRCPY( ((char*)pDragData)+iAnchorOffset,
  166.                            (char*)pImage->anchor_href->anchor);
  167.             }
  168.             if (iExtraHTML_Offset){
  169.                 pDragData->iExtraHTML_Offset = iExtraHTML_Offset;
  170.                 XP_STRCPY( ((char*)pDragData)+iExtraHTML_Offset, pExtraHTML );
  171.                 // Done with string - release it
  172.                 XP_FREE(pExtraHTML);
  173.             }
  174.  
  175.             GlobalUnlock(hImageData);
  176.  
  177.             return hImageData;
  178.             }
  179.         }
  180.     return NULL;
  181. }
  182.  
  183. void WFE_DragDropImage(HGLOBAL h, MWContext *pMWContext)
  184. {
  185.     if( h ){
  186.         FE_CopyImageData* pDragData = (FE_CopyImageData*) GlobalLock( h );
  187.         if ( pDragData && XP_STRLEN(pDragData->pImageURL) > 0 ){
  188.             EDT_ImageData* pEdtData = EDT_NewImageData();
  189.         
  190.             // Get the fixed-size data
  191.             pEdtData->bIsMap = pDragData->bIsMap;
  192.             pEdtData->iWidth = pDragData->iWidth;
  193.             pEdtData->iHeight = pDragData->iHeight;
  194.             pEdtData->iHSpace = pDragData->iHSpace;
  195.             pEdtData->iVSpace = pDragData->iVSpace;
  196.             pEdtData->iBorder = pDragData->iBorder;
  197.  
  198.             // Kludge: Sitemanager gives us D:/foo/bar 
  199.             //  so convert to URL
  200.             CString csURL;
  201.             WFE_ConvertFile2Url(csURL, pDragData->pImageURL);
  202.  
  203.             pEdtData->pSrc = XP_STRDUP(csURL);
  204.  
  205.             // Get variable-allocated strings
  206.             if ( pDragData->iLowResOffset ){
  207.                 pEdtData->pLowSrc =
  208.                         XP_STRDUP(((char*)pDragData)+pDragData->iLowResOffset);
  209.             }
  210.             if ( pDragData->iAltOffset ){
  211.                 pEdtData->pAlt = XP_STRDUP(((char*)pDragData)+pDragData->iAltOffset);
  212.             }
  213.             if ( pDragData->iAnchorOffset ){
  214.             }
  215.             if ( pDragData->iExtraHTML_Offset ){
  216.                 pEdtData->pExtra =
  217.                         XP_STRDUP(((char*)pDragData)+pDragData->iExtraHTML_Offset);
  218.             // HARDTS: USEMAP data is now in pExtra, fix for bug 73283
  219.             }
  220.             int bKeepImages;
  221.             PREF_GetBoolPref("editor.publish_keep_images",&bKeepImages);
  222.  
  223.             EDT_InsertImage(pMWContext, pEdtData,bKeepImages);
  224.             EDT_FreeImageData(pEdtData);
  225.         }
  226.         GlobalUnlock( h );
  227.     }
  228. }
  229. #endif /* EDITOR */    
  230.