diff dwt/dnd/Clipboard.d @ 45:d8635bb48c7c

Merge with SWT 3.5
author Jacob Carlborg <doob@me.com>
date Mon, 01 Dec 2008 17:07:00 +0100
parents e831403a80a9
children a7e41c09df9e
line wrap: on
line diff
--- a/dwt/dnd/Clipboard.d	Tue Oct 21 15:20:04 2008 +0200
+++ b/dwt/dnd/Clipboard.d	Mon Dec 01 17:07:00 2008 +0100
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2007 IBM Corporation and others.
+ * Copyright (c) 2000, 2008 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -8,23 +8,37 @@
  * Contributors:
  *     IBM Corporation - initial API and implementation
  *******************************************************************************/
-module dwt.dnd;
+module dwt.dnd.Clipboard;
+
+import dwt.dwthelper.utils;
 
 
-import dwt.*;
-import dwt.widgets.*;
+import dwt.DWT;
+import dwt.DWTError;
+import dwt.DWTException;
+import dwt.internal.cocoa.NSArray;
+import dwt.internal.cocoa.NSData;
+import dwt.internal.cocoa.NSMutableArray;
+import dwt.internal.cocoa.NSObject;
+import dwt.internal.cocoa.NSPasteboard;
+import dwt.internal.cocoa.NSString;
+import dwt.internal.cocoa.NSURL;
 import dwt.internal.cocoa.OS;
