// Text of project CustomRoute written on 11/21/95 at 7:11 PM
// Beginning of text file Constants
/*
**      Newton Developer Technical Support Sample Code
**
**      Custom Route, a Newton 2.0 routing example
**
**      by J. Christopher Bell, Newton Developer Technical Support
**
**      Copyright © 1994-5 by Apple Computer, Inc.  All rights reserved.
**
**      You may incorporate this sample code into your applications without
**      restriction.  This sample code has been provided "AS IS" and the
**      responsibility for its operation is 100% yours.  You are not
**      permitted to modify and redistribute the source as "DTS Sample Code."
**      If you are going to re-distribute the source, we require that you
**      make it clear in the source that the code was descended from
**      Apple-provided sample code, but that you've made changes.
*/

constant kMyMainDataDefSym 	:= '|myDataDef:customRoute:PIEDTS| ;
constant kMyMainViewDefSym 	:= '|myViewDef:customRoute:PIEDTS| ;
constant kMyFrameViewDefSym := '|myFrameViewDef:customRoute:PIEDTS| ;


// Get the cup icon
OpenResFile(home & "pictures");
DefConst('kCupIcon, GetPictAsBits("minRouteIcon", nil));
CloseResFile();


/*
 * This is a routing format which handles 'text and 'frame (despite the name, protoFrameFormat
 * handles both types. If you want to handle just 'frames, override the dataTypes slot with ['frame].
 *
 * This will be registered in the application installScript with RegisterViewDef(...).
 *
 * Note that we won't necessarily be able to view the item in the iobox unless we write a 
 * NON-ROUTING viewDef for kMyMainDataDefSym so that it can be viewed in the iobox item viewer.
 */
DefConst('kMyFrameRoutingFormat, {	
	_proto:		protoFrameFormat,
	title:		"CustomRoute - picture choice", 
	version:	1, 
	symbol:		kMyFrameViewDefSym, 
	SetupItem:	func(item, targetInfo) 
		begin
			inherited:?SetupItem(item, targetInfo);
			/*If you have any "preprocessing" to do like set anything in the item or add
			 * extra body slots like version slots, or extra data, you can do it here, but if you are
			 * trying to actually prep visual shapes, do that in formatInitScript.		
			 * 
			 * Note that by calling inherited, we get the equivalent of:
			 *		item.body := targetInfo.target;
			 * ...plus if the target is a soup entry alias, it resolves it...
			 *
			 * Only modify the item, not the target.
			 */

			// set the title; note that the string will be editable by the user...
			item.title := "CustomRoute item: " & datentime(time());

			item;
		end,
	textScript: func(fields, target)
		clone(target.data);	// our target.data should always be plain text
});
// End of text file Constants
// Beginning of file PrintFormat

// Before Script for "_view000"
/*
**      Newton Developer Technical Support Sample Code
**
**      Custom Route, a Newton 2.0 routing example
**
**      by J. Christopher Bell, Newton Developer Technical Support
**
**      Copyright © 1994-5 by Apple Computer, Inc.  All rights reserved.
**
**      You may incorporate this sample code into your applications without
**      restriction.  This sample code has been provided "AS IS" and the
**      responsibility for its operation is 100% yours.  You are not
**      permitted to modify and redistribute the source as "DTS Sample Code."
**      If you are going to re-distribute the source, we require that you
**      make it clear in the source that the code was descended from
**      Apple-provided sample code, but that you've made changes.
*/

_view000 :=
    {
     printNextPageScript:
       func()
       begin
          // for this example, always print 3 pages worth of stuff
       	if myPageCounter < 3 then
       		begin
       			// we check this in viewSetupFormScript to distinguish between
       			// Initialization and RedoChildren;
       			myPageCounter := myPageCounter + 1;
       			turningPages := true;	
       			:RedoChildren();	// rebuild all our views in preparation for the next page
       			turningPages := nil;
       			true;
       		end;
       end,
     symbol: kMyMainViewDefSym,
     viewSetupFormScript:
       func()
       begin
       	// we set this in printNextPageScript to distinguish between Initialization and RedoChildren();
       	if not turningPages then
       		myPageCounter := 1;					// don't rely on fields.pageNumber to track pages! We track it
       											// and increment it in printNextPageScript
       	inherited:?viewSetupFormScript();		// this method is defined internally
       end,
     formatInitScript:
       func(fields,theTarget)
       begin
       	/* If you need to do prep something which takes a long time, and you are 
       	 * worried about "timing out" a fax machine, do it here, not
       	 * in viewSetupFormScript!!!
       	 */
       end,
     SetupItem:
       func(item, tInfo)
       begin
       	/* If you have any "preprocessing" to do like set anything in the item or add
       	 * extra slots like version slots, or extra data, you can do it here, but if you are
       	 * trying to actually prep the visual shapes, you should do that in formatInitScript.
       	 */
       	inherited:?SetupItem(item, tInfo);
       	
       	
       	// set the title; note that the string will be editable by the user...
       	item.title := "CustomRoute item: " & datentime(time());
       	
       	item;
       end,
     title: "MyFormat",
     myPageCounter: nil,
     turningPages:
       nil	// a flag to distinguish between initialization and RedoChildren in viewSetupFormScript
       ,
     _proto: @200
    };

