function affichevol(action,varargin)
% main function with all callback form the interface
% the code is split in different file
% Coupeprog (always call to update the rigth display
% Graphprog  (basic graph fonction +orientation
% Posprog (handel of ROI)
%
% Decoursprog and Segprog in construction (it is curently discarded)
% initfigvol is the gui setup
% draw remplie are subfonction for drawing roi
% 
% sorry for this code which will always be in construction ...
% with few information 
% 
% For bug or more information please contact romain.valabregue@snv.jussieu.fr 
% romain valabregue le 01/10/2001

         
global Result_path Data_path

persistent  Volume  nb_vol 

if nargin == 0 

  if ~isempty(gcbf) & strcmp(get(gcbf,'tag'),'affichevol')
	FigNum = gcbf
	hdl = get(FigNum,'UserData');
	titre = get(hdl.vol_list,'string');
  end

  if isempty(nb_vol), nb_vol = 1; else,  nb_vol = nb_vol+1;  end
  Num_vol = nb_vol;
  Data_path  =pwd;  Result_path = pwd;

 P = spm_get([Inf],'.img',{['select images']},Data_path);
if isempty(P)
  mars = spm('getglobal', 'MARSBAR');
  P = {mars.structural.fname};
end

  Vol = spm_vol(P);
  Volume(Num_vol).Vol = Vol{1};
  Volume(Num_vol).M = Volume(Num_vol).Vol.mat;
  Volume(Num_vol).M_base = bare_head(Volume(Num_vol).Vol.fname);
  Volume(Num_vol).M_rot  = [0 1 0 0;1 0 0 0;0 0 1 0;0 0 0 1];

  titre{Num_vol} = Volume(Num_vol).Vol.descrip;  Volume(Num_vol).titre = titre{Num_vol};

  if isempty(gcbf) | ~strcmp(get(gcbf,'tag'),'affichevol')

    M_slice = spm_matrix([0 0 1]);
    slice = (spm_slice_vol(Volume(Num_vol).Vol,M_slice,Volume(Num_vol).Vol.dim(1:2),0))';

  	FigNum = initfigvol(slice,Num_vol, Volume(Num_vol).Vol.dim(3)); 
	hdl = get(FigNum,'UserData');
        set(FigNum,'name',titre{Num_vol})
  end
  

end
          
if nargin == 3
  %for a direct call without the spm_get

  Num_vol = action;
  Volume(action).data  = varargin{1};
  Volume(action).titre = strcat(varargin{2});
   
  initfigvol(varargin{1},Num_vol); 
end
  
if nargin == 3 | nargin == 0,

  Volume(Num_vol).coupe = 1 ;
  Volume(Num_vol).Pos =[];
  Volume(Num_vol).Tpos  = [];
  Volume(Num_vol).seuil = 3;
  Volume(Num_vol).Bw  = []; 
  Volume(Num_vol).Cont = [];
  
  Volume(Num_vol).stackofmaps = colormap;
  Volume(Num_vol).lengthofmap = size(Volume(Num_vol).stackofmaps,1);
        
%  set(gcf,'name',Volume(Num_vol).titre);

elseif  nargin == 2 | nargin == 1,
    
   if isempty(gcbf)
	FigNum = gcf;
   else
	FigNum = gcbf;
   end
   
   hdl = get(FigNum,'UserData');
         
   if isfield(hdl,'origin')
       FigNum = hdl.origin;
       hdl = get(FigNum,'UserData');
       figure(FigNum) 
    end
    Num_vol = hdl.Num;

   Pos     = Volume(hdl.Num).Pos;
   Tpos    = Volume(hdl.Num).Tpos;
   seuil   = Volume(hdl.Num).seuil;
   Bw      = Volume(hdl.Num).Bw;
   Cont    = Volume(hdl.Num).Cont;


   NbLigne = Volume(Num_vol).Vol.dim(1);
   NbCoupe = Volume(Num_vol).Vol.dim(3);
   coupe   = Volume(hdl.Num).coupe;
   if isfield(Volume(hdl.Num),'Clus_Pos'), Clus_Pos = Volume(hdl.Num).Clus_Pos; end

 
if nargin == 1
	
else
   TypeAction = action;
   action = varargin{1};
   
   switch TypeAction
   case 1
      byby=0;
      Graphprog   
      Volume(hdl.Num).coupe = coupe;
      if  byby
         return
      end
       
   case 2      
      Segprog
      Volume(hdl.Num).Bw  = Bw;

   case 3
      Posprog
      Volume(hdl.Num).Pos =Pos;

   case 5
      Decoursprog
   end
end     

Coupeprog   
   
end        

   

     