load_file("nrngui.hoc")

objref ocbox_, ocbox_list_, scene_, scene_list_
objref sph, ms
objref grph0
objref scene_vector_[8]
strdef tmpstr

{ocbox_list_ = new List()  scene_list_ = new List()}

scene_vector_[4] = new Graph(0)
scene_vector_[5] = new Graph(0)
scene_vector_[6] = new Graph(0)
scene_vector_[7] = new PlotShape(0)


proc RunControlWindow() {
//open RunControl Window. requires "stdrun.hoc" to be loaded
	xpanel("RunControl", 0)
	xvalue("Init","v_init", 1,"stdinit()", 1, 1 )
	xbutton("Init & Run","run()")
	xbutton("Stop","stoprun=1")
	xvalue("Continue til","runStopAt", 1,"{continuerun(runStopAt) stoprun=1}", 1, 1 )
	xvalue("Continue for","runStopIn", 1,"{continuerun(t + runStopIn) stoprun=1}", 1, 1 )
	xbutton("Single Step","steprun()")
	xvalue("t","t", 2 )
	xvalue("Tstop","tstop", 1,"tstop_changed()", 0, 1 )
	xvalue("dt","dt", 1,"setdt()", 0, 1 )
	xvalue("Points plotted/ms","steps_per_ms", 1,"setdt()", 0, 1 )
	xvalue("Scrn update invl","screen_update_invl", 1,"", 0, 1 )
	xvalue("Real Time","realtime", 0,"", 0, 1 )
	xpanel(680,8)
}


proc vplot() { local i,count, flag, k, n
//vplot( sections ). generate time vs. membrane potential plot
//vplot( flag, sections ). if flag==1 create new window. flag==0 is default
//sections can be a sectionlist or a series of section names i.e. vplot("soma[0]","dendrite[1]", ...)
	
	count = numarg()
	if (argtype(1)==0) {	// is first input a number
		flag=$1	//	set flag to input
		k=2		//	sections/compartment now starts with 2nd input
	} else {
		flag=0		//	set flag to 0 (default)
		k=1		//	sections starts with 1st input
	}
	grph0 = scene_vector_[4]
	if ( (grph0.view_count()==0) || (flag==1) ) {
		{grph0.view(0, -69, tstop, 40, 800, 400, 800, 350)}
		//(mleft, mbottom, mwidth, mheight, wleft,wtop, wwidth, wheight)
		// in a window with user-specified location (5th and 6th args) and size (last 2 args)
		graphList[0].append(grph0)
	}
	if ((count>0) && (flag==0)) grph0.erase_all()	// check flag
	
	if (argtype(k) == 1) {	// check whether 1st non-flag input is an object
		n=0		// index changing for each section to use different colors
		i=k		// use i because $ok returns error
		forsec $oi { // loop through sectionlist object
			n+=1
			sprint(tmpstr, "%s.v(0.5)", secname() )
			grph0.addvar(tmpstr, n,1) // add section to plot
		}
	} else {
		for i=k,count {	// loop through input strings
			sprint(tmpstr, "%s.v(0.5)", $si )
			grph0.addvar(tmpstr, i,1)
		}
	}
}

proc gplot() { local i,count localobj sl
// gplot( stringlist )

	grph0 = scene_vector_[6]
	if (grph0.view_count()==0) {
		{grph0.view(0, 0, 500, 0.001, 50, 400, 700, 350)}
		//(mleft, mbottom, mwidth, mheight, wleft,wtop, wwidth, wheight)
		// in a window with user-specified location (5th and 6th args) and size (last 2 args)
		graphList[0].append(grph0)
	}
	
	if ((numarg()>0) && (argtype(1)==1)) {
		grph0.erase_all()
		sl = $o1
		for i=0,sl.count()-1 {
			grph0.addexpr(sl.o[i].s, i+1,1)
		}
	} else if (grph0.view_count()==0) {
		grph0.addvar("Tines[621].g_h(0.5)", 2,1)
		grph0.addvar("Tines[786].g_h(0.5)", 4,1)
		grph0.addvar("Tines[621].g_KD(0.5)", 2,1)
		grph0.addvar("Tines[786].g_KD(0.5)", 4,1)
	}
}

proc stateplot() {local i localobj sl
// stateplot( stringlist )

	grph0 = scene_vector_[5]
	if (grph0.view_count()==0) {
		{grph0.view(0, 0, 500, 1, 50, 400, 700, 350)}
		//(mleft, mbottom, mwidth, mheight, wleft, wtop, wwidth, wheight)
		graphList[0].append(grph0)
	}
	
	if ((numarg() > 0) && (argtype(1)==1))	{
		grph0.erase_all()
		sl = $o1
		for i=0,sl.count()-1 {
			grph0.addexpr(sl.o[i].s, i+1,1)
		}
	} else if (grph0.view_count()==0) {
		grph0.addexpr("Handle[0].cai(0.5)*100", 9, 1)
		grph0.addexpr("Handle[0].g_KCa(0.5)/Handle[0].gmax_KCa(0.5)", 6,1)
		grph0.addvar("Tines[621].n_hcn(0.5)", 1,1)
		grph0.addexpr("Tines[621].g_NaP(0.5)/Tines[621].gmax_NaP(0.5)", 3,1)
		grph0.addexpr("Tines[621].g_KD_ca3(0.5)/Tines[621].gmax_KD_ca3(0.5)", 2,1)
		grph0.addexpr("Tines[786].g_KD_ca3(0.5)/Tines[786].gmax_KD_ca3(0.5)", 4,1)
	}
}

proc shapeplot() {local mx, mn
// shapeplot( variable, maxvalue, minvalue )

	sph = scene_vector_[7]
	if ((numarg()>0) && (argtype(1)==2)) {
		sph.variable($s1)
	} else	sph.variable("cai")
	if ((numarg()>1) && (argtype(2)==0)) {
		mx = $2
	} else	mx=5e-3
	if ((numarg()>2) && (argtype(3)==0)) {
		mn = $3
	} else	mn=0
	sph.scale(mn, mx)
	if (sph.view_count()==0) {
		sph.size(60.6917,540.608,62.739,324.261)
		sph.show(0)
		sph.observe(FieldA)
		{sph.view(66, 100, 420, 230, 960, 10, 640, 333)}
		fast_flush_list.append(sph)
		sph.save_name("fast_flush_list.")
		sph.rotate(0,0,0,0.7,0,0)
		sph.exec_menu("Shape Plot")
	}
}

/*0 white
1 black
2 red
3 blue
4 green
5 orange
6 brown
7 violet
8 yellow
9 gray*/
/*