; $Id: dm_choose_file.pro,v 1.5 2006/05/26 19:42:14 ymqiu Exp $
;#######################################################################
;
; NAME:
;  dm_choose_file
;
; PURPOSE:
;  a simple function which opens a file dialog and will take care of the file extensions
;
; CATEGORY:
;  general
;
; AUTHOR:
;  Yiming Qiu
;  NIST Center for Neutron Research
;  100 Bureau Drive, Gaithersburg, MD 20899-8562
;  United States
;  yiming.qiu@nist.gov
;  May, 2006
;
; 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.
;
;#######################################################################

function dm_choose_file,filetype,write=write,read=read,dialog_parent=dialog_parent,deviceKeywords=deviceKeywords,_extra=_extra
    cd,current=oldcurrent
    defsysv,'!dave_defaults',exists=exists
    if exists then $
       if ptr_valid(!dave_defaults) then begin
          updatedefaultptr = 1
          current = (*!dave_defaults).workDir
          if (file_test(current,/directory))[0] then cd,current
       endif
    if n_elements(filetype) eq 0 then type='' $
    else type = strtrim(strlowcase(filetype),2)
    if type eq '' then $
       filter = '*' $
    else if (type eq 'jpg') or (type eq 'jpeg') then $
       filter = ['*.jpeg','*.jpg'] $
    else if (type eq 'ps') or (type eq 'eps') then $
       filter = ['*.ps','*.eps'] $
    else $
       filter = '*.'+type
    done = 0
    while (not done) do begin
       done = 1
       if type eq 'ps' and keyword_set(write) then begin
          deviceKeywords=PSConfig(cancel=canceled,group_leader=dialog_parent,/inch,xsize=5.5,xoffset=1.5)
          if canceled eq 1 then file='' else begin
             file = deviceKeywords.filename
             adir = file_test(file,/directory)
             if adir then begin
                ok   = dialog_message(file+' is a directory.',/error,dialog_parent=dialog_parent)
                file = ''
             endif
          endelse
       endif else begin
          file = dialog_pickfile(filter=filter,read=read,write=write,dialog_parent=dialog_parent,_extra=_extra)
          file = strtrim(file,2)
       endelse
       if (strlen(file[0]) ne 0) then begin
          if keyword_set(write) then begin
             file = file[0]
             if (type ne '') and (type ne '*') then begin
                tmp = strsplit(file,'.',/extract) & count = n_elements(tmp)
                if count le 1 then file=file+'.'+type else begin
                   last = strlowcase(tmp[count-1])
                   tmp  = last+type
                   if last ne type then begin
                      if (tmp ne 'jpgjpeg') and (tmp ne 'jpegjpg') and (tmp ne 'pseps') and (tmp ne 'epsps') then $
                         file = file+'.'+type
                   endif
                endelse
             endif
             exist = file_test(file)
             if exist then begin
                ok = dialog_message([file+' already exists.','Do you want to replace it?'],$
                     /question,title='Save As',dialog_parent=dialog_parent)
                if ok eq 'No' then done = 0
             endif
          endif else if keyword_set(read) then begin
             exist = file_test(file,/read)
             if total(exist) ne n_elements(exist) then begin
                ok = dialog_message("Can't open the file '"+file+"'.",/error,dialog_parent=dialog_parent)
                file = ''
             endif
          endif
       endif
    endwhile
    if keyword_set(updatedefaultptr) and (strlen(file[0]) ne 0) and (float(!version.release) ge 6.0) then $
       (*!dave_defaults).workDir = file_dirname(file[0])
    cd,oldcurrent
    return,file
end