

//////////////////////////////////////
function imageshow(image,map,windowID)
xset("window",windowID);
xbasc();
imshow(image,map);
endfunction
//////////////////////////////////////

//////////////////////////////////////
function imtrans(image,map,head,windowID)
global image0 image1 image2 image3
global map0 map1 map2 map3
global head0 head1 head2 head3
select windowID
	case 0 then image0=image;map0=map;head0=head;
	case 1 then image1=image;map1=map;head1=head;
	case 2 then image2=image;map2=map;head2=head;
	case 3 then image3=image;map3=map;head3=head;
end
endfunction
//////////////////////////////////////

//////////////////////////////////////
function undostate(windowID,state)
select windowID
	case 0 then TK_SetVar('imScilab(undostate0)',state);
	case 1 then TK_SetVar('imScilab(undostate1)',state);
	case 2 then TK_SetVar('imScilab(undostate2)',state);
	case 3 then TK_SetVar('imScilab(undostate3)',state);
end
endfunction

function redostate(windowID,state)
select windowID
	case 0 then TK_SetVar('imScilab(redostate0)','state');
	case 1 then TK_SetVar('imScilab(redostate1)','state');
	case 2 then TK_SetVar('imScilab(redostate2)','state');
	case 3 then TK_SetVar('imScilab(redostate3)','state');
end
endfunction
//////////////////////////////////////

/////////////////////////////////////
function imagePush(windowID,changeID)
global image0 image1 image2 image3 map0 map1 map2 map3
global st0 st1 st2 st3 mark0 mark1 mark2 mark3 head0 head1 head2 head3
global rear0 rear1 rear2 rear3 top0 top1 top2 top3
global stsize

select windowID
	case 0 then head=head0;rear=rear0;top=top0;
	case 1 then head=head1;rear=rear1;top=top1;
	case 2 then head=head2;rear=rear2;top=top2;
	case 3 then head=head3;rear=rear3;top=top3;
end

if head==stsize then head=1
else head=head+1;end
if head==rear then
	if rear==stsize then rear=1
	else rear=rear+2;end
end
//st(head)=image;
select windowID
	case 0 then st0(head)=image0;
	case 1 then st1(head)=image1;
	case 2 then st2(head)=image2;
	case 3 then st3(head)=image3;
end

if head==stsize then head=1
else head=head+1;end
if head==rear then
	if rear==stsize then rear=1
	else rear=rear+1;end
end
select windowID
	case 0 then st0(head)=map0;
	case 1 then st1(head)=map1;
	case 2 then st2(head)=map2;
	case 3 then st3(head)=map3;
end

top=head;

select windowID
	case 0 then rear0=rear;top0=top;head0=head;mark0(changeID)=head;
	case 1 then rear1=rear;top1=top;head1=head;mark1(changeID)=head;
	case 2 then rear2=rear;top2=top;head2=head;mark2(changeID)=head;
	case 3 then rear3=rear;top3=top;head3=head;mark3(changeID)=head;
end

if head>2 then TK_EvalStr('.main.fra17.fra17.btnundo configure -state normal');
select windowID
	case 0 then TK_EvalStr('set imScilab(undostate0) normal');
	case 1 then TK_EvalStr('set imScilab(undostate1) normal');
	case 2 then TK_EvalStr('set imScilab(undostate2) normal');
	case 3 then TK_EvalStr('set imScilab(undostate3) normal');
end;
else TK_EvalStr('.main.fra17.fra17.btnundo configure -state disable');
select windowID
	case 0 then TK_EvalStr('set imScilab(undostate0) disable');
	case 1 then TK_EvalStr('set imScilab(undostate1) disable');
	case 2 then TK_EvalStr('set imScilab(undostate2) disable');
	case 3 then TK_EvalStr('set imScilab(undostate3) disable');
end;
end
TK_EvalStr('.main.fra17.fra17.btnredo configure -state disable');
select windowID
	case 0 then TK_EvalStr('set imScilab(redostate0) disable');
	case 1 then TK_EvalStr('set imScilab(redostate1) disable');
	case 2 then TK_EvalStr('set imScilab(redostate2) disable');
	case 3 then TK_EvalStr('set imScilab(redostate3) disable');
end;

endfunction


