unit findcomp;
{
   File to find components from application

   Author:  Vesa Lappalainen
   Date:    25.4.1998
   Changes:

}

interface
uses StdCtrls,classes;

procedure FillComboWithComponents(cb:TComboBox;foi:integer;ct:array of TComponentClass); overload;
procedure FillComboWithComponents(cb:TComboBox;const fn:string;ct:array of TComponentClass); overload
procedure FillComboWithForms(cb:TComboBox;ct:TComponentClass);
//function FindAppComponent(const n:string; var f,c:integer):TComponent;
//function FindAppComponent(const fn,cn:string; var f,c:integer):TComponent;
function FindAppComponent(const fn,cn:string):TComponent;

implementation
uses Forms,kstring;

function InTypeSet(c:TComponent;ct:array of TComponentClass):boolean;
var i:integer;
begin
  Result := true;
  for i:=0 to High(ct) do
    if ( c is ct[i] ) then exit;
  Result := false;
end;

procedure FillComboWithComponents(startname:string;cb:TComboBox;fo:TComponent;ct:array of TComponentClass); overload;
var ci:integer;
begin
  for ci:=0 to fo.ComponentCount-1 do begin
    if ( fo.Components[ci] is TFrame ) then begin
      FillComboWithComponents(startname+fo.Components[ci].Name+'.',cb,fo.Components[ci],ct);
      continue;
    end;
    if ( not (InTypeSet(fo.Components[ci],ct)) ) then continue;
    cb.Items.Add(startname + fo.Components[ci].Name);
  end;
end;

procedure FillComboWithComponents(cb:TComboBox;foi:integer;ct:array of TComponentClass);
var fi,si,ei:integer; fo:TForm; startname : string;
begin
  cb.Clear;
  si := foi; ei := foi;
  if ( si < 0 ) then begin si := 0; ei := Application.ComponentCount-1; end;
  for fi:=si to ei do begin
    if ( not (Application.Components[fi] is TForm) ) then continue;
    fo := Application.Components[fi] as TForm;
    startname := '';
    if ( foi < 0 ) then startname := fo.Name + '.';
    FillComboWithComponents(startname,cb,fo,ct);
  end;
end;

procedure FillComboWithComponents(cb:TComboBox;const fn:string;ct:array of TComponentClass);
var fi,si,ei:integer; fo:TForm; 
begin
  cb.Clear;
  si := 0; ei := Application.ComponentCount-1;
  for fi:=si to ei do begin
    if ( not (Application.Components[fi] is TForm) ) then continue;
    fo := Application.Components[fi] as TForm;
    if ( fo.Name <> fn ) then continue;
    FillComboWithComponents('',cb,fo,ct);
  end;
end;

procedure FillComboWithForms(cb:TComboBox;ct:TComponentClass);
var fi:integer; fo:TForm;
begin
  cb.Clear;
  for fi:=0 to Application.ComponentCount-1 do begin
    if ( not (Application.Components[fi] is TForm) ) then continue;
    fo := Application.Components[fi] as TForm;
    cb.Items.Add(fo.Name);
  end;
end;


function FindComponentByName(fo:TComponent; const s:string):TComponent;
var ci:integer;  p : TComponent; n,cn:string;
begin
  cn := s;
  n := Separate(cn,'.');
  Result := nil;
  for ci:=0 to fo.ComponentCount-1 do begin
    p := fo.Components[ci];
    if ( p.Name <> n ) then continue;
    if ( p is TFrame ) and ( cn <> '' ) then
      Result := FindComponentByName(p,cn)
    else
      Result := p;
    exit;
  end;
end;

//function FindAppComponent(const fn,cn:string; var f,c:integer):TComponent;
function FindAppComponent(const fn,cn:string):TComponent;
var fi:integer; fo:TForm; //p : TComponent; //fn, cn:string;
begin
  Result := nil;
//  f := -1; c := -1;
//  fi := pos('.',n);
//  fn := copy(n,1,fi-1); cn := copy(n,fi+1,100);
  for fi:=0 to Application.ComponentCount-1 do begin
    if ( not (Application.Components[fi] is TForm) ) then continue;
    fo := Application.Components[fi] as TForm;
    if ( fo.Name <> fn ) and ( fn <> '' ) then continue;
    Result := FindComponentByName(fo,cn);
  end;
(*
    for ci:=0 to fo.ComponentCount-1 do begin
      p := fo.Components[ci];
      if ( p.Name <> cn ) then continue;
//      f := fi; c := ci;
      Result := p;
      exit;
    end;
  end;
*)
end;


end.
