]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/ui-docs.factor
UI cleanup: make some ui.gadgets words private, give labels a virtual slot instead...
[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.geometry.rect colors ui.text fonts ;
5 IN: ui
6
7 HELP: windows
8 { $var-description "Global variable holding an association list mapping native window handles to " { $link world } " instances." } ;
9
10 { windows open-window find-window } related-words
11
12 HELP: open-window
13 { $values { "gadget" gadget } { "title" string } }
14 { $description "Opens a native window with the specified title." } ;
15
16 HELP: set-fullscreen?
17 { $values { "?" "a boolean" } { "gadget" gadget } }
18 { $description "Sets and unsets fullscreen mode for the gadget's world." } ;
19
20 HELP: fullscreen?
21 { $values { "gadget" gadget } { "?" "a boolean" } }
22 { $description "Queries the gadget's world to see if it is running in fullscreen mode." } ;
23
24 { fullscreen? set-fullscreen? } related-words
25
26 HELP: find-window
27 { $values { "quot" { $quotation "( world -- ? )" } } { "world" { $maybe world } } }
28 { $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." } ;
29
30 HELP: register-window
31 { $values { "world" world } { "handle" "a baackend-specific handle" } }
32 { $description "Adds a window to the global " { $link windows } " variable." }
33 { $notes "This word should only be called by the UI backend.  User code can open new windows with " { $link open-window } "." } ;
34
35 HELP: unregister-window
36 { $values { "handle" "a baackend-specific handle" } }
37 { $description "Removes a window from the global " { $link windows } " variable." }
38 { $notes "This word should only be called only by the UI backend, and not user code." } ;
39
40 HELP: (with-ui)
41 { $values { "quot" quotation } }
42 { $contract "Starts the Factor UI." }
43 { $notes "This is a low-level word; user code should call " { $link with-ui } " instead." } ;
44
45 HELP: start-ui
46 { $values { "quot" quotation } }
47 { $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." } ;
48
49 HELP: (open-window)
50 { $values { "world" world } }
51 { $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." }
52 { $notes "This word should not be called directly by user code. Instead, use " { $link open-window } "." } ;
53
54 HELP: raise-window
55 { $values { "gadget" gadget } }
56 { $description "Makes the native window containing the given gadget the front-most window." } ;
57
58 HELP: with-ui
59 { $values { "quot" quotation } }
60 { $description "Calls the quotation, starting the UI first if necessary." }
61 { $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" } ")." }
62 { $examples "The " { $vocab-link "hello-ui" } " vocabulary implements a simple UI application which uses this combinator." } ;
63
64 HELP: beep
65 { $description "Plays the system beep sound." } ;
66
67 ARTICLE: "ui-glossary" "UI glossary"
68 { $table
69     { "color" { "an instance of " { $link color } } }
70     { "dimension" "a pair of integers denoting pixel size on screen" }
71     { "font" { "an instance of " { $link font } } }
72     { "gadget" { "a graphical element which responds to user input. Gadgets are tuples which (directly or indirectly) inherit from " { $link gadget } "." } }
73     { "label specifier" { "a string, " { $link f } " or a gadget. See " { $link "ui.gadgets.buttons" } } }
74     { "orientation specifier" { "one of " { $snippet "{ 0 1 }" } " or " { $snippet "{ 1 0 }" } ", with the former denoting vertical orientation and the latter denoting horizontal. Using a vector instead of symbolic constants allows these values to be directly useful in co-ordinate calculations" } }
75     { "point" "a pair of integers denoting a pixel location on screen" }
76 } ;
77
78 ARTICLE: "building-ui" "Building user interfaces"
79 "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 } "."
80 { $subsection gadget }
81 "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."
82 { $subsection "ui-geometry" }
83 { $subsection "ui-layouts" }
84 { $subsection "gadgets" }
85 { $subsection "ui-windows" }
86 { $see-also "models" } ;
87
88 ARTICLE: "gadgets" "Pre-made UI gadgets"
89 { $subsection "ui.gadgets.labels" }
90 { $subsection "gadgets-polygons" }
91 { $subsection "ui.gadgets.borders" }
92 { $subsection "ui.gadgets.labelled" }
93 { $subsection "ui.gadgets.buttons" }
94 { $subsection "ui.gadgets.sliders" }
95 { $subsection "ui.gadgets.scrollers" }
96 { $subsection "gadgets-editors" }
97 { $subsection "ui.gadgets.menus" }
98 { $subsection "ui.gadgets.panes" }
99 { $subsection "ui.gadgets.presentations" }
100 { $subsection "ui.gadgets.lists" }
101 { $subsection "ui.gadgets.tables" } ;
102
103 ARTICLE: "ui-geometry" "Gadget geometry"
104 "The " { $link gadget } " class inherits from the " { $link rect } " class, and thus all gadgets have a bounding box:"
105 { $subsection "math.geometry.rect" }
106 "Word for converting from a child gadget's co-ordinate system to a parent's:"
107 { $subsection relative-loc }
108 { $subsection screen-loc }
109 "Hit testing:"
110 { $subsection pick-up }
111 { $subsection children-on } ;
112
113 ARTICLE: "ui-windows" "Top-level windows"
114 "Opening a top-level window:"
115 { $subsection open-window }
116 "Finding top-level windows:"
117 { $subsection find-window }
118 "Top-level windows are stored in a global variable:"
119 { $subsection windows }
120 "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:"
121 { $subsection graft* }
122 "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:"
123 { $subsection ungraft* }
124 "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:"
125 { $subsection world } ;
126
127 ARTICLE: "ui-backend" "Developing UI backends"
128 "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."
129 { $subsection "ui-backend-init" }
130 { $subsection "ui-backend-windows" }
131 "UI backends may implement the " { $link "clipboard-protocol" } "." ;
132
133 ARTICLE: "ui-backend-init" "UI initialization and the event loop"
134 "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:"
135 { $code
136     "IN: shells"
137     ""
138     ": ui"
139     "    ... backend-specific initialization ..."
140     "    start-ui"
141     "    ... more backend-specific initialization ..."
142     "    ... start event loop here ... ;"
143 }
144 "The above word must call the following:"
145 { $subsection start-ui }
146 "The " { $link (with-ui) } " word must not return until the event loop has stopped and the UI has been shut down." ;
147
148 ARTICLE: "ui-backend-windows" "UI backend window management"
149 "The high-level " { $link open-window } " word eventually calls a low-level word which you must implement:"
150 { $subsection open-world-window }
151 "This word should create a native window, store some kind of handle in the " { $snippet "handle" } " slot, then call two words:"
152 { $subsection register-window }
153 "The following words must also be implemented:"
154 { $subsection set-title }
155 { $subsection raise-window }
156 "When a world needs to be redrawn, the UI will call a word automatically:"
157 { $subsection draw-world }
158 "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:"
159 { $subsection select-gl-context }
160 { $subsection flush-gl-context }
161 "If the user clicks the window's close box, you must call the following word:"
162 { $subsection close-window } ;
163
164 ARTICLE: "ui-layouts" "Gadget hierarchy and layouts"
165 "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."
166 { $subsection "ui-layout-basics" }
167 "Common layout gadgets:"
168 { $subsection "ui-pack-layout" }
169 { $subsection "ui-track-layout" }
170 { $subsection "ui-grid-layout" }
171 { $subsection "ui-frame-layout" }
172 { $subsection "ui-book-layout" }
173 "Advanced topics:"
174 { $subsection "ui-null-layout" }
175 { $subsection "ui-incremental-layout" }
176 { $subsection "ui-layout-impl" }
177 { $see-also "ui.gadgets.borders" } ;
178
179 ARTICLE: "ui-layout-basics" "Layout basics"
180 "Gadgets are arranged in a hierarchy, and all visible gadgets except for instances of " { $link world } " are contained in a parent gadget."
181 $nl
182 "Managing the gadget hierarchy:"
183 { $subsection add-gadget }
184 { $subsection unparent }
185 { $subsection add-gadgets }
186 { $subsection clear-gadget }
187 "The children of a gadget are available via the "
188 { $snippet "children" } " slot. "
189 $nl
190 "Working with gadget children:"
191 { $subsection gadget-child }
192 { $subsection nth-gadget }
193 { $subsection each-child }
194 { $subsection child? }
195 "Working with gadget parents:"
196 { $subsection parents }
197 { $subsection each-parent }
198 { $subsection find-parent }
199 "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."
200 { $subsection relayout }
201 { $subsection relayout-1 }
202 "Gadgets implement a generic word to inform their parents of their preferred size:"
203 { $subsection pref-dim* }
204 "To get a gadget's preferred size, do not call the above word, instead use " { $link pref-dim  } ", which caches the result." ;
205
206 ARTICLE: "ui-null-layout" "Manual layouts"
207 "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." ;
208
209 ARTICLE: "ui-layout-impl" "Implementing layout gadgets"
210 "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:"
211 { $subsection layout* }
212 "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
213 "Some assorted utility words which are useful for implementing layout logic:"
214 { $subsection pref-dim }
215 { $subsection pref-dims }
216 { $subsection prefer }
217 { $subsection max-dim }
218 { $subsection dim-sum }
219 { $warning
220     "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."
221     $nl
222     "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."
223 } ;
224
225 ARTICLE: "new-gadgets" "Implementing new gadgets"
226 "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."
227 $nl
228 "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" } "):"
229 { $subsection <gadget> }
230 "New gadgets are defined as subclasses of an existing gadget type, perhaps even " { $link gadget } " itself. A parametrized constructor should be used to construct subclasses:"
231 { $subsection new-gadget }
232 "Further topics:"
233 { $subsection "ui-gestures" }
234 { $subsection "ui-paint" }
235 { $subsection "ui-control-impl" }
236 { $subsection "clipboard-protocol" }
237 { $see-also "ui-layout-impl" } ;
238
239 ARTICLE: "starting-ui" "Starting the UI"
240 "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:"
241 { $subsection with-ui } ;
242
243 ARTICLE: "ui" "UI framework"
244 "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."
245 { $subsection "starting-ui" }
246 { $subsection "ui-glossary" }
247 { $subsection "building-ui" }
248 { $subsection "new-gadgets" }
249 { $subsection "ui-backend" } ;
250
251 ABOUT: "ui"