// Filename:	TemplateLibrary.OGS
// Creation:	by Scott Bertrand, 12/15/01
// Purpose:	Script to operate Template Library
//**********************************************
// Modification Log:
// CPY 8/25/03 PLOT_SETUP_DLG_USE_NEW_SMART_LEGEND
//**********************************************

[OnInit]
	ini.get.filename$=template.ini;
	ini.getsection(categories);
	loop(ii,2,ini.keynum)
	{
		%A=ini.keyvalue$(ii)$;
		TemplateLibrary!Categories.v$(ii-1)$=%A;
	}
	TemplateLibrary!Categories.choice=1;
	run.section(,FillFunctions,1);
	return 0;

[Templates.OnSelChange]
	ini.file$=template.ini;
	%A=TemplateLibrary!Categories.s1$;
	ini.getsection(%A);
	%B=TemplateLibrary!Templates.s1$;
	TemplateLibrary!TemEdit.v1$=%B.otp;
	ini.getstr(W,,%B,%A,template.ini);
	if(run.LoadOC("OriginLab\TemplateLibrary",4)!=0)	
	{
		run.section(DBRoutines.OGS,ReportError,$Regression.CFileNoLoad);
		return 1;
	}
	%A=system.path.program$;
	if(%[%W]<=1)
		%W=%A;
	run.section(DBRoutines,SaveAndSetAtOC);
	TemplateLibrary!NoPreview.show=0;
	if(OpenTemplate(%W%B))
	{
		TemplateLibrary!BMP.display(%Ytemplatepict.bmp);
	}
	else
	{
		if(exist(%ATemplates\Previews\%B.bmp)==-1)
		{
			TemplateLibrary!BMP.display();
			TemplateLibrary!NoPreview.show=1;
		}
		else
		{
			TemplateLibrary!BMP.display(%ATemplates\Previews\%B.bmp)
		}
	}
	run.section(DBRoutines,RestoreAtOC);
	TemplateLibrary!Templates.setfocus();
	return 0;	

[Categories.OnSelChange]
	ini.file$=template.ini;
	%A=TemplateLibrary!Categories.s1$;
	ini.getsection(%A);
	run.section(,FillFunctions,1);
	%B=TemplateLibrary!Templates.s1$;
	TemplateLibrary!TemEdit.v1$=%B.otp;
	if(run.LoadOC("OriginLab\TemplateLibrary",4)!=0)	
	{
		run.section(DBRoutines.OGS,ReportError,$Regression.CFileNoLoad);
		return 1;
	}
	run.section(DBRoutines,SaveAndSetAtOC);
	TemplateLibrary!NoPreview.show=0;
	%A=system.path.program$;
	if(%[%W]<=1)
		%W=%A;
	if(OpenTemplate(%W%B))
	{
		TemplateLibrary!BMP.display(%Ytemplatepict.bmp);
	}
	else
	{
		if(exist(%ATemplates\Previews\%B.bmp)==-1)
		{
			TemplateLibrary!BMP.display();
			TemplateLibrary!NoPreview.show=1;
		}
		else
		{
			TemplateLibrary!BMP.display(%ATemplates\Previews\%B.bmp)
		}
	}
	run.section(DBRoutines,RestoreAtOC);
	TemplateLibrary!Categories.setfocus();
	return 0;

[Scroll.Left.OnClick]
	ini.get.filename$=template.ini;
	%A=TemplateLibrary!Categories.v$(TemplateLibrary!Categories.Choice)$;
	ini.getsection(%A);
	if(TemplateLibrary!Templates.Choice==1)
	{
		if(TemplateLibrary!Categories.Choice==1)
			TemplateLibrary!Categories.Choice=TemplateLibrary!Categories.NumItems;
		else
			TemplateLibrary!Categories.Choice-=1;
		TemplateLibrary!Templates.Choice=TemplateLibrary!Templates.NumItems;
		run.section(,FillFunctions,2);
	}
	else
	{
		%B=TemplateLibrary!Templates.v$(TemplateLibrary!Templates.Choice-1)$;
		TemplateLibrary!Templates.Choice-=1;
		TemplateLibrary!TemEdit.v1$=%B.otp;
		if(run.LoadOC("OriginLab\TemplateLibrary",4)!=0)	
		{
			run.section(DBRoutines.OGS,ReportError,$Regression.CFileNoLoad);
			return 1;
		}
		run.section(DBRoutines,SaveAndSetAtOC);
		ini.getstr(W,,%B,%A,template.ini);
		%A=system.path.program$;
		if(%[%W]<=1)
			%W=%A;
		TemplateLibrary!NoPreview.show=0;
		if(OpenTemplate(%W%B))
		{
			TemplateLibrary!BMP.display(%Ytemplatepict.bmp);
		}
		else
		{
			if(exist(%ATemplates\Previews\%B.bmp)==-1)
			{
				TemplateLibrary!BMP.display();
				TemplateLibrary!NoPreview.show=1;
			}
			else
			{
				TemplateLibrary!BMP.display(%ATemplates\Previews\%B.bmp)
			}
		}
		run.section(DBRoutines,RestoreAtOC);
	}
	TemplateLibrary!Templates.setfocus();
	return 0;	

