{Copyright or © or Copr. :

CNRS - Laboratoire Mouvement & Perception
franck.buloup@univmed.fr

This software is a computer program whose purpose is to highly simplify  
the use of ADwinPro realtime systems when you develop experimental
protocols  on life's sciences area.

This software is governed by the CeCILL  license under French law and
abiding by the rules of distribution of free software.  You can  use, 
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info". 

As a counterpart to the access to the source code and  rights to copy,
modify and redistribute granted by the license, users are provided only
with a limited warranty  and the software's author,  the holder of the
economic rights,  and the successive licensors  have only  limited
liability. 

In this respect, the user's attention is drawn to the risks associated
with loading,  using,  modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean  that it is complicated to manipulate,  and  that  also
therefore means  that it is reserved for developers  and  experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or 
data to be ensured and,  more generally, to use and operate it in the 
same conditions as regards security. 

The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.}
unit UOptionsConf;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, FileCtrl, Menus, IniFiles;

type
  TOptionsConfForm = class(TForm)
    Panel1: TPanel;
    Shape1: TShape;
    Shape2: TShape;
    RepsGroupBox: TGroupBox;
    RepRacineLE: TLabeledEdit;
    RepFctsLE: TLabeledEdit;
    RepActionsLE: TLabeledEdit;
    RepTraitementsLE: TLabeledEdit;
    RepManipsLE: TLabeledEdit;
    RepFichiersInfLE: TLabeledEdit;
    ADBasicDocoGroupBox: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label6: TLabel;
    RepRacineButton: TButton;
    RepFctsButton: TButton;
    RepActionsButton: TButton;
    RepTraitementsButton: TButton;
    RepFichiersInfButton: TButton;
    RepManipsButton: TButton;
    LevelAppliComboBox: TComboBox;
    LevelRecupGeneComboBox: TComboBox;
    NbProcessComboBox: TComboBox;
    NumProcessComboBox: TComboBox;
    LinkAdressEdit: TEdit;
    TODOGroupBox: TGroupBox;
    TODOMemo: TMemo;
    OpenDialog1: TOpenDialog;
    AppliquerConfigButton: TButton;
    Label10: TLabel;
    SystemCB: TComboBox;
    EnReseauCheckBox: TCheckBox;
    ReseauPanel: TPanel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    ModeCB: TComboBox;
    IPServeurEdit: TEdit;
    PortEdit: TEdit;
    Label5: TLabel;
    LevelPaintComboBox: TComboBox;
    ADBasicLE: TLabeledEdit;
    ADBasicButton: TButton;
    EnregStatCheckBox: TCheckBox;
    AlphaBlendCheckBox: TCheckBox;
    Label11: TLabel;
    adBasicVersionComboBox: TComboBox;
    procedure AppliquerConfigButtonClick(Sender: TObject);
    procedure EnregStatCheckBoxClick(Sender: TObject);
    procedure RepRacineButtonClick(Sender: TObject);
    procedure RepFctsButtonClick(Sender: TObject);
    procedure RepActionsButtonClick(Sender: TObject);
    procedure RepTraitementsButtonClick(Sender: TObject);
    procedure RepFichiersInfButtonClick(Sender: TObject);
    procedure RepManipsButtonClick(Sender: TObject);
    procedure ADBasicButtonClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure RepRacineLEChange(Sender: TObject);
    procedure EnReseauCheckBoxClick(Sender: TObject);
    procedure AlphaBlendCheckBoxClick(Sender: TObject);
  private
    { Déclarations privées }
    procedure MAJIHM;
    procedure configChange;
    procedure ChangeDirectory(Sender : TLabeledEdit);
  public
    { Déclarations publiques }
    procedure SaveConfig;
    procedure Readconfig;
    procedure CreateListeFichiersRecents;
    procedure MAJListeFichiersRecentsReg(Liste : TStringList);
  end;

var
  OptionsConfForm: TOptionsConfForm;

implementation

uses UGlobal,UDocometre;

{$R *.dfm}

procedure TOptionsConfForm.CreateListeFichiersRecents;
var i : integer;
    fichier : string;
    //Reg : TRegistry;
    Inifile : TIniFile;
    //fichierMenu : TMenuItem;
