// Text of project ListPickerSoup written on 5/23/96 at 9:39 AM
// Beginning of text file ProjectData
/*
**      Newton Developer Technical Support Sample Code
**
**      ListPicker,  the basic listPicker displaying information from a user defined
**		soup.
**
**      by Stephen Harris, Newton Developer Technical Support
**
**      Copyright  1993-1996 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.
*/

//  This sample demonstates the use of the protoListPicker and a user defined soup.
//  The user is to display soup entries only with no editing unless one of the entries
//  will not pass the validationFrame.

// the app stuff
constant kAppTitle := "The List Picker";

//--------------

// Soup stuff
constant kSoupName := kPackageName;
Constant kSoupIndexes := '[{structure: slot,path: first, type: string}];

// used in the listPicker querySpec slot
constant kQuerySpec := '{type: index, indexpath: first};

// used by the regUnionSoup function for soup design
DefConst('kSoupDef, {
	name: kSoupName,
	username: kAppName,
	ownerApp: kAppSymbol,
	userDescr: "Soup for the List Picker sample",
	indexes: kSoupIndexes
}) ;

//--------------


// Random  entry generator.
DefConst('kCanonicalEntry,
   {
		first: nil,
		second: nil,

	});


DefConst('kRandomDataGeneratorFunc, func()
	begin
		local item := clone(kCanonicalEntry);

		item.first := Capitalize(GetRandomWord(4, 12));
		item.second := Capitalize(GetRandomWord(4, 12));
		item;
	end);
		
//--------------

// DeleteScript->  this only gets called when your app is deleted and not
// when the card is removed.

SetPartFrameSlot('DeletionScript, func()
	begin
		//Remove the soup from the stores
		foreach store in GetStores() do 
		begin
			local theSoup := store:GetSoup(kSoupName);
			if theSoup then
			   theSoup:RemoveFromStore();
		end;
	end
);

// Install/Remove Scripts

InstallScript := func(part)
begin	
	RegUnionSoup(kAppSymbol, kSoupDef) ;
end;

RemoveScript := func(part)
begin 
	// unregister autocreation of soup
	UnRegUnionSoup(kSoupName, kAppSymbol);
end;

// End of text file ProjectData
// Beginning of text file pickerDef.f
//Copyright  1993-1996 by Apple Computer, Inc.  All rights reserved
//listPicker defs


//used to define the pickerDef at compile time.  May include all of the slots for the pickerDef.
//In the mylistPicker.pickerDef proto to this DataDef.  

DefConst('kMyBasicSoupDataDef,{
	 _proto: protoNameRefDataDef,		// Required
	 validationFrame: nil,				// used if editing will be supported
	 name:  "Random Data ",				// name at top left of picker if foldersTabs are present
	});
	

//************
// End of text file pickerDef.f
// Beginning of file ListPickerSoup.t
appBase :=
    {viewBounds: {left: 0, top: 0, right: 240, bottom: 320},
     viewFlags: 1,
     viewFormat: 0,
     declareSelf: 'base,
     viewJustify: 0,
     viewSetupFormScript:
       func()
       begin
       	// set up app display
       	local b := GetAppParams();
       	self.viewBounds := RelBounds(b.appAreaLeft, b.appAreaTop,
       		b.appAreaWidth, b.appAreaHeight);
       	
       	if NOT reOrienting then
       	begin
       		// set up soup
       		theSoup := GetUnionSoupAlways(kSoupName); 
       		local theCursor := theSoup:Query(kQuerySpec);
       		
       		// make random Entries if there are none in the soup
       		if theCursor:entry() = NIL then
       			:GenerateData() ;
       	end;
       end,
     GenerateData:
       func()
       begin
       	// get a local for the array invariant
       	local mySoup := theSoup ;
       
       	for i := 0 to 19 do
       		mySoup:AddToDefaultStore(call kRandomDataGeneratorFunc with ());
       end,
     theSoup: nil,
     viewQuitScript:
       func()
       begin
       	// memory reclamation project
       	theSoup:= nil;
       end,
     ReorientToScreen:
       func()
       begin
       	reOrienting := true ;
       	:syncView();
       	
       	// stop the selected array from being purged of
       	// non-selected items when it closes
       	myListPicker.dontPurge:= true;
       
       	:RedoChildren();
       
       	// reset the purge slot so that listPicker
       	// will purge when it closes normally
       	myListPicker.dontPurge:= nil;
       	reOrienting := true ;
       end,
     reOrienting: nil,
     debug: "appBase",
     viewClass: 74
    };

