]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/ui-docs.factor
docs: change $subsection to $subsections
[factor.git] / basis / ui / ui-docs.factor
1 USING: help.markup help.syntax strings quotations debugger
2 namespaces ui.backend ui.gadgets ui.gadgets.worlds
3 ui.gadgets.tracks ui.gadgets.packs ui.gadgets.grids
4 ui.gadgets.private math.rectangles colors ui.text fonts
5 kernel ui.private classes sequences ;
6 IN: ui
7
8 HELP: windows
9 { $var-description "Global variable holding an association list mapping native window handles to " { $link world } " instances." } ;
10
11 { windows open-window find-window world-attributes } related-words
12
13 HELP: open-window
14 { $values { "gadget" gadget } { "title/attributes" { "a " { $link string } " or a " { $link world-attributes } " tuple" } } }
15 { $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." } ;
16
17 HELP: close-window
18 { $values { "gadget" gadget } }
19 { $description "Close the native window containing " { $snippet "gadget" } "." } ;
20
21 HELP: world-attributes
22 { $values { "world-class" class } { "title" string } { "status" gadget } { "gadgets" sequence } { "pixel-format-attributes" sequence } }
23 { $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:" }
24 { $list
25     { { $snippet "world-class" } " specifies the class of world to construct. " { $link world } " is the default." }
26     { { $snippet "title" } " is the window title." }
27     { { $snippet "status" } ", if specified, is a gadget that will be used as the window's status bar." }
28     { { $snippet "gadgets" } " is a sequence of gadgets that will be placed inside the window." }
29     { { $snippet "pixel-format-attributes" } " is a sequence of " { $link "ui.pixel-formats-attributes" } " that the window will request for its OpenGL pixel format." }
30     { { $snippet "window-controls" } " is a sequence of " { $link "ui.gadgets.worlds-window-controls" } " that will be placed in the window." }
31 } ;
32
33 HELP: set-fullscreen
34 { $values { "gadget" gadget } { "?" "a boolean" } }
35 { $description "Sets and unsets fullscreen mode for the gadget's world." } ;
36
37 HELP: fullscreen?
38 { $values { "gadget" gadget } { "?" "a boolean" } }
39 { $description "Queries the gadget's world to see if it is running in fullscreen mode." } ;
40
41 { fullscreen? set-fullscreen } related-words
42
43 HELP: find-window
44 { $values { "quot" { $quotation "( world -- ? )" } } { "world" { $maybe world } } }
45 { $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." } ;
46
47 HELP: register-window
48 { $values { "world" world } { "handle" "a backend-specific handle" } }
49 { $description "Adds a window to the global " { $link windows } " variable." }
50 { $notes "This word should only be called by the UI backend.  User code can open new windows with " { $link open-window } "." } ;
51
52 HELP: unregister-window
53 { $values { "handle" "a backend-specific handle" } }
54 { $description "Removes a window from the global " { $link windows } " variable." }
55 { $notes "This word should only be called only by the UI backend, and not user code." } ;
56
57 HELP: (with-ui)
58 { $values { "quot" quotation } }
59 { $contract "Starts the Factor UI." }
60 { $notes "This is a low-level word; user code should call " { $link with-ui } " instead." } ;
61
62 HELP: start-ui
63 { $values { "quot" quotation } }
64 { $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." } ;
65
66 HELP: (open-window)
67 { $values { "world" world } }
68 { $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." }
69 { $notes "This word should not be called directly by user code. Instead, use " { $link open-window } "." } ;
70
71 HELP: raise-window
72 { $values { "gadget" gadget } }
73 { $description "Makes the native window containing the given gadget the front-most window." } ;
74
75 HELP: with-ui
76 { $values { "quot" quotation } }
77 { $description "Calls the quotation, starting the UI first if necessary." }
78 { $notes "This combinator should be used in the " { $link POSTPONE: MAIN: } " word of a vocabulary, in order for the vocabulary to work when run from the UI listener (" { $snippet "\"my-app\" run" } " and the command line (" { $snippet "./factor -run=my-app" } ")." }
79 { $examples "The " { $vocab-link "hello-ui" } " vocabulary implements a simple UI application which uses this combinator." } ;
80
81 HELP: beep
82 { $description "Plays the system beep sound." } ;
83
84 HELP: topmost-window
85 { $values { "world" world } }
86 { $description "Returns the " { $link world } " representing the currently focused window." } ;
87
88 ARTICLE: "ui-glossary" "UI glossary"
89 { $table
90     { "color" { "an instance of " { $link color } } }
91     { "dimension" "a pair of integers denoting pixel size on screen" }
92     { "font" { "an instance of " { $link font } } }
93     { "gadget" { "a graphical element which responds to user input. Gadgets are tuples which (directly or indirectly) inherit from " { $link gadget } "." } }
94     { "label specifier" { "a string, " { $link f } " or a gadget. See " { $link "ui.gadgets.buttons" } } }
95     { "orientation specifier" { "one of " { $link horizontal } " or " { $link vertical } } }
96     { "point" "a pair of integers denoting a pixel location on screen" }
97 } ;
98
99 ARTICLE: "building-ui" "Building user interfaces"
100 "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 } "."
101 { $subsections gadget }
102 "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."
103 { $subsections
104     "ui-geometry"
105     "ui-layouts"
106     "gadgets"
107     "ui-windows"
108     "ui.gadgets.status-bar"
109 }
110 { $see-also "models" } ;
111
112 ARTICLE: "gadgets" "Pre-made UI gadgets"
113 { $subsections
114     "ui.gadgets.labels"
115     "ui.gadgets.borders"
116     "ui.gadgets.labeled"
117     "ui.gadgets.buttons"
118     "ui.gadgets.sliders"
119     "ui.gadgets.scrollers"
120     "ui.gadgets.editors"
121     "ui.gadgets.menus"
122     "ui.gadgets.panes"
123     "ui.gadgets.presentations"
124     "ui.gadgets.tables"
125 } ;
126
127 ARTICLE: "ui-geometry" "Gadget geometry"
128 "The " { $link gadget } " class inherits from the " { $link rect } " class, and thus all gadgets have a bounding box:"
129 { $subsections "math.rectangles" }
130 "Word for converting from a child gadget's co-ordinate system to a parent's:"
131 { $subsections
132     relative-loc
133     screen-loc
134 }
135 "Hit testing:"
136 { $subsections
137     pick-up
138     children-on
139 } ;
140
141 ARTICLE: "ui-windows" "Top-level windows"
142 "Opening a top-level window:"
143 { $subsections open-window }
144 "Finding top-level windows:"
145 { $subsections find-window }
146 "Top-level windows are stored in a global variable:"
147 { $subsections windows }
148 "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:"
149 { $subsections graft* }
150 "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:"
151 { $subsections ungraft* }
152 "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:"
153 { $subsections world } ;
154
155 ARTICLE: "ui-backend" "Developing UI backends"
156 "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."
157 { $subsections
158     "ui-backend-init"
159     "ui-backend-windows"
160 }
161 "UI backends may implement the " { $link "clipboard-protocol" } "." ;
162
163 ARTICLE: "ui-backend-init" "UI initialization and the event loop"
164 "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:"
165 { $code
166     "IN: shells"
167     ""
168     ": ui"
169     "    ... backend-specific initialization ..."
170     "    start-ui"
171     "    ... more backend-specific initialization ..."
172     "    ... start event loop here ... ;"
173 }
174 "The above word must call the following:"
175 { $subsections start-ui }
176 "The " { $link (with-ui) } " word must not return until the event loop has stopped and the UI has been shut down." ;
177
178 ARTICLE: "ui-backend-windows" "UI backend window management"
179 "The high-level " { $link open-window } " word eventually calls a low-level word which you must implement:"
180 { $subsections open-world-window }
181 "This word should create a native window, store some kind of handle in the " { $snippet "handle" } " slot, then call two words:"
182 { $subsections register-window }
183 "The following words must also be implemented:"
184 { $subsections
185     set-title
186     raise-window
187 }
188 "When a world needs to be redrawn, the UI will call a word automatically:"
189 { $subsections draw-world }
190 "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:"
191 { $subsections
192     select-gl-context
193     flush-gl-context
194 }
195 "If the user clicks the window's close box, you must call the following word:"
196 { $subsections close-window } ;
197
198 ARTICLE: "ui-layouts" "Gadget hierarchy and layouts"
199 "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."
200 { $subsections "ui-layout-basics" }
201 "Common layout gadgets:"
202 { $subsections
203     "ui-pack-layout"
204     "ui-track-layout"
205     "ui-grid-layout"
206     "ui-frame-layout"
207     "ui-book-layout"
208 }
209 "Advanced topics:"
210 { $subsections
211     "ui.gadgets.glass"
212     "ui-null-layout"
213     "ui-incremental-layout"
214     "ui-layout-impl"
215 }
216 { $see-also "ui.gadgets.borders" } ;
217
218 ARTICLE: "ui-layout-basics" "Layout basics"
219 "Gadgets are arranged in a hierarchy, and all visible gadgets except for instances of " { $link world } " are contained in a parent gadget."
220 $nl
221 "Managing the gadget hierarchy:"
222 { $subsections
223     add-gadget
224     unparent
225     add-gadgets
226     clear-gadget
227 }
228 "The children of a gadget are available via the "
229 { $snippet "children" } " slot. "
230 $nl
231 "Working with gadget children:"
232 { $subsections
233     gadget-child
234     nth-gadget
235     each-child
236     child?
237 }
238 "Working with gadget parents:"
239 { $subsections
240     parents
241     each-parent
242     find-parent
243 }
244 "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."
245 { $subsections
246     relayout
247     relayout-1
248 }
249 "Gadgets implement a generic word to inform their parents of their preferred size:"
250 { $subsections pref-dim* }
251 "To get a gadget's preferred size, do not call the above word, instead use " { $link pref-dim  } ", which caches the result." ;
252
253 ARTICLE: "ui-null-layout" "Manual layouts"
254 "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." ;
255
256 ARTICLE: "ui-layout-impl" "Implementing layout gadgets"
257 "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:"
258 { $subsections layout* }
259 "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
260 "Some assorted utility words which are useful for implementing layout logic:"
261 { $subsections
262     pref-dim
263     pref-dims
264     prefer
265     max-dim
266     dim-sum
267 }
268 { $warning
269     "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."
270     $nl
271     "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."
272 } ;
273
274 ARTICLE: "new-gadgets" "Implementing new gadgets"
275 "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."
276 $nl
277 "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" } "):"
278 { $subsections <gadget> }
279 "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" } ")."
280 $nl
281 "Further topics:"
282 { $subsections
283     "ui-gestures"
284     "ui-paint"
285     "ui-control-impl"
286     "clipboard-protocol"
287     "ui.gadgets.line-support"
288 }
289 { $see-also "ui-layout-impl" } ;
290
291 ARTICLE: "starting-ui" "Starting the UI"
292 "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:"
293 { $subsections with-ui } ;
294
295 ARTICLE: "ui" "UI framework"
296 "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."
297 { $subsections
298     "starting-ui"
299     "ui-glossary"
300     "building-ui"
301     "new-gadgets"
302     "ui-backend"
303 } ;
304
305 ABOUT: "ui"
306
307 HELP: close-button
308 { $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 } "." } ;
309
310 HELP: minimize-button
311 { $description "Asks for a minimize button to be available for a window." } ;
312
313 HELP: maximize-button
314 { $description "Asks for a maximize button to be available for a window." } ;
315
316 HELP: resize-handles
317 { $description "Asks for resize controls to be available for a window. Without resize controls, the window size will not be changeable by the user." } ;
318
319 HELP: small-title-bar
320 { $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." } ;
321
322 HELP: normal-title-bar
323 { $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." } ;
324
325 HELP: textured-background
326 { $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." } ;
327
328 ARTICLE: "ui.gadgets.worlds-window-controls" "Window controls"
329 "The following window controls can be placed in a " { $link world } " window:"
330 { $subsections
331     close-button
332     minimize-button
333     maximize-button
334     resize-handles
335     small-title-bar
336     normal-title-bar
337     textured-background
338 }
339 "Provide a sequence of these values in the " { $snippet "window-controls" } " slot of the " { $link world-attributes } " tuple you pass to " { $link open-window } "." ;