unit prgoutl;

interface
  uses
//    Windows,
//    Messages,
//    Forms,
    Classes,
//    Graphics,
//    Menus,
//    StdCtrls,
//    Grids,
    Controls,
    SysUtils,
  KOutline,prgsteps;

  type
    TProgOutline = class(TKOutline)
    private
      FSteps : TAllSteps;
    public
      constructor Create(AOwner: TComponent); override;
      constructor Create2(AOwner: TComponent; ASteps : TAllSteps); virtual;
      Procedure GetColor(o:TCustomKOutline; n:TKOutlineNode; selected : boolean); virtual;
      procedure UpdateBox; virtual;
      function DeleteStep(i:integer) : boolean; virtual;
      procedure AddStep(s:TOneProgStep); virtual;
      function Edit:boolean; virtual;
      function ChangeText(i:integer;s:string):boolean; virtual;
      procedure ChangeCommand(s:TOneProgStep;c:integer);
      property Steps : TAllSteps read FSteps write FSteps;
    end;

implementation

constructor TProgOutline.Create(AOwner: TComponent);
begin
  FSteps := nil;
  inherited;
  Parent := AOwner as TWinControl;
  Align := alClient;
  top := 0;
  left := 0;
  Width := 200;
  Height := 200;
  OutlineStyle := osPlusMinusText;
  OnGetColor := GetColor;
  MinWidth := 200;
end;

constructor TProgOutline.Create2(AOwner: TComponent; ASteps : TAllSteps);
begin
  Create(AOwner);
  FSteps := ASteps;
end;

procedure TProgOutline.UpdateBox;
var pi,l,i:integer; s: TOneProgStep; b: TStepBasic; n: TKOutlineNode;
begin
  Clear;
  l := 1;
  if ( not Assigned(steps) ) then exit;
  for i := 0 to steps.Count-1 do begin
    s := steps.Items[i];
    b := s.Step;
    l := l - b.Level;
    if ( l <= 0 ) or ( i = 0 ) then begin
      Add(0,Trim(s.ListStr));
      l := 1;
    end
    else begin
      pi := i-1;
      n := Items[pi+1];
      while ( n.Level > l ) do begin
        n := n.Parent;
      end;
      if ( n.Level < l ) then AddChild(n.Index,Trim(s.ListStr))
      else                    Add(n.Index,Trim(s.ListStr));
    end;
    l := l + b.Level;
  end;
  Invalidate;
  ItemIndex := Steps.Count-1;
end;

function TProgOutline.DeleteStep(i:integer) : boolean;
var l : integer; s: TOneProgStep; b: TStepBasic;
begin
  Result := false;
  if ( not Assigned(steps) ) then exit;
  s := steps.Items[i];
  b := s.Step;
  if ( b is TStepDummy ) then exit;
  l := b.Level;
  if ( not Steps.Del(i) ) then exit;
  if  l = 0 then Delete(i+1)
  else UpdateBox;
  Result := true;
  ItemIndex := i;
  Steps.Changed;
end;

procedure TProgOutline.AddStep(s:TOneProgStep);
var l,i: integer; so: TOneProgStep; b: TStepBasic;
begin
  if ( not Assigned(steps) ) then exit;
  i := ItemIndex;
  if ( i < 0 ) then i := 0;
  so := Steps.Items[i];
  l := so.step.Level;
  b := s.Step;
  Steps.Insert(i,s);
  if ( b.Level = l ) then Insert(i+1,Trim(s.liststr))
  else                    UpdateBox;
  ItemIndex := i;
  ItemIndex := i+1;
  Steps.Changed;
end;

function TProgOutline.ChangeText(i:integer;s:string):boolean;
begin
  Result := false;
  if ( i < 0 ) then exit;
  if ( i >= ItemCount ) then exit;
  Items[i+1].Text := Trim(s);
  Result := true;
 // Invalidate;
end;

function TProgOutline.Edit:boolean;
var l,i:integer; s:TOneProgStep;
begin
  Result := false;
  if ( not Assigned(steps) ) then exit;
  i := ItemIndex;
  if ( i < 0 ) then exit;
  if ( i >= ItemCount ) then exit;
  s := Steps[i];
  l := s.Step.level;
  if ( not s.Edit ) then exit;
  Steps.FullStop := true;
  if ( s.Step.Level <> l ) then begin
    UpdateBox;
    ItemIndex := i;
  end
  else
    Items[i+1].Text := Trim(s.liststr);
  Result := true;
  Steps.Changed;
 // Invalidate;
end;

procedure TProgOutline.ChangeCommand(s:TOneProgStep;c:integer);
var i,l : integer;
begin
  l := s.Step.Level;
  i := s.CurNr;
  s.Nr := c;
  if ( s.Step.Level <> l ) then begin
    UpdateBox;
    ItemIndex := i;
  end
  else Items[i+1].Text := Trim(s.Liststr);
  Steps.Changed;
end;

Procedure TProgOutline.GetColor(o:TCustomKOutline; n:TKOutlineNode; selected : boolean);
var s:TOneProgStep; b : TStepBasic;
begin
  if ( selected ) then exit;
  if ( not Assigned(Steps) ) then exit;
  s := Steps[n.Index-1];
  b := s.Step;
  if ( not Assigned(b) ) then exit;
  if ( b.Color <> 0 )     then o.Canvas.Brush.Color := b.Color;
  if ( b.TextColor <> 0 ) then o.Canvas.Font.Color := b.TextColor;
  if ( b.Style <> [] )    then o.Canvas.Font.Style := b.Style;
end;

end.
