Algorithmic Solutions > LEDA > LEDA Guide > GraphWin > Customizing the Interactive Interface > Call-Back Functions > Example

Example GraphWin Call-Back Functions

The following simple example shows how to define and use call-back functions to customize the interactive interface of GraphWin. The call-back functions only print a message. In the main functions the call-back functions are set for GraphWin gw and gw is opened in edit mode. For more useful examples of how to use Call-Back functions see Online Demos of Graph Algorithms and A Recipe for Online Demos of Network Algorithms.

#include <LEDA/graphics/graphwin.h>
#include <LEDA/geo/point.h>

using namespace leda;

bool new_node_pre_handler(GraphWin& gw,const point& p) 
{
  std::cout << "new_node_pre_handler(): ";
  std::cout << "create new node at point " << p << std::endl;
  return true;
}

void new_node_post_handler(GraphWin& gw, node v)
{
  std::cout << "new_node_post_handler(): node ";
  gw.get_graph().print_node(v);
  std::cout << " was created" << std::endl << std::endl;
}

bool new_edge_pre_handler(GraphWin& gw,node v, node w) 
{
  std::cout << "new_edge_pre_handler(): ";
  std::cout << "create new edge with source ";
  gw.get_graph().print_node(v);
  std::cout << " and target ";
  gw.get_graph().print_node(w);
  std::cout << std::endl;
  return true;
}

void new_edge_post_handler(GraphWin& gw, edge e)
{
  std::cout << "new_edge_post_handler(): edge ";
  gw.get_graph().print_edge(e);
  std::cout << " was created" << std::endl << std::endl;
}

bool start_move_node_handler(GraphWin& gw, node v)
{
  std::cout << "start_move_node_handler(): move node ";
  gw.get_graph().print_node(v);
  std::cout << std::endl;
  return true;
}

bool move_node_handler(GraphWin& gw, node v, const point& p)
{
  std::cout << "move_node_handler(): node ";
  gw.get_graph().print_node(v);
  std::cout << " is at point " << p << std::endl;
  return true;
}

void end_move_node_handler(GraphWin& gw, node v)
{
  std::cout << "end_move_node_handler(): moving node ";
  gw.get_graph().print_node(v);
  std::cout << " finished" << std::endl << std::endl;
}

int main()
{
  GraphWin gw;

  gw.set_new_node_handler(new_node_pre_handler);
  gw.set_new_node_handler(new_node_post_handler);
  gw.set_new_edge_handler(new_edge_pre_handler);
  gw.set_new_edge_handler(new_edge_post_handler);

  gw.set_start_move_node_handler(start_move_node_handler);
  gw.set_move_node_handler(move_node_handler);
  gw.set_end_move_node_handler(end_move_node_handler);

  gw.display(); gw.edit();

  return 0;
}  

See also:

GraphWin

Customizing the Interactive Interface

Call-Back Functions

A Recipe for Online Demos of Graph Algorithms

A Recipe for Online Demos of Network Algorithms


Manual Pages:

Manual Page Windows




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