![]() |
Extending the graphics editor and pitfalls | ![]() |
The simplest way to add a new kind of object is a graphical macro: a macro whose arguments are points (and possibly labels) and whose expansion is graphical markup. The editor creates such objects exactly like curves: the user clicks the successive points, which become the arguments of the macro. Two ingredients are needed.
The macro must be defined in a style file or package. It can be written in pure markup, as rectangle in
packages/standard/std-graphics.ts
:
<assign|rectangle|<macro|p1|p2|<cline|p1|<point|<look-up|p1|0>|<look-up|p2|1>>|p2|<point|<look-up|p2|0>|<look-up|p1|1>>>>> |
or delegate the computation to Scheme through extern, as arrow-with-text in the same file:
<assign|arrow-with-text|<macro|p1|p2|t|<extern|arrow-with-text|p1|p2|<quote-arg|t>>>> <drd-props|arrow-with-text|arity|3|accessible|2> |
The drd-props declaration makes only the text argument accessible, so that the cursor can enter the label but not the points. The package
packages/experimental/graphical-macros.ts
contains more examples which use extern for all their computations.
The editor only offers tags which are listed in gr-tags-user (
graphics/graphics-drd.scm
). This list is extended by the macro define-graphics of
graphics/graphics-markup.scm
, which also defines the Scheme function used by extern and declares it secure:
(tm-define-macro (define-graphics head . l)
(receive (opts body) (list-break l not-define-option?)
‘(begin
(set! gr-tags-user (cons ',(ca*r head) gr-tags-user))
(tm-define ,head ,@opts (:secure #t) ,@body)))) |
The functions receive their arguments as strees; points are (point x y) with string coordinates, and missing arguments must be tolerated, because the object is typeset while it is being created. The module provides helpers for this: tm-point?, point->complex, complex->point and graphics-transform. For instance:
(define-graphics (rectangle P1 P2)
(let* ((p1 (if (tm-point? P1) P1 '(point "0" "0")))
(p2 (if (tm-point? P2) P2 p1)))
‘(cline ,p1 (point ,(tm-x p2) ,(tm-y p1))
,p2 (point ,(tm-x p1) ,(tm-y p2))))) |
Once the tag is in gr-tags-user and the current style defines it (style-has?), the Insert menu of graphics mode (graphics-mode-menu) offers it, and selecting it sets the mode to (edit tag). object_create then creates (tag (point x y) (point x y)) and the usual point insertion takes over. User tags receive the attributes of curves (graphics-attributes).
By default the object is complete when it has the maximal arity declared in the DRD, and it may be committed as soon as it has the minimal arity. If the macro has non-point arguments (labels), the predicates graphics-incomplete? and graphics-complete? and the function graphics-complete should be overloaded; see triangle-with-text and arrow-with-text, whose graphics-complete appends a default label.
Adding the tag to the groups graphical-contains-curve-tag or graphical-contains-text-tag (with define-group) influences the handling of clicks on labels in edit_left-button.
Structured editing commands can be overloaded for the new tag, as is done for kbd-remove on arrow-with-text.
gr-tags-user is only filled when the module containing the define-graphics forms is loaded.
graphics-markup.scm
is loaded lazily (
init-texmacs.scm
declares arrow-with-text and arrow-with-text* with lazy-define and define-secure-symbols), and the menu entries call (import-from (graphics graphics-markup)). A new module of graphical macros must similarly be loaded before its macros are offered in the menus. Since extern only calls secure functions, the functions of a lazily loaded module should also be declared with define-secure-symbols, as is done for arrow-with-text.
A new built-in graphical tag requires changes at all levels. Taking a hypothetical closed curve as an example:
Tree label. Add the label to the enumeration in
Kernel/Types/tree_label.hpp
(next to CSPLINE) and declare it in
Data/Drd/drd_std.cpp
with returns_graphical () and point_type for the point arguments, and an arity (repeat, fixed) which the editor will use to decide when the object is complete.
Typesetting. Add a case to the dispatch in
Typeset/Concat/concater.cpp
and a method in
concat_graphics.cpp
. Follow typeset_line: evaluate the points with env->as_point (env->exec (t[i])), keep the paths descend (ip, i) of the points in a cip array, build a curve in graphical coordinates, map it with env->fr, apply adjust_extremities and produce a curve_box. Use BEGIN_MAGNIFY and END_MAGNIFY. If you write a new box type instead, implement graphical_select so that it returns selections whose cp paths point to the point subtrees; otherwise the editor cannot select individual points.
Other C++ code. Some functions enumerate the graphical tags explicitly: is_graphical in
Edit/Interface/edit_interface.cpp
, complete in
Typeset/Env/env_animate.cpp
(closed curves in animations), and the upgrader in
Data/Convert/Texmacs/upgradetm.cpp
.
Tag groups. Add the tag to the appropriate group in
graphics/graphics-drd.scm
, for instance graphical-closed-curve-tag. This automatically puts it into gr-tags-all and gr-tags-curves, gives it the curve attributes, and makes object_create treat it as a curve.
Interface. Add entries to graphics-mode-menu and to graphics-insert-icons, and a name in gr-mode->string (
graphics/graphics-menu.scm
).
Documentation. Describe the tag in graphics primitives.
A new editing mode, rather than a new object type, is added by overloading the functions edit_move, edit_left-button, edit_right-button, edit_middle-button, edit_start-drag, edit_drag and edit_end-drag with a :require clause on the mode symbol and the option (:state graphics-state), as
graphics/graphics-single.scm
does for 'hand-edit, and by making graphics-mode-attributes, graphics-finish and gr-mode->string aware of it.
Changing gr-color does not change the color of anything that is already drawn; it changes the color of the next object. Conversely, setting color around a graphics changes all objects without an explicit color, but not the properties offered for new objects.
All coordinates exchanged between C++ and Scheme are strings. The abbreviations f2s and s2f (
graphics/graphics-utils.scm
) are used everywhere for the conversions.
The variables current-x, current-path, current-obj, … are recomputed only when a function with the option (:state graphics-state) is entered. A helper called from elsewhere (a menu command, a keyboard shortcut) sees the values of the last mouse event, which may refer to an object that has been modified or removed since. Moreover, the state is global: it is not attached to a buffer or a picture, which is why graphics-reset-context must be called when the cursor enters or leaves a picture.
During an operation, the objects being edited are not in the document but in the sketch, as detached strees. Code which inspects the document at that moment, or which saves it, does not see them. Conversely, in selecting state the sketch contains live trees, which become invalid if they are removed by other means.
The removal of objects at checkout is a real modification of the document. If an operation ends in any other way than sketch-commit or graphics-reset-context, the objects are lost until the user performs an undo. New code which starts an operation must set graphics-undo-enabled and remove-undo-mark? consistently, as sketch-checkout does.
The graphical object is typeset in the environment of the editor, not of the picture; only the frame is borrowed. Attributes inherited from with tags around the picture are therefore not applied automatically, which is why create-graphical-props puts all attributes explicitly.
Scheme only receives snapped coordinates, and the selection of the object under the mouse is done at the snapped position. When a snapping mode makes the mouse jump to a grid point, the object under the original position may not be found. The snapping category text used for labels and groups by can_snap is not in the list graphics-snap-types shown in the menus, so it can only be enabled through all.
Inside a text-at (more precisely, when inside_graphics (true) is false but inside_graphics (false) is true) the editor behaves as a text editor, except that the release of the left mouse button is still passed to graphics-release-left, which positions the cursor inside the label.
When gr-auto-crop is true, the extents and the origin are determined by the contents, and graphics-move-origin and graphics-change-extents do nothing.
The defaults in attribute-default-table are supposed to coincide with those of initialize_default_env. They currently differ for line-effects (normal in Scheme, none in C++).
Messages like Uncaptured graphical move or Uncaptured reset-context come from the default implementations of the dispatch functions and usually indicate a mode for which a handler is missing.
The following problems were noticed while writing this documentation.
graphical-get-selected-attributes (
graphics/graphics-utils.scm
) calls itself instead of graphical-get-selected-attributes*, and would loop forever; it is currently not used.
In hand drawing mode, a simple click (edit_left-button for 'hand-edit in
graphics/graphics-single.scm
) creates a point inside (with "point style" "disk" …): the variable name contains a space instead of a hyphen, so the style is not applied.
typeset_gr_transform and typeset_gr_effect (
Typeset/Concat/concat_graphics.cpp
) call typeset_error on a wrong arity but then continue, accessing children which may not exist.
The second component returned by graphics-complete, apparently meant as a cursor position inside the completed object, is ignored by object_commit.
The method edit_graphics_rep::find_point and the field cur_pos are unused.