begin
  IniFile := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));
  for i := 1 to 5 do
  begin
    fichier := IniFile.ReadString('DOCO2','Fichier' + inttostr(i),'');
    if fichier <> '' then
    begin
      case i of
        1 : DocoForm.RecentFileAction1.Caption := fichier;
        2 : DocoForm.RecentFileAction2.Caption := fichier;
        3 : DocoForm.RecentFileAction3.Caption := fichier;
        4 : DocoForm.RecentFileAction4.Caption := fichier;
        5 : DocoForm.RecentFileAction5.Caption := fichier;
      end;
    end;
  end;
  Inifile.free;
  {
  Reg := TRegistry.create;
  Reg.rootkey := HKEY_LOCAL_MACHINE;
  if Reg.KeyExists('\Software\DOCOMETRE2Reps') then
  begin
    if Reg.OpenKey('\Software\DOCOMETRE2Reps',false) then
    begin
      //Maj menu fichiers recents
      for i := 1 to 5 do
      begin
        fichier := Reg.ReadString('Fichier' + inttostr(i));
        if fichier <> '' then
        begin
          //fichierMenu := TMenuItem.Create(DocoForm.MainMenu1);
          //fichierMenu.Name := 'Fichier' + inttostr(i);
          //fichierMenu.OnClick := DocoForm.RecentFileActionExecute;
          //fichierMenu.Caption := fichier;
          //DocoForm.RecentFilesMenu.Add(fichierMenu);  
          case i of
            1 : DocoForm.RecentFileAction1.Caption := fichier;
            2 : DocoForm.RecentFileAction2.Caption := fichier;
            3 : DocoForm.RecentFileAction3.Caption := fichier;
            4 : DocoForm.RecentFileAction4.Caption := fichier;
            5 : DocoForm.RecentFileAction5.Caption := fichier;
          end;
        end;
      end;
    end;
  end;
  reg.closeKey;
  reg.free; }
end;

procedure TOptionsConfForm.MAJListeFichiersRecentsReg(Liste : TStringList);
var //Reg : TRegistry;
    Inifile : TIniFile;
    i : integer;
begin
  IniFile := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));
  for i := 0 to 4
  do IniFile.WriteString('DOCO2','Fichier'+inttostr(i+1),Liste[i]);
  Inifile.free;
  {Reg := TRegistry.create;
  Reg.rootkey := HKEY_LOCAL_MACHINE;
  if Reg.KeyExists('\Software\DOCOMETRE2Reps') then
  begin
    if Reg.OpenKey('\Software\DOCOMETRE2Reps',false) then
    begin
      for i := 0 to Liste.Count - 1
      do Reg.WriteString('Fichier'+inttostr(i+1),Liste[i]);
    end;
  end;
  reg.closeKey;
  reg.free;     }
end;

procedure TOptionsConfForm.FormShow(Sender: TObject);
begin
  MAJIHM;
end;

procedure TOptionsConfForm.AppliquerConfigButtonClick(Sender: TObject);
begin
  ConfigChange;
  SaveConfig;
  AppliquerConfigButton.Enabled := false;
end;

procedure TOptionsConfForm.SaveConfig;
var Inifile : TIniFile;
    //Reg : TRegistry;
