;
; Project	: SOHO - LASCO/EIT
;
; Name		: DISPLAY
;
; Purpose	: display images, ...
;
; Category      :
;
; Explanation	: 
;
; Syntax        :
;       	 
; Examples      :
;
; Inputs	: 
;
; Opt. Inputs	: 
;
; Outputs	: 
;
; Opt. Outputs	:
;
; Keywords	: 
;
; Common	: lasco.com, wload.com, wtv.com
;
; Restrictions  : None.
;
; Side effects	: None.
;
; History	: Updated Version: Jesse Bankman, November - 1996
;
; Contact     : BP, borut@lasco1.mpae.gwdg.de
;-



PRO display_event,event

@lasco.com
@wload.com
@chandle.com
@wplot.com
@wtv.com
@wflimb.com

COMMON display, first, last, current, count, count_all, cross_x, cross_y
common colors, r_orig, g_orig, b_orig, r_curr, g_curr, b_curr
COMMON wzoom,zw_id,wttrack,trackhandle
COMMON info,text,text1,text2,tmps,tmpp,new_tmpp,sipath,s_flag,inst,path,filter,wave_set,hnh

COMMON xloadct_com, r0, g0, b0, tfun, state, filename, cps, psave, pnt, $
	top, bot, silent, chop, lock, g_lbl, vbot, vtop, g_slider, $
	gamma, color, use_values, ncolors, cbot, siz, w_height, show_win

 
IF (event.id EQ event.handler) THEN BEGIN
 WIDGET_CONTROL,event.id,TIMER=.2
ENDIF ELSE BEGIN

 WIDGET_CONTROL, event.id, GET_UVALUE=uval           ; get the UVALUE of the event
 WIDGET_CONTROL, event.top, GET_UVALUE=info_hand     ; get handle from UVALUE

 HANDLE_VALUE, info_hand, uvd		     ; get array
	
WSET,dis.win1	 

 IF N_ELEMENTS(uval) EQ 0 THEN uval = 'TIMER'
 CASE uval OF

      "MPAE": BEGIN
        	  path_msg = GETENV('LASCO_MSG')           
        	  tmp_msg  = path_msg + !delimiter+ 'bp.add' 
                  msg = rd_asc(tmp_msg)
                  wmessage,title='MPAe address',text=msg,xsize=65,ysize=10
	      END

     "HELP0": BEGIN
        	  path_msg = GETENV('LASCO_MSG')           
        	  tmp_msg  = path_msg + !delimiter +'main.hlp' 
                  msg = rd_asc(tmp_msg)
                  wmessage,title='Main Window Help',text=msg,xsize=75,ysize=25
	       END

   "HDR_TXT": BEGIN
                 IF n_elements(tmps) GT 0 THEN whdrinfo1,txt=tmps ELSE whdrinfo1
              END
              
    "HEAD":   BEGIN
                 ; WIDGET_CONTROL,dis.id_th,GET_VALUE=head
                 ; HANDLE_VALUE,h_head(index),head,/SET
              END

     "CWG":   BEGIN	 
		 WIDGET_CONTROL, uvd.Order, GET_VALUE=g_order
		 !order = g_order
		 WSET,dis.win1 & TVSCL,image>min_img<max_img
	      END	

   "PDMENU2": BEGIN
   
               CASE event.value OF
      "Graphics.Adjust": BEGIN
       		 	  ;TVLCT, r_orig, g_orig, b_orig
		  	  ADJCT
                	 END

       "Graphics.Color": BEGIN
        	  	  ;TVLCT, r_orig, g_orig, b_orig
                          gamma_ct,gamma ,/CURRENT
		  	  XLOADCTBP ,/USE_CURRENT
		  	  WIDGET_CONTROL,uvd.Gamma_label, $
                                      SET_VALUE=STRING(gamma,format='(f6.3)')
		  	  ;XLOADCT,/USE_CURRENT
		  	  ;XLOADCTBP
                	 END

     "Graphics.Palette": BEGIN
        	  	  ;TVLCT, r_orig, g_orig, b_orig
		  	  XPALETTE
                	 END               
               
               
               ELSE: donothing=0
               ENDCASE
        
             END


   "PDMENU":BEGIN
	     CASE event.value OF 

		"Image.Load": BEGIN
		 f=wload()
		 IF N_ELEMENTS(h_array) EQ 0 THEN RETURN
                 WIDGET_CONTROL,dis.id_ln,SET_VALUE = name

         	 WIDGET_CONTROL,dis.id_min,GET_VALUE=sminv
	         WIDGET_CONTROL,dis.id_max,GET_VALUE=smaxv
		 min_img = float(sminv(0))
		 max_img = float(smaxv(0))
                 WSET,dis.win1 
                 index = n_elements(h_array)-1
		 tvscl_image,index,vtop,dswitch=ff_dswitch,win1id=uvd.draw1
                 IF N_ELEMENTS(a3) EQ 0  THEN BEGIN
                   WSET,dis.win2 & ERASE & WSET,dis.win3 & ERASE 
                   WSET,dis.win4 & ERASE & WSET,dis.win1
                 ENDIF ELSE BEGIN
                   WSET,dis.win3 
                   ERASE
                   TVSCL,a3>min_img<max_img,FIX((h.r1col-21)/8),FIX((h.r1row-1)/8) 
                   WSET,dis.win1
                 ENDELSE
                 n_frames = N_ELEMENTS(h_array)
			 END

      "Image.Save": BEGIN

                      file_name = name
		      file_name = wvaredit(filename=file_name)
                      IF file_name NE 'Cancel' THEN BEGIN
                      
                      wdir = GETENV('WORK')+!delimiter+GETENV('USER')+!delimiter+'fits'+!delimiter
                      
                      IF chk_dir(wdir) EQ 0 THEN BEGIN
                        wdir = GETENV('HOME') + !delimiter
                      ENDIF

                      WRITEFITS,wdir+file_name,image,head
                      
                      ENDIF
                    END

      "Image.Save as": BEGIN
		   	IF N_ELEMENTS(image) EQ 0 THEN RETURN
                        wsave2,image,name,head,1,gct=gamma
                       END

      "Image.Layout": BEGIN

		       IF N_ELEMENTS(image) EQ 0 THEN RETURN
                       wlayout
                   
                      END

      "Image.Print": BEGIN 
                      wprint
                     END

      "Image.Exit": BEGIN

                     WIDGET_CONTROL, event.top, /DESTROY
                     GOTO,done             

                    END


           "Status": BEGIN
                        wimg_info3
		   END

          "Image Processing.Rotate , Transpose.-Y0  X0  (90) , No": BEGIN
               		   	      IF N_ELEMENTS(image) EQ 0 THEN RETURN

                                      image = ROTATE(image,1)
                                      name  = name + '.Rot1'
                                      chandle
                                  
                                      tvscl_image,index,vtop,dswitch=dswitch,clear=1,win1id=uvd.Draw1
                                    END
      "Image Processing.Rotate , Transpose.-X0 -Y0 (180) , No": BEGIN
		   	              IF N_ELEMENTS(image) EQ 0 THEN RETURN

                                      image = ROTATE(image,2)            
                                      name  = name + '.Rot2'
                                      chandle
                                 
                                      tvscl_image,index,vtop,dswitch=dswitch,clear=1,win1id=uvd.Draw1
                                    END
      "Image Processing.Rotate , Transpose. Y0 -X0 (270) , No": BEGIN
		   	              IF N_ELEMENTS(image) EQ 0 THEN RETURN

                                      image = ROTATE(image,3)
                                      name  = name + '.Rot3'
                                      chandle
                                     
                                      tvscl_image,index,vtop,dswitch=dswitch,clear=1,win1id=uvd.Draw1
                                    END
      "Image Processing.Rotate , Transpose. Y0  X0 None  , Yes": BEGIN
		   	              IF N_ELEMENTS(image) EQ 0 THEN RETURN

                                      image = ROTATE(image,4)
                                      name  = name + '.Rot4'
                                      chandle
                                    
                                      tvscl_image,index,vtop,dswitch=dswitch,clear=1,win1id=uvd.Draw1
                                    END
      "Image Processing.Rotate , Transpose.-X0  Y0  (90) , Yes": BEGIN
		   	              IF N_ELEMENTS(image) EQ 0 THEN RETURN

                                      image = ROTATE(image,5)
                                      name  = name + '.Rot5'
                                      chandle
                                      
                                      tvscl_image,index,vtop,dswitch=dswitch,clear=1,win1id=uvd.Draw1
                                    END
      "Image Processing.Rotate , Transpose.-Y0 -X0 (180) , Yes": BEGIN   
     		   	              IF N_ELEMENTS(image) EQ 0 THEN RETURN
  
                                      image = ROTATE(image,6)
                                      name  = name + '.Rot6'
                                      chandle
                                     
                                      tvscl_image,index,vtop,dswitch=dswitch,clear=1,win1id=uvd.Draw1
                                    END
      "Image Processing.Rotate , Transpose. X0 -Y0 (270) , Yes": BEGIN
		   	              IF N_ELEMENTS(image) EQ 0 THEN RETURN

                                      image = ROTATE(image,7)
                                      name  = name + '.Rot7'
                                      chandle
                                     
                                      tvscl_image,index,vtop,dswitch=dswitch,clear=1,win1id=uvd.Draw1
                                    END

      "Image Processing.Transpose": BEGIN
		   	              IF N_ELEMENTS(image) EQ 0 THEN RETURN
                                      image = ROTATE(image,4)
                                      name  = name + '.Transpose'
                                      chandle
                                    
                                      tvscl_image,index,vtop,dswitch=dswitch,clear=1,win1id=uvd.Draw1
                                    END
 
      "Image Processing.Flip Image": BEGIN
		   	              IF N_ELEMENTS(image) EQ 0 THEN RETURN
                                      image = ROTATE(image,7)
                                      name  = name + '.Flip'
                                      chandle
                                     
                                      tvscl_image,index,vtop,dswitch=dswitch,clear=1,win1id=uvd.Draw1
                                     END

      "Image Processing.Rebin": BEGIN

		   	         IF N_ELEMENTS(image) EQ 0 THEN RETURN                                 ;wrebin
      
                                 image = REBIN(image,Di,Di)
                                 chandle

                                 tvscl_image,index,vtop,dswitch=dswitch,clear=1,win1id=uvd.Draw1
                                END

      "Image Processing.Define Box": BEGIN
      
		 IF N_ELEMENTS(image) EQ 0 THEN RETURN  
                 sar = ["", $
                        "Left mouse button:   move box around.", $
                        "Middle mouse button: resize box",  $
                        "Right mouse button:  done."]

                 mes = wyes_no(1,title="Define Box",text=sar,xs=40,ys=8)

                 IF mes.yn EQ "Cancel" THEN RETURN
                 
                 IF box.xbl OR box.ybh EQ 0 THEN BEGIN        
                   ; if box size is not defined, define it!
                                     
                   box.xbl = sz(1)/4
                   box.ybh = sz(2)/4   
                                      
                   box.x = sz(1)/2 - box.xbl/2
                   box.y = sz(2)/2 - box.ybh/2
                                 
                 ENDIF
       
                  x0 = box.x 
                  y0 = box.y 
                  
                  xbl = box.xbl 
                  ybh = box.ybh

                  IF dis.ff_dswitch EQ 1 THEN BEGIN
                      x0 = x0 + (h.r1col-21)  
                      y0 = y0 + (h.r1row-1 )                   
                  ENDIF       
               
