![]() |
The Scheme state machine | ![]() |
The Scheme part of the graphics editor consists of the modules in
src/TeXmacs/progs/graphics/
. At the bottom,
graphics-drd.scm
is used by
graphics-utils.scm
and
graphics-markup.scm
;
graphics-utils.scm
is used by
graphics-env.scm
,
graphics-object.scm
and
graphics-main.scm
; these three are used by
graphics-single.scm
, which is used by
graphics-group.scm
, itself used by
graphics-animate.scm
. The module
graphics-edit.scm
combines the single, group and animation modules, and
graphics-kbd.scm
and
graphics-menu.scm
are on top.
None of them is loaded at start-up.
init-texmacs.scm
declares the entry points with lazy-define (for instance graphics-reset-context, graphics-busy?, the mouse handlers of
graphics-edit.scm
, make-graphics), the keyboard map with lazy-keyboard (activated by the predicate in-active-graphics?) and the menus with lazy-menu. The first call of any of these loads the corresponding module and its dependencies.
The current mode is stored in the property gr-mode of the picture and read by (graphics-mode) (
graphics/graphics-main.scm
), which always returns a list of two symbols:
Point mode: create and modify objects of type tag, which is point, a curve tag (line, cline, spline, arc, carc, …), a text tag (text-at, math-at, document-at) or a user defined graphical macro.
Hand drawing with penscript or calligraphy.
Group mode: select several objects and apply move, zoom, rotate, group-ungroup, props or edit-props to them, or animate them.
A string value of gr-mode (as written by make-graphics) is interpreted as (edit value), and the built-in default of the variable is line. (graphics-set-mode mode) calls graphics-group-start (which clears the current operation and puts the cursor at the start of the picture), then graphics-enter-mode, which resets the state when switching between point mode and group mode, and finally stores the new mode. graphics-group-mode? tests for group mode. The menu entries of the Insert and Focus menus and the toolbar icons (
graphics/graphics-menu.scm
) are mostly calls to graphics-set-mode with the appropriate argument.
The state of the editor is declared in
graphics/graphics-env.scm
with define-state (
kernel/texmacs/tm-states.scm
):
(define-state graphics-state
(slots ((graphics-action #f)
(current-graphical-object #f)
(choosing #f)
(sticky-point #f)
(dragging-create? #f)
(dragging-busy? #f)
(leftclick-waiting #f)
(current-point-no #f)
(current-edge-sel? #f)
(current-selection #f)
(previous-selection #f)
(subsel-no #f)
(graphics-undo-enabled #t)
(remove-undo-mark? #f)
(the-sketch '())
…))
(props ((current-x (f2s (get-graphical-x)))
(current-y (f2s (get-graphical-y)))
(sel (if sticky-point
#f
(select-choose (s2f current-x) (s2f current-y))))
(pxy (if sel (car sel) '()))
(current-path (if sticky-point
(cDr (cursor-path))
(graphics-path pxy)))
(current-obj …)
(other-inits …)))) |
The slots are ordinary global variables which persist between events. The props are recomputed from the C++ state each time a function declared with the option (:state graphics-state) is entered: define-option-state wraps the body of such a function into with-state-by-name, which loads the slots and then evaluates all the props in order. The option (:state-slots graphics-state) loads the slots without recomputing the props. The props compute:
The snapped mouse position, as strings.
Unless an operation is in progress, the graphical selection under the mouse, chosen by select-choose. It is a list of one or two paths, as explained below.
The path of the object under the mouse (graphics-path of the first path of the selection). During an operation, it is the parent of the cursor path instead.
The object at current-path as an stree; during an operation, the object in the graphical object; in group mode, the dummy '(point).
The index of the selected control point in current-obj, and whether the mouse is on a curve segment (a selection with two paths) rather than on a control point.
The most important slot is sticky-point. When it is false, the editor is selecting: moving the mouse only changes the decorations showing which object would be affected. When it is true, the editor is modifying: some objects have been taken out of the document into the sketch, the mouse moves a point or the whole selection, and the props no longer depend on what is under the mouse. graphics-busy? simply returns sticky-point.
(graphics-select x y d) calls the glued graphical-select and post-processes the result with filter-graphical-select. Each element of the result is a list of one or two paths (from the cp field of the C++ selections) which point to point subtrees, or into other leaves of the selected object. Selections which do not lie in a picture (graphics-path returns #f) are removed. For objects which are not built-in graphical tags (user macros), C++ may return paths with negative components; these are cut and replaced by the index of the macro argument closest to the mouse (object-closest-point-pos). The source code calls this a hack.
(graphics-path p) (
graphics/graphics-utils.scm
) goes up from a path until it reaches a tree whose ancestors include a graphics without crossing a graphical text tag, and returns the path of the object, or #f. Hence for a path to the second point of a line, graphics-path returns the path of the line, and the last component of the original path is the point number.
select-choose remembers the list of candidates in current-selection. When several objects lie under the mouse, select-next (bound to the ⇥ key through kbd-variant, graphics-choose-point and edit_tab-key) increments subsel-no to cycle through them.
The handlers called by C++ (
graphics/graphics-edit.scm
) just dispatch on the first component of the mode, unless the cursor is inside a label (inside-graphical-text?), in which case the events are ignored, or a click in a label moves the cursor within the label:
(tm-define (graphics-move x y)
(when (not (inside-graphical-text?))
(edit_move (car (graphics-mode)) x y))) |
The functions edit_move, edit_left-button, edit_middle-button, edit_right-button, edit_start-drag, edit_drag, edit_end-drag and edit_tab-key are defined with tm-define and overloaded with :require clauses on the mode ('edit, 'hand-edit, 'group-edit) and on the current object. The default versions in
graphics/graphics-single.scm
print a message; the default drag handlers fall back to the button and move handlers. Note that graphics-start-drag-right and graphics-end-drag-right are mapped to a right click, and graphics-dragging-right to a move.
The sketch, stored in the slot the-sketch, is the list of objects the editor is working on (
graphics/graphics-object.scm
):
In selecting state, its elements are trees of the document: the selected objects in group mode. sketch-toggle, sketch-set!, sketch-reset and sketch-in? manage it. Objects are always stored by their root, including surrounding with and anim-edit tags (radical->enhanced-tree).
(sketch-checkout) starts an operation: every tree of the sketch is removed from the document (graphics-remove with 'memoize-layer, which remembers its position among the children of the graphics) and replaced in the sketch by a detached copy. It sets sticky-point and clears graphics-undo-enabled.
During the operation, the sketch is modified freely (by sketch-transform in group mode, or by direct mutation of the stree in point mode), and only the graphical object is updated.
(sketch-commit) ends the operation: the objects are inserted back into the picture at their former positions (graphics-group-insert-bis), the sketch now refers to the newly inserted trees, sticky-point is cleared, undo is enabled again, and if objects were removed at checkout, remove-undo-mark merges the removal and the insertion into a single undo step.
Hence a document never contains an object in a half edited state: while the user drags a point, the object exists only in the sketch and in the graphical object.
(graphics-decorations-update) recomputes the graphical object from the state, and (graphics-decorations-reset) clears it. The work is done by create-graphical-object, whose arguments are the object, the source of its properties, the kind of decoration and the selected point:
mode says where the attributes of the drawn object are taken from: 'active (the attributes of the object being edited, cached in the hash table graphical-attrs by graphical-fetch-props), 'new (the gr- properties of the picture), 'default or a path (the attributes in effect at that path; see get-graphical-prop). pts is 'points (only the control points), 'object or 'object-and-points. no is the selected point number or (edge no); the values 'group and 'no-group force or forbid the group mode rendering, in which the contours of all objects of the sketch are drawn by create-graphical-contours.
The control points are produced by create-graphical-contour, which draws the points of the object with the selected one as a square; text objects and groups get a rectangle around their box, computed with box-info (create-graphical-embedding-box). Curves with more than 50 points (hand drawings) only show every other point, recursively (compress). The colors are default-color-go-points and default-color-selected-points.
The functions which implement point mode are in
graphics/graphics-single.scm
. They fall into two layers: the basic operations (object_create, object_set-point, object_add-point, object_remove-point, object_checkout, object_commit), which act on the sketch, and the edit operations (move-over, start-move, move-point, next-point, last-point, remove-point), which implement the transitions of the automaton and print the help messages in the footer.
First click. With nothing under the mouse, edit_left-button calls edit-insert, which calls (object_create tag x y). For a curve this creates (tag (point x y) (point x y)), adds the attributes of new objects with graphics-enrich, saves the initial state ((graphics-store-state 'start-create)), sets current-point-no to 1 and checks the object out into the sketch. The object is not inserted in the document.
Moving. With sticky-point set, edit_move calls move-point, which moves point number current-point-no to the mouse (object_set-point) and updates the overlay: the user sees a rubber band segment.
Next click. next-point checks whether the mouse moved since the previous click (hardly-moved?, with a tolerance of 5 pixels). If it did, leftclick-waiting is set. The next motion then calls object_add-point, which pushes a state (for undo) and inserts a new point after the current one, unless the object is complete (graphics-complete?, which compares the arity with the maximal arity from the DRD; an arc stops at three points).
Finishing. A second click at the same place (that is, with leftclick-waiting set and the mouse hardly moved) calls last-point and object_commit. If the object is not incomplete (graphics-incomplete?), graphics-complete may add missing arguments, the attributes are attached again with graphics-enrich-bis, animation wrappers are restored (graphics-re-enhance) and sketch-commit inserts the object into the picture. A delayed call to graphics-update-constraints follows.
Cancelling. A right or middle click (graphics-delete) pops the last state with graphics-back-state, which removes the last point. When only the initial state is left, it calls (undo 0); since undo is disabled during the operation, the C++ undo calls (graphics-reset-context 'undo), which restores the state saved by graphics-store-first and drops the object.
Dragging instead of clicking (edit_start-drag without object under the mouse) also creates an object; releasing the button (edit_end-drag) then only moves the current point, so that the creation continues with clicks.
When the mouse is over an object in selecting state, edit_move calls move-over, which shows the control points and moves the cursor into the object so that the focus menus and the footer refer to it. A drag starting on the object (edit_start-drag) calls start-move:
the state is stored with action 'start-move;
object_checkout removes the object from the document into the sketch;
if the mouse was on a segment rather than on a control point (current-edge-sel?), a new point is inserted on that segment;
the following edit_drag events move the point, and edit_end-drag calls last-point, which commits.
A right or middle click on an object calls remove-point, which removes the control point under the mouse, or the whole object if it would become degenerate (graphics-minimal?), if it is not a built-in graphical tag, or if shift is pressed.
Objects without interactive construction are inserted directly. For a point and for the text tags, object_create calls object-set! with the option 'new, which inserts the enriched object into the picture with graphics-group-enrich-insert; for text, the cursor is placed inside the new label so that the user can type immediately. A left click on an existing label in a text mode moves the cursor into it.
In hand drawing mode ('hand-edit), edit_start-drag creates a penscript or calligraphy object with an ink-meta tag and the first sample, and checks it out; edit_drag appends a sample (tuple x y t p) (and moves the end point); edit_end-drag commits. In calligraphy mode a simple click is converted into a tiny drag by graphics-release-left, while in penscript mode it inserts a point. Snapping is disabled in this mode (see graphics-get-snap-mode).
Group mode is implemented in
graphics/graphics-group.scm
.
A right click calls toggle-select, which toggles the object under the mouse in the sketch. A right click on an empty place unselects everything if there is a selection, and otherwise starts a rectangular selection: multiselecting is set, the corner is stored in selecting-x0 and selecting-y0, edit_move draws a red rectangle as graphical object, and the next right click selects all objects returned by graphics-select-area. In the modes (group-edit edit-props) and (group-edit animate), the left button selects instead. A middle click unselects everything, or with shift deletes the selection (remove-selected-objects).
A left click calls start-operation. If nothing is selected, the object under the mouse is selected first. Then the barycenter of all points is computed (store-important-points), the sketch is checked out and converted to strees, and a copy is kept in group-first-go. During the operation, edit_move applies group-translate (incrementally), or group-zoom or group-rotate (always from group-first-go, using the angle or distance ratio with respect to the barycenter). These transformations act on all point subtrees (traverse-transform); zooming also multiplies the magnify attribute. The next left click calls start-operation again, which commits.
group-selected-objects replaces the selection by a single gr-group and ungroup-current-object does the converse; both are invoked from start-operation in the mode (group-edit group-ungroup).
In mode (group-edit props), clicking on an object replaces its attributes by the current gr- properties (graphics-assign-props). In mode (group-edit edit-props), graphics-get-property and graphics-set-property are overloaded (with former as fallback), so that the property menus show and modify the attributes of the selected objects directly; a value which differs between objects is shown as mixed.
graphics-copy returns the selected objects wrapped in a graphics tree, graphics-cut also removes them, and graphics-paste inserts the children of a pasted graphics and selects them. These only act in group mode.
Animation editing ((group-edit animate),
graphics/graphics-animate.scm
) builds on group mode and on the animation editor in
dynamic/animate-edit.scm
.
Because objects are removed from the document while they are edited, the interaction with undo is delicate. Three mechanisms cooperate:
The first state. (graphics-store-state action) with a non-false action saves the complete state in graphics-first-state, tagged with the action ('start-create, 'start-move, 'start-operation, …). graphics-state-get and graphics-state-set convert between the global variables and a vector.
The state stack. (graphics-store-state #f) pushes the current state on graphics-states; graphics-back-state pops it (used to remove the last added point).
graphics-reset-context. This function is called by C++ with the argument 'begin and 'exit (entering and leaving a picture), 'undo (after or instead of an undo), 'text-cursor and 'graphics-cursor (updating the mouse pointer). On 'exit during an operation started with 'start-move, it performs an unredoable-undo to put the removed object back. On 'undo during an operation, it either undoes the removal (for 'start-move and 'start-operation) or simply forgets the sketch and returns to the first state. In all cases it ends with graphics-group-start.
The C++ undo routine only performs a real undo when graphics-undo-enabled is true, i.e. when no operation is in progress.
The default properties of new objects are the gr- variables of the picture (
graphics/graphics-utils.scm
):
Looks for var in the with tags above the innermost graphics (get-upwards-tree-property), or returns its initial value in the document. The result is an stree.
Inserts or updates var in the with around the picture (path-insert-with), or removes it (path-remove-with) when val is default or equals graphics-attribute-default. All the menu commands like graphics-set-color, graphics-set-line-width, graphics-set-arrow-end, graphics-set-unit, graphics-set-extents or graphics-set-snap are implemented this way (
graphics/graphics-main.scm
).
Reads all gr- properties, keeps those which are relevant for the tag of obj (graphical-relevant-attributes) and do not have their default value, and wraps obj into a with setting the corresponding unprefixed variables.
The keys ↩ and ⇧↩ copy the gr- properties to the object under the mouse (graphics-apply-props-at-mouse) and conversely (graphics-get-props-at-mouse). For a label which contains the cursor, the functions object-set-property and the commands like object-set-fill-color and object-set-text-at-halign at the end of
graphics/graphics-main.scm
modify the attributes of the label itself instead.
graphics/graphics-kbd.scm
defines a keymap active in in-active-graphics?: zooming (+, -, digits), moving the origin (arrow keys), changing the size (⌥←, …), z-order (↖, ↘, ⇞, ⇟, implemented by graphics-zmove in
graphics/graphics-edit.scm
), grids (#, ⌃G), deletion (⌫, ⌦ through graphics-kbd-remove) and 3D rotations. It overrides keyboard-press so that only these keys are interpreted in a picture (other keys are ignored unless they contain a modifier), and overloads the structured editing commands (kbd-variant, kbd-horizontal, geometry-vertical, …) for pictures and labels. Pinch gestures and the mouse wheel zoom and scroll the picture.
graphics/graphics-menu.scm
defines the Insert and Focus menus of graphics mode (graphics-insert-menu, graphics-focus-menu) and the toolbars (graphics-icons, graphics-focus-icons), which are linked from
texmacs/menus/main-menu.scm
when in-graphics? holds. The focus menu only shows the property submenus which make sense for the current mode (graphics-mode-attribute?). The check marks are obtained with the :check-mark option of the setters, together with predicates like graphics-test-property?.