Defining GraphWin Edit ActionsWhat 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 maskevent_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 MasksEvent_masks are the bitwise-or of some of the following predefined constants.
(A_LEFT|A_NODE|A_DOUBLE)describes a double click of the left mouse button on a node. Setting Edit ActionsThe 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 .
ExampleThe following program redefines some of the default actions. Node and
edge colors can be changed by holding 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:Customizing the Interactive Interface Manual Pages: |