// Filename:    ROI.OGS
// Creation:    by CPY 10/1/01
// Purpose:     Region of Interests Tools script
// Modifications:

[CheckIsMatrix]
	if(exist(%H) != 5)
	{
		type -b "Only support operation on matrix";
		return 1;
	}
	return 0;

// rect region
[Copy]
	if(run.Section(,CheckIsMatrix))
		return 1;

	if( 0 == @ROI )
		run.Section(, OldCopy);
	else
		run.Section(, NewCopy);
	return 0;

[OldCopy]
	matrix -ib ROI.x1 ROI.y1 ROI.x3 ROI.y3;
	return 0;

[NewCopy]
	if(run.Section(,AttentionBox))
		return 1;

	Copy ROI %H;
	return 0;
	
[AttentionBox]
	break.style=1;
	break.open("Loading and compiling Origin C function...");
	// now attempt to load and compile function
	if(run.LoadOC("%YOriginC\OriginLab\ROIProcessing.c") != 0)
	{
	 // report if failure
	    break.close();
	    type -b "Error trying to load and compile Origin C file 'ROIProcessing.c";      
	    return 1;
	 }
	// close attention box
	break.close();
	return 0;

[OldCrop]
	matrix -ic ROI.x1 ROI.y1 ROI.x3 ROI.y3;
	label -r ROI;
	return 0;

[NewCrop]
	if(run.Section(,AttentionBox))
		return 1;

	Crop ROI %H;
	return 0;
[OldCreateNew]
	%W = %H;
	left = ROI.x1;
	top = ROI.y1;
	right = ROI.x3;
	bottom = ROI.y3;
	%Z=page.label$;// need to copy this too

	win -t matrix;
	matrix -ic left top right bottom %W;
	
	del -v top;
	del -v left;
	del -v right;
	del -v bottom;

	// also copy the name
	if(%Z!="")
		%Z = "%Z (copy)";
	else
		%Z = "copy of %W";

	page.label$="%Z";
	page.title = 3;// to show both name and label
	return 0;

[NewCreateNew]
	if(run.Section(,AttentionBox))
		return 1;
		
	CreateNew ROI %H;
	return 0;

[Crop]
	if(run.Section(,CheckIsMatrix))
		return 1;

	if( 0 == @ROI )
		run.Section(, OldCrop);
	else
		run.Section(, NewCrop);
	return 0;

[CreateNew]
	if(run.Section(,CheckIsMatrix))
		return 1;
	if( 0 == @ROI )
		run.Section(, OldCreateNew);
	else
		run.Section(, NewCreateNew);
	return 0;