
; NAME:
;       PRO_CALL 
;
; PURPOSE:
;       This procedure gets such information as the type (function/procedure), parameters, and
;       keywords needed to call a user-defined IDL pro and displays them using the widgets.
;
; CALLING SEQUENCE:
;       PRO_CALL 
;
; INPUTS:
;       None
;
; OUTPUTS:
;       None
;
; OPTIONAL OUTPUTS:
;       PRO may be sent to a printer of choice. 
;
; CALLED ROUTINES:
;       get_pro_info function.
;
; MODIFICATION HISTORY:
;               Written by  Ed Esfandiari, NRL
;   Version 1   aee  May, 1999    Initial release.
;

function get_help
 help_info=[$
 ' This procedure gets such information as the type (function/procedure) and the',$
 'arguments (parameters and keywords) needed to call a user-defined IDL pro and',$
 'displays them using the widgets. This procedure will not work on IDL built-in',$
 'internal routines.',$
 '',$
 ' To use it, enter a .pro filename either manually or using the "PickPro" icon.',$
 'If you know the name of the pro (case sensitive), but do not know where it is',$
 'located, enter it without a path. In this case, first the current working directory',$
 'and then the NRL_LIB paths known to IDL are used to locate the pro. However, if the',$
 'entered pro name contains a path, only that path is used to do the search. The .pro',$
 'extension of the pro name may be omitted when entered manually. In this case, lower',$
 'case .pro will be automatically used as the extension.',$
 'The "PickPro" option, on the other hand, provides a point-and-click system for',$
 'selecting a pro. Starting with the modifiable search directory listed next to the',$
 'icon, you will be able to step through the directory structure in order to pick',$
 'a pro.',$
 '',$
 ' Once a pro is selected, press the "Get Info" button. For a valid pro, the name,',$
 'type, parameters, keywords, and location of the pro will be displayed.',$
 'Additionally, you can use the "Scan pro for more info" to browse the entire',$
 'file. This file can also be sent to a printer of choice for a hardcopy.',$
 '',$
 ' Send mail to esfandiari@susim.nrl.navy.mil for questions or help.']

 return,help_info
end


function modify_info,info
  args= info.args(0)
  for i=1,info.num_args-1 do args= args+', '+info.args(i)
  if(args eq '') then args='None'
  kwargs= info.kw_args(0)
  for i=1,info.num_kw_args-1 do kwargs= kwargs+', '+info.kw_args(i)
  if(kwargs eq '') then kwargs='None'
;  nm= strlowcase(info.name)+'.pro'
  return,{name:info.name,type:info.type,parameters:args,keywords:kwargs,path:info.path}
end

pro write_info,ids,st,init=init
  if(keyword_set(init) or st.type eq 'Not defined') then begin
    b= '                                                                     '
    WIDGET_CONTROL,ids(0),SET_VALUE="Pro Name:   "
    WIDGET_CONTROL,ids(1),SET_VALUE="Pro Type:   " 
    WIDGET_CONTROL,ids(2),SET_VALUE="Parameters: "
    WIDGET_CONTROL,ids(3),SET_VALUE="Keyowrds:   "
    WIDGET_CONTROL,ids(4),SET_VALUE="Location:   "
  endif else begin
    WIDGET_CONTROL,ids(0),SET_VALUE= "Pro Name:   "+strupcase(st.name)
    WIDGET_CONTROL,ids(1),SET_VALUE= "Pro Type:   "+st.type
    WIDGET_CONTROL,ids(2),SET_VALUE= "Parameters: "+st.parameters
    WIDGET_CONTROL,ids(3),SET_VALUE= "Keywords:   "+st.keywords
    if(strpos(st.path,'/') eq -1) then $ 
      WIDGET_CONTROL,ids(4),SET_VALUE= "Location:   ./"+st.path $
    else $
      WIDGET_CONTROL,ids(4),SET_VALUE= "Location:   "+st.path
  endelse
  return
end


