PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/src/stringinput.sc

https://bitbucket.org/bunny351/ezd
Scala | 192 lines | 176 code | 16 blank | 0 comment | 3 complexity | b7370cbe6689465c962b1e1ded0f4f34 MD5 | raw file
  1. ;;; ezd - easy drawing for X11 displays.
  2. ;;;
  3. ;;; The procedures in this module implement simple keyboard entry.
  4. ;* Copyright 1990-1993 Digital Equipment Corporation
  5. ;* All Rights Reserved
  6. ;*
  7. ;* Permission to use, copy, and modify this software and its documentation is
  8. ;* hereby granted only under the following terms and conditions. Both the
  9. ;* above copyright notice and this permission notice must appear in all copies
  10. ;* of the software, derivative works or modified versions, and any portions
  11. ;* thereof, and both notices must appear in supporting documentation.
  12. ;*
  13. ;* Users of this software agree to the terms and conditions set forth herein,
  14. ;* and hereby grant back to Digital a non-exclusive, unrestricted, royalty-free
  15. ;* right and license under any changes, enhancements or extensions made to the
  16. ;* core functions of the software, including but not limited to those affording
  17. ;* compatibility with other hardware or software environments, but excluding
  18. ;* applications which incorporate this software. Users further agree to use
  19. ;* their best efforts to return to Digital any such changes, enhancements or
  20. ;* extensions that they make and inform Digital of noteworthy uses of this
  21. ;* software. Correspondence should be provided to Digital at:
  22. ;*
  23. ;* Director of Licensing
  24. ;* Western Research Laboratory
  25. ;* Digital Equipment Corporation
  26. ;* 250 University Avenue
  27. ;* Palo Alto, California 94301
  28. ;*
  29. ;* This software may be distributed (but not offered for sale or transferred
  30. ;* for compensation) to third parties, provided such third parties agree to
  31. ;* abide by the terms and conditions of this notice.
  32. ;*
  33. ;* THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  34. ;* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  35. ;* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  36. ;* CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  37. ;* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  38. ;* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  39. ;* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  40. ;* SOFTWARE.
  41. (module stringinput)
  42. (include "struct.sch")
  43. (include "ginfo.sch")
  44. (include "commands.sch")
  45. (include "view.sch")
  46. (include "ezd.sch")
  47. (include "events.sch")
  48. (include "match.sch")
  49. (include "text.sch")
  50. (include "xternal.sch")
  51. ;;; A keyboard input object is defined by the following command. When the
  52. ;;; cursor is within the input object (a user defined rectangle), it will
  53. ;;; become a pencil. Characters typed at this time will be collected and
  54. ;;; displayed in the input area. When the return key is pressed, the user
  55. ;;; action is executed. On execution of the user action, *USER-EVENT-TYPE* is
  56. ;;; STRING-INPUT and *USER-EVENT-MISC* is the input string.
  57. ;;;
  58. ;;; Once the object has been created, the following attributes may be accessed
  59. ;;; and set.
  60. ;;;
  61. ;;; X
  62. ;;; Y
  63. ;;; WIDTH
  64. ;;; HEIGHT
  65. ;;; ACTION
  66. ;;; TEXT
  67. ;;; TEXT-COLOR
  68. ;;; FONT
  69. ;;;
  70. ;;; DELETE-OBJECT
  71. ;;; ATTRIBUTES
  72. (define (STRING-INPUT object-name x y width height text action text-color font)
  73. (define CONTROL-U (list->string (list (integer->char 21))))
  74. (define CONTROL-H (list->string (list (integer->char 8))))
  75. (define BSKEY (list->string (list (integer->char 127))))
  76. (define EOL (list->string (list (integer->char 13))))
  77. (define (DRAW-TEXT)
  78. (ezd `(object ,object-name
  79. (fill-rectangle ,x ,y ,width ,height clear)
  80. (text ,x ,y ,width ,height left center
  81. ,(let loop ((text text))
  82. (if (or (eq? text "")
  83. (>= xwidth
  84. (cadr (text->height-width
  85. text font))))
  86. text
  87. (loop (substring text 1
  88. (string-length text)))))
  89. ,@(if text-color (list text-color) '())
  90. ,@(if font (list font) '())))))
  91. (define (KEYIN)
  92. (let ((char (car *user-event-misc*)))
  93. (cond ((equal? char control-u)
  94. (set! text "")
  95. (draw-text))
  96. ((and (or (equal? char control-h) (equal? char bskey)))
  97. (when (not (equal? text ""))
  98. (set! text (substring text 0
  99. (- (string-length text) 1)))
  100. (draw-text)))
  101. ((equal? char eol)
  102. (set! *user-event-type* 'string-input)
  103. (set! *user-event-misc* (list text))
  104. (ezd '(draw-now))
  105. (if (procedure? action) (action) (eval action)))
  106. (else (set! text (string-append text char))
  107. (draw-text)))))
  108. (define XWIDTH width)
  109. (define (VISIBLE)
  110. (if (car *user-event-misc*)
  111. (set! xwidth
  112. ((view-user->width
  113. (window-drawing->view *user-event-window*
  114. *user-event-drawing*)) width))))
  115. (define (ENTER-OBJECT)
  116. (ezd `(save-cursor ,*user-event-window*)
  117. `(set-cursor ,*user-event-window* XC_pencil)))
  118. (define (EXIT-OBJECT)
  119. (ezd `(restore-cursor ,*user-event-window*)))
  120. (define (GET-ATTRIBUTES)
  121. (map (lambda (a)
  122. (case a
  123. ((X) x)
  124. ((Y) y)
  125. ((WIDTH) width)
  126. ((HEIGHT) height)
  127. ((ACTION) action)
  128. ((TEXT) text)
  129. ((TEXT-COLOR) text-color)
  130. ((FONT) font)
  131. ((ATTRIBUTES) '(x y width height action text
  132. text-color font attributes
  133. delete-object))
  134. (else (ezd-error 'string-input
  135. "Invalid attribute: ~s" a))))
  136. *user-event-misc*))
  137. (define (SET-ATTRIBUTES)
  138. (let ((delete #f))
  139. (for-each
  140. (lambda (a)
  141. (cond ((match? (X number?) a)
  142. (set! x (cadr a)))
  143. ((match? (Y number?) a)
  144. (set! y (cadr a)))
  145. ((match? (WIDTH positive-number?) a)
  146. (set! width (cadr a)))
  147. ((match? (HEIGHT positive-number?) a)
  148. (set! height (cadr a)))
  149. ((match? (ACTION any?) a)
  150. (set! action (cadr a)))
  151. ((match? (TEXT string?) a)
  152. (set! text (cadr a)))
  153. ((match? (TEXT-COLOR color?) a)
  154. (set! text-color (cadr a)))
  155. ((match? (FONT string?) a)
  156. (set! font (cadr a)))
  157. ((equal? '(DELETE-OBJECT) a)
  158. (set! delete #t))
  159. (else (ezd-error 'string-input
  160. "Invalid attribute: ~s" a))))
  161. *user-event-misc*)
  162. (if (not delete)
  163. (draw-text)
  164. (ezd `(object ,object-name) `(when ,object-name * #f)))))
  165. (draw-text)
  166. (ezd `(when ,object-name keypress ,keyin)
  167. `(when ,object-name enter ,enter-object)
  168. `(when ,object-name exit ,exit-object)
  169. `(when ,object-name visible ,visible)
  170. `(when ,object-name get-attributes ,get-attributes)
  171. `(when ,object-name set-attributes ,set-attributes)))
  172. (define-ezd-command
  173. `(string-input ,symbol? ,number? ,number? ,number? ,number? ,string? ,any?
  174. (optional ,color?) (optional ,string?))
  175. "(string-input object-name x y width height \"initial\" action [<color>] [<\"font\">])"
  176. string-input)