// MPMorph - Amiga Morphing program
// Copyright (C) © 1993  Topicsave Limited

// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

// mpaddock@cix.compulink.co.uk

// include precompiled headers (if not already)
#ifndef MPMORPH_AMIGA_H
#include "MPMorph-amiga.h"
#endif
#ifndef MPMORPH_H
#include "MPMorph.h"
#endif

/* Remove menus from all windows allowing
 * them to be updated
 */
void
RemoveMenus(void) {
	if (MPMorphWnd) {
		ClearMenuStrip(MPMorphWnd);
	}
	if (ControlWindow) {
		ClearMenuStrip(Pic1.Win);
		ClearMenuStrip(Pic2.Win);
		ClearMenuStrip(ControlWindow);
	}
}

/* Add the menus back again after
 * calling RemoveMenus() and updating
 */
void
AddMenus(void) {
	if (MPMorphWnd) {
		ResetMenuStrip(MPMorphWnd,MPMorphMenus);
	}
	if (ControlWindow) {
		ResetMenuStrip(ControlWindow,MyMenu);
		ResetMenuStrip(Pic1.Win,MyMenu);
		ResetMenuStrip(Pic2.Win,MyMenu);
	}
}

/* UnZoom the image windows
 * i.e. currently displayed 2x and
 * want to display 1x
 */
void
UnZoom(void) {
	// Already zoomed - set number of windows resized (change window box may not work)
	// see the main loop on how the messages are then handled
	changedboxcount = 0;
	// Copy image 1 to superbitmap and resync
	BltBitMap(Pic1.MPi->BitMap,0,0,Pic1.BitMap,0,0,Pic1.MPi->Width,Pic1.MPi->Height,0xC0,0xff,NULL);
	LockLayerRom(Pic1.Win->RPort->Layer);
	CopySBitMap(Pic1.Win->RPort->Layer);
	UnlockLayerRom(Pic1.Win->RPort->Layer);
	/* Resize gadgets and window (if too big)
	 * Window limits are reset and points
	 * redrawn with IDCMP_CHANGEWINDOW
	 */
	SetGadgetAttrs(Pic1.BotGad, Pic1.Win, NULL,
						PGA_Total,	Pic1.MPi->Width,
						TAG_END);
	SetGadgetAttrs(Pic1.SideGad, Pic1.Win, NULL,
						PGA_Total,	Pic1.MPi->Height,
						TAG_END);
	ChangeWindowBox(Pic1.Win,Pic1.Win->LeftEdge,Pic1.Win->TopEdge,
						 min(Pic1.Win->Width,Pic1.MPi->Width+Pic1.Screen->WBorLeft+SIZEIMAGE_W(n)),
						 min(Pic1.Win->Height,Pic1.MPi->Height+Pic1.Screen->WBorTop + Pic1.Screen->Font->ta_YSize+1+SIZEIMAGE_H(n)));
	// Do the same of image/window2
	BltBitMap(Pic2.MPi->BitMap,0,0,Pic2.BitMap,0,0,Pic2.MPi->Width,Pic2.MPi->Height,0xC0,0xff,NULL);
	LockLayerRom(Pic2.Win->RPort->Layer);
	CopySBitMap(Pic2.Win->RPort->Layer);
	UnlockLayerRom(Pic2.Win->RPort->Layer);
	SetGadgetAttrs(Pic2.BotGad, Pic2.Win, NULL,
						PGA_Total,	Pic2.MPi->Width,
						TAG_END);
	SetGadgetAttrs(Pic2.SideGad, Pic2.Win, NULL,
						PGA_Total,	Pic2.MPi->Height,
						TAG_END);
	ChangeWindowBox(Pic2.Win,Pic2.Win->LeftEdge,Pic2.Win->TopEdge,
						 min(Pic2.Win->Width,Pic2.MPi->Width+Pic2.Screen->WBorLeft+SIZEIMAGE_W(n)),
						 min(Pic2.Win->Height,Pic2.MPi->Height+Pic2.Screen->WBorTop + Pic2.Screen->Font->ta_YSize+1+SIZEIMAGE_H(n)));
	// Wait till its all done (why?) and reset Zoom
	WaitBlit();
	Zoom = 0;
}

