unit uFilterUI;
{$INCLUDE 'Filters.inc'}

interface

uses
  SysUtils, Classes, Generics.Collections,
  {$INCLUDE 'DelphiFarUnits.inc'} ,
  uFilter,
  uConsts, uMessages;

type
  TFilterDialog = class(TFarDialog)
  private
    fFilter: TFilter;
    fSaveCofigBeforeUse: boolean;
    fFrame: TFarDialog.TDoubleBox;
    fLabelDescription: TFarDialog.TLabel;
    fEditDescription: TFarDialog.TEdit;
    fLabelWildcards: TFarDialog.TLabel;
    fEditWildcards: TFarDialog.TEdit;
    fCheckEditBeforeUse: TFarDialog.TCheckbox;
    fCheckSaveConfigBeforeUse: TFarDialog.TCheckbox;
    fButtonOK: TFarDialog.TButton;
    fButtonCancel: TFarDialog.TButton;
  protected
    procedure Clear; override;
    function DialogProc(Msg, Param1, Param2: TIntPtr; var DlgProcResult: TIntPtr): boolean; override;
    procedure Resize(ScreenWidth, ScreenHeight: integer); override;
    procedure FilterToDialog; virtual;
    procedure DialogToFilter; virtual;
    function LastDefinedControl: TFarDialog.TControl; virtual;
    procedure CreateMoreControls; virtual;
    property Frame: TFarDialog.TDoubleBox read fFrame;
    property LabelDescription: TFarDialog.TLabel read fLabelDescription;
    property EditDescription: TFarDialog.TEdit read fEditDescription;
    property LabelWildcards: TFarDialog.TLabel read fLabelWildcards;
    property EditWildcards: TFarDialog.TEdit read fEditWildcards;
    property CheckEditBeforeUse: TFarDialog.TCheckbox read fCheckEditBeforeUse;
    property CheckSaveConfigBeforeUse: TFarDialog.TCheckbox read fCheckSaveConfigBeforeUse;
    property ButtonOK: TFarDialog.TButton read fButtonOK;
    property ButtonCancel: TFarDialog.TButton read fButtonCancel;
  public
    constructor Create(GUID: TGUID);
    destructor Destroy; override;
    function Execute: boolean;
    property Filter: TFilter read fFilter write fFilter;
    property SaveCofigBeforeUse: boolean read fSaveCofigBeforeUse write fSaveCofigBeforeUse;
  end;

implementation

{ TFilterDialog }

procedure TFilterDialog.Clear;
begin
  inherited;
  fFrame := nil;
  fLabelDescription := nil;
  fEditDescription := nil;
  fLabelWildcards := nil;
  fEditWildcards := nil;
  fCheckEditBeforeUse := nil;
  fCheckSaveConfigBeforeUse := nil;
  fButtonOK := nil;
  fButtonCancel := nil;
end;

constructor TFilterDialog.Create(GUID: TGUID);
begin
  inherited Create(GUID);
  fFrame := AddDoubleBox(0, 0, 0, 0, GetMsg(MTitle));
  fLabelDescription := AddLabel(0, 0, 0, GetMsg(MDescription));
  fEditDescription := AddEdit(0, 0, 0, '');
  fEditDescription.History := 'FilterDescription';
  fLabelWildcards := AddLabel(0, 0, 0, GetMsg(MWildcard));
  fEditWildcards := AddEdit(0, 0, 0, '');
  fEditWildcards.History := 'FilterWildcards';
  fCheckEditBeforeUse := AddCheckbox(0, 0, GetMsg(MEditBeforeUse), False);
  fCheckSaveConfigBeforeUse := AddCheckbox(0, 0, GetMsg(MSaveFilterContent), False);
  fCheckSaveConfigBeforeUse.Visible := False;
  CreateMoreControls;
  fButtonOK := AddButton(0, 0, GetMsg(MOk));
  fButtonOK.Default := True;
  fButtonCancel := AddButton(0, 0, GetMsg(MCancel));
end;

procedure TFilterDialog.CreateMoreControls;
begin
end;

destructor TFilterDialog.Destroy;
begin
  inherited;
end;

function TFilterDialog.DialogProc(Msg, Param1, Param2: TIntPtr; var DlgProcResult: TIntPtr): boolean;
begin
  case Msg of
    DN_EDITCHANGE:
      Result := HandleEDITCHANGE(Param1, Param2, DlgProcResult);
    else
      Result := inherited;
  end;
end;

procedure TFilterDialog.DialogToFilter;
begin
  Filter.Description := EditDescription.Text;
  Filter.FileMaskStr := EditWildcards.Text;
  Filter.EditBeforeUse := CheckEditBeforeUse.Checked;
  Self.SaveCofigBeforeUse := CheckSaveConfigBeforeUse.Checked;
end;

function TFilterDialog.Execute: boolean;
var
  Button: TFarDialog.TButton;
begin
  Result := False;
  Resize(Screen.Width, Screen.Height);
  FilterToDialog;
  if BuildDialog(Frame.Width + 6, Frame.Height + 2) then
  begin
    if inherited Execute(Button) then
    begin
      if Button = ButtonOK then
      begin
        DialogToFilter;
        Result := True;
      end;
    end;
  end;
end;

procedure TFilterDialog.FilterToDialog;
begin
  Frame.Text := Filter.Name;
  EditDescription.Text := Filter.Description;
  EditWildcards.Text := Filter.FileMaskStr;
  CheckEditBeforeUse.Checked := Filter.EditBeforeUse;
  CheckSaveConfigBeforeUse.Checked := Self.SaveCofigBeforeUse;
end;

function TFilterDialog.LastDefinedControl: TFarDialog.TControl;
begin
  if CheckEditBeforeUse.Visible then
    Result := CheckEditBeforeUse
  else if CheckSaveConfigBeforeUse.Visible then
    Result := CheckSaveConfigBeforeUse
  else
    Result := EditWildcards;
end;

procedure TFilterDialog.Resize(ScreenWidth, ScreenHeight: integer);
begin
  inherited;
  Frame.Left := 3;
  Frame.Width := ScreenWidth - 16;
  Frame.Top := 1;
  Frame.Height := 9;
  LabelDescription.AlignTo(Frame, 2, 1, -4, 0);
  EditDescription.AlignTo(LabelDescription, 0, 1, 0, 0);
  LabelWildcards.AlignTo(EditDescription, 0, 1, 0, 0);
  EditWildcards.AlignTo(LabelWildcards, 0, 1, 0, 0);
  CheckEditBeforeUse.AlignTo(EditWildcards, 0, 1, 0, 0);
  if CheckSaveConfigBeforeUse.Visible then
    CheckEditBeforeUse.Visible := False;
  CheckSaveConfigBeforeUse.AlignTo(CheckEditBeforeUse, 0, 0, 0, 0);
  ButtonOK.Top := Frame.Top + Frame.Height - 2;
  ButtonCancel.Top := ButtonOK.Top;
end;

end.