_view001 :=
    {viewFlags: 1,
     viewFormat: 593,
     viewBounds: {left: 5, top: 5, right: 5, bottom: -5},
     viewJustify: 240,
     viewClass: 74
    };
AddStepForm(_view000, _view001);

_view002 :=
    {viewFlags: 1,
     icon: GetPictAsBits("Llama", nil),
     viewFormat: nil,
     viewBounds: {left: 0, top: 0, right: 0, bottom: 0},
     viewJustify: 242,
     viewClass: 76
    };
AddStepForm(_view001, _view002);





_view003 :=
    {text: "<the user data>",
     viewBounds: {top: -150, left: 0, right: 0, bottom: -20},
     viewSetupFormScript:
       func()
       	begin
       		text := "Custom data from input line: ";
       		if target.data and StrLen(target.data) > 0 then
       			text := text & "\n‘" & target.data & "’" ;
       		else
       			text := text & "\n<empty field>";
       	end,
     viewFont: fancyFont18+tsBold,
     viewJustify: 178,
     _proto: @218
    };
AddStepForm(_view000, _view003);



pageNumber :=
    {text: "Static Text",
     viewBounds: {top: -25, left: 0, right: 0, bottom: -5},
     viewJustify: 8388790
     ,
     viewSetupFormScript:
       func()
       begin
          text := "Page" && NumberStr(myPageCounter) ;
       end,
     viewFont: simpleFont18+tsBold,
     debug: "pageNumber",
     _proto: @218
    };
AddStepForm(_view000, pageNumber);




constant |layout_PrintFormat| := _view000;
// End of file PrintFormat
// Beginning of file CustomRoute.t

// Before Script for "myBase"
/*
**      Newton Developer Technical Support Sample Code
**
**      Custom Route, a Newton 2.0 routing example
**
**      by J. Christopher Bell, Newton Developer Technical Support
**
**      Copyright © 1994-5 by Apple Computer, Inc.  All rights reserved.
**
**      You may incorporate this sample code into your applications without
**      restriction.  This sample code has been provided "AS IS" and the
**      responsibility for its operation is 100% yours.  You are not
**      permitted to modify and redistribute the source as "DTS Sample Code."
**      If you are going to re-distribute the source, we require that you
**      make it clear in the source that the code was descended from
**      Apple-provided sample code, but that you've made changes.
*/