/* Zoom the image windows
 * i.e. currently displayed 1x and
 * want to display 2x
 */
void
ReZoom(void) {
	/* Currently normal size
	 * scale image 1, set window max size
	 * redo gadgets and copy to superbitmap
	 */
	BitMapScale(&(Pic1.BitScaleArgs));
	WindowLimits(Pic1.Win,0,0,(Pic1.MPi->Width<<1)+Pic1.Screen->WBorLeft+SIZEIMAGE_W(n),
					(Pic1.MPi->Height<<1)+Pic1.Screen->WBorTop + Pic1.Screen->Font->ta_YSize+1+SIZEIMAGE_H(n));
	SetGadgetAttrs(Pic1.BotGad, Pic1.Win, NULL,
						PGA_Total,	Pic1.MPi->Width<<1,
						TAG_END);
	SetGadgetAttrs(Pic1.SideGad, Pic1.Win, NULL,
						PGA_Total,	Pic1.MPi->Height<<1,
						TAG_END);
	LockLayerRom(Pic1.Win->RPort->Layer);
	CopySBitMap(Pic1.Win->RPort->Layer);
	UnlockLayerRom(Pic1.Win->RPort->Layer);
	// Same for image 2
	BitMapScale(&(Pic2.BitScaleArgs));
	WindowLimits(Pic2.Win,0,0,(Pic2.MPi->Width<<1)+Pic2.Screen->WBorLeft+SIZEIMAGE_W(n),
					(Pic2.MPi->Height<<1)+Pic2.Screen->WBorTop + Pic2.Screen->Font->ta_YSize+1+SIZEIMAGE_H(n));
	SetGadgetAttrs(Pic2.BotGad, Pic2.Win, NULL,
						PGA_Total,	Pic2.MPi->Width<<1,
						TAG_END);
	SetGadgetAttrs(Pic2.SideGad, Pic2.Win, NULL,
						PGA_Total,	Pic2.MPi->Height<<1,
						TAG_END);
	LockLayerRom(Pic2.Win->RPort->Layer);
	CopySBitMap(Pic2.Win->RPort->Layer);
	UnlockLayerRom(Pic2.Win->RPort->Layer);
	/* Reset Zoom, Check new size gadgets,
	 * and redraw all the points
	 */
	Zoom = 1;
	doNewSize(&Pic1);
	doNewSize(&Pic2);
	DrawAllPoints();
}

/* switch the check mark of a settings menu item
 * Parameters: pos        position in the settings menu
 *             settings   TRUE or FALSE
 *             remove     remove and add menus? see UpdateSaveFormat()
 */
void
SwitchMenuItem(UWORD pos,BOOL setting,BOOL remove) {
	if (remove) {
		RemoveMenus();
	}
	if (MPMorphWnd) {
		if (setting) {
			(ItemAddress(MPMorphMenus,FULLMENUNUM(M_M_SETTINGS,pos,NOSUB)))->Flags |= CHECKED;
		}
		else {
			(ItemAddress(MPMorphMenus,FULLMENUNUM(M_M_SETTINGS,pos,NOSUB)))->Flags &= ~CHECKED;
		}
	}
	if (ControlWindow) {
		if (setting) {
			(ItemAddress(MyMenu,FULLMENUNUM(M_SETTINGS,pos,NOSUB)))->Flags |= CHECKED;
		}
		else {
			(ItemAddress(MyMenu,FULLMENUNUM(M_SETTINGS,pos,NOSUB)))->Flags &= ~CHECKED;
		}
	}
	if (remove) {
		AddMenus();
	}
}

/* Handle Settings menu messages
 * Parameters: Selection   the item number
 *             pic         the current picture
 */
