{

  Copyright (c) 1997-2003 by CoolDev.Com. Part of CoolControls package.

}

{$I versions.inc}
unit PointListUnit;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, CoolCtrls, Buttons, StdCtrls, CoolLists;

type
  TPointListForm = class(TForm)
    PointsGrid: TCoolStringGrid;
    BtnAdd: TBitBtn;
    BtnInsert: TBitBtn;
    BtnDelete: TBitBtn;
    BtnOk: TBitBtn;
    BtnCancel: TBitBtn;
    procedure FormCreate(Sender: TObject);
    procedure PointsGridGetEditMask(Sender: TObject; ACol, ARow: {$IFNDEF VER90}Integer{$ELSE}LongInt{$ENDIF};
      var Value: String);
    procedure BtnAddClick(Sender: TObject);
    procedure BtnDeleteClick(Sender: TObject);
    procedure BtnInsertClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure BtnOkClick(Sender: TObject);
  private
    FPointList: TPointList;
    procedure SetPointList(Value: TPointList);
  public
    property PointList: TPointList read FPointList write SetPointList;
  end;

var
  PointListForm: TPointListForm;

implementation

{$R *.DFM}


procedure TPointListForm.SetPointList(Value: TPointList);
begin
  FPointList.Assign(Value);
end;

procedure TPointListForm.FormCreate(Sender: TObject);
begin
  FPointList := TPointList.Create;

  PointsGrid.Cells[0, 0] := 'X';
  PointsGrid.Cells[1, 0] := 'Y';
end;

procedure TPointListForm.PointsGridGetEditMask(Sender: TObject; ACol,
  ARow: {$IFNDEF VER90}Integer{$ELSE}LongInt{$ENDIF}; var Value: String);
begin
  Value :=  '999';
end;

procedure TPointListForm.BtnAddClick(Sender: TObject);
begin
  PointsGrid.InsertRows(PointsGrid.RowCount, 1);
  PointsGrid.SetFocus;
end;

procedure TPointListForm.BtnDeleteClick(Sender: TObject);
begin
  if PointsGrid.RowCount > 2 then
    PointsGrid.DeleteRows(PointsGrid.Row, 1) else
      begin
        PointsGrid.Cells[0, Pred(PointsGrid.RowCount)] := '';
        PointsGrid.Cells[1, Pred(PointsGrid.RowCount)] := '';
      end;

  PointsGrid.SetFocus;
end;

procedure TPointListForm.BtnInsertClick(Sender: TObject);
begin
  PointsGrid.InsertRows(PointsGrid.Row, 1);
  PointsGrid.SetFocus;
end;

procedure TPointListForm.FormDestroy(Sender: TObject);
begin
  FPointList.Free;
end;

procedure TPointListForm.FormActivate(Sender: TObject);
var
  I: Integer;
  Pt: TPoint;
begin
  if FPointList.Count <> 0 then
  begin
    PointsGrid.DeleteRows(PointsGrid.Row, 1);

    for I := 0 to Pred(FPointList.Count) do
    begin
      PointsGrid.InsertRows(PointsGrid.RowCount, 1);

      Pt := FPointList[I];
      PointsGrid.Cells[0, Pred(PointsGrid.RowCount)] := IntToStr(Pt.X);
      PointsGrid.Cells[1, Pred(PointsGrid.RowCount)] := IntToStr(Pt.Y);
    end;

    PointsGrid.FixedRows := 1;
  end;
end;

function IntString(S: string): Boolean;
var
  I: Integer;
begin
  Result := S <> '';
  if Result then
    for I := 1 to Length(S) do
      if not (S[I] in ['0'..'9']) then
      begin
        Result := False;
        Break;
      end;
end;

procedure TPointListForm.BtnOkClick(Sender: TObject);
var
  I: Integer;
begin
  FPointList.Clear;
  for I := 1 to Pred(PointsGrid.RowCount) do
    if IntString(Trim(PointsGrid.Cells[0, I])) and IntString(Trim(PointsGrid.Cells[1, I])) then
      FPointList.Add(Point(StrToInt(Trim(PointsGrid.Cells[0, I])), StrToInt(Trim(PointsGrid.Cells[1, I]))));
end;

end.
