clipboard
authorClaus Gittinger <cg@exept.de>
Tue, 13 Jul 1999 12:02:35 +0200
changeset 2794 93debab2f79f
parent 2793 8ef2806db16c
child 2795 c3c1422862f8
clipboard
WinWorkstat.st
WinWorkstation.st
--- a/WinWorkstat.st	Mon Jul 12 14:34:20 1999 +0200
+++ b/WinWorkstat.st	Tue Jul 13 12:02:35 1999 +0200
@@ -12502,14 +12502,39 @@
     HANDLE hClip;
     HANDLE hData;
     OBJ s;
-
-    hClip = OpenClipboard(NULL);
-    if (hClip) {
-	hData = GetClipboardData(CF_TEXT);
-	CloseClipboard();
-	INFOPRINT((stderr, "WinWorkstation [info]: clipBoard data is <%s>\n", (char *)hData));
-	s = __MKSTRING( (char *)hData );
-	RETURN(s);
+    unsigned char *src, *p;
+    int n, len, realLen;
+
+    /* check for format CF_TEXT */
+    if (IsClipboardFormatAvailable(CF_TEXT)) {
+	hClip = OpenClipboard(NULL);
+	if (hClip) {
+	    hData = GetClipboardData(CF_TEXT);
+	    src = GlobalLock(hData);
+	    len = src ? strlen(src) : 0;
+	    /* see how much we really need (when LF is dropped after CR */
+	    for (realLen = 0, n=len, p=src; n; ) {
+		ch = *p++; n--; realLen++;
+		if (ch == 0x0D) {
+		    if (n && (*p == 0x0A)) {
+			p++; n--;
+		    }
+		}
+	    }
+	    s = __MKEMPTYSTRING(realLen);
+	    for (n=0, p=__stringVal(s); len; ) {
+		*p = *src++;
+		len--;
+		if ((*p == 0x0D) && len && (*src == 0x=A)) {
+		    src++;
+		    len--;
+		p++;
+	    }
+	    GlobalUnlock(hData);
+	    CloseClipboard();
+	    INFOPRINT((stderr, "WinWorkstation [info]: clipBoard data is <%s>\n", (char *)hData));
+	    RETURN(s);
+	}
     }
     RETURN(nil);
 %}
@@ -12525,24 +12550,44 @@
 %{
     HANDLE hClip;
     HANDLE hData;
-    char *s;
+    char *s, *p, *src, *dst;
+    int len, realLen;
 
     if (__isString(aString)) {
 	s = __stringVal(aString);
 	hClip = OpenClipboard(NULL);
 	if (hClip) {
-	    EmptyClipboard();
-	    hData = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE,strlen(s)+1);
+	    /* must replace CR by CRLF */
+	    len = realLen = __stringSize(aString);
+	    for (p=s; n; n++) {
+		if ((p[0] == 0x0D) && (p[1] != 0x0A)) {
+		    realLen++;
+		}
+	    }
+
+	    hData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, len+1);
 	    if (hData) {
 		char *t = GlobalLock(hData);
 
 		if (t) {
-		    strcpy(t,s);
-		    //printf("setClipboardData:>%s<\n",t);
-		    GlobalUnlock(hData);
-		    if (SetClipboardData(CF_TEXT,hData) == 0) {
+		    for (src=s, dst=t, n=len; n;) {
+			*dst++ = *src; n--;
+			if ((src[0] == 0x0D) && (src[1] != 0x0A)) {
+			    *dst++ = 0x0A;
+			}
+			src++;
+		    }
+		    *dst = 0;
+
+		    EmptyClipboard();
+		    if (SetClipboardData(CF_TEXT, hData) == 0) {
 			DPRINTF(("SetClipboardData error:%d\n", GetLastError()));
-		    }
+			GlobalUnlock(hData);
+		    }
+		    /* Note: After setting clipboard data, 
+		     * the memory block previously allocated belongs to
+		     * the clipboard - not to the app. 
+		     */
 		}
 	    }
 	    CloseClipboard();
@@ -13538,6 +13583,6 @@
 !WinWorkstation class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libview/Attic/WinWorkstat.st,v 1.156 1999-06-25 15:01:13 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libview/Attic/WinWorkstat.st,v 1.157 1999-07-13 10:02:35 cg Exp $'
 ! !
 WinWorkstation initialize!
--- a/WinWorkstation.st	Mon Jul 12 14:34:20 1999 +0200
+++ b/WinWorkstation.st	Tue Jul 13 12:02:35 1999 +0200
@@ -12502,14 +12502,39 @@
     HANDLE hClip;
     HANDLE hData;
     OBJ s;
-
-    hClip = OpenClipboard(NULL);
-    if (hClip) {
-	hData = GetClipboardData(CF_TEXT);
-	CloseClipboard();
-	INFOPRINT((stderr, "WinWorkstation [info]: clipBoard data is <%s>\n", (char *)hData));
-	s = __MKSTRING( (char *)hData );
-	RETURN(s);
+    unsigned char *src, *p;
+    int n, len, realLen;
+
+    /* check for format CF_TEXT */
+    if (IsClipboardFormatAvailable(CF_TEXT)) {
+	hClip = OpenClipboard(NULL);
+	if (hClip) {
+	    hData = GetClipboardData(CF_TEXT);
+	    src = GlobalLock(hData);
+	    len = src ? strlen(src) : 0;
+	    /* see how much we really need (when LF is dropped after CR */
+	    for (realLen = 0, n=len, p=src; n; ) {
+		ch = *p++; n--; realLen++;
+		if (ch == 0x0D) {
+		    if (n && (*p == 0x0A)) {
+			p++; n--;
+		    }
+		}
+	    }
+	    s = __MKEMPTYSTRING(realLen);
+	    for (n=0, p=__stringVal(s); len; ) {
+		*p = *src++;
+		len--;
+		if ((*p == 0x0D) && len && (*src == 0x=A)) {
+		    src++;
+		    len--;
+		p++;
+	    }
+	    GlobalUnlock(hData);
+	    CloseClipboard();
+	    INFOPRINT((stderr, "WinWorkstation [info]: clipBoard data is <%s>\n", (char *)hData));
+	    RETURN(s);
+	}
     }
     RETURN(nil);
 %}
@@ -12525,24 +12550,44 @@
 %{
     HANDLE hClip;
     HANDLE hData;
-    char *s;
+    char *s, *p, *src, *dst;
+    int len, realLen;
 
     if (__isString(aString)) {
 	s = __stringVal(aString);
 	hClip = OpenClipboard(NULL);
 	if (hClip) {
-	    EmptyClipboard();
-	    hData = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE,strlen(s)+1);
+	    /* must replace CR by CRLF */
+	    len = realLen = __stringSize(aString);
+	    for (p=s; n; n++) {
+		if ((p[0] == 0x0D) && (p[1] != 0x0A)) {
+		    realLen++;
+		}
+	    }
+
+	    hData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, len+1);
 	    if (hData) {
 		char *t = GlobalLock(hData);
 
 		if (t) {
-		    strcpy(t,s);
-		    //printf("setClipboardData:>%s<\n",t);
-		    GlobalUnlock(hData);
-		    if (SetClipboardData(CF_TEXT,hData) == 0) {
+		    for (src=s, dst=t, n=len; n;) {
+			*dst++ = *src; n--;
+			if ((src[0] == 0x0D) && (src[1] != 0x0A)) {
+			    *dst++ = 0x0A;
+			}
+			src++;
+		    }
+		    *dst = 0;
+
+		    EmptyClipboard();
+		    if (SetClipboardData(CF_TEXT, hData) == 0) {
 			DPRINTF(("SetClipboardData error:%d\n", GetLastError()));
-		    }
+			GlobalUnlock(hData);
+		    }
+		    /* Note: After setting clipboard data, 
+		     * the memory block previously allocated belongs to
+		     * the clipboard - not to the app. 
+		     */
 		}
 	    }
 	    CloseClipboard();
@@ -13538,6 +13583,6 @@
 !WinWorkstation class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libview/WinWorkstation.st,v 1.156 1999-06-25 15:01:13 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libview/WinWorkstation.st,v 1.157 1999-07-13 10:02:35 cg Exp $'
 ! !
 WinWorkstation initialize!