]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/ui-docs.factor
Solution to Project Euler problem 65
[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 { $subsection 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 { $subsection "ui-geometry" }
104 { $subsection "ui-layouts" }
105 { $subsection "gadgets" }
106 { $subsection "ui-windows" }
107 { $subsection "ui.gadgets.status-bar" }
108 { $see-also "models" } ;
109
110 ARTICLE: "gadgets" "Pre-made UI gadgets"
111 { $subsection "ui.gadgets.labels" }
112 { $subsection "ui.gadgets.borders" }
113 { $subsection "ui.gadgets.labeled" }
114 { $subsection "ui.gadgets.buttons" }
115 { $subsection "ui.gadgets.sliders" }
116 { $subsection "ui.gadgets.scrollers" }
117 { $subsection "ui.gadgets.editors" }
118 { $subsection "ui.gadgets.menus" }
119 { $subsection "ui.gadgets.panes" }
120 { $subsection "ui.gadgets.presentations" }
121 { $subsection "ui.gadgets.tables" } ;
122
123 ARTICLE: "ui-geometry" "Gadget geometry"
124 "The " { $link gadget } " class inherits from the " { $link rect } " class, and thus all gadgets have a bounding box:"
125 { $subsection "math.rectangles" }
126 "Word for converting from a child gadget's co-ordinate system to a parent's:"
127 { $subsection relative-loc }
128 { $subsection screen-loc }
129 "Hit testing:"
130 { $subsection pick-up }
131 { $subsection children-on } ;
132
133 ARTICLE: "ui-windows" "Top-level windows"
134 "Opening a top-level window:"
135 { $subsection open-window }
136 "Finding top-level windows:"
137 { $subsection find-window }
138 "Top-level windows are stored in a global variable:"
139 { $subsection windows }
140 "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:"
141 { $subsection graft* }
142 "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:"
143 { $subsection ungraft* }
144 "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:"
145 { $subsection world } ;
146
147 ARTICLE: "ui-backend" "Developing UI backends"
148 "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."
149 { $subsection "ui-backend-init" }
150 { $subsection "ui-backend-windows" }
151 "UI backends may implement the " { $link "clipboard-protocol" } "." ;
152
153 ARTICLE: "ui-backend-init" "UI initialization and the event loop"
154 "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:"
155 { $code
156     "IN: shells"
157     ""
158     ": ui"
159     "    ... backend-specific initialization ..."
160     "    start-ui"
161     "    ... more backend-specific initialization ..."
162     "    ... start event loop here ... ;"
163 }
164 "The above word must call the following:"
165 { $subsection start-ui }
166 "The " { $link (with-ui) } " word must not return until the event loop has stopped and the UI has been shut down." ;
167
168 ARTICLE: "ui-backend-windows" "UI backend window management"
169 "The high-level " { $link open-window } " word eventually calls a low-level word which you must implement:"
170 { $subsection open-world-window }
171 "This word should create a native window, store some kind of handle in the " { $snippet "handle" } " slot, then call two words:"
172 { $subsection register-window }
173 "The following words must also be implemented:"
174 { $subsection set-title }
175 { $subsection raise-window }
176 "When a world needs to be redrawn, the UI will call a word automatically:"
177 { $subsection draw-world }
178 "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:"
179 { $subsection select-gl-context }
180 { $subsection flush-gl-context }
181 "If the user clicks the window's close box, you must call the following word:"
182 { $subsection close-window } ;
183
184 ARTICLE: "ui-layouts" "Gadget hierarchy and layouts"
185 "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."
186 { $subsection "ui-layout-basics" }
187 "Common layout gadgets:"
188 { $subsection "ui-pack-layout" }
189 { $subsection "ui-track-layout" }
190 { $subsection "ui-grid-layout" }
191 { $subsection "ui-frame-layout" }
192 { $subsection "ui-book-layout" }
193 "Advanced topics:"
194 { $subsection "ui.gadgets.glass" }
195 { $subsection "ui-null-layout" }
196 { $subsection "ui-incremental-layout" }
197 { $subsection "ui-layout-impl" }
198 { $see-also "ui.gadgets.borders" } ;
199
200 ARTICLE: "ui-layout-basics" "Layout basics"
201 "Gadgets are arranged in a hierarchy, and all visible gadgets except for instances of " { $link world } " are contained in a parent gadget."
202 $nl
203 "Managing the gadget hierarchy:"
204 { $subsection add-gadget }
205 { $subsection unparent }
206 { $subsection add-gadgets }
207 { $subsection clear-gadget }
208 "The children of a gadget are available via the "
209 { $snippet "children" } " slot. "
210 $nl
211 "Working with gadget children:"
212 { $subsection gadget-child }
213 { $subsection nth-gadget }
214 { $subsection each-child }
215 { $subsection child? }
216 "Working with gadget parents:"
217 { $subsection parents }
218 { $subsection each-parent }
219 { $subsection find-parent }
220 "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."
221 { $subsection relayout }
222 { $subsection relayout-1 }
223 "Gadgets implement a generic word to inform their parents of their preferred size:"
224 { $subsection pref-dim* }
225 "To get a gadget's preferred size, do not call the above word, instead use " { $link pref-dim  } ", which caches the result." ;
226
227 ARTICLE: "ui-null-layout" "Manual layouts"
228 "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." ;
229
230 ARTICLE: "ui-layout-impl" "Implementing layout gadgets"
231 "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:"
232 { $subsection layout* }
233 "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
234 "Some assorted utility words which are useful for implementing layout logic:"
235 { $subsection pref-dim }
236 { $subsection pref-dims }
237 { $subsection prefer }
238 { $subsection max-dim }
239 { $subsection dim-sum }
240 { $warning
241     "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."
242     $nl
243     "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."
244 } ;
245
246 ARTICLE: "new-gadgets" "Implementing new gadgets"
247 "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."
248 $nl
249 "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" } "):"
250 { $subsection <gadget> }
251 "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" } ")."
252 $nl
253 "Further topics:"
254 { $subsection "ui-gestures" }
255 { $subsection "ui-paint" }
256 { $subsection "ui-control-impl" }
257 { $subsection "clipboard-protocol" }
258 { $subsection "ui.gadgets.line-support" }
259 { $see-also "ui-layout-impl" } ;
260
261 ARTICLE: "starting-ui" "Starting the UI"
262 "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:"
263 { $subsection with-ui } ;
264
265 ARTICLE: "ui" "UI framework"
266 "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."
267 { $subsection "starting-ui" }
268 { $subsection "ui-glossary" }
269 { $subsection "building-ui" }
270 { $subsection "new-gadgets" }
271 { $subsection "ui-backend" } ;
272
273 ABOUT: "ui"
274
275 HELP: close-button
276 { $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 } "." } ;
277
278 HELP: minimize-button
279 { $description "Asks for a minimize button to be available for a window." } ;
280
281 HELP: maximize-button
282 { $description "Asks for a maximize button to be available for a window." } ;
283
284 HELP: resize-handles
285 { $description "Asks for resize controls to be available for a window. Without resize controls, the window size will not be changeable by the user." } ;
286
287 HELP: small-title-bar
288 { $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." } ;
289
290 HELP: normal-title-bar
291 { $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." } ;
292
293 ARTICLE: "ui.gadgets.worlds-window-controls" "Window controls"
294 "The following window controls can be placed in a " { $link world } " window:"
295 { $subsection close-button }
296 { $subsection minimize-button }
297 { $subsection maximize-button }
298 { $subsection resize-handles }
299 { $subsection small-title-bar }
300 { $subsection normal-title-bar }
301 "Provide a sequence of these values in the " { $snippet "window-controls" } " slot of the " { $link world-attributes } " tuple you pass to " { $link open-window } "." ;