þ a‹R þ w <þ mP9     þ hý	 oP     þ nSystem-wide$NOLIST

MODULE Forms;

$COMPACT (-CONST IN CODE-)

$INCLUDE (``Incs`Common.Inc~Text~)
$INCLUDE (``Incs`ConPas.Inc~Text~)
$INCLUDE (``Incs`Keys.Inc~Text~)
$INCLUDE (``Incs`Math.Inc~Text~)

$INCLUDE (``Incs`StringTypes.Inc~Text~)
$INCLUDE (``Incs`StringProcs.Inc~Text~)

$INCLUDE (``Incs`WindowTypes.Inc~Text~)
$INCLUDE (``Incs`WindowProcs.Inc~Text~)

$INCLUDE (``Incs`OsPasTypes.Inc~Text~)
$INCLUDE (``Incs`FieldTypes.Inc~Text~)
$INCLUDE (``Incs`TableEditTypes.Inc~Text~)
$INCLUDE (``Incs`MessageTypes.Inc~Text~)

$INCLUDE (``Incs`MenuFormTypes.Inc~Text~)
$INCLUDE (``Incs`MenuFormProcs.Inc~Text~)

$INCLUDE (``Incs`DataForms.Inc~Text~)

$INCLUDE (``Incs`VolumeName.Inc~Text~)

$INCLUDE (Interface.Inc~Text~)

PUBLIC FormsInit;
  VAR theVolumeNameForm:    DataFormType;
      theBigDeviceMenu:     DataMenuType;
      theEraseAllFilesForm: DataFormType;

PRIVATE Forms;

$EJ

FUNCTION NilScrollKeyShell (ch: Char; col, row: Integer): UpdateKind;
  BEGIN
  NilScrollKeyShell := NilScrollKey (ch, col, row);
  END; { NilScrollKeyShell }


FUNCTION NilItemStrShell (col, row: Integer; field: FieldPtr): StringPtr;
  BEGIN
  NilItemStrShell := NilItemStr (col, row, field);
  END; { NilItemStrShell }


FUNCTION NilChoiceInfoShell (col, row, choice: Integer;
  request: ChoiceRequest): Integer;

  BEGIN
  NilChoiceInfoShell := NilChoiceInfo (col, row, choice, request);
  END; { NilChoiceInfoShell }


FUNCTION NilChoiceProcShell (col, row, choice: Integer): StringPtr;

  BEGIN
  NilChoiceProcShell := NilChoiceProc (col, row, choice);
  END; { NilChoiceProcShell }


FUNCTION GetMediaChoiceName (choice: Integer): StringPtr;
VAR curMedia:  MediaPtr;
    curChoice: INTEGER;
BEGIN
  curChoice := 1;
  curMedia := media;
  WHILE curChoice < choice DO
    BEGIN
    curChoice := curChoice + 1;
    curMedia := curMedia^.next;
    END;
  GetMediaChoiceName := CopyOfString (curMedia^.devName);
END;

PROCEDURE SetMenuFormRect (VAR r: Rectangle; msgCount: Integer);
BEGIN
  WITH r DO
    BEGIN
    topLeft.x := 0;
    topLeft.y := 0;
    WinGetWindowExtent (extent);
    extent.y  := extent.y - (MsgHeight * msgCount);
    IF extent.y < 0 THEN
      extent.y := 0;
    END;
END;

$EJ

FUNCTION DeviceSelected (VAR ch: Char): Boolean;
CONST itemCount    = 1;
      maxChPerLine = 21;
VAR confirmed:         Boolean;
    choiceLines:       Integer;
    selection:         Integer;
    usableRect:        Rectangle;
    form:              MenuFormPtr;
    newMediumToFormat: Integer;

  FUNCTION ItemStr (col,row: Integer; field: FieldPtr): StringPtr;
  CONST labelLits = 'Medium to initialize';
  BEGIN
    ItemStr := NIL;
    CASE col OF
      1: IF row IN [1..itemCount] THEN
           ItemStr := SubStringLit (labelLits, '', row);
      2: BEGIN
         field^.kind.choice    := TRUE;
         field^.kind.editable  := FALSE;
         ItemStr := ChoiceStr (col, row, newMediumToFormat);
         END;
      OTHERWISE;
    END;
  END;


  FUNCTION ChoiceStr (col,row,choice: Integer): StringPtr;
  BEGIN
    ChoiceStr := NIL;
    IF choice IN [1..numMedia] THEN
      ChoiceStr := GetMediaChoiceName (choice);
  END;

  FUNCTION ChoiceInfo (col,row,choice: Integer;request: ChoiceRequest): Integer;
  BEGIN
    ChoiceInfo := choice;
    IF col = 2 THEN 
      BEGIN
      CASE request OF
        choiceSetCurrent,
        choiceLeavingItem:    newMediumToFormat := choice;
        choiceCurrentRequest: ChoiceInfo := newMediumToFormat;
        choiceCountRequest:   ChoiceInfo := numMedia;
        OTHERWISE;
      END;
      END;
  END;

