unit U_CrossWordHelper;
{Copyright 2001, Gary Darby, Intellitech Systems Inc., www.DelphiForFun.org

 This program may be used or modified for any non-commercial purpose
 so long as this original notice remains in place.
 All other rights are reserved
 }
{A word completion program}
interface

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

type
  TWordCompleteForm = class(TForm)
    SolveBtn: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    ListBox1: TListBox;
    LoadBtn: TButton;
    procedure SolveBtnClick(Sender: TObject);
    procedure LoadBtnClick(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    dic:TDic;
    procedure solveit;
  end;

var
  WordCompleteForm: TWordCompleteForm;

implementation
{$R *.DFM}

Procedure TWordCompleteForm.Solveit;

      Procedure Doit(Dic:TDic; s:string);
      var
        validword:string;
        i,len:integer;
        error:boolean;
        abbrev,foreign:boolean;
      Begin
        {if we have the first letter, search it only, otherwise search all}
        Len:=length(s);
        if s[1] <>'_' then  Dic.SetRange(s[1],Len,s[1],Len)
        else  Dic.SetRange('a',Len,'z',Len);
        while dic.getnextword(validword,abbrev,foreign) do  {get a word}
        if (not abbrev) and (not foreign) then
        Begin
          error:=false;
          i:=0;
          {match sure all letters match given letters}
          while (not error) and (i<len) do
          Begin
            inc(i);
            if (s[i]='_') or (s[i]=validword[i]) then
            else error :=true;
          end;
          {if so, show it}
          if not error then listbox1.items.add(validword);
        End;
      End;

var
  s:string;
  i,len:integer;
  error:boolean;
Begin
  listbox1.items.clear;
  s:=lowercase(edit1.text);
  len:=length(s);
  error:=false;
  If len>0 then
  Begin
    for i:= 1 to len do if not (s[i] in ['a'..'z','_']) then error:=true;
    If not error then
    Begin
      If dic.dicloaded then Doit(dic,s);
      {If privdic.dicloaded then Doit(Privdic,s);}
      if listbox1.items.count=0 then listbox1.items.add('No words found');
    End
    else showmessage('Words may contain only letters and underscore characters');
  end;
end;

procedure TWordCompleteForm.SolveBtnClick(Sender: TObject);
begin
  solveit;
end;

procedure TWordCompleteForm.LoadBtnClick(Sender: TObject);
begin
  with DicForm.opendialog1 do
  begin
    initialdir:=extractfilepath(dic.dicname);
    if execute then
    begin
      dic.loadDicFromFile(filename);
      caption:='Cross Word Helper - Current Dictionary:'+dic.dicname;
    end;
  end;
end;

procedure TWordCompleteForm.FormActivate(Sender: TObject);
begin
  If not assigned(dic) then
  begin
    Dic:=TDic.Create(false);
    Dic.LoadDefaultDic; {load the last dictionary loaded}
    caption:='Cross Word Helper - Current Dictionary:'+dic.dicname;
  end;
end;

end.