function ed_res(mat_in, NE, op, stage)

%   Copyright 2003
%   Centre for Nonlinear Dynamics in Physiology and Medicine

% op : 0 = etat  1 = stim  2 = discon 3=cell to trace

global cmap etat_ass
global liste_stim liste_disc h_main
[NR, NC] = size(mat_in);

frac = 1.0;
%frac = 0.9;
dcoth = ((1/1) * frac) / 2; % demi-coté horiz.
dcotv = ((1/1) * frac) / 2;

LFIG = 600;
HFIG = 640;

figure('unit','norm','pos', [.15 .15 .7 HFIG/LFIG*.7]);
set(gcf, 'user', [NE op stage]);

if(op == 0)
  str = ['Lattice state'];
elseif(op == 1)
  str = ['Stimulated cells, stage ', num2str(stage)];
elseif(op == 2)
  str = ['Disconnected cells, stage ', num2str(stage)];
elseif(op == 3)
  str = ['choose cells to trace'];
else
  error('Dans ed_res.m, op invalide');
end

set(gcf, 'numbertitle', 'off', 'name', str);
ha = axes('units', 'norm', 'pos', [0, 1 - LFIG/HFIG, 1, LFIG/HFIG]);
axis off
set(ha, 'xlim', [0.5, NR + 0.5], 'ylim', [ 0.5, NC + 0.5]);

% Pré-calcul des patches
X = zeros(NR, 4);
Y = zeros(NC, 4);


for r = 1:NR
  for c = 1:NC
    Y(c, :) = [c-dcoth, c+dcoth, c+dcoth, c-dcoth];
    X(r, :) = [ r-dcotv, r-dcotv, r+dcotv, r+dcotv];
  end
end

if(op==0)
posx=0.4;
largx=.6;
else
posx=0.8;
largx=.2;
end;

if(op ==0)
uicontrol('style', 'push', 'units', 'norm',...
	'pos', [posx, 0, largx, 1 - LFIG/HFIG],...
	'string', 'OK', 'user', [],'min',0,'max',1,...
	'call', ['lire_ed;delete(gcf);'...
          'll=findobj(h_main,''type'',''patch'');'...
          'for i=1:length(ll),'...
          'set(ll(i),''Cdata'', etat( get( ll(i),''Userdata'') )+1);end;end;'])

else

uicontrol('style', 'push', 'units', 'norm',...
	'pos', [posx, 0, largx, 1 - LFIG/HFIG],...
	'string', 'OK', 'user', [],'min',0,'max',1,...
	'call', ['lire_ed;delete(gcf);']);
end

if(NE == 2)

if(op==0)
largx=.4;
else
largx=.3;
end;


  str = 'Left mouse to toggle';
  uicontrol('style', 'text', 'units', 'norm',...
		 'pos', [0.0, 0, 0.4, 1 - LFIG/HFIG], 'string', str, 'user', []);

if(op==1 | op==2 |op == 3)

uicontrol('style', 'push', 'units', 'norm',...
	'pos', [.3, 0, .2, 1 - LFIG/HFIG],...
	'string', 'reset', 'user', [],'min',0,'max',1,...
	'call', ['hl=findobj(gcf,''type'',''patch'');',...
	'for i=1:length(hl),if(get(hl(i), ''cdata'') == 1)',...
        'set(hl(i), ''cdata'', 2);rc = get(hl(i), ''user'');'...
        'rc(3)=1;set(hl(i), ''user'', rc);end;end;']);

if(stage > 1)

uicontrol('style', 'push', 'units', 'norm',...
	'pos', [.5, 0, .3, 1 - LFIG/HFIG],...
	'string', 'set to previous stage', 'user',[stage op],'min',0,'max',1,...
	'call', ['hl=findobj(gcf,''type'',''patch'');',...
	'for i=1:length(hl),if(get(hl(i), ''cdata'') == 1)',...
        'set(hl(i), ''cdata'', 2);rc = get(hl(i), ''user'');'...
        'rc(3)=1;set(hl(i), ''user'', rc);end;end;'...
         '[st]=get(gco,''user'');op=st(2);st=st(1);'...
         'if(op ==1)cible=liste_stim;else cible=liste_disc;end;'...
         'if(~isempty(cible))',...
	'ii=find(cible(3,:)==st-1);if(length(ii) > 0)',...
         'for i=1:length(ii),for j=1:length(hl),rc = get(hl(j), ''user'');',...
	 'if(rc(1)==cible(1,ii(i)) & rc(2)==cible(2,ii(i))) rc = get(hl(j), ''user'');',...
         'rc(3)=0;set(hl(j), ''user'', rc);set(hl(j), ''cdata'', 1);end;end;end;end;end;'])


end



end;

  cmap = [0 0 0; 1 1 1];
  colormap(cmap);
  caxis([1 2]);
  for r = 1:NR
    for c = 1:NC
      patch(X(r, :), Y(c, :), mat_in(r, c) + 1, 'user', [r c mat_in(r, c)],...
		 'edgecolor',[0 0 0],'erasemode','none','ButtonDownFcn', 'ed_toggl');
    end
  end

elseif(NE > 2) 
 cmap = hot(NE);
 colormap(cmap); 
  uicontrol(gcf,'style', 'edit', 'min', NE, 'units', 'norm','backgroundcolor',[1 1 1],...
	'pos', [0.3, 0, 0.1, 1 - LFIG/HFIG],...
	'string', num2str(etat_ass),'user', [],'max',NE-1,...
	'call', ['etat_ass = min([str2num(get(gco,''string'')) get(gco,''max'')]);'...
        'set(gco,''string'',num2str(etat_ass));']);

  str = ['states  (0 to ', num2str(NE - 1), ')'];
  uicontrol(gcf,'style', 'text', 'units', 'norm',...
		 'pos', [0.0, 0, 0.3, 1 - LFIG/HFIG], 'string', str, 'user', []);


 
  caxis([1 NE]);
  for r = 1:NR
    for c = 1:NC
      patch(X(r, :), Y(c, :), cmap(mat_in(r, c)+1,:), 'user', [r c mat_in(r, c)],...
	'facecolor',cmap(mat_in(r, c)+1,:),'edgecolor',[0 0 0],...
        'erasemode','none','ButtonDownFcn', 'ed_ass(etat_ass);');
    end
  end


else
  error('NE invalide dans ed_res.m');
end