pro pro_call_event,event
  common id_data, filename,prname,file_id,pn_id,l0,l1,l2,l3,l4,l5, $
                  diag,beg_id,info,beg_srch,orig_beg,frst_ch

 WIDGET_CONTROL, event.id, GET_UVALUE=uval
 WIDGET_CONTROL, event.top, GET_UVALUE=struct   ; get structure from UVALUE

 case (uval) of
  'done': begin
            file=findfile('dummy.ps')
            sz= size(file)
            if(sz(0) eq 1) then spawn,'/bin/rm dummy.ps'
            ; Load the original color table into display divice:
            WIDGET_CONTROL, /DESTROY, struct.pbase
          end
  'help': begin
            frst_ch= 'true'
            xdisplayfile,TITLE='Help',GROUP=event.top, HEIGHT=30, WIDTH=83,$
                         TEXT= get_help()
        end
  'prnt': begin
            frst_ch= 'true'
            sz= size(info) ; see if a structure.
            ok= ''
            if(sz(2) eq 8) then ok= info.path
            if(strtrim(filename,2) eq '' or ok eq '') then begin
              WIDGET_CONTROL, diag, SET_VALUE='Nothing to print or invalid pro name. Try scan, first.'
              return
            end
            ans= WIDGET_MESSAGE('print to '+prname+'?',/question)
            if(ans ne "Yes") then return
            WIDGET_CONTROL, diag, SET_VALUE= 'printing.........'
            WIDGET_CONTROL, /hour
            spawn,'lp -o nobanner -d '+prname+' '+info.path,/sh
            WIDGET_CONTROL, diag, SET_VALUE= 'Print done.'
       end
  'pr_nm': begin
             frst_ch= 'true'
             WIDGET_CONTROL, pn_id, GET_VALUE= prname
         end
  'brsh': begin
            frst_ch= 'true'
            WIDGET_CONTROL, beg_id, GET_VALUE=beg_srch
            beg_srch= strtrim(beg_srch(0),2) 
          end
  'more': begin
            frst_ch= 'true'
            sz= size(info) ; see if a structure.
            ok= ''
            if(sz(2) eq 8) then ok= info.path 
            if(strtrim(filename,2) eq '' or ok eq '') then begin
              WIDGET_CONTROL, diag, SET_VALUE='Must click on "Get Info" before scaning.'
              return
            end
            xdisplayfile, strtrim(info.path,2), HEIGHT=40, WIDTH=90
          end
  'f_name': begin
            WIDGET_CONTROL, file_id, GET_VALUE=filename
            filename= filename(0)
            info= ''
            if(frst_ch eq 'true') then begin
              WIDGET_CONTROL, diag, SET_VALUE='Enter a valid pro name and then Click on "Get Info".'
              write_info,[l1,l2,l3,l4,l5],{type:'Not Defined'},/init 
            end
            frst_ch= 'false'
          end
  'pick': begin
            frst_ch= 'true'
            info= ''
            write_info,[l1,l2,l3,l4,l5],{type:'Not Defined'},/init 
            filename= pickfile(path=beg_srch,filter='*.pro',/fix_filter,/must_exist,get_path=fpath)
;            filename= pickfile(path=getenv('NRL_LIB'),filter='*.pro',/fix_filter,/must_exist,get_path=fpath)
;            f_name=strmid(filename,strlen(fpath),strlen(filename))
            f_name= filename
;            info.path= fpath
            WIDGET_CONTROL, file_id, SET_VALUE=f_name
            if(filename ne '') then $
              WIDGET_CONTROL, diag, SET_VALUE='Click on "Get Info" now.' $
            else $
              WIDGET_CONTROL, diag, SET_VALUE='Pick/Enter a valid pro name and then Click on "Get Info".'
         end
  'read': begin
           frst_ch= 'true'
           WIDGET_CONTROL, /hour