TryAgain:         BOX_CURSOR,x0,y0,xbl,ybh,INIT = 1

                  res = wbox(x0,y0,xbl,ybh)

                  IF res.yn EQ "Cancel" THEN RETURN

                  x0  = res.x0
                  xbl = res.xbl
                  y0  = res.y0
                  ybh = res.ybh

                  box.x = x0  
                  box.y = y0
                  
                  box.xbl = xbl 
                  box.ybh = ybh
                   
                  IF res.yn EQ 'No' THEN GOTO,TryAgain
                  

                  n_frames = N_ELEMENTS(sel_array)
                  FOR i=0,n_frames-1 DO BEGIN

                   frame = sel_array(i)
                  ; IF frame NE frame_s THEN BEGIN ; *** this is a bad
                                ; solution but it works.
                                ; better without IF...

		   ghandle,frame
          
                   r1col = (FXPAR(head,"R1COL")-21) > 0
                   r1row = (FXPAR(head,"R1ROW") -1) > 0

                  IF dis.ff_dswitch EQ 1 THEN BEGIN
                   x1 = x0  
                   y1 = y0                    
                  ENDIF ELSE BEGIN                                          
                   x1 = x0 - r1col
                   y1 = y0 - r1row
                  ENDELSE 
                  
                   x2 = x1 + xbl-1
                   y2 = y1 + ybh-1

                   x1 = x1 > 0 < (sz(1)-32)
                   x2 = x2 > 0 < (sz(1)-1)

                   y1 = y1 > 0 < (sz(2)-32)
                   y2 = y2 > 0 < (sz(2)-1)

                   w = image(x1:x2,y1:y2)

                   ; next two lines for Rainer discovering Jupiter
                   ; bp 25-Feb-1997
                   help,w
                   print,"Total: ",total(w), " Name: ",name

                   image = TEMPORARY(w)
                   name = name + '.box'
                   
                   sz = size(image)
                   fxaddpar,head,'NAXIS1',sz(1)
                   fxaddpar,head,'NAXIS2',sz(2)
                   fxaddpar,head,'R1COL',x1 + FXPAR(head,"R1COL")
                   fxaddpar,head,'R1ROW',y1 + FXPAR(head,"R1ROW")
                   
                   fxaddpar,head,'R2COL',x2-x1 + FXPAR(head,"R1COL")
                   fxaddpar,head,'R2ROW',y2-y1 + FXPAR(head,"R1ROW")

                   chandle

                   erase
                   ;tvscl_image,index,vtop
                   ;tvscl_image,index,vtop,dswitch=dswitch
                   tvscl_image,index,vtop,dswitch=dswitch,clear=1,win1id=uvd.Draw1

              ; ENDIF
              ENDFOR             
      END
 
      "Image Processing.Find Limb": BEGIN
		
                                      IF N_ELEMENTS(image) EQ 0 THEN RETURN
                                      scan4limb,image,name,head,h_head,index,1,mini=min_img,maxi=max_img
         			    END

      "Image Processing.Rescale Images": BEGIN
                  wimg_info3
                 IF N_ELEMENTS(string_name) LT 2 THEN BEGIN
          ;        wimg_info1
                  msg = WIDGET_MESSAGE("Select at least two images.")
                  print,"o Select at least 2 images or :"
                  print,"o Input Sun radius and Sun center coordinates " + $
                    ""
                  GOTO,end_ipr
                 ENDIF
             
                  ; ** find index of the selected image to scale to.

                  t1 = WHERE( string_name EQ name )
                  t1 = t1(0)
                  IF t1 LT 0 THEN RETURN

                  t2 = WHERE( sel_array EQ t1 )
                  t2 = t2(0)
                  IF t2 LT 0 THEN RETURN

                  frame_s = sel_array(t2)

                  HANDLE_VALUE,h_array(frame_s),img1
   	          HANDLE_VALUE,h_name(frame_s),name1
       	          HANDLE_VALUE,h_head(frame_s),head1

		   detector = SXPAR(head1,'DETECTOR')
		   detector = STRCOMPRESS(detector,/REMOVE_ALL)

		   disk = 1.1  ; default

		   CASE detector OF

		     'C1':  BEGIN 
		            ; mf = 'mask_c1.fts'
		             disk = 1.1
		            END
		     'C2':  BEGIN
		             ;mf = 'mask_c2.fts'
		             disk = 2.0
		            END
		     'C3':  BEGIN
		             ;mf = 'mask_c3.fts'
		             disk = 3.7
		            END
		   ELSE: mf = 'mask_c1.fts'
		   ENDCASE

                  scan4limb,img1,name1,head1,h_head,frame_s,0,mini=min_img,maxi=max_img
	 
                  x1 = x0 & y1 = y0 & r1 = dr
                  n_frames = N_ELEMENTS(sel_array)

                 FOR i=0,n_frames-1 DO BEGIN

                  frame = sel_array(i)
                  IF frame NE frame_s THEN BEGIN ; *** this is a bad
                                ; solution but it works.
                                ; better without IF...
                  HANDLE_VALUE,h_array(frame),img2
   	          HANDLE_VALUE,h_name(frame),name2
       	          HANDLE_VALUE,h_head(frame),head2

                  scan4limb,img2,name2,head2,h_head,frame,0,mini=min_img,maxi=max_img
                  x2 = x0 & y2 = y0 & r2 = dr

                  wset,dis.win1

                  rescale,img1,x1,y1,r1,img2,x2,y2,r2,image

                  head = TEMPORARY(head2)

                  name = name2 + ".scale"

                  SXADDPAR,head,'HISTORY','=' + "'" + name2 + "'" + '/'
                  SXADDPAR,head,'OCLTR_X', x1
                  SXADDPAR,head,'OCLTR_Y', y1 
                  SXADDPAR,head,'OCLTR_R', r1
                  SXADDPAR,head,'HISTORY','=' + "'" + name1 + "'" + '/'
  
                  chandle

                  erase               
                  tvscl_image,index,vtop,dswitch=dswitch,clear=1,win1id=uvd.Draw1

                 ENDIF
                 ENDFOR
                 
           end_ipr:
          END

      "Image Processing.Rotate Sun": BEGIN
		   	              IF N_ELEMENTS(image) EQ 0 THEN RETURN
                                      date_obs = '1995/02/28 12:00'
                                      xvaredit,date_obs
                                      ang = pb0r(date_obs)
                                      ang = ang(0)
                                      
                                      scan4limb,image,name,head,h_head,index,0,mini=min_img,maxi=max_img

                                      image = $
                                        ROT(image,ang,1.0,x0,y0,/INTERP,/PIVOT)

                                      name = name + ".rot"
                                      chandle
                                      
                                      ;tvscl_image,index,vtop
                                      ;tvscl_image,index,vtop,dswitch=dswitch
                                      tvscl_image,index,vtop,dswitch=dswitch,clear=1,win1id=uvd.Draw1

                                     END

      "Image Processing.Cartesian to polar": BEGIN

         wc2p,1
 
         wset,dis.win1 & erase 
         ;tvscl_image,index,vtop
         ;tvscl_image,index,vtop,dswitch=dswitch
         tvscl_image,index,vtop,dswitch=dswitch,clear=1,win1id=uvd.Draw1

		 END

      "Image Processing.Polar to cartesian": BEGIN

         wc2p,-1
 
         wset,dis.win1 & erase 
         ;tvscl_image,index,vtop
         ;tvscl_image,index,vtop,dswitch=dswitch
         tvscl_image,index,vtop,dswitch=dswitch,clear=1,win1id=uvd.Draw1

		 END

      "Image Processing.Radial filter": BEGIN
		   	              IF N_ELEMENTS(image) EQ 0 THEN RETURN
              c = image
              s = TOTAL(image, 1)

              FOR i=0,sz(2)-1 DO BEGIN
                c(*,i) = image(*,i) / s(i)
              ENDFOR
              image = TEMPORARY(c)
              name  = name + '.rf'
              chandle
              ;tvscl_image,index,vtop
              ;tvscl_image,index,vtop,dswitch=dswitch
              tvscl_image,index,vtop,dswitch=dswitch,clear=1,win1id=uvd.Draw1

		 END
      
	   "Image Processing.Calculator": BEGIN
		  	  wcalcl
;	                  ;wtv_fits,dis.id_th,head,lcol2
		 	 END
		 	 
                "Image Processing.Exposures": begin