void
HandleSettings(UWORD Selection,struct Picture *pic) {
	ULONG OZoom;	// The old zoom mode
	// switch on the selection
	switch(ITEMNUM(Selection)) {
	case MM_ICONS:
		// Toggle Create Icons?
		CreateIcons = !CreateIcons;
		SwitchMenuItem(MM_ICONS,CreateIcons,TRUE);
		break;
	case MM_ZOOM:
		// Toggle Zoom?
		if (ControlWindow) {
			// UnZoom or ReZoom the images if open
			DisableWindows(DI_Zoom);
			if (Zoom) {
				UnZoom();
			}
			else {
				ReZoom();
			}
			EnableWindows();
		}
		else {
			if (Zoom) {
				Zoom = 0;
			}
			else {
				Zoom = 1;
			}
		}
		SwitchMenuItem(MM_ZOOM,Zoom,TRUE);
		break;
	case MM_SCREEN:
		{
			// Get public screen name
			struct MPGuiHandle *MPGuiHandle;
			char *res;
			struct Hook Hook = {
				0
			};
			char *params[2];

			DisableWindows(H_P_PS);
			Hook.h_Entry = (HOOKFUNC)HelpHook;
			params[0] = ScreenName;
			params[1] = NULL;
			if (MPGuiHandle = AllocMPGuiHandle(MPG_RELMOUSE,TRUE,
													MPG_PUBSCREENNAME,(ULONG)PubScreenName,
													MPG_HELP,(ULONG)&Hook,
													MPG_CHELP,GHelp,
													MPG_PARAMS,(ULONG)params,
													TAG_END)) {
				res = SyncMPGuiRequest(MyGetMsg(MSG_PSCRGUI),MPGuiHandle);
				if (res) {
					if (res == (char *)-1) {
						Error(MyGetMsg(MSG_MPG),MyGetMsg(MSG_OK),MPGuiError(MPGuiHandle),HE_MPG);
					}
					else {
						strcpy(ScreenName,res);
						if (ScreenName[strlen(ScreenName)-1] == ' ') {
							ScreenName[strlen(ScreenName)-1] = 0;
						}
					}
				}
			}
			else {
				Error(MyGetMsg(MSG_MPG),MyGetMsg(MSG_OK),MyGetMsg(MSG_OUTOFMEM),HE_MPG);
			}
			EnableWindows();
		}
		break;
	case MM_CUSTOM:
		// Get custom screen mode
		DisableWindows(H_PCUSTM);
		{
			ULONG id = (ULONG)INVALID_ID;
			ULONG myid = (ULONG)INVALID_ID;
			struct NameInfo buff;
			struct List List = {0};
			struct DisplayMode DisplayMode = {0};

			NewList(&List);
			DisplayMode.dm_Node.ln_Name = MyGetMsg(MSG_NONE);
         DisplayMode.dm_PropertyFlags                 	= DIPF_IS_WB;
			DisplayMode.dm_DimensionInfo.Header.StructID		= DTAG_DIMS;
			DisplayMode.dm_DimensionInfo.Header.DisplayID	= (ULONG)INVALID_ID;
			DisplayMode.dm_DimensionInfo.Header.SkipID 		= TAG_SKIP;
         DisplayMode.dm_DimensionInfo.Header.Length		= sizeof(struct DimensionInfo);
         DisplayMode.dm_DimensionInfo.MaxDepth         = 24;
         DisplayMode.dm_DimensionInfo.MinRasterWidth   = 16;
         DisplayMode.dm_DimensionInfo.MinRasterHeight  = 16;
         DisplayMode.dm_DimensionInfo.MaxRasterWidth   = 2048;
         DisplayMode.dm_DimensionInfo.MaxRasterHeight  = 2048;
         DisplayMode.dm_DimensionInfo.Nominal.MinX     = 0;
         DisplayMode.dm_DimensionInfo.Nominal.MinY     = 0;
         DisplayMode.dm_DimensionInfo.Nominal.MaxX     = 640;
         DisplayMode.dm_DimensionInfo.Nominal.MaxY     = 200;
         DisplayMode.dm_DimensionInfo.TxtOScan         = DisplayMode.dm_DimensionInfo.Nominal;
         DisplayMode.dm_DimensionInfo.MaxOScan         = DisplayMode.dm_DimensionInfo.Nominal;
         DisplayMode.dm_DimensionInfo.VideoOScan       = DisplayMode.dm_DimensionInfo.Nominal;
         DisplayMode.dm_DimensionInfo.StdOScan         = DisplayMode.dm_DimensionInfo.Nominal;
			AddHead(&List,&(DisplayMode.dm_Node));

			id = NextDisplayInfo(id);
			while ((id != INVALID_ID) && (myid == INVALID_ID)) {
				if (GetDisplayInfoData(NULL,(UBYTE *)&buff,sizeof(struct NameInfo),DTAG_NAME,id)) {
					if (!Stricmp(CustomName,buff.Name)) {
						myid = id;
					}
				}
				id = NextDisplayInfo(id);
			}
			{
				struct ScreenModeRequester *req;

				if (req = AllocAslRequest(ASL_ScreenModeRequest,NULL)) {
					if (AslRequestTags(req,
										ASLSM_Window, MPMorphWnd,
										ASLSM_TitleText, MyGetMsg(MSG_CSCRMODE),
										ASLSM_InitialDisplayID, myid,
										ASLSM_InitialDisplayDepth, CustomDepth,
										ASLSM_DoDepth, TRUE,
										ASLSM_CustomSMList, (ULONG)&List,
										ASLFR_PositiveText, MyGetMsg(MSG_SET),
										TAG_END)) {
						if (GetDisplayInfoData(NULL,(UBYTE *)&buff,sizeof(struct NameInfo),DTAG_NAME,
								req->sm_DisplayID)) {
							strcpy(CustomName, buff.Name);
						}
						else {
							strcpy(CustomName,"");
						}
						CustomDepth = req->sm_DisplayDepth;
					}
					FreeAslRequest(req);
				}
			}
		}
		EnableWindows();
		break;
	case MM_SPREVIEW:
		// Get preview script
		DisableWindows(H_P_Preview);
		if (GetAFile(PreviewScript,MyGetMsg(MSG_SELECTPRE),0,MyGetMsg(MSG_HQDMPM),MyGetMsg(MSG_SELECT))) {
			strcpy(PreviewScript,TempFilename);
		}
		EnableWindows();
		break;
	case MM_ICONSP:
		// Toggle Create prefs Icons?
		CreateIconsP = !CreateIconsP;
		SwitchMenuItem(MM_ICONSP,CreateIconsP,TRUE);
		break;
	case MM_KEEPS:
		// Toggle Keep Settings?
		KeepSettings = !KeepSettings;
		SwitchMenuItem(MM_KEEPS,KeepSettings,TRUE);
		break;
	case MM_LOADS:
		// Load new settings from requester
		LoadSettings(NULL);
		break;
	case MM_SAVES:
		// Save settings to ENV: and ENVARC:
		SaveSettings(MyGetMsg(MSG_ENVARCP));
		SaveSettings(MyGetMsg(MSG_ENVP));
		break;
	case MM_SAVESAS:
		// save settings from requester
		SaveSettings(NULL);
		break;
	case MM_RESETD:
		// Reset to default prefs
		OZoom = Zoom;	// keep current zoom
		if (!ControlWindow) {
			// if the images are open then we can not change these
			ZoomAllowed = TRUE;
		}
		// Set all other settings to defaults
		ScreenName[0] = 0;
		CreateIcons = TRUE;
		CreateIconsP = TRUE;
		KeepSettings = TRUE;
		Zoom = 0;
		strcpy(PreviewScript,MyGetMsg(MSG_DEFPRE));
		CustomName[0] = 0;
		CustomDepth = 4;
		GHelp = FALSE;
		UpdateParams(OZoom);
		break;
	case MM_LASTS:
		// Load settings from ENVARC:
		LoadSettings(MyGetMsg(MSG_ENVARCP));
		break;
	case MM_RESTORE:
		// Load settings from ENV:
		LoadSettings(MyGetMsg(MSG_ENVP));
		break;
	case MM_HELP:
		// Toggle Help?
		GHelp = !GHelp;
		SwitchMenuItem(MM_HELP,GHelp,TRUE);
		break;
	default:
		// something else ??
		break;
	}
}

