; calib_integ  (interactive)
;
; creates a tool which permits to handle several peaks used for intensity 
; calibration
;
; you can define peaks and associated upper and lower distance bounds.
;         store and load a set of peak definitions (stored as macro commands)
;         draw the peak definitions on screen
;
; when loaded, the peak definition are stored in an associative array called
; calib_dst[]

set fnm = "calib_int"		; the name of the storage file

set pic = 0
=redo

if ($arg == 0) then
; first prepare every thing for form.
	set pk_list = " "
	if (exist("calib_dst[]")) then
	  set i = (nextlm("calib_dst"," "))
	  while ($i s! " ")
		set pk_list = ($i // ',' // %)
		set i = (nextlm("calib_dst",$i))
	  endwhile
	endif
	if ($pk_list s= "") set pk_list = " "
	formbox "Calibration control" \
		"calib_integ Edit $pic" \
		"Peak :" enum $pk_list pic % \
		Define action "closeform $widget calib_integ Define" \
		noreturn Remove action "calib_integ Remove $pic $widget" \
		noreturn Edit action "calib_integ Edit $pic" \
		noreturn Show action "calib_integ Show $pic" \
		noreturn 'Show All' action "calib_integ ShowAll" \
		noreturn Load action "closeform $widget calib_integ Load" \
		noreturn Store action "calib_integ Store" \
		*

else
	set action = $_
	if ($action s= "Edit") then
                set pic = $_
                if (exist("att[" // $pic //"]")) then
                   set l = $att[$pic]
                else
                   error "Unknown peak"
                endif
                local_pk $pic
                @mod_calib $pic
        elsif ($action s= "Show") then
                set pic = $_
                @show_att $pic
        elsif ($action s= "ShowAll") then
	        set i = (nextlm("calib_dst"," "))
                @show_att $i
	        while ($i s! " ")
;                   print ("i " ; $i)
                   @show_att $i
		   set i = (nextlm("calib_dst",$i))
	        endwhile
	elsif ($action s= "Define") then
                alert "Click on the peak you want to add to the calibration list"
                @find_att
                set pic = $returned
                set calib_dst[$pic] := 0.0
                @mod_calib $pic
                goto redo
	elsif ($action s= "Remove") then
                set pic = $_
                set t = $_
                set var = ("calib_dst[" // $pic // "]")
                unset $var
                closeform $t
                goto redo
	elsif ($action s= "Store") then
                dialogbox Read 'Enter file name' message ' ' file fnm $fnm * 		
                set t = ("constraints/"//$fnm)
                open $t
	   	set i = (nextlm("calib_dst"," "))
	   	while ($i s! " ")
                 fprint $t ("set calib_dst[" // $i // "] := " // $calib_dst[$i])
                 set i = (nextlm("calib_dst",$i))
		endwhile
                close $t
		alert ("Written to a file called :"; $fnm)
	elsif ($action s= "Load") then
                dialogbox Read 'Enter file name' message ' ' file fnm $fnm * 		
		print ("Loading from file '"; $fnm; "'")
                set t = ("constraints/"//$fnm)
		@($t)
		goto redo
	endif		; $action
endif			; $arg
	










