pro cp_event, event

@cpdispfiles.common 
@cpcolors.common 
@cpkeywords.common 
@cplastpass.common
@cpnew_window.common 
@cpprolocal.common
@cpwidgbases.common

;e 2/19/93 @cplogic_envron.common 



 widget_control, event.id, get_uvalue=input ; which widget was clicked
 if n_elements(input) eq 0 then input = ''

 type = strmid(tag_names(event, /structure_name),7,4)    ; get widget type

 case type of 
    'BUTT' : case input of
    'QUITPRO': widget_control, event.top, /destroy  ; kill all widgets
        'KILLSCL': begin
                 widget_control, event.top, /destroy  ; kill top widget
                     widget_control, base, sensitive=1    ; can use BASE again
                   end

    'DELWIN' : winup, group_leader=event.top, message=message

    'REUSE'   : reuse = 1
    'NOREUSE' : reuse = 0

    'SAVE'    : begin
            save, /xdr, /ver, FILE='CPSAVE.DAT',  $
                last_filename,  $       ; normal image filename
                last_diffname1, $   ; base image filename
                last_diffname2, $   ; subtraction image filename
                cpmaxscale, $   ; the !p.color value for color table
                cpmidscale, $   ; mid point of data area in difference
                cpnegfac,   $   ; the diff image neg scaling factor
                cpposfac,   $   ; the diff image pos scaling factor
                cpdata,         $   ; The data array.  NOTE: these values
                            ;   are not scaled for the color table.
                cphk,       $   ; the house keeping
                cpimsiz,    $   ; original image size
                cpih,       $   ; FOV positional value for image
                cpiv,       $   ; FOV positional value for image
                cptheta,    $   ; the value of theta entered by user,
                            ;   in degrees
                cprsun,     $   ; the Rsun value entered by user
                cpnum_vals_n_scans, $   ; vector w/ # of valid data for each
                            ;   scan
                cpintensity,    $   ; the intensity values
                cpabscissa      ; for RULECONDITION 3 it is the Rsun 
                            ;        values for the intensity array
                                                ; for RULECONDITION 4 it is the angle
                            ;        values for the intensity array
            end

    'HELP'    : cphelp, group=event.top

    ; the user set options
    'NORMAL'   :  if event.select ne 0 then begin     ; ignore offs

                         if norm_diff eq 1 then begin   ; difference was active
                if xregistered('cpone') or $      ; have wrong
                xregistered('cpmovie') then begin ;   option
                                  ;   display 
                                  ;   active
                    acknowledge, message=([ $
                        'Cancel active top widget,',$
                    'either "Display one image"',$
                    'or "Show a movie". ', $
                    'It is incompatible ', $
                    'with "Normal display.'])

                ; undo normal event, put back to difference
                    widget_control, norm, set_button=0   ; undo 
                    widget_control, diff, set_button=1   ;   event
                return
                endif

             endif

             coltyp=0  &  norm_diff=0
             widget_control, scal, sensitive=0  ; not applicable
              endif

    'DIFFERE'  :  if event.select ne 0 then begin   ; ignore offs

                         if norm_diff eq 0 then begin   ; normal was active
                if xregistered('cpone') or $      ; have wrong
                xregistered('cpmovie') then begin ;   option
                                  ;   display 
                                  ;   active
                    acknowledge, message=([ $
                        'Cancel active top widget,',$
                    'either "Display one image"',$
                    'or "Show a movie". ', $
                    'It is incompatible ', $
                    'with "Make a difference".'])


                ; undo difference event, put back to normal
                    widget_control, norm, set_button=1  
                    widget_control, diff, set_button=0 
                return
                endif

             endif


             coltyp=1  &  norm_diff=1  
             widget_control, scal, sensitive=1  ; now an option
              endif

    'SOLNORTH' :  if event.select eq 0 then donorth=0 else donorth=1
    'SOLDISK'  :  if event.select eq 0 then dodisk=0  else dodisk=1
    'SOLRADII' :  if event.select ne 0 then doradii=1 else doradii=0
    'SOLRADIAL':  if event.select ne 0 then doradial=1 else doradial=0

    'RULNONE'  :  if event.select ne 0 then begin
            ruletimes = 0
                widget_control, ruleopt2, sensitive=0
                      endif
    'RULONCE'  :  if event.select ne 0 then begin
            ruletimes = 1
                widget_control, ruleopt2, sensitive=1
                      endif
    'RULMANY'  :  if event.select ne 0 then begin
            ruletimes = 2
                widget_control, ruleopt2, sensitive=1
                      endif
    'RULINPT'  :  if event.select ne 0 then begin
            ruletimes = 3
                widget_control, ruleopt2, sensitive=1
                      endif
    'MEMRUL'   :  if event.select ne 0 then begin
            ruletimes = 4
                widget_control, ruleopt2, sensitive=1
                      endif



    'RULIT'         :  if event.select ne 0 then rulecondition=0
    'MARKIT'        :  if event.select ne 0 then rulecondition=1
    'NORULMARK'     :  if event.select ne 0 then rulecondition=2
    'RADIALSCAN'    :  if event.select ne 0 then rulecondition=3
    'AZMUTHSCAN'    :  if event.select ne 0 then rulecondition=4

        'AUTOSCL'   :  if event.select ne 0 then begin   ; default clip
                         negfac = 65.  &  posfac = 65.
                         cpnegfac = 65.  &  cpposfac = 65.
               endif    
    'MANUSCL'   :  if event.select ne 0 then begin
            widget_control, base, sensitive=0 ; no BASE inputs now
                        negfac = 65.  &  posfac = 65.       ; default clip
                        cpnegfac = 65.  &  cpposfac = 65.

                        ; make a widget to ask for the scaling factors
                inpscl = widget_base(title='C/P difference image ' +$
                 'scaling',/column, space=10,   $
                      xpad=20, ypad=20)

                lab = widget_label(inpscl, value='Select the ' + $
                  'difference image scaling factors.')

                lab = widget_label(inpscl, value='Difference data '+$
                  'has a possible value range of -255 to +255.')
            
                lab = widget_label(inpscl, value='Values outside '+$
                  'of the scaled range are set to the scaling ' +$
                  'factor value.')
                  
                lab = widget_label(inpscl, value='Defaults are -65 '+$
                  'and +65.')

                tex = widget_slider(inpscl, title='Negative (blue) '+$
                  'clipping value', minimum=-256, maximum=-1, $
                  value=-65, xsize=700, uvalue = 'NEGINP')
                tex = widget_slider(inpscl, title='Positive (red) '+$
                  'clipping value', minimum=1, maximum=255, $
                  value=65, xsize=700, uvalue = 'POSINP') 

                go_on = widget_button(inpscl, value='READY', $
                                  uvalue='KILLSCL')
                widget_control, inpscl, /realize
                xmanager,'cp', inpscl, /modal, group_leader=event.top
               endif           

    'ONEIMAG' : begin
           sin_mov=0                ; single image
       onewin = -99             ; no cp display window yet
       widget_control, event.top, sensitive=0     ; no user for a while
       cpone, group_leader=event.top    ; go display single image
       end

        'MOVIEIM' : begin
       sin_mov=1                ; doing a movie
       widget_control, event.top, sensitive=0       ; no user for a while
       cpmovie, group_leader=event.top      ; go make a movie
           end
    else : print, 'Invalid BUTTON value in CP:' + input
    endcase     ; of input            

    'SLID': case input of       ; only if diff and manual scale factor
    'POSINP' : begin
                  widget_control, event.id, get_value=value
              posfac = float(value)
              cpposfac = posfac 
                   end
    'NEGINP' : begin
                  widget_control, event.id, get_value=value
              negfac = float(value)
              cpnegfac = negfac
                   end                            
    else : print, 'Invalid SLIDER value' + input
    endcase     ; of input


    else : print, 'Invalid type of widget ' + type

   endcase          ; of type
end

;==============================================================================