#if 0
/* Request a number from the user using reqtools.library
 * Parameters: title   - title of requester
 *             number  - address of number to display and get
 *             HelpNum - help key if help required
 */
void
GetANumber(char *title,ULONG *number,ULONG HelpNum) {
	ULONG 					ret = 2;	// current gadget
	struct AmigaGuideMsg	*agm;		// amiga guide stuff
	ULONG 					signals;	// signals to wait on
	struct rtHandlerInfo	*rth;		// reqtools stuff
	// disable all the windows
	DisableWindows(HelpNum);
	// loop until Ok or Cancel pressed
	while (ret == 2) {
		ret = CALL_HANDLER;
		if (rtGetLong(number,title,NULL,
							RT_ReqHandler,		&rth,
							RT_Window,			MPMorphWnd,
							RTGL_ShowDefault,	TRUE,
							RT_Underscore,		'_',
							RTGL_GadFmt, 		handle?MyGetMsg(MSG_OKHC):MyGetMsg(MSG_OKC), // if amigaguide then help gadget
							TAG_END) == CALL_HANDLER) {
			while (ret == CALL_HANDLER) {
				if (!rth->DoNotWait) {
					// wait if possible
					signals = Wait(rth->WaitMask | ASig);
				}
				ret = rtReqHandlerA(rth,signals,NULL);
				if (ret == 2) {
					// display help if requested, 2 is only possible if help gadget is available
					help(HelpNum);
				}
				if (signals & ASig) {
					// do amigaguide stuff
				  	while (agm = GetAmigaGuideMsg(handle)) {
			 			ReplyAmigaGuideMsg(agm);
			 		}
			 	}
			}
		}
	}
	// re-enable all the windows
	EnableWindows();
}
#endif

