home *** CD-ROM | disk | FTP | other *** search
/ Freelog 11 / Freelog011.iso / BestOf / PhoenixMail / Source / langkit / Main.pas < prev    next >
Pascal/Delphi Source File  |  1999-02-10  |  15KB  |  516 lines

  1. {*****************************************************************************
  2.  *
  3.  *  Main.pas - Phoenix Mail Language Kit MainForm
  4.  *
  5.  *  Copyright (c) 1998-99 Michael Haller
  6.  *
  7.  *  Author:     Michael Haller
  8.  *  E-mail:     michael@discountdrive.com
  9.  *  Homepage:   http://www.discountdrive.com/sunrise
  10.  *
  11.  *  This program is free software; you can redistribute it and/or
  12.  *  modify it under the terms of the GNU General Public License
  13.  *  as published by the Free Software Foundation;
  14.  *
  15.  *  This program is distributed in the hope that it will be useful,
  16.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  *  GNU General Public License for more details.
  19.  *
  20.  *  You should have received a copy of the GNU General Public License
  21.  *  along with this program; if not, write to the Free Software
  22.  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
  23.  *
  24.  *----------------------------------------------------------------------------
  25.  *
  26.  *  Revision history:
  27.  *
  28.  *     DATE     REV                 DESCRIPTION
  29.  *  ----------- --- ----------------------------------------------------------
  30.  *
  31.  *****************************************************************************}
  32.  
  33. unit Main;
  34.  
  35. interface
  36.  
  37. uses
  38.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  39.   StdCtrls, Grids, ImgList, Menus, Buttons, Registry, ExtCtrls, ComCtrls,
  40.   ShellAPI;
  41.  
  42. type
  43.   TMainForm = class(TForm)
  44.     ListBox1: TListBox;
  45.     StatusBar1: TStatusBar;
  46.     Panel1: TPanel;
  47.     Bevel1: TBevel;
  48.     SpeedButton1: TSpeedButton;
  49.     OpenDialog1: TOpenDialog;
  50.     SaveDialog1: TSaveDialog;
  51.     SpeedButton2: TSpeedButton;
  52.     SpeedButton3: TSpeedButton;
  53.     SpeedButton4: TSpeedButton;
  54.     SpeedButton6: TSpeedButton;
  55.     MainMenu1: TMainMenu;
  56.     File1: TMenuItem;
  57.     New1: TMenuItem;
  58.     Open1: TMenuItem;
  59.     Save1: TMenuItem;
  60.     SaveAs1: TMenuItem;
  61.     N1: TMenuItem;
  62.     Exit1: TMenuItem;
  63.     Help1: TMenuItem;
  64.     Readme1: TMenuItem;
  65.     SpeedButton7: TSpeedButton;
  66.     SpeedButton8: TSpeedButton;
  67.     FindDialog1: TFindDialog;
  68.     Edit1: TMenuItem;
  69.     Search1: TMenuItem;
  70.     PageControl1: TPageControl;
  71.     TabSheet1: TTabSheet;
  72.     StringGrid1: TStringGrid;
  73.     TabSheet4: TTabSheet;
  74.     ScrollBox1: TScrollBox;
  75.     GroupBox1: TGroupBox;
  76.     Label3: TLabel;
  77.     Label4: TLabel;
  78.     Label5: TLabel;
  79.     Edit2: TEdit;
  80.     Label6: TLabel;
  81.     Edit3: TEdit;
  82.     GroupBox2: TGroupBox;
  83.     Label7: TLabel;
  84.     Edit4: TEdit;
  85.     Label8: TLabel;
  86.     Edit5: TEdit;
  87.     TabSheet5: TTabSheet;
  88.     Memo5: TMemo;
  89.     procedure FormCreate(Sender: TObject);
  90.     procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
  91.       var CanSelect: Boolean);
  92.     procedure SpeedButton1Click(Sender: TObject);
  93.     procedure SpeedButton2Click(Sender: TObject);
  94.     procedure SpeedButton3Click(Sender: TObject);
  95.     procedure StringGrid1SetEditText(Sender: TObject; ACol, ARow: Integer;
  96.       const Value: String);
  97.     procedure SpeedButton4Click(Sender: TObject);
  98.     procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  99.     procedure SpeedButton6Click(Sender: TObject);
  100.     procedure SpeedButton7Click(Sender: TObject);
  101.     procedure Search1Click(Sender: TObject);
  102.     procedure FindDialog1Find(Sender: TObject);
  103.   private
  104.     { Private declarations }
  105.     FFilename: String;
  106.     procedure SetFilename(Value: String);
  107.   public
  108.     { Public declarations }
  109.     Changed: Boolean;
  110.     SearchIndex: Integer;
  111.     function Confirmation: Boolean;
  112.     property Filename: String read FFilename write SetFilename;
  113.     function NewFile: Boolean;
  114.     procedure LoadFile;
  115.     function SaveFile: Boolean;
  116.     function SaveFileAs: Boolean;
  117.     function InteractWithPhoenix: Boolean;
  118.   end;
  119.  
  120. type
  121.   TLangFileHeader = record
  122.     IDStr: String[30];
  123.     Version: Byte;
  124.     Author: String[40];
  125.     Comment: String[50];
  126.     Created: String[10];
  127.     NatName: String[20];
  128.     EngName: String[20];
  129.     LongStrCount: Byte;
  130.   end;
  131.  
  132.   TLongStrEntry = record
  133.     Len: SmallInt;
  134.   end;
  135.  
  136.   TLangEntry = record
  137.     ID: SmallInt;
  138.     SubID: Byte;
  139.     Len: Byte;
  140.   end;
  141.  
  142. const
  143.   WM_GETFORMHANDLES = WM_USER + 523;
  144.   sPXAppTitle = 'Phoenix Mail 0.92';
  145.   sRegKey = 'Software\Michael Haller\Phoenix Mail';
  146.   sVersion = '0.92';
  147.  
  148.   sNonameFile = 'Noname.lng';
  149.   sLangCopyrightStr: String[30] = 'Phoenix Mail Language File';
  150.  
  151. var
  152.   MainForm: TMainForm;
  153.   sTempLanguageFile: String;
  154.   sLangFolder: String;
  155.   sReadmeFile: String;
  156.  
  157. implementation
  158.  
  159. {$R *.DFM}
  160. {$R pxlang.res}
  161.  
  162. var
  163.   T: Text;
  164.  
  165. function MakeValidDirName(Dir: String): String;
  166. begin
  167.   if Dir[Length(Dir)] <> '\' then Dir := Dir+'\';
  168.   Result := Dir;
  169. end;
  170.  
  171. function TMainForm.Confirmation: Boolean;
  172. begin
  173.   Result := True;
  174.   if Changed = False then Exit;
  175.   Result := False;
  176.   Changed := False;
  177.   case MessageDlg('Save changes to '+ExtractFilename(Filename)+'?', mtConfirmation, [mbYes,mbNo,mbCancel], 0) of
  178.     mrYes: if SaveFile then Result := True else Changed := True;
  179.     mrNo: Result := True;
  180.     mrCancel: Changed := True;
  181.   end;
  182. end;
  183.  
  184. procedure TMainForm.SetFilename(Value: String);
  185. begin
  186.   FFilename := Value;
  187.   Caption := Application.Title + ' - '+ExtractFilename(FFilename);
  188. end;
  189.  
  190. function TMainForm.InteractWithPhoenix: Boolean;
  191. var
  192.   H: HWnd;
  193.   I: Integer;
  194.   S: String;
  195.   NativeReg: TRegistry;
  196. begin
  197.   Result := False;
  198.   try
  199.     try
  200.     NativeReg := TRegistry.Create;
  201.     NativeReg.OpenKey(sRegKey, True);
  202.     sTempLanguageFile := NativeReg.ReadString('Path')+'templang.tmp';
  203.     sLangFolder :=  NativeReg.ReadString('Path')+'Lang\';
  204.     S := NativeReg.ReadString('Version');
  205.     NativeReg.CloseKey;
  206.     NativeReg.Free;
  207.     if S <> sVersion then begin
  208.       MessageDlg(sPXAppTitle+' is needed.', mtError, [mbOK], 0);
  209.       Exit;
  210.     end;
  211.     H := FindWindow(nil, PChar(sPXAppTitle));
  212.     if H = 0 then begin
  213.       MessageDlg(sPXAppTitle+' must be running.', mtError, [mbOK], 0);
  214.       Exit;
  215.     end;
  216.     Screen.Cursor := crHourGlass;
  217.     DeleteFile(sTempLanguageFile);
  218.     SendMessage(H, WM_GETFORMHANDLES, Handle, 0);
  219.     for I := 0 to 4 do Application.HandleMessage;
  220.     if FileExists(sTempLanguageFile) = False then begin
  221.       MessageDlg(sPXAppTitle+' must be running.', mtError, [mbOK], 0);
  222.       Screen.Cursor := crDefault;
  223.       Exit;
  224.     end;
  225.     AssignFile(T, sTempLanguageFile);
  226.     Reset(T);
  227.  
  228.     ReadLn(T, S);
  229.     for I := 1 to StrToInt(S) do begin
  230.       ReadLn(T, S);
  231.       //Memo1.Lines.Add(S);
  232.     end;
  233.     ReadLn(T, S);
  234.     for I := 1 to StrToInt(S) do begin
  235.       ReadLn(T, S);
  236.       //Memo3.Lines.Add(S);
  237.     end;
  238.  
  239.     I := 1;
  240.     StringGrid1.RowCount := 2;
  241.     while not EoF(T) do begin
  242.       StringGrid1.RowCount := StringGrid1.RowCount+1;
  243.       ReadLn(T, S);
  244.       StringGrid1.Cells[1,I] := S;
  245.       ReadLn(T, S);
  246.       ListBox1.Items.Add(S);
  247.       ReadLn(T, S);
  248.       StringGrid1.Cells[0,I] := S;
  249.       Inc(I);
  250.     end;
  251.     StringGrid1.RowCount := StringGrid1.RowCount-1;
  252.     StringGrid1.Row := 1;
  253.     StringGrid1.Col := 2;
  254.     StringGrid1.Cells[0,0] := 'ID/Property/Component';
  255.     StringGrid1.Cells[1,0] := 'Original Text';
  256.     StringGrid1.Cells[2,0] := 'Translated Language';
  257.     OpenDialog1.InitialDir := sLangFolder;
  258.     SaveDialog1.InitialDir := sLangFolder;
  259.     Result := True;
  260.     finally
  261.       Screen.Cursor := crDefault;
  262.       try
  263.         CloseFile(T);
  264.       except end;
  265.       DeleteFile(sTempLanguageFile);
  266.       Changed := False;
  267.     end;
  268.   except end;
  269. end;
  270.  
  271. procedure TMainForm.FormCreate(Sender: TObject);
  272. begin
  273.   Icon := Application.Icon;
  274.   StringGrid1.ColWidths[0] := 140;
  275.   sReadmeFile := MakeValidDirName(ExtractFilePath(Application.ExeName))+'LangKit.txt';
  276.   Filename := sNonameFile;
  277.   Label5.Caption := DateToStr(Date);
  278.   Changed := False;
  279. end;
  280.  
  281. function TMainForm.NewFile: Boolean;
  282. var
  283.   I: Integer;
  284. begin
  285.   Result := False;
  286.   if Confirmation = False then Exit;
  287.   Filename := sNonameFile;
  288.   for I := 1 to StringGrid1.RowCount-1 do
  289.     StringGrid1.Cells[2,I] := '';
  290.   //Memo2.Lines.Clear;
  291.   //Memo4.Lines.Clear;
  292.   Changed := False;
  293.   Result := True;
  294. end;
  295.  
  296. function TMainForm.SaveFileAs: Boolean;
  297. begin
  298.   Result := False;
  299.   if SaveDialog1.Execute then begin
  300.     Filename := SaveDialog1.Filename;
  301.     Result := SaveFile;
  302.   end;
  303. end;
  304.  
  305. function TMainForm.SaveFile: Boolean;
  306. var
  307.   I, E: Integer;
  308.   P: PChar;
  309.   LangEntry: TLangEntry;
  310.   LongStrEntry: TLongStrEntry;
  311.   LangFileHeader: TLangFileHeader;
  312.   F: File;
  313.   S: String[255];
  314.   LongS: String;
  315. begin
  316.   Result := False;
  317.   if Filename = sNonameFile then begin
  318.     if SaveDialog1.Execute then begin
  319.       Filename := SaveDialog1.Filename;
  320.     end else
  321.       Exit;
  322.   end;
  323.   AssignFile(F, Filename);
  324.   Rewrite(F, 1);
  325.   FillChar(LangFileHeader, SizeOf(TLangFileHeader), 0);
  326.   with LangFileHeader do begin
  327.     IDStr := sLangCopyrightStr;
  328.     Version := 1;
  329.     Author := Edit2.Text;
  330.     Comment := Edit3.Text;
  331.     Created := Label5.Caption;
  332.     NatName := Edit4.Text;
  333.     EngName := Edit5.Text;
  334.     LongStrCount := 2;
  335.   end;
  336.   BlockWrite(F, LangFileHeader, SizeOf(LangFileHeader));
  337.   for E := 1 to 2 do begin
  338.     LongS := '';
  339.     {[if E = 1 then
  340.       for I := 0 to Memo2.Lines.Count-1 do
  341.         LongS := LongS + Memo2.Lines[I];
  342.     if E = 2 then
  343.       for I := 0 to Memo4.Lines.Count-1 do
  344.         LongS := LongS + Memo4.Lines[I];  }
  345.     GetMem(P, Length(LongS)+1);
  346.     StrCopy(P, PChar(LongS));
  347.     LongStrEntry.Len := Length(LongS);
  348.     BlockWrite(F, LongStrEntry, SizeOf(TLongStrEntry));
  349.     BlockWrite(F, P^, LongStrEntry.Len);
  350.     FreeMem(P, Length(LongS)+1);
  351.   end;
  352.   for I := 0 to ListBox1.Items.Count-1 do begin
  353.     if ListBox1.Items[I] <> '' then begin
  354.       S := StringGrid1.Cells[2,I+1];
  355.       with LangEntry do begin
  356.         E := Pos('.', ListBox1.Items[I]);
  357.         if E > 0 then begin
  358.           ID := StrToInt(Copy(ListBox1.Items[I], 1, E-1));
  359.           SubID := StrToInt(Copy(ListBox1.Items[I], E+1, Length(ListBox1.Items[I])-E+1));
  360.         end else begin
  361.           ID := StrToInt(ListBox1.Items[I]);
  362.           SubID := 0;
  363.         end;
  364.         Len := Length(S);
  365.       end;
  366.       BlockWrite(F, LangEntry, SizeOf(LangEntry));
  367.       BlockWrite(F, S[1], LangEntry.Len);
  368.     end;
  369.   end;
  370.   CloseFile(F);
  371.   Changed := False;
  372.   Result := True;
  373. end;
  374.  
  375. procedure TMainForm.LoadFile;
  376. var
  377.   I, E: Integer;
  378.   LangEntry: TLangEntry;
  379.   LangFileHeader: TLangFileHeader;
  380.   LongStrEntry: TLongStrEntry;
  381.   P: PChar;
  382.   F: File;
  383.   LongS: String;
  384.   S: String[255];
  385. begin
  386.   if Confirmation = False then Exit;
  387.   try
  388.     try
  389.       if OpenDialog1.Execute then begin
  390.         Screen.Cursor := crHourGlass;
  391.         Filename := OpenDialog1.FileName;
  392.         AssignFile(F, Filename);
  393.         Reset(F, 1);
  394.         //BlockRead(F, S, SizeOf(sLangCopyrightStr));
  395.         BlockRead(F, LangFileHeader, SizeOf(LangFileHeader));
  396.         if LangFileHeader.IDStr <> sLangCopyrightStr then
  397.            raise Exception.Create('Invalid Language File.');
  398.         if LangFileHeader.Version <> 1 then
  399.            raise Exception.Create('Invalid version of Language File.');
  400.         Edit2.Text := LangFileHeader.Author;
  401.         Edit3.Text := LangFileHeader.Comment;
  402.         Edit4.Text := LangFileHeader.NatName;
  403.         Edit5.Text := LangFileHeader.EngName;
  404.         Label5.Caption := LangFileHeader.Created;
  405.         for I := 1 to LangFileHeader.LongStrCount do begin
  406.           BlockRead(F, LongStrEntry, SizeOf(TLongStrEntry));
  407.           GetMem(P, LongStrEntry.Len+1);
  408.           BlockRead(F, P^, LongStrEntry.Len);
  409.           P[LongStrEntry.Len] := #0;
  410.           LongS := P;
  411.           FreeMem(P, LongStrEntry.Len+1);
  412.           {if I = 1 then
  413.             Memo2.Lines.Add(LongS);
  414.           if I = 2 then
  415.             Memo4.Lines.Add(LongS); }
  416.         end;
  417.         //
  418.         while not Eof(F) do begin
  419.           BlockRead(F, LangEntry, SizeOf(LangEntry));
  420.           BlockRead(F, S[1], LangEntry.Len);
  421.           SetLength(S, LangEntry.Len);
  422.           if LangEntry.SubID <> 0 then
  423.             E := ListBox1.Items.IndexOf(IntToStr(LangEntry.ID)+'.'+IntToStr(LangEntry.SubID))
  424.           else
  425.             E := ListBox1.Items.IndexOf(IntToStr(LangEntry.ID));
  426.           StringGrid1.Cells[2,E+1] := S;
  427.         end;
  428.       end;
  429.     finally
  430.       Screen.Cursor := crDefault;
  431.       Changed := False;
  432.       try
  433.         CloseFile(F);
  434.       except end;
  435.     end;
  436.   except
  437.     on Exception do MessageDlg('Invalid Language File.', mtError, [mbOK], 0);
  438.   end;
  439. end;
  440.  
  441. procedure TMainForm.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
  442. begin
  443.   //if ACol in [0,1] then CanSelect := False else CanSelect := True;
  444. end;
  445.  
  446. procedure TMainForm.SpeedButton1Click(Sender: TObject);
  447. begin
  448.   if NewFile = False then Exit;
  449.   LoadFile;
  450. end;
  451.  
  452. procedure TMainForm.SpeedButton2Click(Sender: TObject);
  453. begin
  454.   SaveFile;
  455. end;
  456.  
  457. procedure TMainForm.SpeedButton3Click(Sender: TObject);
  458. begin
  459.   NewFile;
  460. end;
  461.  
  462. procedure TMainForm.StringGrid1SetEditText(Sender: TObject; ACol, ARow: Integer; const Value: String);
  463. begin
  464.   Changed := True;
  465. end;
  466.  
  467. procedure TMainForm.SpeedButton4Click(Sender: TObject);
  468. begin
  469.   SaveFileAs;
  470. end;
  471.  
  472. procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  473. begin
  474.   if Confirmation = False then CanClose := False else CanClose := True;
  475. end;
  476.  
  477. procedure TMainForm.SpeedButton6Click(Sender: TObject);
  478. begin
  479.   Close;
  480. end;
  481.  
  482. procedure TMainForm.SpeedButton7Click(Sender: TObject);
  483. begin
  484.  if FileExists(sReadmeFile) then
  485.     ShellExecute(Handle, 'open', PChar(sReadmeFile), '', PChar(ExtractFilePath(sReadmeFile)), SW_SHOWNORMAL);
  486. end;
  487.  
  488. procedure TMainForm.Search1Click(Sender: TObject);
  489. begin
  490.   PageControl1.ActivePage := TabSheet1;
  491.   SearchIndex := 1;
  492.   FindDialog1.Execute;
  493. end;
  494.  
  495. procedure TMainForm.FindDialog1Find(Sender: TObject);
  496. var
  497.  I: Integer;
  498.  S: String;
  499. begin
  500.   if SearchIndex > StringGrid1.RowCount-1 then Exit;
  501.   for I := SearchIndex to StringGrid1.RowCount-1 do begin
  502.     Inc(SearchIndex);
  503.     S := LowerCase(StringGrid1.Cells[0,I]+' '+StringGrid1.Cells[1,I]+' '+StringGrid1.Cells[2,I]);
  504.     if Pos(LowerCase(FindDialog1.FindText), S) <> 0 then begin
  505.       StringGrid1.Row := I;
  506.       StringGrid1.SetFocus;
  507.       SetActiveWindow(StringGrid1.Handle);
  508.       SearchIndex := I+1;
  509.       Exit;
  510.     end;
  511.   end;
  512.   MessageDlg('Search string '''+FindDialog1.FindText+''' not found!', mtInformation, [mbOK], 0);
  513. end;
  514.  
  515. end.
  516.