]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/gadgets/gadgets-docs.factor
f5f6def67d709e5bf16d5a8ec6e3960d227fad95
[factor.git] / basis / ui / gadgets / gadgets-docs.factor
1 USING: accessors concurrency.flags help.markup help.syntax kernel
2 math.rectangles models strings ui.gadgets.private ;
3 IN: ui.gadgets
4
5 HELP: <gadget>
6 { $values { "gadget" "a new " { $link gadget } } }
7 { $description "Creates a new gadget." } ;
8
9 HELP: children-on
10 { $values { "rect" rect } { "gadget" gadget } { "seq" "a sequence of gadgets" } }
11 { $contract "Outputs a sequence of gadgets which potentially intersect a rectangle in the co-ordinate system of the gadget." }
12 { $notes "This does not have to be an accurate intersection test, and simply returning " { $link children>> } " is a valid implementation. However, an accurate intersection test reduces the amount of work done when drawing this gadget if it is partially clipped and not all children are visible." } ;
13
14 HELP: control-value
15 { $values { "control" gadget } { "value" object } }
16 { $description "Outputs the value of the control's model." } ;
17
18 HELP: gadget-child
19 { $values { "gadget" gadget } { "child" gadget } }
20 { $description "Outputs the first child of the gadget. Typically this word is used with gadgets which are known to have only one child." } ;
21
22 HELP: notify
23 { $values { "gadget" gadget } }
24 { $description "Notifies the gadget that it has a graft message to handle." }
25 { $see-also graft* ungraft* } ;
26
27 HELP: notify-ui-thread
28 { $description "Notifies the UI thread that there is work to do." }
29 { $see-also ui-notify-flag } ;
30
31 HELP: nth-gadget
32 { $values { "n" "a non-negative integer" } { "gadget" gadget } { "child" gadget } }
33 { $description "Outputs the " { $snippet "n" } "th child of the gadget." }
34 { $errors "Throws an error if " { $snippet "n" } " is negative or greater than or equal to the number of children." } ;
35
36 HELP: relative-loc
37 { $values { "fromgadget" gadget } { "togadget" gadget } { "loc" "a pair of integers" } }
38 { $description
39     "Outputs the location of the top-left corner of " { $snippet "togadget" } " relative to the co-ordinate system of " { $snippet "fromgadget" } "."
40 }
41 { $errors
42     "Throws an error if " { $snippet "togadget" } " is not contained in a child of " { $snippet "fromgadget" } "."
43 } ;
44
45 HELP: user-input*
46 { $values { "str" string } { "gadget" gadget } { "?" boolean } }
47 { $contract "Handle free-form textual input while the gadget has keyboard focus." } ;
48
49 HELP: pick-up
50 { $values { "point" "a pair of integers" } { "gadget" gadget } { "child/f" { $maybe gadget } } }
51 { $description "Outputs the child at a point in the gadget's co-ordinate system. This word recursively descends the gadget hierarchy, and so outputs the deepest child." } ;
52
53 HELP: max-dims
54 { $values { "seq" "a sequence of pairs of integers" } { "dim" "a pair of integers" } }
55 { $description "Outputs the smallest dimensions of a rectangle which can fit all the dimensions in the sequence." } ;
56
57 { pref-dims max-dims sum-dims } related-words
58
59 HELP: each-child
60 { $values { "gadget" gadget } { "quot" { $quotation ( child -- ) } } }
61 { $description "Applies the quotation to each child of the gadget." } ;
62
63 HELP: gadget-selection?
64 { $values { "gadget" gadget } { "?" boolean } }
65 { $contract "Outputs if the gadget has an active text selection; if so, the selected text can be obtained with a call to " { $link gadget-selection } "." } ;
66
67 HELP: gadget-selection
68 { $values { "gadget" gadget } { "string/f" { $maybe string } } }
69 { $contract "Outputs the gadget's text selection, or " { $link f } " if nothing is selected." } ;
70
71 HELP: relayout
72 { $values { "gadget" gadget } }
73 { $description "Relayout and redraw a gadget before the next iteration of the event loop. Unlike " { $link relayout-1 } ", this relayouts all parents up to a gadget having " { $snippet "root?" } " set, so this word should be used when the gadget's dimensions have potentially changed." } ;
74
75 HELP: relayout-1
76 { $values { "gadget" gadget } }
77 { $description "Relayout and redraw a gadget before the next iteration of the event loop. Unlike " { $link relayout } ", this does not propagate requests up to the parent, and so this word should be used when the gadget's internal layout or appearance has changed, but the dimensions have not." } ;
78
79 { relayout relayout-1 } related-words
80
81 HELP: pref-dim*
82 { $values { "gadget" gadget } { "dim" "a pair of integers" } }
83 { $contract "Outputs the preferred dimensions of the gadget, possibly computing them from the preferred dimensions of the gadget's children." }
84 { $notes "User code should not call this word directly, instead call " { $link pref-dim } "." } ;
85
86 HELP: pref-dim
87 { $values { "gadget" gadget } { "dim" "a pair of integers" } }
88 { $description "Outputs the preferred dimensions of the gadget. The value is cached between calls, and invalidated when the gadget needs to be relayout." } ;
89
90 HELP: pref-dims
91 { $values { "gadgets" "a sequence of gadgets" } { "seq" "a sequence of pairs of integers" } }
92 { $description "Collects the preferred dimensions of every gadget in the sequence into a new sequence." } ;
93
94 HELP: layout*
95 { $values { "gadget" gadget } }
96 { $contract "Lays out the children of the gadget according to the gadget's policy. The dimensions of the gadget are already set by the parent by the time this word is called." }
97 { $notes "User code should not call this word directly, instead call " { $link relayout } " and " { $link relayout-1 } "." } ;
98
99 HELP: prefer
100 { $values { "gadget" gadget } }
101 { $contract "Resizes the gadget to assume its preferred dimensions." } ;
102
103 HELP: sum-dims
104 { $values { "seq" "a sequence of pairs of integers" } { "dim" "a pair of integers" } }
105 { $description "Sums a sequence of dimensions." } ;
106
107 HELP: layout
108 { $values { "gadget" gadget } }
109 { $description "Lays out the children of the gadget if the gadget needs to be relayout, and otherwise does nothing." }
110 { $notes "User code should not call this word directly, instead call " { $link relayout } " and " { $link relayout-1 } "." } ;
111
112 { pref-dim pref-dim* layout layout* } related-words
113
114 HELP: graft*
115 { $values { "gadget" gadget } }
116 { $contract "Called to notify the gadget it has become visible on the screen. This should set up timers and threads, and acquire any resources used by the gadget." } ;
117
118 { graft graft* ungraft ungraft* } related-words
119
120 HELP: ungraft*
121 { $values { "gadget" gadget } }
122 { $contract "Called to notify the gadget it is no longer visible on the screen. This should stop timers and threads, and release any resources used by the gadget." } ;
123
124 HELP: graft
125 { $values { "gadget" gadget } }
126 { $description "Calls " { $link graft* } " on the gadget and all children." }
127 { $notes "This word should never be called directly." } ;
128
129 HELP: ungraft
130 { $values { "gadget" gadget } }
131 { $description "If the gadget is grafted, calls " { $link ungraft* } " on the gadget and all children." }
132 { $notes "This word should never be called directly." } ;
133
134 HELP: unparent
135 { $values { "gadget" gadget } }
136 { $description "Removes the gadget from its parent. This will relayout the parent." }
137 { $notes "This may result in " { $link ungraft* } " being called on the gadget and its children, if the gadget's parent is visible on the screen." } ;
138
139 HELP: clear-gadget
140 { $values { "gadget" gadget } }
141 { $description "Removes all children from the gadget. This will relayout the gadget." }
142 { $notes "This may result in " { $link ungraft* } " being called on the children, if the gadget is visible on the screen." }
143 { $side-effects "gadget" } ;
144
145 HELP: add-gadget
146 { $values { "parent" gadget } { "child" gadget } }
147 { $description "Adds a child gadget to a parent. If the gadget is contained in another gadget, " { $link unparent } " is called on the gadget first. The parent will be relayout." }
148 { $notes "Adding a gadget to a parent may result in " { $link graft* } " being called on the children, if the parent is visible on the screen." }
149 { $side-effects "parent" } ;
150
151 HELP: add-gadgets
152 { $values { "parent" gadget } { "children" "a sequence of gadgets" } }
153 { $description "Adds a sequence of gadgets to a parent. The parent will be relayout." }
154 { $notes "This may result in " { $link graft* } " being called on the children, if the parent is visible on the screen." }
155 { $side-effects "parent" } ;
156
157 HELP: child?
158 { $values { "parent" gadget } { "child" gadget } { "?" boolean } }
159 { $description "Tests if " { $snippet "child" } " is contained inside " { $snippet "parent" } "." } ;
160
161 HELP: each-parent
162 { $values { "gadget" gadget } { "quot" { $quotation ( gadget -- ? ) } } { "?" boolean } }
163 { $description "Applies the quotation to every parent of the gadget, starting from the gadget itself, stopping if the quotation yields " { $link f } ". Outputs " { $link t } " if the iteration completed, and outputs " { $link f } " if it was stopped prematurely." } ;
164
165 HELP: find-parent
166 { $values { "gadget" gadget } { "quot" { $quotation ( gadget -- ? ) } } { "parent" gadget } }
167 { $description "Outputs the first parent of the gadget, starting from the gadget itself, for which the quotation outputs a true value, or " { $link f } " if the quotation outputs " { $link f } " for every parent." } ;
168
169 HELP: focusable-child*
170 { $values { "gadget" gadget } { "child/t" "a " { $link gadget } " or " { $link t } } }
171 { $description "Outputs the child of the gadget which would prefer to receive keyboard focus, or " { $link t } " if the gadget itself should receive focus." }
172 { $examples "For example, if your gadget consists of an editor together with an output area whose contents react to changes in editor contents, then the " { $link focusable-child* } " method for your gadget class should return the editor, so that when the gadget is displayed in a window or passed to " { $link request-focus } ", the editor receives keyboard focus automatically." } ;
173
174 HELP: focusable-child
175 { $values { "gadget" gadget } { "child" gadget } }
176 { $description "Outputs the child of the gadget which would prefer to receive keyboard focus." } ;
177
178 { control-value set-control-value } related-words
179
180 HELP: layout-later
181 { $values { "gadget" gadget } }
182 { $description "Adds the gadget to the " { $link layout-queue } " and notifies the UI thread that there is a gadget to layout. If the length of the queue is larger than " { $link layout-queue-limit } ", then the current thread is yielded so that the UI thread has a chance to run." } ;
183
184 HELP: parents
185 { $values { "gadget" gadget } { "seq" "a sequence of gadgets" } }
186 { $description "Outputs a sequence of all parents of the gadget, with the first element being the gadget itself." } ;
187
188 HELP: screen-loc
189 { $values { "gadget" gadget } { "loc" "a pair of integers" } }
190 { $description "Outputs the location of the gadget relative to the top-left corner of the world containing the gadget. This word does not output a useful value if the gadget is not grafted." } ;
191
192 HELP: set-control-value
193 { $values { "value" object } { "control" gadget } }
194 { $description "Sets the value of the control's model." } ;
195
196 HELP: unqueue-graft
197 { $values { "gadget" gadget } }
198 { $description "Removes the gadget from the " { $link graft-queue } "." } ;
199
200 HELP: ui-notify-flag
201 { $var-description "A " { $link flag } " raised to notify the UI thread that there is work to do." }
202 { $see-also notify-ui-thread } ;
203
204 ARTICLE: "ui-control-impl" "Implementing controls"
205 "A " { $emphasis "control" } " is a gadget which is linked to an underlying " { $link model } " by having its " { $snippet "model" } " slot set to a " { $link model } " instance."
206 $nl
207 "Some utility words useful in control implementations:"
208 { $subsections
209     control-value
210     set-control-value
211 }
212 "Graft handling:"
213 { $subsections
214   graft
215   notify
216   ungraft
217   unqueue-graft
218 }
219 { $see-also "models" } ;