[Scroll.Right.OnClick]
	ini.get.filename$=template.ini;
	%A=TemplateLibrary!Categories.v$(TemplateLibrary!Categories.Choice)$;
	ini.getsection(%A);
	if(TemplateLibrary!Templates.Choice==TemplateLibrary!Templates.NumItems)
	{
		if(TemplateLibrary!Categories.Choice==TemplateLibrary!Categories.NumItems)
			TemplateLibrary!Categories.Choice=1;
		else
			TemplateLibrary!Categories.Choice+=1;
		TemplateLibrary!Templates.Choice=1;
		run.section(,FillFunctions,1);
	}
	else
	{
		%B=TemplateLibrary!Templates.v$(TemplateLibrary!Templates.Choice+1)$;
		TemplateLibrary!Templates.Choice+=1;
		TemplateLibrary!TemEdit.v1$=%B.otp;
		if(run.LoadOC("OriginLab\TemplateLibrary",4)!=0)	
		{
			run.section(DBRoutines.OGS,ReportError,$Regression.CFileNoLoad);
			return 1;
		}
		run.section(DBRoutines,SaveAndSetAtOC);
		ini.getstr(W,,%B,%A,template.ini);
		%A=system.path.program$;
		if(%[%W]<=1)
			%W=%A;
		TemplateLibrary!NoPreview.show=0;
		if(OpenTemplate(%W%B.otp))
		{
			TemplateLibrary!BMP.display(%Ytemplatepict.bmp);
		}
		else
		{
			if(exist(%ATemplates\Previews\%B.bmp)==-1)
			{
				TemplateLibrary!BMP.display();
				TemplateLibrary!NoPreview.show=1;
			}
			else
			{
				TemplateLibrary!BMP.display(%ATemplates\Previews\%B.bmp)
			}
		}
		run.section(DBRoutines,RestoreAtOC);
	}
	TemplateLibrary!Templates.setfocus();
	return 0;
	
[FillFunctions]	
	TemplateLibrary!Templates.reset();
	ini.get.filename$=template.ini;
	%A=TemplateLibrary!Categories.s1$;
	ini.getsection(%A);
	TemplateLibrary!CatEdit.v1$="%A";
	loop(ii,1,ini.keynum)
	{
		%B=ini.keyname$(ii)$;
		TemplateLibrary!Templates.v$(ii)$=%B;
	}
	if(%1==1)
		ii=1;
	else
		ii=TemplateLibrary!Templates.NumItems;
	%K=ini.keyname$(ii)$;
	TemplateLibrary!TemEdit.v1$=%K.otp;
	TemplateLibrary!Templates.Choice=ii;

	if(run.LoadOC("OriginLab\TemplateLibrary",4)!=0)	
	{
		run.section(DBRoutines.OGS,ReportError,$Regression.CFileNoLoad);
		return 1;
	}
	run.section(DBRoutines,SaveAndSetAtOC);
	ini.getstr(W,,%K,%A,template.ini);
	%A=system.path.program$;
	if(%[%W]<=1)
		%W=%A;
	TemplateLibrary!NoPreview.show=0;

	if(OpenTemplate(%W%K))
	{
		TemplateLibrary!BMP.display(%Ytemplatepict.bmp);
	}
	else
	{
		if(exist(%ATemplates\Previews\%K.bmp)==-1)
		{
			TemplateLibrary!BMP.display();
			TemplateLibrary!NoPreview.show=1;
		}
		else
		{
			TemplateLibrary!BMP.display(%ATemplates\Previews\%K.bmp);
		}
	}
	run.section(DBRoutines,RestoreAtOC);
	return 0;

[CategoryNew.OnClick]
	getstring
	$TemplateLibrary.NameCategory $TemplateLibrary.CategoryName $TemplateLibrary.AddCategory; // ask for new category
	run.section(,AddCategory, "%B");
	TemplateLibrary!Categories.Choice=TemplateLibrary!Categories.NumItems;
	TemplateLibrary!Templates.reset();
	return 0;

