unit uCustomCommandLine;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, Buttons, GR32_Image, IniFiles;

type
  TFormCustomCommandLine = class(TForm)
    ButtonOk: TButton;
    ButtonCancel: TButton;
    LabelSystemDescription: TLabel;
    LabelCustomEmulatorFile: TLabel;
    EmulatorBatchFile: TEdit;
    ButtonEmulatorBatchFileBrowse: TButton;
    LabelAdditionalParameters: TLabel;
    AdditionalParameters: TEdit;
    ButtonClearCustomCommandLine: TButton;
    LabelGameDescription: TLabel;
    ImageGameIcon: TImage;
    TopImage: TImage32;
    ParametersBox: TGroupBox;
    ButtonMoveParameterDown: TButton;
    ButtonParameterDelete: TButton;
    ButtonMoveParameterUp: TButton;
    Parameters: TListView;
    ButtonReload: TButton;
    ButtonUpdate: TButton;
    LabelPrefixToAdd: TLabel;
    PrefixToAdd: TEdit;
    LabelFieldToAdd: TLabel;
    FieldToAdd: TComboBoxEx;
    LabelSuffixToAdd: TLabel;
    SuffixToAdd: TEdit;
    ButtonAddParameter: TButton;
    ParameterSurroundWithQuotes: TCheckBox;
    ButtonPreview: TButton;
    PrefixSendLeadingSpace: TCheckBox;
    procedure ButtonEmulatorBatchFileBrowseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ButtonClearCustomCommandLineClick(Sender: TObject);
    procedure ButtonOkClick(Sender: TObject);
    procedure ButtonCancelClick(Sender: TObject);
    procedure ButtonAddParameterClick(Sender: TObject);
    procedure ButtonMoveParameterUpClick(Sender: TObject);
    procedure ButtonParameterDeleteClick(Sender: TObject);
    procedure ParametersKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure ButtonReloadClick(Sender: TObject);
    procedure ButtonUpdateClick(Sender: TObject);
    procedure ParametersSelectItem(Sender: TObject; Item: TListItem;
      Selected: Boolean);
    procedure ButtonPreviewClick(Sender: TObject);
  private
    { Private declarations }
    sFileName: String;
    procedure MoveItem(ListHolder: TListView; Tag: ShortInt);
    procedure DeleteItem(ListHolder: TListView);
  public
    { Public declarations }
  end;

var
  FormCustomCommandLine: TFormCustomCommandLine;

implementation

uses uMain, uCommon;

{$R *.dfm}

procedure TFormCustomCommandLine.MoveItem(ListHolder: TListView; Tag: ShortInt);
var
  tmpItem, destItem: TListItem;
begin
  if ListHolder.Selected <> nil then
     begin
       tmpItem:= ListHolder.Selected;
       if ((tmpItem.Index > 0) and (Tag = -1)) or
          ((tmpItem.Index < ListHolder.Items.Count-1) and (Tag = 1)) then
          begin
            ListHolder.Items.BeginUpdate;
            case Tag of
               1: destItem:= ListHolder.Items.Insert(tmpItem.Index+(Tag*2));
              -1: destItem:= ListHolder.Items.Insert(tmpItem.Index+Tag);
            end;
            with destItem do
            begin
              ImageIndex:= tmpItem.ImageIndex; // real index of column
              StateIndex:= tmpItem.StateIndex; // index of item in the FieldToAdd
              Caption:= tmpItem.Caption;
              SubItems.Add(tmpItem.SubItems[0]);
              SubItems.Add(tmpItem.SubItems[1]);
              SubItems.Add(tmpItem.SubItems[2]);
            end;
            ListHolder.DeleteSelected;
            ListHolder.ItemFocused:= ListHolder.Items[destItem.Index+Tag];
            ListHolder.ItemIndex:= destItem.Index;
            ListHolder.Selected.MakeVisible(False);
            ListHolder.Items.EndUpdate;
          end;
       ListHolder.SetFocus;
     end;
end;

procedure TFormCustomCommandLine.DeleteItem(ListHolder: TListView);
begin
  if ListHolder.Selected <> nil then
     ListHolder.DeleteSelected;
end;

procedure TFormCustomCommandLine.ButtonEmulatorBatchFileBrowseClick(
  Sender: TObject);
begin
  FormMain.DialogOpenFile(2, 'Select a file', EmulatorBatchFile, False, False);
  SetCurrentDir(FormMain.FrontendPath);
end;

procedure TFormCustomCommandLine.FormShow(Sender: TObject);
var
  TopBarFileName: String;