;;                    w_las_exp_norm, group = event.top
                    w_exp_norm, group = event.top
                end
                
                "Image Processing.Make standard image.From Loaded Images": begin
                    w_mk_stdim, group = event.top
                end
                "Image Processing.Make standard image.Search headers": begin
                    w_stdim_list, group = event.top
                end
                
                "Image Processing.Subtract standard": begin
                    w_poly_diffim, group = event.top
                end

                "Image Processing.Running differences": begin

                  n_frames = N_ELEMENTS(sel_array)
                  IF n_frames LT 2 THEN RETURN

                  FOR i=0,n_frames-2 DO BEGIN

                   frame = sel_array(i)
		   ghandle,sel_array(i)
                   image0 = image
                   name0  = SXPAR(head,"FILENAME")
                   ghandle,sel_array(i+1)
                   image = image - image0
                   
                
                   name = name + '-' + name0
                   
                   SXADDPAR,head,'HISTORY','=' + "'" + name + "'" + '/'

                   chandle

		  ENDFOR
                  undefine,image0
                end


           "Image Processing.Remove cosmic rays": BEGIN

                  n_frames = N_ELEMENTS(sel_array)
                  FOR i=0,n_frames-1 DO BEGIN

                   frame = sel_array(i)
                  ; IF frame NE frame_s THEN BEGIN ; *** this is a bad
                                ; solution but it works.
                                ; better without IF...

		   ghandle,frame

                          image = ccosmics(image)
                          name = name + ".cr"
                   chandle
                   tvscl_image,index,vtop,dswitch=dswitch,clear=1,win1id=uvd.Draw1

                  ENDFOR
                                                  END
		 	 
           "Plots.Plot": BEGIN
		   	  IF N_ELEMENTS(image) EQ 0 THEN BEGIN
			   wimg_info1
		   	   RETURN
	  	          ENDIF
                          wplot1
                         END

      "Plots.Histogram": BEGIN
		   	  IF N_ELEMENTS(image) EQ 0 THEN BEGIN
			   wimg_info1
		   	   RETURN
	  	          ENDIF
                         ; whist
                 	 END

       "Plots.Contours": BEGIN
		   	  IF N_ELEMENTS(image) EQ 0 THEN BEGIN
			   wimg_info1
		   	   RETURN
	  	          ENDIF
                          wic,gct=gamma
                 	 END

       "Plots.Lines": BEGIN
		   	  IF N_ELEMENTS(image) EQ 0 THEN BEGIN
			   ;wimg_info1
		   	   RETURN
	  	          ENDIF
                           wlines                          
                 	 END


     "Plots.Radial Beam": BEGIN
		  	  IF N_ELEMENTS(image) EQ 0 THEN RETURN


			;x0=256
			;y0=256
			;image_profile = PROFILE1(image,xx,yy,xc=x0,yc=y0)
			;plot,image_profile

                           ;wr_beam1
                           
                         ; tmp = size(image) 
                         ; print,tmp
                         ; WIDGET_CONTROL,uvd.Draw1,DRAW_XSIZE=tmp(1),DRAW_YSIZE=tmp(2), $
                         ;               XSIZE=tmp(1),YSIZE=tmp(2),SET_DRAW_VIEW=[0,0]
                         ; TVSCL,image>min_img<max_img
                          ;dis.ff_dswitch = 0
			  ;wsunprofile
                	 END

     "Plots.Height-Time": BEGIN
                                id = HANDLE_INFO(trackhandle)  
                           
				if id(0) eq 0  then begin

				 msg = WIDGET_MESSAGE("No data present! Turn Time Tracker on, colect data and come back.", $
  				    /INFORMATION, TITLE= 'Time Tracker is Off!')

				 return
				endif     
  		    	           HANDLE_VALUE, trackhandle, trackarr

				if n_elements(trackarr) eq 0 then begin		

				 msg = WIDGET_MESSAGE("No data present! Turn Time Tracker on, colect data and come back.", $
 				     /INFORMATION, TITLE= 'Time Tracker is Off!')

				 return
				endif   
                         
				if trackarr(current,1) eq 0 then begin

				 msg = WIDGET_MESSAGE("No data present! Turn Time Tracker on, colect data and come back.", $
  				    /INFORMATION, TITLE= 'Time Tracker is Off!')

 				return
				endif                            
                  	        tmp = arrtable2string(trackarr)
               		        wtime_height,table=tmp                           
     	                 END

       "Plots.Distance": BEGIN
       		  	  IF N_ELEMENTS(image) EQ 0 THEN RETURN

                          tmp = size(image) 

                          dis.ff_dswitch = 1

                          tvscl_image,index,vtop,dswitch=dis.ff_dswitch,clear=1,win1id=uvd.Draw1
			  wsunprofile
                	 END

      "Graphics.Adjust": BEGIN
       		 	  ;TVLCT, r_orig, g_orig, b_orig
		  	  ADJCT
                	 END

       "Graphics.Color": BEGIN
        	  	  ;TVLCT, r_orig, g_orig, b_orig
                          gamma_ct,gamma ,/CURRENT
		  	  XLOADCTBP ,/USE_CURRENT
		  	  ;WIDGET_CONTROL,uv
		  	  WIDGET_CONTROL,uvd.Gamma_label, $
                                      SET_VALUE=STRING(gamma,format='(f6.3)')
		  	  ;XLOADCT,/USE_CURRENT
		  	  ;XLOADCTBP
                	 END

     "Graphics.Palette": BEGIN
        	  	  ;TVLCT, r_orig, g_orig, b_orig
		  	  XPALETTE
                	 END


     "Options.Edit Header": BEGIN
     
                             wedithdr,head   

                             h.r1col = FXPAR(head,"R1COL") 
                             h.r1row = FXPAR(head,"R1ROW")
                      
                             frame = WHERE( string_name EQ name ) 
                             frame = TEMPORARY(frame(0))
                             shandle,image,head,frame

                             ;tvscl_image,frame,vtop,dswitch=dis.ff_dswitch
                             tvscl_image,frame,vtop,dswitch=dswitch,clear=1,win1id=uvd.Draw1
                            END

    "Options.View Catalog": BEGIN
                             IF n_elements(tmps) GT 0 THEN whdrinfo1,txt=tmps ELSE whdrinfo1
		           END

	  "Read Cursor": BEGIN
		   	  IF N_ELEMENTS(image) EQ 0 THEN BEGIN
				wimg_info1
		   		RETURN
	  	   	  ENDIF
	
		   	  wcursor,image,x,y,counts,win_id=dis.win1
		   	  var_arr(0) = x
		   	  var_arr(1) = y
			 END

	   "Fast Movie": BEGIN

                 ;w_movie
		WIDGET_CONTROL,dis.id_min,GET_VALUE=sminv
		WIDGET_CONTROL,dis.id_max,GET_VALUE=smaxv
		min_img = float(sminv(0))
		max_img = float(smaxv(0))


	       		  IF N_ELEMENTS(h_array) EQ 0 THEN RETURN
	       	 	  WIDGET_CONTROL, uvd.DispBase,HOURGLASS=1
                 n_frames = N_ELEMENTS(sel_array)
                 frame = sel_array(0)
		  	        IF n_frames EQ 1 THEN RETURN
		           HANDLE_VALUE,h_array(frame),image
                 s_image = size(image)
                  	  hsize = s_image(1)
                 	  vsize = s_image(2)
                 ;hsize = 512
                 ;vsize = 512
                 names = ''
                 
                 ;WINDOW, 9, XSIZE = hsize, YSIZE = vsize*n_frames, /PIXMAP
                 win_index = INTARR(n_frames)
                 FOR j = 0, n_frames-1 DO BEGIN
                  	HANDLE_VALUE,h_array(sel_array(j)),image
                    	HANDLE_VALUE,h_name(sel_array(j)),string_temp
                   	names = [names,string_temp]
         ;           		std = STDEV(image, mn)
                        WINDOW, XSIZE=hsize, YSIZE = vsize, /PIXMAP, /FREE
                        win_index(j) = !D.WINDOW
                   	image = BYTSCL(image>min_img<max_img,TOP=!d.n_colors)
                    	TV, image
                 ENDFOR
                 

                ; WRUNMOVIE2, win_index,names=string_name(sel_array)
                 WRUNMOVIE, win_index,names=string_name(sel_array)


                 ghandle,frame
		 ;tvscl_image,frame,vtop
		 ;tvscl_image,frame,vtop,dswitch=dswitch
		 tvscl_image,frame,vtop,dswitch=dswitch,clear=1,win1id=uvd.Draw1

                 x=var_arr(0)
                 y=var_arr(1)			
   			     zoom_it,x,y,uvd.win2s,uvd.win2s,uvd.izoom,dis.win1,dis.win2,0

			 END

     		 "Help": BEGIN
       			  path_msg = GETENV('LASCO_MSG')           
        		  tmp_msg  = path_msg + '/help.msg' 
		  	  xdisplayfile,tmp_msg,height=10,width=45,title='Help'
		 	 END 	

            ELSE: donothing=0
	    ENDCASE
	END

  ; "REFRESH": BEGIN
;		pointing2
;	    END

    "CONTROL":	BEGIN
		WIDGET_CONTROL, uvd.cbase, MAP = 1
		END

   "CDISMISS":	BEGIN
		WIDGET_CONTROL, uvd.cbase, MAP = 0
		END

 
   "DONE": BEGIN
             WIDGET_CONTROL, event.top, /DESTROY
             GOTO,done             
           END

	    "PS": out_win   = 'PS'          ; write Post Script file
	     "X": out_win   = !d.name       ; output to screen

     "DRAW4":   BEGIN
                        
                 IF n_elements(image) eq 0 then RETURN
                 tn = Tag_Names( event, /STRUCTURE_NAME )
                 CASE tn OF
                        'WIDGET_DRAW':  BEGIN

                 IF (event.press NE 0 ) THEN uvd.press = event.press
                 IF (event.release NE 0 ) THEN uvd.press = 0B
                 IF (uvd.press NE 0 ) THEN BEGIN
                 CASE uvd.press OF
                   1B: BEGIN     ;** Left mouse button 
                        uvd.ltbt = 0
                        wset,dis.win4
                        y = var_arr(1)
                        y = 0 > y < (sz(2)-1)
                        plot,image(*,y),XTICKS=2,YTICKFORMAT='(I5)', $
                          POS=[0.25,0.15,.95,.85],XSTYLE=1,NSUM=2,   $ 
                          TITLE = "LEFT TO RIGHT" 
                        wset,dis.win1
                       END
                   2B: BEGIN
                        wplot1
                       END
                   4B: BEGIN       ;** Right mouse button
                        uvd.ltbt = 1 
                        wset,dis.win4
                        x = var_arr(0)
                        x = 0 > x < (sz(1)-1)
                        plot,image(x,*),XTICKS=2,YTICKFORMAT='(I5)', $
                          POS=[0.25,0.15,.95,.85],XSTYLE=1,NSUM=2,   $
                          TITLE = "BOTTOM TO TOP"
                        wset,dis.win1
                       END
                       ELSE: donothing = 0
                 ENDCASE
                ENDIF
       
           ;WIDGET_CONTROL, event.top, SET_UVALUE=uvd ; ** set uvd.press to new value