[CategoryEdit.OnClick]
	%A=TemplateLibrary!Categories.s1$; // get selected category name	
	getstring
	$TemplateLibrary.NameCategory (%A) $TemplateLibrary.AddCategory; // ask for new category
	if(run.section(,AddCategory, "%B")==1)
		break;
	ini.file$=template.ini;
	ini.getsection(%A);
	loop(ii,1,ini.keynum)
	{
		%A=ini.keyname$(ii)$;
		%W=ini.keyvalue$(ii)$;
		if(%[%W]<=1)
			%W=0;
		ini.setstr(%W,%A,%B,Template.ini);
	}
	jj=TemplateLibrary!Categories.Choice;
	%A=TemplateLibrary!Categories.s1$; // get selected category name	
	run.section(,DeleteCategory, "%A" $(jj));
	TemplateLibrary!Categories.del(jj);
	TemplateLibrary!Categories.Choice=TemplateLibrary!Categories.NumItems; 
	return 0;

[CategoryDelete.OnClick]
	%A=TemplateLibrary!Categories.s1$; // get selected category name	
	type -n $TemplateLibrary.DeleteCategory;
	ii=TemplateLibrary!Categories.Choice;
	TemplateLibrary!Categories.del(ii);
	TemplateLibrary!Categories.Choice=ii-1;
	run.section(,DeleteCategory, "%A" $(ii));
	run.section(,Fillfunctions, 1);
	return 0;

[AddCategory]
	ini.file$=template.ini;
	ini.section$=Categories;
	ii=TemplateLibrary!Categories.Numitems;
	TemplateLibrary!Categories.Add(%1,0);
	if(TemplateLibrary!Categories.NumItems==ii)
	{
		type -b Category "%1" already exists.;
		return 1;
	}
	ini.setstr(%1,$(TemplateLibrary!Categories.Numitems),Categories,Template.ini); // add keyname %1 to Category section
	ini.setstr($(TemplateLibrary!Categories.Numitems),count,Categories,Template.ini);
	ini.check(%1); // add section name called %1
	return 0;

[DeleteCategory]
	ini.setstr(,,%1,template.ini);
	ini.setstr(,%2,Categories,template.ini);
	ini.getsection(Categories);
	loop(ii,%2+1,ini.keynum)
	{
		%B=ini.keyvalue$(ii)$;
		ini.setstr(%B,$(ii-1),Categories,Template.ini);
	}
	ini.setstr(,$(ii-1),Categories,Template.ini);
	ini.setstr($(ii-2),count,Categories,Template.ini);
	return 0;

[TempAdd.OnClick]
	run.section(File,Init);
	fdlog.usegroup(template);
	fdlog.usetype(OTP);
	if(FDLOG.Open(A))
		return 1;

	%B=FDLOG.path$;
	%K=TemplateLibrary!Categories.s1$; // get selected category name	
	ini.setstr(%B,%[%A,"."],%K,template.ini);
	TemplateLibrary!Templates.Add(%[%A,"."]);
	TemplateLibrary!Templates.Choice=TemplateLibrary!Templates.NumItems;
	%K=TemplateLibrary!Templates.s1$;
	TemplateLibrary!TemEdit.v1$=%K.otp;
	if(run.LoadOC("OriginLab\TemplateLibrary",4)!=0)	
	{
		run.section(DBRoutines.OGS,ReportError,$Regression.CFileNoLoad);
		return 1;
	}
	run.section(DBRoutines,SaveAndSetAtOC);
	ini.getstr(W,,%B,%A,template.ini);
	%A=system.path.program$;
	if(%[%W]<=1)
		%W=%A;
	TemplateLibrary!NoPreview.show=0;
	if(OpenTemplate(%W%K))
	{
		TemplateLibrary!BMP.display(%Ytemplatepict.bmp);
	}
	else
	{
		if(exist(%ATemplates\Previews\%K.bmp)==-1)
		{
			TemplateLibrary!BMP.display();
			TemplateLibrary!NoPreview.show=1;
		}
		else
		{
			TemplateLibrary!BMP.display(%ATemplates\Previews\%K.bmp)
		}
	}
	run.section(DBRoutines,RestoreAtOC);
	return 0;	

[TempDel.OnClick]
	%A=TemplateLibrary!Categories.s1$; // get selected category name	
	%B=TemplateLibrary!Templates.s1$; // get selected category name	
	type -n $TemplateLibrary.DeleteFunction;
	ii=TemplateLibrary!Templates.Choice;
	TemplateLibrary!Templates.del(ii);
	if(ii>TemplateLibrary!Templates.NumItems)
		ii=TemplateLibrary!Templates.NumItems;
	TemplateLibrary!Templates.choice=ii;
	run.section(,DeleteTemplate, "%A" "%B");
	ini.file$=template.ini;
	%A=TemplateLibrary!Categories.s1$;
	ini.getsection(%A);
	%B=TemplateLibrary!Templates.s1$;
	TemplateLibrary!TemEdit.v1$=%B.otp;
	ini.getstr(W,,%B,%A,template.ini);
	%A=system.path.program$;
	if(%[%W]<=1)
		%W=%A;
	if(run.LoadOC("OriginLab\TemplateLibrary",4)!=0)	
	{
		run.section(DBRoutines.OGS,ReportError,$Regression.CFileNoLoad);
		return 1;
	}
	run.section(DBRoutines,SaveAndSetAtOC);
	TemplateLibrary!NoPreview.show=0;
	if(OpenTemplate(%W%B))
	{
		TemplateLibrary!BMP.display(%Ytemplatepict.bmp);
	}
	else
	{
		if(exist(%ATemplates\Previews\%B.bmp)==-1)
		{
			TemplateLibrary!BMP.display();
			TemplateLibrary!NoPreview.show=1;
		}
		else
		{
			TemplateLibrary!BMP.display(%ATemplates\Previews\%B.bmp)
		}
	}
	run.section(DBRoutines,RestoreAtOC);
	TemplateLibrary!Templates.setfocus();
	return 0;

