{ *************************************************************************** }
{                                                                             }
{ EControl Syntax Editor SDK                                                  }
{                                                                             }
{ Copyright (c) 2004 - 2008 EControl Ltd., Zaharov Michael                    }
{     www.econtrol.ru                                                         }
{     support@econtrol.ru                                                     }
{                                                                             }
{ *************************************************************************** }
{$I ecSyntEdit.INC}

unit ecGoLineDlg;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TecGotoLineDlg = class(TForm)
    Label1: TLabel;
    Combo: TComboBox;
    Bevel1: TBevel;
    Button1: TButton;
    Button2: TButton;
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormShow(Sender: TObject);
  private
    FMax: integer;
  public

  end;

function SelectLineNumber(var LineNum: integer; LinesCount: integer): Boolean;

implementation

{$R *.dfm}

var
  LineNumbersHistory: TList = nil;

function SelectLineNumber(var LineNum: integer; LinesCount: integer): Boolean;
begin
  with TecGotoLineDlg.Create(nil) do
   try
     Combo.Text := IntToStr(LineNum);
     FMax := LinesCount;
     Result := ShowModal = mrOk;
     if Result then
      begin
       LineNum := StrToInt(Combo.Text);
       if LineNumbersHistory = nil then
         LineNumbersHistory := TList.Create;
       LineNumbersHistory.Remove(TObject(LineNum));
       LineNumbersHistory.Add(TObject(LineNum));
       if LineNumbersHistory.Count > 20 then
         LineNumbersHistory.Delete(0);
      end;
   finally
     Free;
   end;
end;

procedure TecGotoLineDlg.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
var L: integer;
begin
  if ModalResult = mrOK then
    begin
      L := StrToInt(Combo.Text);
      CanClose := (L >= 1) and (L <= FMax);
      if not CanClose then
        MessageBox(Handle, {$IFNDEF EC_DOTNET}PChar{$ENDIF}(Format('Line number must be between 1 and %d', [FMax])), 'Error', MB_OK or MB_ICONHAND);
    end;
end;

procedure TecGotoLineDlg.FormShow(Sender: TObject);
var i: integer;
begin
  Combo.Items.Clear;
  if LineNumbersHistory <> nil then
   for i := LineNumbersHistory.Count - 1 downto 0 do
    Combo.Items.Add(IntToStr(integer(LineNumbersHistory[i])));
end;

initialization

finalization
  if LineNumbersHistory <> nil then
    begin
      LineNumbersHistory.Free;
      LineNumbersHistory := nil;
    end;

end.
