{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 UInitCanal;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, Grids, Buttons, xmldom, XMLIntf, msxmldom,
  XMLDoc, Registry, UGLobal;

type
  TInitCanalForm = class(TForm)
    Memo1: TMemo;
    Panel1: TPanel;
    Panel2: TPanel;
    InitStringGrid: TStringGrid;
    Label1: TLabel;
    SortieComboBox: TComboBox;
    Label2: TLabel;
    ParamMemo: TMemo;
    Splitter1: TSplitter;
    OKBitBtn: TBitBtn;
    AjouterButton: TButton;
    XMLDocEchange: TXMLDocument;
    ModifierButton: TButton;
    SupprimerButton: TButton;
    procedure FormShow(Sender: TObject);
    procedure AjouterButtonClick(Sender: TObject);
    procedure InitStringGridMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure ModifierButtonClick(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure SupprimerButtonClick(Sender: TObject);
    procedure OKBitBtnClick(Sender: TObject);
    procedure InitStringGridMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure ParamMemoKeyPress(Sender: TObject; var Key: Char);
    procedure SortieComboBoxChange(Sender: TObject);
  private
    { Déclarations privées }
  public
    { Déclarations publiques }  
    procedure MAJIHM;
  end;

var
  InitCanalForm: TInitCanalForm;
  
implementation

uses XMLClassesEchanges;

const Mess1 = 'Cette action n''est pas totalement configurée.'+
          #13'Quitter tout de même ?';
      Mess2 = 'Des modifications n''ont pas été prises en compte'+
          #13'Quitter tout de même ?';
      Mess3 = 'Des modification sur le canal courant n''ont pas été appliquées' +
          #13'Continuer tout de même ?';

var DocEchange : IXMLEchangeROOTType;
    //Rep_Racine,Rep_Traitements,Rep_Actions,Rep_Fonctions : string;
    //SelectedCol,SelectedRow : integer;

{$R *.dfm}

procedure TInitCanalForm.MAJIHM;
var i : integer;
begin

  if DocEchange.FONCTION.SORTIE.Count > 0
  then InitStringGrid.RowCount := DocEchange.FONCTION.SORTIE.Count + 1
  else InitStringGrid.RowCount := 2;

  if DocEchange.FONCTION.SORTIE.Count = 0 then
  begin
    InitStringGrid.Cells[0,1] := '';
    InitStringGrid.Cells[1,1] := '';
  end;

  for i := 0 to DocEchange.FONCTION.SORTIE.Count - 1 do
  begin
    InitStringGrid.Cells[0,i+1] := DocEchange.FONCTION.SORTIE.Items[i].NOM;
    InitStringGrid.Cells[1,i+1] := DocEchange.FONCTION.PARAM.Items[i].NOM;
  end;
end;

procedure TInitCanalForm.FormShow(Sender: TObject);
var i : integer;
    selection : TGridRect;
begin
  Readconfig;

  XMLDocEchange.LoadFromFile(Rep_Fonctions+'Echange.xml');
  DocEchange := GetRootEchange(XMLDocEchange);

  SortieCombobox.Clear;
  for i := 0 to DocEchange.CANAL.Count - 1
  do SortieCombobox.Items.Add(DocEchange.CANAL.Items[i].NOM);
  ParamMemo.Clear;
  InitStringGrid.DefaultColWidth :=   InitStringGrid.ClientWidth div 2;
  InitStringGrid.Cells[0,0] := ' Canal';
  InitStringGrid.Cells[1,0] := ' Valeur';
  MAJIHM;
  selection.Top :=   -1;
  selection.Bottom := -1;
  InitStringGrid.Selection := selection;


end;

procedure TInitCanalForm.AjouterButtonClick(Sender: TObject);
var i : integer;
    S : string;
    NewSortie : IXMLEchangeSortieType;
    NewParam  : IXMLEchangeParamType;
begin
  if (SortieComboBox.Text <> '') and (ParamMemo.Lines.Count > 0) then
  begin
    if ParamMemo.Lines[0] <> '' then
    begin
      S := '';
      for i := 0 to ParamMemo.Lines.Count - 1 do S := S + ParamMemo.Lines[i];
      NewSortie := DocEchange.FONCTION.SORTIE.Add;
      NewSortie.NOM := SortieComboBox.Text;
      NewParam := DocEchange.FONCTION.PARAM.Add;
      NewParam.NOM := S;
      SortieComboBox.ItemIndex := -1;
      ParamMemo.Clear;
      MAJIHM;
    end;
  end;
end;

procedure TInitCanalForm.InitStringGridMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);   
var  response : integer;
     oldSelectedRow : integer;
     selection : TGridRect;
     oldChannelName : String;
     oldChannelValue : String;
begin

  oldSelectedRow := SelectedRow;
  InitStringGrid.MouseToCell(X,Y,SelectedCol,SelectedRow);
  if (InitStringGrid.Cells[0,SelectedRow] <> '') and
     (InitStringGrid.Cells[1,SelectedRow] <> '') and
     (SelectedRow > -1) then
  begin
     response := mrYes;
     if (ModifierButton.Enabled)
     then response := MessageDlg(Mess3,mtInformation,[mbYes,mbNo],0);

     if ((response = mrYes) or (not ModifierButton.Enabled)) then
     begin
       SortieComboBox.ItemIndex := -1;
       ParamMemo.Clear;
       SortieComboBox.ItemIndex := SortieComboBox.Items.IndexOf(InitStringGrid.Cells[0,SelectedRow]);
       if(  SortieComboBox.ItemIndex = -1) then SortieComboBox.Text :=  InitStringGrid.Cells[0,SelectedRow];
       ParamMemo.Lines.Add(InitStringGrid.Cells[1,SelectedRow]);
       ModifierButton.Enabled := false;
       SupprimerButton.Enabled := true;
     end else
     begin
       SelectedRow := oldSelectedRow;
       selection.Left := 0;
       selection.Right := 1;
       selection.Top := selectedRow;
       selection.Bottom := SelectedRow;
       InitStringGrid.Selection := selection;
       //SortieComboBox.ItemIndex := SortieComboBox.Items.IndexOf(InitStringGrid.Cells[0,SelectedRow]);
       //ParamMemo.Lines.Add(InitStringGrid.Cells[1,SelectedRow]);
       //SortieComboBox.Text := oldChannelName;
       //ParamMemo.Lines.Add(InitStringGrid.Cells[1,SelectedRow]);
     end;
  end;
end;

procedure TInitCanalForm.ModifierButtonClick(Sender: TObject);
var S : string;
    i : integer;
    ASortie : IXMLEchangeSortieType;
    AParam  : IXMLEchangeParamType;
begin
  if (SortieComboBox.Text <> '') and (ParamMemo.Lines.Count > 0) then
  begin
    if ParamMemo.Lines[0] <> '' then
    begin
      ModifierButton.Enabled := false;
      //SupprimerButton.Enabled := false;
      ASortie := DocEchange.FONCTION.SORTIE.Items[SelectedRow-1];
      AParam  := DocEchange.FONCTION.PARAM.Items[SelectedRow-1];
      ASortie.NOM := SortieComboBox.Text;
      S := '';
      for i := 0 to ParamMemo.Lines.Count - 1 do S := S + ParamMemo.Lines[i];
      AParam.NOM := S;
      //SortieComboBox.ItemIndex := -1;
      //ParamMemo.Clear;
      MAJIHM;
    end;
  end;
end;

procedure TInitCanalForm.FormResize(Sender: TObject);
begin
  InitStringGrid.ColWidths[1] := InitStringGrid.Width - InitStringGrid.ColWidths[0];
  //InitStringGrid.DefaultColWidth :=   InitStringGrid.ClientWidth div 2;
end;

procedure TInitCanalForm.SupprimerButtonClick(Sender: TObject);
var selection : TGridRect;
begin
  ModifierButton.Enabled := false;
  SupprimerButton.Enabled := false;
  DocEchange.FONCTION.SORTIE.Delete(SelectedRow-1);
  DocEchange.FONCTION.PARAM.Delete(SelectedRow-1);
  SortieComboBox.ItemIndex := -1;
  ParamMemo.Clear;
  MAJIHM;
  SelectedRow :=  SelectedRow - 1;
  selection.Top :=   SelectedRow;
  selection.Bottom := SelectedRow;
  InitStringGrid.Selection := selection;

end;

procedure TInitCanalForm.OKBitBtnClick(Sender: TObject);
var reponse : integer;
begin
  reponse := mrYes;

  if DocEchange.FONCTION.SORTIE.Count = 0
  then reponse :=  messagedlg(Mess1,mtInformation,[mbYes,mbNo],0);
  if reponse = mrYes then
  begin
    if ModifierButton.Enabled
    then reponse :=  messagedlg(Mess2,mtInformation,[mbYes,mbNo],0);
    if reponse = mrYes then
    begin
      XMLDocEchange.SaveToFile(Rep_Fonctions+'Echange.xml');
      InitCanalForm.close;
    end;
  end;
end;


procedure TInitCanalForm.InitStringGridMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  InitStringGrid.ColWidths[1] := InitStringGrid.Width - InitStringGrid.ColWidths[0];
end;

procedure TInitCanalForm.ParamMemoKeyPress(Sender: TObject; var Key: Char);
begin
  ModifierButton.Enabled := (SelectedRow > 0);

end;

procedure TInitCanalForm.SortieComboBoxChange(Sender: TObject);
begin
  if(ParamMemo.Text <> '') then ModifierButton.Enabled := true;
end;

end.