[DeleteTemplate]
	ini.setstr(,%2,%1,template.ini);
	return 0;

[OnOK]
	%W=%H;
	ini.file$=template.ini;
	%A=TemplateLibrary!Categories.s1$;
	%B=TemplateLibrary!Templates.s1$;
	ini.getstr(W,,%B,%A,template.ini);
	%A=system.path.program$;
	if(%[%W]<=1)
		%W=%A;
	%A=%W%B.OTP;

	if (wks.sel<=1)
	{
		worksheet -P ? %A;
		Legend -S;//CPY 8/25/03 PLOT_SETUP_DLG_USE_NEW_SMART_LEGEND
		dialog.close(OTools,templatelibrary);
		return 0;
	}
	rone=wks.r1;
	rtwo=wks.r2;

	jj=0;xx=0;
	count=wks.iscolsel();
	create _tempPos -t 1;
	create _tempLayer -t 1;
	tempPos=1;
	loop(ii,1,count)
	{
		jj=wks.c1+%(%H,@O,ii-1);
		//pass valid column types (X,Y,YERR,XERR,L)
		if(mod(wks.col$(jj).type,2)!=0 && wks.col$(jj).type!=4)
		{
			_tempPos[tempPos]=jj;		//save positions in temp dataset
			if(wks.col$(jj).type==1)
			{
				xx+=1;
				//save layer in temp dataset
				_tempLayer[tempPos]=xx;
			}
			else
				_tempLayer[tempPos]=xx;	//save layer in temp dataset
			tempPos+=1;
		}
	}
	%W=%H;
	worksheet -ps;
	document -T %A;
	doc -cl;
	if(count==1) 
	{
		loop(ii,1,tempPos-1)
		{
			layer -wc %W _tempPos[ii] rone _tempPos[ii] rtwo ?;
		}
		///-----------CPY 10/28/99 t7082 v6.0236 PLOT_TO_TEMPLATE_CHECK_GROUP
		//layer -g;
		layer -gc; /// gc will check style holder for no-group bit on 1st plot
		///-----------
		layer -a;
		if (legend.show)
			legend;
		dialog.close(OTools,templatelibrary);
		ApplySystemThemeToActivePage();
		return 0;
	}

	jj=%(%W,@Y#,2);
	if(jj == 1)      
	{       
		doc -each LW 
		{ 
			loop(ii,1,tempPos-1)
			{
				layer -wc %W _tempPos[ii] rone _tempPos[ii] rtwo ?;
			}
			layer -a;
		};
		if (legend.show)
			legend;
		dialog.close(OTools,templatelibrary);
		ApplySystemThemeToActivePage();
		return 0;
	}
	loop(ii,1,tempPos-1)
	{
		jj=mod(_tempLayer[ii],page.nlayers);
		jj=jj==0?page.nlayers:jj;
		layer -o jj {		
			layer -wd %W _tempPos[ii] rone _tempPos[ii] rtwo ?;
			layer -a;
		}
	}
	doc -e LW {layer -gc;}
	if (legend.show)
		legend -s;//CPY 8/25/03 PLOT_SETUP_DLG_USE_NEW_SMART_LEGEND, changed from legend to legend -s
	dialog.close(OTools,templatelibrary);
	ApplySystemThemeToActivePage();
	return 0;

[AddTemplate]
	ini.file$=template.ini;
	ini.getsection(Categories);

	loop(ii,2,ini.keynum)
	{
		%B=ini.keyvalue$(ii)$;
		if("%B"==%1)
		{
			%B=fdlog.path$;
			ini.setstr(%B,%[%A,"."],%1,Template.ini);
			break;
		}
	}
	if(ii>ini.keynum)
	{
		ini.getsection(Categories);
		ini.setstr($(ini.keynum),count,Categories,Template.ini);
		ini.setstr(%1,$(ii-1),Categories,Template.ini);
		%B=fdlog.path$;
		ini.setstr(%B,%[%A,"."],%1,Template.ini);
	}
	return 0;
