GNU TeXmacsUser manualReferenceAboutEnglish
The C++ side of the graphics editor

1.The class edit_graphics_rep

The editor (editor_rep, see the chapter on the architecture) is assembled from several components by virtual inheritance. The graphics component is edit_graphics_rep, declared in

Edit/Interface/edit_graphics.hpp

. Its public methods are also declared as pure virtual functions in

Edit/editor.hpp

, so that the other components and the glue can call them. Its state is small:

class edit_graphics_rep: virtual public editor_rep {
private:
  box go_box;           // The graphical object typesetted as a box
  double p_x, p_y;      // Last unadjusted (x, y) position
  double gr_x, gr_y;    // Last (x, y) position of the mouse
  gr_selections gs;     // Last graphical_select (x, y)
  grid gr0;             // Last grid
protected:
  point cur_pos;
  tree graphical_object;
…
};

Besides these fields, the snapping parameters are kept in the static variables snap_mode and snap_distance of

edit_graphics.cpp

, set by set_snap_mode and set_snap_distance. The field cur_pos is not used.

1.1.Where am I?

bool inside_graphics (bool b= true)
(is the cursor in a picture?)

Walks the cursor path tp down from the root of the document and returns whether a graphics tag is crossed. With b true (the default), entering a text-at, math-at or document-at (is_graphical_text) cancels the flag: when the cursor is inside a label, the editor is in text mode and keyboard events are handled as usual. The glued Scheme function is in-graphics?.

bool inside_active_graphics (bool b= true)
(in an editable picture?)

Same, but also requires preamble to be false, so that pictures shown as source code are not edited graphically.

path graphics_path ()
(path of the picture)
tree get_graphics ()
(the picture itself)

graphics_path returns the path of the first child of the innermost graphics above the cursor (or the cursor path if there is none); get_graphics returns the innermost graphics subtree.

frame find_frame (bool last= false)
(frame of the picture)
grid find_grid ()
(edit grid of the picture)
void find_limits (point& lim1, point& lim2)
(visible region)

These locate the box of the picture with eb->find_box_path and call the corresponding box_rep methods (see graphical markup and its typesetting). They return nil objects when the box cannot be found, for instance when the picture is not typeset yet. find_graphical_region converts the limits into a rectangle in document coordinates.

bool over_graphics (SI x, SI y)
(is the mouse over the picture?)

True if the point lies within the limits of the picture. Outside these limits, it returns the value of the Scheme predicate graphics-busy?, which is true while an object is being created or moved: one can therefore drag a point outside the picture.

double get_x (), double get_y (), double get_pixel ()
(current position and pixel size)

The last (snapped) mouse position in graphical coordinates, and the size of a screen pixel in graphical units. Glued as get-graphical-x, get-graphical-y and get-graphical-pixel.

The method find_point only builds a point tree from a point and is not used anywhere.

2.From a mouse click to a tree modification

2.1.The path of an event