begin
  IniFile := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));
  IniFile.WriteString('Repertoires','RepRacine',Rep_Racine);
  IniFile.WriteString('Repertoires','RepTraitements',Rep_Traitements);
  IniFile.WriteString('Repertoires','RepActions',Rep_Actions);
  IniFile.WriteString('Repertoires','RepFonctions',Rep_Fonctions);
  IniFile.WriteString('Repertoires','RepFichiersInf',Rep_FichiersInf);
  IniFile.WriteString('Repertoires','RepManips',Rep_manip);
  IniFile.WriteString('Repertoires','RepADBasic',ADBasic);

  IniFile.WriteString('ADBasic','SystemType',SystemType);
  IniFile.WriteInteger('ADBasic','NbProcess',Nb_Process);
  IniFile.WriteInteger('ADBasic','NumProcess',Num_Process);
  IniFile.WriteInteger('ADBasic','LinkAddress',Link_Address);     
  IniFile.WriteInteger('ADBasic','adBasicVersion',adBasicVersion);

  IniFile.WriteString('DOCO2','LevelAppli',Level_Appli);//Process priority
  IniFile.WriteString('DOCO2','LevelRecupGene',Level_RecupGene);//Thread RecupGene priority
  IniFile.WriteString('DOCO2','LevelAfficher',Level_Affiche);//Thread Affichage priority
  IniFile.WriteBool('DOCO2','EnregStats',Enreg_Stats);
  IniFile.WriteBool('DOCO2','ShowAlphaBlending',Show_Alpha_Blending);
  IniFile.WriteBool('DOCO2','EnReseau',EnReseau);
  IniFile.WriteString('DOCO2','Mode',Mode);
  IniFile.WriteString('DOCO2','IPServeur',IPServeur);
  IniFile.WriteString('DOCO2','Port',Port);
  IniFile.UpdateFile;
  Inifile.free;
  {
  Reg := TRegistry.create;
  Reg.rootkey := HKEY_LOCAL_MACHINE;
  if Reg.OpenKey('\Software\DOCOMETRE2Reps',true) then
  begin
    Reg.WriteString('RepRacine',Rep_Racine);
    Reg.WriteString('RepTraitements',Rep_Traitements);
    Reg.WriteString('RepActions',Rep_Actions);
    Reg.WriteString('RepFonctions',Rep_Fonctions);
    Reg.WriteString('RepFichiersInf',Rep_FichiersInf);
    Reg.WriteString('RepManips',Rep_manip);
    Reg.WriteString('RepADBasic',ADBasic);
    Reg.WriteString('SystemType',SystemType);
    Reg.WriteInteger('NbProcess',Nb_Process);
    Reg.WriteInteger('NumProcess',Num_Process);
    Reg.WriteInteger('LinkAddress',Link_Address);
    Reg.WriteString('LevelAppli',Level_Appli);//Process priority
    Reg.WriteString('LevelRecupGene',Level_RecupGene);//Thread RecupGene priority
    Reg.WriteString('LevelAfficher',Level_Affiche);//Thread Affichage priority
		Reg.WriteBool('EnregStats',Enreg_Stats);
		Reg.WriteBool('ShowAlphaBlending',Show_Alpha_Blending);
    Reg.WriteBool('EnReseau',EnReseau);
    Reg.WriteString('Mode',Mode);
    Reg.WriteString('IPServeur',IPServeur);
    Reg.WriteString('Port',Port);
  end;
  reg.closeKey;
  reg.free;              }
end;

procedure TOptionsConfForm.Readconfig;
begin
	UGlobal.Readconfig;
	CreateListeFichiersRecents;
end;

procedure TOptionsConfForm.MAJIHM;
begin
  RepRacineLE.Text := Rep_Racine;
  RepFctsLE.Text := Rep_Fonctions;
  RepActionsLE.Text := Rep_Actions;
  RepTraitementsLE.Text := Rep_Traitements;
  RepFichiersInfLE.Text := Rep_FichiersInf;
  RepManipsLE.Text := Rep_Manip;
  ADBasicLE.Text := ADBasic;
  SystemCB.ItemIndex := SystemCB.Items.IndexOf(SystemType);

  NbProcessComboBox.ItemIndex := Nb_Process - 1;
  NumProcessComboBox.ItemIndex := Num_Process - 1;
  LinkAdressEdit.Text := inttostr(Link_Address);
  LevelAppliComboBox.ItemIndex := LevelAppliComboBox.Items.IndexOf(Level_Appli);
  LevelRecupGeneComboBox.ItemIndex := LevelRecupGeneComboBox.Items.IndexOf(Level_RecupGene);
  LevelPaintComboBox.ItemIndex := LevelPaintComboBox.Items.IndexOf(Level_Affiche);
  
  EnregStatCheckBox.OnClick := nil;
  EnregStatCheckBox.Checked := Enreg_Stats;
	EnregStatCheckBox.OnClick := EnregStatCheckBoxClick;

	AlphaBlendCheckBox.OnClick := nil;
	AlphaBlendCheckBox.Checked := Show_Alpha_Blending;
  AlphaBlendCheckBox.OnClick := AlphaBlendCheckBoxClick;

	EnReseauCheckBox.OnClick := nil;
  EnReseauCheckBox.Checked := EnReseau;
  EnReseauCheckBox.OnClick := EnReseauCheckBoxClick;

  ReseauPanel.Enabled := EnReseau;
  if EnReseau then
  begin
    ModeCB.ItemIndex := ModeCB.Items.IndexOf(Mode);
    IPServeurEdit.Text := IPServeur;
    PortEdit.Text := Port;
  end;

  adBasicVersionComboBox.ItemIndex := adBasicVersion - 4;

  AppliquerConfigButton.Enabled := false;