/////////////////////////////////////


/////////////////////////////////////
function imageUndo_Son(image,map,head,rear,top,st,mark,windowID,stackID)

head=mark(stackID);
map=st(head);
head=head-1;image=st(head);
imageshow(image,map,windowID);
imtrans(image,map,head+1,windowID);
TK_EvalStr('.main.fra17.fra17.btnundo configure -state normal');
TK_EvalStr('.main.fra17.fra17.btnredo configure -state normal');

if head==1 then
TK_EvalStr('.main.fra17.fra17.btnundo configure -state disable');
select windowID
	case 0 then TK_EvalStr('set imScilab(undostate0) disable');
	case 1 then TK_EvalStr('set imScilab(undostate1) disable');
	case 2 then TK_EvalStr('set imScilab(undostate2) disable');
	case 3 then TK_EvalStr('set imScilab(undostate3) disable');
end;
end
TK_EvalStr('.main.fra17.fra17.btnredo configure -state normal');
select windowID
	case 0 then TK_EvalStr('set imScilab(undostate0) normal');
	case 1 then TK_EvalStr('set imScilab(undostate1) normal');
	case 2 then TK_EvalStr('set imScilab(undostate2) normal');
	case 3 then TK_EvalStr('set imScilab(undostate3) normal');
end;

endfunction
//////////////////////////////////////

/////////////////////////////////////
function imageRedo_Son(image,map,head,rear,top,st,mark,windowID,stackID)
	
head=mark(stackID)+1;
image=st(head);
head=head+1;map=st(head);
imageshow(image,map,windowID);
imtrans(image,map,head,windowID);
TK_EvalStr('.main.fra17.fra17.btnundo configure -state normal');
	
if head==top then
TK_EvalStr('.main.fra17.fra17.btnredo configure -state disable');
select windowID
	case 0 then TK_EvalStr('set imScilab(undostate0) disable');
	case 1 then TK_EvalStr('set imScilab(undostate1) disable');
	case 2 then TK_EvalStr('set imScilab(undostate2) disable');
	case 3 then TK_EvalStr('set imScilab(undostate3) disable');
end;
end
TK_EvalStr('.main.fra17.fra17.btnundo configure -state normal');
select windowID
	case 0 then TK_EvalStr('set imScilab(undostate0) normal');
	case 1 then TK_EvalStr('set imScilab(undostate1) normal');
	case 2 then TK_EvalStr('set imScilab(undostate2) normal');
	case 3 then TK_EvalStr('set imScilab(undostate3) normal');
end;

endfunction
////////////////////////////////////

///////////////////////////////////
function imageUndo(windowID,stackID)
global st0 st1 st2 st3 mark0 mark1 mark2 mark3 head0 head1 head2 head3 
global rear0 rear1 rear2 rear3 top0 top1 top2 top3
global image0 image1 image2 image3
global map0 map1 map2 map3

select windowID
	case 0 then imageUndo_Son(image0,map0,head0,rear0,top0,st0,mark0,windowID,stackID)
	case 1 then imageUndo_Son(image1,map1,head1,rear1,top1,st1,mark1,windowID,stackID)
	case 2 then imageUndo_Son(image2,map2,head2,rear2,top2,st2,mark2,windowID,stackID)
	case 3 then imageUndo_Son(image3,map3,head3,rear3,top3,st3,mark3,windowID,stackID)
end
endfunction
///////////////////////////////////

///////////////////////////////////
function imageRedo(windowID,stackID)
global st0 st1 st2 st3 mark0 mark1 mark2 mark3 head0 head1 head2 head3 
global rear0 rear1 rear2 rear3 top0 top1 top2 top3
global image0 image1 image2 image3
global map0 map1 map2 map3

select windowID
	case 0 then imageRedo_Son(image0,map0,head0,rear0,top0,st0,mark0,windowID,stackID)
	case 1 then imageRedo_Son(image1,map1,head1,rear1,top1,st1,mark1,windowID,stackID)
	case 2 then imageRedo_Son(image2,map2,head2,rear2,top2,st2,mark2,windowID,stackID)
	case 3 then imageRedo_Son(image3,map3,head3,rear3,top3,st3,mark3,windowID,stackID)
end
endfunction
///////////////////////////////////