;           filename= strlowcase(filename(0))
           filename= strtrim(filename(0))
           
           if(strpos(strlowcase(filename),'pro_call') ge 0) then begin
             info= {name:'pro_call',type:'Procedure',parameters:'None',keywords:'None',$
                    path:getenv('NRL_LIB')+'/lasco/util/pro_call.pro'}
             write_info,[l1,l2,l3,l4,l5],info            
             WIDGET_CONTROL, diag, SET_VALUE= '"Get Info" done.'
             return
           end
           if(strpos(strlowcase(filename),'get_pro_info') ge 0) then begin
             info= {name:'get_pro_info',type:'Function',parameters:'PRO_NAME',keywords:'None',$
                    path:getenv('NRL_LIB')+'/lasco/util/get_pro_info.pro'}
             write_info,[l1,l2,l3,l4,l5],info
             WIDGET_CONTROL, diag, SET_VALUE= '"Get Info" done.'
             return
           end

           if(strpos(filename,'/') ge 0) then begin
             ; do this only for a user types pro name that contains a path. For the
             ; pro that does't contain a path, don't do this since we will want to check
             ; the current working directory as well as the NRL_LIB paths to look for it.
             ff= filename
             if(strpos(strlowcase(ff),'.pro') eq -1) then ff= ff+'.pro'
             f= findfile(ff)
             if(f(0) eq '') then begin
               WIDGET_CONTROL, diag, $
               SET_VALUE= '"'+ff+'" is not defined. Provide a valid pro name.' 
               return
             end
           end
           pos= strpos(strlowcase(filename),'.pro')  ; must remove .pro from the filename, if any.
           fullpath= filename
           if(pos eq -1) then fullpath=fullpath+'.pro'
           if(pos gt 0) then filename= strmid(filename,0,pos)
           ftmp= filename
           while(strpos(filename,'/') ge 0) do $
             filename=strmid(filename,strpos(filename,'/')+1,strlen(filename))
           fpath= strmid(ftmp,0,strpos(ftmp,filename)-1) ; keep path without last '/'.
  
           if(fpath ne '') then cd,fpath,current=cur_path

           info= get_pro_info(filename)  ; filename can not contain path and .pro extension or it fails.
           if(strpos(info.path,'/') eq -1 and info.type ne 'Not defined') then info.path= fullpath

           if(fpath ne '') then cd,cur_path 

           info= modify_info(info)

           ;Make sure the pro in info.path exists (in case it only existed in the current working
           ;directory and was compiled from there and then user did a "cd" to a different directory
           ;from the idl prompt and then tries to use that pro again). If the pro can't be located
           ; using findfile, then set the request to 'Not defined':'

           f= findfile(info.path)
           if(f(0) eq '') then begin
             info.type= 'Not defined' 
             info.path= ''
           end

           filename= ftmp 

           WIDGET_CONTROL, diag, SET_VALUE= 'Getting info for '+filename+' .....'
           write_info,[l1,l2,l3,l4,l5],info            
           if (info.type eq 'Not defined') then $ 
             WIDGET_CONTROL, diag, $
               SET_VALUE= '"'+fullpath+'" is not defined. Provide a valid pro name.' $
           else $ 
             WIDGET_CONTROL, diag, SET_VALUE= '"Get Info" done.'
         end
 endcase

end
 



pro pro_call
  common id_data, filename,prname,file_id,pn_id,l0,l1,l2,l3,l4,l5, $
                  diag,beg_id,info,beg_srch,orig_beg,frst_ch

    info=''
    frst_ch= 'true'

    font='-adobe-times-bold-r-normal--14-140-75-75-p-77-iso8859-1'

