source: trunk/source/contrib/krueger/InterfaceProjects/PackageView/package-view.lisp @ 13390

Last change on this file since 13390 was 13390, checked in by plkrueger, 10 years ago

New contrib from Paul Krueger

File size: 7.6 KB
Line 
1;;; package-view.lisp
2
3#|
4The MIT license.
5
6Copyright (c) 2010 Paul L. Krueger
7
8Permission is hereby granted, free of charge, to any person obtaining a copy of this software
9and associated documentation files (the "Software"), to deal in the Software without restriction,
10including without limitation the rights to use, copy, modify, merge, publish, distribute,
11sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all copies or substantial
15portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
18LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23|#
24
25(require :NIB)
26
27(defpackage :package-view
28  (:nicknames :pv)
29  (:use :ccl :common-lisp)
30  (:export test-package))
31
32(in-package :pv)
33
34;;; Sample lisp/Cocoa interface that uses a NIB file defined with interface builder;
35;;; A definition is provided for the "SpeechController" class that was specified to interface builder
36;;; as the class of the NIB file owner.
37;;; We manually create an instance of SpeechController and specify it as the owner for the NIB file.
38
39(defclass package-view-controller (ns:ns-object)
40  ((package-table :foreign-type :id :accessor package-table)
41   (use-table :foreign-type :id :accessor use-table)
42   (used-by-table :foreign-type :id :accessor used-by-table)
43   (current-package :accessor current-package :initform nil)
44   (current-nicknames :accessor current-nicknames :initform nil)
45   (all-packages :accessor all-packages :initform nil)
46   (current-package-use-list :accessor current-package-use-list :initform nil)
47   (current-package-used-by-list :accessor current-package-used-by-list :initform nil)
48   (window-controller :accessor window-controller :initform nil))
49  (:metaclass ns:+ns-object))
50
51(defmethod initialize-instance :after ((self package-view-controller) 
52                                       &key &allow-other-keys)
53  (let ((pkgs (list-all-packages)))
54    (setf (all-packages self) (make-array (list (list-length pkgs)) 
55                                          :initial-contents pkgs)))
56  (let ((nib-name (ccl::%make-nsstring 
57                   (namestring (truename "ip:PackageView;packageview.nib")))))
58    (setf (window-controller self)
59          (make-instance ns:ns-window-controller
60            :with-window-nib-path nib-name
61            :owner self))
62    ;; Now make the controller load the nib file and make the window visible
63    (#/window (window-controller self))
64    (#/release nib-name))
65  ;; we do the following so that ccl:terminate will be called before we are garbage
66  ;; collected and we can release the window-controller that we created
67  (ccl:terminate-when-unreachable self))
68
69(defmethod ccl:terminate ((self package-view-controller))
70  (#/release (window-controller self)))
71
72;; Initialization methods called when the nib file is loaded
73;; These initialize links from this package-view-controller to the text-views we defined
74;; to display things. Names correspond to the outlet names we defined for the
75;; PackageViewController class as file-owner in IB.
76
77(objc:defmethod (#/setPackageTable: :void) 
78                ((self package-view-controller) (tab :id))
79  (setf (package-table self) tab)
80  ;; Table may already have initialized before this link was set. Tell it to reload
81  ;; just in case.
82  (#/reloadData tab))
83
84(objc:defmethod (#/setUseTable: :void) 
85                ((self package-view-controller) (tab :id))
86  (setf (use-table self) tab)
87  ;; Table may already have initialized before this link was set. Tell it to reload
88  ;; just in case
89  (#/reloadData tab))
90
91(objc:defmethod (#/setUseByTable: :void) 
92                ((self package-view-controller) (tab :id))
93  (setf (used-by-table self) tab)
94  ;; Table may already have initialized before this link was set. Tell it to reload just in case
95  (#/reloadData tab))
96
97;; Methods called because we linked this class (as file owner) via the data-source outlet for
98;; our text views in IB
99
100(objc:defmethod (#/numberOfRowsInTableView: #>NSInteger) 
101                ((self package-view-controller) (tab :id))
102  (cond ((eql tab (package-table self))
103         (array-dimension (all-packages self) 0))
104        ((eql tab (use-table self))
105         (if (current-package-use-list self)
106           (array-dimension (current-package-use-list self) 0)
107           0))
108        ((eql tab (used-by-table self))
109         (if (current-package-used-by-list self)
110           (array-dimension (current-package-used-by-list self) 0)
111           0))
112        (t
113         ;; We can get called before the links are initialized. If so, return 0
114         0)))
115 
116(objc:defmethod (#/tableView:objectValueForTableColumn:row: :id) 
117                ((self package-view-controller) 
118                 (tab :id)
119                 (col :id)
120                 (row #>NSInteger))
121  (let ((ret-str nil))
122    (cond ((eql tab (package-table self))
123           (let ((col-id (ccl::lisp-string-from-nsstring (#/identifier col))))
124             (setf ret-str (ccl::%make-nsstring 
125                            (if (string= col-id "1") 
126                              (package-name (svref (all-packages self) row))
127                              (format nil 
128                                      "~{~a~^,~}" 
129                                      (package-nicknames 
130                                       (svref (all-packages self) row))))))))
131          ((eql tab (use-table self))
132           (setf ret-str (ccl::%make-nsstring 
133                          (if (current-package-use-list self)
134                            (package-name (svref (current-package-use-list self) row))
135                            ""))))
136          ((eql tab (used-by-table self))
137           (setf ret-str (ccl::%make-nsstring 
138                          (if (current-package-used-by-list self)
139                            (package-name (svref (current-package-used-by-list self) row))
140                            ""))))
141          (t
142           (error "~s is not a linked view (~s, ~s, or ~s)" 
143                  tab
144                  (package-table self)
145                  (use-table self)
146                  (used-by-table self))))
147    (#/autorelease ret-str)
148    ret-str))
149
150;; Methods called because we linked this class (as file owner) via the text-view delegate outlets in IB
151
152
153(objc:defmethod (#/tableViewSelectionDidChange: :void) 
154                ((self package-view-controller) (notif :id))
155  (let ((tab (#/object notif)))
156    (when (eql tab (package-table self))
157      ;; change the other two tables to reflect the package selected
158      (let* ((pkg (svref (all-packages self) (#/selectedRow (package-table self))))
159             (pkgs-used (package-use-list pkg))
160             (pkgs-using (package-used-by-list pkg)))
161        (setf (current-package-use-list self)
162              (make-array (list (list-length pkgs-used)) :initial-contents pkgs-used))
163        (setf (current-package-used-by-list self)
164              (make-array (list (list-length pkgs-using)) :initial-contents pkgs-using))
165        (#/reloadData (use-table self))
166        (#/reloadData (used-by-table self))))))
167 
168
169;; Methods called because we linked this class (as file owner) via the Window delegate outlet in IB
170
171;; test by
172(defun test-package ()
173  (make-instance 'package-view-controller))
174
175(provide :package-view)
Note: See TracBrowser for help on using the repository browser.