{
  Author: IDimager

  This script will copy a macro command's output to the clipboard

  Version: 1.1
    Author: IDimager
    Date: 03 November 2021
    Changes: made the editor a memo

  Version: 1.0
    Author: IDimager
    Date: 06 March 2021
    Changes: n.a.

}

var
  FMacro: WideString;
  FSl: TTntStringList;

var
  AMacroEdit: TvxMemo;

procedure _MacroButtonClick (Sender: TObject);
begin
  MacroValuesPopup.Component := AMacroEdit;
  MacroValuesPopup.Popup(Mouse.CursorPos.X, Mouse.CursorPos.Y);
end;

function BuildForm: TvxSceneForm;
var
  ALabel: TvxLabel;
  AEdit: TvxMemo;
  AOk: TvxButton;
  AMacroButton: TvxButton;
begin
  result := TvxSceneForm.Create(nil);
  result.Caption  := 'Copy Macro Command to Clipboard';
  result.Position := poScreenCenter;
  result.Width := 400 * result.DPIFactor;
  result.Height := 200 * result.DPIFactor;

  ALabel := TvxLabel.Create(result.Root);
  result.Root.AddObject(ALabel);
  ALabel.Text := 'Macro Command';
  ALabel.TextAlign := vgTextAlignNear;
  ALabel.Position.Point := vgPoint(20, 20);

  AEdit := TvxMemo.Create(result.Root);
  result.Root.AddObject(AEdit);
  AEdit.Text := FMacro;
  AEdit.Width := 300;
  AEdit.Height := 100;
  AEdit.Position.Point := vgPoint(20, 40);
  AMacroEdit := AEdit;

  AMacroButton := TvxButton.Create(result.Root);
  result.Root.AddObject(AMacroButton);
  AMacroButton.Position.Point := vgPoint(AEdit.Position.X + AEdit.Width, AEdit.Position.Y);
  AMacroButton.Width  := 20;
  AMacroButton.Text   := '...';
  AMacroButton.OnClick:= '_MacroButtonClick';

  AOk := TvxButton.Create(result.Root);
  result.Root.AddObject(AOk);
  AOk.ModalResult := mrOk;
  AOk.Position.Point := vgPoint(20, 200 - 30 - AOk.Height);
  AOk.Text := 'OK';
end;

procedure AddResultForItem(AResult: TTntStringList; AImage: TImageItem; AMacroCommand: WideString);
begin
  AResult.Add(AImage.ParseMacros(AMacroCommand, nil));
end;

procedure HandleImage (AImage: TImageItem);
begin
  AddResultForItem(FSl, AImage, FMacro);
end;

var
  i: Integer;
  AForm: TvxSceneForm;
begin
  if Selected.Count = 0 then
  begin
    Say('No image selected');
    exit;
  end;

  FMacro    := '%CatalogLabelList';
  FMacro    := ReadFromRegistry ('Scripts', 'CopyMacroToClipboard', FMacro);

  AForm := BuildForm;
  try
    AForm.ShowModal;
    if (AForm.ModalResult = mrOk) then
    begin
      FMacro          := Trim(AMacroEdit.Text);

      if FMacro = '' then
      begin
        Say('No macro selected');
        exit;
      end;

      WriteToRegistry ('Scripts', 'CopyMacroToClipboard', FMacro);

      FSl := TTntStringList.Create;
      try
        for i := 0 to Selected.Count - 1 do
          HandleImage(Selected.Items[i]);

        if FSl.Count > 0 then
        begin
          //Say(FSl.Text);
          CopyTextToClipboard(FSl.Text);
          SendVisualFeedback('The macro command for ' + IntToStr(FSl.Count) + ' ' + iif(FSl.Count = 1, 'image', 'images') + ' was copied to the clipboard', 5000);
        end;
      finally
        FSl.Free;
      end;
    end;
  finally
    AForm.Free;
  end;
end;
