; $Id: xtasfit_event.pro,v 1.3 2002/03/28 01:41:34 jmspark Exp $
FUNCTION getwinvals, wins
;**** This function gets all of the displayed values in the window ids given
;**** by wins. Wins should be an array of window ids
jim=fltarr(n_elements(wins))
FOR i=0,n_elements(wins)-1 DO BEGIN
        widget_control, wins(i),get_value=grib
        jim(i)=grib
END
RETURN, jim
END

pro Xtasfit_event,ev,newfile=newfile, davePtr=davePtr
;Event handler FOR curve fitting window
COMMON data,xax,yax,fit,symboln,dindex,d,xmin,xmax,ymin,ymax,pcomm ;relevant for plot
COMMON fitp,ngau,nlor,a,vary,pnames,gw,ufunct,nufun ;relevant numbers for fit
COMMON nums,gb,gn,vback_ids,ln,wgw              ;more numbers needed
COMMON com_tasfit, tasfitids                    ;Relevant window id values
COMMON com_minchoice, minchoice, minpath        ;Minimizer type and MINUIT path
COMMON com_convolution, condim, epar, resoly, tilt, ghorder
COMMON preferences, pref

IF (XRegistered("Xtasfit") EQ 0) THEN RETURN
;In case a new file is read in, we'll call this thing too
IF keyword_set(newfile) THEN BEGIN
  FOR i=0,2 do widget_control,tasfitids.gb(i),set_value=strcompress(a(i))
  IF (gn gt 0) THEN FOR i=3,5 do widget_control,tasfitids.gb(i),set_value=a(i+3*(gn-1))
  IF (ln gt 0) THEN FOR i=6,8 do widget_control,tasfitids.gb(i),set_value=a(i+3*(ngau-1)+3*(ln-1))
  IF (nufun gt 0) THEN FOR i=9,9+nufun-1 do widget_control,tasfitids.gb(i),set_value=a(i+3*(ngau+nlor-2))
  WIDGET_CONTROL, tasfitids.ngautext, $ ;And update the text field showing the no. of gauians
   SET_VALUE='# of Gaussians: '+STRING(ngau,FORMAT='(I2)')
  WIDGET_CONTROL, tasfitids.nlortext, $   ;And update the text field showing the no. of gaussians
   SET_VALUE='# of Lorentzians: '+STRING(nlor,FORMAT='(I2)')
  WIDGET_CONTROL, tasfitids.gnumtext, $ ;And update the text field showing the gaussian number
   SET_VALUE='Gaussian #: '+STRING(gn,FORMAT='(I2)')
  WIDGET_CONTROL, tasfitids.lnumtext, $ ;And update the text field showing the lor number
   SET_VALUE='Lorentzian #: '+STRING(ln,FORMAT='(I2)')
  RETURN
END
CASE ev.id OF
 tasfitids.minchoice: minchoice=ev.value
 tasfitids.fitorexit: BEGIN
  case ev.value of
   ' Help ': xdisplayfile,'/opt/rsi_idl/idl_4/lib/riso/xtasfit.hlp'
   ' Fit ':BEGIN
   ;before fitting, we must grab the values of the background and resolution width
   ;as well as the current setting for the visible gaussian and lorentzians windows
   ;the possible hidden ones were updated when they were hidden
      widget_control,/hourglass
      taspl,d,xax,yax,nsym=symboln,xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax, davePtr=davePtr
   ;For some reason, one cannot use an array element as the get_value return, so
   ;do it this way instead.
      jim=getwinvals(tasfitids.gb)
      FOR i=0,2 do a(i)=jim(i)
      IF (gn gt 0) THEN FOR i=3,5 do a(i+3*(gn-1))=jim(i)
      IF (ln gt 0) THEN FOR i=6,8 do a(i+3*(ngau-1)+3*(ln-1))=jim(i)
      IF (nufun gt 0) THEN FOR i=9,9+nufun-1 do a(i+3*(ngau+nlor-2))=jim(i)
      widget_control,tasfitids.wgw,get_value=gw
      gw=float(gw(0))

IF pref.vert NE 0 THEN BEGIN
      widget_control,tasfitids.vertw,get_value=resoly
      resoly=float(resoly(0))
      widget_control,tasfitids.tiltw,get_value=tilt
      tilt=float(tilt(0))
      widget_control,tasfitids.eparw,get_value=epar
