unit _frmSearch;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, cxGraphics, cxControls, cxLookAndFeels,
  cxLookAndFeelPainters, cxContainer, cxEdit, dxSkinsCore,
  dxSkinOffice2016Colorful, Vcl.Menus, cxButtons, cxCheckBox, cxGroupBox,
  cxTextEdit, cxMaskEdit, cxDropDownEdit, cxRadioGroup, dxSkinOffice2019Colorful;

type
  TfrmSearch = class(TForm)
    Label1: TLabel;
    cbSearchText: TcxComboBox;
    gbSearchOptions: TcxGroupBox;
    cbSearchCaseSensitive: TcxCheckBox;
    cbSearchWholeWords: TcxCheckBox;
    cbSearchFromCursor: TcxCheckBox;
    cbSearchSelectedOnly: TcxCheckBox;
    cbRegularExpression: TcxCheckBox;
    btnOK: TcxButton;
    btnCancel: TcxButton;
    rgSearchDirection: TcxRadioGroup;
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
  private
    function GetSearchBackwards: boolean;
    function GetSearchCaseSensitive: boolean;
    function GetSearchFromCursor: boolean;
    function GetSearchInSelection: boolean;
    function GetSearchText: string;
    function GetSearchTextHistory: string;
    function GetSearchWholeWords: boolean;
    procedure SetSearchBackwards(Value: boolean);
    procedure SetSearchCaseSensitive(Value: boolean);
    procedure SetSearchFromCursor(Value: boolean);
    procedure SetSearchInSelection(Value: boolean);
    procedure SetSearchText(Value: string);
    procedure SetSearchTextHistory(Value: string);
    procedure SetSearchWholeWords(Value: boolean);
    procedure SetSearchRegularExpression(const Value: boolean);
    function GetSearchRegularExpression: boolean;
  public
    property SearchBackwards: boolean read GetSearchBackwards
      write SetSearchBackwards;
    property SearchCaseSensitive: boolean read GetSearchCaseSensitive
      write SetSearchCaseSensitive;
    property SearchFromCursor: boolean read GetSearchFromCursor
      write SetSearchFromCursor;
    property SearchInSelectionOnly: boolean read GetSearchInSelection
      write SetSearchInSelection;
    property SearchText: string read GetSearchText write SetSearchText;
    property SearchTextHistory: string read GetSearchTextHistory
      write SetSearchTextHistory;
    property SearchWholeWords: boolean read GetSearchWholeWords
      write SetSearchWholeWords;
    property SearchRegularExpression: boolean read GetSearchRegularExpression
      write SetSearchRegularExpression;
  end;

var
  frmSearch: TfrmSearch;

implementation

{$R *.DFM}

uses _frmMain;


function TfrmSearch.GetSearchBackwards: boolean;
begin
  Result := rgSearchDirection.ItemIndex = 1;
end;

function TfrmSearch.GetSearchCaseSensitive: boolean;
begin
  Result := cbSearchCaseSensitive.Checked;
end;

function TfrmSearch.GetSearchFromCursor: boolean;
begin
  Result := cbSearchFromCursor.Checked;
end;

function TfrmSearch.GetSearchInSelection: boolean;
begin
  Result := cbSearchSelectedOnly.Checked;
end;

function TfrmSearch.GetSearchRegularExpression: boolean;
begin
  Result := cbRegularExpression.Checked;
end;

function TfrmSearch.GetSearchText: string;
begin
  Result := cbSearchText.Text;
end;

function TfrmSearch.GetSearchTextHistory: string;
var
  i: integer;
begin
  Result := '';
  for i := 0 to cbSearchText.Properties.Items.Count - 1 do
  begin
    if i >= 10 then
      break;
    if i > 0 then
      Result := Result + #13#10;
    Result := Result + cbSearchText.Properties.Items[i];
  end;
end;

function TfrmSearch.GetSearchWholeWords: boolean;
begin
  Result := cbSearchWholeWords.Checked;
end;

procedure TfrmSearch.SetSearchBackwards(Value: boolean);
begin
  rgSearchDirection.ItemIndex := Ord(Value);
end;

procedure TfrmSearch.SetSearchCaseSensitive(Value: boolean);
begin
  cbSearchCaseSensitive.Checked := Value;
end;

procedure TfrmSearch.SetSearchFromCursor(Value: boolean);
begin
  cbSearchFromCursor.Checked := Value;
end;

procedure TfrmSearch.SetSearchInSelection(Value: boolean);
begin
  cbSearchSelectedOnly.Checked := Value;
end;

procedure TfrmSearch.SetSearchText(Value: string);
begin
  cbSearchText.Text := Value;
end;

procedure TfrmSearch.SetSearchTextHistory(Value: string);
begin
  cbSearchText.Properties.Items.Text := Value;
end;

procedure TfrmSearch.SetSearchWholeWords(Value: boolean);
begin
  cbSearchWholeWords.Checked := Value;
end;

procedure TfrmSearch.SetSearchRegularExpression(
  const Value: boolean);
begin
  cbRegularExpression.Checked := Value;
end;

{ event handlers }

procedure TfrmSearch.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  frmMain.MainConfig.WriteInteger('SearchPos', 'Left', Left);
  frmMain.MainConfig.WriteInteger('SearchPos', 'Top', Top);
end;

procedure TfrmSearch.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
var
  s: string;
  i: integer;
begin
  if ModalResult = mrOK then
  begin
    s := cbSearchText.Text;
    if s <> '' then
    begin
      i := cbSearchText.Properties.Items.IndexOf(s);
      if i > -1 then
      begin
        cbSearchText.Properties.Items.Delete(i);
        cbSearchText.Properties.Items.Insert(0, s);
        cbSearchText.Text := s;
      end else
        cbSearchText.Properties.Items.Insert(0, s);
    end;
  end;
end;

procedure TfrmSearch.FormShow(Sender: TObject);
begin
  Left := frmMain.MainConfig.ReadInteger('SearchPos', 'Left', Round((Screen.Width - Width) div 2));

  if Left >= Screen.Width then
  begin
    Left := 10;
  end;

  Top := frmMain.MainConfig.ReadInteger('SearchPos', 'Top', Round((Screen.Height - Height) div 2));

  if Top >= Screen.Height then
  begin
    Top := 10;
  end;

  cbSearchText.SetFocus;
end;

end.

 