Add Gdk/Pixbufs
[cl-gtk2.git] / gtk / ui-markup.lisp
blob8bb13bca722bd23d93bef1fd03eee5d1fa94c24c
1 (in-package :gtk)
3 (defstruct ui-d class props children expansion var initform initializer)
5 (defstruct ui-prop name value)
7 (defstruct ui-child v props)
9 (defun parse-ui-props (list)
10 ;; list is ({:prop value}* rest)
11 (iter (for x first list then (cddr x))
12 (while (keywordp (first x)))
13 (for (name value) = x)
14 (collect (make-ui-prop :name name :value value) into props)
15 (finally (return (values props x)))))
17 (defun parse-ui-children (list)
18 ;; list is (child*)
19 ;; child is {ui {:prop value}*}
20 (iter (while list)
21 (for child = (if (eq :expr (first (first list)))
22 (make-ui-d :var (second (first list)))
23 (parse-ui-description (first list))))
24 (for (values props rest) = (parse-ui-props (rest list)))
25 (setf list rest)
26 (collect (make-ui-child :v child :props props))))
28 (defun parse-ui-description (description)
29 ;; description is (class {:prop value}* child*)
30 ;; child is {ui {:prop value}*}
31 (let ((class (first description)))
32 (multiple-value-bind (props rest) (parse-ui-props (rest description))
33 (let ((children (parse-ui-children rest)))
34 (make-ui-d :class class :props props :children children)))))
36 (defun get-ui-d-var (d)
37 (let ((prop (find :var (ui-d-props d) :key #'ui-prop-name)))
38 (if prop
39 (ui-prop-value prop)
40 (gensym (format nil "~A-" (symbol-name (ui-d-class d)))))))
42 (defun get-ui-d-initform (d)
43 `(make-instance ',(ui-d-class d)
44 ,@(iter (for prop in (ui-d-props d))
45 (unless (eq (ui-prop-name prop) :var)
46 (appending (list (ui-prop-name prop) (ui-prop-value prop)))))))
48 (defvar *ui-child-packers* (make-hash-table))
50 (defmacro def-ui-child-packer (class (var child-def child) &body body)
51 `(setf (gethash ',class *ui-child-packers*)
52 (lambda (,var ,child-def ,child) ,@body)))
54 (def-ui-child-packer container (w d child)
55 (declare (ignore d))
56 `(container-add ,w ,child))
58 (defun get-ui-child-prop-value (d name required-p context)
59 (let ((prop (find name (ui-child-props d) :key #'ui-prop-name)))
60 (if (and required-p (null prop))
61 (error "~A is a mandatory child property for ~A" name context)
62 (when prop (ui-prop-value prop)))))
64 (def-ui-child-packer box (b d child)
65 (let ((expand-prop (find :expand (ui-child-props d) :key #'ui-prop-name))
66 (fill-prop (find :fill (ui-child-props d) :key #'ui-prop-name))
67 (padding-prop (find :padding (ui-child-props d) :key #'ui-prop-name)))
68 `(box-pack-start ,b ,child
69 ,@(when expand-prop (list :expand (ui-prop-value expand-prop)))
70 ,@(when fill-prop (list :fill (ui-prop-value fill-prop)))
71 ,@(when padding-prop (list :padding (ui-prop-value padding-prop))))))
73 (def-ui-child-packer paned (p d child)
74 (let ((resize-prop (find :resize (ui-child-props d) :key #'ui-prop-name))
75 (shrink-prop (find :shrink (ui-child-props d) :key #'ui-prop-name)))
76 `(if (null (paned-child-1 ,p))
77 (paned-pack-1 ,p ,child
78 ,@(when resize-prop (list :resize (ui-prop-value resize-prop)))
79 ,@(when shrink-prop (list :shrink (ui-prop-value shrink-prop))))
80 (paned-pack-2 ,p ,child
81 ,@(when resize-prop (list :resize (ui-prop-value resize-prop)))
82 ,@(when shrink-prop (list :shrink (ui-prop-value shrink-prop)))))))
84 (def-ui-child-packer table (table d child)
85 `(table-attach ,table ,child
86 ,(get-ui-child-prop-value d :left t "table packing")
87 ,(get-ui-child-prop-value d :right t "table packing")
88 ,(get-ui-child-prop-value d :top t "table packing")
89 ,(get-ui-child-prop-value d :bottom t "table packing")
90 ,@(let ((x-options (get-ui-child-prop-value d :x-options nil nil)))
91 (when x-options
92 (list :x-options x-options)))
93 ,@(let ((y-options (get-ui-child-prop-value d :y-options nil nil)))
94 (when y-options
95 (list :y-options y-options)))
96 ,@(let ((x-padding (get-ui-child-prop-value d :x-padding nil nil)))
97 (when x-padding
98 (list :x-padding x-padding)))
99 ,@(let ((y-padding (get-ui-child-prop-value d :y-padding nil nil)))
100 (when y-padding
101 (list :y-padding y-padding)))))
103 (defun get-child-packer-fn (d)
104 (iter (for class first (find-class (ui-d-class d)) then (first (c2mop:class-direct-superclasses class)))
105 (while class)
106 (for packer = (gethash (class-name class) *ui-child-packers*))
107 (when packer (return packer))))
109 (defun get-child-packer (d var)
110 (let ((fn (get-child-packer-fn d)))
111 (when fn
112 (let ((forms (iter (for child in (ui-d-children d))
113 (for child-var = (ui-d-var (ui-child-v child)))
114 (collect (funcall fn var child child-var)))))
115 (when forms (cons 'progn forms))))))
117 (defun get-ui-d-initializer (d var)
118 (get-child-packer d var))
120 (defun set-ui-expansion-1 (d)
121 (when (ui-d-class d)
122 ;; only direct-vars do not have class
123 (setf (ui-d-var d) (get-ui-d-var d)
124 (ui-d-initform d) (get-ui-d-initform d))
125 (setf (ui-d-initializer d) (get-ui-d-initializer d (ui-d-var d)))))
127 (defun set-ui-expansion (description)
128 (iter (for child in (ui-d-children description))
129 (set-ui-expansion (ui-child-v child)))
130 (set-ui-expansion-1 description))
132 (defun flattened-ui-descriptions (d)
133 (cons d
134 (iter (for child in (ui-d-children d))
135 (when (ui-d-class (ui-child-v child))
136 (appending (flattened-ui-descriptions (ui-child-v child)))))))
138 (defmacro let-ui (ui-description &body body)
139 (let* ((description (parse-ui-description ui-description))
140 (items (flattened-ui-descriptions description)))
141 (set-ui-expansion description)
142 `(let (,@(iter (for i in items)
143 (collect (list (ui-d-var i)
144 (ui-d-initform i)))))
145 ,@(iter (for i in items)
146 (when (ui-d-initializer i)
147 (collect (ui-d-initializer i))))
148 ,@body)))