chiark / gitweb /
Fixed memory corruption problem in KEYVAL-NAME
[clg] / examples / ginspect.lisp
CommitLineData
55212af1 1;; Common Lisp bindings for GTK+ 2.x
2;; Copyright 2005 Espen S. Johnsen <espen@users.sf.net>
3;;
4;; Permission is hereby granted, free of charge, to any person obtaining
5;; a copy of this software and associated documentation files (the
6;; "Software"), to deal in the Software without restriction, including
7;; without limitation the rights to use, copy, modify, merge, publish,
8;; distribute, sublicense, and/or sell copies of the Software, and to
9;; permit persons to whom the Software is furnished to do so, subject to
10;; the following conditions:
11;;
12;; The above copyright notice and this permission notice shall be
13;; included in all copies or substantial portions of the Software.
14;;
15;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
4fbbbd0e 23;; $Id: ginspect.lisp,v 1.14 2006/09/15 12:46:30 espen Exp $
55212af1 24
ce5b19ea 25#+sbcl(require :gtk)
bdfbb284 26#+(or cmu clisp)(asdf:oos 'asdf:load-op :gtk)
ce5b19ea 27
28(defpackage "GINSPECT"
ee59f701 29 (:use "COMMON-LISP" "GFFI" "GLIB" "GTK" #+cmu"PCL" #+sbcl"SB-PCL" #+clisp"MOP")
30 #+clisp(:shadowing-import-from "MOP" "SLOT-DEFINITION-TYPE")
46471546 31 (:export "GINSPECT" "GINSPECT-TOPLEVELS"))
ce5b19ea 32
33(in-package "GINSPECT")
9d059f24 34
e7b35488 35(defvar *ginspect-unbound-object-marker*
ee59f701 36 #+(or cmu clisp)(gensym "UNBOUND-OBJECT-")
e7b35488 37 #+sbcl sb-impl::*inspect-unbound-object-marker*)
38
39
9d059f24 40(defgeneric insert-object (object store parent &optional prefix))
41(defgeneric insert-parts (object store parent))
e7b35488 42(defgeneric object-has-parts-p (object))
43(defgeneric decompose-describe-object (object))
9d059f24 44
45
ce5b19ea 46;; A container to hold lisp objects "inside" the tree store
47(defclass object-container (gobject)
48 ((object :initarg :object))
49 (:metaclass gobject-class))
50
51
9d059f24 52(defun ginspect (object)
53 (let* ((store (make-instance 'tree-store
54 :column-types '(string string gobject boolean)
e7b35488 55 :column-names '(name pprinted object expanded)))
9d059f24 56 (view (make-instance 'tree-view :model store :headers-visible nil)))
57
58 (let ((column (make-instance 'tree-view-column))
e7b35488 59 (name (make-instance 'cell-renderer-text))
9d059f24 60 (object (make-instance 'cell-renderer-text)))
e7b35488 61 (tree-view-append-column view column)
62 (cell-layout-pack column name :expand nil)
4fbbbd0e 63 (cell-layout-add-attribute column name :text (tree-model-column-index store 'name))
9d059f24 64 (cell-layout-pack column object :expand t)
4fbbbd0e 65 (cell-layout-add-attribute column object :text (tree-model-column-index store 'pprinted)))
9d059f24 66
67 (insert-object object store nil)
68
69 (signal-connect view 'row-expanded
70 #'(lambda (iter path)
e7b35488 71 (when (setf
26dbb426 72 (tree-model-value store iter 'expanded)
73 (not (tree-model-value store iter 'expanded)))
e7b35488 74 (multiple-value-bind (valid child-iter)
9d059f24 75 (tree-model-iter-children store iter)
e7b35488 76 ;; Remove old children
9d059f24 77 (when valid
e7b35488 78 (loop while (tree-store-remove store child-iter))))
ce5b19ea 79 (let ((container (tree-model-value store iter 'object)))
80 (insert-parts (slot-value container 'object) store iter))
9d059f24 81 (tree-view-expand-row view path nil))))
82
83 (make-instance 'dialog
e7b35488 84 :title "Object Inspector" :show-children t :visible t
9d059f24 85 :default-width 600 :default-height 600
86 :button (list "gtk-close" #'widget-destroy :object t)
87 :child (make-instance 'scrolled-window
88 :hscrollbar-policy :automatic :child view))))
89
90
e7b35488 91(defmethod decompose-describe-object ((object t))
92 #+cmu
93 (destructuring-bind (description named-p &rest parts)
94 (inspect::describe-parts object)
95 (if (equal parts (list object))
96 (values description nil nil)
97 (values description named-p parts)))
ee59f701 98 #+sbcl(sb-impl::inspected-parts object)
99 #+clisp(values (format nil "The object is an ATOM of type ~A" (type-of object) nil nil)))
e7b35488 100
101(defmethod decompose-describe-object ((object (eql t)))
102 (values (call-next-method) nil nil))
103
104(defmethod decompose-describe-object ((object (eql nil)))
105 (values (call-next-method) nil nil))
106
107(defun propper-list-p (object)
108 (and (listp object) (null (cdr (last object)))))
109
110(defmethod decompose-describe-object ((object cons))
111 (if (propper-list-p object)
112 (values (call-next-method) nil object)
113 (values "The object is a CONS." nil (list (car object) (cdr object)))))
114
ee59f701 115#+(or cmu sbcl)
e7b35488 116(defmethod decompose-describe-object ((object #+cmu alien:system-area-pointer
117 #+sbcl sb-alien:system-area-pointer))
118 (values "The object is a SYSTEM-AREA-POINTER" nil nil))
119
120(defmethod decompose-describe-object ((object (eql *ginspect-unbound-object-marker*)))
121 (values "The slot is unbound" nil nil))
122
ee59f701 123#+(or cmu clisp)
e7b35488 124(defmethod decompose-describe-object ((object symbol))
125 (values
126 (call-next-method) t
59aecf50 127 (list
128 (cons "Name" (symbol-name object))
129 (cons "Package" (symbol-package object))
130 (cons "Value" (if (boundp object)
131 (symbol-value object)
132 *ginspect-unbound-object-marker*))
133 (cons "Function" (if (fboundp object)
134 (symbol-function object)
135 *ginspect-unbound-object-marker*))
136 (cons "Plist" (symbol-plist object)))))
e7b35488 137
e7b35488 138(defmethod decompose-describe-object ((object standard-object))
139 (values
ee59f701 140 (format nil "The instance is an object of type ~A"
48acc6ae 141 (class-name (class-of object)))
142 t
e7b35488 143 (loop
144 for slotd in (class-slots (class-of object))
48acc6ae 145 when (slot-readable-p slotd)
146 collect (let* ((slot-name (slot-definition-name slotd))
e7b35488 147 (slot-value (if (slot-boundp object slot-name)
148 (slot-value object slot-name)
d90e21ff 149 *ginspect-unbound-object-marker*)))
e7b35488 150 (cons (string slot-name) slot-value)))))
151
ee59f701 152#+clisp
153(defmethod decompose-describe-object ((object vector))
154 (values
155 (format nil "The object is a ~A of length ~A" (type-of object) (length object))
156 nil
157 (coerce object 'list)))
158
e7b35488 159
160(defmethod object-has-parts-p ((object t))
161 (nth-value 2 (decompose-describe-object object)))
162
163(defmethod object-has-parts-p ((object cons))
164 t)
165
166(defmethod object-has-parts-p ((object standard-object))
167 (class-slots (class-of object)))
168
169(defmethod object-has-parts-p ((object vector))
170 (not (zerop (length object))))
171
172
173(defmethod object-to-string ((object t))
9d059f24 174 (with-output-to-string (stream)
175 (write object :stream stream :lines 1 :right-margin 80)))
176
e7b35488 177(defmethod object-to-string ((object (eql *ginspect-unbound-object-marker*)))
178 "<unbound>")
179
180(defmethod insert-object ((object t) store parent &optional (name ""))
ce5b19ea 181 (let ((container (make-instance 'object-container :object object))
9d059f24 182 (has-parts (object-has-parts-p object)))
9d059f24 183 (let ((iter (tree-store-append store parent
e7b35488 184 (vector name (object-to-string object)
ce5b19ea 185 container (not has-parts)))))
9d059f24 186 (when has-parts
187 ;; Insert dummy child
ce5b19ea 188 (tree-store-append store iter (vector "" "" container t))))))
9d059f24 189
e7b35488 190(defmethod insert-parts :around ((object t) store parent)
191 (when (object-has-parts-p object)
192 (call-next-method)))
9d059f24 193
194(defmethod insert-parts ((object t) store parent)
e7b35488 195 (multiple-value-bind (description named-p parts)
196 (decompose-describe-object object)
197 (declare (ignore description))
198 (loop
199 for part in parts
200 do (if named-p
201 (insert-object (cdr part) store parent (string (car part)))
202 (insert-object part store parent)))))
203
204
205(defun ginspect-toplevels ()
206 (ginspect (window-list-toplevels)))