]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/ui-docs.factor
improve help by linking to types directly.
[factor.git] / basis / ui / ui-docs.factor
1 USING: classes colors debugger fonts help.markup help.syntax kernel
2 math.rectangles namespaces quotations sequences strings ui.backend
3 ui.gadgets ui.gadgets.books ui.gadgets.grids ui.gadgets.packs
4 ui.gadgets.private ui.gadgets.tracks ui.gadgets.worlds ui.private ui.text
5 vocabs.loader ;
6
7 IN: ui
8
9 HELP: windows
10 { $var-description "Global variable holding an association list mapping native window handles to " { $link world } " instances." } ;
11
12 { windows open-window find-window world-attributes } related-words
13
14 HELP: open-window
15 { $values { "gadget" gadget } { "title/attributes" { "a " { $link string } " or a " { $link world-attributes } " tuple" } } }
16 { $description "Opens a native window containing " { $snippet "gadget" } " with the specified attributes. If a string is provided, it is used as the window title; otherwise, the window attributes are specified in a " { $link world-attributes } " tuple." } ;
17
18 HELP: close-window
19 { $values { "gadget" gadget } }
20 { $description "Close the native window containing " { $snippet "gadget" } "." } ;
21
22 HELP: world-attributes
23 { $values { "world-class" class } { "title" string } { "status" gadget } { "gadgets" sequence } { "pixel-format-attributes" sequence } }
24 { $class-description "Tuples of this class can be passed to " { $link open-window } " to control attributes of the window opened. The following attributes can be set:" }
25 { $list
26     { { $snippet "world-class" } " specifies the class of world to construct. " { $link world } " is the default." }
27     { { $snippet "title" } " is the window title." }
28     { { $snippet "status" } ", if specified, is a gadget that will be used as the window's status bar." }
29     { { $snippet "gadgets" } " is a sequence of gadgets that will be placed inside the window." }
30     { { $snippet "pixel-format-attributes" } " is a sequence of " { $link "ui.pixel-formats-attributes" } " that the window will request for its OpenGL pixel format." }
31     { { $snippet "window-controls" } " is a sequence of " { $link "ui.gadgets.worlds-window-controls" } " that will be placed in the window." }
32 } ;
33
34 HELP: set-fullscreen
35 { $values { "gadget" gadget } { "?" boolean } }
36 { $description "Sets and unsets fullscreen mode for the gadget's world." } ;
37
38 HELP: fullscreen?
39 { $values { "gadget" gadget } { "?" boolean } }
40 { $description "Queries the gadget's world to see if it is running in fullscreen mode." } ;
41
42 { fullscreen? set-fullscreen } related-words
43
44 HELP: find-window
45 { $values { "quot" { $quotation ( world -- ? ) } } { "world" { $maybe world } } }
46 { $description "Finds a native window such that the gadget passed to " { $link open-window } " satisfies the quotation, outputting " { $link f } " if no such gadget could be found. The front-most native window is checked first." } ;
47
48 HELP: register-window
49 { $values { "world" world } { "handle" "a backend-specific handle" } }
50 { $description "Adds a window to the global " { $link windows } " variable." }
51 { $notes "This word should only be called by the UI backend. User code can open new windows with " { $link open-window } "." } ;
52
53 HELP: unregister-window
54 { $values { "handle" "a backend-specific handle" } }
55 { $description "Removes a window from the global " { $link windows } " variable." }
56 { $notes "This word should only be called only by the UI backend, and not user code." } ;
57
58 HELP: (with-ui)
59 { $values { "quot" quotation } }
60 { $contract "Starts the Factor UI." }
61 { $notes "This is a low-level word; user code should call " { $link with-ui } " instead." } ;
62
63 HELP: start-ui
64 { $values { "quot" quotation } }
65 { $description "Called by the UI backend to initialize the platform-independent parts of UI. This word should be called after the backend is ready to start displaying new windows, and before the event loop starts." } ;
66
67 HELP: (open-window)
68 { $values { "world" world } }
69 { $description "Opens a native window containing the given world. This grafts the world by calling " { $link graft } ". Each world can only be displayed in one top-level window at a time." }
70 { $notes "This word should not be called directly by user code. Instead, use " { $link open-window } "." } ;
71
72 HELP: raise-window
73 { $values { "gadget" gadget } }
74 { $description "Makes the native window containing the given gadget the front-most window." } ;
75
76 HELP: with-ui
77 { $values { "quot" { $quotation ( -- ) } } }
78 { $description "Calls the quotation, starting the UI if necessary. If starting the UI is necessary, this word does not return and the UI will start after the quotation returns." }
79 { $notes "This word should be used in the " { $link POSTPONE: MAIN: } " word of an application that uses the UI in order for the vocabulary to work when run from either the UI listener (" { $snippet "\"my-app\" run" } ") and the command line (" { $snippet "./factor -run=my-app" } ")." }
80 { $examples "The " { $vocab-link "hello-ui" } " vocabulary implements a simple UI application which uses this word." } ;
81
82 HELP: beep
83 { $description "Plays the system beep sound." } ;
84
85 HELP: system-alert
86 { $values { "caption" string } { "text" string } }
87 { $description "Displays an application-modal alert dialog box with the given caption and text." }
88 { $notes "Since the window raised by this word is modal, all processing in all Factor threads will halt until the dialog is dismissed. In an application that makes full use of the UI framework, it would be more appropriate to display a pane gadget in a Factor window. This word is meant primarily to be used to display errors in deployed applications with minimal dependencies on the UI library, such as games." } ;
89
90 HELP: topmost-window
91 { $values { "world" world } }
92 { $description "Returns the " { $link world } " representing the currently focused window." } ;
93
94 ARTICLE: "ui-glossary" "UI glossary"
95 { $table
96     { "color" { "an instance of " { $link color } } }
97     { "dimension" "a pair of integers denoting pixel size on screen" }
98     { "font" { "an instance of " { $link font } } }
99     { "gadget" { "a graphical element which responds to user input. Gadgets are tuples which (directly or indirectly) inherit from " { $link gadget } "." } }
100     { "label specifier" { "a string, " { $link f } " or a gadget. See " { $link "ui.gadgets.buttons" } } }
101     { "orientation specifier" { "one of " { $link horizontal } " or " { $link vertical } } }
102     { "point" "a pair of integers denoting a pixel location on screen" }
103 } ;
104
105 ARTICLE: "building-ui" "Building user interfaces"
106 "A gadget is a graphical element which responds to user input. Gadgets are implemented as tuples which (directly or indirectly) inherit from " { $link gadget } ", which in turn inherits from " { $link rect } "."
107 { $subsections gadget }
108 "Gadgets are arranged in a hierarchy, and all visible gadgets except for instances of " { $link world } " are contained in a parent gadget, stored in the " { $snippet "parent" } " slot."
109 { $subsections
110     "ui-geometry"
111     "ui-layouts"
112     "gadgets"
113     "ui-windows"
114     "ui.gadgets.status-bar"
115 }
116 { $see-also "models" } ;
117
118 ARTICLE: "gadgets" "Pre-made UI gadgets"
119 { $subsections
120     "ui.gadgets.labels"
121     "ui.gadgets.borders"
122     "ui.gadgets.labeled"
123     "ui.gadgets.buttons"
124     "ui.gadgets.sliders"
125     "ui.gadgets.scrollers"
126     "ui.gadgets.editors"
127     "ui.gadgets.menus"
128     "ui.gadgets.panes"
129     "ui.gadgets.presentations"
130     "ui.gadgets.tables"
131 } ;
132
133 ARTICLE: "ui-geometry" "Gadget geometry"
134 "The " { $link gadget } " class inherits from the " { $link rect } " class, and thus all gadgets have a bounding box:"
135 { $subsections "math.rectangles" }
136 "Word for converting from a child gadget's co-ordinate system to a parent's:"
137 { $subsections
138     relative-loc
139     screen-loc
140 }
141 "Hit testing:"
142 { $subsections
143     pick-up
144     children-on
145 } ;
146
147 ARTICLE: "ui-windows" "Top-level windows"
148 "Opening a top-level window:"
149 { $subsections open-window }
150 "Finding top-level windows:"
151 { $subsections find-window }
152 "Top-level windows are stored in a global variable:"
153 { $subsections windows }
154 "When a gadget is displayed in a top-level window, or added to a parent which is already showing in a top-level window, a generic word is called allowing the gadget to perform initialization tasks:"
155 { $subsections graft* }
156 "When the gadget is removed from a parent shown in a top-level window, or when the top-level window is closed, a corresponding generic word is called to clean up:"
157 { $subsections ungraft* }
158 "The root of the gadget hierarchy in a window is a special gadget which is rarely operated on directly, but it is helpful to know it exists:"
159 { $subsections world }
160 "There is also syntax for defining a main window as the entry point for a vocabulary:"
161 { $subsections POSTPONE: MAIN-WINDOW: } ;
162
163 ARTICLE: "ui-backend" "Developing UI backends"
164 "None of the words documented in this section should be called directly by user code. They are only of interest when developing new UI backends."
165 { $subsections
166     "ui-backend-init"
167     "ui-backend-windows"
168 }
169 "UI backends may implement the " { $link "clipboard-protocol" } "." ;
170
171 ARTICLE: "ui-backend-init" "UI initialization and the event loop"
172 "An UI backend is required to define a method on the " { $link (with-ui) } " word. This word should contain backend initialization, together with some boilerplate:"
173 { $code
174     "IN: shells"
175     ""
176     ": ui"
177     "    ... backend-specific initialization ..."
178     "    start-ui"
179     "    ... more backend-specific initialization ..."
180     "    ... start event loop here ... ;"
181 }
182 "The above word must call the following:"
183 { $subsections start-ui }
184 "The " { $link (with-ui) } " word must not return until the event loop has stopped and the UI has been shut down." ;
185
186 ARTICLE: "ui-backend-windows" "UI backend window management"
187 "The high-level " { $link open-window } " word eventually calls a low-level word which you must implement:"
188 { $subsections open-world-window }
189 "This word should create a native window, store some kind of handle in the " { $snippet "handle" } " slot, then call two words:"
190 { $subsections register-window }
191 "The following words must also be implemented:"
192 { $subsections
193     set-title
194     raise-window
195 }
196 "When a world needs to be redrawn, the UI will call a word automatically:"
197 { $subsections draw-world }
198 "This word can also be called directly if the UI backend is notified by the window system that window contents have been invalidated. Before and after drawing, two words are called, which the UI backend must implement:"
199 { $subsections
200     select-gl-context
201     flush-gl-context
202 }
203 "If the user clicks the window's close box, you must call the following word:"
204 { $subsections close-window } ;
205
206 ARTICLE: "ui-layouts" "Gadget hierarchy and layouts"
207 "A layout gadget is a gadget whose sole purpose is to contain other gadgets. Layout gadgets position and resize children according to a certain policy, taking the preferred size of the children into account. Gadget hierarchies are constructed by building up nested layouts."
208 { $subsections "ui-layout-basics" }
209 "Common layout gadgets:"
210 { $subsections
211     "ui-pack-layout"
212     "ui-track-layout"
213     "ui-grid-layout"
214     "ui-frame-layout"
215     "ui-book-layout"
216 }
217 "Advanced topics:"
218 { $subsections
219     "ui.gadgets.glass"
220     "ui-null-layout"
221     "ui-incremental-layout"
222     "ui-layout-impl"
223 }
224 { $see-also "ui.gadgets.borders" } ;
225
226 ARTICLE: "ui-layout-basics" "Layout basics"
227 "Gadgets are arranged in a hierarchy, and all visible gadgets except for instances of " { $link world } " are contained in a parent gadget."
228 $nl
229 "Managing the gadget hierarchy:"
230 { $subsections
231     add-gadget
232     unparent
233     add-gadgets
234     clear-gadget
235 }
236 "The children of a gadget are available via the "
237 { $snippet "children" } " slot. "
238 $nl
239 "Working with gadget children:"
240 { $subsections
241     gadget-child
242     nth-gadget
243     each-child
244     child?
245 }
246 "Working with gadget parents:"
247 { $subsections
248     parents
249     each-parent
250     find-parent
251 }
252 "Adding children, removing children and performing certain other operations initiates relayout requests automatically. In other cases, relayout may have to be triggered explicitly. There is no harm from doing this several times in a row as consecutive relayout requests are coalesced."
253 { $subsections
254     relayout
255     relayout-1
256 }
257 "Gadgets implement a generic word to inform their parents of their preferred size:"
258 { $subsections pref-dim* }
259 "To get a gadget's preferred size, do not call the above word, instead use " { $link pref-dim } ", which caches the result." ;
260
261 ARTICLE: "ui-null-layout" "Manual layouts"
262 "When automatic layout is not appropriate, gadgets can be added to a parent with no layout policy, and then positioned and sized manually by setting the " { $snippet "loc" } " field." ;
263
264 ARTICLE: "ui-layout-impl" "Implementing layout gadgets"
265 "The relayout process proceeds top-down, with parents laying out their children, which in turn lay out their children. Custom layout policy is implemented by defining a method on a generic word:"
266 { $subsections layout* }
267 "When a " { $link layout* } " method is called, the size and location of the gadget has already been determined by its parent, and the method's job is to lay out the gadget's children. Children can be positioned and resized by setting a pair of slots, " { $snippet "loc" } " and " { $snippet "dim" } "." $nl
268 "Some assorted utility words which are useful for implementing layout logic:"
269 { $subsections
270     pref-dim
271     pref-dims
272     prefer
273     max-dims
274     sum-dims
275 }
276 { $warning
277     "When implementing the " { $link layout* } " generic word for a gadget which inherits from another layout, the " { $link children-on } " word might have to be re-implemented as well."
278     $nl
279     "For example, suppose you want a " { $link grid } " layout which also displays a popup gadget on top. The implementation of " { $link children-on } " for the " { $link grid } " class determines which children of the grid are visible at one time, and this will never include your popup, so it will not be rendered, nor will it respond to gestures. The solution is to re-implement " { $link children-on } " on your class."
280 } ;
281
282 ARTICLE: "new-gadgets" "Implementing new gadgets"
283 "One of the goals of the Factor UI is to minimize the need to implement new types of gadgets by offering a highly reusable, orthogonal set of building blocks. However, in some cases implementing a new type of gadget is necessary, for example when writing a graphical visualization."
284 $nl
285 "Bare gadgets can be constructed directly, which is useful if all you need is a custom appearance with no further behavior (see " { $link "ui-pen-protocol" } "):"
286 { $subsections <gadget> }
287 "New gadgets are defined as subclasses of an existing gadget type, perhaps even " { $link gadget } " itself. Direct subclasses of " { $link gadget } " can be constructed using " { $link new } ", however some subclasses may define their own parametrized constructors (see " { $link "parametrized-constructors" } ")."
288 $nl
289 "Further topics:"
290 { $subsections
291     "ui-gestures"
292     "ui-paint"
293     "ui-control-impl"
294     "clipboard-protocol"
295     "ui.gadgets.line-support"
296 }
297 { $see-also "ui-layout-impl" } ;
298
299 ARTICLE: "starting-ui" "Starting the UI"
300 "The main word of a vocabulary implementing a UI application should use a combinator to ensure that the application works when run from the command line as well as in the UI listener:"
301 { $subsections with-ui } ;
302
303 ARTICLE: "ui" "UI framework"
304 "The " { $vocab-link "ui" } " vocabulary hierarchy implements the Factor UI framework. The implementation relies on a small amount of platform-specific code to open windows and receive keyboard and mouse events; UI gadgets are rendered using OpenGL."
305 { $subsections
306     "starting-ui"
307     "ui-glossary"
308     "building-ui"
309     "new-gadgets"
310     "ui-backend"
311 } ;
312
313 ABOUT: "ui"
314
315 HELP: close-button
316 { $description "Asks for a close button to be available for a window. Without a close button, a window cannot be closed by the user and must be closed by the program using " { $link close-window } "." } ;
317
318 HELP: minimize-button
319 { $description "Asks for a minimize button to be available for a window." } ;
320
321 HELP: maximize-button
322 { $description "Asks for a maximize button to be available for a window." } ;
323
324 HELP: resize-handles
325 { $description "Asks for resize controls to be available for a window. Without resize controls, the window size will not be changeable by the user." } ;
326
327 HELP: small-title-bar
328 { $description "Asks for a window to have a small title bar. Without a title bar, the " { $link close-button } ", " { $link minimize-button } ", and " { $link maximize-button } " controls will not be available. A small title bar may have other side effects in the window system, such as causing the window to not show up in the system task switcher and to float over other Factor windows." } ;
329
330 HELP: normal-title-bar
331 { $description "Asks for a window to have a title bar. Without a title bar, the " { $link close-button } ", " { $link minimize-button } ", and " { $link maximize-button } " controls will not be available." } ;
332
333 HELP: textured-background
334 { $description "Asks for a window to have a background that blends seamlessly with the window frame. Factor will leave the window background transparent and pass mouse button gestures not handled directly by a gadget through to the window system so that the window can be dragged from anywhere on its background." } ;
335
336 HELP: MAIN-WINDOW:
337 { $syntax "MAIN-WINDOW: window-word { attributes }
338     attribute-code ;" }
339 { $description "Defines a " { $link POSTPONE: MAIN: } " word for the current vocabulary named " { $snippet "window-word" } " that opens a UI window when the vocabulary is " { $link run } ". The " { $snippet "attributes" } " specify the key-value pairs of the window's " { $link world-attributes } ". The " { $snippet "attribute-code" } " is run with the " { $snippet "world-attributes" } " on the stack; this allows the word to construct gadget objects to place in the " { $snippet "gadget" } " slot or set other runtime-dependent world attributes." }
340 { $examples
341 "From the " { $vocab-link "hello-ui" } " vocabulary. Creates a window with the title \"Hi\" containing a label reading \"Hello world\":"
342 { $code
343 """USING: accessors ui ui.gadgets.labels ;
344 IN: hello-ui
345
346 MAIN-WINDOW: hello { { title "Hi" } }
347     "Hello world" <label> >>gadgets ;"""
348 } } ;
349
350 ARTICLE: "ui.gadgets.worlds-window-controls" "Window controls"
351 "The following window controls can be placed in a " { $link world } " window:"
352 { $subsections
353     close-button
354     minimize-button
355     maximize-button
356     resize-handles
357     small-title-bar
358     normal-title-bar
359     textured-background
360 }
361 "Provide a sequence of these values in the " { $snippet "window-controls" } " slot of the " { $link world-attributes } " tuple you pass to " { $link open-window } "." ;