#! /home/xed/xsrexx
/*
 * dir_open: Selects a directory
 *
 * -------------------------
 * |                       |
 * |   Current_directory   |
 * |                       |
 * |  +---------------+    |
 * |  |               |    |
 * |  |               |    |
 * |  |               |    |
 * |  |               |    |
 * |  |               |    |
 * |  |               |    |
 * |  |               |    |
 * |  +---------------+    |
 * |  Directory: xxxxxxx   |
 * |                       |
 * |                       |
 * |      | Cancel |       |
 * -------------------------
 *
 *  Actions:     Simple click:  Copies the clicked directory in the INPUT field
 *               Double click:  Changes directory
 *               Enter       :  Return the directory name displayed in the INPUT field
 */
signal on novalue
procedure
parse arg start

if start = '' then start = '.'

if ~state(start, 'd') then
 { call prompt 'dir_open(): Invalid "'start'" directory'
   return ''
 }

start = follow(start)

call scan_dir

call dy_start()

$browse = dy_label(3, 1.5, "Browsing: "start)

$handle_scl1 = dy_scl(2, 4, 50, 13, dirs, /* dirs.0 */, 'm')

call dy_label 2, 24, "Directory:"
$input    = dy_input(14, 24, 43)

$dismiss  = dy_button(26, 27,  "Cancel")

$handle_dy   = dy_end("dy_ex_cb", -25, -10, 67, 30, "n")

selected = ''  /* Default */
call dy_map $handle_dy

call dy_destroy $handle_dy

return selected

dy_ex_cb:parse arg handle reason

         select
           when reason = 'B'$dismiss then
            {  call dy_unmap(handle)
               selected = ''
               return
            }

           /*
            *  Simple click: Copies the clicked directory in the INPUT field
            */
           when reason[1:2] = 'SC' then
            { call dy_vscl(handle, $handle_scl1, num, cnt)
              call dy_sinput(handle, $input, cnt.1)
              return
            }


           /*
            *  Double click: Changes directory
            */
           when reason[1:3] = 'DSC' then
            { call dy_vscl(handle, $handle_scl1, num, cnt)
              start = follow(start'/'cnt.1)
              call dy_slabel(handle, $browse, "Browsing: "start)
              call dy_dscl(handle, $handle_scl1, 1, dirs.0)
              call scan_dir
              call dy_ascl(handle, $handle_scl1, dirs)
              call dy_sinput(handle, $input, "")
              return
            }

           when reason = 'return' then
            { selected = dy_vinput(handle, $input)
              if selected ~= '' then
               { call dy_unmap(handle)
                 selected = follow(start'/'selected)
               }
              return
            }

           otherwise return 0
         end


prompt:procedure

       parse arg mes

       call dy_start()
       la = max(40, length(mes)+10)
       pos = (la-length(mes))/2
       call dy_label pos, 2, mes
       pos = la/2 - 4
       call dy_button(pos, 5,  "Continue")
       $prompt = dy_end(, 0, 0, la+1, 6, "g")
       call dy_map $prompt
       call dy_destroy $prompt
return



scan_dir: procedure expose dirs. start
          stem.0 = 0
          call SysFileTree(start, 'stem.', 'od')
          /* if 0 ~= result then
           { call prompt 'dir_open(): 'result
             return ''
           } */

          dirs.0 = stem.0+1
          dirs.1 = '..'
          do i = 1 to stem.0
             a = i+1
             dirs.a = fn(stem.i)||ft(stem.i)  /* Strips the path */
          end
return