begin
  FormMain.GetSystemID(FormMain.GameInfoData.eROMIdentification);
  FormMain.GetGameIcon(FormMain.IL_StandardIconsLarge, FormMain.IL_StandardIconsStandard, FormMain.IL_StandardIconsSmall, FormMain.GameInfoData.eImageIndex, ImageGameIcon.Picture.Icon);
  case ImageGameIcon.Tag of
    0, 1: LabelGameDescription.Caption:= FormMain.GameInfoData.eDescription;
       2: LabelGameDescription.Caption:= FormMain.GetEmuFullDescription(FormMain.SystemID_Number);
  end;
  case ImageGameIcon.Tag of
    0:
      begin
        sFileName:= FormMain.GameInfoData.eName;
        Caption:= Caption+' [Game]';
      end;
    1:
      begin
        sFileName:= ChangeFileExt(FormMain.GameInfoData.eDriverName, '');
        Caption:= Caption+' [Driver]';
      end;
    2:
      begin
        sFileName:= FormMain.GetSystemFolderName(FormMain.GameInfoData.eROMIdentification);
        Caption:= Caption+' [System]';
      end;
  end;
  LabelGameDescription.Caption:= LabelGameDescription.Caption+#13#10+
                                 Format('> File: ini_files\cmdline\%s\%s.ini <',
                                 [FormMain.GetSystemFolderName(FormMain.GameInfoData.eROMIdentification),
                                  sFileName]);
  LabelSystemDescription.Caption:= 'System: '+FormMain.GetEmuFullDescription(FormMain.SystemID_Number);
  TopBarFileName:= 'emu_'+FormMain.GetSystemFolderName(FormMain.GameInfoData.eROMIdentification)+'.png';
  FormMain.LoadTopBar(TopImage, TopBarFileName);
  ButtonReload.Click;
end;

procedure TFormCustomCommandLine.ButtonClearCustomCommandLineClick(Sender: TObject);
begin
  EmulatorBatchFile.Clear;
  PrefixToAdd.Clear;
  PrefixSendLeadingSpace.Checked:= True;
  Parameters.Clear;
  SuffixToAdd.Clear;
  ParameterSurroundWithQuotes.Checked:= False;
  AdditionalParameters.Clear;
end;

procedure TFormCustomCommandLine.ButtonOkClick(Sender: TObject);
begin
  if EmulatorBatchFile.Text = '' then
     GenerateMessage('Error', 'Filename is empty! Please select a file first.', 2)
  else
     begin
       FormMain.WriteCustomCommandLine(sFileName, EmulatorBatchFile.Text, Parameters, AdditionalParameters.Text, FormMain.GameInfoData.eROMIdentification, Ord(PrefixSendLeadingSpace.Checked));
       Close;
     end;
end;

procedure TFormCustomCommandLine.ButtonCancelClick(Sender: TObject);
begin
  Close;
end;

procedure TFormCustomCommandLine.ButtonAddParameterClick(Sender: TObject);

  procedure AddParameter(ListHolder: TListView);
  begin
    ListHolder.Items.BeginUpdate;
    with ListHolder.Items.Add do
    begin
      Caption:= PrefixToAdd.Text;
      ImageIndex:= FieldToAdd.ItemsEx[FieldToAdd.ItemIndex].ImageIndex; // real index of column
      StateIndex:= FieldToAdd.ItemIndex; // index of item in the FieldToAdd
      SubItems.Add(FieldToAdd.ItemsEx[FieldToAdd.ItemIndex].Caption);
      SubItems.Add(SuffixToAdd.Text);
      case ParameterSurroundWithQuotes.Checked of
        True : SubItems.Add('Yes');
        False: SubItems.Add('No');
      end;
    end;
    ListHolder.Items.EndUpdate;
  end;

begin
  if (FieldToAdd.ItemIndex <> -1) then
     AddParameter(Parameters);
end;

procedure TFormCustomCommandLine.ButtonMoveParameterUpClick(
  Sender: TObject);
begin
  MoveItem(Parameters, TButton(Sender).Tag);
end;

procedure TFormCustomCommandLine.ButtonParameterDeleteClick(
  Sender: TObject);
begin
  DeleteItem(Parameters);
end;

procedure TFormCustomCommandLine.ParametersKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  if Key = VK_DELETE then
     ButtonParameterDelete.Click;
end;

procedure TFormCustomCommandLine.ButtonReloadClick(Sender: TObject);
begin
  if FormMain.ReadCustomCommandLine(sFileName, FormMain.GameInfoData.eROMIdentification) = '' then
     begin
       // if arcades, add a name field
       // if consoles, add a game filename field
       SetComboBoxEx(FieldToAdd, 4, True); // Name field
       ButtonAddParameter.Click;
     end;
