/[mcclim]/mcclim/Doc/views.lisp
ViewVC logotype

Contents of /mcclim/Doc/views.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Mon Oct 30 06:26:53 2006 UTC (7 years, 5 months ago) by rstrandh
Branch: MAIN
CVS Tags: mcclim-0-9-4, McCLIM-0-9-5, McCLIM-0-9-4, McCLIM-0-9-6, McCLIM-0-9-3, HEAD
Added a new part (User manual) to the manual, and a first chapter
(Using views) to that part.
1 ;;; part of application "business logic"
2 (defclass person ()
3 ((%last-name :initarg :last-name :accessor last-name)
4 (%first-name :initarg :first-name :accessor first-name)
5 (%address :initarg :address :accessor address)
6 (%membership-number :initarg :membership-number :reader membership-number)))
7
8 ;;; constructor for the PERSON class. Not strictly necessary.
9 (defun make-person (last-name first-name address membership-number)
10 (make-instance 'person
11 :last-name last-name
12 :first-name first-name
13 :address address
14 :membership-number membership-number))
15
16 ;;; initial list of members of the organization we imagine for this example
17 (defparameter *members*
18 (list (make-person "Doe" "Jane" "123, Glencoe Terrace" 12345)
19 (make-person "Dupont" "Jean" "111, Rue de la Republique" 54321)
20 (make-person "Smith" "Eliza" "22, Trafalgar Square" 121212)
21 (make-person "Nilsson" "Sven" "Uppsalagatan 33" 98765)))
22
23 ;;; the CLIM view class that corresponds to a list of members, one member
24 ;;; per line of text in a CLIM application pane.
25 (defclass members-view (view) ())
26
27 ;;; since this view does not take any parameters in our simple example,
28 ;;; we need only a single instance of it.
29 (defparameter *members-view* (make-instance 'members-view))
30
31 ;;; the application frame. It contains instance-specific data
32 ;;; such as the members of our organization.
33 (define-application-frame views ()
34 ((%members :initform *members* :accessor members))
35 (:panes
36 (main-pane :application :height 500 :width 500
37 :display-function 'display-main-pane
38 ;; notice the initialization of the default view of
39 ;; the application pane.
40 :default-view *members-view*)
41 (interactor :interactor :height 100 :width 500))
42 (:layouts
43 (default (vertically ()
44 main-pane
45 interactor))))
46
47 ;;; the trick here is to define a generic display function
48 ;;; that is called on the frame, the pane AND the view,
49 ;;; whereas the standard CLIM display functions are called
50 ;;; only on the frame and the pane.
51 (defgeneric display-pane-with-view (frame pane view))
52
53 ;;; this is the display function that is called in each iteration
54 ;;; of the CLIM command loop. We simply call our own, more elaborate
55 ;;; display function with the default view of the pane.
56 (defun display-main-pane (frame pane)
57 (display-pane-with-view frame pane (stream-default-view pane)))
58
59 ;;; now we can start writing methods on our own display function
60 ;;; for different views. This one displays the data each member
61 ;;; on a line of its own.
62 (defmethod display-pane-with-view (frame pane (view members-view))
63 (loop for member in (members frame)
64 do (with-output-as-presentation
65 (pane member 'person)
66 (format pane "~a, ~a, ~a, ~a~%"
67 (membership-number member)
68 (last-name member)
69 (first-name member)
70 (address member)))))
71
72 ;;; this CLIM view is used to display the information about
73 ;;; a single person. It has a slot that indicates what person
74 ;;; we want to view.
75 (defclass person-view (view)
76 ((%person :initarg :person :reader person)))
77
78 ;;; this method on our own display function shows the detailed
79 ;;; information of a single member.
80 (defmethod display-pane-with-view (frame pane (view person-view))
81 (let ((person (person view)))
82 (format pane "Last name: ~a~%First Name: ~a~%Address: ~a~%Membership Number: ~a~%"
83 (last-name person)
84 (first-name person)
85 (address person)
86 (membership-number person))))
87
88 ;;; entry point to start our applciation
89 (defun views-example ()
90 (run-frame-top-level (make-application-frame 'views)))
91
92 ;;; command to quit the application
93 (define-views-command (com-quit :name t) ()
94 (frame-exit *application-frame*))
95
96 ;;; command to switch the default view of the application pane
97 ;;; (which is the value of *standard-output*) to the one that
98 ;;; shows a member per line.
99 (define-views-command (com-show-all :name t) ()
100 (setf (stream-default-view *standard-output*) *members-view*))
101
102 ;;; command to switch to a view that displays a single member.
103 ;;; this command takes as an argument the person to display.
104 ;;; In this application, the only way to satisfy the demand for
105 ;;; the argument is to click on a line of the members view. In
106 ;;; more elaborate application, you might be able to type a
107 ;;; textual representation (using completion) of the person.
108 (define-views-command (com-show-person :name t) ((person 'person))
109 (setf (stream-default-view *standard-output*)
110 (make-instance 'person-view :person person)))
111

  ViewVC Help
Powered by ViewVC 1.1.5