; $Id: db_dispdata.pro,v 1.12 2005/02/08 17:01:16 azuah Exp $
;###############################################################################
;
; NAME:
;   DB_DispData
;
; PURPOSE:
;   A small utility widget program to display the data contained in a
;   davePtr.
;
; CATEGORY:
;   Data Browser
;
; AUTHOR:
;   Richard Tumanjong Azuah
;   NIST Center for Neutron Research
;   100 Bureau Drive, Gaithersburg, MD 20899
;   United States
;   azuah@nist.gov; (301) 9755604
;   Aug, 2004
;
; LICENSE:
;  The software in this file is written by an employee of 
;  National Institute of Standards and Technology 
;  as part of the DAVE software project.
;
;  The DAVE software package is not subject to copyright protection
;  and is in the public domain. It should be considered as an
;  experimental neutron scattering data reduction, visualization, and
;  analysis system. As such, the authors assume no responsibility
;  whatsoever for its use, and make no guarantees, expressed or
;  implied, about its quality, reliability, or any other
;  characteristic. The use of certain trade names or commercial
;  products does not imply any endorsement of a particular product,
;  nor does it imply that the named product is necessarily the best
;  product for the stated purpose. We would appreciate acknowledgment
;  if the DAVE software is used or if the code in this file is
;  included in another product.
;
;###############################################################################



;===============================================================================
; DB_DispData_Event
;
; PURPOSE:
;   Event handler for events originating from the main UI
;
; PARAMETERS:
;   event [in] - the event structure to be handled
;
pro DB_DispData_event,event

uname = widget_info(event.id,/uname)

case uname of
    'DISMISS': widget_control,event.top,/destroy
    else: begin
        ;if (vartype(event) eq 'WIDGET_BASE') then begin
            ;; this is a resize event

            ;; Reset dimensions for text display
            ;widget_control, event.top, get_uvalue=sPtr
            ;widget_control, (*sPtr).textID $
            ;                ,scr_xsize = (event.x - (*sPtr).butXsize) $
            ;                ,scr_ysize = (event.y - (*sPtr).butYsize)
        ;endif
    end
endcase

end
;-------------------------------------------------------------------------------



;===============================================================================
; DB_DispData
;
; PURPOSE:
;   Main dialog builder for program
;
; PARAMETERS
;   davePtr [in;req] - the dataset to be displayed.
;
;   filename [in] - the filename from which davePtr was obtained.
;
; KEYWORDS:
;   group_leader [in] - the ID of the parent widget from which this
;                       dialog was launched.
;
pro db_DispData, davePtr,filename, group_leader=grpl

;
if (~ptr_valid(davePtr[0])) then begin
    msg = 'Invalid data structure'
    void = dialog_message(msg,/error,dialog_parent=grpl)
    return
endif

dims = (size((*(*(*davePtr).dataStrPtr).commonStr.histPtr).qty))[0]
if (dims ne 1 && dims ne 2) then begin
    msg = 'Empty data block'
    void = dialog_message(msg,/error,dialog_parent=grpl)
    return
endif
; 
title = "DATABROWSER - Data Display"
if (n_elements(grpl) eq 0) then begin
    modal = 0
    tlb = widget_base(/col,title=title,/base_align_center,tlb_frame_attr=1)
endif else begin
    modal = 1
    tlb = widget_base(group_leader=grpl,modal=modal,/col,title=title,/base_align_center,tlb_frame_attr=1)
endelse


; Filename
header = '[Original filename]'
buffer = ((n_elements(filename) gt 0) && (strlen(filename) gt 0))? filename : '<Not Available>'
header = [header,buffer]

; descriPtr
header = [header,'','[Sample Information Tag]']
buffer = '<Not Defined>'
void = where(tag_names(*davePtr) eq 'DESCRIPTR', cnt)
if ((cnt eq 1) && (strlen(strtrim((*(*davePtr).descriPtr).name,2)) gt 0) ) then begin
    if (ptr_valid((*davePtr).descriPtr) && $
        (n_elements(*(*davePtr).descriPtr) gt 0)) then begin

        buffer = (*(*davePtr).descriPtr).name
        buffer = buffer+' = '+ strtrim(string(format='(G10.4)',(*(*davePtr).descriPtr).qty),2)
        buffer = buffer+' +/- '+ strtrim(string(format='(G10.4)',(*(*davePtr).descriPtr).err),2)
        buffer = buffer+' '+ (*(*davePtr).descriPtr).units
    endif
endif
header = [header,buffer]

; Independent Axis 1 Properties
xval = (*(*(*davePtr).dataStrPtr).commonStr.histPtr).x
nx = n_elements(xval)
xmin = min(xval,max=xmax)
header = [header,'','[Independent Variable :: x-axis]']
nxTxt = strtrim(string(nx),2)
xminTxt = strtrim(string(format='(G10.3)',xmin),2)
xmaxTxt = strtrim(string(format='(G10.3)',xmax),2)
buffer = 'Consists of '+nxTxt+' values from '+xminTxt+' to '+xmaxTxt
buffer = [buffer,'Mode: '+(*(*davePtr).dataStrPtr).commonStr.xtype]
buffer = [buffer,'Label: '+(*(*davePtr).dataStrPtr).commonStr.xlabel]
buffer = [buffer,'Units: '+(*(*davePtr).dataStrPtr).commonStr.xunits]
header = [header,buffer]

