PRO vizit_event, sEvent
;+
;  Handle events for VIZIT
;-
    WIDGET_CONTROL, sEvent.top, GET_UVALUE=sState    ; Get state
    ;print, sEvent

    CASE TAG_NAMES(sEvent, /STRUCTURE_NAME) OF       ; Kill request
    'WIDGET_KILL_REQUEST': BEGIN
        IF not ptr_valid(sState) THEN return
        IF n_elements(*sState) eq 0 THEN return
        OBJ_DESTROY, (*sState).oHolder
;        WIDGET_CONTROL, sEvent.top, /DESTROY
        WIDGET_CONTROL, (*sState).wControl, /DESTROY
        WIDGET_CONTROL, (*sState).wBase, /DESTROY
        RETURN
    END
    'WIDGET_BASE': BEGIN                             ; Resize request
        xsz = sEvent.x
        ysz = sEvent.y

        ;(*sState).oPlotWindow->SetProperty, DIM=[xsz,(*sState).ysize]

        (*sState).oMainWindow->SetProperty, DIM=[xsz,ysz]
        (*sState).oMainView->Remove, (*sState).oLights
        set_view, (*sState).oMainView, (*sState).oMainWindow, /ISOTR
        (*sState).oMainView->Add, (*sState).oLights
        if ((*sState).autodraw eq 1) then  (*sState).oMainWindow->Draw, (*sState).oMainView
        (*sState).oPlotWindow->Draw, (*sState).oPlotView

        (*sState).xsize = xsz
        ;WIDGET_CONTROL, sEvent.top, SET_UVALUE=sState
        RETURN
    END
    ELSE:
    ENDCASE


     if (WIDGET_INFO(sEvent.id,/UNAME) eq 'CW_BGROUP_UNAME') then begin
      IF (sEvent.Value eq 'auto') then begin
        (*sState).autodraw=sEvent.select
      endif else IF (sEvent.Value eq 'bbox') then begin
        (*sState).drawbbox=sEvent.select
      endif else IF (sEvent.Value eq 'content') then begin
        (*sState).drawcontent=sEvent.select
      endif else IF (sEvent.Value eq 'xaxis') then begin
        (*sState).hideXAxis=1-sEvent.select
        (*sState).oXAxis->SetProperty,HIDE=(*sState).hideXAxis
      endif else IF (sEvent.Value eq 'yaxis') then begin
        (*sState).hideYAxis=1-sEvent.select
        (*sState).oYAxis->SetProperty,HIDE=(*sState).hideYAxis
      endif else IF (sEvent.Value eq 'zaxis') then begin
        (*sState).hideZAxis=1-sEvent.select
        (*sState).oZAxis->SetProperty,HIDE=(*sState).hideZAxis
      endif
      ;WIDGET_CONTROL, sEvent.top, SET_UVALUE=sState

      (*sState).oBBox->SetProperty,HIDE=1-(*sState).drawbbox
      if (has_tag(*sState, 'oVolume')) then begin
        (*sState).oVolume->SetProperty,HIDE=1-(*sState).drawcontent
      endif else if (has_tag(*sState, 'oPolygon')) then begin
        if (not (*sState).noiso) then begin
          if n_elements((*sState).oPolygon) eq 1 then begin
            (*sState).oPolygon->SetProperty,HIDE=1-(*sState).drawcontent
          endif else begin
            for i=0,n_elements((*sState).oPolygon)-1 do begin
              (*sState).oPolygon[i]->SetProperty,HIDE=1-(*sState).drawcontent
            endfor
          endelse
        endif
      endif
      if (has_tag(*sState, 'oSurfaces')) then begin
        if ((*sState).nsurfaces gt 0) then begin
          for i=0,n_elements((*sState).oSurfaces)-1 do begin
            (*sState).oSurfaces[i]->SetProperty,HIDE=1-(*sState).drawcontent
          endfor
        endif
      endif
    endif else begin
    WIDGET_CONTROL, sEvent.id, GET_UVALUE=uval
    CASE uval OF
        'HIST': BEGIN                                    ; Vizit
           CASE sEvent.type OF
           0: (*sState).active = 1
           1: (*sState).active = 0
           ELSE:
           ENDCASE
           IF (*sState).active EQ 1 THEN BEGIN
              if (not (*sState).noiso) then begin
                index = long(255.*((sEvent.x+2) > 0)/(*sState).xsize) < 255
                level = (*sState).min + index/255.*((*sState).max-(*sState).min)
                (*sState).level = level
                (*sState).index = index