_view000 :=
    {title: kAppTitle,
     viewBounds: {left: 0, top: 0, right: 150, bottom: 20},
     _proto: @229
    };
AddStepForm(appBase, _view000);



mylistpicker :=
    {viewFlags: 513,
     viewBounds: {left: 0, top: 20, right: 0, bottom: -70},
     viewJustify: 240,
     pickerDef:
       {
       	_proto: kMyBasicSoupDataDef,	// defined in the pickerDef.f file
       	class:  'nameRef,      		// always include 
       	columns:
       	[	// Column 1
       		{
       			fieldPath: 'first,	// field to display in column
       			tapWidth: 100,			// width for checkbox & name combined, offset from the right margin		
       			doRowHilite: true,
       		},
       		
       		// Column 2
       		{
       			fieldPath: 'second,	// field to display in column
       			tapWidth: 0, 			// width from preceeding column to right bounds
       			doRowHilite: true,
       		},
       	],
       	
       },
     querySpec: kQuerySpec,
     soupToQuery: kSoupName,
     selected: nil,
     suppressNew: true,
     suppressCloseBox: true,
     suppressFolderTabs: true,
     viewQuitScript:
       func()
       begin
       	//when dealing with soups ensure that the listPicker is torn down after closing if 
       	//it will be opened again later.
       	selected:= nil;
       	inherited:viewQuitScript();
       end,
     dontpurge: true,
     debug: "mylistpicker",
     _proto: @461
    };
AddStepForm(appBase, mylistpicker);
StepDeclare(appBase, mylistpicker, 'mylistpicker);



_view001 :=
    {
     buttonClickScript:
       func()
       begin
       	:GenerateData();
       	mylistPicker:Update();	
       end,
     text: "Add more Data",
     viewBounds: {left: 0, top: 6, right: 100, bottom: 24},
     viewJustify: 8396950
     ,
     _proto: @226
    };
AddStepForm(appBase, _view001);



_view002 :=
    {
     buttonClickScript:
       func()
       begin
       	whatSelected:open();
       end,
     text: "What is selected",
     viewBounds: {left: 0, top: 6, right: 100, bottom: 24},
     viewJustify: 8396950
     ,
     _proto: @226
    };
AddStepForm(appBase, _view002);



_view003 := {_proto: @401};
AddStepForm(appBase, _view003);



whatSelected :=
    {viewBounds: {left: -1, top: 72, right: 106, bottom: 188},
     debug: "whatSelected",
     _proto: @180
    };
AddStepForm(appBase, whatSelected);
StepDeclare(appBase, whatSelected, 'whatSelected);

_view004 :=
    {
     buttonClickScript:
       func(textIndex)
       begin
       	print("selected index " & textIndex);
       end,
     viewBounds: {left: 0, top: 0, right: 0, bottom: 116},
     viewFont: ROM_fontSystem9,
     viewLines: 6,
     viewSetupFormScript:
       func()
       begin
       	// call the getSelected fucntion with true to return only the items
       	// that are currently selected'
       	// NOTE: as items are selected nameRef's are created and put in the selected array. 
       	// GetSelected returns an array of the selected items and removes all other nameRef's
       		
       	local myArray := [];
       	foreach item in myListPicker:getSelected(true) do
       		AddArraySlot(myArray,(item.first && item.second));
       
       	self.listItems := myArray;
       
       	:SetupList();
       end,
     listItems: nil,
     viewJustify: 48,
     viewFormat: 1,
     useScrollers: true,
     _proto: @228
    };
AddStepForm(whatSelected, _view004);





// After Script for "appBase"
thisView := appBase;
// Copyright  1993-1996 by Apple Computer, Inc.  All rights reserved.



constant |layout_ListPickerSoup.t| := appBase;
// End of file ListPickerSoup.t



