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

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, ExtCtrls, StdCtrls, ComCtrls, ActnList, Menus, XMLClasses, Contnrs,
	ActnMan, UActStimTraitObject, xmldom, XMLIntf, msxmldom, XMLDoc, ImgList,
	ShellApi;

type
	TRefXMLBloc=class(TOBject)
    XMLBloc : IXMLBlocType;
  end;
  TDescriptFrame = class(TFrame)
    Panel1: TPanel;
    Shape1: TShape;
    STComboBox: TComboBox;
    BlocsTreeView: TTreeView;
    Splitter1: TSplitter;
    STBlocsFonctionsList: TActionList;
    NewSTAction: TAction;
    RenSTAction: TAction;
    SuppSTAction: TAction;
    CopySTAction: TAction;
    PasteSTAction: TAction;
    DefBlocAction: TAction;
    SuppBlocAction: TAction;
    InsertBlocBeforeAction: TAction;
    InsertBlocAfterAction: TAction;
    AddBlocAction: TAction;
    CopyBlocAction: TAction;
    PasteBlocAction: TAction;
    BlocsPopupMenu: TPopupMenu;
    DfinirceBloc1: TMenuItem;
    SuppBlocMenuItem: TMenuItem;
    InsertBlocBeforeMenuItem: TMenuItem;
    InsertBlocAfterMenuItem: TMenuItem;
    InclureBlocMenuItem: TMenuItem;
    CopyBlocMenuItem: TMenuItem;
    PastBlocMenuItem: TMenuItem;
    Shape2: TShape;
    STPopupMenu: TPopupMenu;
    NouvelleSquencetype2: TMenuItem;
    Copiercettesquencetype2: TMenuItem;
    Collerlasquencetype2: TMenuItem;
    Renommercettesquencetype2: TMenuItem;
    Supprimercettesquencetype2: TMenuItem;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    N4: TMenuItem;
    N5: TMenuItem;
    N6: TMenuItem;
    AddTraitementMenuItem: TMenuItem;
    AddActionMenuItem: TMenuItem;
    AddStimulusMenuItem: TMenuItem;
    FunctionScrollBox: TScrollBox;
    DeleteFunctionTimer: TTimer;
    ESPopupMenu: TPopupMenu;
    ActionPopupMenu: TPopupMenu;
    SupprimerFonctionAction: TAction;
    AddActionAction: TAction;
    AddTraitementAction: TAction;
    AddStimulusAction: TAction;
    TraitementPopupMenu: TPopupMenu;
    StimulusPopupMenu: TPopupMenu;
    FichierEchangeXML: TXMLDocument;
    AffecterGeneriqueAction: TAction;
    DragDropPopupMenu: TPopupMenu;
    DragDropDeplacerFonctionAction: TAction;
    DragDropCopierFonctionAction: TAction;
    Dplacerlafonction1: TMenuItem;
    Copierlafonction1: TMenuItem;
    Image1: TImage;
    ImageList1: TImageList;
    FonctionPopupMenu: TPopupMenu;
    EditerFonction: TMenuItem;
    InsertAfter: TMenuItem;
    InsertBefore: TMenuItem;
    SupprimerFonction: TMenuItem;
    EditerFoncionAction: TAction;
    Affecter: TMenuItem;
    N7: TMenuItem;
    AjouterAction: TMenuItem;
    AjouterTraitement: TMenuItem;
    AjouterStimulus: TMenuItem;
    UpdateMainMenuAction: TAction;
    STNameLabel: TLabel;
    GlobalVueSTAction: TAction;
    N8: TMenuItem;
    VueglobaledecetteSquenceType1: TMenuItem;
    procedure Splitter1Moved(Sender: TObject);
    procedure NewSTActionExecute(Sender: TObject);
    procedure RenSTActionExecute(Sender: TObject);
    procedure SuppSTActionExecute(Sender: TObject);
    procedure CopySTActionExecute(Sender: TObject);
    procedure PasteSTActionExecute(Sender: TObject);
    procedure STComboBoxExit(Sender: TObject);
    procedure STComboBoxKeyPress(Sender: TObject; var Key: Char);
    procedure BlocsPopupMenuPopup(Sender: TObject);
    procedure STComboBoxSelect(Sender: TObject);
    procedure InsertBlocBeforeActionExecute(Sender: TObject);
    procedure InsertBlocAfterActionExecute(Sender: TObject);
    procedure AddBlocActionExecute(Sender: TObject);
    procedure SuppBlocActionExecute(Sender: TObject);
    procedure CopyBlocActionExecute(Sender: TObject);
    procedure PasteBlocActionExecute(Sender: TObject);
    procedure DefBlocActionExecute(Sender: TObject);
    procedure BlocsTreeViewChange(Sender: TObject; Node: TTreeNode);
    procedure STPopupMenuPopup(Sender: TObject);
    procedure FunctionScrollBoxDragOver(Sender, Source: TObject; X,
      Y: Integer; State: TDragState; var Accept: Boolean);
    procedure FunctionScrollBoxDragDrop(Sender, Source: TObject; X,
      Y: Integer);
    procedure DeleteFunctionTimerTimer(Sender: TObject);
    procedure Panel1DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure ChangeESClick(Sender: TObject);
    procedure SupprimerFonctionActionExecute(Sender: TObject);
    procedure AddActionActionExecute(Sender: TObject);
    procedure AddTraitementActionExecute(Sender: TObject);
    procedure AddStimulusActionExecute(Sender: TObject);
    procedure AffecterGeneriqueActionExecute(Sender: TObject);
    procedure FrameResize(Sender: TObject);
    procedure BlocsTreeViewDragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure BlocsTreeViewDragDrop(Sender, Source: TObject; X,
      Y: Integer);
    procedure DragDropDeplacerFonctionActionExecute(Sender: TObject);
    procedure DragDropCopierFonctionActionExecute(Sender: TObject);
    procedure HelpImageClick(Sender: TObject);
    procedure EditerFoncionActionExecute(Sender: TObject);
    procedure FonctionPopupMenuPopup(Sender: TObject);
    procedure FunctionScrollBoxMouseMove(Sender: TObject;
      Shift: TShiftState; X, Y: Integer);
		procedure UpdateMainMenuExecute(Sender: TObject);
    procedure GlobalVueSTActionExecute(Sender: TObject);
    procedure BlocsTreeViewStartDrag(Sender: TObject;
      var DragObject: TDragObject);
    procedure FunctionScrollBoxMouseUp(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  private
    { Déclarations privées }
    droppedActStimTrait : TActStimTrait;
		procedure MAJBlocs;
		procedure BlocsTreeViewClearItems;
  public
    { Déclarations publiques }
    CurentXMLST      : IXMLSTType;
		CopyXMLST        : IXMLSTType;
    CurentXMLBloc    : IXMLBlocType;
    CopyXMLBloc      : IXMLBlocType;
    CurentNodeBloc   : TTreeNode;
    CurentFct        : TActStimTrait;
    CurentES         : TES;
    DragDropTreeNode : TTreeNode;
    procedure MAJIHM;
    procedure ClearIHM;
		function AjouterFonction(pXMLFunction: IXMLFONCTIONType):TActStimTrait;
		procedure MAJMAINMENU;
  end;

implementation

uses UDocometre, UDefinirBloc, UGlobal;

{$R *.dfm}
procedure TDescriptFrame.MAJMAINMENU;
var newCategorie : TActionClientItem;
		newAction : TActionClientItem;
		newBasicActionLink : TActionClientLink;
		i:integer;
begin
	//Creation catégorie
	newCategorie := DocoForm.ActionManager.AddCategory('Outils',DocoForm.ActionManager.ActionBars[0].Items[0]);
	newBasicActionLink := TActionClientLink.Create(newCategorie);
	newBasicActionLink.Action := Self.UpdateMainMenuAction;
	newCategorie.ActionLink := newBasicActionLink;
	for i := 0 to 18 do
	begin
		case i of
		6:DocoForm.ActionManager.AddSeparator(newAction);
		14:DocoForm.ActionManager.AddSeparator(newAction);
		20:DocoForm.ActionManager.AddSeparator(newAction);
		21:DocoForm.ActionManager.AddSeparator(newAction);
		22:DocoForm.ActionManager.AddSeparator(newAction);
		else
			//Ajout des actions ds la catégorie
			newAction := newCategorie.Items.Add();
			//Creation du lien vers la liste d'action existante facon ancienne
			newBasicActionLink := TActionClientLink.Create(newAction);
			case i of
				0:newBasicActionLink.Action := Self.NewSTAction;
				1:newBasicActionLink.Action := Self.RenSTAction;
				2:newBasicActionLink.Action := Self.SuppSTAction;
				3:newBasicActionLink.Action := Self.CopySTAction;
				4:newBasicActionLink.Action := Self.PasteSTAction;
        5:newBasicActionLink.Action := Self.GlobalVueSTAction;

				7:begin
            newBasicActionLink.Action := Self.DefBlocAction;
            newAction.ImageIndex := 2;
          end;
				8:begin
            newBasicActionLink.Action := Self.SuppBlocAction;
            newAction.ImageIndex := 6;
          end;
				9:begin
            newBasicActionLink.Action := Self.InsertBlocBeforeAction;
            newAction.ImageIndex := 0;
          end;
				10:begin
            newBasicActionLink.Action := Self.InsertBlocAfterAction;
            newAction.ImageIndex := 0;
          end;
				11:begin
             newBasicActionLink.Action := Self.AddBlocAction;
             newAction.ImageIndex := 0;
           end;
				12:begin
             newBasicActionLink.Action := Self.CopyBlocAction;
             newAction.ImageIndex := 7;
           end;
				13:begin
             newBasicActionLink.Action := Self.PasteBlocAction;
             newAction.ImageIndex := 5;
           end;

				15:begin
             newBasicActionLink.Action := Self.AddActionAction;
             newAction.ImageIndex := 3;
           end;
				16:begin
             newBasicActionLink.Action := Self.AddTraitementAction;
             newAction.ImageIndex := 4;
           end;
				17:begin
             newBasicActionLink.Action := Self.AddStimulusAction;
             newAction.ImageIndex := 1;
           end;
				18:begin
             newBasicActionLink.Action := Self.EditerFoncionAction;
           end;
				19:begin
             newBasicActionLink.Action := Self.SupprimerFonctionAction;
             newAction.ImageIndex := 6;
           end;
			end;
			//Affectation du lien a l'action
			newAction.ActionLink := newBasicActionLink;
		end;
	end;   
end;

procedure TDescriptFrame.Splitter1Moved(Sender: TObject);
var i : integer;
begin
  FunctionScrollBox.Visible := false;
  STCombobox.Width := BlocsTreeView.Width;
  STNameLabel.Left := STCombobox.Width + 10;
  for i := 0 to FunctionScrollBox.ControlCount - 1
  do(FunctionScrollBox.Controls[i] as TActStimTrait).MoveALLS;
  FunctionScrollBox.Visible := true;
end;

procedure TDescriptFrame.FrameResize(Sender: TObject);
begin
  Splitter1Moved(Self);
end;

procedure TDescriptFrame.BlocsTreeViewClearItems;
var i : integer;
//		RefXMLBloc : TRefXMLBloc;
begin
	for i := 0 to BlocsTreeView.Items.Count - 1 do
	begin
		TRefXMLBloc(BlocsTreeView.Items[i].Data).Free;
		BlocsTreeView.Items[i].Data := nil;
	end;
  BlocsTreeView.Items.Clear;
end;

procedure TDescriptFrame.MAJIHM;
var i : integer;
begin
	CurentXMLST := nil;
	CurentXMLBloc := nil;
	CurentNodeBloc := nil;
	//BlocsTreeView.Items.Clear;
	BlocsTreeViewClearItems;
	STComboBox.Clear;
	STComboBox.Hint := '';
  STNameLabel.Caption := '';
	for i := 0 to DocDOCOMETRE.ST.Count - 1
	do STCombobox.Items.Add(DocDOCOMETRE.ST.Items[i].NOM);
end;

procedure TDescriptFrame.ClearIHM;
begin
	STComboBox.Clear;

	BlocsTreeViewClearItems;
	while FunctionScrollBox.ControlCount > 0
	do (FunctionScrollBox.Controls[0] as TActStimTrait).Free;

	STComboBox.Hint := '';
end;

procedure TDescriptFrame.MAJBlocs;
var i : integer;
    FirstBloc : IXMLBlocType;
		FirstNode : TTreeNode;
    RefFirstXMLBloc : TRefXMLBloc;

  procedure AddBlocsToTreeView(BlocPere : IXMLBlocType; TVNodePere : TTreeNode);
  var j : integer;
      TVNodeFils : TTreeNode;
      BlocFils : IXMLBlocType;
      RefXMLBloc : TRefXMLBloc;
  begin
    for j := 0 to BlocPere.BLOC.Count - 1 do
    begin
			BlocFils := BlocPere.BLOC.Items[j];
			TVNodeFils := BlocsTreeView.Items.AddChild(TVNodePere,BlocFils.NOM);
			RefXMLBloc := TRefXMLBloc.Create;
			RefXMLBloc.XMLBloc := BlocFils;
			TVNodeFils.Data := RefXMLBloc;
			TVNodeFils.ImageIndex := 0;
			TVNodeFils.SelectedIndex := 0;
			AddBlocsToTreeView(BlocFils,TVNodeFils);
    end;
  end;

begin
	//BlocsTreeView.Items.Clear;
	BlocsTreeViewClearItems;
  for i := 0 to CurentXMLST.BLOC.Count - 1 do
	begin
		FirstBloc := CurentXMLST.BLOC.Items[i];
		FirstNode := BlocsTreeView.Items.Add(nil,FirstBloc.NOM);
		if FirstBloc.NOM = 'BLOC - DEBUT' then
		begin
			FirstNode.ImageIndex := 8;
			FirstNode.SelectedIndex := 8;
		end;
		if FirstBloc.NOM = 'BLOC - FIN' then
		begin
			FirstNode.ImageIndex := 9;
			FirstNode.SelectedIndex := 9;
		end;
		RefFirstXMLBloc := TRefXMLBloc.Create;
		RefFirstXMLBloc.XMLBloc := FirstBloc;
		FirstNode.Data := RefFirstXMLBloc;
		AddBlocsToTreeView(FirstBloc,FirstNode);
  end;
  BlocsTreeView.FullExpand;
end;

//***************************
//Gestion des séquences types
procedure TDescriptFrame.NewSTActionExecute(Sender: TObject);
var NewBloc : IXMLBlocType;
begin
  CurentXMLST := DocDOCOMETRE.ST.Add;
  CurentXMLST.NOM := 'Nouvelle Séquence Type';
  CurentXMLST.FULLSCREEN := 0;
  CurentXMLST.STATUSBAR := 1;
  NewBloc := CurentXMLST.BLOC.Add;
  NewBloc.NOM := 'BLOC - DEBUT';
  NewBloc.GENRE := 'DEBUT';
  NewBloc.ADBASICCODE1 := '';//'LOWINIT:';
  NewBloc.ADBASICCODE2 := '';//'EVENT:';
  NewBloc.DELPHICODE1 := 'PROCEDURE INIT(VAR Index:integer;'+#13+#10+
                         '               VAR TAB_DATA:TTAB_DATA_PARAM;'+#13+#10+
                         '               VAR TAB_PARAM:TTAB_DATA_PARAM;'+#13+#10+
                         '               VAR REALTIME_LOOP_STATE: integer);'#13+#10+
                         'BEGIN';
  NewBloc.DELPHICODE2 := 'END;'+#13+#10+
                         ' '+#13+#10+
                         'PROCEDURE EVENT(VAR Index:integer;'+#13+#10+
                         '                VAR TAB_DATA:TTAB_DATA_PARAM;'+#13+#10+
                         '                VAR TAB_PARAM:TTAB_DATA_PARAM;'+#13+#10+
                         '                VAR REALTIME_LOOP_STATE: integer);'#13+#10+
                         'BEGIN';
  NewBloc.PARAM1 := '';
  NewBloc.PARAM2 := '';
  NewBloc.PARAM3 := '';

  NewBloc := CurentXMLST.BLOC.Add;
  NewBloc.NOM := 'BLOC - NORMAL';
  NewBloc.GENRE := 'NORMAL';
  NewBloc.ADBASICCODE1 := '';
  NewBloc.ADBASICCODE2 := '';
  NewBloc.DELPHICODE1 := '';
  NewBloc.DELPHICODE2 := '';
  NewBloc.PARAM1 := '';
  NewBloc.PARAM2 := '';
  NewBloc.PARAM3 := '';

  NewBloc := CurentXMLST.BLOC.Add;
  NewBloc.NOM := 'BLOC - FIN';
  NewBloc.GENRE := 'FIN';
  NewBloc.ADBASICCODE1 := '';//'FINISH:';
  NewBloc.ADBASICCODE2 := '';
  NewBloc.DELPHICODE1 := 'END;'+#13+#10+
                         ' '+#13+#10+ 
                         'PROCEDURE FINISH(VAR Index:integer;'+#13+#10+
                         '                 VAR TAB_DATA:TTAB_DATA_PARAM;'+#13+#10+
                         '                 VAR TAB_PARAM:TTAB_DATA_PARAM;'+#13+#10+
                         '                 VAR REALTIME_LOOP_STATE: integer);'#13+#10+
                         'BEGIN';
  NewBloc.DELPHICODE2 := 'END;'+#13+#10+
                         ' '+#13+#10+
                         'EXPORTS INIT,EVENT,FINISH;'+#13+#10+
                         'BEGIN'+#13+#10+
                         'END.'; 
  NewBloc.PARAM1 := '';
  NewBloc.PARAM2 := '';
  NewBloc.PARAM3 := '';

  MAJBlocs;

  STCombobox.Items.Add(CurentXMLST.NOM);
  STCombobox.ItemIndex := STCombobox.Items.IndexOf(CurentXMLST.NOM);
  STCombobox.OnExit := nil;
  STCombobox.Style := csDropDown;
  STCombobox.SelectAll;
  STCombobox.SetFocus;
  STComboBox.OnExit := STComboBoxExit;

  STNameLabel.Caption := '[ Nouvelle Séquence Type ]';

  DocoForm.SimulFrame.MAJIHM;
end;

procedure TDescriptFrame.RenSTActionExecute(Sender: TObject);
begin
  STCombobox.OnExit := nil;
  STCombobox.Style := csDropDown;
  STCombobox.SelectAll;
  STCombobox.SetFocus;
  STComboBox.OnExit := STComboBoxExit;
end;

procedure TDescriptFrame.STComboBoxSelect(Sender: TObject);
begin
  if STCombobox.Style = csDropDownList then
  begin
    CurentXMLST := DocDOCOMETRE.ST.Items[STCombobox.ItemIndex];
		MAJBlocs;
		STCombobox.Hint := STCombobox.Text;
    STNameLabel.Caption := '[ ' +  STCombobox.Text + ' ]';
  end;
end;

procedure TDescriptFrame.STComboBoxExit(Sender: TObject);
var S : string;
begin
  if (STCombobox.Style = csDropDown) and (STCombobox.Text <> '') then
  begin
    S := STCombobox.Text;
		STCombobox.Items[STCombobox.Items.IndexOf(CurentXMLST.NOM)] := S;
		DocDOCOMETRE.SESSIONS.ChangeSTName(CurentXMLST.NOM,S);
		if FileExists(Rep_Manip + 'BinSources\' + CurentXMLST.NOM + '.BAS')
		then DeleteFile(Rep_Manip + 'BinSources\' + CurentXMLST.NOM + '.BAS');
		if FileExists(Rep_Manip + 'BinSources\' + CurentXMLST.NOM + '.T91')
		then DeleteFile(Rep_Manip + 'BinSources\' + CurentXMLST.NOM + '.T91');
		if DirectoryExists(rep_manip + 'Test_' + CurentXMLST.NOM)
		then if RenameFile(rep_manip + 'Test_' + CurentXMLST.NOM,rep_manip + 'Test_' + S) = false
				 then showmessage('Impossible de renommer le répertoire ' + rep_manip + 'Test_' + CurentXMLST.NOM);
		CurentXMLST.NOM := S;

		STCombobox.Style := csDropDownList;
		STCombobox.ItemIndex := STCombobox.Items.IndexOf(CurentXMLST.NOM);
		STCombobox.Hint := S;
    STNameLabel.Caption := '[ ' +  S + ' ]';
		DocoForm.OrgaFrame.MAJIHM;
		DocoForm.SimulFrame.MAJIHM;
		DocoForm.InspectFrame.MAJIHM;
  end;
end;

procedure TDescriptFrame.STComboBoxKeyPress(Sender: TObject;
  var Key: Char);
begin
  if (Key = Char(VK_RETURN)) and (STCombobox.Text <> '')
  then  STComboBoxExit(self);
end;

procedure TDescriptFrame.SuppSTActionExecute(Sender: TObject);
var i,j,k : integer;
		fos: TSHFileOpStruct;
		from : string;
begin

	if FileExists(Rep_Manip + 'BinSources\ST' + inttostr(STCombobox.itemIndex + 1) + '.PAS')
	then DeleteFile(Rep_Manip + 'BinSources\ST' + inttostr(STCombobox.itemIndex + 1) + '.PAS');
	if FileExists(Rep_Manip + 'BinSources\ST' + inttostr(STCombobox.itemIndex + 1) + '.dll')
	then DeleteFile(Rep_Manip + 'BinSources\ST' + inttostr(STCombobox.itemIndex + 1) + '.dll');
	if FileExists(Rep_Manip + 'BinSources\ST' + inttostr(STCombobox.itemIndex + 1) + '.out')
	then DeleteFile(Rep_Manip + 'BinSources\ST' + inttostr(STCombobox.itemIndex + 1) + '.out');
	if FileExists(Rep_Manip + 'BinSources\' + DocDOCOMETRE.ST[STCombobox.itemIndex].NOM + '.BAS')
	then DeleteFile(Rep_Manip + 'BinSources\' + DocDOCOMETRE.ST[STCombobox.itemIndex].NOM + '.BAS');
	if FileExists(Rep_Manip + 'BinSources\' + DocDOCOMETRE.ST[STCombobox.itemIndex].NOM + '.T91')
	then DeleteFile(Rep_Manip + 'BinSources\' + DocDOCOMETRE.ST[STCombobox.itemIndex].NOM + '.T91');
	if DirectoryExists(rep_manip + 'Test_' + STCombobox.Text) then
	begin
    ZeroMemory(@fos, SizeOf(fos));
		with fos do
		begin
			wFunc := FO_DELETE;
			fFlags := FOF_SILENT or FOF_NOCONFIRMATION;
			from := rep_manip + 'Test_' + STCombobox.Text + #0;
			pFrom := PChar(from);
		end;
		if (ShFileOperation(fos) <> 0)
		then showmessage ('Impossible de supprimer "' + rep_manip + 'Test_' + STCombobox.Text +'"');
	end;

  for i := 0 to DocDOCOMETRE.SESSIONS.Count - 1 do
  begin
    for j := 0 to DocDOCOMETRE.SESSIONS[i].ESSAIS.Count - 1 do
    begin
			if DocDOCOMETRE.SESSIONS[i].ESSAIS[j].NOMST = STCombobox.Text
      then DocDOCOMETRE.SESSIONS[i].ESSAIS[j].NOMST := '?';
    end;
  end;

  for i := 0 to DocDOCOMETRE.SUJETS.Count - 1 do
  begin
    for j := 0 to DocDOCOMETRE.SUJETS[i].SESSIONS.Count - 1 do
    begin
      for k := 0 to DocDOCOMETRE.SUJETS[i].SESSIONS[j].ESSAIS.Count - 1 do
      begin
        if DocDOCOMETRE.SUJETS[i].SESSIONS[j].ESSAIS[k].NOMST = STCombobox.Text
        then DocDOCOMETRE.SUJETS[i].SESSIONS[j].ESSAIS[k].NOMST := '?';
      end;
    end;
  end;

	DocDOCOMETRE.ST.Delete(STCombobox.ItemIndex);
  STCombobox.DeleteSelected;
	CurentXMLST := nil;
  //Effacement des fonctions existantes du boc selectionné
  while FunctionScrollBox.ControlCount > 0
  do (FunctionScrollBox.Controls[0] as TActStimTrait).Free;
	//BlocsTreeView.Items.Clear;
	BlocsTreeViewClearItems;
  STCombobox.ItemIndex := -1;
  BlocsTreeView.SetFocus;

  STCombobox.Hint := '';
  STNameLabel.Caption := '';

  DocoForm.SimulFrame.MAJIHM;
	DocoForm.OrgaFrame.MAJIHM;
	DocoForm.InspectFrame.MAJIHM;
end;

procedure TDescriptFrame.CopySTActionExecute(Sender: TObject);
begin
  CopyXMLST := CurentXMLST.CloneNode(true) as IXMLSTType;
end;

procedure TDescriptFrame.PasteSTActionExecute(Sender: TObject);
var STName : string;
    i : integer;
    NewBloc : IXMLBlocType;
begin
  if CopyXMLST <> nil then
  begin
    //sauvegarde nom de la ST courante
    STName := CurentXMLST.NOM;
    //Suppression de la ST Courante
    SuppSTActionExecute(Self);
    //Ajout d'une nouvelle ST
    CurentXMLST := DocDOCOMETRE.ST.Add;
    CurentXMLST.NOM := STName;
    CurentXMLST.FULLSCREEN := CopyXMLST.FULLSCREEN;
    CurentXMLST.STATUSBAR := CopyXMLST.STATUSBAR;
    //Copie de tous les blocs dans cette nouvelle ST
    for i := 0 to CopyXMLST.BLOC.Count - 1 do
    begin
      NewBloc := CopyXMLST.BLOC.Items[i].CloneNode(true) as IXMLBlocType;
      CurentXMLST.ChildNodes.Add(NewBloc);
    end;
    //MAJ IHM
    CopyXMLST := nil;
    MAJIHM;
    STCombobox.ItemIndex := STCombobox.Items.IndexOf(STName);
    STComboBoxSelect(Self);
  end;
end;

procedure TDescriptFrame.STPopupMenuPopup(Sender: TObject);
begin
  RenSTAction.Enabled := (CurentXMLST <> nil);
  SuppSTAction.Enabled := (CurentXMLST <> nil);
  CopySTAction.Enabled := (CurentXMLST <> nil);
  PasteSTAction.Enabled := (CopyXMLST <> nil);
  GlobalVueSTAction.Enabled := (CurentXMLST <> nil);
end;
//Gestion des séquences types
//***************************

//***********************
//Gestion des ST et Blocs
procedure TDescriptFrame.BlocsPopupMenuPopup(Sender: TObject);
begin
  BlocsTreeView.Selected := BlocsTreeView.Selected;
  BlocsTreeViewChange(BlocsTreeView,BlocsTreeView.Selected);
  DefBlocAction.Enabled := false;
  SuppBlocAction.Enabled := false;
	InsertBlocBeforeAction.Enabled := false;
	InsertBlocAfterAction.Enabled := false;
	AddBlocAction.Enabled := false;
	CopyBlocAction.Enabled := false;
	PasteBlocAction.Enabled := false;

	Self.AddActionAction.Enabled := CurentXMLBloc <> nil ;
	Self.AddTraitementAction.Enabled := CurentXMLBloc <> nil ;
	Self.AddStimulusAction.Enabled := CurentXMLBloc <> nil ;
  
  if CurentXMLBloc <> nil then
  begin
    DefBlocAction.Enabled := (CurentXMLBloc.GENRE <> 'DEBUT') and
                             (CurentXMLBloc.GENRE <> 'FIN');
    SuppBlocAction.Enabled := (CurentXMLBloc.GENRE <> 'DEBUT') and
                              (CurentXMLBloc.GENRE <> 'FIN') and
                              ((CurentXMLBloc.Collection.Count > 3) or
                               (CurentNodeBloc.Parent <> nil));
    InsertBlocBeforeAction.Enabled := (CurentXMLBloc.GENRE <> 'DEBUT');
    InsertBlocAfterAction.Enabled := (CurentXMLBloc.GENRE <> 'FIN') and
                                     (CurentNodeBloc.getNextSibling <> nil);
    AddBlocAction.Enabled := (CurentXMLBloc.GENRE <> 'DEBUT') and
                             (CurentXMLBloc.GENRE <> 'FIN');

    CopyBlocAction.Enabled := true;
		PasteBlocAction.Enabled := (CopyXMLBloc <> nil);
  end;
end;
//Gestion des ST et Blocs
//***********************

//*****************
//Gestion des Blocs
procedure TDescriptFrame.BlocsTreeViewChange(Sender: TObject;Node: TTreeNode);
var RefXMLBloc : TRefXMLBloc;
    i : integer;
begin
	if CurentFct <> nil then CurentFct.HideTitreEdit;
  //BlocsTreeView.Selected := Node;
  FunctionScrollBox.Visible := false;
	RefXMLBloc := Node.Data;
	if RefXMLBloc <> nil then
	begin
		CurentXMLBloc := RefXMLBloc.XMLBloc;
		CurentNodeBloc := Node;
		//Effacement des fonctions existantes du boc précedent
		while FunctionScrollBox.ControlCount > 0
		do (FunctionScrollBox.Controls[0] as TActStimTrait).Free;
		//construction des fonctions de ce bloc
		for i := 0 to CurentXMLBloc.FONCTION.Count - 1
		do AjouterFonction(CurentXMLBloc.FONCTION.Items[i]);
		FunctionScrollBox.Visible := true;
	end;
	CurentFct := nil;
end;

procedure TDescriptFrame.InsertBlocBeforeActionExecute(Sender: TObject);
var index : integer;
    XMLBlocTypeListParent : IXMLBLOCTypeList;
    NewXMLBloc : IXMLBlocType;
    NewNodeBloc : TTreeNode;
    RefXMLBloc : TRefXMLBloc;
begin
  //MAJ Document
  XMLBlocTypeListParent := CurentXMLBloc.Collection as IXMLBLOCTypeList;
  index := XMLBlocTypeListParent.ChildNodes.IndexOf(CurentXMLBloc);
  NewXMLBloc := CurentXMLBloc.CloneNode(false) as IXMLBlocType;
  NewXMLBloc.NOM := 'BLOC - NORMAL';
  NewXMLBloc.GENRE := 'NORMAL';
  NewXMLBloc.ADBASICCODE1 := '';
  NewXMLBloc.ADBASICCODE2 := '';
  NewXMLBloc.DELPHICODE1 := '';
  NewXMLBloc.DELPHICODE2 := '';
  NewXMLBloc.PARAM1 := '';
  NewXMLBloc.PARAM2 := '';
  NewXMLBloc.PARAM3 := '';
  XMLBlocTypeListParent.ChildNodes.Insert(index,NewXMLBloc);
  //MAJ vue
  NewNodeBloc := BlocsTreeView.Items.Insert(CurentNodeBloc,NewXMLBloc.NOM);
  RefXMLBloc := TRefXMLBloc.Create;
  RefXMLBloc.XMLBloc := NewXMLBloc;
	NewNodeBloc.Data := RefXMLBloc;

	Self.BlocsTreeViewChange(BlocsTreeView,NewNodeBloc);

end;

procedure TDescriptFrame.InsertBlocAfterActionExecute(Sender: TObject);
var index : integer;
    XMLBlocTypeListParent : IXMLBLOCTypeList;
    NewXMLBloc : IXMLBlocType;
		NewNodeBloc : TTreeNode;
    RefXMLBloc : TRefXMLBloc;
begin
	//MAJ Document
	XMLBlocTypeListParent  := CurentXMLBloc.Collection as IXMLBLOCTypeList;
  index := XMLBlocTypeListParent.ChildNodes.IndexOf(XMLBlocTypeListParent.ChildNodes.FindSibling(CurentXMLBloc,1));
  NewXMLBloc := CurentXMLBloc.CloneNode(false) as IXMLBlocType;
  NewXMLBloc.NOM := 'BLOC - NORMAL';
  NewXMLBloc.GENRE := 'NORMAL';
  NewXMLBloc.ADBASICCODE1 := '';
  NewXMLBloc.ADBASICCODE2 := '';
  NewXMLBloc.DELPHICODE1 := '';
  NewXMLBloc.DELPHICODE2 := '';
	NewXMLBloc.PARAM1 := '';
  NewXMLBloc.PARAM2 := '';
  NewXMLBloc.PARAM3 := '';
  XMLBlocTypeListParent.ChildNodes.Insert(index,NewXMLBloc);
  //MAJ vue
	NewNodeBloc := BlocsTreeView.Items.insert(CurentNodeBloc.getNextSibling,NewXMLBloc.NOM);
	RefXMLBloc := TRefXMLBloc.Create;
	RefXMLBloc.XMLBloc := NewXMLBloc;
	NewNodeBloc.Data := RefXMLBloc;

	Self.BlocsTreeViewChange(BlocsTreeView,NewNodeBloc);
	
end;

procedure TDescriptFrame.AddBlocActionExecute(Sender: TObject);
var NewXMLBloc : IXMLBlocType;
    NewNodeBloc : TTreeNode;
    RefXMLBloc : TRefXMLBloc;
begin
  NewXMLBloc := CurentXMLBloc.BLOC.Add;
  NewXMLBloc.NOM := 'BLOC - NORMAL';
  NewXMLBloc.GENRE := 'NORMAL';
  NewXMLBloc.ADBASICCODE1 := '';
	NewXMLBloc.ADBASICCODE2 := '';
  NewXMLBloc.DELPHICODE1 := '';
  NewXMLBloc.DELPHICODE2 := '';
  NewXMLBloc.PARAM1 := '';
  NewXMLBloc.PARAM2 := '';
  NewXMLBloc.PARAM3 := '';
  NewNodeBloc := BlocsTreeView.Items.AddChild(CurentNodeBloc,NewXMLBloc.NOM);
  RefXMLBloc := TRefXMLBloc.Create;
  RefXMLBloc.XMLBloc := NewXMLBloc;
	NewNodeBloc.Data := RefXMLBloc;

	Self.BlocsTreeViewChange(BlocsTreeView,NewNodeBloc);
end;

procedure TDescriptFrame.SuppBlocActionExecute(Sender: TObject);
var i : integer;
    XMLBlocTypeListParent : IXMLBLOCTypeList;
    RefXMLBloc : TRefXMLBloc;
    StimFileNames : TStringList;
begin

  if MessageDlg('Certain de vouloir supprimer ce bloc ?', mtInformation, [mbYes,mbNo],0) = mrYes then
  begin

  XMLBlocTypeListParent  := CurentXMLBloc.Collection as IXMLBLOCTypeList;
  for i := 0 to XMLBlocTypeListParent.Count - 1 do
  begin
    if XMLBlocTypeListParent.Items[i] = CurentXMLBloc
    then break;
  end;
  RefXMLBloc := CurentNodeBloc.Data;
  StimFileNames := TStringList.Create;
  RefXMLBloc.XMLBloc.GetStimFileNames(StimFileNames);
  RefXMLBloc.free;
  XMLBlocTypeListParent.Delete(i);
  BlocsTreeView.Items.Delete(CurentNodeBloc);

  for i := 0 to StimFileNames.Count - 1 do
  begin
    DeleteFile(StimFileNames[i]);
  end;

  StimFileNames.Free;
  end;
end;

procedure TDescriptFrame.CopyBlocActionExecute(Sender: TObject);
begin
  CopyXMLBloc := CurentXMLBloc.CloneNode(true) as IXMLBLOCType;
end;

procedure TDescriptFrame.PasteBlocActionExecute(Sender: TObject);
var AXMLFunction : IXMLFONCTIONType;
    AXMLBloc     : IXMLBlocType;
    i : integer;
begin
  if CopyXMLBloc <> nil then
  begin
    //Copie du type, nom etc ... de bloc
    if (CopyXMLBloc.GENRE <> 'DEBUT') and (CopyXMLBloc.GENRE <> 'FIN') then
    begin
      CurentXMLBloc.GENRE := CopyXMLBloc.GENRE;
      CurentXMLBloc.NOM := CopyXMLBloc.NOM;
      CurentXMLBloc.ADBASICCODE1 := CopyXMLBloc.ADBASICCODE1;
      CurentXMLBloc.ADBASICCODE2 := CopyXMLBloc.ADBASICCODE2;
      CurentXMLBloc.DELPHICODE1 := CopyXMLBloc.DELPHICODE1;
      CurentXMLBloc.DELPHICODE2 := CopyXMLBloc.DELPHICODE2;
      CurentXMLBloc.PARAM1 := CopyXMLBloc.PARAM1;
      CurentXMLBloc.PARAM2 := CopyXMLBloc.PARAM2;
      CurentXMLBloc.PARAM3 := CopyXMLBloc.PARAM3;
    end;
    //Copie de toute les fonctions
    for i := 0 to CopyXMLBloc.FONCTION.Count - 1 do
    begin
      AXMLFunction := CopyXMLBloc.FONCTION.Items[i].CloneNode(true) as IXMLFONCTIONType;
      CurentXMLBloc.FONCTION.ChildNodes.Add(AXMLFunction);
    end;
    //Copie de tous les blocs
    for i := 0 to CopyXMLBloc.BLOC.Count - 1 do
    begin
      AXMLBloc := CopyXMLBloc.BLOC.Items[i].CloneNode(true) as IXMLBLOCType;
      CurentXMLBloc.BLOC.ChildNodes.Add(AXMLBloc);
    end;
    //MAJ IHM
    CopyXMLBloc := nil;
    MAJBlocs;
  end;
end;

procedure TDescriptFrame.DefBlocActionExecute(Sender: TObject);
begin
  if DefinirBlocForm.ShowModal = mrOK then
  begin
    //Changement du code XML
    case DefinirBlocForm.TypeBlocComboBox.ItemIndex of
      0: begin
           CurentXMLBloc.GENRE := 'NORMAL';
           CurentXMLBloc.PARAM1 := '';
           CurentXMLBloc.PARAM2 := '';
           CurentXMLBloc.PARAM3 := '';
           CurentXMLBloc.MAJCodeADBasic_Pascal;
         end;
      1: begin
           CurentXMLBloc.GENRE := 'FOR';
           CurentXMLBloc.PARAM1 := DefinirBlocForm.FORComboBox.Text;
           CurentXMLBloc.PARAM2 := DefinirBlocForm.DELabeledEdit.Text;
           CurentXMLBloc.PARAM3 := DefinirBlocForm.AforLabeledEdit.Text;
           CurentXMLBloc.MAJCodeADBasic_Pascal;
         end;
      2: begin
           CurentXMLBloc.GENRE := 'DO';
           CurentXMLBloc.PARAM1 := DefinirBlocForm.FAIRETANTQUEComboBox.Text;
           CurentXMLBloc.PARAM2 := DefinirBlocForm.ESTdoComboBox.Text;
           CurentXMLBloc.PARAM3 := DefinirBlocForm.AdoLabeledEdit.Text;
           CurentXMLBloc.MAJCodeADBasic_Pascal;
         end;
      3: begin
           CurentXMLBloc.GENRE := 'IF';
           CurentXMLBloc.PARAM1 := DefinirBlocForm.SIComboBox.Text;
           CurentXMLBloc.PARAM2 := DefinirBlocForm.ESTifComboBox.Text;
           CurentXMLBloc.PARAM3 := DefinirBlocForm.AifLabeledEdit.Text;
           CurentXMLBloc.MAJCodeADBasic_Pascal;
         end;
    end;
  end;
  //Changement dans la vue
  CurentNodeBloc.Text := CurentXMLBloc.NOM;
end;
//Gestion des Blocs
//*****************

//*********************
//Gestion des fonctions
function TDescriptFrame.AjouterFonction(pXMLFunction:IXMLFonctionType):TActStimTrait;
var i,n : integer;
    finish : boolean;
begin
  n := -1;
  repeat
    inc(n);
    finish := true;
    for i := 0 to FunctionScrollBox.ControlCount - 1 do
    begin
      if FunctionScrollBox.Controls[i].Name = 'Fonction_'+inttostr(n)
      then finish := false;
    end;
  until finish;
  result :=
  TActStimTrait.Construct(FunctionScrollBox,n,pXMLFunction);
end;

procedure TDescriptFrame.AddActionActionExecute(Sender: TObject);
var NewFunction : IXMLFONCTIONType;
begin
  NewFunction := CurentXMLBloc.FONCTION.Add;
  NewFunction.GENRE := 'A';
  NewFunction.DUREE := 0.0;
  NewFunction.FICHIER := '';
  NewFunction.TITRE := 'Sans titre';
  NewFunction.AddChild('ADBASIC');
  NewFunction.AddChild('PASCAL');
  AjouterFonction(NewFunction);
end;

procedure TDescriptFrame.AddTraitementActionExecute(Sender: TObject);
var NewFunction : IXMLFONCTIONType;
begin
  NewFunction := CurentXMLBloc.FONCTION.Add;
  NewFunction.GENRE := 'T';
  NewFunction.DUREE := 0.0;
  NewFunction.FICHIER := '';
  NewFunction.TITRE := 'Sans titre';
  NewFunction.AddChild('ADBASIC');
  NewFunction.AddChild('PASCAL');
  AjouterFonction(NewFunction);
end;

procedure TDescriptFrame.AddStimulusActionExecute(Sender: TObject);
var NewFunction : IXMLFONCTIONType;
begin
  NewFunction := CurentXMLBloc.FONCTION.Add;
  NewFunction.GENRE := 'S';
  NewFunction.DUREE := 0.0;  
  NewFunction.FICHIER := '';
  NewFunction.TITRE := 'Sans titre';
  NewFunction.AddChild('ADBASIC');
  NewFunction.AddChild('PASCAL');
  AjouterFonction(NewFunction);
end;

procedure TDescriptFrame.SupprimerFonctionActionExecute(Sender: TObject);
begin
	CurentFct.DeleteFonction(Self);
end;

procedure TDescriptFrame.EditerFoncionActionExecute(Sender: TObject);
begin
	CurentFct.EditerFonction(Self);
end;

procedure TDescriptFrame.FunctionScrollBoxDragOver(Sender, Source: TObject;
  X, Y: Integer; State: TDragState; var Accept: Boolean);
begin
  if Source is TActStimTrait then
  begin
    DocoForm.Caption := (Source as TActStimTrait).Caption;
    Accept := true;
    
  end;
end;

procedure TDescriptFrame.FunctionScrollBoxDragDrop(Sender, Source: TObject;
  X, Y: Integer);
var IndexFonctionSource : integer;
    ListFunction : IXMLFonctionTypeList;
    NewFonction,SourceFonction : IXMLFonctionType;
begin
  if Source is TActStimTrait then
  begin
    (Source as TActStimTrait).Top := Y;
    //Deplacement de la balise XML correspondante
    //Recherche index de la fonction deplacée (Source)
    ListFunction := (Source as TActStimTrait).RefXMLFunction.Collection as IXMLFonctionTypeList;
    IndexFonctionSource := ListFunction.ChildNodes.IndexOf((Source as TActStimTrait).RefXMLFunction);
    SourceFonction := ListFunction.ChildNodes.Get(IndexFonctionSource) as IXMLFonctionType;
    NewFonction := SourceFonction.CloneNode(true) as IXMLFONCTIONType;;
    ListFunction.ChildNodes.Add(NewFonction);
    //Actualisation du pointeur
    (Source as TActStimTrait).RefXMLFunction := NewFonction;
    ListFunction.ChildNodes.Delete(IndexFonctionSource);
  end;
end;

procedure TDescriptFrame.DeleteFunctionTimerTimer(Sender: TObject);
var n : integer;
    TheActStimTrait : TActStimTrait;
begin
  DeleteFunctionTimer.Enabled := false;
  n := 0;
  repeat
    TheActStimTrait := FunctionScrollBox.Controls[n] as TActStimTrait;
    if TheActStimTrait.Delete
    then TheActStimTrait.Free
    else inc(n);
  until FunctionScrollBox.ControlCount = n;
  FunctionScrollBox.Visible := false;
  FunctionScrollBox.Visible := true;
end;

procedure TDescriptFrame.Panel1DragOver(Sender, Source: TObject; X,
  Y: Integer; State: TDragState; var Accept: Boolean);
begin
  FunctionScrollBox.VertScrollBar.Position :=
                               FunctionScrollBox.VertScrollBar.Position - 1;
end;

procedure TDescriptFrame.ChangeESClick(Sender: TObject);
begin
  CurentES.Caption := (Sender as TMenuItem).Caption;
  if CurentES.Genre = 'E'
  then CurentES.RefXMLEntree.NOM :=CurentES.Caption
	else begin
    CurentES.RefXMLSortie.NOM :=CurentES.Caption;
    CurentES.UpdateHorizontalPosition;
  end;
	CurentFct.MAJCodeADBASIC_PASCAL;
  DocDOCOMETRE.Resync;
end;

procedure TDescriptFrame.AffecterGeneriqueActionExecute(Sender: TObject);
begin
  CurentFct.RefXMLFunction.FICHIER := (Sender as TMenuItem).Hint;
  CurentFct.Hint := CurentFct.RefXMLFunction.FICHIER  ;

  CurentFct.EditerFonction(Sender);

end;

procedure TDescriptFrame.BlocsTreeViewDragOver(Sender, Source: TObject; X,
  Y: Integer; State: TDragState; var Accept: Boolean);
begin
  if Source is TActStimTrait then
  begin
    if (Source as TActStimTrait).RefXMLFunction.GENRE <> 'S'
    then Accept := true
    else Accept := false;
    //DocoForm.Caption := (Source as TActStimTrait).Caption;
  end;
end;

procedure TDescriptFrame.BlocsTreeViewDragDrop(Sender, Source: TObject; X,
  Y: Integer);
begin
  if Source is TActStimTrait then
  begin
    droppedActStimTrait := Source as TActStimTrait;
    DragDropTreeNode := BlocsTreeView.GetNodeAt(X, Y);
    DragDropPopupMenu.Popup(BlocsTreeView.ClientToScreen(Point(X,Y)).X,BlocsTreeView.ClientToScreen(Point(X,Y)).Y);
  end;
end;

procedure TDescriptFrame.DragDropDeplacerFonctionActionExecute(
  Sender: TObject);
var AXMLFunction : IXMLFonctionType;
    RefXMLBloc : TRefXMLBloc;
    n : integer;
begin
  //Dupliquer le noeud
  AXMLFunction := droppedActStimTrait.RefXMLFunction.CloneNode(true) as IXMLFonctionType;
  //Affecter le noeud dupliqué au noeud bloc courant
  RefXMLBloc := DragDropTreeNode.Data;
  RefXMLBloc.XMLBloc.FONCTION.ChildNodes.Add(AXMLFunction);
  //Supprimer le noeud d'oringine
  n := CurentXMLBloc.FONCTION.ChildNodes.IndexOf(droppedActStimTrait.RefXMLFunction);
  CurentXMLBloc.FONCTION.ChildNodes.Delete(n);
  BlocsTreeViewChange(Self,CurentNodeBloc);
end;

procedure TDescriptFrame.DragDropCopierFonctionActionExecute(
  Sender: TObject);
var AXMLFunction : IXMLFonctionType;
    RefXMLBloc : TRefXMLBloc;
begin
  //Dupliquer le noeud
  AXMLFunction := droppedActStimTrait.RefXMLFunction.CloneNode(true) as IXMLFonctionType;
  //Affecter le noeud dupliqué au noeud bloc courant
  RefXMLBloc := DragDropTreeNode.Data;
  RefXMLBloc.XMLBloc.FONCTION.ChildNodes.Add(AXMLFunction);
  BlocsTreeViewChange(Self,CurentNodeBloc);
end;

//Gestion des fonctions
//*********************

procedure TDescriptFrame.HelpImageClick(Sender: TObject);
begin
  DocoForm.ShowHelp((Sender as TImage).Tag);
end;

procedure TDescriptFrame.FonctionPopupMenuPopup(Sender: TObject);
var i : integer;
		NewMenuItem : TMenuItem;
begin

	Affecter.Enabled := false;
	SupprimerFonction.Enabled := false;
	EditerFonction.Enabled := false;
	AjouterStimulus.Enabled := false;
	AjouterTraitement.Enabled := false;
	AjouterAction.Enabled := false;

	if CurentNodeBloc <> nil then
	begin
    AjouterStimulus.Enabled := true;
		AjouterTraitement.Enabled := true;
		AjouterAction.Enabled := true;
	end;


	if CurentFct <> nil then
	begin
	
		Affecter.Enabled := true;
		SupprimerFonction.Enabled := true;
		EditerFonction.Enabled := true;
		AjouterStimulus.Enabled := true;
		AjouterTraitement.Enabled := true;
		AjouterAction.Enabled := true;


		while (Affecter.Count > 0) do
		begin
			NewMenuItem := Affecter.Items[0];
			NewMenuItem.Free;
		end;

		if CurentFct.RefXMLFunction.GENRE = 'A'	then
		begin
			for i := 0 to ActionPopupMenu.Items.Count - 1	do
			begin
				NewMenuItem := TMenuItem.Create(self);
				NewMenuItem.Name := ActionPopupMenu.Items[i].Name + 'Popup';
				NewMenuItem.Hint := ActionPopupMenu.Items[i].Hint;
				NewMenuItem.OnClick := AffecterGeneriqueActionExecute;
				NewMenuItem.Caption := ActionPopupMenu.Items[i].Caption;
				Affecter.Add(NewMenuItem);
			end;
		end;
		if CurentFct.RefXMLFunction.GENRE = 'T'	then
		begin
			for i := 0 to TraitementPopupMenu.Items.Count - 1	do
			begin
				NewMenuItem := TMenuItem.Create(self);
				NewMenuItem.Name := TraitementPopupMenu.Items[i].Name + 'Popup';
				NewMenuItem.Hint := TraitementPopupMenu.Items[i].Hint;
				NewMenuItem.OnClick := AffecterGeneriqueActionExecute;
				NewMenuItem.Caption := TraitementPopupMenu.Items[i].Caption;
				Affecter.Add(NewMenuItem);
			end;
		end;;
		if CurentFct.RefXMLFunction.GENRE = 'S'
		then Affecter.Enabled := false;
	end;
end;

procedure TDescriptFrame.FunctionScrollBoxMouseMove(Sender: TObject;
	Shift: TShiftState; X, Y: Integer);
begin
  if CurentFct <> nil then CurentFct.HideTitreEdit;
  CurentFct := nil;
end;

procedure TDescriptFrame.UpdateMainMenuExecute(Sender : TObject);
begin
	STPopupMenuPopup(self);
	BlocsPopupMenuPopup(self);
end;

procedure TDescriptFrame.GlobalVueSTActionExecute(Sender: TObject);
var i,j : integer;
    STVue : TFileStream;
    FirstBloc : IXMLBlocType;
    S,URL : string;

    procedure AddBlocsToGlobalView(BlocPere : IXMLBlocType);
    var k,l : integer;
        BlocFils : IXMLBlocType;
    begin
      for k := 0 to BlocPere.BLOC.Count - 1 do
      begin
        BlocFils := BlocPere.BLOC.Items[k];

        if BlocFils.GENRE = 'IF'
        then S := '<div id="BLOC"><img src="file:///' + rep_racine + 'Aide\Resume\Images\bloc_if.bmp"/>' + BlocFils.NOM + #13
        else if (BlocFils.GENRE = 'FOR') or (BlocFils.GENRE = 'DO')
             then S := '<div id="BLOC"><img src="file:///' + rep_racine + 'Aide\Resume\Images\bloc_for_do.bmp"/>' + BlocFils.NOM + #13
             else S := '<div id="BLOC">' + BlocFils.NOM + #13;
        STVue.Write(PChar(S)^,length(S));

        for l := 0 to BlocFils.FONCTION.Count - 1 do
        begin
          S := '<div id="FONCTION">';
          if BlocFils.FONCTION[l].GENRE = 'A'
          then S := S + '<img src="file:///' + rep_racine + 'Aide\Resume\Images\Action.bmp"/>';
          if BlocFils.FONCTION[l].GENRE = 'S'
          then S := S + '<img src="file:///' + rep_racine + 'Aide\Resume\Images\Stimulus.bmp"/>';
          if BlocFils.FONCTION[l].GENRE = 'T'
          then S := S + '<img src="file:///' + rep_racine + 'Aide\Resume\Images\Traitement.bmp"/>';
          S := S + BlocFils.FONCTION[l].TITRE + '</div>'#13;
          STVue.Write(PChar(S)^,length(S));
        end;

        AddBlocsToGlobalView(BlocFils);

        S := '</div>'#13;
        STVue.Write(PChar(S)^,length(S));
      end;
    end;

begin
  deleteFile(rep_manip + CurentXMLST.NOM + '.html');
  STVue := TFileStream.Create(rep_manip + CurentXMLST.NOM + '.html',fmCreate);

  S := '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">'#13;
  STVue.Write(PChar(S)^,length(S));
  S := '<html>'#13;
  STVue.Write(PChar(S)^,length(S));
  S := '<head>'#13;
  STVue.Write(PChar(S)^,length(S));
  S := '<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">'#13;
  STVue.Write(PChar(S)^,length(S));
  S := '<title>Séquence type "' + CurentXMLST.NOM + '"</title>'#13;
  STVue.Write(PChar(S)^,length(S));
  S := '<link rel="stylesheet" type="text/css" href="file:///' + rep_racine + 'Aide\Resume\vueGlobaleST.css" />'#13;
  STVue.Write(PChar(S)^,length(S));
  S := '</head>'#13;
  STVue.Write(PChar(S)^,length(S));
  S := '<body>'#13;
  STVue.Write(PChar(S)^,length(S));

  S := '<h1> Expérience : ' + DocDOCOMETRE.EXPERIENCE.NOM + ' - Séquence type : ' + CurentXMLST.NOM + '</h1>';
  STVue.Write(PChar(S)^,length(S));
  for i := 0 to CurentXMLST.BLOC.Count - 1 do
	begin
	 	FirstBloc := CurentXMLST.BLOC.Items[i];

    if FirstBloc.NOM = 'BLOC - DEBUT'
    then S := '<div id="BLOC"><img src="file:///' + rep_racine + 'Aide\Resume\Images\bloc_debut.bmp"/>' + FirstBloc.NOM + #13
    else if FirstBloc.NOM = 'BLOC - FIN'
         then S := '<div id="BLOC"><img src="file:///' + rep_racine + 'Aide\Resume\Images\bloc_fin.bmp"/>' + FirstBloc.NOM + #13
         else if FirstBloc.GENRE = 'IF'
              then S := '<div id="BLOC"><img src="file:///' + rep_racine + 'Aide\Resume\Images\bloc_if.bmp"/>' + FirstBloc.NOM + #13
              else if (FirstBloc.GENRE = 'FOR') or (FirstBloc.GENRE = 'DO')
                   then S := '<div id="BLOC"><img src="file:///' + rep_racine + 'Aide\Resume\Images\bloc_for_do.bmp"/>' + FirstBloc.NOM + #13
                   else S := '<div id="BLOC">' + FirstBloc.NOM + #13;
                   
    STVue.Write(PChar(S)^,length(S));
    for j := 0 to FirstBloc.FONCTION.Count - 1 do
    begin
      S := '<div id="FONCTION">' + FirstBloc.FONCTION[j].TITRE + '</div>'#13;
      STVue.Write(PChar(S)^,length(S));
    end;
		AddBlocsToGlobalView(FirstBloc);
    S := '</div>'#13;
    STVue.Write(PChar(S)^,length(S));
  end;

  S := '</body>'#13;
  STVue.Write(PChar(S)^,length(S));
  S := '</html>'#13;
  STVue.Write(PChar(S)^,length(S));

  STVue.Free;

  URL := 'file:///'+rep_manip  + CurentXMLST.NOM + '.html';
  ShellExecute(GetDesktopWindow(), 'open',PChar(URL), nil, nil, SW_SHOWNORMAL);

end;

procedure TDescriptFrame.BlocsTreeViewStartDrag(Sender: TObject;
  var DragObject: TDragObject);
begin
  if Sender is TActStimTrait then
  begin
    DocoForm.Caption := (Sender as TActStimTrait).Caption;
  end;
end;

procedure TDescriptFrame.FunctionScrollBoxMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var Pt : TPoint;
begin
	if Button = mbRight then
	begin
	  Pt.X := X;
		Pt.Y := Y;
		Pt := FunctionScrollBox.ClientToScreen(Point(X,Y));
    FonctionPopupMenu.Popup(Pt.X,Pt.Y);
  end;
end;

end.