;!ZARKS
	   HANDLE_VALUE, info_hand, uvd, /SET
          END ; end of 'WIDGET_DRAW'

                 ELSE: donothing = 0
                 ENDCASE
 
                END ; End DRAW4

     "DRAW3":   BEGIN
;                IF n_elements(image) eq 0 then RETURN
                 tn = Tag_Names( event, /STRUCTURE_NAME )
                 CASE tn OF
                        'WIDGET_DRAW':  BEGIN

                                         IF (event.press NE 0 ) THEN uvd.press = event.press
                                         IF (event.release NE 0 ) THEN uvd.press = 0B
                IF (uvd.press NE 0 ) THEN BEGIN
                 CASE uvd.press OF
                   1B: BEGIN     ;** Left mouse button 
                        ;swx = event.x*8 ; - uvd.w1xss/2 ; version 2 (chandle)
                        ;swy = event.y*8 ; - uvd.w1yss/2

                        swx = event.x*8  - uvd.w1xss/2
                        swy = event.y*8  - uvd.w1yss/2

                        swx = event.x * sz(1)/128
                        swy = event.y * sz(2)/128

			zoom_it,swx,swy,uvd.win2s,uvd.win2s,uvd.izoom,dis.win1,dis.win2,1

                        IF (XREGISTERED("wzoom_it")  NE 0) THEN  $
       			zoom_it,swx,swy,512,512,uvd.izoom,dis.win1,zw_id,0

                       END
                   2B: BEGIN
                        swx = event.x*8 - uvd.w1xss/2
                        swy = event.y*8 - uvd.w1yss/2
                        WIDGET_CONTROL,uvd.Draw1,SET_DRAW_VIEW=[swx,swy]	
                       END

                   4B: BEGIN       ;** Right mouse button

                        ; dis.ff_dswitch = 1 -> screen size = image size
                        ; dis.ff_dswitch = 0 -> screen size = (1024,1024), scroll bar enabled
                        
                        IF dis.ff_dswitch EQ 0 THEN dis.ff_dswitch = 1 ELSE dis.ff_dswitch = 0

                        IF dis.ff_dswitch EQ 1 THEN BEGIN

                          tmp = size(image) 
                          uvd.win1_xs = tmp(1)
                          uvd.win1_ys = tmp(2)

                          HANDLE_VALUE, info_hand, uvd, /SET       
                          WIDGET_CONTROL,uvd.Draw1,DRAW_XSIZE=tmp(1),DRAW_YSIZE=tmp(2), $
                                                   XSIZE=tmp(1) ,YSIZE=tmp(2)
                          widget_control, uvd.DispBase,/update                          
                          IF wttrack EQ 1 THEN dswitch = 0 ELSE dswitch = dis.ff_dswitch
                                     
                          frame = sel_array(current)                
	                  tvscl_image,frame,vtop,dswitch=dswitch,clear=1,win1id=uvd.Draw1                                               
                        ENDIF ELSE BEGIN

                          uvd.win1_xs = 1024 ; uvd.w1xss ; 1024
                          uvd.win1_ys = 1024 ; uvd.w1yss ; 1024
 
                          HANDLE_VALUE, info_hand, uvd, /SET 
		          WIDGET_CONTROL,uvd.Draw1,DRAW_XSIZE=uvd.win1_xs,DRAW_YSIZE=uvd.win1_ys, $
                                        XSIZE=uvd.w1xss,YSIZE=uvd.w1yss ,SET_DRAW_VIEW=[0,0] 
                          widget_control, uvd.DispBase,/update     
                          IF wttrack EQ 1 THEN dswitch = 0 ELSE dswitch = dis.ff_dswitch
                        
                          frame = sel_array(current)
	                  tvscl_image,frame,vtop,dswitch=dswitch,clear=1,win1id=uvd.Draw1                         
      
                        ENDELSE                         
                       END
                       ELSE: donothing = 0
                 ENDCASE
                ENDIF

	   HANDLE_VALUE, info_hand, uvd, /SET	
          END ; end of 'WIDGET_DRAW'

                 ELSE: donothing = 0
                 ENDCASE
 
                END ; End DRAW3


     "DRAW2":   BEGIN
;	         IF n_elements(image) eq 0 then RETURN
		 tn = Tag_Names( event, /STRUCTURE_NAME )
		 CASE tn OF
			'WIDGET_DRAW':  BEGIN

  	         IF (event.press NE 0 ) THEN uvd.press = event.press
        	 IF (event.release NE 0 ) THEN uvd.press = 0B
       		 IF (uvd.press NE 0 ) THEN BEGIN
                 ; x = 0 > event.x < (uvd.ms-1)
                 ; y = 0 > event.y < (uvd.ms-1)
                 CASE uvd.press OF
                   1B: BEGIN     ;** Left mouse button - increment zoom
                 
                        uvd.izoom = uvd.izoom + 1
                        IF uvd.izoom GT 15 THEN uvd.izoom = 1
 
                        a0 = uvd.a0
                        x=var_arr(0)
                        y=var_arr(1)

			zoom_it,x,y,uvd.win2s,uvd.win2s,uvd.izoom,dis.win1,dis.win2,0

                        IF (XREGISTERED("wzoom_it")  NE 0) THEN  $
       			zoom_it,x,y,512,512,uvd.izoom,dis.win1,zw_id,0
                        ;WIDGET_CONTROL, event.top, SET_UVALUE=uvd
                        HANDLE_VALUE, info_hand, uvd, /SET

                       END
                   2B: BEGIN
                        wzoom_it,image,name,head
                        
                        x=var_arr(0)
                        y=var_arr(1)

         		         zoom_it,x,y,512,512,uvd.izoom,dis.win1,zw_id,0

                       END
                   4B: BEGIN       ;** Right mouse button - decrement zoom
                        uvd.izoom = uvd.izoom -1
                        IF uvd.izoom LT 2 THEN uvd.izoom = 1
   
                        a0 = uvd.a0
                        x=var_arr(0)
                        y=var_arr(1)

			zoom_it,x,y,uvd.win2s,uvd.win2s,uvd.izoom,dis.win1,dis.win2,0
 ;WIDGET_CONTROL, event.top, SET_UVALUE=uvd
  HANDLE_VALUE, info_hand, uvd, /SET

            IF (XREGISTERED("wzoom_it")  NE 0) THEN  $
       			zoom_it,x,y,512,512,uvd.izoom,dis.win1,zw_id,0

                       END
                       ELSE: donothing = 0
                 ENDCASE
			    ENDIF
        
			;WIDGET_CONTROL, event.top, SET_UVALUE=uvd ; ** set uvd.press to new value
 			HANDLE_VALUE, info_hand, uvd, /SET
          END ; end of 'WIDGET_DRAW'
		 ELSE: donothing = 0
		 ENDCASE

		END ; End DRAW2
  
     "DRAW1": 	BEGIN
	         IF n_elements(image) eq 0 then RETURN
		 tn = Tag_Names( event, /STRUCTURE_NAME )
		 CASE tn OF
			'WIDGET_DRAW':  BEGIN

;                          x = 0 > event.x < (sz(1)-1)
;                          y = 0 > event.y < (sz(2)-1)

