; $Id: db_editdescriptr.pro,v 1.8 2005/02/08 17:01:16 azuah Exp $
;###############################################################################
;
; NAME:
;   DB_EditDescriPtr
;
; PURPOSE:
;   A small utility widget program to view/edit the contents of a
;   descriPtr (this a davePtr variable used for tagging a
;   dataset). Useful in grouping datasets that share a common feature
;   or are part of a series, for example data for the same sample
;   taken at different temperatures.
;
; 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
;   Mar, 2003
;
; 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_EditdescriPtr_Event
;
; PURPOSE:
;   Main event handler for app.
;
; PARAMETERS:
;   event [in] - the event structure to be handled
;
pro DB_EditDescriPtr_event, event

widget_control,event.top,get_uvalue = sPtr
uname = widget_info(event.id,/uname)

case uname of
    'ACCEPT': begin
        widID = widget_info(event.top,find_by_uname = 'NAME')
        widget_control, widID, get_value=value
        (*sPtr).name = strtrim(value,2)
        widID = widget_info(event.top,find_by_uname = 'LABEL')
        widget_control, widID, get_value=value
        (*sPtr).label = strtrim(value,2)
        widID = widget_info(event.top,find_by_uname = 'UNITS')
        widget_control, widID, get_value=value
        (*sPtr).units = strtrim(value,2)
        widID = widget_info(event.top,find_by_uname = 'VALUE')
        widget_control, widID, get_value=value
        (*sPtr).value = value
        widID = widget_info(event.top,find_by_uname = 'ERROR')
        widget_control, widID, get_value=value
        (*sPtr).error = value

        (*sPtr).cancel=0
        widget_control,event.top,/destroy
    end

    'CANCEL': begin
        (*sPtr).cancel=1
        widget_control,event.top,/destroy
    end

    else:

endcase

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



;===============================================================================
; DB_EditDescriPtr
;
; PURPOSE:
;   Entry point and dialog builder for application.
;
; PARAMETERS
;   descriPtr [in|out;req] - a pointer to the descriPtr structure to be viewed/modified.
;
; KEYWORDS:
;   group_leader [in] - the ID of the parent widget from which this
;                       app was launched.
;
; RETURN VALUES:
;    1 on success, 0 on failure 
;    In addition, if successful, the descriPtr input parameter will be
;    modified to reflect the changes made in the dialog.
;
function DB_EditDescriPtr, descriPtr, group_leader=group_leader

; A descriPtr parameter must be passed
if (n_params() ne 1) then return, 0

; descriPtr must be a valid ptr
if (ptr_valid(descriPtr) ne 1) then return, 0

; descriPtr must be a descriptrstr structure
if (vartype(*descriPtr) ne 'DESCRIPTRSTR') then return, 0

; The widgets
title = 'View/Edit Sample Information' 
if (n_elements(group_leader) eq 0) then begin
    group_leader = 0L
    modal = 0
    tlb = widget_base(/col,title=title,/align_center)
endif else begin
    modal = 1
    tlb = widget_base(group_leader=group_leader,/col,title=title,modal=modal,/align_center)
endelse

cb0 = widget_base(tlb,/col,/align_center)
void = widget_label(cb0,value='Modify the fields below to edit the Sample Information Tag.',/align_center)
void = widget_label(cb0,value="Name  - name of tag/variable without spaces, eg 'Temperature'",/align_left)
void = widget_label(cb0,value="Label - descriptive text, eg 'Sample Temperature'",/align_left)
void = widget_label(cb0,value="Units - the units of the tag/variable, eg 'K'",/align_left)
void = widget_label(cb0,value="Value - the value of the tag/variable, eg '200.0'",/align_left)
void = widget_label(cb0,value="Error - the error in the value, eg '0.50'",/align_left)

cb1 = widget_base(tlb,/col,/frame,/base_align_right)
xsize = 50
void = cw_field(cb1,/row,title='Name : ',value=(*descriPtr).name,/string,uname='NAME',/all_events,xsize=xsize)
void = cw_field(cb1,/row,title='Label: ',value=(*descriPtr).legend,/string,uname='LABEL',/all_events,xsize=xsize)
void = cw_field(cb1,/row,title='Units: ',value=(*descriPtr).units,/string,uname='UNITS',/all_events,xsize=xsize)
void = cw_field(cb1,/row,title='Value: ',value=(*descriPtr).qty,/float,uname='VALUE',/all_events,xsize=xsize)
void = cw_field(cb1,/row,title='Error: ',value=(*descriPtr).err,/float,uname='ERROR',/all_events,xsize=xsize)

cb2 = widget_base(tlb,/row,/align_center)
acptTip = 'Save changes and exit'
cancTip = 'Discard changes and exit'
void = widget_button(cb2,value='Accept',uname='ACCEPT',tooltip=acptTip)
void = widget_button(cb2,value='Cancel',uname='CANCEL',tooltip=cancTip)

widget_control,tlb,/realize
sPtr = ptr_new({cancel:0, $
                name:'', $
                label:'', $
                units:'', $
                value:0.0, $
                error:0.0 $
               })

widget_control,tlb, set_uvalue=sPtr

registerName = 'db_editdescriptr'
xmanager,registerName,tlb,no_block=modal,event_handler='db_editdescriptr_event'

name = strtrim((*sPtr).name, 2)
if ((strlen(name) le 0) or (*sPtr).cancel) then begin
    ptr_free, sPtr
    return, 0
endif

(*descriPtr).name = (*sPtr).name
(*descriPtr).legend = (*sPtr).label
(*descriPtr).units = (*sPtr).units
(*descriPtr).qty = (*sPtr).value
(*descriPtr).err = (*sPtr).error

ptr_free, sPtr

return, 1

end
