Algorithmic Solutions > LEDA > LEDA Guide > GraphWin > Customizing the Interactive Interface > Defining Edit Actions

Defining GraphWin Edit Actions

What is an Edit Action?

Mouse operations in the display region of a GraphWin generate events (see also window events). An event is characterized by its event bit mask event_mask and the current position mouse_position of the mouse pointer. Event masks have associated edit actions. All edit actions are functions of type
    void action(GraphWin& gw, const point& pos);
When an event occurs, the associated action function is called with the GraphWin object and the current mouse_position as arguments. The object, node or edge, under the current position can be queried by get_edit_node() or get_edit_edge().

Event Masks

Event_masks are the bitwise-or of some of the following predefined constants.
  • A_LEFT, A_MIDDLE, A_RIGHT: If one of these bits is one, the corresponding mouse button has been clicked.
  • A_DRAG: This bit indicates that the mouse is moved with one or more buttons (specified by the bits discussed above) held down.
  • A_DOUBLE: This bit indicates a double click, i.e., the event that a mouse button has been clicked twice.
  • A_SHIFT, A_CTRL, A_ALT: If one of these bits is set, the corresponding keyboard control key is pressed.
  • A_NODE: If this bit is one, the mouse pointer is located over a node and the node can be queried by gw.get_edit_node().
  • A_EDGE: If this bit is one, the mouse pointer is located over an edge and the edge can be queried by gw.get_edit_edge().
  • A_SLIDER: If this bit is set, the mouse pointer is located over a slider of an edge. The corresponding edge can be queried by gw.get_edit_edge() and the number of the slider (0,1, or 2) can be obtained by gw.get_edit_slider().
An event mask is defined by a combination of these bits, for instance,
    (A_LEFT|A_NODE|A_DOUBLE)
describes a double click of the left mouse button on a node.

Setting Edit Actions

The following operations can be used to change the action functions associated with events.
    gw_action gw.set_action(long mask, 
                  void (*func)(GraphWin&, const point&));
sets the action on condition mask to func and returns the previous action of this condition. After this call func is called with the GraphWin object and the current edit position as arguments whenever the condition defined by mask becomes true.
    void gw.reset_actions();
resets all actions to their default values and
    void gw.clear_actions();
sets all actions to NULL.

Example

The following program redefines some of the default actions. Node and edge colors can be changed by holding down Ctrl and clicking on the node edge with the left mouse button. Holding down Shift and clicking on a node with the left mouse button centers the node at the position of the mous pointer. Holding down Ctrl and clicking on a node with the right mouse button deletes the node.Holding down Ctrl and clicking on the background with the left (right) mouse button, zooms the graph up (down).

#include <LEDA/graphics/graphwin.h>

using namespace leda;

//definition of new edit functions
void change_node_color(GraphWin& gw, const point&)
{
  node v=gw.get_edit_node();
  int col=(gw.get_color(v)+1)%16;
  gw.set_color(v,color(col));
}

void change_edge_color(GraphWin& gw, const point&)
{	
  edge e=gw.get_edit_edge();
  int col=(gw.get_color(e)+1)%16;
  gw.set_color(e,color(col));
}

void center_node(GraphWin& gw, const point& p)
{
  node v=gw.get_edit_node();
  gw.set_position(v,p);
}

void delete_node(GraphWin& gw, const point&)
{
  std::cout << "delete node" << std::endl;
  node v=gw.get_edit_node();
  gw.del_node(v);
}

void zoom_up(GraphWin& gw, const point&) {gw.zoom(1.5);}
void zoom_down(GraphWin& gw, const point&) {gw.zoom(0.5);}

//setting edit actions and start GraphWin
int main()
{
  GraphWin gw;

  gw.set_action(A_LEFT|A_NODE|A_CTRL, change_node_color);
  gw.set_action(A_LEFT|A_EDGE|A_CTRL, change_edge_color);
  gw.set_action(A_LEFT|A_NODE|A_SHIFT, center_node);
  gw.set_action(A_RIGHT|A_NODE|A_CTRL, delete_node);
  gw.set_action(A_LEFT|A_CTRL, zoom_up);
  gw.set_action(A_RIGHT|A_CTRL, zoom_down);

  gw.display(window::center,window::center);
  gw.edit();
  gw.get_window().screenshot("def_edit_actions");

  return 0;	
}

See also:

GraphWin

Customizing the Interactive Interface

Interactive Interface

Call-Back Functions


Windows and Panels

Events


Graph Data Types


Manual Pages:

Manual Page GraphWin




Please send any suggestions, comments or questions to leda@algorithmic-solutions.com
© Copyright 2001-2003, Algorithmic Solutions Software GmbH