home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / src / EditorDrop.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  6.2 KB  |  218 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.    EditorDrop.cpp -- class definition for the editor drop class
  20.    Created: Alastair Gourlay <sgidev@netscape.com>, 1-Jan-96.
  21.  */
  22.  
  23.  
  24.  
  25. #include "EditorDrop.h"
  26. #include "xeditor.h"
  27. #include "edt.h"
  28.  
  29. #if defined(DEBUG_sgidev) || defined(DEBUG_djw)
  30. #define XDEBUG(x) x
  31. #else
  32. #define XDEBUG(x)
  33. #endif
  34.  
  35. //
  36. // XFE_EditorDrop class
  37. //
  38.  
  39. // constructor
  40.  
  41. XFE_EditorDrop::XFE_EditorDrop(Widget parent,XFE_EditorView *editorView)
  42.     : XFE_DropNetscape(parent)
  43. {
  44.     _editorView=editorView;
  45. }
  46.  
  47. XFE_EditorDrop::~XFE_EditorDrop()
  48. {
  49. }
  50.  
  51. void XFE_EditorDrop::targets()
  52. {
  53.     _numTargets=2;
  54.     _targets=new Atom[_numTargets];
  55.  
  56.     _targets[0]=_XA_NETSCAPE_URL;
  57.     _targets[1]=XA_STRING;
  58.  
  59.     acceptFileTargets();
  60. }
  61.  
  62. void XFE_EditorDrop::operations()
  63. {
  64.     // always copy - move/link irrelevant
  65.     _operations=(unsigned int)XmDROP_COPY;
  66. }
  67.  
  68.  
  69. Atom XFE_EditorDrop::acceptDrop(unsigned int dropOperation,Atom *dropTargets,unsigned int numDropTargets)
  70. {
  71.     // reject a drag from the same browser window
  72.     if (XFE_DragBase::_activeDragShell) {
  73.         Widget shell=_widget;
  74.         while (!XtIsShell(shell)) shell=XtParent(shell);
  75.         if (shell==XFE_DragBase::_activeDragShell)
  76.             return None;
  77.     }
  78.     
  79.     // continue with regular target selection
  80.     return XFE_DropNetscape::acceptDrop(dropOperation,dropTargets,numDropTargets);
  81. }
  82.  
  83.  
  84. int XFE_EditorDrop::processTargets(Atom *targets,const char **data,int numItems)
  85. {
  86.     if (!targets || !data || numItems==0)
  87.         return FALSE;
  88.  
  89.     // process dropped files/documents
  90.  
  91.     int anySuccess=FALSE;
  92.     for (int i=0;i<numItems;i++) {
  93.         if (targets[i]==None || data[i]==NULL || strlen(data[i])==0)
  94.             continue;
  95.  
  96.         // a file is just a file.. (XFE_DropNetscape parse any NetscapeURL
  97.         // or WebJumper shortcut files in _NETSCAPE_URL)
  98.         if (targets[i]==_XA_FILE_NAME) {
  99.             anySuccess |= insertFile(data[i]);
  100.         }
  101.  
  102.         // a link is just a link, unless it's a file: url, in which case
  103.         // lets import it as a file. This covers local files, viewed in
  104.         // Navigator, then dragged to the Editor.
  105.         else if (targets[i]==_XA_NETSCAPE_URL) {
  106.             XFE_URLDesktopType urlData(data[i]);
  107.             for (int j=0;j<urlData.numItems();j++) {
  108.                 if (XP_STRNCASECMP(urlData.url(j),"file:",5)==0) {
  109.                     anySuccess |= insertFile(urlData.url(j)+5);
  110.                 }
  111.                 else {
  112.                     anySuccess |= insertLink(urlData.url(j));
  113.                 }
  114.             }
  115.         }
  116.  
  117.         // A string is just a string, unless it's a URL, which is just
  118.         // a URL, unless it's a file: url. See above for thrilling details.
  119.         else if (targets[i]==XA_STRING) {
  120.             if (NET_URL_Type(data[i])!=0) {
  121.                 if (XP_STRNCASECMP(data[i],"file:",5)==0) {
  122.                     anySuccess |= insertFile(data[i]+5);
  123.                 }
  124.                 else {
  125.                     anySuccess |= insertLink(data[i]);
  126.                 }
  127.             }
  128.             else {
  129.                 anySuccess |= insertText(data[i]);
  130.             }
  131.         }
  132.     }
  133.  
  134.     // if any of the dropped items succeeded, let's celebrate.
  135.     return anySuccess;
  136. }
  137.  
  138. int XFE_EditorDrop::insertLink(const char *data)
  139. {
  140.     XDEBUG(printf("XFE_EditorDrop:insertLink(%s) at (%d,%d)\n",
  141.                   data,_dropEventX,_dropEventY));
  142.  
  143.     MWContext* context = _editorView->getContext();
  144.  
  145.     int32 x = _dropEventX + CONTEXT_DATA(context)->document_x;
  146.     int32 y = _dropEventY + CONTEXT_DATA(context)->document_y;
  147.  
  148.     EDT_PositionCaret(context, x, y);
  149.  
  150.     fe_EditorHrefInsert(context, NULL, (char*)data);
  151.  
  152.     return TRUE;
  153. }
  154.  
  155. int XFE_EditorDrop::insertFile(const char *data)
  156. {
  157.     XDEBUG(printf("XFE_EditorDrop:insertFile(%s) at (%d,%d)\n",
  158.                   data,_dropEventX,_dropEventY));
  159.  
  160.     const char *dataType=XFE_DragBase::guessUrlMimeType(data);
  161.  
  162.     MWContext* context = _editorView->getContext();
  163.  
  164.     if (XP_STRCASECMP(dataType,"image/gif") == 0
  165.         ||
  166.         XP_STRCASECMP(dataType,"image/jpeg") == 0) {
  167.         XDEBUG(printf("    GIF/JPG Image\n"));
  168.         EDT_ImageData* image;
  169.         char buf[1024];
  170.  
  171.         if (data[0] == '/' && data[1] != '/') { // local file name, url-ise it
  172.             XP_STRCPY(buf, "file://");
  173.             XP_STRNCAT_SAFE(buf, data, sizeof(buf) - 7/*sizeof("file://")*/);
  174.             data = buf;
  175.         }
  176.  
  177.         int32 x = _dropEventX + CONTEXT_DATA(context)->document_x;
  178.         int32 y = _dropEventY + CONTEXT_DATA(context)->document_y;
  179.  
  180.         EDT_PositionCaret(context, x, y);
  181.  
  182.         image = EDT_NewImageData();
  183.         image->pSrc = XP_STRDUP((char*)data); //need heap for EDT_FreeImageData
  184.         image->bNoSave = FALSE;  // copy to be with file.
  185.  
  186.         EDT_InsertImage(context, image, FALSE);
  187.  
  188.         EDT_FreeImageData(image);
  189.  
  190.         return TRUE;
  191.     } else {
  192.         fe_EditorEdit(context, (XFE_Frame*)_editorView->getToplevel(),
  193.                       /*chromespec=*/NULL, (char*)data);
  194.         return TRUE;
  195.     }
  196. }
  197.  
  198. int XFE_EditorDrop::insertText(const char *data)
  199. {
  200.     XDEBUG(printf("XFE_EditorDrop:insertText(%s) at (%d,%d)\n",
  201.                   data,_dropEventX,_dropEventY));
  202.     MWContext* context = _editorView->getContext();
  203.  
  204.     int32 x = _dropEventX + CONTEXT_DATA(context)->document_x;
  205.     int32 y = _dropEventY + CONTEXT_DATA(context)->document_y;
  206.  
  207.     EDT_PositionCaret(context, x, y);
  208.  
  209.     EDT_ClipboardResult result = EDT_PasteText(context, (char*)data);
  210.  
  211.     if (result != EDT_COP_OK) {
  212.         XBell(XtDisplay(CONTEXT_WIDGET(context)), 0);
  213.         return FALSE;
  214.     }
  215.  
  216.     return TRUE;
  217. }
  218.