185607 - Can't copy and paste into dialog text fields from some applications
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/carbon/org/eclipse/swt/widgets/Combo.java b/bundles/org.eclipse.swt/Eclipse SWT/carbon/org/eclipse/swt/widgets/Combo.java
index e4e2100..15c4602 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/carbon/org/eclipse/swt/widgets/Combo.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/carbon/org/eclipse/swt/widgets/Combo.java
@@ -438,15 +438,7 @@
 	checkWidget ();
 	Point selection = getSelection ();
 	if (selection.x == selection.y) return;
-	copy (getText (selection.x, selection.y));
-}
-
-void copy (char [] buffer) {
-	if (buffer.length == 0) return;
-	OS.ClearCurrentScrap ();
-	int [] scrap = new int [1];
-	OS.GetCurrentScrap (scrap);
-	OS.PutScrapFlavor (scrap [0], OS.kScrapFlavorTypeUnicode, 0, buffer.length * 2, buffer);
+	copyToClipboard (getText (selection.x, selection.y));
 }
 
 void createHandle () {
@@ -517,7 +509,7 @@
 	}
 	char [] buffer = new char [newText.length ()];
 	newText.getChars (0, buffer.length, buffer, 0);
-	copy (buffer);
+	copyToClipboard (buffer);
 	setText (leftText + newText + rightText, false);
 	start += newText.length ();
 	setSelection (new Point (start, start));
@@ -934,16 +926,6 @@
 	return -1;
 }
 
-String getClipboardText () {
-	int [] scrap = new int [1];
-	OS.GetCurrentScrap (scrap);
-	int [] size = new int [1];
-	if (OS.GetScrapFlavorSize (scrap [0], OS.kScrapFlavorTypeUnicode, size) != OS.noErr || size [0] == 0) return "";
-	char [] buffer = new char [size [0] / 2];
-	if (OS.GetScrapFlavorData (scrap [0], OS.kScrapFlavorTypeUnicode, size, buffer) != OS.noErr) return "";
-	return new String (buffer);
-}
-
 int getCharCount () {
 //	checkWidget ();
 	int [] ptr = new int [1];
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/carbon/org/eclipse/swt/widgets/Spinner.java b/bundles/org.eclipse.swt/Eclipse SWT/carbon/org/eclipse/swt/widgets/Spinner.java
index 81094a7..e2b54f9 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/carbon/org/eclipse/swt/widgets/Spinner.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/carbon/org/eclipse/swt/widgets/Spinner.java
@@ -273,15 +273,7 @@
 	char [] buffer= new char [range.length];
 	OS.CFStringGetCharacters (ptr [0], range, buffer);
 	OS.CFRelease (ptr [0]);
-	copy (buffer);
-}
-
-void copy (char [] buffer) {
-	if (buffer.length == 0) return;
-	OS.ClearCurrentScrap ();
-	int [] scrap = new int [1];
-	OS.GetCurrentScrap (scrap);
-	OS.PutScrapFlavor (scrap [0], OS.kScrapFlavorTypeUnicode, 0, buffer.length * 2, buffer);
+	copyToClipboard (buffer);
 }
 
 void createHandle () {
@@ -328,7 +320,7 @@
 	if (selection [0] == selection [1]) return;
 	char [] buffer = setText ("", selection [0], selection [1], true);
 	if (buffer != null) {
-		copy (buffer);
+		copyToClipboard (buffer);
 	}
 }
 
@@ -610,15 +602,10 @@
 public void paste () {
 	checkWidget ();
 	if ((style & SWT.READ_ONLY) != 0) return;
-	int [] scrap = new int [1];
-	OS.GetCurrentScrap (scrap);
-	int [] size = new int [1];
-	if (OS.GetScrapFlavorSize (scrap [0], OS.kScrapFlavorTypeUnicode, size) != OS.noErr || size [0] == 0) return;
-	char [] buffer = new char [size [0] / 2];
-	if (OS.GetScrapFlavorData (scrap [0], OS.kScrapFlavorTypeUnicode, size, buffer) != OS.noErr) return;
+	String text = getClipboardText ();
 	short [] selection = new short [2];
 	if (OS.GetControlData (textHandle, (short)OS.kControlEntireControl, OS.kControlEditTextSelectionTag, 4, selection, null) != OS.noErr) return;
-	setText (new String (buffer), selection [0], selection [1], true);
+	setText (text, selection [0], selection [1], true);
 }
 
 public void redraw () {
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/carbon/org/eclipse/swt/widgets/Text.java b/bundles/org.eclipse.swt/Eclipse SWT/carbon/org/eclipse/swt/widgets/Text.java
index f53c4fd..46151b7 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/carbon/org/eclipse/swt/widgets/Text.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/carbon/org/eclipse/swt/widgets/Text.java
@@ -457,20 +457,12 @@
 	if (txnObject == 0) {
 		Point selection = getSelection ();
 		if (selection.x == selection.y) return;
-		copy (getEditText (selection.x, selection.y - 1));	
+		copyToClipboard (getEditText (selection.x, selection.y - 1));	
 	} else {
 		OS.TXNCopy (txnObject);
 	}
 }
 