END

      fitov,d,xax,yax,xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax,/FIT,/MINUIT
      fit='true'
      FOR i=0,2 do widget_control,tasfitids.gb(i),set_value=strcompress(a(i))
      IF (gn gt 0) THEN FOR i=3,5 do widget_control,tasfitids.gb(i),set_value=a(i+3*(gn-1))
      IF (ln gt 0) THEN FOR i=6,8 do widget_control,tasfitids.gb(i),set_value=a(i+3*(ngau-1)+3*(ln-1))
      IF (nufun gt 0) THEN FOR i=9,9+nufun-1 do widget_control,tasfitids.gb(i),set_value=a(i+3*(ngau+nlor-2))
      END
   ' Exit ':widget_control,ev.top,/destroy
  ENDCASE
 END
 tasfitids.gbutton: IF ngau NE 0 THEN BEGIN
gstart:  notice=widget_base()
        info=widget_label(notice,VALUE='Click on desired peak')
        WIDGET_CONTROL, notice, /REALIZE
        x=0.0 & y=0.0
        cursor, x,y,/data,/down
        a(3+3*(gn-1))=y & a(4+3*(gn-1))=x
        WIDGET_CONTROL, notice, /DESTROY
        ;Now update the parameter window
        WIDGET_CONTROL, tasfitids.gb(3),$
         SET_VALUE=y-min(d.data(tasid(yax,d),*))
        WIDGET_CONTROL, tasfitids.gb(4),SET_VALUE=x
        WIDGET_CONTROL, tasfitids.gb(5),$
         SET_VALUE=0.1*(max(d.data(tasid(xax,d),*))-min(d.data(tasid(xax,d),*)))
 END
  tasfitids.lbutton: IF nlor NE 0 THEN BEGIN
lstart: notice=widget_base()
        info=widget_label(notice,VALUE='Click on desired peak')
        WIDGET_CONTROL, notice, /REALIZE
        x=0.0 & y=0.0
        cursor, x,y,/data,/down
        a(3+3*(ngau+ln-1))=y & a(4+3*(ngau+ln-1))=x
        WIDGET_CONTROL, notice, /DESTROY
        ;Now update the parameter window
        WIDGET_CONTROL, tasfitids.gb(6),$
         SET_VALUE=y-min(d.data(tasid(yax,d),*))
        WIDGET_CONTROL, tasfitids.gb(7),SET_VALUE=x
        WIDGET_CONTROL, tasfitids.gb(8),$
         SET_VALUE=0.1*(max(d.data(tasid(xax,d),*))-min(d.data(tasid(xax,d),*)))
 END
 tasfitids.numb_gau: BEGIN
        newval = ngau+ev.value      ;ev.value is +1 or -1 depending which button was pressed
        IF newval LT 0 THEN newval=0            ;Do some range checking
        IF newval GT 99 THEN newval=99    ;You want to fit more than 99 gaussians? Go away....
        ngpeaks, newval                         ;Update the number of gaussians
        WIDGET_CONTROL, tasfitids.numb_gau, set_button=0   ;Put the buttons in the up state
        WIDGET_CONTROL, tasfitids.ngautext, $ ;And update the text field showing the no. of gauians
                SET_VALUE='# of Gaussians: '+STRING(ngau,FORMAT='(I2)')
        WIDGET_CONTROL, tasfitids.gnumtext, $ ;And update the text field showing the gaussian number
                SET_VALUE='Gaussian #: '+STRING(gn,FORMAT='(I2)')
        goto, updtgn
 END
 tasfitids.gtnumi: BEGIN
        newval = gn+ev.value     ;ev.value is +1 or -1 dependinng which button was pressed