#if 0
/* Request a string from the user using reqtools.library
 * Parameters: title   - title of requester
 *             number  - address of buffer to display and get
 *             HelpNum - help key if help required
 * See GetANumber for comments
 */
void
GetAString(char *title,char *buffer,ULONG HelpNum) {
	ULONG 					ret = 2;
	struct AmigaGuideMsg	*agm;
	ULONG 					signals;
	struct rtHandlerInfo	*rth;
	DisableWindows(HelpNum);
	while (ret == 2) {
		ret = CALL_HANDLER;
		if (rtGetString(buffer,127,title,NULL,
							RT_ReqHandler,		&rth,
							RT_Window,			MPMorphWnd,
							RTGL_ShowDefault,	TRUE,
							RT_Underscore, 	'_',
							RTGL_GadFmt, 		handle?MyGetMsg(MSG_OKHC):MyGetMsg(MSG_OKC),
							RTGS_AllowEmpty,	TRUE,
							TAG_END) == CALL_HANDLER) {
			while (ret == CALL_HANDLER) {
				if (!rth->DoNotWait) {
					signals = Wait(rth->WaitMask | ASig);
				}
				ret = rtReqHandlerA(rth,signals,NULL);
				if (ret == 2) {
					help(HelpNum);
				}
				if (signals & ASig) {
				  	while (agm = GetAmigaGuideMsg(handle)) {
			 			ReplyAmigaGuideMsg(agm);
			 		}
			 	}
			}
		}
	}
	EnableWindows();
}
#endif

/* Save the current settings
 * Parameters: filename - filename to save to
 *                        or NULL for requester
 */
