summaryrefslogtreecommitdiffstats
path: root/Mac/Contrib/BBPy/source/BBPy_main.c
blob: a96b271abb34c6ca5e8da3bde49cd1bcc5298d79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*	BBPython
	A simple menu command to send the contents of a window to the Python interpreter
	
	copyright © 1996 Just van Rossum, Letterror: just@knoware.nl
	
	All Rights Reserved
*/

#include "BBPy.h"

OSErr SendTextAsAE(ExternalCallbackBlock *callbacks, Ptr theText, long theSize, Str255 windowTitle)
{
	OSErr			err;
	AEDesc		theEvent;
	AEAddressDesc	theTarget;
	AppleEvent	theReply;
	AEDesc		theTextDesc;
	AEDesc		theNameDesc;
	OSType		pythonSig = 'Pyth';
	FSSpec		docSpec;
	short			itemHit;
	long			time;
	EventRecord	theDummyEvent;
	
	/* initialize AE descriptor for python's signature */
	err = AECreateDesc (typeApplSignature, &pythonSig, sizeof(OSType), &theTarget);
	if(err != noErr) return err;
	
	/* initialize AE descriptor for the title of our window */
	err = AECreateDesc (typeChar, &windowTitle[1], windowTitle[0], &theNameDesc);
	if(err != noErr) return err;
	
	/* initialize AE descriptor for the content of our window */
	err = AECreateDesc ('TEXT', theText, theSize, &theTextDesc);
	if(err != noErr) return err;
	
	/* initialize AppleEvent */
	err = AECreateAppleEvent ('pyth', 'EXEC', &theTarget, kAutoGenerateReturnID, kAnyTransactionID, &theEvent);
	if(err != noErr) return err;
	
	/* add the content of our window to the AppleEvent */
	err = AEPutParamDesc (&theEvent, keyDirectObject, &theTextDesc);
	if(err != noErr) return err;
	
	/* add the title of our window to the AppleEvent */
	err = AEPutParamDesc (&theEvent, 'NAME', &theNameDesc);
	if(err != noErr) return err;
	
	/* send the AppleEvent */
	err = AESend (&theEvent, &theReply, kAEWaitReply, kAEHighPriority, kNoTimeOut, NULL, NULL);
	if(err == connectionInvalid) {
		// launch PythonSlave.py
		itemHit = Alert(128, NULL);
		if(itemHit == 2)  return noErr;	/* user cancelled */
		
		if( ! GetPythonSlaveSpec(&docSpec) )
			return noErr;		/* user cancelled */
		
		err = LaunchPythonSlave(&docSpec);
		if(err != noErr) return err;
	} else if(err != noErr) 
		return err;
	
	/* clean up */
	err = AEDisposeDesc (&theTarget);
	if(err != noErr) return err;
	
	err = AEDisposeDesc (&theNameDesc);
	if(err != noErr) return err;
	
	err = AEDisposeDesc (&theTextDesc);
	if(err != noErr) return err;
	
	err = AEDisposeDesc (&theEvent);
	if(err != noErr) return err;
	
	err = AEDisposeDesc (&theReply);
	if(err != noErr) return err;
	
	/* everything is cool */
	return noErr;
}

pascal void main(ExternalCallbackBlock *callbacks, WindowPtr theWindow)
{
	long 		oldA4;
	OSErr		err;
	Handle	windowContents;
	Str255	windowTitle;
	
	//RememberA0(); /* Can't find header file for this. Seems to work anyway. */
	
	oldA4 = SetUpA4();	

	GetWTitle(theWindow, windowTitle);
	windowContents = callbacks->GetWindowContents(theWindow);
	
	HLock(windowContents);
	err = SendTextAsAE(callbacks, *windowContents, GetHandleSize(windowContents), windowTitle);
	if(err != noErr) callbacks->ReportOSError(err);
	HUnlock(windowContents);
	
	RestoreA4(oldA4);
}