Typo.
[cl-gtk2.git] / gtk / ui-markup.lisp
1 (in-package :gtk)
2
3 (defstruct ui-d class props children expansion var initform initializer)
4
5 (defstruct ui-prop name value)
6
7 (defstruct ui-child v props)
8
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)))))
16
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))))
27
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)))))
35
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)))))))
41
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)))))))
47
48 (defgeneric pack-child (container child &key))
49
50 (defmethod pack-child ((w container) child &key)
51   (container-add w child))
52
53 (defmethod pack-child ((b box) child &key (expand t) (fill t) (padding 0) pack-type position)
54   (box-pack-start b child
55                   :expand expand
56                   :fill fill
57                   :padding padding)
58   (when pack-type
59     (setf (box-child-pack-type b child) pack-type))
60   (when position
61     (setf (box-child-position b child) position)))
62
63 (defmethod pack-child ((p paned) child &key (resize 'default) (shrink t))
64   (if (null (paned-child-1 p))
65       (paned-pack-1 p child
66                     :resize (if (eq resize 'default) nil resize)
67                     :shrink shrink)
68       (paned-pack-2 p child
69                     :resize (if (eq resize 'default) t resize)
70                     :shrink shrink)))
71
72 (defmethod pack-child ((table table) child &key
73                        left right top bottom
74                        (x-options '(:expand :fill)) (y-options '(:expand :fill)) (x-padding 0) (y-padding 0))
75
76   (unless left
77     (error "left is a mandatory child property for table packing"))
78   (unless right
79     (error "right is a mandatory child property for table packing"))
80   (unless top
81     (error "top is a mandatory child property for table packing"))
82   (unless bottom
83     (error "bottom is a mandatory child property for table packing"))
84
85   (table-attach table child left right top bottom
86                 :x-options x-options
87                 :y-options y-options
88                 :x-padding x-padding
89                 :y-padding y-padding))
90
91 (defmethod pack-child ((w tree-view) child &key)
92   (tree-view-append-column w child))
93
94 (defmethod pack-child ((w tree-view-column) child &key (expand t) attributes)
95   (tree-view-column-pack-start w child :expand expand)
96   (iter (for a on attributes by #'cddr)
97         (tree-view-column-add-attribute w child
98                                         (first a)
99                                         (second a))))
100
101 (defmethod pack-child ((b toolbar) child &key (expand 'default) (homogeneous 'default))
102   (toolbar-insert b child -1)
103   (unless (eq expand 'default)
104     (container-call-set-property b child "expand" expand +g-type-boolean+))
105   (unless (eq homogeneous 'default)
106     (container-call-set-property b child "homogeneous" homogeneous +g-type-boolean+)))
107
108 (defun set-ui-expansion-1 (d)
109   (when (ui-d-class d)
110     ;; only direct-vars do not have class
111     (setf (ui-d-var d) (get-ui-d-var d)
112           (ui-d-initform d) (get-ui-d-initform d))))
113
114 (defun set-ui-expansion (description)
115   (iter (for child in (ui-d-children description))
116         (set-ui-expansion (ui-child-v child)))
117   (set-ui-expansion-1 description))
118
119 (defun flattened-ui-descriptions (d)
120   (cons d
121         (iter (for child in (ui-d-children d))
122               (when (ui-d-class (ui-child-v child))
123                 (appending (flattened-ui-descriptions (ui-child-v child)))))))
124
125 (defmacro let-ui (ui-description &body body)
126   (let* ((description (parse-ui-description ui-description))
127          (items (flattened-ui-descriptions description)))
128     (set-ui-expansion description)
129     `(let (,@(iter (for item in items)
130                    (collect (list (ui-d-var item)
131                                   (ui-d-initform item)))))
132        ,@(iter (for item in items)
133                (appending (iter (for child in (ui-d-children item))
134                                 (for child-var = (ui-d-var (ui-child-v child)))
135                                 (let ((props
136                                        (iter (for p in (ui-child-props child))
137                                              (appending (list (ui-prop-name p) (ui-prop-value p))))))
138                                   (collect (list* 'pack-child (ui-d-var item) child-var props))))))
139        
140        ,@body)))
141