/[mcclim]/mcclim/dialog.lisp
ViewVC logotype

Contents of /mcclim/dialog.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.21 - (show annotations)
Thu Aug 25 20:48:40 2005 UTC (8 years, 7 months ago) by mretzlaff
Branch: MAIN
Changes since 1.20: +3 -3 lines
A patch against the first bug a patch by me introduced to mcclim.
Sorry, that shouldn't have happened.. At least it was only a bug in a
feature that the patch introduced (displaying an ACCEPTING-VALUES
dialog in its :OWN-WINDOW), and it affected only code that
thoughtlessly relied of this new and still broken feature.

(Broken because OPEN-WINDOW-STREAM doesn't really work yet. I think
there has to be a (redisplay-frame-panes frame) or something like that
in its event-loop (that is the function STANDALONE-EVENT-LOOP).)
1 ;;; -*- Mode: Lisp; Package: CLIM-INTERNALS -*-
2
3 ;;; (c) copyright 2003 by Tim Moore (moore@bricoworks.com)
4 ;;; This library is free software; you can redistribute it and/or
5 ;;; modify it under the terms of the GNU Library General Public
6 ;;; License as published by the Free Software Foundation; either
7 ;;; version 2 of the License, or (at your option) any later version.
8 ;;;
9 ;;; This library is distributed in the hope that it will be useful,
10 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ;;; Library General Public License for more details.
13 ;;;
14 ;;; You should have received a copy of the GNU Library General Public
15 ;;; License along with this library; if not, write to the
16 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 ;;; Boston, MA 02111-1307 USA.
18
19 #| Random notes:
20
21 An accepting-values stream diverts the calls to accept into calling
22 accept-present-default, as described in the spec. The output record
23 produced by accept-present-default, as well as the current value of
24 that query, arguments that were passed to accept, etc. are stored in a
25 query object. The stream stores all the query objects for this
26 invocation of accepting-values. The record created and returned by
27 accept-present-default must be a subclass of updating-output-record.
28
29 After the initial output records are drawn, invoke-accepting-values
30 blocks accepting commands. The state of the dialog state machine is changed
31 via these commands. The commands currently are:
32
33 COM-SELECT-QUERY query-id -- calls the method select-query with the
34 corresponding query object and output record object. When select-query returns
35 the "next" field, if any, is selected so the user can move from field to field
36 easily.
37
38 COM-CHANGE-QUERY query-id value -- This command is used to directly change the
39 value of a query field that does not need to be selected first for input. For
40 example, a user would click directly on a radio button without selecting the
41 gadget first.
42
43 COM-DESELECT-QUERY -- deselects the currently selected query.
44
45 COM-QUERY-EXIT -- Exits accepting-values
46
47 COM-QUERY-ABORT -- Aborts accepting-values
48
49 These commands are generated in two ways. For query fields that are entirely
50 based on CLIM drawing commands and presentations, these are emitted by
51 presentation translators. There is a presentation type selectable-query that
52 throws com-select-query for the :select gesture. Fields that are based on
53 gadgets have to throw presentations from their callbacks. This can be done
54 using the method on p. 305 of the Franz CLIM user guide, or by using the
55 McCLIM function throw-object-ptype.
56
57 After a command is executed the body of accepting-values is rerun, calling
58 accept-present-default again to update the fields' graphic appearance. [This
59 may be calling these methods too often an may change in the future]. The
60 values returned by the user's calls to accept are come from the query objects.
61
62
63 If a query field is selectable than it should implement the method
64 select-query:
65
66 SELECT-QUERY stream query record -- Make a query field active and do any
67 input. This should change the query object and setf (changedp query). This
68 method might be interrupted at any time if the user selects another field.
69
70 |#
71
72 (in-package :clim-internals)
73
74 (defclass query ()
75 ((query-identifier :accessor query-identifier :initarg :query-identifier)
76 (ptype :accessor ptype :initarg :ptype)
77 (view :accessor view :initarg :view)
78 (default :accessor default :initarg :default :initform nil)
79 (default-supplied-p :accessor default-supplied-p
80 :initarg :default-supplied-p :initform nil)
81 (value :accessor value :initarg :value :initform nil)
82 (changedp :accessor changedp :initform nil)
83 (record :accessor record :initarg :record)
84 (activation-gestures :accessor activation-gestures
85 :initform *activation-gestures*
86 :documentation "Binding of *activation-gestures* on
87 entry to this accept")
88 (delimeter-gestures :accessor delimiter-gestures
89 :initform *delimiter-gestures*
90 :documentation "Binding of *delimeter-gestures* on entry
91 to this accept")
92 (accept-arguments :accessor accept-arguments :initarg :accept-arguments)
93 (accept-condition :accessor accept-condition :initarg :accept-condition
94 :initform nil
95 :documentation "Condition signalled, if any, during
96 accept of this query")))
97
98 (defclass accepting-values-record (standard-updating-output-record)
99 ())
100
101 (defclass accepting-values-stream (standard-encapsulating-stream)
102 ((queries :accessor queries :initform nil)
103 (selected-query :accessor selected-query :initform nil)
104 (align-prompts :accessor align-prompts :initarg :align-prompts
105 :initform nil)
106 (last-pass :accessor last-pass :initform nil
107 :documentation "Flag that indicates the last pass through the
108 body of ACCEPTING-VALUES, after the user has chosen to exit. This controls
109 when conditions will be signalled from calls to ACCEPT.")))
110
111 (defmethod stream-default-view ((stream accepting-values-stream))
112 +textual-dialog-view+)
113
114 (define-condition av-exit (condition)
115 ())
116
117 ;;; The accepting-values state machine is controlled by commands. Each
118 ;;; action (e.g., "select a text field") terminates
119
120 (define-command-table accepting-values) ; :inherit-from nil???
121
122 (defvar *default-command* '(accepting-values-default-command))
123
124 ;;; The fields of the query have presentation type query. Fields that
125 ;;; are "selectable", like the default text editor field, have type
126 ;;; selectable-query. The presentation object is the query
127 ;;; identifier.
128
129 (define-presentation-type query () :inherit-from t)
130
131 (define-presentation-type selectable-query () :inherit-from 'query)
132
133 (define-presentation-type exit-button () :inherit-from t)
134
135 (define-presentation-type abort-button () :inherit-from t)
136
137 (defvar *accepting-values-stream* nil)
138
139 (defmacro with-stream-in-own-window ((&optional (stream '*query-io*)
140 &rest further-streams)
141 &rest body)
142 `(let* ((,stream (open-window-stream))
143 ,@(mapcar (lambda (a-stream)
144 (list a-stream stream))
145 further-streams))
146 (sleep 0.1) ;; hackhack.. some delay to "ensure" that the window-stream ist opened
147 (unwind-protect
148 (progn
149 ,@body)
150 (close ,stream))))
151
152 (defmacro accepting-values
153 ((&optional (stream t)
154 &rest args
155 &key own-window exit-boxes initially-select-query-identifier
156 modify-initial-query resynchronize-every-pass resize-frame
157 align-prompts label scroll-bars
158 x-position y-position width height command-table frame-class)
159 &body body)
160 (declare (ignorable exit-boxes initially-select-query-identifier
161 modify-initial-query resynchronize-every-pass resize-frame
162 align-prompts label scroll-bars
163 x-position y-position width height command-table frame-class))
164 (setq stream (stream-designator-symbol stream '*standard-input*))
165 (with-gensyms (accepting-values-continuation)
166 (let ((return-form
167 `(flet ((,accepting-values-continuation (,stream)
168 ,@body))
169 (invoke-accepting-values ,stream
170 #',accepting-values-continuation
171 ,@args))
172 ))
173 `(if ,own-window
174 (with-stream-in-own-window (,stream *standard-input* *standard-output*) ,return-form)
175 ,return-form))))
176
177 (defun invoke-accepting-values
178 (stream body
179 &key own-window exit-boxes
180 (initially-select-query-identifier nil initially-select-p)
181 modify-initial-query resynchronize-every-pass resize-frame
182 align-prompts label scroll-bars
183 x-position y-position width height
184 (command-table 'accepting-values)
185 (frame-class 'accept-values))
186 (declare (ignore own-window exit-boxes modify-initial-query
187 resize-frame label scroll-bars x-position y-position
188 width height frame-class))
189 (when (and align-prompts ;; t means the same as :right
190 (not (eq align-prompts :left)))
191 (setf align-prompts :right))
192 (multiple-value-bind (cx cy) (stream-cursor-position stream)
193 (let* ((*accepting-values-stream*
194 (make-instance 'accepting-values-stream
195 :stream stream
196 :align-prompts align-prompts))
197 (arecord (updating-output (stream
198 :record-type 'accepting-values-record)
199 (if align-prompts
200 (formatting-table (stream)
201 (funcall body *accepting-values-stream*))
202 (funcall body *accepting-values-stream*))
203 (display-exit-boxes *application-frame*
204 stream
205 (stream-default-view
206 *accepting-values-stream*))))
207 (first-time t)
208 (current-command (if initially-select-p
209 `(com-select-query
210 ,initially-select-query-identifier)
211 *default-command*)))
212 (letf (((frame-command-table *application-frame*)
213 (find-command-table command-table)))
214 (unwind-protect
215 (handler-case
216 (loop
217 (if first-time
218 (setq first-time nil)
219 (when resynchronize-every-pass
220 (redisplay arecord stream)))
221 (with-input-context
222 ('(command :command-table accepting-values))
223 (object)
224 (progn
225 (apply (command-name current-command)
226 (command-arguments current-command))
227 ;; If current command returns without throwing a
228 ;; command, go back to the default command
229 (setq current-command *default-command*))
230 (t (setq current-command object)))
231 (redisplay arecord stream))
232 (av-exit ()
233 (finalize-query-records *accepting-values-stream*)
234 (setf (last-pass *accepting-values-stream*) t)
235 (redisplay arecord stream)))
236 (erase-output-record arecord stream)
237 (setf (stream-cursor-position stream)
238 (values cx cy)))))))
239
240 (defgeneric display-exit-boxes (frame stream view))
241
242 (defmethod display-exit-boxes (frame stream (view textual-dialog-view))
243 (declare (ignore frame))
244 (updating-output (stream :unique-id 'buttons :cache-value t)
245 (fresh-line stream)
246 (with-output-as-presentation
247 (stream nil 'exit-button)
248 (format stream "OK"))
249 (write-char #\space stream)
250 (with-output-as-presentation
251 (stream nil 'abort-button)
252 (format stream "Cancel"))
253 (terpri stream)))
254
255 (defmethod stream-accept ((stream accepting-values-stream) type
256 &rest rest-args
257 &key
258 (view (stream-default-view stream))
259 (default nil default-supplied-p)
260 default-type
261 provide-default
262 insert-default
263 replace-input
264 history
265 active-p
266 prompt
267 prompt-mode
268 display-default
269 (query-identifier prompt)
270 activation-gestures
271 additional-activation-gestures
272 delimiter-gestures
273 additional-delimiter-gestures)
274 (declare (ignore activation-gestures additional-activation-gestures
275 delimiter-gestures additional-delimiter-gestures))
276 (let ((query (find query-identifier (queries stream)
277 :key #'query-identifier :test #'equal))
278 (align (align-prompts stream)))
279 (unless query
280 ;; If there's no default but empty input could return a sensible value,
281 ;; use that as a default.
282 (unless default-supplied-p
283 (setq default
284 (ignore-errors (accept-from-string type
285 ""
286 :view +textual-view+ ))))
287 (setq query (make-instance 'query
288 :query-identifier query-identifier
289 :ptype type
290 :view view
291 :default default
292 :default-supplied-p default-supplied-p
293 :value default))
294 (setf (queries stream) (nconc (queries stream) (list query))))
295 (setf (accept-arguments query) rest-args)
296 ;; If the program changes the default, that becomes the value.
297 (unless (equal default (default query))
298 (setf (default query) default)
299 (setf (value query) default))
300 (flet ((do-prompt ()
301 (apply #'prompt-for-accept stream type view rest-args))
302 (do-accept-present-default ()
303 (funcall-presentation-generic-function
304 accept-present-default
305 type (encapsulating-stream-stream stream) view
306 (value query)
307 default-supplied-p nil query-identifier)))
308 (let ((query-record nil))
309 (if align
310 (formatting-row (stream)
311 (formatting-cell (stream :align-x align)
312 (do-prompt))
313 (formatting-cell (stream)
314 (setq query-record (do-accept-present-default))))
315 (progn
316 (do-prompt)
317 (setq query-record (do-accept-present-default))))
318 (setf (record query) query-record)
319 (when (and (last-pass stream) (accept-condition query))
320 (signal (accept-condition query)))
321 (multiple-value-prog1
322 (values (value query) (ptype query) (changedp query))
323 (setf (default query) default)
324 (setf (ptype query) type)
325 (setf (changedp query) nil))))))
326
327
328 (defmethod prompt-for-accept ((stream accepting-values-stream)
329 type view
330 &rest args)
331 (declare (ignore view))
332 (apply #'prompt-for-accept-1 stream type :display-default nil args))
333
334 (define-command (com-query-exit :command-table accepting-values
335 :name nil
336 :provide-output-destination-keyword nil)
337 ()
338 (signal 'av-exit))
339
340 (define-command (com-query-abort :command-table accepting-values
341 :name nil
342 :provide-output-destination-keyword nil)
343 ()
344 (and (find-restart 'abort)
345 (invoke-restart 'abort)))
346
347 (define-command (com-change-query :command-table accepting-values
348 :name nil
349 :provide-output-destination-keyword nil)
350 ((query-identifier t)
351 (value t))
352 (when *accepting-values-stream*
353 (let ((query (find query-identifier (queries *accepting-values-stream*)
354 :key #'query-identifier :test #'equal)))
355 (when query
356 (setf (value query) value)
357 (setf (changedp query) t)))))
358
359 (defgeneric select-query (stream query record)
360 (:documentation "Does whatever is needed for input (e.g., calls accept) when
361 a query is selected for input. It is responsible for updating the
362 query object when a new value is entered in the query field." ))
363
364 (defgeneric deselect-query (stream query record)
365 (:documentation "Deselect a query field: turn the cursor off, turn off
366 highlighting, etc." ))
367
368 (define-command (com-select-query :command-table accepting-values
369 :name nil
370 :provide-output-destination-keyword nil)
371 ((query-identifier t))
372 (when *accepting-values-stream*
373 (with-accessors ((selected-query selected-query))
374 *accepting-values-stream*
375 (let* ((query-list (member query-identifier
376 (queries *accepting-values-stream*)
377 :key #'query-identifier :test #'equal))
378 (query (car query-list)))
379 (when selected-query
380 (unless (equal query-identifier (query-identifier selected-query))
381 (deselect-query *accepting-values-stream*
382 selected-query
383 (record selected-query))))
384 (when query
385 (setf selected-query query)
386 (select-query *accepting-values-stream* query (record query))
387 (let ((command-ptype '(command :command-table accepting-values)))
388 (if (cdr query-list)
389 (throw-object-ptype `(com-select-query ,(query-identifier
390 (cadr query-list)))
391 command-ptype)
392 (throw-object-ptype '(com-deselect-query) command-ptype))))))))
393
394 (define-command (com-deselect-query :command-table accepting-values
395 :name nil
396 :provide-output-destination-keyword nil)
397 ()
398 (when *accepting-values-stream*
399 (with-accessors ((selected-query selected-query))
400 *accepting-values-stream*
401 (when selected-query
402 (deselect-query *accepting-values-stream*
403 selected-query
404 (record selected-query))
405 (setf selected-query nil)))))
406
407 (defclass av-text-record (standard-updating-output-record)
408 ((editing-stream :accessor editing-stream)
409 (snapshot :accessor snapshot :initarg :snapshot :initform nil
410 :documentation "A copy of the stream buffer before accept
411 is called. Used to determine if any editing has been done by user")))
412
413 (defparameter *no-default-cache-value* (cons nil nil))
414
415 ;;; Hack until more views / dialog gadgets are defined.
416
417 (define-default-presentation-method accept-present-default
418 (type stream (view text-field-view) default default-supplied-p
419 present-p query-identifier)
420 (if (width view)
421 (multiple-value-bind (cx cy)
422 (stream-cursor-position stream)
423 (declare (ignore cy))
424 (letf (((stream-text-margin stream) (+ cx (width view))))
425 (funcall-presentation-generic-function accept-present-default
426 type
427 stream
428 +textual-dialog-view+
429 default default-supplied-p
430 present-p
431 query-identifier)))))
432
433 (define-default-presentation-method accept-present-default
434 (type stream (view textual-dialog-view) default default-supplied-p
435 present-p query-identifier)
436 (declare (ignore present-p))
437 (let* ((editing-stream nil)
438 (record (updating-output (stream :unique-id query-identifier
439 :cache-value (if default-supplied-p
440 default
441 *no-default-cache-value*)
442 :record-type 'av-text-record)
443 (with-output-as-presentation
444 (stream query-identifier 'selectable-query
445 :single-box t)
446 (surrounding-output-with-border
447 (stream :shape :inset :move-cursor t)
448 (setq editing-stream
449 (make-instance 'standard-input-editing-stream
450 :stream stream
451 :cursor-visibility nil
452 :background-ink +grey90+
453 :single-line t))))
454 (when default-supplied-p
455 (input-editing-rescan-loop ;XXX probably not needed
456 editing-stream
457 (lambda (s)
458 (presentation-replace-input s default type view
459 :rescan t)
460 (goatee::update-input-editing-stream s)))))))
461 (when editing-stream
462 (setf (editing-stream record) editing-stream))
463 record))
464
465 (defun av-do-accept (query record interactive)
466 (let* ((estream (editing-stream record))
467 (ptype (ptype query))
468 (view (view query))
469 (default (default query))
470 (default-supplied-p (default-supplied-p query))
471 (accept-args (accept-arguments query))
472 (*activation-gestures* (apply #'make-activation-gestures
473 :existing-activation-gestures
474 (activation-gestures query)
475 accept-args))
476 (*delimiter-gestures* (apply #'make-delimiter-gestures
477 :existing-delimiter-args
478 (delimiter-gestures query)
479 accept-args)))
480 ;; If there was an error on a previous pass, set the insertion pointer to
481 ;; 0 so the user has a chance to edit the field without causing another
482 ;; error. Otherwise the insertion pointer should already be at the end of
483 ;; the input (because it was activated); perhaps we should set it anyway.
484 (when (accept-condition query)
485 (setf (stream-insertion-pointer estream) 0))
486 (reset-scan-pointer estream)
487 (setf (accept-condition query) nil)
488 ;; If a condition is thrown, then accept should return the old value and
489 ;; ptype.
490 (block accept-condition-handler
491 (setf (changedp query) nil)
492 (setf (values (value query) (ptype query))
493 (input-editing-rescan-loop
494 estream
495 #'(lambda (s)
496 (handler-bind
497 ((error
498 #'(lambda (c)
499 (format *trace-output*
500 "accepting-values accept condition: ~A~%"
501 c)
502 (if interactive
503 (progn
504 (beep)
505 (goatee::set-editing-stream-insertion-pointer
506 estream
507 (1- (stream-scan-pointer estream)))
508 (immediate-rescan estream)
509 (format *trace-output* "Ack!~%"))
510 (progn
511 (setf (accept-condition query) c)
512 (return-from accept-condition-handler
513 c))))))
514 (goatee::update-input-editing-stream s)
515 (if default-supplied-p
516 (accept ptype :stream s
517 :view view :prompt nil :default default)
518 (accept ptype :stream s :view view :prompt nil))))))
519 (setf (changedp query) t))))
520
521
522
523
524 ;;; The desired
525 (defmethod select-query (stream query (record av-text-record))
526 (declare (ignore stream))
527 (let ((estream (editing-stream record))
528 (ptype (ptype query))
529 (view (view query)))
530 (declare (ignore ptype view)) ;for now
531 (with-accessors ((stream-input-buffer stream-input-buffer))
532 estream
533 (setf (cursor-visibility estream) t)
534 (setf (snapshot record) (copy-seq stream-input-buffer))
535 (av-do-accept query record t))))
536
537
538 ;;; If the query has not been changed (i.e., ACCEPT didn't return) and there is
539 ;;; no error, act as if the user activated the query.
540 (defmethod deselect-query (stream query (record av-text-record))
541 (let ((estream (editing-stream record)))
542 (setf (cursor-visibility estream) nil)
543 (when (not (or (changedp query) (accept-condition query)))
544 (finalize-query-record query record))))
545
546
547 (defgeneric finalize-query-record (query record)
548 (:documentation "Do any cleanup on a query before the accepting-values body
549 is run for the last time"))
550
551 (defmethod finalize-query-record (query record)
552 nil)
553
554 ;;; If the user edits a text field, selects another text field and
555 ;;; then exits from accepting-values without activating the first
556 ;;; field, the values returned would be some previous value or default
557 ;;; for the field, not what's on the screen. That would be completely
558 ;;; bogus. So, if a field has been edited but not activated, activate
559 ;;; it now. Unfortunately that's a bit hairy.
560
561 (defmethod finalize-query-record (query (record av-text-record))
562 (let ((estream (editing-stream record)))
563 (when (and (snapshot record)
564 (not (equal (snapshot record)
565 (stream-input-buffer estream))))
566 (let* ((activation-gestures (apply #'make-activation-gestures
567 :existing-activation-gestures
568 (activation-gestures query)
569 (accept-arguments query)))
570 (gesture (car activation-gestures)))
571 (when gesture
572 (let ((c (character-gesture-name gesture)))
573 (activate-stream estream c)
574 (reset-scan-pointer estream)
575 (av-do-accept query record nil)))))))
576
577 (defun finalize-query-records (av-stream)
578 (loop for query in (queries av-stream)
579 do (finalize-query-record query (record query))))
580
581
582 (define-presentation-to-command-translator com-select-field
583 (selectable-query com-select-query accepting-values
584 :gesture :select
585 :documentation "Select field for input"
586 :pointer-documentation "Select field for input"
587 :echo nil
588 :tester ((object)
589 (let ((selected (selected-query *accepting-values-stream*)))
590 (or (null selected)
591 (not (eq (query-identifier selected) object))))))
592 (object)
593 `(,object))
594
595 (define-presentation-to-command-translator com-exit-button
596 (exit-button com-query-exit accepting-values
597 :gesture :select
598 :documentation "Exit dialog"
599 :pointer-documentation "Exit dialog"
600 :echo nil)
601 ()
602 ())
603
604 (define-presentation-to-command-translator com-abort-button
605 (abort-button com-query-abort accepting-values
606 :gesture :select
607 :documentation "Abort dialog"
608 :pointer-documentation "Abort dialog"
609 :echo nil)
610 ()
611 ())
612
613 (defun accepting-values-default-command ()
614 (loop
615 (read-gesture :stream *accepting-values-stream*)))

  ViewVC Help
Powered by ViewVC 1.1.5