var
  ATextBox: TvxTextBox;
  AStartsWith: TvxCheckBox;
  ASearchFilePath: TvxCheckBox;

  function CreateForm: TvxSceneForm;
  var
    ARoot: TvxVisualObject;
    ALabel: TvxLabel;
  begin
    result := TvxSceneForm.Create(nil);
    result.Caption := UTF8Encode('Search by Filename part');
    result.Position := poScreenCenter;
    result.Height := 130 * result.DPIFactor;

    ARoot := result.Root;

    ALabel := TvxLabel.Create(ARoot);
    ALabel.Text := 'Enter filename part to search for:';
    ALabel.TextAlign := vgTextAlignNear;
    ALabel.Padding.Left := 10;
    ALabel.Padding.Top := 10;
    ALabel.Align := vaTop;
    ARoot.AddObject(ALabel);

    ATextBox := TvxTextBox.Create(ARoot);
    ATextBox.Padding.Left := 10;
    ATextBox.Padding.Right := 10;
    ATextBox.Padding.Top := 10;
    ATextBox.Align := vaTop;
    ARoot.AddObject(ATextBox);

    AStartsWith := TvxCheckBox.Create(ARoot);
    AStartsWith.Padding.Left := 10;
    AStartsWith.Padding.Right := 10;
    AStartsWith.Padding.Top := 10;
    AStartsWith.Align := vaTop;
    AStartsWith.Text := 'Should start with';
    ARoot.AddObject(AStartsWith);

    ASearchFilePath := TvxCheckBox.Create(ARoot);
    ASearchFilePath.Padding.Left := 10;
    ASearchFilePath.Padding.Right := 10;
    ASearchFilePath.Padding.Top := 10;
    ASearchFilePath.Align := vaTop;
    ASearchFilePath.Text := 'Search in full file path';
    ARoot.AddObject(ASearchFilePath);
  end;

  procedure SearchFileName(ASearchString: WideString);
  var
    ADs: TDBXOMClientDataSet;
    AData: TidElement;
    AColl: TImageCollection;
  begin
    ADs := PublicCatalog.NewDataset;

    ADs.CommandText := 'select ' + PublicCatalog.FullColumnList('') + ' ' +
                       'from   v_catalogitem ' +
                       'where  ' + iif(ASearchFilePath.IsChecked, 'FullFileName', 'FileName') + ' like ''' + iif(AStartsWith.IsChecked, '', '%') + ASearchString + '%' + ''' ' +
                       'UNION ALL ' +
                       'select ' + PublicCatalog.FullColumnList('') + ' ' +
                       'from   v_catalogitemversion ' +
                       'where  ' + iif(ASearchFilePath.IsChecked, 'FullFileName', 'FileName') + ' like ''' + iif(AStartsWith.IsChecked, '', '%') + ASearchString + '%' + ''' ' +
                       '';
    ADs.LockType := ReadLockType;
    ADs.OpenSet
    try
      AData := TidElement.Create(nil);
      try
        PublicBroadCast(nil, 'OpenNewTab', nil);
        PublicBroadCastRequest(nil, 'ActiveCollection', AData);
        AColl := AData.Data;
        AColl.Clear;
        AColl.Items.AddDataset(ADs, True);
        PublicBroadCast(nil, 'RefreshActiveCollection', nil);
      finally
        AData.Free;
      end;
    finally
      ADs.CloseSet;
    end;

    PublicCatalog.FreeDataset(ADs);
  end;

var
  AForm: TvxSceneForm;
begin
  AForm := CreateForm;
  try
    ATextBox.Text := ReadFromRegistry('', 'scSearchFileName.LastText', '');
    AStartsWith.IsChecked := ReadFromRegistry('', 'scSearchFileName.LastStartsWith', 0) = 1;
    ASearchFilePath.IsChecked := ReadFromRegistry('', 'scSearchFileName.SearchFilePath', 0) = 1;

    AForm.PrepareModal([mbOK, mbCancel]);
    AForm.ShowModal;
    if AForm.ModalResult = mrOk then
    begin
      WriteToRegistry('', 'scSearchFileName.SearchFilePath', iif(ASearchFilePath.IsChecked, 1, 0));
      WriteToRegistry('', 'scSearchFileName.LastStartsWith', iif(AStartsWith.IsChecked, 1, 0));
      WriteToRegistry('', 'scSearchFileName.LastText', ATextBox.Text);
      SearchFileName(ATextBox.Text);
    end;
  finally
    AForm.Free;
  end;
end;
