home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / gprocs / navitrix.icn < prev    next >
Text File  |  2002-01-24  |  7KB  |  293 lines

  1. ############################################################################
  2. #
  3. #    File:     navitrix.icn
  4. #
  5. #    Subject:  Procedures to perform file navigation
  6. #
  7. #    Author:   Ralph E. Griswold
  8. #
  9. #    Date:     December 22, 2001
  10. #
  11. ############################################################################
  12. #
  13. #  This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #  This package provides an interface for file navigation.  It is
  18. #  intended for use with another application with a visual interface.
  19. #
  20. ############################################################################
  21. #
  22. #  At present, it is UNIX-specific.  See the preprocessor definitions
  23. #  for where platform-specific code is needed.
  24. #
  25. #  Directories are shown with a trailing slash.  Clicking on a directory
  26. #  moves there.  Clicking on a file name selects it.  The text of the
  27. #  button used to dismiss the navigator is put in the global variable
  28. #  nav_state, while the name of the selected file is put in the global
  29. #  variable nav_file.
  30. #
  31. #  nav_keyboard() processes keyboard shortcuts.  A return character is
  32. #  equivalent to clicking on the Okay button.  Other characters cause
  33. #  the top list entry to be positioned at a name that starts with or is
  34. #  close to the character.
  35. #
  36. #  The other application needs only to know this:
  37. #
  38. #    The navigator is initialized by calling nav_init().  This opens a
  39. #    hidden window, assigned to the global variable nav_window, for
  40. #    the navigator.  It also assigns the navigator root vidget to the
  41. #    global variable nav_root.
  42. #
  43. #    To use the navigator, the other application needs to change the
  44. #    canvas status nav_window to normal so it can accept user events
  45. #    and hide it again when it has been "dismissed".  The navigator
  46. #    puts the selected file in nav_file as mentioned above.
  47. #
  48. #    If the application wants to support the navigator's keyboard
  49. #    shortcuts, it needs to set the shortcut procedure to nav_keyboard
  50. #    when the navigator window is active.
  51. #
  52. #  A typical event loop for using the navigator is:
  53. #
  54. #  repeat {            # event loop
  55. #     case Active() of {
  56. #        &window   :  {        # application window
  57. #           root_cur := root
  58. #           shortcuts_cur := shortcuts
  59. #           }
  60. #        nav_window   :  {    # navigation window
  61. #           root_cur := nav_root
  62. #           shortcuts_cur := nav_keyboard
  63. #           }
  64. #        }
  65. #     ProcessEvent(root_cur, , shortcuts_cur)
  66. #     case nav_state of {
  67. #        &null   :  next
  68. #        "Okay"  :  load_pattern()
  69. #        }
  70. #     nav_state := &null
  71. #     WAttrib(nav_window, "canvas=hidden")
  72. #     }
  73. #
  74. #  where process_file() is a procedure that does something with the
  75. #  file.
  76. #
  77. #  Note that the value of nav_state determines what needs to be done.  It is
  78. #  null when the navigator has not been used since the last event.  If
  79. #  the navigator is dismissed with "Cancel" instead of "Okay", nothing
  80. #  needs to be done except hide the navigator window and set the nav_state
  81. #  to null.
  82. #
  83. #  Coupled with this is a procedure (or more than one) that makes the
  84. #  navigator window visible, as in
  85. #
  86. #    procedure open_cb()
  87. #       WAttrib(nav_window, "canvas=normal")
  88. #       ...
  89. #       return
  90. #    end
  91. #
  92. #  If there is more than one use of the navigator, the callbacks that
  93. #  enable it can set process_file to the appropriate companion procedure.
  94. #
  95. ############################################################################
  96. #
  97. #  Requires:  Version 9 graphics, UNIX
  98. #
  99. ############################################################################
  100. #
  101. #  Links: vsetup
  102. #
  103. ############################################################################
  104.  
  105. link vsetup
  106.  
  107. $include "keysyms.icn"
  108.  
  109. global directory
  110. global dir
  111. global file_list
  112. global files
  113.  
  114. # Globals used to communicate with the application that uses the navigator
  115.  
  116. global nav_file
  117. global nav_root
  118. global nav_state
  119. global nav_vidgets
  120. global nav_window
  121.  
  122. procedure nav_init()
  123.    local window_save, atts
  124.  
  125.    window_save := &window        # save current subject window
  126.    &window := &null            # clear for new subject
  127.    atts := navig_atts()
  128.    put(atts, "canvas=hidden")
  129.    (WOpen ! atts) | stop("*** can't open navigation window")
  130.    nav_vidgets := navig()        # initialize interface
  131.    nav_window := &window        # name navigation window
  132.    &window := window_save         # restore previous subject window
  133.  
  134.    files := nav_vidgets["files"]
  135.    nav_root := nav_vidgets["root"]
  136.  
  137.    nav_file := &null
  138.  
  139.    nav_refresh()
  140.  
  141.    return
  142.  
  143. end
  144.  
  145. procedure nav_files_cb(vidget, value)
  146.    static last_file, last_time
  147.  
  148.    initial {
  149.       last_file := ""
  150.       last_time := 0
  151.       }
  152.  
  153.    if /value then {
  154.       last_time := 0
  155.       return
  156.       }
  157.  
  158. $ifdef _UNIX
  159.    if value ?:= tab(upto('/')) then {
  160.       chdir(value)
  161.       nav_refresh()
  162.       return
  163.       }
  164. $else
  165.    Deliberate Syntax Error
  166. $endif
  167.  
  168.    nav_file := value
  169.  
  170.    if (value == last_file) then {
  171.       last_file := ""
  172.       nav_state := "Okay"
  173.       return
  174.       }
  175.  
  176.    last_time := 0
  177.    last_file := value
  178.  
  179.    return
  180.  
  181. end
  182.  
  183. procedure nav_refresh()
  184.    local ls, input
  185.    static x, y
  186.  
  187.    initial {
  188.       x := nav_vidgets["placeholder"].ax
  189.       y := nav_vidgets["placeholder"].ay
  190.       directory := ""
  191.       }
  192.  
  193. $ifdef _UNIX
  194.    input := open("pwd", "p")
  195. $else
  196.    Deliberate Syntax Error
  197. $endif
  198.  
  199.    WAttrib( nav_window, "drawop=reverse")
  200.    DrawString(nav_window, x, y, directory)
  201.    DrawString(nav_window, x, y, directory := !input)
  202.    WAttrib(nav_window, "drawop=copy")
  203.  
  204.    close(input)
  205.  
  206.    file_list := []
  207.  
  208. $ifdef _UNIX
  209.    ls := open("ls -a -p .", "p")
  210. $else
  211.    Deliberate Syntax Error
  212. $endif
  213.  
  214.    every put(file_list, !ls)
  215.  
  216.    VSetItems(files, file_list)
  217.  
  218.    close(ls)
  219.  
  220.    return
  221.  
  222. end
  223.  
  224. procedure nav_okay_cb()
  225.  
  226.    if /nav_file then {
  227.       Notice("No file selected.")
  228.       fail
  229.       }
  230.  
  231.    nav_state := "Okay"
  232.  
  233.    return
  234.  
  235. end
  236.  
  237. procedure nav_keyboard(e)
  238.  
  239.    case e of {
  240.       "\r"      :  nav_okay_cb()
  241.       Key_Home  :  VSetState(files, 1)
  242.       Key_End   :  VSetState(files, *file_list)
  243.       default   :  if type(e) == "string" then nav_locate(e)
  244.       }
  245.  
  246.    return
  247.  
  248. end
  249.  
  250. procedure nav_locate(e)
  251.    local i
  252.    static pos
  253.  
  254.    initial pos := list(1)
  255.  
  256.    every i := 1 to *file_list do {
  257.       if file_list[i] >>= e then break
  258.       }
  259.  
  260.    pos[1] := i
  261.  
  262.    VSetState(files, pos)
  263.  
  264.    return
  265.  
  266. end
  267.  
  268. procedure nav_cancel_cb()
  269.  
  270.    nav_state := "Cancel"
  271.  
  272.    return
  273.  
  274. end
  275.  
  276. #===<<vib:begin>>===    modify using vib; do not remove this marker line
  277. procedure navig_atts()
  278.    return ["size=294,412", "bg=pale gray", "label=Navitrix"]
  279. end
  280.  
  281. procedure navig(win, cbk)
  282. return vsetup(win, cbk,
  283.    ["navig:Sizer:::0,0,294,412:Navitrix",],
  284.    ["cancel:Button:regular::86,378,49,20:Cancel",nav_cancel_cb],
  285.    ["files:List:w::13,50,273,314:",nav_files_cb],
  286.    ["okay:Button:regular::21,378,49,20:Okay",nav_okay_cb],
  287.    ["placeholder:Button:regularno::20,22,65,17:    ",],
  288.    ["refresh:Button:regular::224,378,56,20:Refresh",nav_refresh],
  289.    ["border:Rect:grooved::18,374,55,28:",nav_okay_cb],
  290.    )
  291. end
  292. #===<<vib:end>>===    end of section maintained by vib
  293.