;                          x = (h.r1col-21) > event.x < (h.r2col-1)
;                          y = (h.r1row- 1) > event.y < (h.p2row-1)

                          xi = 0 > event.x < uvd.win1_xs
                          yi = 0 > event.y < uvd.win1_ys  ;(h.r1row+sz(2)-1)
                          
                         
                          IF dis.ff_dswitch EQ 1 THEN BEGIN
                           xi = xi + (h.r1col-21)  
                           yi = yi + (h.r1row-1 )
                           x  = xi
                           y  = yi                      
                          ENDIF                          
                                                   
                          x = xi - ( h.r1col-21 )
                          y = yi - ( h.r1row-1  )

                          x = 0 > x < (sz(1)-1)
                          y = 0 > y < (sz(2)-1)

			  sx = STRCOMPRESS(STRING(event.x))
			  sy = STRCOMPRESS(STRING(event.y))

                          svalue = " ("+STRING(x,FORMAT='(I4)')+","+ STRING(y,FORMAT='(I4)')+")" + $
                                        STRCOMPRESS(STRING(FLOAT(image(x,y)),FORMAT='(F10.3)'))
                          
                         ;htmp = head
                         ;sc_str = get_sun_center(htmp)
                         ;sc = [sc_str.xcen,sc_str.ycen]
                         sc = occltr_cntr(head)
                         IF sc(0) EQ -1 THEN sc = [512.0,512.0] ; default for 'Telescope not recognized'
                         ;head = htmp

                         r1row = (FXPAR(head,"R1ROW") - 1) > 0
                         r1col = (FXPAR(head,"R1COL") -21) > 0
                         
                         IF ( STRCOMPRESS(FXPAR(head,"DETECTOR"),/REMOVE_ALL) EQ 'C2') THEN BEGIN
                             sc(0) = sc(0) + 1.5
                             sc(1) = sc(1) + 1.5
                         ENDIF
                         
                         sc_x= sc(0) ;+ r1col
                         sc_y= sc(1) ;+ r1row

                         ;IF dis.ff_dswitch EQ 1 THEN BEGIN
                         ; xi = xi + (h.r1col-21)  
                         ; yi = yi + (h.r1row-1 )                      
                         ;ENDIF

                         ;r = SQRT( ABS(sc_x-xi)^2 + ABS(sc_y-yi)^2 )/ get_det_radius(FXPAR(head,"DETECTOR"))
                         r = SQRT( ABS(sc_x-xi)^2 + ABS(sc_y-yi)^2 )/solar_radius

                         theta = atan((sc_y-yi)/(sc_x-xi)) * !RADEG

                         IF theta GT 0 AND (xi-sc_x) GT 0 THEN theta = 270. + theta
                         IF theta LT 0 AND (xi-sc_x) GT 0 THEN theta = 270. + theta
                         IF theta GT 0 AND (xi-sc_x) LT 0 THEN theta = 90. + theta
                         IF theta LT 0 AND (xi-sc_x) LT 0 THEN theta = 90. + theta

                         svalue = svalue +  " " + STRCOMPRESS(STRING(r,FORMAT='(F8.3)'),/REMOVE_ALL)+' Rs,'+ $
                                  " PA="+ STRCOMPRESS(STRING(theta,FORMAT='(F6.2)'),/REMOVE_ALL)

			 svalue = STRMID(svalue,0,39)

			 WIDGET_CONTROL,uvd.Cur,SET_VALUE=svalue
                         widget_control, uvd.DispBase,/update

                        IF (event.press NE 0 ) THEN uvd.press = event.press
                        IF (event.release NE 0 ) THEN uvd.press = 0B
                        IF (uvd.press NE 0 ) THEN BEGIN
	               ;1(origin crash)	HANDLE_VALUE, trackhandle, trackarr
            CASE uvd.press OF
              1B: BEGIN

			IF wttrack EQ 1 THEN cross = 1 ELSE cross = 0
		;2(second place crash)   HANDLE_VALUE, trackhandle, trackarr                        
                        if cross eq 1 then begin
 		HANDLE_VALUE, trackhandle, trackarr                         
                        ;Check if picture has already been crossed

                        	if trackarr(current, 17) EQ 0 then begin
                        	
                        	trackarr(current, 17) = 1	; show that Picture has been crossed
                        	HANDLE_VALUE, trackhandle, trackarr, /SET
                        	endif else begin
                        	
                        ;Delete the old cross if one existed
                        	          	
                        	xold = FIX(trackarr(current, 1)) ;get old x and y
                        	yold = FIX(trackarr(current, 2))
                        	
                        	image(xold-3:xold+3, yold:yold) = trackarr(current, 3:9)
                           	image(xold:xold, yold-3:yold+3) = trackarr(current, 10:16)
                           	                           	
                           	endelse

                           x = x > 3 < (sz(1)-4) 
                           y = y > 3 < (sz(2)-4) 
                           
                           ;*** draw a cross 6x6 pixels in the center
                           cross_x =  image(x-3:x+3,y:y)
                           cross_y =  image(x:x,y-3:y+3)
                           
                           ;save old info before you draw the cross
			   trackarr(current,  3: 9) = cross_x
			   trackarr(current, 10:16) = cross_y
			   trackarr(current, 1) = x
			   trackarr(current, 2) = y
			   trackarr(current,19) = image(x,y)
			   trackarr(current,20) = r
			   trackarr(current,21) = theta
			   HANDLE_VALUE, trackhandle, trackarr, /SET
                           image(x-3:x+3,y:y) = 255    ;max(a)
                           image(x:x,y-3:y+3) = 255    ;max(a)
			   HANDLE_VALUE, h_array(sel_array(current)),image,/SET

                           TVSCL,image>min_img<max_img,h.r1col-21,h.r1row-1
                          
                        endif

                        IF XREGISTERED("wplot1") NE 0 OR XREGISTERED("wlines") THEN BEGIN
                        	var_arr(0) = x 
                        	var_arr(1) = y
                        ENDIF
                        IF XREGISTERED("wsunprofile") NE 0 THEN BEGIN
                        	var_arr(0) = x 
                        	var_arr(1) = y 
                        ENDIF	
                        IF wttrack EQ 1 THEN BEGIN
                        	var_arr(0) = xi 
                        	var_arr(1) = yi
                        ENDIF

                   ifact = uvd.izoom 

		   zoom_it, xi, yi, uvd.win2s, uvd.win2s, ifact, dis.win1, dis.win2,1

                   IF (XREGISTERED("wzoom_it")  NE 0) THEN $
       		      zoom_it,xi,yi,512,512,uvd.izoom,dis.win1,zw_id,0

                   IF flag_limb GT 0 AND flag_limb LT 4 THEN BEGIN
                    limb_x(flag_limb-1) = event.x
                    limb_y(flag_limb-1) = event.y
                    
                    IF flag_limb EQ 3 THEN BEGIN
                      CIR_3PNT,limb_x,limb_y,dr,x0,y0

                      theta = (findgen(360)/360) * 6.2832

                      xc = ((x0 + (dr * COS(theta)))>0.)<(sz(1)-1)
                      yc = ((y0 + (dr * SIN(theta)))>0.)<(sz(2)-1) 

                      image(xc,yc) = max(image)
                      TVSCL,image>min_img<max_img ; write in image and in header ?

                      ;sun_center,limb_x,limb_y,rcpix1,rcpix2
                    ENDIF

                    flag_limb = flag_limb + 1

                   ENDIF

                   wset,dis.win4

                   IF uvd.ltbt EQ 0 THEN BEGIN
                    plot,image(*,y),XTICKS=2,YTICKFORMAT='(I5)', $ 
                          YRANGE=[min_img,max_img], $
                          POS=[0.25,0.15,.95,.85],XSTYLE=1,NSUM=2,   $
                          TITLE = "LEFT TO RIGHT"
                   ENDIF ELSE BEGIN
                     plot,image(x,*),XTICKS=2,YTICKFORMAT='(I5)', $
                          YRANGE=[min_img,max_img], $ 
                          POS=[0.25,0.15,.95,.85],XSTYLE=1,NSUM=2,   $
                          TITLE = "BOTTOM TO TOP"
                   ENDELSE
                   wset,dis.win1

                  END

              2B: BEGIN ; gamma correction

                   IF wttrack NE 1 THEN BEGIN

                    WIDGET_CONTROL,uvd.Draw1,GET_DRAW_VIEW = xy

                    xt = event.x > xy(0) < ( uvd.w1xss + xy(0) )
                    xt =  (xt-xy(0)) / (uvd.w1xss/2.0) - 1

                    ;gamma = 10^(TEMPORARY(xt))
                    gamma = 10^((xt))
                    WIDGET_CONTROL,uvd.Gamma_label, $
                         SET_VALUE=STRING(gamma,format='(f6.3)')
                    gamma_ct,gamma

		    IF XREGISTERED("xloadctbp") GT 0 THEN BEGIN
		       WIDGET_CONTROL,g_slider,SET_VALUE=(xt+1)*50
		       
                       WIDGET_CONTROL, g_lbl, SET_VALUE = $
			     STRING(gamma, format='(f6.3)')
		    ENDIF			     
                  ENDIF ELSE BEGIN                    
                                       
                   uvd.movie = 0

		   HANDLE_VALUE, info_hand, uvd, /SET

                   last = N_ELEMENTS(sel_array) - 1
                   current = current + 1
                   IF (current GT last) THEN current = first ;** out of range error check
                   frame = sel_array(current)
                  
                   IF wttrack EQ 1 THEN dis.ff_dswitch = 0   
                   ;tvscl_image,frame,vtop,dswitch=dis.ff_dswitch
                   tvscl_image,frame,vtop,dswitch=dswitch,win1id=uvd.Draw1
 
                   xi=var_arr(0) + ( h.r1col-21 )
                   yi=var_arr(1) + ( h.r1row- 1 )
                    			
		   zoom_it,xi,yi,uvd.win2s,uvd.win2s,uvd.izoom,dis.win1,dis.win2,0
		  
                  ENDELSE		  
                   
                   
                  END

              4B: BEGIN       ;** Right mouse button; adjust color table
                   		
                  ;check if wttrack is active
                  IF wttrack EQ 1 THEN BEGIN
                      HANDLE_VALUE, trackhandle, trackarr
                  	;check if there is a cross
                   	IF trackarr(current,17) EQ 1 THEN BEGIN
                   	
                   	;if there is a cross, read in the old values to remove it.
                   	xold = trackarr(current,1) ;get old x and y
                        yold = trackarr(current,2)
                        image(xold-3:xold+3,yold:yold) = trackarr(current, 3:9)
                        image(xold:xold,yold-3:yold+3) = trackarr(current, 10:16)	
                        trackarr(current,17) = 0
                        trackarr(current,20) = 0
                        trackarr(current,21) = 0
                        HANDLE_VALUE, trackhandle, trackarr, /SET 
			HANDLE_VALUE, h_array(sel_array(current)),image,/SET
			  ; HANDLE_VALUE, h_array(sel_array(current)),image
                                         
                        ;TVSCL,image>min_img<max_img,h.r1col-21,h.r1row-1
                        IF wttrack EQ 1 THEN dis.ff_dswitch = 0
                   
                        ;tvscl_image,sel_array(current),vtop,dswitch=dis.ff_dswitch 
                        tvscl_image,sel_array(current),vtop,dswitch=dswitch,win1id=uvd.Draw1                         
                       ENDIF
		  ENDIF
                
                  END

          ELSE: donothing = 0
          ENDCASE

         ENDIF
        
			;WIDGET_CONTROL, event.top, SET_UVALUE=uvd ; ** set uvd.press to new value
			HANDLE_VALUE, info_hand, uvd, /SET

        END	; end of 'DRAW1'

			'WIDGET_TIMER': BEGIN
			                ;***** this is a movie section *****
                          IF uvd.movie THEN BEGIN
			                   ;n_frames = N_ELEMENTS(h_array)
                            n_frames = N_ELEMENTS(sel_array)
                            ; ***think about it !
			                   last = n_frames - 1
                            IF N_ELEMENTS(n_frames) GT 0 THEN BEGIN
                    	      ;** out of range error checks
    			      IF (current LT 0) THEN current = last
     			      IF (current GT last) THEN current = first 

                              frame = sel_array(current)
                  
                              IF wttrack EQ 1 THEN dis.ff_dswitch = 0
                              tvscl_image,frame,vtop,dswitch=dis.ff_dswitch,win1id=uvd.Draw1
                  
                              ;tvscl_image,frame,vtop
                               xi=var_arr(0) + ( h.r1col-21 )
                               yi=var_arr(1) + ( h.r1row- 1 )
                              ;x=var_arr(0)
                              ;y=var_arr(1)			
			      zoom_it,xi,yi,uvd.win2s,uvd.win2s,uvd.izoom,dis.win1,dis.win2,0
                            ENDIF
                            current = current + 1
                            IF current GT (N_ELEMENTS(sel_array) - 1) THEN current = 0

                   	  WIDGET_CONTROL, event.id, TIMER=0.1
     ;ZARKS!
                  	  ;WIDGET_CONTROL,uvd.DispBase,TIMER=0.1
                	  ENDIF
                         END		
		 ELSE: donothing=0
		 ENDCASE

	 	 END


   "TRACKON":  BEGIN
                current = 0
                WIDGET_CONTROL,uvd.p_track1,/SET_BUTTON

                IF (HANDLE_INFO(trackhandle)) EQ 0 THEN trackhandle = HANDLE_CREATE()
                                                                          
                          	;Otherwise it was off, so turn it on.
                          	wttrack = 1 ; turn it on
                          	trackarr = fltarr(n_elements(sel_array), 22)
                          	
                          	FOR i=0, n_elements(sel_array)-1 DO BEGIN
                          		trackarr(i, 0)=sel_array(i)
                          		trackarr(i, 17)=0
                          		trackarr(i, 19)=0
                                ENDFOR                                                                                   
                          
                         	HANDLE_VALUE, trackhandle, trackarr, /SET 
                           
                          IF wttrack NE 0 AND wttrack NE 1 THEN BEGIN
                          msg = WIDGET_MESSAGE("Check WTTRACK VARIABLE!")
                          ENDIF
                             
               END
               
               
   "TRACKOFF": BEGIN
                 ;delete last point
                 ;clear structure
                 trackarr=0
                 
                 HANDLE_VALUE, trackhandle,trackarr,/SET
                 undefine,trackarr     
                 HANDLE_FREE, trackhandle
                    
                 wttrack = 0 ; turn it off
                 ;msg = WIDGET_MESSAGE("Time Tracking Off!",$
                 ;         	 /INFORMATION, TITLE= 'Tracking Off!')               
                 WIDGET_CONTROL,uvd.p_track0,/SET_BUTTON
               
               END

     "TVA":      BEGIN
		   uvd.movie = 1
		   ;WIDGET_CONTROL,uvd.DispBase, SET_UVALUE = uvd
		   HANDLE_VALUE, info_hand, uvd, /SET
		   WSET,dis.win1
		   WIDGET_CONTROL,uvd.Draw1,TIMER=0
		 END

     "TVS":      BEGIN
 		  uvd.movie = 0
		  ;WIDGET_CONTROL, uvd.DispBase, SET_UVALUE = uvd
		  HANDLE_VALUE, info_hand, uvd, /SET
		 END

     "TVN":      BEGIN 
	          IF N_ELEMENTS(image) EQ 0 THEN RETURN
	          
	          uvd.movie = 0

		  HANDLE_VALUE, info_hand, uvd, /SET

                  last = N_ELEMENTS(sel_array) - 1
                  current = current + 1
                  IF (current GT last) THEN current = first ;** out of range error check
                  frame = sel_array(current)
                  index = frame
                  IF wttrack EQ 1 THEN dswitch = 0 ELSE dswitch = dis.ff_dswitch 
	          tvscl_image,frame,vtop,dswitch=dswitch,clear=1,win1id=uvd.draw1     

                  xi=var_arr(0) + ( h.r1col-21 )
                  yi=var_arr(1) + ( h.r1row- 1 )
                    			
		  zoom_it,xi,yi,uvd.win2s,uvd.win2s,uvd.izoom,dis.win1,dis.win2,0
				
                 END

     "TVP":      BEGIN
 	          IF N_ELEMENTS(image) EQ 0 THEN RETURN
 	          
		  uvd.movie = 0

		  HANDLE_VALUE, info_hand, uvd, /SET

                  last = N_ELEMENTS(sel_array) - 1
                  current = current - 1
                  IF (current LT first) THEN current = last ;** out of range error check
                  frame = sel_array(current)
                  index = frame
                  IF wttrack EQ 1 THEN dswitch = 0 ELSE dswitch = dis.ff_dswitch                                             
	          tvscl_image,frame,vtop,dswitch=dswitch,clear=1,win1id=uvd.draw1     
	                            
                  xi=var_arr(0) + ( h.r1col-21 )
                  yi=var_arr(1) + ( h.r1row- 1 )			
		
		  zoom_it,xi,yi,uvd.win2s,uvd.win2s,uvd.izoom,dis.win1,dis.win2,0
		
               END

   "LAI":  BEGIN
            wcurrent 
           END   


    "TVSCL":  BEGIN
	        IF N_ELEMENTS(image) EQ 0 THEN RETURN

		WIDGET_CONTROL,dis.id_min,GET_VALUE=sminv
		WIDGET_CONTROL,dis.id_max,GET_VALUE=smaxv
		min_img = float(sminv(0))
		max_img = float(smaxv(0))

                 WSET,dis.win1 
                 
                 ;frame = sel_array(current)                  
	         tvscl_image,index,vtop,dswitch=dis.ff_dswitch,win1id=uvd.draw1
	                           
                 WSET,dis.win3
                 ERASE 
                 TVSCL,a3>min_img<max_img,FIX((h.r1col-21)/8),FIX((h.r1row-1)/8) 
                 WSET,dis.win1

      ;  		TVLCT, r_orig, g_orig, b_orig
                ;erase 
 	        ;tvscl,image>min_img<max_img 
         	   max_min = ( max_img - min_img ) / 100.0
         	   
	      END

  "GC":  BEGIN
                   WIDGET_CONTROL,uvd.Gamma_label, GET_VALUE=sgamma
                   gamma = FLOAT(sgamma(0))

                   gamma = gamma > .1 < 10.0 

                  WIDGET_CONTROL,uvd.Gamma_label, $
                         SET_VALUE=STRING(gamma,format='(f6.3)')

                   gamma_ct,gamma

	        tvscl_image,index,vtop,dswitch=dis.ff_dswitch,win1id=uvd.draw1
         END

 "IMIN": BEGIN
	        IF N_ELEMENTS(image) EQ 0 THEN RETURN

		min_img = min(image)
                sminv = STRCOMPRESS(STRING(FLOAT(min_img)),/REMOVE_ALL)
		WIDGET_CONTROL,dis.id_min,SET_VALUE=sminv
                ;TVSCL,image>min_img<max_img,h.r1col-21,h.r1row-1
                ;frame = sel_array(current)
	        tvscl_image,index,vtop,dswitch=dis.ff_dswitch,win1id=uvd.draw1
         END

 "IMAX": BEGIN
	        IF N_ELEMENTS(image) EQ 0 THEN RETURN

		max_img = max(image)
                smaxv = STRCOMPRESS(STRING(FLOAT(max_img)),/REMOVE_ALL)
		WIDGET_CONTROL,dis.id_max,SET_VALUE=smaxv
                ;TVSCL,image>min_img<max_img,h.r1col-21,h.r1row-1
                ;frame = sel_array(current)
	        tvscl_image,index,vtop,dswitch=dis.ff_dswitch,win1id=uvd.draw1                

         END
		
   "BBW":    BEGIN 
               loadct,0 & gamma_ct,gamma
             END
   "BRED":   BEGIN
               loadct,3 & gamma_ct,gamma
             END
   "BBLUE":  BEGIN
               loadct,1 & gamma_ct,gamma
             END
   "BGREEN": BEGIN
               loadct,8 & gamma_ct,gamma
             END

    "BGRID": BEGIN
              scan4limb,image,name,head,h_head,index,1,mini=min_img,maxi=max_img
         ; WIDGET_CONTROL,dis.id_th,SET_VALUE=head
               center_x = FIX(SXPAR(head,"CENTER_X"))
               center_y = FIX(SXPAR(head,"CENTER_Y"))
               radius = FIX(SXPAR(head,"RADIUS"))
               image = circle(image,center_x,center_y,radius,radius)
               TVSCL,image>min_img<max_img,h.r1col-21,h.r1row-1

               ;tvscl,image
             END


 	ELSE:	donothing=0 
	
 ENDCASE
