// Text of project PreeferMadnessTNG written on 2/19/96 at 8:17 PM
// Beginning of text file AppConstants.f
// Copyright  1993 - 1996 Apple Computer, Inc. All rights reserved.
/*
	Application constants for PreeferMadnessTNG
	
	constants for:
		app preferences
		start sound
*/


// prefs constants

DefConst('kPrefsFrame,		// default prefs settings
	{tag: kAppString,
	 version: 1,
	 startWithSound: nil,
	 onePrefOnly: true,
	 philospherIndex: 0,
	 });

constant kPreferenceTitle := "Preefer Preferences" ;
constant kApplicationPreferenceTitle := "Application" ;


// philisophical constants
DefConst('kPhilPicker, 
				["None", 'pickSeparator, "Plato", "Aristotle",
				"Descartes", "Mill", "Tsunemoto", "Nietzsche",
				"Marx", "Wittgenstien", "Heidegger", "Russell", "Popper", "Dr. Llama"]) ;


// start sound
r := OpenResFileX("PreeferMadness.rsrc");
DefConst('kStartSound, GetSound("Oh la la")) ;
CloseResFileX(r) ;
// End of text file AppConstants.f
// Beginning of file protoPrefsOverview
_userproto000 :=
    {viewBounds: {left: 0, top: 0, right: 224, bottom: 220},
     viewFlags: 548,
     viewJustify: 80,
     prefItemsArray:
       // user of this proto MUST provide this!
       // an array of protoPrefRollItem's that are the preferences
       // of the application
       nil,
     OpenPref:
       DefConst( 'kPrefFrame, {
       		_proto: protoFloatngo,
       
       		viewJustify: vjParentLeftH + vjParentTopV,
       
       		viewBounds: nil,
       
       		stepChildren: nil,
       		
       		ReorientToScreen: ROM_DefRotateFunc,
       
       		viewSetupDoneScript: func()
       		begin
       		local myBounds := :LocalBox();
       		local prefBounds ;
       
       		foreach kid in :ChildViewFrames() do
       			if kid._proto = prefPanelProto then
       				prefBounds := kid:LocalBox() ;
       
       		if prefBounds AND myBounds.bottom < prefBounds.bottom then
       			SetValue(self, 'viewBounds,	RelBounds(0, 0, myBounds.right, prefBounds.bottom)) ;
       		end,	
       		});
       
       func(prefPanel)
       begin
       	local item := BuildContext( kPrefFrame );
       	
       	// pref panel will be attached to the root view, but want
       	// to center it inside the parent view or overview
       	item.viewBounds := RelBounds(:GlobalBox().left, 
       		:GlobalBox().top + :LocalBox().bottom DIV 2 - prefPanel.height DIV 2,
       		:LocalBox().right, prefPanel.height) ;
       
       	item.stepChildren := item.viewChildren;
       	item.viewChildren := [prefPanel];
       	item.prefPanelProto := prefPanel ;
       
       	item:Open();
       	return item;
       end,
     openChild: nil,
     viewQuitScript:
       func()
       begin
       	if openChild then
       		openChild:Close();
       	inherited:?viewQuitScript();		// this method might be defined internally
       end,
     _proto: @180
    };

_view000 :=
    {viewBounds: {left: 14, top: 4, right: 94, bottom: 20},
     viewSetupFormScript:
       func()
       begin
       	if title then
       		self.title := title ;
       	else
       		self.title := defaultTitle ;
       
       	inherited:?viewSetupFormScript();		// this method is defined internally
       end,
     defaulttitle: "Preferences",
     _proto: @229
    };
AddStepForm(_userproto000, _view000);



_view001 :=
    {viewBounds: {left: 0, top: 16, right: 0, bottom: -16},
     viewFlags: 517,
     viewJustify: 240,
     cursor:
       {
       	items: nil,
       
       	index: 0,
       	
       	NumItems: func()
       		Length(items),
       
       	Entry: func()
       	begin
       		if index < Length(items) then
       		items[index];
       	end,
       
       	Next: func()
       		if index < Length(items)-1 then
       		begin
       			index := index + 1;
       			items[index];
       		end,
       		
       	Move: func(delta)
       	begin
       		index := Min(Max(index + delta, 0), Length(items)-1) ;
       		items[index];
       	end,
       	
       	Reset: func()
       		index := 0,
       
       	Clone: func()
       		Clone(self),
       		
       	GetIndexEntry: func(theIndex)
       		items[theIndex],
       },
     selectIndent: 0,
     Abstract:
       constant kIconPrefSpace := 25;
       
       r := OpenResFileX("PreeferMadness.rsrc");
       DefConst('kPrefsDefaultIcon, GetPictAsBits("defaultPrefIcon", nil));
       CloseResFileX(r);
       
       func(pref,bbox)
       begin
       	[
       	MakeText(pref.overview, bbox.left + kIconPrefSpace, bbox.top, bbox.right, bbox.top + 11),
       	if pref.icon then
       		OffsetShape(MakeShape(pref.icon), 
       			bbox.left + (kIconPrefSpace - pref.icon.bounds.right) DIV 2, 
       			bbox.top + (kIconPrefSpace - pref.icon.bounds.bottom) DIV 2);
       	else
       		OffsetShape(
       			MakeShape(kPrefsDefaultIcon),
       				bbox.left + (kIconPrefSpace - kPrefsDefaultIcon.bounds.right) DIV 2, 
       				bbox.top + kPrefsDefaultIcon.bounds.bottom DIV 2),
       	]
       end,
     HitItem:
       func(hitIndex, xcoord, ycoord)
       begin
       	// find the actual item hit
       	// i.e., compensate for any scrolling offset
       	local realIndex := hitIndex + topIndex;
       	if openChild then
       		openChild:Close();
       	
       	openChild := :OpenPref(cursor:GetIndexEntry(realIndex));
       end,
     Scroller:
       func(dir)
       begin
       	local newIndex := Min(Max(topIndex + dir, 0), cursor:NumItems()-1);
       	local realDir := topIndex - newIndex ;
       
       	// can get called when the list is at the top or
       	// bottom, so check if we need to do real work
       	if realDir <> 0 then
       	begin
       		// update the index for the item at the top of the display
       		topIndex := newIndex ;
       
       		// let viewSetupChildrenScript do the work
       		:RedoChildren();
       	end ;
       end,
     topIndex:
       // index of the item that is currently at the
       // top of the displayed items
       0,
     viewSetupChildrenScript:
       // Required
       // use SetupAbstracts to setup the pictures shown
       func()
       begin
       	// can get called when a scroll occurs
       	// so clone off the cursor and move
       	// it so that the first item is the
       	// current top of the displayed items
       	local curs := cursor:Clone();
       
       	curs:Reset();
       	curs:Move(topIndex);
       	:SetupAbstracts(curs);
       end,
     viewSetupFormScript:
       func()
       begin
       	// only need to setup cursor if not already there
       	// i.e., viewSetupFormScript may get called multiple times
       	if IsReadOnly(cursor) then
       	begin
       		cursor := {_proto: cursor,
       					  items: prefItemsArray};
       	end ;
       end,
     nothingCheckable: true,
     autoDeselect: true,
     lineHeight: 16,
     _proto: @191
    };
AddStepForm(_userproto000, _view001);



// After Script for "_userproto000"
thisView := _userproto000;
// Copyright 1996 Apple Computer, Inc. All rights reserved.
nil


constant |layout_protoPrefsOverview| := _userproto000;
// End of file protoPrefsOverview
// Beginning of file PreeferFormulaItem

// Before Script for "_userproto001"
// Copyright  1993-6 Apple Computer, Inc. All rights reserved.


_userproto001 :=
    {viewBounds: {left: 0, top: 22, right: 240, bottom: 88},
     overview: "Preefer Formula Overview Title",
     title: "Preefer Formula Panel Title",
     _proto: @180
    };

_view002 := {viewBounds: {left: 5, top: 6, right: 77, bottom: 22}, _proto: @229}
    ;
AddStepForm(_userproto001, _view002);



_view003 :=
    {viewBounds: {left: 16, top: 32, right: 200, bottom: 56},
     viewFlags: 1,
     text: "42",
     label: "Enter the answer:",
     entryFlags: 10753,
     viewSetupFormScript:
       func()
       begin
       	// make the entry line ignore spaces
       	inherited:?viewSetupFormScript();
       	entryLine.textFlags := vNoSpaces ;
       end,
     _proto: @189
    };
AddStepForm(_userproto001, _view003);




constant |layout_PreeferFormulaItem| := _userproto001;
// End of file PreeferFormulaItem
// Beginning of file PreeferAutoPrefsItem

// Before Script for "PrefsPanelBase"
// Copyright  1993-6 Apple Computer, Inc. All rights reserved.


PrefsPanelBase :=
    {viewBounds: {left: 0, top: 0, right: 0, bottom: 40},
     overview: "Preefer Prefs",
     text: "Preefer Prefs",
     prefsEntry: nil,
     viewQuitScript:
       func()
       begin
       
        //nil out slots to allow for GC
        prefsEntry := nil;
       
       end,
     height: 40,
     icon: GetPictAsBits("SmallPreeferIcon", nil),
     viewSetupFormScript:
       func()
       begin
         prefsEntry := GetAppPrefs(kAppSymbol, kPrefsFrame) ;
       
         //IMPORTANT to call the inherited ViewSetupDoneScript in our proto
         inherited:?viewSetupFormScript();
       end,
     debug: "PrefsPanelBase",
     _proto: @385
    };

soundCheckbox :=
    {text: "Start with sound",
     viewBounds: {left: 24, top: 16, right: 192, bottom: 32},
     buttonValue: true,
     valueChanged:
       func()
       begin
         prefsEntry.startWithSound := viewValue;
         EntryChange(prefsEntry);
       end,
     viewValue: nil,
     viewSetupFormScript:
       func()
       begin
       	viewValue := prefsEntry.startWithSound ;
       end,
     debug: "soundCheckbox",
     _proto: @164
    };
AddStepForm(PrefsPanelBase, soundCheckbox);
StepDeclare(PrefsPanelBase, soundCheckbox, 'soundCheckbox);




constant |layout_PreeferAutoPrefsItem| := PrefsPanelBase;
// End of file PreeferAutoPrefsItem
// Beginning of file PreeferPhilPrefItem

// Before Script for "_userproto002"
// Copyright  1993-6 Apple Computer, Inc. All rights reserved.


_userproto002 :=
    {viewBounds: {left: 0, top: 0, right: 0, bottom: 35},
     height: 35,
     overview: "Philisophical Leaning",
     text: "Philisophical Leaning",
     icon: nil,
     prefsEntry: nil,
     viewSetupFormScript:
       func()
       begin
       	prefsEntry := GetAppPrefs(kAppSymbol, kPrefsFrame);
       	inherited:?viewSetupFormScript();
       end,
     _proto: @385
    };

_view004 :=
    {viewBounds: {left: 25, top: 17, right: 207, bottom: 39},
     labelCommands: kPhilPicker,
     text: "Philosopher",
     textSetup:
       func()
       begin
       	labelCommands[prefsEntry.philosopherIndex] ;
       end,
     labelActionScript:
       func(cmd)
       begin
       	prefsEntry.philosopherIndex := cmd;
       	EntryChange(prefsEntry);
       	GetRoot().(kAppSymbol):RevisePhilosophy();
       end,
     _proto: @190
    };
AddStepForm(_userproto002, _view004);




constant |layout_PreeferPhilPrefItem| := _userproto002;
// End of file PreeferPhilPrefItem
// Beginning of file PreeferAppPrefsItems

// Before Script for "_userproto003"
// Copyright  1993-6 Apple Computer, Inc. All rights reserved.


_userproto003 :=
    {viewBounds: {left: 0, top: 0, right: 0, bottom: 50},
     height: 50,
     icon: nil,
     prefsEntry: nil,
     viewSetupFormScript:
       func()
       begin
       	prefsEntry := GetAppPrefs(kAppSymbol, kPrefsFrame);
       
       	// hack code, see the comment in the overview slot
       	if prefsEntry.onePrefOnly then
       		overview := kPreferenceTitle ;
       
       	inherited:?viewSetupFormScript();
       end,
     overview:
       // Some hacking occurs here :-)
       
       // Since this sample demos either one pref or multiple prefs,
       // we need to change the title of this preference on the fly.
       // However, the protoPrefsOverview will construct the abstract for
       // the overview based on an uninstantiated template... i.e., the
       // viewSetupFormScript of this template will not run before the
       // abstract is constructed.
       
       // The bottom line is that this is the overview text for the
       // multiple pref case.
       
       kApplicationPreferenceTitle,
     text: "Is this slot used?",
     _proto: @385
    };

soundCheckbox :=
    {text: "Start with sound",
     viewBounds: {left: 32, top: 16, right: 200, bottom: 32},
     buttonValue: true,
     valueChanged:
       func()
       begin
         prefsEntry.startWithSound := viewValue;
         EntryChange(prefsEntry);
       end,
     viewSetupFormScript:
       func()
       begin
       	viewValue := prefsEntry.startWithSound ;
       end,
     debug: "soundCheckbox",
     _proto: @164
    };
AddStepForm(_userproto003, soundCheckbox);



onePrefItemOnlyCheckbox :=
    {text: "Only One Preference",
     viewBounds: {left: 32, top: 32, right: 200, bottom: 48},
     buttonValue: true,
     valueChanged:
       func()
       begin
         prefsEntry.onePrefOnly := viewValue;
         EntryChange(prefsEntry);
       end,
     viewSetupFormScript:
       func()
       begin
       	viewValue := prefsEntry.onePrefOnly ;
       end,
     debug: "onePrefItemOnlyCheckbox",
     _proto: @164
    };
AddStepForm(_userproto003, onePrefItemOnlyCheckbox);




constant |layout_PreeferAppPrefsItems| := _userproto003;
// End of file PreeferAppPrefsItems
// Beginning of file PreeferSinglePrefFloater.t

// Before Script for "_view005"
// Copyright  1996 Apple Computer, Inc. All rights reserved.


_view005 :=
    {viewBounds: {left: 0, top: 0, right: 224, bottom: 50},
     viewJustify: 80,
     _proto: @180
    };

_view006 := {_proto: _userproto003};
AddStepForm(_view005, _view006);




constant |layout_PreeferSinglePrefFloater.t| := _view005;
// End of file PreeferSinglePrefFloater.t
// Beginning of file PreeferMultiplePrefsOverview.t

// Before Script for "_view007"
// Copyright  1993-6 Apple Computer, Inc. All rights reserved.


_view007 :=
    {title: kPreferenceTitle,
     prefItemsArray:
       // MUST provide this!
       // an array of protoPrefRollItem's that are the preference items
       
       [GetLayout("PreeferAppPrefsItems"), GetLayout("PreeferPhilPrefItem")];,
     viewBounds: {left: 0, top: 0, right: 224, bottom: 65},
     _proto: _userproto000
    };


constant |layout_PreeferMultiplePrefsOverview.t| := _view007;
// End of file PreeferMultiplePrefsOverview.t
// Beginning of file PreeferMadness.t

// Before Script for "MyBase"
// Copyright  1993-6 Apple Computer, Inc. All rights reserved.


MyBase :=
    {viewBounds: {left: 0, top: 0, right: 240, bottom: 336},
     viewSetupFormScript:
       func()
       begin
       	// Resize the app to fill the screen.
       	// This will look very odd on watch-size or whiteboard-sized Newton
       	// devices, but then again this sample isn't exactly useful to a user...
       	local l := GetAppParams() ;
       	self.viewBounds := RelBounds(l.appAreaLeft, l.appAreaTop, l.appAreaWidth, l.appAreaHeight);
       	
       	prefsEntry := GetAppPrefs(kAppSymbol, kPrefsFrame) ;
       	if prefsEntry.startWithSound then
       	   PlaySound(kStartSound);
       end,
     viewQuitScript:
       func()
       begin
       
        //nil out slots to allow for GC
        prefsEntry := nil;
       
       end,
     prefsEntry:
       // will be the our entry from the system preferences soup
       // set in protoApp.viewSetupFormScript
       nil,
     viewFlags: 4,
     viewFormat: 328017,
     declareSelf: 'base,
     currentPrefsVersion: 1,
     DoInfoPrefs:
       func()
       begin
       	// if we have multiple preferences, open a prefs overview
       	// otherwise just open the pref
       
       	if prefsEntry.onePrefOnly then	
       		SinglePref:Open();
       	else
       		// open up the preferences overview
       		PrefsOverview:Open();
       
       end,
     RevisePhilosophy:
       func()
       begin
       	philView:RevisePhilosophy();
       end,
     ReorientToScreen: ROM_DefRotateFunc,
     viewJustify: 16,
     title: kAppName,
     debug: "MyBase",
     viewClass: 74
    };

_view008 :=
    {viewBounds: {left: -6, top: 2, right: 74, bottom: 18}, _proto: @229};
AddStepForm(MyBase, _view008);



_view009 := {menuLeftButtons: [protoInfoButton], _proto: @401};
AddStepForm(MyBase, _view009);



PhilView :=
    {text: "",
     viewBounds: {left: 2, top: 42, right: 242, bottom: 218},
     viewJustify: 0,
     viewFont: {family: 'NewYork, face: kFaceBold, size: 14},
     viewSetupFormScript:
       func()
       begin
       	if prefsEntry.philosopherIndex then
       		text := philText[prefsEntry.philosopherIndex] ;
       	else
       	begin
       		prefsEntry.philosopherIndex := 0 ;
       		EntryChange(prefsEntry) ;
       		text := "";
       	end;
       end,
     philText:
       ["","",
       // Plato
       "There is an objective ideal Llama form of which all manifistations are just shadows",
       // Aristotle
       "Every Llama can be classified into one of a finite number of sub-types of Llamas",
       // Rene Descartes
       "I think, therefore there must be Llamas",
       // John Stuart Mill
       "It is up to the free will of the individual to believe in Llamas or not",
       // Tsunemoto
       "It does not matter if Llamas exist or not, it only matters than an individual live by the way of courage",
       // Nietzsche
       "The Llama is dead!",
       // Marx
       "To each Llama according to its needs, from each according to its abilities",
       // Wittgenstien
       "It is futile to talk about Llamas because we will never agree on what the concept means",
       // Heidegger
       "There is no objective ideal Llama, each individual's concept of Llama is made up of their own experiences of and about Llamas",
       // Russell
       "With the help of mathematics we have discovered that Llamas are not valid constructs",
       // Popper
       "The question of the verification of Llamas depends upon the ability to falsify that Llamas do not exist",
       // Dr. Llama
       "I am the Llama",
       ],
     RevisePhilosophy:
       func()
       begin
       	SetValue(self, 'text, philText[prefsEntry.philosopherIndex]) ;
       end,
     debug: "PhilView",
     _proto: @218
    };
AddStepForm(MyBase, PhilView);
StepDeclare(MyBase, PhilView, 'PhilView);



_view010 :=
    {
     text:
       "Click on the Info button and choose \"Prefs\" to set the application preferences"
     ,
     viewBounds: {top: -65, left: 10, right: 154, bottom: -20},
     viewJustify: 128,
     viewFont: ROM_fontsystem10,
     _proto: @218
    };
AddStepForm(MyBase, _view010);


PrefsOverview := LinkedSubview(_view007,
    {viewBounds: {left: 2, top: 218, right: 122, bottom: 258},
     debug: "PrefsOverview"
    });
AddStepForm(MyBase, PrefsOverview);
StepDeclare(MyBase, PrefsOverview, 'PrefsOverview);
SinglePref := LinkedSubview(_view005,
    {viewBounds: {left: 130, top: 218, right: 234, bottom: 258},
     debug: "SinglePref"
    });
AddStepForm(MyBase, SinglePref);
StepDeclare(MyBase, SinglePref, 'SinglePref);


constant |layout_PreeferMadness.t| := MyBase;
// End of file PreeferMadness.t
// Beginning of text file PackageStuff.f
// Copyright  1993 - 1996 Apple Computer, Inc. All rights reserved.
/*
	package and part stuff
	
	Includes:
		part frame additions
		InstallScript
		RemoveScript	

 */


// These calls to SetPartFrameSlot need to be done after the layout has
// been compiled, which is why this file is last in the build sequence.

SetPartFrameSlot('PreeferFormulaItem, GetLayout("PreeferFormulaItem"));
SetPartFrameSlot('PreeferAutoPrefsItem, GetLayout("PreeferAutoPrefsItem"));


InstallScript := func(partFrame)
begin
	// NOTE: only auto parts would register their prefs with the
	//			system, and only if there was no other way to present
	//			preferences. This is done here for demonstration purposes
	//			only.
	RegPrefs(kAppSymbol, partFrame.PreeferAutoPrefsItem);

	// register the formula.
	// NOTE: can not use GetLayout directly in a form part installScript
	//       since the InstallScript is EnsureInternal'd. If we did do
	//       a GetLayout call, the layout would be copied into the NS Heap!
	RegFormulas(kAppSymbol, partFrame.PreeferFormulaItem);

end;


RemoveScript := func(partFrame)
begin
	// NOTE: only autoparts need to register system prefs... see above
	UnregPrefs(kAppSymbol);

	//unregister the formula
	UnregFormulas(kAppSymbol);

end;
// End of text file PackageStuff.f