void
SaveSettings(char *filename) {
	char	*ifilename = NULL;	// the actual file name
	BPTR	fh;						// file handle
	BOOL	ok = TRUE;				// Is it working?
	char	buffer[65];				// buffer for records
	struct DiskObject *MyDiskObject;	// The Icon
	// Disable all windows
	DisableWindows(DI_WaitSS);
	// if no file name supplied then request a name
	if (!filename) {
		if (GetAFile(MyGetMsg(MSG_DOTPREFS),MyGetMsg(MSG_SAVESET),FRF_DOSAVEMODE,NULL,MyGetMsg(MSG_SAVE))) {
			ifilename = TempFilename;
		}
	}
	else {
		ifilename = filename;
	}
	// if we now a file name
	if (ifilename) {
		// try and open the file
		if (fh = Open(ifilename,MODE_NEWFILE)) {
			// save all MPMorph settings
			FPuts(fh,MyGetMsg(MSG_PREFSHEAD));
			FPuts(fh,MyGetMsg(MSG_CICONSE));
			FPuts(fh,CreateIcons?MyGetMsg(MSG_YESN):MyGetMsg(MSG_NON));
			FPuts(fh,MyGetMsg(MSG_ZOOME));
			FPuts(fh,(!ZoomAllowed)?MyGetMsg(MSG_OFFN):(Zoom?MyGetMsg(MSG_YESN):MyGetMsg(MSG_NON)));
			FPuts(fh,MyGetMsg(MSG_PSCREENE));
			FPuts(fh,ScreenName);
			FPuts(fh,MyGetMsg(MSG_SN));
			FPuts(fh,MyGetMsg(MSG_CMODEE));
			FPuts(fh,CustomName);
			FPuts(fh,MyGetMsg(MSG_SN));
			sprintf(buffer,MyGetMsg(MSG_CDEPTHE),CustomDepth);
			FPuts(fh,buffer);
			FPuts(fh,MyGetMsg(MSG_PREVIEWE));
			FPuts(fh,PreviewScript);
			FPuts(fh,MyGetMsg(MSG_SN));
			FPuts(fh,MyGetMsg(MSG_HELPE));
			FPuts(fh,GHelp?MyGetMsg(MSG_YESN):MyGetMsg(MSG_NON));
			// Save all prefs settings
			FPuts(fh,MyGetMsg(MSG_CICONSPE));
			FPuts(fh,CreateIconsP?MyGetMsg(MSG_YESN):MyGetMsg(MSG_NON));
			FPuts(fh,MyGetMsg(MSG_KEEPSE));
			FPuts(fh,KeepSettings?MyGetMsg(MSG_YESN):MyGetMsg(MSG_NON));
			FPuts(fh,MyGetMsg(MSG_RECENT1E));
			FPuts(fh,Recent1);
			FPuts(fh,MyGetMsg(MSG_SN));
			FPuts(fh,MyGetMsg(MSG_RECENT2E));
			FPuts(fh,Recent2);
			FPuts(fh,MyGetMsg(MSG_SN));
			FPuts(fh,MyGetMsg(MSG_RECENT3E));
			FPuts(fh,Recent3);
			FPuts(fh,MyGetMsg(MSG_SN));
			FPuts(fh,MyGetMsg(MSG_RECENT4E));
			FPuts(fh,Recent4);
			FPuts(fh,MyGetMsg(MSG_SN));
			FPuts(fh,MyGetMsg(MSG_RECENT5E));
			FPuts(fh,Recent5);
			FPuts(fh,MyGetMsg(MSG_SN));
			FPuts(fh,MyGetMsg(MSG_USER2E));
			FPuts(fh,User2);
			FPuts(fh,MyGetMsg(MSG_SN));
			FPuts(fh,MyGetMsg(MSG_USER3E));
			FPuts(fh,User3);
			FPuts(fh,MyGetMsg(MSG_SN));
			FPuts(fh,MyGetMsg(MSG_USER4E));
			FPuts(fh,User4);
			FPuts(fh,MyGetMsg(MSG_SN));
			FPuts(fh,MyGetMsg(MSG_USER5E));
			FPuts(fh,User5);
			FPuts(fh,MyGetMsg(MSG_SN));
			FPuts(fh,MyGetMsg(MSG_USER6E));
			FPuts(fh,User6);
			FPuts(fh,MyGetMsg(MSG_SN));
			FPuts(fh,MyGetMsg(MSG_USER7E));
			FPuts(fh,User7);
			FPuts(fh,MyGetMsg(MSG_SN));
			FPuts(fh,MyGetMsg(MSG_USER8E));
			FPuts(fh,User8);
			FPuts(fh,MyGetMsg(MSG_SN));
			FPuts(fh,MyGetMsg(MSG_USER9E));
			FPuts(fh,User9);
			FPuts(fh,MyGetMsg(MSG_SN));
			// If want to save prefs Icon
			if (CreateIconsP) {
				if (MyDiskObject = GetDiskObject(ifilename)) {
					// Icon already present so do not overwrite
					FreeDiskObject(MyDiskObject);
				}
				else {
					// Try and get an icon and write it
					if ((MyDiskObject = GetDiskObject(MyGetMsg(MSG_MDEFPREFS))) ||
					    (MyDiskObject = GetDiskObject(MyGetMsg(MSG_SDEFPREFS))) ||
						 (MyDiskObject = GetDefDiskObject(WBPROJECT))) {
						PutDiskObject(ifilename,MyDiskObject);
						FreeDiskObject(MyDiskObject);
					}
				}
			}
			// try and close the file
			if (!Close(fh)) {
				ok = FALSE;
			}
		}
		else {
			ok = FALSE;
		}
	}
	// enable the windows and display error if required
	EnableWindows();
	if (!ok) {
		Error(MyGetMsg(MSG_ERR_SSET),MyGetMsg(MSG_OK),ifilename,HE_SaveSettings);
	}
}