;                index = (*sState).index
;                level = (*sState).level
                shade_volume, *((*sState).data), level, vert, poly
                if n_elements(vert) le 1 then begin
                  vert=fltarr(3,3)
                  vert[*,*]=0.
                  poly=[3,0,1,2]
                endif
                if (*sState).calcnormals eq 1 then begin
                  gradvar=grad(*(*sState).data)
                  normals=vert
                  normals[0,*]=interpolate(gradvar[*,*,*,0],vert[0,*],vert[1,*],vert[2,*])
                  normals[1,*]=interpolate(gradvar[*,*,*,1],vert[0,*],vert[1,*],vert[2,*])
                  normals[2,*]=interpolate(gradvar[*,*,*,2],vert[0,*],vert[1,*],vert[2,*])
                endif
                if (*sState).nonequidistant eq 1 then begin
                  vert[0,*]=interpol((*sState).xx,findgen(n_elements((*sState).xx)),reform(vert[0,*]))
                  vert[1,*]=interpol((*sState).yy,findgen(n_elements((*sState).yy)),reform(vert[1,*]))
                  vert[2,*]=interpol((*sState).zz,findgen(n_elements((*sState).zz)),reform(vert[2,*]))
                endif
                (*sState).oPolygon->GetProperty,TEXTURE_MAP=oTexture
                oTexture->GetProperty,DATA=TextImg
                TextImg[0,*,*]=index
                oTexture->SetProperty,DATA=TextImg
                (*sState).oPolygon->SetProperty, DATA=vert, NORMALS=normals, POLYGONS=poly, COLOR=index, BOTTOM=index, TEXTURE_MAP=oTexture
                (*sState).oLine->SetProperty, DATAX=[index,index]
                (*sState).oPlotWindow->draw, (*sState).oPlotView
                if ((*sState).autodraw eq 1) then (*sState).oMainWindow->draw, (*sState).oMainView
                print,"Count polygons:",count_polygons(poly)
                i = WIDGET_INFO(sEvent.top, FIND_BY_UNAME='LEVEL')
                if i gt 0 then WIDGET_CONTROL, i, $
                  set_value='level = '+str(level,format='(g10.3)')
              endif
           ENDIF
           ;WIDGET_CONTROL, sEvent.top, SET_UVALUE=sState
        END

        'OPAC': BEGIN                                    ; VolViz
           CASE sEvent.type OF
             0: BEGIN                                    ; Mouse press
                (*sState).active = 1
                (*sState).xprv = long(255.*(sEvent.x > 0)/(*sState).xsize) < 255
                (*sState).yprv = long(255.*(sEvent.y > 0)/(*sState).ysize) < 255
;                WIDGET_CONTROL, sEvent.top, SET_UVALUE=sState
             END
             1: BEGIN                                    ; Mouse release
                (*sState).active=0
                if ((*sState).autodraw eq 1) then (*sState).oMainWindow->draw, (*sState).oMainView