-void copy (char [] buffer) {
-	if (buffer.length == 0) return;
-	OS.ClearCurrentScrap ();
-	int [] scrap = new int [1];
-	OS.GetCurrentScrap (scrap);
-	OS.PutScrapFlavor (scrap [0], OS.kScrapFlavorTypeUnicode, 0, buffer.length * 2, buffer);
-}
-
 void createHandle () {
 	int [] outControl = new int [1];
 	if ((style & SWT.MULTI) != 0 || (style & (SWT.BORDER | SWT.SEARCH)) == 0) {
@@ -598,7 +590,7 @@
 	if (cut) {
 		if (txnObject == 0) {
 			if (oldText == null) oldText = getEditText (oldSelection.x, oldSelection.y - 1);
-			copy (oldText);
+			copyToClipboard (oldText);
 			insertEditText ("");
 		} else {
 			OS.TXNCut (txnObject);
@@ -743,16 +735,6 @@
 	return OS.TXNDataSize (txnObject) / 2;
 }
 
-String getClipboardText () {
-	int [] scrap = new int [1];
-	OS.GetCurrentScrap (scrap);
-	int [] size = new int [1];
-	if (OS.GetScrapFlavorSize (scrap [0], OS.kScrapFlavorTypeUnicode, size) != OS.noErr || size [0] == 0) return "";
-	char [] buffer = new char [size [0] / 2];
-	if (OS.GetScrapFlavorData (scrap [0], OS.kScrapFlavorTypeUnicode, size, buffer) != OS.noErr) return "";
-	return new String (buffer);
-}
-
 /**
  * Returns the double click enabled flag.
  * <p>
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/carbon/org/eclipse/swt/widgets/Widget.java b/bundles/org.eclipse.swt/Eclipse SWT/carbon/org/eclipse/swt/widgets/Widget.java
index a7a9fc6..75055c0 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/carbon/org/eclipse/swt/widgets/Widget.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/carbon/org/eclipse/swt/widgets/Widget.java
@@ -12,6 +12,7 @@
 
 
 import org.eclipse.swt.internal.*;
+import org.eclipse.swt.internal.carbon.CFRange;
 import org.eclipse.swt.internal.carbon.CGRect;
 import org.eclipse.swt.internal.carbon.OS;
 import org.eclipse.swt.internal.carbon.RGBColor;
@@ -381,6 +382,14 @@
 	return OS.eventNotHandledErr;
 }
 
+void copyToClipboard (char [] buffer) {
+	if (buffer.length == 0) return;
+	OS.ClearCurrentScrap ();
+	int [] scrap = new int [1];
+	OS.GetCurrentScrap (scrap);
+	OS.PutScrapFlavor (scrap [0], OS.kScrapFlavorTypeUnicode, 0, buffer.length * 2, buffer);
+}
+
 int createCIcon (Image image) {
 	int imageHandle = image.handle;
 	int width = OS.CGImageGetWidth(imageHandle);
@@ -679,6 +688,41 @@
 	return j;
 }
 
+String getClipboardText () {
+	String result = "";
+	int [] scrap = new int [1];
+	OS.GetCurrentScrap (scrap);
+	int [] size = new int [1];
+	if (OS.GetScrapFlavorSize (scrap [0], OS.kScrapFlavorTypeUnicode, size) == OS.noErr) {
+		if (size [0] != 0) {
+			char [] buffer = new char [size [0] / 2];
+			if (OS.GetScrapFlavorData (scrap [0], OS.kScrapFlavorTypeUnicode, size, buffer) == OS.noErr) {
+				result = new String (buffer);
+			}
+		}
+	} else if (OS.GetScrapFlavorSize (scrap [0], OS.kScrapFlavorTypeText, size) == OS.noErr) {
+		if (size [0] != 0) {
+			byte [] buffer = new byte [size [0]];
+			if (OS.GetScrapFlavorData (scrap [0], OS.kScrapFlavorTypeText, size, buffer) == OS.noErr) {
+				int encoding = OS.CFStringGetSystemEncoding();
+				int cfstring = OS.CFStringCreateWithBytes(OS.kCFAllocatorDefault, buffer, buffer.length, encoding, true);
+				if (cfstring != 0) {
+					int length = OS.CFStringGetLength(cfstring);
+					if (length != 0) {
+						char[] chars = new char[length];
+						CFRange range = new CFRange();
+						range.length = length;
+						OS.CFStringGetCharacters(cfstring, range, chars);
+						result = new String(chars);
+					}
+					OS.CFRelease(cfstring);
+				}
+			}
+		}
+	}
+	return result;
+}
+
 Rectangle getControlBounds (int control) {
 	CGRect rect = new CGRect ();
 	OS.HIViewGetFrame (control, rect);