myBase :=
    {viewBounds: {left: 0, top: 2, right: 236, bottom: 326},
     appSymbol: kAppSymbol,
     title: "CustomRoute",
     viewSetupFormScript:
       func()
       	viewBounds := GetAppParams().appAreaBounds;  // should define max/min, but we're lazy
       ,
     PutAwayScript:
       func(item)
       begin
       	/* your app might be asked to put away something you don't know about. Return
       	 * nil if you don't know how to deal with it!
       	 */
       	if classof(item.body) <> kMyMainDataDefSym or TargetIsCursor(item.body) then
       		return nil;
       				
       	:Open();  // we might not be open!
       
       	// This is the place where you could get wild and move the information
       	// into soups, other apps, and otherwise extract the information.
       	if item.body.data then
           	SetValue(theLine, 'text, item.body.data);
       	
       	true; // return true if it was put away successfully
       end,
     GetActiveView:
       func()
       	begin
       		printFormatView; // return the current "active" view (for Intelligent Assistant)
       	end,
     myFormats:
       {
       	viewFormat:	GetLayout("PrintFormat"),
       	myFrameFormat:  kMyFrameRoutingFormat,	// see Constants file
       	// put your other formats in this frame and then check out the code in
       	// the install/removescripts that uses it...
       };,
     ReorientToScreen:
       ROM_defRotateFunc  // Allow landscape mode
       ,
     debug: "myBase",
     _proto: @157
    };

printFormatView :=
    {viewBounds: {left: 10, top: 37, right: -10, bottom: 156},
     viewFlags: 1,
     viewFormat: 336,
     routeScripts:
       [
        {title: "Drink tea", routeScript: 'calmScript, icon: kCupIcon}, // defined in constants file
        {title: "Duplicate", routeScript: 'duplicateScript, icon: ROM_routeDuplicateIcon},
        {title: "Delete", routeScript: 'deleteScript, icon: ROM_routeDeleteIcon},
       ],
     GetTargetInfo:
       func(targetType)
       begin
       	// the targetType will always be 'routing for routing operations
       	// This function is also called for filing and find
       	
       	local theFrame := {
       		target: { 
       			class: kMyMainDataDefSym,  // set unique class (and register viewDefs on this symbol!)
       			data: theLine.text
       		},
       		targetView: theLine,
       		appSymbol: kAppSymbol,
       	};
       	// Note: if the target is a soup entry, then also include a targetStore slot for use by filing...
       	
       	theFrame;
       end,
     calmScript:
       func(target, targetView)
       	begin
       	   local oldText := theLine.text;
       	   :SysBeep();
       	   SetValue(theLine, 'text, "Mellow out! (drink peppermint tea?)");
       	   RefreshViews();
       	   theLine:Hilite(true);
       	   sleep(120);
       	   theLine:Hilite(nil);
       	   SetValue(theLine, 'text, oldText);
       	end,
     deleteScript:
       func(target, targetView)
       begin
       	if kDebugOn then print("Delete...");
       	
       	SetValue(theLine, 'text, clone(""));
       end,
     duplicateScript:
       func(target, targetView)
       begin
       	if kDebugOn then print("Duplicate...");
       	
       	// Umm...this is how our app will show duplication, but you would normally do 
       	// duplication by DeepCloning the target and creating a new soup entry...
       	SetValue(theLine, 'text, theLine:GetRichString() && "and" && theLine:GetRichString() );
       end,
     viewJustify: 48,
     debug: "printFormatView",
     viewClass: 74
    };
AddStepForm(myBase, printFormatView);
StepDeclare(myBase, printFormatView, 'printFormatView);

theLine :=
    {viewBounds: {left: 10, top: 0, right: -10, bottom: -20},
     text: "Text",
     viewJustify: 240,
     debug: "theLine",
     _proto: @185
    };
AddStepForm(printFormatView, theLine);
StepDeclare(myBase, theLine, 'theLine);



_view004 :=
    {
     menuRightButtons:
       [
       	protoActionButton,
       ],
     _proto: @401
    };
AddStepForm(printFormatView, _view004);

// After Script for "_view004"
thisView := _view004;
thisView._proto := newtStatusBarNoClose;	// not in platform file yet ••







constant |layout_CustomRoute.t| := myBase;
// End of file CustomRoute.t
// Beginning of text file Install&RemoveScripts.f
/*
**      Newton Developer Technical Support Sample Code
**
**      Custom Route, a Newton 2.0 routing example
**
**      by J. Christopher Bell, Newton Developer Technical Support
**
**      Copyright © 1994-5 by Apple Computer, Inc.  All rights reserved.
**
**      You may incorporate this sample code into your applications without
**      restriction.  This sample code has been provided "AS IS" and the
**      responsibility for its operation is 100% yours.  You are not
**      permitted to modify and redistribute the source as "DTS Sample Code."
**      If you are going to re-distribute the source, we require that you
**      make it clear in the source that the code was descended from
**      Apple-provided sample code, but that you've made changes.
*/


InstallScript := func(partFrame)
	begin
		local myApp := partFrame.theForm;
		
		RegisterViewDef(myApp.MyFormats.viewFormat, kMyMainDataDefSym);
		RegisterViewDef(myApp.MyFormats.myFrameFormat, kMyMainDataDefSym);
	end;


// run when application and/or card with app removed
// cleanup routing stuff
RemoveScript := func(partFrame)
	begin
		UnregisterViewDef(kMyMainViewDefSym, kMyMainDataDefSym);
		UnregisterViewDef(kMyFrameViewDefSym, kMyMainDataDefSym);
	end;

// End of text file Install&RemoveScripts.f