;               WIDGET_CONTROL, sEvent.top, SET_UVALUE=sState
             END
             2: BEGIN                                    ; Mouse motion
                if ((*sState).active eq 0) then return
                x = long(255.*(sEvent.x > 0)/(*sState).xsize) < 255
                y = long(255.*(sEvent.y > 0)/(*sState).ysize) < 255
                x0 = (*sState).xprv
                y0 = (*sState).yprv
                i0 = (x0 < x)
                i1 = (x > x0)
                FOR i = i0, i1 DO BEGIN
                    (*sState).opac[i]=y0+((y-y0)*(i-i0))/(i1-i0+1e-6)
                ENDFOR
                (*sState).xprv = x
                (*sState).yprv = y

                (*sState).oPlot->SetProperty, DATAY=(*sState).opac
                (*sState).oVolume->SetProperty, OPACITY_TABLE0=(*sState).opac
                (*sState).oPlotWindow->draw, (*sState).oPlotView

                ;WIDGET_CONTROL, sEvent.top, SET_UVALUE=sState
             END
             ELSE: BEGIN                                 ; Ignore others
             END
           ENDCASE
        END
        'MAIN': BEGIN

           CASE sEvent.type OF
           0: BEGIN                                    ; Mouse press
                (*sState).active = 1
                if (*sState).type eq 'vizit' then begin
                  loc = [sEvent.x, sEvent.y]
                  hitType=0
                  iSurface=-1
                  if PTR_VALID(sState) then begin
                    result = -1
  		            if obj_valid((*sState).oPolygon) then begin
                      result = (*sState).oMainWindow->PickData((*sState).oMainView,(*sState).oPolygon,loc,xyz)
                      hitType=1
                    endif
                    if (result le 0) then begin
                      for i=0,n_elements((*sState).oSurfaces)-1 do begin
                        if (size((*sState).oSurfaces[i],/type) ne 11) then continue
                        result = (*sState).oMainWindow->PickData((*sState).oMainView,(*sState).oSurfaces[i],loc,xyz)
                        if (result eq 1) then begin
                          hitType=2
                          iSurface=i
                          break
                        endif
                      endfor
                    endif
		          endif else begin
		            result=-1
		          endelse

                  if result eq 1 then begin
                    case hitType OF
                      1: begin
                        (*sState).oPolygon->GetProperty,XCOORD_CONV=xcc
                        (*sState).oPolygon->GetProperty,YCOORD_CONV=ycc
                        (*sState).oPolygon->GetProperty,ZCOORD_CONV=zcc
                      end
                      2: begin
                        (*sState).oSurfaces[iSurface]->GetProperty,XCOORD_CONV=xcc
                        (*sState).oSurfaces[iSurface]->GetProperty,YCOORD_CONV=ycc
                        (*sState).oSurfaces[iSurface]->GetProperty,ZCOORD_CONV=zcc
                      end
                    endcase
                    print,'PickData: xyz=',xyz
                    print,'PickData: conv xyz=',xyz[0]*xcc[1]+xcc[0],xyz[1]*ycc[1]+ycc[0],xyz[1]*zcc[1]+zcc[0]
                  endif else if result eq 0 then begin
                    print,'PickData: Hit background'
                  endif else begin
                    print,'PickData: Missed'
                  endelse
                  if (not (*sState).noiso) then begin
                    if !d.name eq 'MAC' then $
                      (*sState).oModel->remove, (*sState).oPolygon
                  endif
                endif
                if (*sState).type eq 'volviz' then $
                  (*sState).oModel->remove, (*sState).oVolume
                if ((*sState).autodraw eq 1) then (*sState).oMainWindow->draw, (*sState).oMainView
                ;WIDGET_CONTROL, sEvent.top, SET_UVALUE=sState
           END
           1: BEGIN                                    ; Mouse release
                (*sState).active = 0
                if (not (*sState).noiso) then begin
                  if (*sState).type eq 'vizit' $
                  and !d.name eq 'MAC' then $
                    (*sState).oModel->add, (*sState).oPolygon
                endif
                if (*sState).type eq 'volviz' then $
                  (*sState).oModel->add, (*sState).oVolume
                if ((*sState).autodraw eq 1) then (*sState).oMainWindow->draw, (*sState).oMainView
                ;WIDGET_CONTROL, sEvent.top, SET_UVALUE=sState
           END
           4: BEGIN                                    ; Expose
               if ((*sState).drawbbox eq 1) then vizit_BBoxFade, (*sState).oBBox, (*sState).oModel
               if ((*sState).autodraw eq 1) then (*sState).oMainWindow->Draw, (*sState).oMainView
               (*sState).oPlotWindow->Draw, (*sState).oPlotView
               ;WIDGET_CONTROL, sEvent.top, SET_UVALUE=sState
               RETURN
           END
           ELSE:
           END

           bHaveTransform = (*sState).oTrack->Update( sEvent, TRANSFORM=qmat )
           IF (bHaveTransform NE 0) THEN BEGIN           ; Trackball updates.
               (*sState).oModel->GetProperty, TRANSFORM=t
               (*sState).oModel->SetProperty, TRANSFORM=t#qmat
               if ((*sState).drawbbox eq 1) then vizit_BBoxFade, (*sState).oBBox, (*sState).oModel
               if ((*sState).autodraw eq 1) then (*sState).oMainWindow->Draw, (*sState).oMainView
               (*sState).oMainView->GetProperty, ZCLIP=zclip
               widget_control,(*sState).wZClip0,set_value=zclip[0]
               widget_control,(*sState).wZClip1,set_value=zclip[1]

           END
        END
        ELSE: BEGIN
           print, 'VIZIT_EVENT: Unexpected UVALUE = ',uval
        END
     ENDCASE
     endelse
END