end;

procedure TOptionsConfForm.configChange;
begin
  Rep_Racine := RepRacineLE.Text;
  Rep_Fonctions := RepFctsLE.Text;
  Rep_Actions := RepActionsLE.Text;
  Rep_Traitements := RepTraitementsLE.Text;
  Rep_FichiersInf := RepFichiersInfLE.Text;
  Rep_manip := RepManipsLE.Text;
  ADBasic := ADBasicLE.Text;
  adBasicVersion := adBasicVersionComboBox.ItemIndex + 4;
  SystemType := SystemCB.Text;
  Nb_Process := NbProcessComboBox.ItemIndex + 1;
  Num_Process := NumProcessComboBox.ItemIndex + 1;
  Link_Address := strtoint(LinkAdressEdit.Text);
  Level_Appli := LevelAppliComboBox.Text;
  Level_RecupGene := LevelRecupGeneComboBox.Text;
  Level_Affiche := LevelPaintComboBox.Text;
	Enreg_Stats := EnregStatCheckBox.Checked;
	Show_Alpha_Blending := AlphaBlendCheckBox.Checked; 
  EnReseau := EnReseauCheckBox.Checked;
  ReseauPanel.Enabled := EnReseau;
  Mode := ModeCB.Text;
  IPServeur := IPServeurEdit.Text;
  Port := PortEdit.Text;
end;

procedure TOptionsConfForm.EnregStatCheckBoxClick(Sender: TObject);
begin
	AppliquerConfigButton.Enabled := true;
	configChange;
end;

procedure TOptionsConfForm.AlphaBlendCheckBoxClick(Sender: TObject);
begin
	AppliquerConfigButton.Enabled := true;
	configChange;
end;

procedure TOptionsConfForm.ChangeDirectory(Sender : TLabeledEdit);
var SelectedDir : string;
begin
  if SelectDirectory('Selectionnez un repertoire ...','',SelectedDir) then
  begin
    Sender.Text := SelectedDir + '\';
    configChange;
  end;
end;

procedure TOptionsConfForm.RepRacineButtonClick(Sender: TObject);
begin
  Self.ChangeDirectory(RepRacineLE);
end;

procedure TOptionsConfForm.RepFctsButtonClick(Sender: TObject);
begin
  Self.ChangeDirectory(RepFctsLE);
end;

procedure TOptionsConfForm.RepActionsButtonClick(Sender: TObject);
begin
  Self.ChangeDirectory(RepActionsLE);
end;

procedure TOptionsConfForm.RepTraitementsButtonClick(Sender: TObject);
begin
  Self.ChangeDirectory(RepTraitementsLE);
end;

procedure TOptionsConfForm.RepFichiersInfButtonClick(Sender: TObject);
begin
  Self.ChangeDirectory(RepFichiersInfLE);
end;

procedure TOptionsConfForm.RepManipsButtonClick(Sender: TObject);
begin
  Self.ChangeDirectory(RepManipsLE);
end;

procedure TOptionsConfForm.ADBasicButtonClick(Sender: TObject);
begin
  Self.ChangeDirectory(ADBasicLE);
  {if OpenDialog1.Execute then
  begin
    ADBasicLE.Text := OpenDialog1.FileName;
    configChange;
  end;}
end;

procedure TOptionsConfForm.RepRacineLEChange(Sender: TObject);
begin
  AppliquerConfigButton.Enabled := true;
end;

procedure TOptionsConfForm.EnReseauCheckBoxClick(Sender: TObject);
begin
  AppliquerConfigButton.Enabled := true;
  configChange;
end;



end.