if (dims eq 2) then begin
    ;; Independent Axis 2 Properties
    yval = (*(*(*davePtr).dataStrPtr).commonStr.histPtr).y
    ny = n_elements(xval)
    ny = n_elements(yval)
    ymin = min(yval,max=ymax)
    header = [header,'','[Independent Variable :: y-axis]']
    nyTxt = strtrim(string(ny),2)
    yminTxt = strtrim(string(format='(G10.3)',ymin),2)
    ymaxTxt = strtrim(string(format='(G10.3)',ymax),2)
    buffer = 'Consists of '+nyTxt+' values from '+yminTxt+' to '+ymaxTxt
    buffer = [buffer,'Mode: '+(*(*davePtr).dataStrPtr).commonStr.ytype]
    buffer = [buffer,'Label: '+(*(*davePtr).dataStrPtr).commonStr.ylabel]
    buffer = [buffer,'Units: '+(*(*davePtr).dataStrPtr).commonStr.yunits]
    header = [header,buffer]
endif

; Data Properties
data = (*(*(*davePtr).dataStrPtr).commonStr.histPtr).qty
error = (*(*(*davePtr).dataStrPtr).commonStr.histPtr).err
dmin = min(data,max=dmax)
header = [header,'','[Dependent Variable]']
dminTxt = strtrim(string(format='(G10.3)',dmin),2)
dmaxTxt = strtrim(string(format='(G10.3)',dmax),2)
buffer = 'Minimum value is '+dminTxt+';   Maximum value = '+dmaxTxt
buffer = [buffer,'Label: '+(*(*davePtr).dataStrPtr).commonStr.histlabel]
buffer = [buffer,'Units: '+(*(*davePtr).dataStrPtr).commonStr.histunits]
header = [header,buffer]

if (dims eq 1) then begin
    ;; The data - convert to a string array
    ;xval = (*(*(*davePtr).dataStrPtr).commonStr.histPtr).x
    format = '(3(G10.3,4X))'
    buffer = string(xval[0],data[0],error[0],format=format)
    for i = 1, n_elements(data)-1 do $
      buffer = [buffer,string(xval[i],data[i],error[i],format=format)]
    if (strcmp((*(*davePtr).dataStrPtr).commonStr.xtype,'Histogram',1,/fold_case)) then $
      buffer = [buffer,string(xval[i],format='(G10.4)')]

    ;; width of widget to display output
    maxw = 60                   ; width of text widget
    width = maxw < max(strlen(header))

    ;; create a line of underscores
    line = strjoin(replicate('_',width),/single)

    ;; the final buffer to display
    buffer = [header,line,buffer]

    ;; height
    maxh = 45
    height = n_elements(buffer) < maxh
endif

if (dims eq 2) then begin
    format='(8(G10.3,1X))'
    maxw = 88                   ; max display width
    width = maxw                ; display width
    line = strjoin(replicate('_',width),/single) ; a line of underscores

    ;; x-axis data
    buffer = ['### X-Axis Values']
    buffer = [temporary(buffer),string(format=format,xval)]

    ;; y-axis data
    buffer = [temporary(buffer),'### Y-Axis Values']
    buffer = [temporary(buffer),string(format=format,yval)]

    ;; data and errors
    size = 1.0e-3*n_elements(data)*4.0
    progressOn = 0
    if (2*size ge 20.0) then begin
        progressOn = 1
        title = 'Data Display Progress'
        pTlb = keyword_set(grpl)? widget_base(group_leader=grpl,tlb_frame_attr=1,modal=1,title=title,/col) : $
               widget_base(tlb_frame_attr=1,modal=0,title=title,/col)
        msg = 'The data is '+strtrim(string(format='(G10.4)',2*size),2)+' KB!'
        msg = ['',msg,'Be patient while it is being formatted for display','']
        void = widget_text(pTlb,value=msg,xsize=max(strlen(msg)),ysize=5)
        geom = widget_info(pTLB,/geometry)
        slider = widget_slider(pTLB,/frame,minimum=0,maximum=100,xsize=geom.xsize)

        widget_control,pTLB,/realize
    endif
    nGroups = (size(data))[2]
    nGroupsTxt = strtrim(string(nGroups),2)
    fraction = 100.0/ngroups
    for i=0,nGroups-1 do begin
        grpHeader  = 'Group '+strtrim(string(i+1),2)+' of '+nGroupsTxt
        grpHeader += '; y-axis value = '+strtrim(string(format='(G10.3)',yval[i]),2)
        buffer = [temporary(buffer),'### Data: '+grpHeader]
        buffer = [temporary(buffer),string(format=format,data[*,i])]
        buffer = [temporary(buffer),'### Data Errors: '+grpHeader]
        buffer = [temporary(buffer),string(format=format,error[*,i])]
        if (progressOn) then widget_control, slider, set_value=fix((i+1)*fraction)
    endfor

    line = strjoin(replicate('_',width),/single)     ; create a line of underscores

    buffer = [header,line,temporary(buffer)] ; the final buffer to display

    maxh = 45 ; max text display height
    height = n_elements(buffer) < maxh ; text display height

    if (progressOn) then widget_control, pTlb, /destroy
endif

; Create the text display widget and populate it with the data
case (!version.os_family eq 'Windows') of
    1: font = 'Courier New*14'
    else: font = '-adobe-courier-bold-r-normal--12-120-75-75-m-70-iso8859-1'
endcase
scroll = ((width ge maxw) || (height ge maxh))? 1 : 0
textID = widget_text(tlb,value=buffer,xsize=width,ysize=height,scroll=scroll,uname='TEXT',font=font)

; Create a close button
dismTip = 'Close this dialog'
butID = widget_button(tlb,value='Dismiss',uname='DISMISS',toolTip=dismTip)

widget_control,tlb,/realize

;butGeom = widget_info(butID,/geometry)

;sPtr = ptr_new({textID:textID,butXsize:butGeom.xsize,butYsize:butGeom.ysize})
;widget_control, tlb, set_uvalue=sPtr

xmanager,'DB_DispData',tlb,event_handler='db_DispData_event',no_block=modal

return
end