ENDELSE

done:
END

PRO display,GROUP=GROUP

@lasco.com
@wload.com
@chandle.com
@wplot.com
@wtv.com
@wflimb.com

COMMON display, first, last, current, count, count_all, cross_x, cross_y
common colors, r_orig, g_orig, b_orig, r_curr, g_curr, b_curr
COMMON wzoom,zw_id,wttrack,trackhandle

COMMON info,text,text1,text2,tmps,tmpp,new_tmpp,sipath,s_flag,inst,path,filter,wave_set,hnh

COMMON xloadct_com, r0, g0, b0, tfun, state, filename, cps, psave, pnt, $
	top, bot, silent, chop, lock, g_lbl, vbot, vtop, g_slider, $
	gamma, color, use_values, ncolors, cbot, siz, w_height, show_win

IF (XRegistered("display") NE 0) THEN RETURN


defsysv,"!delimiter","/"

siz = 256                       ;Length from intensity slider

;loadct,3			;Load red color table as default
regis = 2			;don't start with background task  
lock = 0
chop = 0

use_values=0

;INSTALLED TRACKING
wttrack=0	; Initialize tracking to 0 .. ie -> off
trackhandle = HANDLE_CREATE()

if n_elements(image) eq 0 then begin

   f = WLOAD()

   if n_elements(h_array) eq 0 then begin
    tmp = ["                                    ", $
           " You don't feel like working ...", $
           " Anyway, you can try again later!", $
           "                                           " ]
    wmessage,title='Warning',text=tmp,xsize=50,ysize=4
    return
   endif

endif

if n_elements(ff_dswitch) eq 0 then ff_dswitch = 0 

if n_elements(g_order) eq 0 then g_order = 0
if n_elements(vbot) eq 0 then vbot = 0
if n_elements(vtop) eq 0 then vtop = 100
if n_elements(gamma) eq 0 then gamma = 1.0

if n_elements(flag_limb) eq 0 then flag_limb = 0
if n_elements(limb_x) eq 0 then limb_x = intarr(3)
if n_elements(limb_y) eq 0 then limb_y = intarr(3)

if n_elements(text) eq 0 then text = ' '
if n_elements(name) eq 0 then name = ' '
if n_elements(var_arr) eq 0 then var_arr = intarr(2)
if n_elements(press_flag) eq 0 then press_flag = 0

if n_elements(h_array) eq 0 then h_array = lonarr(1) 
n_frames = N_ELEMENTS(h_array)
current = 0
first = 0
last = n_frames - 1