;   Declare base as a row:

    pbase= widget_base(title='PRO_CALL',xoffset=0,/row,/frame)

    row1= widget_base(pbase,/COL)
    rc= widget_base(row1,/ROW)

    blks='            '
    txt= widget_label(rc,value=blks+blks)
    exit=  WIDGET_BUTTON(rc, VALUE='Done',UVALUE='done',/frame)
    txt= widget_label(rc,value='                                   ')
    hlp= WIDGET_BUTTON(rc, VALUE='Help',UVALUE='help',/frame)


    row2= widget_base(row1,/ROW)

    lcol= widget_base(row2,/COL,/frame)
   
    lrow=widget_base(lcol,/ROW)
    colb1= widget_base(lrow,/COLUMN)
    rowb1= widget_base(colb1,/ROW)

    avl_profs=["NA"]
    prof_num= avl_profs(0)

    r1= widget_base(rowb1,/ROW,/FRAME)
    dd= widget_base(r1,/ROW)
    r2=widget_base(dd,/COL)
    rt= widget_base(r2,/ROW)
    beg_srch= getenv('NRL_LIB')
    txt= WIDGET_LABEL(rt,VALUE="Search Path:")
    beg_id= WIDGET_TEXT(rt,UVALUE='brsh',VALUE=beg_srch,/EDITABLE, $
              /ALL_EVENTS,xsize=40)
    pick_id= WIDGET_BUTTON(rt,VALUE='PickPro',UVALUE='pick')
  
    lab= WIDGET_LABEL(r2,VALUE="Enter a pro name (or use PickPro)")
    filename=''
    file_id= WIDGET_TEXT(r2,UVALUE='f_name',VALUE=filename,/EDITABLE, $
             /ALL_EVENTS,xsize=60,/SCROLL)

    lab= WIDGET_LABEL(dd,VALUE='  ')
    rd_id= WIDGET_BUTTON(dd,VALUE='Get Info',UVALUE='read',/frame)
    lab= WIDGET_LABEL(dd,VALUE='  ')

    ee= widget_base(colb1,/COL) ; not used, for spacing only.
    ee1= widget_base(colb1,/COL) ; not used, for spacing only.

    cc= widget_base(colb1,/COL,/FRAME)

    cc3= widget_base(cc,/COL)
    b= '                                                                     '
    l1= WIDGET_LABEL(cc3,VALUE="Pro Name:   "+b,/align_left)
    l2= WIDGET_LABEL(cc3,VALUE="Pro Type:   "+b,/align_left) 
    l3= WIDGET_LABEL(cc3,VALUE="Parameters: "+b,/align_left)
    l4= WIDGET_LABEL(cc3,VALUE="Keyowrds:   "+b,/align_left)
    l5= WIDGET_LABEL(cc3,VALUE="Location:   "+b,/align_left)
    ff= WIDGET_LABEL(cc3,VALUE="") ; not used, for spacing only.
    minfo= WIDGET_BUTTON(cc3,VALUE='Scan pro for more info',UVALUE='more',/frame)
   
    gg= widget_base(colb1,/COL) ; not used, for spacing only.
    gg1= widget_base(colb1,/COL) ; not used, for spacing only.

    bb= widget_base(colb1,/COL,/FRAME)
    bb1= widget_base(bb,/ROW)
    txt= WIDGET_LABEL(bb1,VALUE="                      ")
    pt_id= WIDGET_BUTTON(bb1,VALUE="Print Pro",UVALUE='prnt',/frame)
    txt= WIDGET_LABEL(bb1,VALUE=" -> ")
    prname='hp4-247'
    pn_id= WIDGET_TEXT(bb1,UVALUE='pr_nm',VALUE=prname,xsize=18,/EDITABLE,$
                       /ALL_EVENTS)

    aa= widget_base(colb1,/COL) ; not used, for spacing only.
    aa1= widget_base(colb1,/COL) ; not used, for spacing only.
   
     dg= widget_base(row1,/ROW)
;     b='                        '
     b='              '
     diag= widget_label(dg,VALUE=b+'Pick/Enter a valid pro name and press on "Get Info" to get started.'+b,FONT=font)
    widget_control,pbase,/realize

    struct={pbase:pbase}
    widget_control,pbase, SET_UVALUE=struct
    xmanager,'pro_call',pbase,EVENT_HANDLER='pro_call_event',$
             group_leader=group
;             /MODAL

end
