{*************************************************************************
                Copyright (c) PilotLogic Software House

  Package pl_ORCA
  This file is part of CodeTyphon Studio (https://www.pilotlogic.com/)

 ***** BEGIN LICENSE BLOCK *****
 * Version: LGPL-2.1 with link exception (Modified LGPL)
 *
 * The contents of this file are subject to the 
 * GNU LIBRARY GENERAL PUBLIC LICENSE Version 2.1 (the "License")
 * with the following modification:
 * 
 * As a special exception, the copyright holders of this library give you
 * permission to link this library with independent modules to produce an
 * executable, regardless of the license terms of these independent modules,
 * and to copy and distribute the resulting executable under terms of your choice,
 * provided that you also meet, for each linked independent module, the terms
 * and conditions of the license of that module. An independent module is a
 * module which is not derived from or based on this library. If you modify this
 * library, you may extend this exception to your version of the library, but
 * you are not obligated to do so. If you do not wish to do so, delete this
 * exception statement from your version.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * ***** END LICENSE BLOCK *****
 
***************************************************************************}

Unit orca_editor_lang;

{$mode objfpc}{.$H+}

Interface

Uses
   LCLProc, LCLIntf, LCLType, LMessages, LResources, LazUtf8,
   Math, typinfo, IntfGraphics,
   Classes, Variants, SysUtils, Contnrs, Forms, Controls, Dialogs, Graphics,
   StdCtrls, DB, DBCtrls, ExtCtrls, Menus, Clipbrd, ActnList, ImgList,
   orca_scene2d;

Type

   TD2LangDesigner = Class(TForm)
      d2Scene1: TD2Scene;
      Root1: TD2HudWindow;
      d2Resources1: TD2Resources;
      cnPrimaryList: TD2HudListBox;
      btnAddStrItem: TD2HudCornerButton;
      langList: TD2HudPopupBox;
      ToolBar1: TD2ToolBar;
      inputLang: TD2HudTextBox;
      HudLabel1: TD2HudLabel;
      layoutSelect: TD2Layout;
      HudLabel2: TD2HudLabel;
      btnAddNewLang: TD2HudButton;
      btnCancalAdd: TD2HudButton;
      layoutAdd: TD2Layout;
      layoutAddText: TD2Layout;
      btnAddText: TD2HudButton;
      btnCancelAddText: TD2HudButton;
      inputAddText: TD2HudTextBox;
      btnRemoveStrItem: TD2HudCornerButton;
      btnScanStrs: TD2HudCornerButton;
      SaveDialog1: TSaveDialog;
      OpenDialog1: TOpenDialog;
      btnAddLang: TD2HudButton;
      btnLoadTxt: TD2HudButton;
      btnCreateTextFile: TD2HudCornerButton;
      btnLoadLngFile: TD2HudCornerButton;
      btnSaveLngFile: TD2HudCornerButton;
      OpenDialog2: TOpenDialog;
      SaveDialog2: TSaveDialog;
      Procedure btnAddClick(Sender: TObject);
      Procedure btnAddLangClick(Sender: TObject);
      Procedure langListChange(Sender: TObject);
      Procedure btnAddStrItemClick(Sender: TObject);
      Procedure btnRemoveStrItemClick(Sender: TObject);
      Procedure btnAddNewLangClick(Sender: TObject);
      Procedure btnCancalAddClick(Sender: TObject);
      Procedure btnCancelAddTextClick(Sender: TObject);
      Procedure btnAddTextClick(Sender: TObject);
      Procedure btnScanStrsClick(Sender: TObject);
      Procedure btnCreateTextFileClick(Sender: TObject);
      Procedure btnLoadTxtClick(Sender: TObject);
      Procedure btnLoadLngFileClick(Sender: TObject);
      Procedure btnSaveLngFileClick(Sender: TObject);
   Private
      FLang: TD2Lang;
      FCurLang: WideString;
      Procedure RebuildPrimaryList;
      Procedure DolateChanged(Sender: TObject);
   End;

Procedure ShowDsgnLang(Lang: TD2Lang);

Var
   GvarD2LangDesigner: TD2LangDesigner;

Implementation

{$R *.lfm}

//=======================================================================

Procedure ShowDsgnLang(Lang: TD2Lang);
Begin
   GvarD2LangDesigner := TD2LangDesigner.Create(Application);

   With GvarD2LangDesigner Do
   Begin
      FLang := Lang;
      langList.Items.Assign(Lang.Resources);

      If langList.Items.Count > 0 Then
         langList.ItemIndex := Lang.Resources.IndexOf(Lang.Lang);
      layoutAdd.Visible := langList.Items.Count = 0;
      layoutSelect.Visible := langList.Items.Count > 0;
      RebuildPrimaryList;

      If ShowModal = mrOk Then
         FLang.Lang := langList.Text;
   End;

   GvarD2LangDesigner.Free;
End;
//==================== TD2LangDesigner ==================================

Procedure TD2LangDesigner.DolateChanged(Sender: TObject);
Begin
   If (FLang.LangStr[langList.TextW] <> nil) And (cnPrimaryList.Selected <> nil) Then
      With FLang.LangStr[langList.TextW] Do
      Begin
         Values[cnPrimaryList.Selected.TextW] := TD2TextBox(Sender).TextW;
      End;
End;

Procedure TD2LangDesigner.RebuildPrimaryList;
Var
   i: Integer;
   Str: TD2WideStrings;
   Item: TD2ListboxItem;
Begin
   cnPrimaryList.Clear;
   If FLang.Original.Count = 0 Then
   Begin
      CollectLangStart;
      UpdateLang;
      FLang.Original.Assign(CollectLangStrings);
      CollectLangFinish;
   End;
   Str := FLang.Original;
   For i := 0 To Str.Count - 1 Do
   Begin
      Item := TD2ListboxItem.Create(Self);
      Item.AutoTranslate := False;
      Item.Resource := 'langitem';
      Item.TextW := Str[i];
      Item.TextAlign := d2TextAlignCenter;
      Item.Height := 22;
      Item.Parent := cnPrimaryList;
      If (FLang.Resources.Count > 0) And (langList.ItemIndex >= 0) Then
      Begin
         If FLang.LangStr[langList.TextW] <> nil Then
         Begin
            Item.Binding['late'] := FLang.LangStr[langList.TextW].Values[Str[i]];
            Item.Binding['late'] := EventToVariant(@DolateChanged);
         End;
      End Else Begin
         If Item.FindBinding('late') <> nil Then
            Item.FindBinding('late').Visual.Visible := False;
      End;
   End;
End;

Procedure TD2LangDesigner.btnAddClick(Sender: TObject);
Var
   xList: TD2ListBox;
Begin
   xList := TD2ListBox.Create(Self);
End;

Procedure TD2LangDesigner.btnAddNewLangClick(Sender: TObject);
Begin
   layoutAdd.Visible := True;
   layoutSelect.Visible := False;
   btnCancalAdd.Visible := langList.Items.Count > 0;

   inputLang.TextW := '';
   inputLang.SetFocus;
End;

Procedure TD2LangDesigner.btnAddLangClick(Sender: TObject);
Var
   S: String;
Begin
   If inputLang.TextW = '' Then
      Exit;
   S := inputLang.TextW;
   If Length(S) > 2 Then
      Delete(S, 3, MaxInt);
   FLang.AddLang(S);
   langList.Items := FLang.Resources;
   langList.ItemIndex := langList.Items.IndexOf(S);
   RebuildPrimaryList;

   layoutAdd.Visible := False;
   layoutSelect.Visible := True;
End;

Procedure TD2LangDesigner.btnCancalAddClick(Sender: TObject);
Begin
   If langList.Items.Count > 0 Then
   Begin
      layoutAdd.Visible := False;
      layoutSelect.Visible := True;
   End;
End;

Procedure TD2LangDesigner.langListChange(Sender: TObject);
Begin
   RebuildPrimaryList;
End;

Procedure TD2LangDesigner.btnAddStrItemClick(Sender: TObject);
Begin
   layoutAdd.Visible := False;
   layoutSelect.Visible := False;
   layoutAddText.Visible := True;
   inputAddText.TextW := '';
   inputAddText.SetFocus;
   RebuildPrimaryList;
End;

Procedure TD2LangDesigner.btnRemoveStrItemClick(Sender: TObject);
Begin
   If cnPrimaryList.ItemIndex >= 0 Then
   Begin
      FLang.Original.Delete(cnPrimaryList.ItemIndex);
      RebuildPrimaryList;
   End;
End;

Procedure TD2LangDesigner.btnLoadLngFileClick(Sender: TObject);
Begin
   If OpenDialog2.Execute Then
   Begin
      FLang.LoadFromFile(OpenDialog2.FileName);
      RebuildPrimaryList;
   End;
End;

Procedure TD2LangDesigner.btnCreateTextFileClick(Sender: TObject);
Var
   Str: TD2WideStrings;
   i: Integer;
Begin
   If SaveDialog1.Execute Then
   Begin
      Str := TD2WideStringList.Create;
      Str.Assign(FLang.Original);
      For i := 0 To Str.Count - 1 Do
         Str[i] := Str[i] + '=';
      Str.SaveToFile(SaveDialog1.FileName);
      Str.Free;
   End;
End;

Procedure TD2LangDesigner.btnLoadTxtClick(Sender: TObject);
Var
   Str: TD2WideStrings;
   i: Integer;
Begin
   If OpenDialog1.Execute Then
   Begin
      FLang.AddLang(inputLang.TextW);
      langList.Items := FLang.Resources;
      langList.ItemIndex := langList.Items.IndexOf(inputLang.TextW);
      RebuildPrimaryList;

      layoutAdd.Visible := False;
      layoutSelect.Visible := True;

      Str := TD2WideStringList.Create;
      Str.LoadFromFile(OpenDialog1.FileName);
      For i := 0 To Str.Count - 1 Do
         If FLang.LangStr[langList.TextW].IndexOfName(Str.Names[i]) < 0 Then
            FLang.LangStr[langList.TextW].Add(Str[i])
         Else
            FLang.LangStr[langList.TextW].Values[Str.Names[i]] := Str.Values[Str.Names[i]];
      Str.Free;
      RebuildPrimaryList;
   End;
End;

Procedure TD2LangDesigner.btnSaveLngFileClick(Sender: TObject);
Begin
   If SaveDialog2.Execute Then
   Begin
      FLang.SaveToFile(SaveDialog2.FileName);
   End;
End;

Procedure TD2LangDesigner.btnCancelAddTextClick(Sender: TObject);
Begin
   layoutAdd.Visible := langList.Items.Count = 0;
   layoutSelect.Visible := langList.Items.Count > 0;
   layoutAddText.Visible := False;
End;

Procedure TD2LangDesigner.btnAddTextClick(Sender: TObject);
Begin
   btnCancelAddTextClick(Sender);
   FLang.Original.Add(inputAddText.TextW);
   RebuildPrimaryList;
   cnPrimaryList.ItemIndex := cnPrimaryList.Count - 1;
End;

Procedure TD2LangDesigner.btnScanStrsClick(Sender: TObject);
Var
   Str: TD2WideStrings;
   i: Integer;
Begin
   CollectLangStart;
   UpdateLang;
   Str := TD2WideStringList.Create;
   Str.Assign(CollectLangStrings);
   For i := 0 To Str.Count - 1 Do
      If FLang.Original.IndexOf(Str[i]) < 0 Then
         FLang.Original.Add(Str[i]);
   Str.Free;
   CollectLangFinish;
   RebuildPrimaryList;
End;

End.