;Image array min & max

sminv = STRCOMPRESS(STRING(min_img))
smaxv = STRCOMPRESS(STRING(max_img))
cr = STRING(byte(169)) + " bp"
DispBase = WIDGET_BASE(TITLE = "LASCO Image Display Version 3.0 "+cr,/FRAME,/COLUMN)
			
                                                        ;mpae bitmap
                                                        ;definition
mpae =	        [                               $
                [000B, 000B, 000B, 000B],                       $
                [000B, 128B, 000B, 000B],                       $
                [000B, 128B, 000B, 000B],                       $
                [000B, 128B, 000B, 000B],                       $
                [000B, 128B, 000B, 000B],                       $
                [112B, 096B, 255B, 015B],                       $
                [240B, 112B, 255B, 015B],                       $
                [176B, 121B, 003B, 012B],                       $
                [048B, 111B, 003B, 006B],                       $
                [096B, 102B, 003B, 003B],                       $
                [192B, 096B, 131B, 001B],                       $
                [128B, 097B, 255B, 000B],                       $
                [000B, 099B, 127B, 000B],                       $
                [000B, 102B, 003B, 000B],                       $
                [000B, 108B, 003B, 000B],                       $
                [000B, 000B, 000B, 000B],                       $
                [000B, 000B, 000B, 000B],                       $
                [000B, 112B, 031B, 000B],                       $
                [000B, 11B, 031B, 000B],                       $
                [000B, 108B, 003B, 000B],                       $
                [000B, 102B, 003B, 000B],                       $
                [000B, 099B, 255B, 000B],                       $
                [128B, 127B, 255B, 000B],                       $
                [192B, 127B, 003B, 000B],                       $
                [096B, 096B, 003B, 000B],                       $
                [048B, 096B, 255B, 015B],                       $
                [016B, 096B, 255B, 015B],                       $
                [000B, 128B, 000B, 000B],                       $
                [000B, 128B, 000B, 000B],                       $
                [000B, 128B, 000B, 000B],                       $
                [000B, 128B, 000B, 000B],                       $
                [000B, 000B, 000B, 000B]                        $
                ]
							;/home/lasco1/borut/idl/display3/help1.bm bitmap
							;definition
help = 	[				$
		[000B, 000B, 000B, 000B],			$
		[000B, 000B, 000B, 000B],			$
		[000B, 000B, 000B, 000B],			$
		[000B, 224B, 001B, 000B],			$
		[000B, 248B, 003B, 000B],			$
		[000B, 060B, 014B, 000B],			$
		[000B, 006B, 024B, 000B],			$
		[000B, 003B, 056B, 000B],			$
		[128B, 003B, 048B, 000B],			$
		[128B, 001B, 096B, 000B],			$
		[128B, 001B, 096B, 000B],			$
		[000B, 000B, 064B, 000B],			$
		[000B, 000B, 096B, 000B],			$
		[000B, 000B, 096B, 000B],			$
		[000B, 000B, 048B, 000B],			$
		[000B, 000B, 024B, 000B],			$
		[000B, 000B, 014B, 000B],			$
		[000B, 000B, 003B, 000B],			$
		[000B, 192B, 001B, 000B],			$
		[000B, 224B, 000B, 000B],			$
		[000B, 096B, 000B, 000B],			$
		[000B, 096B, 000B, 000B],			$
		[000B, 096B, 000B, 000B],			$
		[000B, 096B, 000B, 000B],			$
		[000B, 096B, 000B, 000B],			$
		[000B, 096B, 000B, 000B],			$
		[000B, 000B, 000B, 000B],			$
		[000B, 000B, 000B, 000B],			$
		[000B, 096B, 000B, 000B],			$
		[000B, 096B, 000B, 000B],			$
		[000B, 000B, 000B, 000B],			$
		[000B, 000B, 000B, 000B]			$
		]


WMPAe = WIDGET_BASE(DispBase,/ROW,/FRAME)
w_m_b = WIDGET_BUTTON(WMPAe, VALUE = mpae,UVALUE = 'MPAE')

junk = { CW_PDMENU_S, flags:0, name:'' }

pdm_desc = [ { CW_PDMENU_S, 1, 'Image' },		$
                { CW_PDMENU_S, 0, 'Load' },  $
                { CW_PDMENU_S, 0, 'Save' },  $
                { CW_PDMENU_S, 0, 'Save as' },  $
                { CW_PDMENU_S, 0, 'Layout' }, $
                { CW_PDMENU_S, 2, 'Print' },  $
             { CW_PDMENU_S, 0, 'Status' },  	$
             { CW_PDMENU_S, 1, 'Image Processing' }, 	$
                { CW_PDMENU_S, 1, 'Rotate , Transpose' }, 		$
                  { CW_PDMENU_S, 0, '-Y0  X0  (90) , No' }, 	$
                  { CW_PDMENU_S, 0, '-X0 -Y0 (180) , No'  }, 	$
                  { CW_PDMENU_S, 0, ' Y0 -X0 (270) , No' }, 	$
                  { CW_PDMENU_S, 0, ' Y0  X0 None  , Yes' }, 	$
                  { CW_PDMENU_S, 0, '-X0  Y0  (90) , Yes' }, 	$
                  { CW_PDMENU_S, 0, '-Y0 -X0 (180) , Yes' }, 	$
                  { CW_PDMENU_S, 2, ' X0 -Y0 (270) , Yes' }, 	$
                { CW_PDMENU_S, 0, 'Transpose' }, 	$
                { CW_PDMENU_S, 0, 'Flip Image' }, 		$ 
                { CW_PDMENU_S, 0, 'Find Limb' }, 		$ 
                { CW_PDMENU_S, 0, 'Define Box' }, 		$ 
                { CW_PDMENU_S, 0, 'Rescale Images' }, 		$ 
                  ;{ CW_PDMENU_S, 0, 'Rotate Sun' },    $
                  ;{ CW_PDMENU_S, 0, 'Cartesian to polar' },    $
                  ;{ CW_PDMENU_S, 0, 'Polar to cartesian' },    $
                  ;{ CW_PDMENU_S, 0, 'Radial filter' },    $
                { CW_PDMENU_S, 0, 'Calculator' },  	$
               { cw_pdmenu_s, 0, 'Exposures'},  $
               { cw_pdmenu_s, 1, 'Make standard image'}, $
                { cw_pdmenu_s, 0, 'From Loaded Images'},  $
                { cw_pdmenu_s, 2, 'Search headers'}, $
                { cw_pdmenu_s, 0, 'Subtract standard'}, $ 
                { cw_pdmenu_s, 0, 'Running differences'}, $
                { CW_PDMENU_S, 2, 'Remove cosmic rays' }, 	$
                ;{ CW_PDMENU_S, 2, 'Dynamic Imaging' }, $ 
             { CW_PDMENU_S, 1, 'Plots' }, 		$
                { CW_PDMENU_S, 0, 'Plot' }, 		$
                { CW_PDMENU_S, 0, 'Lines' }, 		$ 
;               { CW_PDMENU_S, 0, 'Histogram' }, 	$
                { CW_PDMENU_S, 0, 'Contours' }, 		$
                { CW_PDMENU_S, 0, 'Radial Beam' }, 	$
                { CW_PDMENU_S, 0, 'Height-Time' }, 	$
                { CW_PDMENU_S, 2, 'Distance' }, 		$
             ;{ CW_PDMENU_S, 1, 'Graphics' }, 		$
             ;  { CW_PDMENU_S, 0, 'Adjust' }, 		$
             ;  { CW_PDMENU_S, 0, 'Color' }, 		$
             ;  { CW_PDMENU_S, 2, 'Palette' }, 		$
             { CW_PDMENU_S, 0, 'Fast Movie' },     $		
             { CW_PDMENU_S, 1, 'Options' }, $
                            { CW_PDMENU_S, 0, 'Edit Header' }, $
                            { CW_PDMENU_S, 2, 'View Catalog' } ]
        

menu = CW_PDMENU(WMPAe, pdm_desc, UVALUE = "PDMENU", /RETURN_FULL_NAME)

point = WIDGET_BUTTON(WMPAe, VALUE = "Refresh",UVALUE = "TVSCL")
;w_h_b = WIDGET_BUTTON(WMPAe, VALUE = "Catalog",UVALUE = 'HDR_TXT')
wdone = WIDGET_BUTTON(WMPAe, VALUE = "Exit",UVALUE = "DONE")
w_h_b = WIDGET_BUTTON(WMPAe, VALUE = help,UVALUE = 'HELP0')
cntrl = WIDGET_BUTTON(WMPAe, VALUE = "Control Panel", UVALUE = 'CONTROL')

win1_xs = 1024
win1_ys = 1024
w1xss =  768
w1yss =  512

shinfo = STRCOMPRESS(hinfos(h))

twl= WIDGET_BASE(DispBase,/ROW)
id_wl1 = WIDGET_LABEL(twl,VALUE = shinfo)

drawbase = WIDGET_BASE(DispBase,/ROW)
drawb = WIDGET_BASE(drawbase,/COLUMN)
draw1 = WIDGET_DRAW(drawb,UVALUE="DRAW1",Xsize=win1_xs,Ysize=win1_ys, $
		/scroll, x_scroll_size=w1xss, y_scroll_size=w1yss,/MOTION,/BUTTON)

;
; Create text widget to display current cursor position
;
wc = WIDGET_BASE(drawb, /ROW)
cur_label1 = WIDGET_LABEL(wc,VALUE="Cursor Position (x,y), Pixel Value:")
 
PosHistory = " " 

cur_label2 = WIDGET_LABEL(wc, VALUE=PosHistory, /DYNAMIC_RESIZE)

Control_Base = WIDGET_BASE(/FRAME,/ROW, TITLE='Lasco Image Display Control Panel', GROUP_LEADER = DispBase)

resbase = WIDGET_BASE(Control_Base,/COL)

w8 = WIDGET_BASE(resbase, /ROW)
chide = WIDGET_BUTTON(w8, VALUE = "Dismiss", UVALUE = "CDISMISS", /ALIGN_CENTER)

