unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, ExtCtrls, DB, DBTables, ComCtrls, StdCtrls, DBCtrls,
  Grids, DBGrids, Mask, Buttons, IniFiles, OleCtrls, HTTSLib_TLB, StrUtils,
  ShellAPI;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Table1: TTable;
    DataSource1: TDataSource;
    DBEdit1: TDBEdit;
    DBEdit2: TDBEdit;
    MainMenu1: TMainMenu;
    Commands1: TMenuItem;
    Save1: TMenuItem;
    N2: TMenuItem;
    Exit1: TMenuItem;
    N3: TMenuItem;
    N4: TMenuItem;
    Panel3: TPanel;
    DBEdit3: TDBEdit;
    Help1: TMenuItem;
    N5: TMenuItem;
    N6: TMenuItem;
    Table1Word: TStringField;
    Table1Mean: TStringField;
    Table1Categoly: TStringField;
    Table1Synonim: TStringField;
    N7: TMenuItem;
    N8: TMenuItem;
    N9: TMenuItem;
    N10: TMenuItem;
    N11: TMenuItem;
    N12: TMenuItem;
    Show1: TMenuItem;
    N13: TMenuItem;
    N14: TMenuItem;
    Timer1: TTimer;
    N1: TMenuItem;
    N15: TMenuItem;
    N16: TMenuItem;
    TextToSpeech1: TTextToSpeech;
    N17: TMenuItem;
    N18: TMenuItem;
    N19: TMenuItem;
    N20: TMenuItem;
    N21: TMenuItem;
    N22: TMenuItem;
    sakura: TMenuItem;
    orange: TMenuItem;
    sea: TMenuItem;
    Detail1: TMenuItem;
    N23: TMenuItem;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    N24: TMenuItem;
    N25: TMenuItem;
    ool1: TMenuItem;
    N26: TMenuItem;
    N28: TMenuItem;
    N29: TMenuItem;
    autumn: TMenuItem;
    jiro1: TMenuItem;
    winter: TMenuItem;
    N30: TMenuItem;
    N31: TMenuItem;
    Table2: TTable;
    Table2Word: TStringField;
    Table2Mean: TStringField;
    Table2Categoly: TStringField;
    Table2Synonim: TStringField;
    N27: TMenuItem;
    soccor1: TMenuItem;
    brazzile: TMenuItem;
    argentina: TMenuItem;
    procedure Table1AfterScroll(DataSet: TDataSet);
    procedure ComboBox1Change(Sender: TObject);
    procedure Save1Click(Sender: TObject);
    procedure Exit1Click(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure N3Click(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Panel2Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Panel1Click(Sender: TObject);
    procedure N12Click(Sender: TObject);
    procedure N6Click(Sender: TObject);
    procedure N8Click(Sender: TObject);
    procedure N13Click(Sender: TObject);
    procedure N14Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N16Click(Sender: TObject);
    procedure Table1BeforeScroll(DataSet: TDataSet);
    procedure N18Click(Sender: TObject);
    procedure SkinValue(Sender: TObject);
    procedure Detail1Click(Sender: TObject);
    procedure N23Click(Sender: TObject);
    procedure Table1AfterOpen(DataSet: TDataSet);
    procedure FormMouseWheel(Sender: TObject; Shift: TShiftState;
      WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure N25Click(Sender: TObject);
    procedure N27Click(Sender: TObject);
    procedure N29Click(Sender: TObject);
    procedure Table1BeforeOpen(DataSet: TDataSet);
    procedure Table1BeforeClose(DataSet: TDataSet);
    procedure N30Click(Sender: TObject);
    procedure N31Click(Sender: TObject);
  private
    { Private 宣言 }
    Loop: integer;
    Origin: TColor;
    FSkin: string;
    procedure Panel2mode;
    procedure Panel3mode;
    procedure SetSkin(const Value: string);
  public
    { Public 宣言 }
    DBHide: Boolean;
    RecCount: integer;
    property Skin: string read FSkin write SetSkin;
  end;

var
  Form1: TForm1;

implementation

uses Unit3, Unit5, Unit7, Unit9, Unit6, Unit11;

{$R *.dfm}

procedure TForm1.Table1AfterScroll(DataSet: TDataSet);
var
  i, j: integer;
  s: string;
begin
  if (Timer1.Enabled = true)and(Table1.Eof = true)and(Table1.Bof = false) then
  begin
        Table1.First;
  end;
  if Form2.ListBox1.Items.IndexOf(DBEdit1.Text) = -1 then
  begin
        Panel1.Font.Color:=Origin;
  end else
  begin
        Panel1.Font.Color:=clRed;
  end;
  if DBHide = true then
  begin
        s:='';
        i:=Form3.ComboBox1.ItemIndex+3;
    if Form3.RadioButton1.Checked = true then
    begin
      for j:=i+1 to Length(DBEdit1.Text) do
      begin
        case DBEdit1.Text[j] of
        ' ','(',')':
                s:=s+DBEdit1.Text[j];
        else
                s:=s+'*';
        end;
      end;
        Panel1.Caption:=Copy(DBEdit1.Text,1,i)+s;
    end else
    begin
      for j:=1 to Length(DBEdit1.Text)-i do
      begin
        case DBEdit1.Text[j] of
        ' ','(',')':
                s:=s+DBEdit1.Text[j];
        else
                s:=s+'*';
        end;
      end;
        j:=Length(DBEdit1.Text);
        Panel1.Caption:=s+Copy(DBEdit1.Text,j-i+1,j);
    end;
  end else
  begin
        Panel1.Caption:=DBEdit1.Text;
  end;
        Caption:=ExtractFileName(Table1.TableName)+' '+IntToStr(RecCount)+
                ' Categoly='+Table1.FieldByName('Categoly').AsString+' ';
        Panel2mode;
        Panel3mode;
end;

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
        Table1AfterScroll(nil);
end;

procedure TForm1.Save1Click(Sender: TObject);
var
  s: TIniFile;
begin
  if SaveDialog1.Execute = true then
  begin
        CopyFile(PChar(Table1.TableName),PChar(SaveDialog1.FileName),false);
        Table1.Close;
        s:=TIniFile.Create(ExtractFilePath(Application.ExeName)+'englishdb.ini');
    try
        Table1.TableName:=SaveDialog1.FileName;
        s.WriteString('database','tablename',SaveDialog1.FileName);
    finally
        Table1.Open;
        s.Free;
    end;
  end;
end;

procedure TForm1.Exit1Click(Sender: TObject);
begin
        Close;
end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  i: integer;
begin
  if Timer1.Enabled = true then
  begin
    if Key = VK_ESCAPE then
    begin
        N1Click(Sender);
        Exit;
    end else
        if Key <> VK_SPACE then
    begin
        Exit;
    end;
  end else
        if Key = VK_RETURN then
  begin
      if N18.Checked = true then
      begin
        if DBEdit1.Text <> '' then
        begin
                TextToSpeech1.Speak(DBEdit1.Text);
        end;
      end else
                if DBEdit3.Text <> '' then
      begin
                TextToSpeech1.Speak({DBEdit1.Text+','+}AnsiReplaceStr(DBEdit3.Text,'\',','));
      end;
      if Form3.CheckBox1.Checked = true then
      begin
                Exit;
      end;
  end else
        if Key = VK_ESCAPE then
  begin
        N1Click(nil);
        Exit;
  end;
  if Form3.CheckBox1.Checked = true then
  begin
    if Key = VK_SPACE then
    begin
        DBHide:=not DBHide;
        Table1AfterScroll(nil);
    end else
    begin
        Randomize;
        i:=Table1.RecNo;
      repeat
                Table1.First;
                Table1.MoveBy(Random(RecCount));
      until i <> Table1.RecNo;
    end;
        Exit;
  end;
  case Key of
  VK_DOWN:
        Table1.Next;
  VK_UP:
        Table1.Prior;
  VK_SPACE:
  begin
        DBHide:=not DBHide;
        N11.Checked:=DBHide;
        Table1AfterScroll(nil);
  end;
  VK_PRIOR:
    for i:=1 to 5 do
    begin
      if (i > 1)and(Form2.ListBox1.Items.IndexOf(
        Table1.FieldByName('Word').AsString) <> -1) then
      begin
                break;
      end;
        Table1.Prior;
    end;
  VK_NEXT:
    for i:=1 to 5 do
    begin
      if (i > 1)and(Form2.ListBox1.Items.IndexOf(
        Table1.FieldByName('Word').AsString) <> -1) then
      begin
                break;
      end;
        Table1.Next;
    end;
  VK_HOME:
        Table1.First;
  VK_END:
        Table1.Last;
  end;
end;

procedure TForm1.N3Click(Sender: TObject);
begin
        Form3.Show;
end;

procedure TForm1.N5Click(Sender: TObject);
begin
        AboutBox.ShowModal;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  s: TIniFile;
begin        {
        Table1.IndexName:='Categoly';
        Table1.First;
        }
        s:=TIniFile.Create(ExtractFilePath(Application.ExeName)+'englishdb.ini');
  try
        DBHide:=s.ReadBool('active','hide',true);
        N11.Checked:=DBHide;
        N25.Checked:=s.ReadBool('active','mouse',true);
        Table1.TableName:=s.ReadString('database','tablename','english.db');
    if s.ReadBool('sound','main',true) = true then
    begin
        N18.Checked:=true;
    end else
    begin
        N19.Checked:=true;
    end;
    if s.ReadBool('field','mean',false) then
    begin
        Panel2.BorderStyle:=bsSingle;
    end else
    begin
        Panel2.BorderStyle:=bsNone;
    end;
    if s.ReadBool('field','synonym',false) then
    begin
        Panel3.BorderStyle:=bsSingle;
    end else
    begin
        Panel3.BorderStyle:=bsNone;
    end;
  finally
        s.Free;
  end;
end;

procedure TForm1.Panel2Click(Sender: TObject);
begin
  with Sender as TPanel do
  begin
  if BorderStyle = bsNone then
  begin
        BorderStyle:=bsSingle;
  end else
  begin
        BorderStyle:=bsNone;
  end;
  end;
        Panel2mode;
        Panel3mode;
end;

procedure TForm1.Panel2mode;
begin
  if Panel2.BorderStyle = bsNone then
  begin
        Panel2.Caption:=DBEdit2.Text;
  end else
  begin
        Panel2.Caption:='[答えてください]';
  end;
end;

procedure TForm1.Panel3mode;
begin
  if (DBEdit3.Text = '')or(Panel3.BorderStyle = bsSingle) then
  begin
        Panel3.Caption:='-';
  end else
  begin
        Panel3.Caption:=DBEdit3.Text;
  end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
  s: TIniFile;
begin
        Form3.Hide;
        s:=TIniFile.Create(ExtractFilePath(Application.ExeName)+'englishdb.ini');
  try
        s.WriteBool('mode','hide',DBHide);
        s.WriteBool('mode','random',Form3.CheckBox1.Checked);
        s.WriteInteger('mode','categoly',Form3.ComboBox2.ItemIndex);
        s.WriteInteger('mode','count',Form3.ComboBox1.ItemIndex);
        s.WriteInteger('mode','font',Form3.UpDown1.Position);
        s.WriteInteger('timer','interval',Form3.UpDown2.Position);
        s.WriteBool('sound','main',N18.Checked);
        s.WriteString('skin','name',Skin);
        s.WriteBool('bookmark','remained',OKBottomDlg.CheckBox1.Checked);
        s.WriteBool('database','readonly',OKBottomDlg.CheckBox2.Checked);
        s.WriteString('database','tablename',Table1.TableName);
        s.WriteBool('active','hide',DBHide);
        s.WriteBool('active','mouse',N25.Checked);
    if Table1.Active = true then
    begin
        s.WriteInteger('mode','index',Table1.RecNo);
        Table1BeforeClose(nil);
    end;
    if Panel2.BorderStyle = bsNone then
    begin
        s.WriteBool('field','mean',false);
    end else
    begin
        s.WriteBool('field','mean',true);
    end;
    if Panel3.BorderStyle = bsNone then
    begin
        s.WriteBool('field','synonym',false);
    end else
    begin
        s.WriteBool('field','synonym',true);
    end;
    if Panel1.Top = 0 then
    begin
        s.WriteInteger('field','position',0);;
    end else
    begin
        if Panel1.Top = 65 then
    begin
        s.WriteInteger('field','position',1);
    end else
        s.WriteInteger('field','position',2);
    end;
    if Form3.RadioButton1.Checked = true then
    begin
        s.WriteBool('mode','front',true);
    end else
    begin
        s.WriteBool('mode','front',false);
    end;
  finally
        s.Free;
  end;
end;

procedure TForm1.Panel1Click(Sender: TObject);
var
  i: integer;
begin
        i:=Form2.ListBox1.Items.IndexOf(DBEdit1.Text);
  if i = -1 then
  begin
        Form2.Show;
        Panel1.Font.Color:=clRed;
        Form2.ListBox1.Items.Add(DBEdit1.Text);
  end else
  begin
        Panel1.Font.Color:=Origin;
        Form2.ListBox1.Items.Delete(i);
  end;
        Form1.Show;
end;

procedure TForm1.N12Click(Sender: TObject);
begin
        Form2.Show;
end;

procedure TForm1.N6Click(Sender: TObject);
begin
        Application.HelpFile:='Englishdb.hlp';
        Application.HelpCommand(HELP_FINDER,0);
end;

procedure TForm1.N8Click(Sender: TObject);
var
  s: Word;
begin
  if Sender = N9 then
  begin
        s:=VK_UP;
  end else
        if Sender = N8 then
  begin
        s:=VK_DOWN;
  end else
  begin
        s:=VK_SPACE;
  end;
        FormKeyDown(Sender,s,[]);
end;

procedure TForm1.N13Click(Sender: TObject);
begin
  if Panel1.Top > 0 then
  begin
        Panel1.Top:=Panel1.Top-65;
  end;
end;

procedure TForm1.N14Click(Sender: TObject);
begin
  if Panel1.Top < 130 then
  begin
        Form1.AutoSize:=false;
    if Panel1.Top = 0 then
    begin
        Panel3.Align:=alNone;
        Panel1.Align:=alNone;
        Panel1.Top:=65;
        Panel1.Align:=alTop;
        Panel3.Align:=alTop;
    end else
    begin
        Panel1.Align:=alNone;
        Panel1.Top:=130;
        Panel1.Align:=alTop;
    end;
        Form1.AutoSize:=true;
  end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if Form2.ListBox1.Items.IndexOf(DBEdit1.Text) > -1 then
  begin              
    if Loop < 1 then
    begin
        TextToSpeech1.Speak(DBEdit1.Text);
        inc(Loop);
        Exit;
    end else
    begin
        Loop:=0;
    end;
  end;
  if Table1.Eof = true then
  begin
        Table1.First;
  end else
  begin
        Table1.Next;
  end;
        TextToSpeech1.Speak(DBEdit1.Text);
end;

procedure TForm1.N1Click(Sender: TObject);
begin
  if N1.Checked = true then
  begin
        N1.Checked:=false;
  end else
  begin
        TextToSpeech1.Speak(DBEdit1.Text);
        N1.Checked:=true;
  end;
        Timer1.Enabled:=N1.Checked;
end;

procedure TForm1.N16Click(Sender: TObject);
begin
        Form2.Hide;
end;

procedure TForm1.Table1BeforeScroll(DataSet: TDataSet);
begin
        TextToSpeech1.StopSpeaking;
end;

procedure TForm1.N18Click(Sender: TObject);
begin
        (Sender as TMenuItem).Checked:=true;
end;

procedure TForm1.SkinValue(Sender: TObject);
begin
        Skin:=(Sender as TMenuItem).Name;
end;

procedure TForm1.SetSkin(const Value: string);
var
  s: TColor;
begin
  if Value = 'N22' then
  begin
        s:=clTeal;
        Origin:=clWhite;
  end else
        if Value = 'sakura' then
  begin
        s:=16638460;
        Origin:=4194432;
  end else
        if Value = 'orange' then
  begin
        s:=4227327;
        Origin:=-2147483640;
  end else
        if Value = 'sea' then
  begin
        s:=16230144;
        Origin:=3704114;
  end else
        if Value = 'autumn' then
  begin
        s:=4210816;
        Origin:=4227327;
  end else
        if Value = 'winter' then
  begin
        s:=16777215;
        Origin:=16776960;
  end else
        if Value = 'brazzile' then
  begin
        s:=8454143;
        Origin:=32768;
  end else
        if Value = 'argentina' then
  begin
        s:=16699265;
        Origin:=16777215;
  end else
  begin
        s:=clBtnFace;
        Origin:=clBlack;
  end;
        FSkin:=Value;
        Panel1.Color:=s;
        Panel2.Color:=s;
        Panel3.Color:=s;
        Panel2.Font.Color:=Origin;
        Panel3.Font.Color:=Origin;
        Table1AfterScroll(nil);
end;

procedure TForm1.Detail1Click(Sender: TObject);
begin
        OKBottomDlg.ShowModal;
end;

procedure TForm1.N23Click(Sender: TObject);
var
  s: TIniFile;
begin
  if OpenDialog1.Execute = true then
  begin
    if (FileExists(OpenDialog1.FileName) = false)and
      (MessageDlg('新規作成',mtConfirmation,mbOKCancel,0) = mrCancel) then
    begin
        Exit;
    end;
        Table1.Close;
        s:=TIniFile.Create(ExtractFilePath(Application.ExeName)+'englishdb.ini');
    try
        Table1.TableName:=OpenDialog1.FileName;
        s.WriteString('database','tablename',OpenDialog1.FileName);
    finally
        Table1.Open;
        s.Free;
    end;
  end;
end;

procedure TForm1.Table1AfterOpen(DataSet: TDataSet);
var
  s: string;
begin
        Form3.Caption:=ExtractFileName(Table1.TableName);
        Form3.ComboBox2.Items.Clear;
        Form3.ComboBox2.Items.Add('all');
        Form3.ComboBox2.ItemIndex:=0;
        s:=ChangeFileExt(Table1.TableName,'.txt');
  if FileExists(s) = true then
  begin
        Form2.ListBox1.Items.LoadFromFile(s);
  end else
  begin
        Form2.ListBox1.Items.Clear;
  end;
        Table1.AfterScroll:=nil;
  try
    while Table1.Eof = false do
    begin
        s:=Table1.FieldByName('Categoly').AsString;
      if (s <> '')and(Form3.ComboBox2.Items.IndexOf(s) = -1) then
      begin
                Form3.ComboBox2.Items.Add(s);
      end;
        Table1.Next;
    end;
  finally
        Table1.AfterScroll:=Table1AfterScroll;
  end;
        RecCount:=Table1.RecordCount;
        Table1.First;
end;

procedure TForm1.FormMouseWheel(Sender: TObject; Shift: TShiftState;
  WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
var
  s: Word;
begin
  if (Timer1.Enabled = true)or(N25.Checked = false) then
  begin
        Exit;
  end;
  if WheelDelta < 0 then
  begin
    if ssShift in Shift then
    begin
        s:=VK_NEXT;
    end else
    begin
        s:=VK_DOWN;
    end;
  end else
  begin
    if ssShift in Shift then
    begin
        s:=VK_PRIOR;
    end else
    begin
        s:=VK_UP;
    end;
  end;
        FormKeyDown(Sender,s,[]);
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  s: Word;
begin
  case Button of
  mbMiddle:
        Form3.Show;
  mbRight:
  begin
        s:=VK_RETURN;
        FormKeyDown(Sender,s,[]);
  end;
  end;
end;

procedure TForm1.N25Click(Sender: TObject);
begin
        N25.Checked:=not N25.Checked;
end;

procedure TForm1.N27Click(Sender: TObject);
var
  s: string;
begin
  if Sender = N26 then
  begin
        s:=ExtractFilePath(Application.ExeName)+'Project6.exe';
  end else
        if Sender = N28 then
  begin
        s:=ExtractFilePath(Application.ExeName)+'Project4.exe';
  end;
        ShellExecute(Handle,nil,PChar(s),nil,nil,SW_NORMAL);
end;

procedure TForm1.N29Click(Sender: TObject);
begin
        Showmessage(ExtractFileName(Table1.TableName));
end;

procedure TForm1.Table1BeforeOpen(DataSet: TDataSet);
begin
  if (FileExists(Table1.TableName) = false)and(Table1.Exists = false) then
  begin
        Table1.CreateTable;
  end;
end;

procedure TForm1.Table1BeforeClose(DataSet: TDataSet);
begin
        Form2.ListBox1.Items.SaveToFile(ChangeFileExt(Table1.TableName,'.txt'));
        Table1.Filter:='';
end;

procedure TForm1.N30Click(Sender: TObject);
begin
        Form6.ShowModal;
end;

procedure TForm1.N31Click(Sender: TObject);
var
  i, j: integer;
begin
  if SaveDialog1.Execute = true then
  begin
    if SaveDialog1.FileName = Table1.TableName then
    begin
        Showmessage('使用中です:'+Table1.TableName);
        Exit;
    end;
        Table2.TableName:=SaveDialog1.FileName;
        Table2.CreateTable;
        Table2.Open;
        i:=Table1.RecNo;
        Table1.Filter:='';
        Form10.ProgressBar1.Max:=Table1.RecordCount;
        Form10.ProgressBar1.Position:=0;
        Form10.Show;
        Table1.First;
    while Table1.Eof = false do
    begin
      if ((Sender = N31)and(Form2.ListBox1.Items.IndexOf(Table1.FieldByName('Word').AsString) > -1))or
        ((Sender = N27)and(Form2.ListBox1.Items.IndexOf(Table1.FieldByName('Word').AsString) = -1)) then
      begin
                Table2.Append;
        for j:=0 to Table1.FieldCount-1 do
        begin
                Table2.Fields[j]:=Table1.Fields[j];
        end;
      end;
        Table1.Next;
        Form10.ProgressBar1.Position:=Form10.ProgressBar1.Position+1;
    end;
        Form3.ComboBox2Change(nil);
        Table1.RecNo:=i;
        Table2.Close;
        Form10.Hide;
  end;
end;

end.
