;+
; Project     : SOHO - CDS     
;                   
; Name        : CWQ_CUBE
;               
; Purpose     : Show one data cube from a QLDS any way you like it
;               
; Explanation : This quicklook compound widget makes up one column in the
;               DSP_CUBE program.
;               
; Use         : ID = CWQ_CUBE(BASE,QLDS,WINDOWI ..)
;    
; Inputs      : BASE : The base to put it on
;
;               QLDS : The qlds.
;
;               WINDOWI : The (initial) window index.
; 
; Opt. Inputs : None.
;               
; Outputs     : Returns the widget ID of the compound widget.
;               
; Opt. Outputs: None.
;               
; Keywords    : UVALUE : The usual
;
;               XSIZE, YSIZE : The size of the draw windows.. sorry, only one
;                              size.
;
;               ROW : Set to make the image/plot appear side by side.
;
;               FRAME : The usual..
;
; Calls       : cw_cubeview(), cwq_winsel(), default, gt_windata(),
;               handle_killer_hookup, since_version(), tag_exist()
;
; Common      : QLSAVE : See qlds_checkin and qlds_report.
;               
; Restrictions: ...
;               
; Side effects: ...
;               
; Category    : QuickLook Compound Widget.
;               
; Prev. Hist. : None.
;
; Written     : Stein Vidar H. Haugan, UiO, 29 May 1997
;               
; Modified    : Not yet.
;
; Version     : 1, 29 May 1997
;-            


PRO cwq_cube_setv,id,value
  stash = widget_info(id,/child)
  widget_control,stash,get_uvalue=status,/no_copy
  
  ;; The only valid thing to set is the focus...
  
  widget_control,status.cw_cube_id,get_value=val
  focus = value
  focus(0) = val.focus(0)
  IF total(focus NE val.focus) NE 0 THEN $
     widget_control,status.cw_cube_id,set_value={focus:focus}
  
  
  widget_control,stash,set_uvalue=status,/no_copy
END


FUNCTION cwq_cube_event,ev
  common QLSAVE,QLDS,saved
  on_error,0
  
  stash = widget_info(ev.handler,/child)
  widget_control,stash,get_uvalue=status,/no_copy
  
  widget_control,ev.id,get_uvalue=uval

  qlds_checkin,status.ql_no
  
  nuevent = 0
  
  CASE uval OF 
  'CWQ_WINSEL':BEGIN
     status.windowi = ev.windowi
     IF status.oldsys THEN BEGIN
        handle = status.handle
        da = gt_windata(qlds,status.windowi)
        handle_value,handle,da,/set,/no_copy
     END ELSE handle = qlds.detdesc(status.windowi).handle
     widget_control,status.cw_cube_id,set_value=handle
     ENDCASE
     
  ELSE:BEGIN
     nfocus = n_elements(ev.focus)
     nuevent = create_struct(name='CWQ_CUBE_EVENT'+trim(nfocus),$
                             "ID",EV.HANDLER,$
                             "TOP",EV.TOP,$
                             "HANDLER",0L,$
                             "FOCUS",EV.FOCUS)
     ENDCASE
  END
  
  widget_control,stash,set_uvalue=status,/no_copy
  return,nuevent
END


FUNCTION cwq_cube,base,qlds,windowi,uvalue=uvalue,xsize=xsize,ysize=ysize,$
                  row=row,frame=frame
  
  IF since_version('4.0') then sml=1 else sml=0
  sml2 = 1
  
  small = {xpad:sml,ypad:sml,space:sml}
  default,frame,0
  
  mybase = widget_base(base,/column,_extra=small,frame=frame,$
                       event_func='cwq_cube_event', $
                       pro_set_value='cwq_cube_setv')
  
  IF n_elements(uvalue) NE 0 THEN widget_control,mybase,set_uvalue=uvalue
  
  stash = widget_base(mybase,_extra=small)
  
  selector_id = cwq_winsel(mybase,qlds,title='Win: ',uvalue='CWQ_WINSEL',$
                           initial=windowi)
  
  ndim = n_elements(qlds.detdesc(windowi).axes)
  
  ;; Only scale X vs Y
  phys_scale = ([0,1,1,0])(0:ndim-1)
  
  origin = qlds.detdesc(windowi).origin
  scale = qlds.detdesc(windowi).spacing
  dimnames = qlds.detdesc(windowi).axes
  
  IF ndim EQ 3 THEN image_dim=[1,2] $
  ELSE IF ndim EQ 4 THEN image_dim=[3,2] $ ;; Time vs Y
  ELSE image_dim=[0,1]
  
  oldsys = tag_exist(qlds,'DETDATA')
  IF oldsys THEN BEGIN
     value = gt_windata(qlds,windowi)
     handle = handle_create(value=value,/no_copy)
     handle_killer_hookup,handle,group_leader=base
  END ELSE BEGIN
     handle = qlds.detdesc(windowi).handle
  END
  
  missing = qlds.detdesc(windowi).missing
  
  cw_cube_id = cw_cubeview(mybase,hvalue=handle,dimnames=dimnames,$
                           origin=origin,scale=scale,phys_scale=phys_scale,$
                           image_dim=image_dim,plot_dim=0,row=row,$
                           missing=missing,uvalue='CW_CUBE',$
                           xsize=xsize,ysize=ysize)
  
  status = { ql_no : qlds.ql_no,$
             handle : handle,$
             oldsys : oldsys,$
             cw_cube_id : cw_cube_id,$
             windowi : windowi,$
             focus : lonarr(ndim)}
  
  widget_control,stash,set_uvalue=status
  
  return,mybase
  
END