;
; Create text widget to display image file name.
;
name = STRCOMPRESS(name)
t_title = WIDGET_BASE(w8, /ROW)
title_l0 = WIDGET_LABEL(t_title,VALUE = "File : ")
title_l1 = WIDGET_LABEL(t_title,VALUE = name, /DYNAMIC_RESIZE)
;
; Create text widget to display image header
;
;text1 = strarr(1)
;resheader  = WIDGET_BASE(resbase, /FRAME, /COL)
;lcol2   = WIDGET_BASE(resheader,/COLUMN)
;w_text  = WIDGET_TEXT(lcol2,XSIZE=50,YSIZE=3,VALUE=text1, UVALUE='HEAD', $
;                      /SCROLL,/EDITABLE)
;w_text  = WIDGET_TEXT(lcol2, XSIZE=50, YSIZE=1, VALUE=text1, UVALUE='HEAD')


;
; Create widgets for manipulating gamma corection and set min(image) and max(image)
;

resbase2  = WIDGET_BASE(resbase, /ROW) 
w6  = WIDGET_BASE(resbase2,/FRAME, /ROW) ; make a frame

w61 = WIDGET_BASE(w6, /FRAME,/COL)

w64  = WIDGET_BASE(w61, /ROW)
w_label = WIDGET_LABEL(w64,VALUE="Color table: ")
bred   = WIDGET_BUTTON(w64, VALUE = ' R ', UVALUE = 'BRED',RESOURCE_NAME='red')
bgreen = WIDGET_BUTTON(w64, VALUE = ' G ', UVALUE = 'BGREEN',RESOURCE_NAME='green')
bblue  = WIDGET_BUTTON(w64, VALUE = ' B ', UVALUE = 'BBLUE',RESOURCE_NAME='blue')
bblack = WIDGET_BUTTON(w64, VALUE = ' B/W ', UVALUE = 'BBW',RESOURCE_NAME='black')
;bgrid = WIDGET_BUTTON(w64, VALUE = ' Grid ', UVALUE = 'BGRID')

junk2 = { CW_PDMENU_Z, flags:0, name:'' }
pdm_desc2 = [ { CW_PDMENU_Z, 1, 'Graphics' },		$
               { CW_PDMENU_Z, 0, 'Adjust' }, 		$
               { CW_PDMENU_Z, 0, 'Color' }, 		$
               { CW_PDMENU_Z, 2, 'Palette' } ]

menu2 = CW_PDMENU(w64, pdm_desc2, UVALUE = "PDMENU2", /RETURN_FULL_NAME)

w62  = WIDGET_BASE(w61, /ROW)
g_lbl = WIDGET_LABEL(w62, VALUE = "Gamma Correction:  ")
;g_lbl = WIDGET_LABEL(w62, VALUE = STRING(1.0))
g_lbl  = WIDGET_TEXT(w62,XSIZE=8,/EDITABLE, $
                     VALUE=STRING(gamma,FORMAT='(F6.3)'),UVALUE="GC")

;b_gam = WIDGET_BUTTON(w62, VALUE = ' max ', UVALUE = 'IMAX')

w63  = WIDGET_BASE(w61, /ROW)
w_label = WIDGET_LABEL(w63,VALUE="min:")
w_tmin  = WIDGET_TEXT(w63,XSIZE=9,/EDITABLE,VALUE=sminv,UVALUE="TVSCL")
w_label = WIDGET_LABEL(w63,VALUE=" max:")
w_tmax  = WIDGET_TEXT(w63,XSIZE=9,/EDITABLE,VALUE=smaxv,UVALUE="TVSCL")
w_label = WIDGET_LABEL(w63,VALUE="  ")
b_min = WIDGET_BUTTON(w63, VALUE = ' min ', UVALUE = 'IMIN')
b_max = WIDGET_BUTTON(w63, VALUE = ' max ', UVALUE = 'IMAX')

w61 = WIDGET_BASE(w61,/FRAME,/ROW)

sel_base = WIDGET_BASE(w61,/ROW,/EXCLUSIVE)
p_track1 = WIDGET_BUTTON(sel_base,VALUE='  Track On  ',UVALUE='TRACKON',/NO_RELEASE)
p_track0 = WIDGET_BUTTON(sel_base,VALUE='  Track Off ',UVALUE='TRACKOFF',/NO_RELEASE)

;
; Create WIDGET_DRAW for zoom and to display field view
;
win2s = 128
win3s = 128
win4s = 128
;wbuffer = 10  ; space between two windows

drawp = WIDGET_BASE(resbase2,/COL,/FRAME)
draww  = WIDGET_BASE(drawp,/ROW)

drawb1 = WIDGET_BASE(draww,/ROW)
drawb2 = WIDGET_BASE(drawb1,/COLUMN)
w_label = WIDGET_LABEL(drawb2,VALUE="Zoom Window")
draw2 = WIDGET_DRAW(drawb2,UVALUE="DRAW2",Xsize=win2s,Ysize=win2s, /MOTION,/BUTTON ) 

drawb2 = WIDGET_BASE(drawb1,/COLUMN)
w_label = WIDGET_LABEL(drawb2,VALUE="Full Frame")
draw3 = WIDGET_DRAW(drawb2,UVALUE="DRAW3",Xsize=win3s,Ysize=win3s, /MOTION,/BUTTON ) 

drawb2 = WIDGET_BASE(drawb1,/COLUMN)
w_label = WIDGET_LABEL(drawb2,VALUE="Plot Window")
draw4 = WIDGET_DRAW(drawb2,UVALUE="DRAW4",Xsize=win4s,Ysize=win4s, /MOTION,/BUTTON ) 

;
; Create widgets buttons in last frame ( < , Show All Images, > , Stop )
;

w7= WIDGET_BASE(drawp, /ROW)
;w7 = WIDGET_BASE(resbase, /COL,/FRAME)
t_pre = WIDGET_BUTTON(w7, VALUE = "  <<  ",UVALUE = "TVP")
t_all = WIDGET_BUTTON(w7, VALUE = " Play ",UVALUE = "TVA")
t_nex = WIDGET_BUTTON(w7, VALUE = "  >>  ",UVALUE = "TVN")
t_stp = WIDGET_BUTTON(w7, VALUE = " Stop ",UVALUE = "TVS")
t_act = WIDGET_BUTTON(w7, VALUE = " List of active images ",UVALUE = "LAI")

;w8 = WIDGET_BASE(resbase, /ROW,/FRAME)
;chide = WIDGET_BUTTON(w8, VALUE = "Dismiss", UVALUE = "CDISMISS", /ALIGN_CENTER)


;*------------------------ Done Creating Widgets -------------------------*

WIDGET_CONTROL, DispBase,/REALIZE
WIDGET_CONTROL, Control_Base,MAP=1,GROUP_LEADER=DispBase
WIDGET_CONTROL, Control_Base, /REALIZE
WIDGET_CONTROL, p_track0,/SET_BUTTON

;WIDGET_CONTROL, w_text, SET_VALUE = head, MAP=0
WIDGET_CONTROL, draw1, GET_VALUE = win1,SET_DRAW_VIEW=[256,256]
WIDGET_CONTROL, draw2, GET_VALUE = win2
WIDGET_CONTROL, draw3, GET_VALUE = win3
WIDGET_CONTROL, draw4, GET_VALUE = win4

WSET,win1

IF N_ELEMENTS(min_img) EQ 0 THEN min_img = min(image)
IF N_ELEMENTS(max_img) EQ 0 THEN max_img = max(image)

IF N_ELEMENTS(h) EQ 0 THEN h = { r1col:0, r1row:0 }

;IF N_ELEMENTS(r1col) EQ 0 THEN r1col = 0
;IF N_ELEMENTS(r1row) EQ 0 THEN r1row = 0

if n_elements(image) gt 0 then tvscl,image>min_img<max_img,h.r1col-21,h.r1row-1
sz = size(image)

WSET,win3 
TVSCL,a3>min_img<max_img,FIX((h.r1col-21)/8),FIX((h.r1row-1)/8) 
WSET,win1

scale = 'L'
slice = 'ROW'
inter = 'NI'
out_win = !d.name

if n_elements(bottom) gt 0 then cbot = bottom else cbot = 0
ncolors = !d.table_size - cbot
if n_elements(nc) gt 0 then ncolors = ncolors < nc
if ncolors le 0 then message,'Number of colors is 0 or negative'

press = 0
;
; Create a structure that will be carried around as the UVALUE of the main base (DispBase) 
;
ms = 512
gcol = 1
izoom = 4
a0 = bytarr(win2s,win2s)

dis = { win1:win1,      $
        win2:win2,      $
        win3:win3,      $
        win4:win4,      $
        id_ln:title_l1, $
;        id_th:w_text,   $
        id_min:w_tmin,  $
        id_max:w_tmax,   $
        id_wl1:id_wl1,  $
        ff_dswitch:ff_dswitch  $
      }

box = { x:0, y:0, xbl:0, ybh:0 }                                      

wd_uv = { DispBase:DispBase,  $  ; Base widget ID
          Draw1:draw1,        $  ; Widget ID for the WIDGET_DRAW	
          Draw2:draw2,        $  ; Widget ID for the WIDGET_DRAW zoom
          win1_xs:win1_xs,    $
          win1_ys:win1_ys,    $
          win2s:win2s,        $
          win3s:win3s,        $
          win4s:win4s,        $
          w1xss:w1xss,        $
          w1yss:w1yss,        $
	  cbase:Control_Base, $
          izoom:izoom,        $
          a0:a0,              $
          Gamma_label:g_lbl,  $
          Cur:cur_label2,     $  ; ID for the WIDGET_LABEL cursor position
	  press:press,	      $
	  ms:ms,	      $
	  gcol:gcol,	      $
	  movie:0,            $
          ltbt:0,	      $
          p_track0:p_track0,  $
          p_track1:p_track1 }	


info_hand = HANDLE_CREATE()
HANDLE_VALUE, info_hand, wd_uv, /SET

WIDGET_CONTROL, DispBase, SET_UVALUE = info_hand
WIDGET_CONTROL, Control_Base, SET_UVALUE =  info_hand

;WIDGET_CONTROL,point,SENSITIVE=0

XMANAGER, "display", Control_Base, EVENT_HANDLER="display_event", /JUST_REG
XMANAGER,"display", DispBase, EVENT_HANDLER="display_event",GROUP_LEADER=GROUP

HANDLE_FREE, info_hand

END