end;

procedure TFormCustomCommandLine.ButtonUpdateClick(Sender: TObject);
begin
  if Parameters.Selected <> nil then
     begin
       Parameters.Selected.ImageIndex:= FieldToAdd.ItemsEx[FieldToAdd.ItemIndex].ImageIndex;
       Parameters.Selected.StateIndex:= FieldToAdd.ItemIndex;
       Parameters.Selected.Caption:= PrefixToAdd.Text;
       Parameters.Selected.SubItems[0]:= FieldToAdd.ItemsEx[FieldToAdd.ItemIndex].Caption;
       Parameters.Selected.SubItems[1]:= SuffixToAdd.Text;
       case ParameterSurroundWithQuotes.Checked of
         True : Parameters.Selected.SubItems[2]:= 'Yes';
         False: Parameters.Selected.SubItems[2]:= 'No';
       end;
     end
  else
     GenerateMessage('Information', 'Please select a parameter to edit first.', 2);
end;

procedure TFormCustomCommandLine.ParametersSelectItem(Sender: TObject;
  Item: TListItem; Selected: Boolean);
var
  Index: Integer;
begin
  if Selected then
     begin
       PrefixToAdd.Text:= Item.Caption;
       Index:= FieldToAdd.Items.IndexOf(Item.SubItems[0]);
       if Index = -1 then
          Index:= 0;
       SetComboBoxEx(FieldToAdd, Index, True);
       SuffixToAdd.Text:= Item.SubItems[1];
       ParameterSurroundWithQuotes.Checked:= Item.SubItems[2] = 'Yes';
     end;
end;

procedure TFormCustomCommandLine.ButtonPreviewClick(Sender: TObject);
var
  Loop: Integer;
  cmdLine, sPrefix, sParameters, sSuffix, sQuotes: String;
begin
  if EmulatorBatchFile.Text = '' then
     Exit;
  cmdLine:= '';
  if Parameters.Items.Count > 0 then
     begin
       for Loop:= 0 to Parameters.Items.Count-1 do
       begin
         sPrefix:= Parameters.Items[Loop].Caption;
         if (cmdLine <> '') and PrefixSendLeadingSpace.Checked then
            sPrefix:= ' '+sPrefix;

         sParameters:= FormMain.GetBatchFileFieldIndex(IntToStr(Parameters.Items[Loop].StateIndex));
         sSuffix:= Parameters.Items[Loop].SubItems[1];
         if Parameters.Items[Loop].SubItems[2] = 'No' then
            sQuotes:= ''
         else
            sQuotes:= '"';
         case StrToInt(sParameters) of
           00: cmdLine:= cmdLine+sPrefix+sQuotes+FormMain.GameInfoData.eDescription+sQuotes+sSuffix;
           06: cmdLine:= cmdLine+sPrefix+sQuotes+aScreenType[FormMain.GameInfoData.eScreenType1]+sQuotes+sSuffix;
           07: cmdLine:= cmdLine+sPrefix+sQuotes+FormMain.MountOrientation(FormMain.GameInfoData.eScreenRotation1)+sQuotes+sSuffix;
           08: cmdLine:= cmdLine+sPrefix+sQuotes+FormMain.MountResolution(FormMain.GameInfoData.eResolutionWidth1, FormMain.GameInfoData.eResolutionHeight1, FormMain.GameInfoData.eScreenRotation1)+sQuotes+sSuffix;
           31: cmdLine:= cmdLine+sPrefix+sQuotes+FormMain.GameInfoData.eName+sQuotes+sSuffix;
           32: cmdLine:= cmdLine+sPrefix+sQuotes+FormMain.GameInfoData.eClone+sQuotes+sSuffix;
           33: cmdLine:= cmdLine+sPrefix+sQuotes+FormMain.GameInfoData.eDriverName+sQuotes+sSuffix;
           39: cmdLine:= cmdLine+sPrefix+sQuotes+aControlType[FormMain.GameInfoData.eControlType]+sQuotes+sSuffix;
         end;
       end;
     end;
  if cmdLine <> '' then
     cmdLine:= FormMain.WinQuotesStr+EmulatorBatchFile.Text+FormMain.WinQuotesStr+' '+cmdLine
  else
    cmdLine:= FormMain.WinQuotesStr+EmulatorBatchFile.Text+FormMain.WinQuotesStr;
  if AdditionalParameters.Text <> '' then
     cmdLine:= cmdLine+' '+AdditionalParameters.Text;
  if cmdLine <> '' then
     GenerateMessage('Custom Command Line Preview', cmdLine, 2);
end;

end.
