MODULE InfoDialog;

	(*
	Dialog for creating link list, collection files and correcting links
	Helmut Zinn - 

	 "StdCmds.OpenToolDialog('Info/Rsrc/CreateLinks', 'Create Links')"
	 "StdCmds.OpenToolDialog('Info/Rsrc/CollectFiles', 'Collect Files')"
	*)

	IMPORT
		Dialog, Files, HostFiles, PacDirChoose, Strings, TextCmds, TextControllers, TextMappers;

	CONST
		pathLen = HostFiles.pathLen;

	VAR
		param*: RECORD
			source*: ARRAY pathLen OF CHAR;
			filter*: ARRAY 16 OF CHAR;
			useRelativePath*: BOOLEAN;
			include*, expand*: BOOLEAN;
			sortHit*, listDate*: BOOLEAN;
		END;
		relative: BOOLEAN;
		workingDir: ARRAY pathLen OF CHAR;

		(* used by UpdateGuard *)
		cn: TextControllers.Controller;
		sc: TextMappers.Scanner;

	PROCEDURE GetWorkingPath (VAR workingDir: ARRAY OF CHAR);
		VAR i: INTEGER; loc: Files.Locator;
	BEGIN
		loc := Files.dir.This("");
		workingDir := loc(HostFiles.Locator).path$ + '/';
		FOR i := 0 TO LEN(workingDir$) DO
			IF workingDir[i] = '\' THEN workingDir[i] := '/' END;
		END;
	END GetWorkingPath;

	PROCEDURE Initialise;
	BEGIN
		param.source := "Archiv/Help/Uhl/";
		param.filter := 'odc';
		param.useRelativePath := TRUE;
		param.include := TRUE;
		param.expand := FALSE;
		param.sortHit := TRUE;
		param.listDate := FALSE;
		TextCmds.find.ignoreCase := TRUE;
		(* BlackBox Working Directory *)
		GetWorkingPath(workingDir);
		PacDirChoose.SetStartingPath(workingDir+param.source);
		(* Show the Directory path relative to Working Directory *)
		relative := param.useRelativePath;
	END Initialise;

	(* Browse directory *)

	PROCEDURE FixRoot* (IN path: Files.Name);
		VAR i, n: INTEGER;
	BEGIN
		IF param.useRelativePath THEN
			i := 0;
			n := LEN(workingDir$);
			WHILE (i < n) & (CAP(workingDir[i]) = CAP(Strings.Upper(path[i]))) DO INC(i) END;
			IF i < n THEN i := 0; relative := FALSE ELSE i := n; relative := TRUE END;
			Strings.Extract(path$, i, LEN(path$) - i, param.source);
		ELSE
			IF relative THEN param.source := workingDir$ + path$ ELSE param.source := path$ END;
			relative := FALSE;
		END;
		Dialog.Update(param);
	END FixRoot;

	PROCEDURE BrowseSource*;
	BEGIN
		PacDirChoose.Choose(FixRoot);
	END BrowseSource;

	(* Check parameters & show status *)

	PROCEDURE ProofParam*;
		VAR i, n: INTEGER;
	BEGIN
		FOR i := 0 TO LEN(param.source$) - 1 DO
			IF param.source[i] = '\' THEN param.source[i] := '/' END;
		END;
		n := LEN(param.source$);
		IF (n > 0) & (param.source[n - 1] # '/') THEN param.source := param.source + '/' END;
		IF param.source = '/' THEN param.source := '' END;
		Dialog.Update(param);
	END ProofParam;

	PROCEDURE ShowStatus* (count: INTEGER; IN msg: ARRAY OF CHAR);
		VAR
			s: ARRAY 10 OF CHAR;
			note: ARRAY 128 OF CHAR;
	BEGIN
		Strings.IntToString(count, s);
		Strings.Extract(s$ + ' ' + msg$, 0, 127, note);
		Dialog.ShowStatus(note$);
	END ShowStatus;

	(* Guards & Notifiers - TextCmds.FocusGuard used too *)

	PROCEDURE ReplaceGuard* (VAR par: Dialog.Par);
	BEGIN
		par.disabled := (TextCmds.find.find$ = "") OR (TextCmds.find.replace$ = "");
	END ReplaceGuard;

	PROCEDURE UpdateGuard* (VAR par: Dialog.Par);
	BEGIN
		par.disabled := TRUE;
		cn := TextControllers.Focus();
		IF cn # NIL THEN
			sc.ConnectTo(cn.text);
			sc.Scan;
			IF (sc.type = TextMappers.string) & (sc.string = "auto") THEN par.disabled := FALSE END;
		END;
	END UpdateGuard;

	PROCEDURE UseRelativePathNotifier* (op, from, to: INTEGER);
	BEGIN
		IF op = Dialog.changed THEN
			FixRoot(param.source$);
			Dialog.Update(param);
		END;
	END UseRelativePathNotifier;

BEGIN
	Initialise;
END InfoDialog.
