unit AERoi; { This module is adds some AppleEvent support to Image Written and partly debugged by Rob Douglas University of British Columbia douglas@org.ecc.ubc.ca McKellar Designs rdouglas@mckellar.com } interface uses Memory, QuickDraw, Packages, Menus, Events, Fonts, Scrap, ToolUtils,Resources, Errors, Palettes, LowMem, AppleEvents,AEObjects, AERegistry, Errors,globals, Utilities,Camera, Msc, AEutility, AEPicture; function RoiGetData(var inAppleEvent, reply:AppleEvent; inToken: TokenPtr):OSerr; function RoiPutData(var inAppleEvent, reply:AppleEvent; inToken: TokenPtr):OSerr; function RoiEvents(var inAppleEvent, reply:AppleEvent; inToken: TokenPtr; inCommand: OSType):OSerr; implementation procedure HideRoi; var trect: rect; begin with info^ do if RoiShowing then begin if OpPending then begin OpPending := false; DoOperation(CurrentOp); end; trect := RoiRect; if RoiType = LineRoi then InsetRect(trect, -RoiHandleSize, -RoiHandleSize); UpdateScreen(trect); RoiUpdateTime := 0; end; end; function MakePolyROI(var ptList:AEDescList):OSerr; VAR theError:OSerr; pt1,pt2:Point; tPort: GrafPtr; actualSize:Size; returnedType:DescType; keywd:AEKeyword; index, ItemsInList:LongInt; begin with info^ do begin HideRoi; GetPort(tPort); theError := AECountItems(ptList, itemsInList); SetPort(wptr); OpenRgn; theError := AEGetNthPtr(ptList, 1, typeQDPoint, keywd, returnedType, @pt1, SizeOf(Point), actualSize); MoveTo(pt1.h,pt1.v); nCoordinates := 0; xCoordinates^[1] := pt1.h; yCoordinates^[1] := pt1.v; for index:=2 to ItemsInList do begin theError := AEGetNthPtr(ptList, index, typeQDPoint, keywd, returnedType, @pt2, SizeOf(Point), actualSize); if theError = noErr then LineTo(pt2.h,pt2.v); if nCoordinates < MaxCoordinates then nCoordinates := nCoordinates + 1; xCoordinates^[nCoordinates] := pt2.h; yCoordinates^[nCoordinates] := pt2.v; end; LineTo(pt1.h,pt1.v); CloseRgn(roiRgn); RoiRect := roiRgn^^.rgnBBox; RoiType := PolygonRoi; SetPort(tPort); RoiShowing := true; end end; { *************************************************************************************** Handles events specific to the Regions of interest. *************************************************************************************** } function MakeROI(var inAppleEvent:AppleEvent):OSerr; VAR theWindow:WindowPtr; isRoi:Boolean; oldSlice, theShortValue:integer; answer:Boolean; pt, lastPt:Point; theError, ignoreErr:OSerr; theRect:Rect; pt1,pt2:Point; tPort: GrafPtr; ptList:AEDescList; actualSize:Size; returnedType:DescType; keywd:AEKeyword; index, ItemsInList:LongInt; begin MakeROI := noErr; theError := AEGetParamDesc(inAppleEvent, 'poly', typeAEList, ptList); if theError = noErr then theError := MakePolyROI(ptList) else begin theError := GetAEBoolean(inAppleEvent, 'rect', answer); if (theError = noErr) and answer then theRect := CurrentWPtr^.portRect else theError := GetAERect(inAppleEvent, 'rect', theRect); if theError <> noErr then begin theError := GetAERect(inAppleEvent, 'oval', theRect); answer := false; end else answer := true; if theError = noErr then begin with Info^ do begin KillRoi; StopDigitizing; if answer then RoiType := RectRoi else RoiType := OvalRoi; RoiRect := theRect; MakeRegion; ShowRoi; end; end else begin theError := AEGetParamDesc(inAppleEvent, 'line', typeAEList, ptList); if theError = noErr then begin theError := AECountItems(ptList, itemsInList); theError := AEGetNthPtr(ptList, 1, typeQDPoint, keywd, returnedType, @pt1, SizeOf(Point), actualSize); if theError = noErr then theError := AEGetNthPtr(ptList, 2, typeQDPoint, keywd, returnedType, @pt2, SizeOf(Point), actualSize); if theError = noErr then with info^ do begin KillRoi; StopDigitizing; LX1 := pt1.h; LY1 := pt1.v; LX2 := pt2.h; LY2 := pt2.v; RoiType := LineRoi; MakeRegion; ShowRoi; end; end; end; end; MakeROI := theError; end; function RoiEvents(var inAppleEvent, reply:AppleEvent; inToken: TokenPtr; inCommand: OSType):OSerr; VAR theError:OSErr; theWindow:WindowPtr; theShortValue:integer; rh:RgnHandle; on:boolean; begin theError := noErr; on := info^.RoiShowing; if inCommand = 'rCmp' then { Complement} with info^ do begin KillRoi; rh := NewRgn; RectRgn(rh,CurrentWPtr^.portRect); DiffRgn(rh,roiRgn,roiRgn); DisposeRgn(rh); RoiRect := roiRgn^^.rgnBBox; if on then ShowRoi; end else if inCommand = 'rIns' then { INSET ROI } with info^ do begin theError := GetAEShort(inAppleEvent, 'delt', theShortValue); if on then KillRoi; InsetRgn(roiRgn, theShortValue, theShortValue); RoiRect := roiRgn^^.rgnBBox; if on then ShowRoi; end else if inCommand = 'rDel' then KillROI { KILL ROI } else if inCommand = 'clos' then KillROI { CLOSE ROI } else if inCommand = 'Disp' then KillROI { DISPOSE ROI } else if inCommand = 'rMak' then theError := MakeROI(inAppleEvent) { MAKE ROI } else if inCommand = 'rRes' then RestoreRoi { RESTORE ROI } else if inCommand = 'save' then SaveRoi { SAVE ROI } else if inCommand = 'Upda' then UpdateScreen(info^.RoiRect) { UPDATE } else if (inCommand = 'copy') and (inCommand = 'cut ') or (inCommand = 'clon') or (inCommand = 'past') or (inCommand = 'cler') or (inCommand = 'Fill') or (inCommand = 'Invt') or (inCommand = 'AddC') or (inCommand = 'SubC') or (inCommand = 'MulC') or (inCommand = 'DivC') or (inCommand = 'AnlP') or (inCommand = 'BinC') or (inCommand = 'Conv') or (inCommand = 'copB') or (inCommand = 'EquH') or (inCommand = 'Fltr') or (inCommand = 'FLIP') or (inCommand = 'ScRo') or (inCommand = 'Shad') or (inCommand = 'Shad') or (inCommand = 'Fram') then theError := PictureEvents(inAppleEvent, reply, inToken, inCommand) else theError := errAEEventNotHandled; RoiEvents := noErr; end; function RoiGetData(var inAppleEvent, reply:AppleEvent; inToken: TokenPtr):OSerr; VAR theError:OSErr; pt:Point; theList:AEDescList; begin theError := errAEDescNotFound; with inToken^, info^ do begin pt.h := RoiRect.left; pt.v := RoiRect.top; if name = 'IsOn' then theError := ReturnBooleanProperty(reply,RoiShowing) else if name = 'rTyp' then theError := ReturnEnumProperty(reply,210,integer(RoiType)) else if name = 'pbnd' then theError := ReturnRectProperty(reply,RoiRect) else if name = 'pLft' then theError := ReturnShortProperty(reply,pt.h) else if name = 'pTop' then theError := ReturnShortProperty(reply,pt.v) else if name = 'wide' then theError := ReturnShortProperty(reply,RoiRect.right - RoiRect.left) else if name = 'hite' then theError := ReturnShortProperty(reply,RoiRect.bottom - RoiRect.top) else if name = 'pLoc' then theError := ReturnPointProperty(reply,pt) else if name = 'rCoo' then begin theError := AECreateList(nil, 0,false,theList); theError := AEPutParamDesc( reply, keyDirectObject, theList ); end; end; RoiGetData := theError; end; function RoiPutData(var inAppleEvent, reply:AppleEvent; inToken: TokenPtr):OSerr; VAR theError:OSErr; pt:Point; theList:AEDescList; theRect:Rect; i:integer; on:Boolean; begin theError := noErr; with inToken^, info^ do begin on := RoiShowing; pt.h := RoiRect.left; pt.v := RoiRect.top; if name = 'pCoo' then begin theError := AEGetParamDesc(inAppleEvent, 'poly', typeAEList, theList); if theError = noErr then theError := MakePolyROI(theList); end else if name = 'rTyp' then begin i := GetEnumProperty(inAppleEvent,210); end else if name = 'pbnd' then begin HideRoi; StopDigitizing; RoiRect := GetRectProperty(inAppleEvent); if RoiType <> OvalRoi then RoiType := RectRoi; MakeRegion; SetupUndo; RoiShowing := true; RoiUpdateTime := 0; end else if name = 'pLft' then begin i := GetShortProperty(inAppleEvent,-10000,10000); if on then HideRoi; OffsetRgn(roiRgn, i - RoiRect.left,0); RoiRect := roiRgn^^.rgnBBox; RoiShowing := true; RoiUpdateTime := 0; end else if name = 'pTop' then begin i := GetShortProperty(inAppleEvent,-10000,10000); if on then HideRoi; OffsetRgn(roiRgn, 0,i - RoiRect.top); RoiRect := roiRgn^^.rgnBBox; RoiShowing := true; RoiUpdateTime := 0; end else if name = 'pLoc' then begin pt := GetPointProperty(inAppleEvent); if on then HideRoi; OffsetRgn(roiRgn, pt.h - RoiRect.left, pt.v - RoiRect.top); RoiRect := roiRgn^^.rgnBBox; RoiShowing := true; RoiUpdateTime := 0; end else if name = 'wide' then begin i := GetShortProperty(inAppleEvent,0,10000); if on then HideRoi; RoiRect.right := RoiRect.left + i; MakeRegion; RoiShowing := true; RoiUpdateTime := 0; end else if name = 'hite' then begin i := GetShortProperty(inAppleEvent,0,10000); if on then HideRoi; RoiRect.bottom := RoiRect.top + i; MakeRegion; RoiShowing := true; RoiUpdateTime := 0; end; end; RoiPutData := theError; end; end.