[comp.sys.mac.programmer] Problem trying to PutScrap

nick@lfcs.ed.ac.uk (Nick Rothwell) (05/03/89)

Hello. Thanks for the suggestions about pasting some TEXT or a PICT along
with a private scrap format - it seems to work. Well, almost. I'm following
the instructions in the Scrap Manager chapter of IM, where it says that a
TEXT scrap is a longword for the length, followed by the characters. So, I
have this example program:

	static char *message = "\pHello world.";

	main()
	{
		Handle textHan;
		long len;
		struct Coerce_ {long len; char text[4];} *coerce;

		printf("ZeroScrap() -> %ld\n", ZeroScrap());
	
		len = (long) message[0];
		textHan = NewHandle(len + sizeof(long));

		HLock(textHan);
		coerce = (struct Coerce_ *)(*textHan);
		coerce->len = len;
		BlockMove(&message[1], &coerce->text[0], len);

		printf("PutScrap() -> %ld\n",
			   PutScrap(len + sizeof(long), 'TEXT', *textHan)
			  );
		printf("LoadScrap() -> %ld\n", LoadScrap());
	}

In other words, I map a record with the required structure ("Coerce_") onto
the handle storage, fill in the fields, and that's it. When I view the
scrap from the Finder's "Show Clipboard" command, the text is there, but
there's a bum character at the front (whose ASCII value is the length of
the text).
   The "char text[4]" is an experiment, in case there were alignment problems.
I've also tried "char text[1]" and "Str255 text".
   There are rules that say that the thing being pasted must be an even number
of bytes long. I've followed these rules, but the same thing happens. Has
anybody had (or solved) the same kind of problems?
   As an aside: why do all the scrap manager calls return their error status
as a long?

		Nick.
Nick Rothwell,	Laboratory for Foundations of Computer Science, Edinburgh.
		nick@lfcs.ed.ac.uk    <Atlantic Ocean>!mcvax!ukc!lfcs!nick
~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~
               Fais que ton reve soit plus long que la nuit.

oster@dewey.soe.berkeley.edu (David Phillip Oster) (05/04/89)

In article <1907@etive.ed.ac.uk> nick@lfcs.ed.ac.uk (Nick Rothwell) writes:
_>Hello. Thanks for the suggestions about pasting some TEXT or a PICT along
_>with a private scrap format - it seems to work. Well, almost. I'm following
_>the instructions in the Scrap Manager chapter of IM, where it says that a
_>TEXT scrap is a longword for the length, followed by the characters. 
You are confusing the Scrap manager's private, internal format with what
you need to do. Just post the text. You don't need to include the length,
just tell it the length:
_>
_>	static char *message = "\pHello world.";
_>
_>	main()
_>	{
		SystemEdit(3);	/* fake multi-finder into externalizing clip */	
_>		printf("ZeroScrap() -> %ld\n", ZeroScrap());
_>		printf("PutScrap() -> %ld\n",
			   PutScrap(message[0], 'TEXT', &message[1]));
_>	}
You don't need the LoadScrap(), that is an internal matter of the Scrap
Manager. You don't need the call to SystemEdit in a real program, but you
do in this toy if you want multi-finder to make your scrap change visible
in other programs. 
Note: the general case of posting a string would have you use:
(int) (unsigned int) (unsigned char) message[0]
to make C sign extend the length byte correctly. This is only an issue for
strings longer than 127 characters. I bind all this up in a macro called
#define Length(s) ((int) (unsigned int) (unsigned char) (s)[0])
_>   There are rules that say that the thing being pasted must be an even number
_>of bytes long. I've followed these rules, but the same thing happens. Has
_>anybody had (or solved) the same kind of problems?
Can you document this? I've never had any trouble posting an odd number of
characters.
_>   As an aside: why do all the scrap manager calls return their error status
_>as a long?
Some knowledge isn't given to mortals.