// Text of project ListPickerIcon written on 5/23/96 at 9:43 AM
// Beginning of text file ProjectData
/*
**      Newton Developer Technical Support Sample Code
**
**      ListPicker,  This sample illistrates displaying soup entries and icons.
**
**      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 icon in the first column.

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

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

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

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

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

//-------icon

rFile := OpenResFileX(home & "icons.rsrc");
defconst('kTreeIcon, GetPictAsBits("tree",nil)) ;
CloseResFileX(rFile);

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


// 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 listPicker.pickerDef proto to this DataDef.  
DefConst('kMySoupDataDef,{
		 _proto: protoNameRefDataDef,		// required
		 validationFrame: nil,				// used if editing will be supported
		 name:  "Random Data ",				// name at top left of picker if folderTabs are present
		 primaryPath: 'first,				// uses the path in an item for the sort order
		 
		// need to define Get method in either of 2 circumstances:
		// 1. the value for sorting is different from the default vaule.
		//	  e.g. last name first
		// 2. if any of the slots in your soup/array are not text slots
		//	  and you need to display the slot in a
		//    particular format. e.g., a currency string or shape.
		Get: func(item, fieldPath, format)
		begin
			if fieldPath = 'icon then
				begin
						makeShape(kTreeIcon);
				end;
			else
				inherited:Get(item,fieldPath,format);
		end,
		});
		

//************
// End of text file pickerDef.f
// Beginning of file ListPickerIcon.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,
     title: kAppTitle,
     viewQuitScript:
       func()
       begin
       	//memory reclamation
       	theSoup:= nil;
       end,
     theSoup: nil,
     ReorientToScreen:
       func()
       begin
       	reOrienting := true ;
       	:syncView();
       	:RedoChildren();
       	reOrienting := nil ;
       end,
     reOrienting: nil,
     debug: "appBase",
     viewClass: 74
    };

_view000 :=
    {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: kMySoupDataDef,			    //your defined pickerDef
       	class:  'nameRef,					//required
       	
       	//the items to be displayed in the listPicker
       	columns:
       	[
       		// Column 1
       		{
       			fieldPath: 'icon,			// field to display in column
       			tapWidth: 50,				// width for checkbox & name combined
       			
       			doRowHilite: true,
       			optional: true,			// set to allow for selecting of rows
       											// when columns are not complete
       		},
       		
       		// Column 2
       		{
       			fieldPath: 'first,
       			tapWidth: 80,
       			
       			doRowHilite: true,
       		},
       		
       		// Column 3
       		{
       			fieldPath: 'second,
       			tapWidth: 0,
       			
       			doRowHilite: true,
       		},
       		
       	],
       
       },
     querySpec: kQuerySpec,
     soupToQuery: kSoupName,
     selected:  nil,
     suppressCloseBox: true,
     suppressNew: true,
     lineHeight: 16,
     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,
     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_ListPickerIcon.t| := appBase;
// End of file ListPickerIcon.t