$EJ

BEGIN
  RemoveMessages;
  Message (mWarning);
  Message (mfillIn);
  newMediumToFormat := mediumToFormat;
  SetMenuFormRect (usableRect, pMsgStatus^.stackSize);
  choiceLines := numMedia;
  form := FormInit (usableRect, itemCount, maxChPerLine, choiceLines, ItemStr);

  REPEAT confirmed := MenuFormConfirmed
    (form, cursor.keyProcess, ItemStr, ChoiceStr,
     ChoiceInfo, NilScrollKeyShell, selection, ch);
  UNTIL confirmed OR IsEsc (ch);

  IF confirmed THEN
    BEGIN
    mediumToFormat := newMediumToFormat;
    deviceName     := ConcatStrings (NewStringLit ('`'),
                                     GetMediaChoiceName (mediumToFormat));
    END;
  form := MenuFormDispose (form);
  RemoveMessages;
  DeviceSelected := confirmed;
END;

$EJ

{                MenuChoiceConfirmed            }

FUNCTION MenuChoiceConfirmed (VAR menuChoice: menuChoiceType;
                        VAR ch: Char): Boolean;
CONST
  numHelpItems   = 3;
  helpMenuString =
    '          Initialize  Prepare medium for use'
    'CODE-Q    Quit        Exit'
    'CODE-ESC  Cancel      Exit';
VAR
  confirmed:  Boolean;
  selection:  Integer;
  usableRect: Rectangle;
  menu:       MenuFormPtr;


FUNCTION HelpStr (index: Integer): StringPtr;
  BEGIN
  HelpStr := NIL;
  IF index IN [1..numHelpItems] THEN
    HelpStr := SubStringLit (helpMenuString, CHR (127), index);
  END; { HelpStr }


BEGIN { MenuChoiceConfirmed }
  MenuChoiceConfirmed := FALSE;
  RemoveMessages;
  Message (mGRiD);
  Message (mCopyright);
  Message (mVersion);
  Message (mCommands);
  SetMenuFormRect (usableRect, pMsgStatus^.stackSize);  
  menu := MenuInit (usableRect, numHelpItems, HelpStr);

  REPEAT confirmed := MenuFormConfirmed
    (menu,cursor.keyProcess,NilItemStrShell,
     NilChoiceProcShell, NilChoiceInfoShell,
     NilScrollKeyShell, selection, ch);
  UNTIL confirmed OR IsEsc (ch);

  menu := MenuFormDispose (menu);
  RemoveMessages;
  IF confirmed THEN
    CASE selection OF
      1: BEGIN
         menuChoice := doFormat;
         MenuChoiceConfirmed := TRUE;
         END;
      2: ch := CHR (quitKey);
      3: ch := CHR (cancelKey);
      OTHERWISE;
    END; { CASE }
END;

$EJ

FUNCTION EraseAllFiles (VAR ch: CHAR) : BOOLEAN;
CONST
  itemCount    = 1;
  maxChPerLine = 43;
  choiceLines  = 1;
VAR
  proceedToFormat: Integer;
  confirmed:       Boolean;
  selection:       Integer;
  usableRect:      Rectangle;
  form:            MenuFormPtr;
    

FUNCTION ItemStr (col,row: Integer; field: FieldPtr): StringPtr;
  CONST
    startOfQuestionLit = 'Erase all files on ';
  VAR
    s: StringPtr;
  BEGIN
  ItemStr := NIL;
  CASE col OF
    1: CASE row OF
        1: BEGIN
            s := ConcatStrings (NewStringLit (startOfQuestionLit),
                   ChoiceStr (row, col, mediumToFormat));
            AppendAnyChar (s, '?');
            ItemStr := s;
            END;
        OTHERWISE;
        END; { CASE }
    2: BEGIN
       field^.kind.choice    := TRUE;
       field^.kind.editable  := FALSE;
       ItemStr := ChoiceStr (col,row, proceedToFormat);
       END;
    OTHERWISE;
  END; { CASE }
  END; { ItemStr }


FUNCTION ChoiceStr (col,row,choice: Integer): StringPtr;
  BEGIN
  ChoiceStr := NIL;
  CASE col OF
    1: IF choice IN [1..numMedia] THEN
         ChoiceStr := GetMediaChoiceName (choice);
    2: IF choice IN [yes, no] THEN
         ChoiceStr := SubStringLit (yesOrNoLit, CHR (127), choice);
    OTHERWISE;
  END;
  END; { ChoiceStr }
$EJ
FUNCTION ChoiceInfo (col,row,choice: Integer;
                     request: ChoiceRequest): Integer;
  BEGIN
  ChoiceInfo := choice;
  IF col = 2 THEN 
    CASE request OF
      choiceSetCurrent,
      choiceLeavingItem:    proceedToFormat := choice;
      choiceCurrentRequest: ChoiceInfo := proceedToFormat;
      choiceCountRequest:   ChoiceInfo := 2;
    OTHERWISE;
    END;
  END; { ChoiceInfo }


