unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtrls, HTTSLib_TLB, ExtCtrls, Menus, DB, DBTables, StdCtrls,
  Mask, DBCtrls, AppEvnts;

type
  TShowMode = (shWord,shMean);
  
  TForm1 = class(TForm)
    Panel1: TPanel;
    MainMenu1: TMainMenu;
    DataSource1: TDataSource;
    DBEdit1: TDBEdit;
    DBEdit2: TDBEdit;
    Table1: TTable;
    N1: TMenuItem;
    Timer1: TTimer;
    TextToSpeech1: TTextToSpeech;
    OpenDialog1: TOpenDialog;
    Open1: TMenuItem;
    N2: TMenuItem;
    Exit1: TMenuItem;
    Help1: TMenuItem;
    About1: TMenuItem;
    N3: TMenuItem;
    ApplicationEvents1: TApplicationEvents;
    procedure Timer1Timer(Sender: TObject);
    procedure Panel1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Panel1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormMouseWheel(Sender: TObject; Shift: TShiftState;
      WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
    procedure Open1Click(Sender: TObject);
    procedure Exit1Click(Sender: TObject);
    procedure About1Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure ApplicationEvents1Deactivate(Sender: TObject);
    procedure ApplicationEvents1Activate(Sender: TObject);
  private
    { Private 宣言 }
    Show: TShowMode;
    Count: integer;
  public
    { Public 宣言 }
  end;

var
  Form1: TForm1;

implementation

uses Unit2, IniFiles;

{$R *.dfm}

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if Table1.Active = false then
  begin
        Exit;
  end;
  if Count > 10 then
  begin
        Timer1.Enabled:=false;
  end;
  case Show of
  shWord:
  begin
        Panel1.Caption:=DBEdit1.Text;
    if DBEdit1.Text <> '' then
    begin
        TextToSpeech1.Speak(DBEdit1.Text);
    end;
        Show:=shMean;
  end;
  shMean:
  begin
        Panel1.Caption:=DBEdit2.Text;
        Show:=shWord;
        inc(Count);
  end;
  end;
end;

procedure TForm1.Panel1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
        Count:=0;
        Timer1.Enabled:=true;
end;

procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
        Timer1.Enabled:=false;
end;

procedure TForm1.FormMouseWheel(Sender: TObject; Shift: TShiftState;
  WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
begin
  if Table1.Active = false then
  begin
        Exit;
  end;
        Timer1.Enabled:=false;
  if WheelDelta < 0 then
  begin
        Table1.Next;
  end else
  begin
        Table1.Prior;
  end;
        Count:=0;
        Panel1.Caption:=DBEdit1.Text;
        Timer1.Enabled:=true;
end;

procedure TForm1.Open1Click(Sender: TObject);
begin
  if OpenDialog1.Execute = true then
  begin
        Table1.Close;
    try
        Table1.TableName:=OpenDialog1.FileName;
    finally
        Table1.Open;
    end;
  end;
end;

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

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

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

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
  s: TIniFile;
begin
        s:=TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));
  try
        s.WriteString('database','tablename',Table1.TableName);
  finally
        s.Free;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  s: TIniFile;
  t: string;
begin
        s:=TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));
        Table1.Close;
  try
        t:=s.ReadString('database','tablename','');
  finally
        s.Free;
  end;
  if FileExists(t) = true then
  begin
        Table1.TableName:=t;
        Table1.Open;
  end else
  begin
        Showmessage('.DBファイルが見つかりません');
  end;
end;

procedure TForm1.ApplicationEvents1Deactivate(Sender: TObject);
begin
        Timer1.Enabled:=false;
end;

procedure TForm1.ApplicationEvents1Activate(Sender: TObject);
begin
  if Timer1.Enabled = false then
  begin
        Timer1.Enabled:=true;
  end;
end;

end.
