home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / mozil06.zip / bin / chrome / toolkit.jar / content / global / nsTransferable.js < prev    next >
Text File  |  2001-02-14  |  7KB  |  197 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is mozilla.org code.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation.  Portions created by Netscape are
  17.  * Copyright (C) 1998 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Original Author:
  21.  *   Ben Matthew Goodger <ben@netscape.com>
  22.  *
  23.  * Contributor(s): 
  24.  */
  25.  
  26. /** 
  27.  *  nsTransferable - a wrapper for nsITransferable that simplifies
  28.  *                   javascript clipboard and drag&drop. for use in
  29.  *                   these situations you should use the nsClipboard
  30.  *                   and nsDragAndDrop wrappers for more convenience
  31.  **/ 
  32.  
  33. var nsTransferable = {
  34.  
  35.   /** 
  36.    * Flavour List Format:
  37.    * flavourList["text/unicode"].width // width of data
  38.    * flavourList["text/unicode"].iid   // iid of data type
  39.    * flavourList["text/unicode"].data  // data to be stored (if any)
  40.    **/
  41.  
  42.   /**
  43.    * nsITransferable set (Object aFlavourList) ;
  44.    *
  45.    * Creates a transferable with data for a list of supported types ("flavours")
  46.    * 
  47.    * @param Object aFlavourList
  48.    *        a javascript object in the format described above 
  49.    **/ 
  50.   set: function (aFlavourList)
  51.     {
  52.       var trans = this.createTransferable();
  53.       for (var flavour in aFlavourList)
  54.         {
  55.           trans.addDataFlavor(flavour);
  56.           var width = aFlavourList[flavour].width;
  57.           var wrapper = this.createEmptyWrapper(width);
  58.           if (wrapper) 
  59.             {
  60.               wrapper.data = aFlavourList[flavour].data;
  61.               trans.setTransferData(flavour, wrapper, wrapper.data.length * width);
  62.             }
  63.         }
  64.       return trans;
  65.     },
  66.   
  67.   /**
  68.    * Array/Object get (Object aFlavourList, Function aRetrievalFunc, Boolean aAnyFlag) ;
  69.    *
  70.    * Retrieves data from the transferable provided in aRetrievalFunc, formatted
  71.    * for more convenient access.
  72.    *
  73.    * @param Object aFlavourList
  74.    *        a javascript object in the format described above
  75.    * @param Function aRetrievalFunc
  76.    *        a reference to a function that returns a nsISupportsArray of nsITransferables
  77.    *        for each item from the specified source (clipboard/drag&drop etc)
  78.    * @param Boolean aAnyFlag
  79.    *        a flag specifying whether or not a specific flavour is requested. If false,
  80.    *        data of the type of the first flavour in the flavourlist parameter is returned,
  81.    *        otherwise the best flavour supported will be returned.
  82.    **/
  83.   get: function (aFlavourList, aRetrievalFunc, aAnyFlag)
  84.     {
  85.       var firstFlavour = null;
  86.       for (var flav in aFlavourList)
  87.         {
  88.           firstFlavour = flav;
  89.           break;
  90.         }
  91.       
  92.       if (aRetrievalFunc)
  93.         { 
  94.           var supportsArray = aRetrievalFunc(aFlavourList);
  95.           var dataArray = [];
  96.           for (var i = 0; i < supportsArray.Count(); i++)
  97.             {
  98.               trans = supportsArray.GetElementAt(i);
  99.               if (trans) 
  100.                 trans = trans.QueryInterface(Components.interfaces.nsITransferable);
  101.                 
  102.               var data = { };
  103.               var flavour = { };
  104.               var length = { };
  105.               
  106.               if (aAnyFlag)
  107.                 { 
  108.                   trans.getAnyTransferData(flavour, data, length);
  109.                   if (data && flavour)
  110.                     {
  111.                       var selectedFlavour = aFlavourList[flavour.value];
  112.                       if (selectedFlavour)
  113.                         {
  114.                           data = data.value.QueryInterface(Components.interfaces[selectedFlavour.iid]);
  115.                           var currData = 
  116.                             {
  117.                               data: { data: data, length: length.value, width: selectedFlavour.width }, // this.wrapData(data.value, length.value, selectedFlavour.width),
  118.                               flavour: flavour.value
  119.                             };
  120.                           if (supportsArray.Count() == 1)
  121.                             return currData;
  122.                           else
  123.                             dataArray[i] = currData;  
  124.                         }
  125.                     }
  126.                   dataArray[i] = null;
  127.                 }
  128.               else
  129.                 {
  130.                   trans.getTransferData(firstFlavour, data, length);
  131.                   var curData = data ? this.wrapData(data.value, length.value, aFlavourList[firstFlavour].width) : null;
  132.                   if (supportsArray.Count() == 1)
  133.                     return curData;
  134.                   else
  135.                     dataArray[i] = curData;
  136.                 }
  137.             }
  138.           return dataArray;
  139.         }
  140.       else
  141.         throw "No data retrieval handler provided!";
  142.         
  143.       return null;      // quiet warnings
  144.     },
  145.  
  146.   /** 
  147.    * nsITransferable createTransferable (void) ;
  148.    *
  149.    * Creates and returns a transferable object.
  150.    **/    
  151.   createTransferable: function ()
  152.     {
  153.       return nsJSComponentManager.createInstance("@mozilla.org/widget/transferable;1",
  154.                                                  "nsITransferable");
  155.     },
  156.  
  157.   /**
  158.    * nsISupports createEmptyWrapper (int aWidth) ;
  159.    *
  160.    * Creates a wrapper for string data. XXX - this is bad, we're assuming the data we're
  161.    * dragging is string.
  162.    *
  163.    * @param int aWidth  
  164.    *        the width of the data (single or double byte)
  165.    **/
  166.   createEmptyWrapper: function (aWidth)
  167.     {
  168.       return aWidth == 2 ? nsJSSupportsUtils.createSupportsWString() : 
  169.                            nsJSSupportsUtils.createSupportsString();
  170.  
  171.     },
  172.   
  173.   /**
  174.    * String wrapData (Object aDataObject, int aLength, int aWidth)
  175.    *
  176.    * Returns the actual string representation of nsISupports[W]String wrapped
  177.    * data.
  178.    *
  179.    * @param Object aDataObject
  180.    *        the data to be unwrapped
  181.    * @param int aLength
  182.    *        the integer length of the data 
  183.    * @param int aWidth
  184.    *        the unit size
  185.    **/
  186.   wrapData: function (aDataObject, aLength, aWidth)
  187.     {
  188.       const IID = aWidth == 2 ? Components.interfaces.nsISupportsWString :
  189.                                 Components.interfaces.nsISupportsString;
  190.       var data = aDataObject.QueryInterface(IID);
  191.       if (data)
  192.         return data.data.substring(0, aLength / aWidth);
  193.       
  194.       return null;
  195.     }
  196. };  
  197.