/* Load the current settings
 * Parameters: filename - filename to load from
 *                        or NULL for requester
 */
void
LoadSettings(char *filename) {
	BPTR	fh;						// file handle
	int 	kount = 0;				// count of lines
	int 	kount1 = 0;				// index
	BOOL	ok = TRUE;				// Still ok?
	char	string[65];				// record buffer
	char	*ifilename = NULL;	// actual filename
	// if no file name then show requester
	if (!filename) {
		if (GetAFile(MyGetMsg(MSG_DOTPREFS),MyGetMsg(MSG_LOADS),0,NULL,MyGetMsg(MSG_LOAD))) {
			ifilename = TempFilename;
		}
	}
	else {
		ifilename = filename;
	}
	// if we now have a file name...
	// see Args.c MyArgArrayInit() for more info
	if (ifilename) {
		if (ArgArraySettings) {
			FreeVec(ArgArraySettings);
			ArgArraySettings = NULL;
		}
		if (fh = Open(ifilename,MODE_OLDFILE)) {
			// If we have found a file the relevant lines
			while (FGets(fh,string,64)) {
				if ((string[0] != '\n') &&
					 (string[0] != ';')) {
					++kount;
				}
			}
			Close(fh);
			if (fh = Open(ifilename,MODE_OLDFILE)) {
				// Allocate memory for settings from file pointers
				if (ArgArraySettings = AllocVec((kount+1) * sizeof(UBYTE *),MEMF_CLEAR)) {
					// Read in all settings
					while (FGets(fh,string,64) &&
							 (kount1 < kount)) {
						// remove newline
						string[strlen(string)-1] = '\0';
						// ignore blank and comment lines
						if (string[0] && (string[0]!=';')) {
							// Clone settings in memory
							ArgArraySettings[kount1] = strdup(string);
							++kount1;
						}
					}
					// reset up the new settings
					InitParams(TRUE);
				}
				else {
					ok = FALSE;
				}
				Close(fh);
			}
			else {
				ok=FALSE;
			}
		}
		else {
			ok=FALSE;
		}
	}
	if (!ok) {
		Error(MyGetMsg(MSG_ERR_LSET),MyGetMsg(MSG_OK),ifilename,HE_LoadSettings);
	}
}