BEGIN
  EraseAllFiles := FALSE;
  RemoveMessages;
  Message (mFillIn);
  proceedToFormat := no;
  SetMenuFormRect (usableRect, pMsgStatus^.stackSize);
  form := FormInit (usableRect, itemCount,
    maxChPerLine, choiceLines, ItemStr);

  REPEAT confirmed := MenuFormConfirmed
    (form, cursor.keyProcess, ItemStr, ChoiceStr,
     ChoiceInfo, NilScrollKeyShell, selection,ch);
  UNTIL confirmed OR IsEsc (ch);

  form := MenuFormDispose (form);
  IF confirmed AND (proceedToFormat = yes) THEN
    IF ThisDeviceIsRemovable (pMediaToFormat^.devName) THEN
      BEGIN
      RemoveMessages;
      Message (mPutInDisk);
      ch := ConCharIn;
      IF ch = CHR(confirmKey) THEN EraseAllFiles := TRUE;
      END
    ELSE
      EraseAllFiles := TRUE;

  RemoveMessages;
END;

$EJ

{ Must dynamically add device name to form label
FUNCTION EraseAllFiles (VAR ch: CHAR) : BOOLEAN;
VAR confirmed:  BOOLEAN;
    usableRect: Rectangle;
BEGIN
  RemoveMessages;
  Message (mFillIn)
  SetMenuFormRect (usableRect, 0);
  WITH theEraseAllFilesForm DO
    BEGIN
    rows[1].currentChoice := 2;
    confirmed := DataFormConfirmed
                   (theEraseAllFilesForm,
                    normalDataForm,
                    pMsgStatus,
                    NIL,
                    usableRect,
                    cursor.keyProcess,
                    ch);
    IF confirmed THEN
      confirmed := rows[1].currentChoice = 1;
    FreeStringsInDataForm (theEraseAllFilesForm);
    UndoDataForm (theEraseAllFilesForm, TRUE);
    END;
  EraseAllFiles := confirmed;
END;
}

FUNCTION EraseBigDevice (VAR ch: CHAR) : BOOLEAN;
VAR usableRect: Rectangle;
    itemSelected: INTEGER;
BEGIN
  IF NOT ThisDeviceIsRemovable (pMediaToFormat^.devName) THEN
    BEGIN
    RemoveMessages;
    Message (mBigDevice);
    SetMenuFormRect (usableRect, 0); { Include Msg line in rect }
    EraseBigDevice := DataMenuConfirmed
                       (theBigDeviceMenu,
                        pMsgStatus,
                        NIL,
                        usableRect,
                        cursor.keyProcess,
                        itemSelected,
                        ch);
    END
  ELSE
    EraseBigDevice := TRUE; { Not a big device }
END;

$EJ

FUNCTION VolumeNameFilledIn (VAR ch: CHAR) : BOOLEAN;
VAR confirmed:  BOOLEAN;
    usableRect: Rectangle;
    error:      WORD;
    legalName:  BOOLEAN;
    nameOnDisk: BOOLEAN;
    createDate: TimeType;
BEGIN
  volumeName := NIL;
  Message (mVolumeName);
  REPEAT
    SetMenuFormRect (usableRect, 0); { Include Msg line in rect }
    WITH theVolumeNameForm DO
      BEGIN
      rows[1].theData.string := NewString (maxRemVolumeNameLen);
      CopyString (volumeName, rows[1].theData.string);
      rows[1].currentChoice  := 1;
      confirmed := DataFormConfirmed
                     (theVolumeNameForm,
                      normalDataForm,
                      pMsgStatus,
                      NIL,
                      usableRect,
                      cursor.keyProcess,
                      ch);
      IF confirmed THEN
        BEGIN
        volumeName := CopyOfString (rows[1].theData.string);
        legalName  := CheckVolumeName (volumeName);
        END;
      FreeStringsInDataForm (theVolumeNameForm);
      UndoDataForm (theVolumeNameForm, TRUE);
      END;
  UNTIL NOT confirmed OR legalName;
  VolumeNameFilledIn := confirmed;
END;

FUNCTION CheckVolumeName (name: StringPtr) : BOOLEAN;
BEGIN
  RemoveMessages;
  CheckVolumeName := FALSE;
  IF (volumeName^.len = 0) AND
     ThisDeviceIsRemovable (pMediaToFormat^.devName) THEN
    Message (mNoVolumeName)
  ELSE
  IF NOT VolumeNameOk (volumeName) THEN
    Message (mBadVolumeName)
  ELSE
  IF InListOfBadNames (volumeName) THEN
    Message (mNoGenericName)
  ELSE
    CheckVolumeName := TRUE;
END;
.
