User input/Graphical: Difference between revisions
Content added Content deleted
m (ββTI-89 BASIC: new example) |
(A CL entry using LispWorks & CAPI) |
||
Line 191: | Line 191: | ||
return 0; |
return 0; |
||
}</lang> |
}</lang> |
||
=={{header|Common Lisp}}== |
|||
{{libheader|CAPI}} |
|||
{{works with|LispWorks}} |
|||
Prompt for a string: |
|||
<lang lisp>(capi:prompt-for-string "Enter a string:")</lang> |
|||
Repeatedly prompt for an integer until either the user presses 'Cancel' (instead of 'OK') or the integer is 75,000. |
|||
<lang lisp>(do ((number 0) (okp t)) |
|||
((or (not okp) (= 75000 number))) |
|||
(multiple-value-setq (number okp) |
|||
(capi:prompt-for-integer "Enter an integer:")))</lang> |
|||
Alternatively, display a prompt where the 'OK' button will not be enabled until the input is 75,000: |
|||
<lang lisp>(capi:prompt-for-integer "Enter an integer:" |
|||
:ok-check #'(lambda (n) (= n 75000)))</lang> |
|||
And a version which displays one prompt with an area for a string and an area for an integer, and only enables the 'OK' button when the integer is 75,000. |
|||
First, define an interface with the text-areas: |
|||
<lang lisp>(capi:define-interface string/integer-prompt () () |
|||
(:panes |
|||
(string-pane |
|||
capi:text-input-pane |
|||
:title "Enter a string:") |
|||
(integer-pane |
|||
capi:text-input-pane |
|||
:title "Enter an integer:" |
|||
:change-callback :redisplay-interface)) |
|||
(:layouts |
|||
(main |
|||
capi:column-layout |
|||
'(string-pane integer-pane))))</lang> |
|||
Then a function to extract the string and integer: |
|||
<lang lisp>(defun string/integer-prompt-value (pane) |
|||
(with-slots (string-pane integer-pane) pane |
|||
(let* ((string (capi:text-input-pane-text string-pane)) |
|||
(integer-string (capi:text-input-pane-text integer-pane)) |
|||
(integer (when (every 'digit-char-p integer-string) |
|||
(parse-integer integer-string :junk-allowed t)))) |
|||
(values (cons string integer)))))</lang> |
|||
Finally, display a prompt using the defined function to extract a value, and an 'ok-check' to ensure that the integer value is 75,000. |
|||
<lang lisp>(defun do-prompting () |
|||
(capi:popup-confirmer |
|||
(make-instance 'string/integer-prompt) |
|||
"Enter some values:" |
|||
:value-function 'string/integer-prompt-value |
|||
:ok-check #'(lambda (result) (eql (cdr result) 75000))))</lang> |
|||
=={{header|Java}}== |
=={{header|Java}}== |