updtgn: IF ngau NE 0 THEN BEGIN
          IF newval GT ngau THEN newval=1       ;Do some range checking
          IF newval LT 1 THEN newval=ngau       ;Loop around the max. number of gaussians

          ;Save the current values for the parameters before switching
          jim=getwinvals(tasfitids.gb)
          FOR i=3,5 DO a(i+3*(gn-1))=jim(i)
          gn=fix(newval)                       ;Do the switch
          WIDGET_CONTROL, tasfitids.gtnumi, set_button=0  ;Put the buttons in the up state
          WIDGET_CONTROL, tasfitids.gnumtext, $ ;And update the text field showing the gaussian number
                  SET_VALUE='Gaussian #: '+STRING(gn,FORMAT='(I2)')
          if ((ev.id eq tasfitids.numb_gau) and (ev.value eq 1)) then goto, gstart else $
          FOR i=3,5 do widget_control,tasfitids.gb(i),set_value=a(i+3*(gn-1))   ;And display these values now
          IF (ngau gt 0) THEN FOR i=3,5 DO  IF (vary(i+3*(gn-1)) eq 1) THEN $
            widget_control,vback_ids(i),/set_button ELSE $
            widget_control,vback_ids(i),set_button=0
        ENDIF
 END

 tasfitids.numb_lor: BEGIN
        newval = nlor+ev.value      ;ev.value is +1 or -1 dependinng which button was pressed
        IF newval LT 0 THEN newval=0     ;Do some range checking
        IF newval GT 99 THEN newval=99  ;You want to fit more than 99 gaussians? Go away....
        nlpeaks, newval                         ;Update the number of gaussians
        WIDGET_CONTROL, tasfitids.numb_lor, set_button=0   ;Put the buttons in the up state
        WIDGET_CONTROL, tasfitids.nlortext, $   ;And update the text field showing the no. of gaussians
                SET_VALUE='# of Lorentzians: '+STRING(nlor,FORMAT='(I2)')
        WIDGET_CONTROL, tasfitids.lnumtext, $ ;And update the text field showing the lor number
                SET_VALUE='Lorentzian #: '+STRING(ln,FORMAT='(I2)')
        goto, updtln
 END
 tasfitids.ltnumi: BEGIN
        newval = ln+ev.value                    ;ev.value is +1 or -1 dependinng which button was pressed
updtln: IF nlor NE 0 THEN BEGIN
          IF newval GT nlor THEN newval=1               ;Do some range checking
          IF newval LT 1 THEN newval=nlor       ;Loop around the max. number of gaussians
          ;Save the current values for the parameters before switching
          jim=getwinvals(tasfitids.gb)
          FOR i=6,8 DO a(i+3*(ngau-1)+3*(ln-1))=jim(i)
          ln=fix(newval)                                                ;Do the switch
          WIDGET_CONTROL, tasfitids.ltnumi, set_button=0     ;Put the buttons in the up state
          WIDGET_CONTROL, tasfitids.lnumtext, $ ;And update the text field showing the lor number
                  SET_VALUE='Lorentzian #: '+STRING(ln,FORMAT='(I2)')
          if ((ev.id eq tasfitids.numb_lor) and (ev.value eq 1)) then goto, lstart else $
          FOR i=6,8 do widget_control,tasfitids.gb(i),set_value=a(i+3*(ngau-1)+3*(ln-1)) ;And display these values now
          IF (nlor gt 0) THEN FOR i=6,8 DO  IF (vary(i+3*(ngau-1)+3*(ln-1)) eq 1) THEN $
            widget_control,vback_ids(i),/set_button ELSE $
            widget_control,vback_ids(i),set_button=0
	    ENDIF
 END
 tasfitids.utfun: BEGIN
  minchoice='SFit'
  IF (ev.value ne '') THEN BEGIN
   widget_control,ev.top,/destroy
   IF (ufunct ne '') THEN exstr=execute(ufunct+',/destroy')
   ufunct=ev.value
   exstr=execute(ev.value+',/reset')
   Xtasfit
  ENDIF ELSE IF (ufunct ne '') THEN BEGIN
   widget_control,ev.top,/destroy
   exstr=execute(ufunct+',/destroy')
   ufunct=''
   Xtasfit
   ENDIF
  END
 ELSE: BEGIN             ;Fiddle with the vary buttons
  idx=where(pnames eq ev.value)
  idx=idx(0)
  idxsave=idx
  IF ((idx gt 2) and (idx le 5)) THEN idx=idx+3*(gn-1) ELSE $
  IF ((idx gt 5) and (idx le 8)) THEN idx=idx+3*(ngau-1)+3*(ln-1) ELSE $
  IF (idx gt 8) THEN idx=idx+3*(ngau+nlor-2)
  IF (ev.select eq 1) THEN vary(idx)=1 ELSE vary(idx)=0
  END
ENDCASE
END