+import dwt.widgets.Display;
 
 /**
  * The <code>Clipboard</code> provides a mechanism for transferring data from one
  * application to another or within an application.
  * 
  * <p>IMPORTANT: This class is <em>not</em> intended to be subclassed.</p>
+ *
+ * @see <a href="http://www.eclipse.org/swt/snippets/#clipboard">Clipboard snippets</a>
+ * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: ClipboardExample</a>
+ * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
  */
 public class Clipboard {
 
     Display display;
-    int scrap = 0;
 
 /**
  * Constructs a new instance of this class.  Creating an instance of a Clipboard
@@ -41,7 +55,7 @@
  * @see Clipboard#dispose
  * @see Clipboard#checkSubclass
  */
-public this(Display display) { 
+public Clipboard(Display display) { 
     checkSubclass ();
     if (display is null) {
         display = Display.getCurrent();
@@ -85,7 +99,7 @@
 protected void checkSubclass () {
     String name = getClass().getName ();
     String validName = Clipboard.class.getName();
-    if (!validName.opEquals(name)) {
+    if (!validName.equals(name)) {
         DND.error (DWT.ERROR_INVALID_SUBCLASS);
     }
 }
@@ -164,14 +178,9 @@
  */
 public void clearContents(int clipboards) {
     checkWidget();
-    if ((clipboards & DND.CLIPBOARD) is 0 || scrap is 0) return;
-    int oldScrap = scrap;
-    scrap = 0;
-    int[] currentScrap = new int[1];
-    if (OS.GetCurrentScrap(currentScrap) !is OS.noErr) return;
-    if (currentScrap[0] is oldScrap) {
-        OS.ClearCurrentScrap();
-    }
+    if ((clipboards & DND.CLIPBOARD) is 0) return;
+    NSPasteboard pasteboard = NSPasteboard.generalPasteboard();
+    pasteboard.declareTypes(NSMutableArray.arrayWithCapacity(0), null);
 }
 
 /**
@@ -203,10 +212,10 @@
  *    <code><pre>
  *    Clipboard clipboard = new Clipboard(display);
  *    TextTransfer textTransfer = TextTransfer.getInstance();
- *    String textData = cast(String)clipboard.getContents(textTransfer);
+ *    String textData = (String)clipboard.getContents(textTransfer);
  *    if (textData !is null) System.out.println("Text is "+textData);
  *    RTFTransfer rtfTransfer = RTFTransfer.getInstance();
- *    String rtfData = cast(String)clipboard.getContents(rtfTransfer);
+ *    String rtfData = (String)clipboard.getContents(rtfTransfer);
  *    if (rtfData !is null) System.out.println("RTF Text is "+rtfData);
  *    clipboard.dispose();
  *    </code></pre>
@@ -239,10 +248,10 @@
  *    <code><pre>
  *    Clipboard clipboard = new Clipboard(display);
  *    TextTransfer textTransfer = TextTransfer.getInstance();
- *    String textData = cast(String)clipboard.getContents(textTransfer);
+ *    String textData = (String)clipboard.getContents(textTransfer);
  *    if (textData !is null) System.out.println("Text is "+textData);
  *    RTFTransfer rtfTransfer = RTFTransfer.getInstance();
- *    String rtfData = cast(String)clipboard.getContents(rtfTransfer, DND.CLIPBOARD);
+ *    String rtfData = (String)clipboard.getContents(rtfTransfer, DND.CLIPBOARD);
  *    if (rtfData !is null) System.out.println("RTF Text is "+rtfData);
  *    clipboard.dispose();
  *    </code></pre>
@@ -275,26 +284,32 @@
     checkWidget();
     if (transfer is null) DND.error(DWT.ERROR_NULL_ARGUMENT);
     if ((clipboards & DND.CLIPBOARD) is 0) return null;
-    int[] scrap = new int[1];
-    if (OS.GetCurrentScrap(scrap) !is OS.noErr) return null;
-    int[] typeIds = transfer.getTypeIds();
-    int[] size = new int[1];    
-    // get data from system clipboard
-    for (int i=0; i<typeIds.length; i++) {
-        int type = typeIds[i];
-        size[0] = 0;
-        if (OS.GetScrapFlavorSize(scrap[0], type, size) is OS.noErr && size[0] > 0) {
-            byte[] buffer = new byte[size[0]];
-            if (OS.GetScrapFlavorData(scrap[0], type, size, buffer) is OS.noErr) {
-                TransferData tdata = new TransferData();
-                tdata.type = type;      
-                tdata.data = new byte[1][];
-                tdata.data[0] = buffer;
-                return transfer.nativeToJava(tdata);
-            }
+    NSPasteboard pasteboard = NSPasteboard.generalPasteboard();
+    String[] typeNames = transfer.getTypeNames();
+    NSMutableArray types = NSMutableArray.arrayWithCapacity(typeNames.length);
+    for (int i = 0; i < typeNames.length; i++) {
+        types.addObject(NSString.stringWith(typeNames[i]));
+    }
+    NSString type = pasteboard.availableTypeFromArray(types);
+    if (type !is null) {
+        TransferData tdata = new TransferData();
+        tdata.type = Transfer.registerType(type.getString());
+        if (type.isEqual(OS.NSStringPboardType) || 
+                type.isEqual(OS.NSRTFPboardType) ||
+                type.isEqual(OS.NSHTMLPboardType)) {
+            tdata.data = pasteboard.stringForType(type);
+        } else if (type.isEqual(OS.NSFilenamesPboardType)) {
+            tdata.data = new NSArray(pasteboard.propertyListForType(type).id);
+        } else if (type.isEqual(OS.NSURLPboardType)) {
+            tdata.data = NSURL.URLFromPasteboard(pasteboard);
+        } else {
+            tdata.data = pasteboard.dataForType(type);
+        }
+        if (tdata.data !is null) {
+            return transfer.nativeToJava(tdata);
         }
     }
-    return null;    // No data available for this transfer
+    return null;
 }
 
 /**
@@ -435,30 +450,31 @@
         }
     }
     if ((clipboards & DND.CLIPBOARD) is 0) return;
-    if (OS.ClearCurrentScrap() !is OS.noErr) {
-        DND.error(DND.ERROR_CANNOT_SET_CLIPBOARD);
-    }
-    scrap = 0;
-    int[] currentScrap = new int[1];
-    if (OS.GetCurrentScrap(currentScrap) !is OS.noErr) {
+    NSPasteboard pasteboard = NSPasteboard.generalPasteboard();
+    if (pasteboard is null) {
         DND.error(DND.ERROR_CANNOT_SET_CLIPBOARD);
     }
-    scrap = currentScrap[0];
-    // copy data directly over to System clipboard (not deferred)
+    pasteboard.declareTypes(NSMutableArray.arrayWithCapacity(0), null);
     for (int i=0; i<dataTypes.length; i++) {
-        int[] typeIds = dataTypes[i].getTypeIds();
-        for (int j=0; j<typeIds.length; j++) {
+        String[] typeNames = dataTypes[i].getTypeNames();
+        for (int j=0; j<typeNames.length; j++) {
             TransferData transferData = new TransferData();
-            transferData.type = typeIds[j];
-            dataTypes[i].javaToNative(data[i], transferData); 
-            if (transferData.result !is OS.noErr) {
-                DND.error(DND.ERROR_CANNOT_SET_CLIPBOARD);
-            }
-            //Drag and Drop can handle multiple items in one transfer but the
-            //Clipboard can not.
-            byte[] datum = transferData.data[0];
-            if (OS.PutScrapFlavor(scrap, transferData.type, 0, datum.length, datum) !is OS.noErr){
-                DND.error(DND.ERROR_CANNOT_SET_CLIPBOARD);
+            transferData.type = Transfer.registerType(typeNames[j]);
+            dataTypes[i].javaToNative(data[i], transferData);
+            NSObject tdata = transferData.data;
+            NSString dataType = NSString.stringWith(typeNames[j]);
+            pasteboard.addTypes(NSArray.arrayWithObject(dataType), null);
+            if (dataType.isEqual(OS.NSStringPboardType) || 
+                    dataType.isEqual(OS.NSRTFPboardType) ||
+                    dataType.isEqual(OS.NSHTMLPboardType)) {
+                pasteboard.setString((NSString) tdata, dataType);
+            } else if (dataType.isEqual(OS.NSURLPboardType)) {
+                NSURL url = (NSURL) tdata;
+                url.writeToPasteboard(pasteboard);
+            } else if (dataType.isEqual(OS.NSFilenamesPboardType)) {
+                pasteboard.setPropertyList((NSArray) tdata, dataType);
+            } else {
+                pasteboard.setData((NSData) tdata, dataType);
             }
         }
     }
@@ -508,12 +524,14 @@
  */
 public TransferData[] getAvailableTypes(int clipboards) {
     checkWidget();
-    if ((clipboards & DND.CLIPBOARD) is 0) return new TransferData[0]; 
-    int[] types = _getAvailableTypes();
-    TransferData[] result = new TransferData[types.length];
-    for (int i = 0; i < types.length; i++) {
+    if ((clipboards & DND.CLIPBOARD) is 0) return new TransferData[0];
+    NSPasteboard pasteboard = NSPasteboard.generalPasteboard();
+    NSArray types = pasteboard.types();
+    int count = (int)/*64*/types.count();
+    TransferData[] result = new TransferData[count];
+    for (int i = 0; i < count; i++) {
         result[i] = new TransferData();
-        result[i].type = types[i];
+        result[i].type = Transfer.registerType(new NSString(types.objectAtIndex(i)).getString());
     }
     return result;
 }
@@ -536,32 +554,13 @@
  */
 public String[] getAvailableTypeNames() {
     checkWidget();
-    int[] types = _getAvailableTypes();
-    String[] names = new String[types.length];
-    for (int i = 0; i < types.length; i++) {
-        int type = types[i];
-        StringBuffer sb = new StringBuffer();
-        sb.append(cast(wchar)((type & 0xff000000) >> 24));
-        sb.append(cast(wchar)((type & 0x00ff0000) >> 16));
-        sb.append(cast(wchar)((type & 0x0000ff00) >> 8));
-        sb.append(cast(wchar)((type & 0x000000ff) >> 0));
-        names[i] = sb.toString();
+    NSPasteboard pasteboard = NSPasteboard.generalPasteboard();
+    NSArray types = pasteboard.types();
+    int count = (int)/*64*/types.count();
+    String[] result = new String[count];
+    for (int i = 0; i < count; i++) {
+        result[i] = new NSString(types.objectAtIndex(i)).getString();
     }
-    return names;
-}
-
-int[] _getAvailableTypes() {
-    int[] types = new int[0];
-    int[] scrap = new int[1];
-    if (OS.GetCurrentScrap(scrap) !is OS.noErr) return types;
-    int[] count = new int[1];
-    if (OS.GetScrapFlavorCount(scrap[0], count) !is OS.noErr || count[0] is 0) return types;
-    int[] info = new int[count[0] * 2];
-    if (OS.GetScrapFlavorInfoList(scrap[0], count, info) !is OS.noErr) return types;
-    types = new int[count[0]];
-    for (int i= 0; i < count [0]; i++) {
-        types[i] = info[i*2];
-    }
-    return types;
+    return result;
 }
 }