A mouse event follows this route:

  1. The GUI back-end calls edit_interface_rep::handle_mouse (

    Edit/Interface/edit_mouse.cpp

    ). It detects drags (detect_left_drag, detect_right_drag), which turns raw presses and motions into the event types start-drag-left, dragging-left, end-drag-left, and passes the event to the Scheme function mouse-event.

  2. Unless it is overridden (by tooltips, comments and the like), mouse-event (

    kernel/gui/kbd-handlers.scm

    ) calls mouse-any, glued to edit_interface_rep::mouse_any.

  3. mouse_any performs the generic work (pointer, popups, gestures) and then the graphics specific dispatch:

    if (inside_graphics (type != "release-left")) {
      path gp= search_upwards (GRAPHICS);
      if (!is_nil (gp) && gp != previous_gp) {
        eval ("(graphics-exit-right)");
        mouse_any (type, x, y, mods, t, data);
        previous_gp= gp;
        return;
      }
    }
    if (inside_graphics (type != "release-left")) {
      if (mouse_graphics (type, x, y, mods, t, data)) return;
      if (!over_graphics (x, y))
        eval ("(graphics-reset-context 'text-cursor)");
    }

    The first time an event arrives for a picture which differs from the previous one (previous_gp), the cursor is moved behind the picture by graphics-exit-right and the event is dispatched again, now as an ordinary text event. The ordinary click handler (mouse_select) then puts the cursor into the picture and calls (graphics-reset-context 'begin), which initializes the Scheme state. The argument type != "release-left" means that a left click inside a label is still handled graphically, so that one can click from a label onto another object.

  4. edit_graphics_rep::mouse_graphics converts the position to graphical coordinates, snaps it and calls a Scheme handler:

    point p = f [point (x, y)];
    graphical_select (p[0], p[1]); // init the caching for adjust().
    p= adjust (p);
    gr_x= p[0];
    gr_y= p[1];
    string sx= as_string (p[0]);
    string sy= as_string (p[1]);
    invalidate_graphical_object ();
    double pressure= (N(data) == 0? 1.0: data[0]);
    call ("set-keyboard-modifiers", object (m));
    if (type == "move")
      call ("graphics-move", sx, sy);
    else if (type == "release-left" || type == "double-left")
      call ("graphics-release-left", sx, sy, (double) t, 1);
    …
    invalidate_graphical_object ();
    notify_change (THE_CURSOR);
    return true;

    The coordinates are passed to Scheme as strings. Events of type move and dragging-left are dropped when more motion events are pending (check_event (MOTION_EVENT)), and wheel events are converted into a relative displacement and sent to graphics-wheel, which scrolls the picture.

  5. The Scheme handlers in

    graphics/graphics-edit.scm

    (graphics-move, graphics-release-left, graphics-start-drag-left, …) dispatch on the current mode to the functions edit_move, edit_left-button, … described in the next chapter. These modify the document with the ordinary tree functions (tree-insert, tree-assign, tree-remove, …) and set the graphical object.

  6. At the next repaint, the modified document is retypeset, and edit_interface_rep::draw_graphics (

    Edit/Interface/edit_repaint.cpp

    ) draws the graphical object and the graphical cursor.

C++ event type Scheme handler
move (graphics-move x y)
release-left, double-left (graphics-release-left x y t p)
release-middle (graphics-release-middle x y)
release-right, double-right (graphics-release-right x y)
start-drag-left, dragging-left, end-drag-left (graphics-start-drag-left x y t p) etc.
start-drag-right, dragging-right, end-drag-right (graphics-start-drag-right x y) etc.
drop-object (graphics-drop-object x y)
wheel (graphics-wheel dx dy)

The left button handlers also receive the time and the pressure (from the event data; 1 for clicks), which are recorded in hand drawings. The keyboard modifiers are stored on the Scheme side by set-keyboard-modifiers and read back with get-keyboard-modifiers; the handlers test them against ShiftMask. A drop of an external object first calls the Scheme hook mouse-drop-event, which in graphics mode only memorizes the object in the-graphics-drop-object, and then mouse_graphics ("drop-object", …), which inserts it as a text-at.

2.2.Other hooks from the generic editor

Entering and leaving a picture

mouse_select calls (graphics-reset-context 'begin) when a click brings the cursor into a picture, and invalidates the graphical object and calls (graphics-reset-context 'exit) when it leaves it (or moves to another picture).

Repainting

draw_graphics calls (graphics-reset-context 'graphics-cursor) or (graphics-reset-context 'text-cursor) depending on whether the mouse is over an active picture, draws the graphical object, and draws a red cross (or a cross with arrows) at the mouse position depending on the Scheme variable graphics-texmacs-pointer. The editor cursor itself is replaced by the snapped mouse position in edit_interface_rep::get_cursor.

Applying changes

edit_interface_rep::apply_changes invalidates the whole region of an active picture when it changed, and reads the snapping parameters from Scheme (graphics-get-snap-mode, graphics-get-snap-distance) into snap_mode and snap_distance.

Undo

edit_modify_rep::undo (

Edit/Modify/edit_modify.cpp

) consults the Scheme variable graphics-undo-enabled. While an object is being created or moved this variable is false, and undo only calls (graphics-reset-context 'undo), which cancels the current operation. Otherwise the undo is performed and (graphics-reset-context 'undo) resynchronizes the Scheme state.

Clipboard

selection_copy, selection_paste and selection_cut (

Edit/Replace/edit_select.cpp

) call graphics-copy, graphics-paste and graphics-cut when the cursor is in an active picture.

Deletion

back_in_text_at is called by the deletion routines (

Edit/Modify/edit_delete.cpp

) when backspace or delete is pressed at the start of an empty label: the text-at (and its surrounding with) is removed from the picture.

Cursor correction

edit_typeset_rep::typeset_exec_until moves the cursor to the closest accessible position when it is inside a picture but not at an accessible place.

3.Snapping

3.1.The algorithm

Snapping is done in C++ by edit_graphics_rep::adjust, before Scheme sees the coordinates. In mouse_graphics the call to graphical_select stores in gs all parts of the picture within snap_distance of the mouse (as returned by the boxes), and adjust then proceeds as follows:

  1. The candidate list starts with gs. If the edit grid is not empty, the nearest grid point (find_point_around) is added as a grid-point candidate and the nearby grid lines (get_curves_around) are added as grid-curve-point candidates, each with its curve, provided they are within the snap distance. All distances are measured in the local coordinates of the graphics box (find_frame (true)).

  2. snap_to_guide sorts the candidates by distance and chooses:

    • a candidate without curve (a point, a text, a text handle, a group, …) is returned at once if no grid point was met before it;

    • otherwise all pairs of candidates with curves are intersected (intersection from

      Graphics/Types/curve.hpp

      , with a precision of a tenth of a pixel), except for pairs of two grid lines, pairs mixing a point type with a text border, and pairs involving a handle; the closest admissible intersection is taken if it is closer than the grid point;

    • else the closest candidate is used, or the mouse position itself (type free) if snapping to it is not allowed.

  3. The snapped point is converted back to graphical coordinates.

Whether a candidate is admissible is decided by can_snap, which maps the selection types onto the snapping categories shown in the Snap menu and checks them against snap_mode:

Selection type Category in snap_mode
point, curve-handle, text-handle control point
curve-point curve point
curve-point&curve-point curve-curve intersection
grid-point grid point
grid-curve-point grid curve point
curve-point&grid-curve-point (and reversed) curve-grid intersection
text, group text
text-border text border
text-border-point text border point
box never
free always

The snap mode is a tuple of category names; the category all allows everything, and a non-tuple value (no setting) also allows everything. On the Scheme side (

graphics/graphics-main.scm

) the mode is stored in the property gr-snap of the picture; graphics-get-snap-mode returns the empty tuple in hand drawing mode, which disables snapping for hand drawings. The snap distance is gr-snap-distance (default 10px).

3.2.Snapping and selection are separate

Note that the selection used for snapping is not the selection used by Scheme to decide which object is under the mouse. The latter is recomputed in Scheme (graphics-select, through the glued graphical-select) at the snapped position, with the same snapping distance. The editor therefore usually finds the object whose control point the mouse was snapped to.

4.The graphical object

The graphical object is a tree which is not part of the document but is typeset and drawn on top of it. The Scheme code uses it to show the control points of the object under the mouse, the selected objects in group mode, the selection rectangle, and above all the objects being edited: while an object is created or moved, it is removed from the document and only exists in the graphical object (see the sketch in the next chapter).

void set_graphical_object (tree t)
(set the overlay)

Stores t and typesets it immediately with typeset_as_concat, in the current typesetting environment but with the frame of the picture (find_frame ()). The result is a composite_box go_box with one child per non-empty box of the concatenation. Glued as set-graphical-object; the Scheme wrapper graphical-object! takes an stree.

tree get_graphical_object ()
(get the overlay)

Glued as get-graphical-object.

void invalidate_graphical_object ()
(schedule a redraw)

Invalidates the ink rectangles of the children of go_box, intersected with the region of the picture. It has to be called before the overlay is changed (to erase the old one) and after (to draw the new one); graphics-decorations-update and mouse_graphics do both. Glued as invalidate-graphical-object.

void draw_graphical_object (renderer ren)
(draw the overlay)

Called from draw_graphics. It retypesets the graphical object if go_box is nil, installs a clipping rectangle for the picture and draws the children of go_box: point and curve boxes with display, other boxes (texts) with redraw.

The overlay is typeset in the environment of the editor at the moment of the call, not in the environment of the picture: only the frame is taken from the picture. This is why the Scheme code wraps the overlay in an explicit with containing all relevant attributes (create-graphical-props) and the magnification (get-local-magnify).

5.Glue functions

The following functions are exported to Scheme in

Scheme/Glue/build-glue-editor.scm

and

build-glue-basic.scm

:

Scheme C++
in-graphics? inside_graphics
get-graphical-x, get-graphical-y get_x, get_y
get-graphical-pixel get_pixel
get-graphical-object, set-graphical-object get_graphical_object, set_graphical_object
invalidate-graphical-object invalidate_graphical_object
graphical-select graphical_select (double, double)
graphical-select-area graphical_select (double, double, double, double)
graphics-set, graphics-has?, graphics-ref set_graphical_value, has_graphical_value, get_graphical_value
graphics-needs-update?, graphics-notify-update graphics_needs_update, graphics_notify_update

Conversely, the C++ code calls the following Scheme functions and variables: graphics-move, graphics-release-left, graphics-release-middle, graphics-release-right, graphics-start-drag-left, graphics-dragging-left, graphics-end-drag-left, graphics-start-drag-right, graphics-dragging-right, graphics-end-drag-right, graphics-drop-object, graphics-wheel, set-keyboard-modifiers, graphics-busy?, graphics-reset-context, graphics-exit-right, graphics-texmacs-pointer, graphics-undo-enabled, graphics-get-snap-mode, graphics-get-snap-distance, graphics-copy, graphics-cut, graphics-paste and graphics-notify-extents. Most of them are declared with lazy-define in

init-texmacs.scm

, so that the graphics modules are only loaded when needed.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".