_leda_window.c
Upload User: gzelex
Upload Date: 2007-01-07
Package Size: 707k
Code Size: 23k
Development Platform:

MultiPlatform

  1. /*******************************************************************************
  2. +
  3. +  LEDA-R  3.2.3
  4. +
  5. +  _leda_window.c
  6. +
  7. +  Copyright (c) 1995  by  Max-Planck-Institut fuer Informatik
  8. +  Im Stadtwald, 66123 Saarbruecken, Germany     
  9. +  All rights reserved.
  10. *******************************************************************************/
  11. // defines the LEDA_WINDOW operations declared in <LEDA/leda_window.h>
  12. // using the basic graphics routines from <LEDA/impl/x_basic.h>
  13. #include <LEDA/leda_window.h>
  14. #include <LEDA/impl/x_basic.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <ctype.h>
  18. #include <stdlib.h>
  19. #include <math.h>
  20. inline double HyPot(double x, double y) { return sqrt(x*x + y*y); }
  21. color::color(const char* name) { col_index = new_color(name);  }
  22. color::color(int i ) { col_index = i;  }
  23. color FG_color = black;
  24. color BG_color = white;
  25. void LEDA_WINDOW::set_palette(int c, int r, int g, int b)
  26. { ::set_palette(c,r,g,b); }
  27. void LEDA_WINDOW::flush() { flush_display(); }
  28. void LEDA_WINDOW::set_frame_label(const char* label) 
  29. { ::set_header(draw_win,label); }
  30. int LEDA_WINDOW::load_text_font(const char* fname) 
  31. { return ::load_text_font(fname); }
  32. int LEDA_WINDOW::load_bold_font(const char* fname) 
  33. { return ::load_bold_font(fname); }
  34. int LEDA_WINDOW::load_message_font(const char* fname) 
  35. { return ::load_message_font(fname); }
  36. int LEDA_WINDOW::set_font(const char* fname) 
  37. { return ::set_font(fname); }
  38. void LEDA_WINDOW::reset_frame_label() 
  39. { ::set_header(draw_win,default_frame_label); }
  40. void LEDA_WINDOW::set_grid_mode(int i) 
  41. { if (i != grid_mode) init(xmin(),xmax(),ymin(),i); }
  42. drawing_mode LEDA_WINDOW::set_mode(drawing_mode m) 
  43. { drawing_mode save = drawing_mo;
  44.   drawing_mo = m;
  45.   ::set_mode(m);
  46.   return save;
  47.  }
  48. int LEDA_WINDOW::set_line_width(int w)
  49. { if (w < 1) w = 1;
  50.   int save = line_width;
  51.   line_width = w;
  52.   ::set_line_width(w);
  53.   return save;
  54.  }
  55. line_style LEDA_WINDOW::set_line_style(line_style s)
  56. { line_style save = line_st;
  57.   line_st = s;
  58.   ::set_line_style(s);
  59.   return save;
  60.  }
  61. int LEDA_WINDOW::set_node_width(int w)
  62. { if (w < 1) w = 1;
  63.   int save = node_width;
  64.   node_width = w;
  65.   return save;
  66.  }
  67. text_mode LEDA_WINDOW::set_text_mode(text_mode m)
  68. { text_mode save = text_mo;
  69.   text_mo = m;
  70.   return save;
  71.  }
  72. void LEDA_WINDOW::set_redraw(redraw_func_ptr f)
  73. { redraw = f;
  74.   ::set_redraw(draw_win,f);
  75.  }
  76. int LEDA_WINDOW::read_event(int& k, double& x, double& y)
  77. { LEDA_WINDOW* w=0;
  78.   int e = event_handler(w,1);
  79.   while (w != this) e = event_handler(w,1);
  80.   x = mouse_xreal;
  81.   y = mouse_yreal;
  82.   k = mouse_key;
  83.   return e;
  84.  }
  85. int LEDA_WINDOW::get_event(int& k, double& x, double& y)
  86. { LEDA_WINDOW* w=0;
  87.   int e = event_handler(w,0);
  88.   while (e != no_event && w != this) e = event_handler(w,0);
  89.   x = mouse_xreal;
  90.   y = mouse_yreal;
  91.   k = mouse_key;
  92.   return e;
  93.  }
  94. void LEDA_WINDOW::put_back_event() { ::put_back_event(); }
  95. int LEDA_WINDOW::event_handler(LEDA_WINDOW*& w, int blocking)
  96. {
  97.   char s[32];
  98.   Window win;
  99.   int   val,x,y,e;
  100.   unsigned long t;
  101.   if (blocking)
  102.      e = get_next_event(&win, &val, &x, &y, &t);
  103.   else
  104.      e = check_next_event(&win, &val, &x, &y, &t);
  105.   int i = 0;
  106.   while(i<count && window_list[i]->draw_win != win) i++; 
  107.   if (i >= count)  return no_event;
  108.   w = window_list[i];
  109.   active_window = w;
  110.   switch (e) {
  111.   case configure_event:
  112.        { if (w->xdots != x || w->ydots != y)
  113.            w->configure(w->min_xcoord,w->max_xcoord,w->min_ycoord,w->grid_mode);
  114.          break; 
  115.         }
  116.   case button_press_event:
  117.            w->mouse_key = val;
  118.            w->mouse_xpix = x;
  119.            w->mouse_ypix = y;
  120.            w->mouse_press_time = t;
  121.            break;
  122.   case button_release_event:
  123.            w->mouse_key = val;
  124.            w->mouse_xpix = x;
  125.            w->mouse_ypix = y;
  126.            w->mouse_release_time = t;
  127.            break;
  128.   case motion_event:
  129.          w->mouse_xpix = x;
  130.          w->mouse_ypix = y;
  131.          if (w->grid_mode) w->cursor();
  132.          w->mouse_xreal =  w->min_xcoord + ((double)w->mouse_xpix)/w->scaling;
  133.          w->mouse_yreal =  w->max_ycoord - ((double)w->mouse_ypix)/w->scaling;
  134.         
  135.          if (blocking)
  136.          { if (w->grid_mode)
  137.            { int g = w->grid_mode;
  138.              w->mouse_xreal = g*(int)(w->mouse_xreal/g +((w->mouse_xreal>0) ? 0.5:-0.5));
  139.              w->mouse_yreal = g*(int)(w->mouse_yreal/g +((w->mouse_yreal>0) ? 0.5:-0.5));
  140.              w->mouse_xpix  = w->xpix(w->mouse_xreal);
  141.              w->mouse_ypix  = w->ypix(w->mouse_yreal);
  142.              w->cursor();
  143.             }
  144.      
  145.            if (w->show_coord && blocking)
  146.            { sprintf(s,"%8.2f %8.2f", w->mouse_xreal,w->mouse_yreal);
  147.              show_coordinates(win,s);
  148.             }
  149.           
  150.            if (w->mouse_action && w == read_window) /* user defined action */
  151.            { w->mouse_action(w->mouse_last_xreal,w->mouse_last_yreal);
  152.              w->mouse_action(w->mouse_xreal,w->mouse_yreal);
  153.             }
  154.           }
  155.       
  156.          w->mouse_last_xreal = w->mouse_xreal;
  157.          w->mouse_last_yreal = w->mouse_yreal;
  158.      }
  159.   return e;
  160. }
  161. int LEDA_WINDOW::read_mouse(int kind, double xstart, double ystart, double &x, double &y)
  162.   // 0: point, 1: segment, 2:rectangle, 3: circle 
  163.   switch(kind) {
  164.   case  0: return read_mouse_action(mouse_default_action,xstart,ystart,x,y);
  165.   case  1: return read_mouse_action(mouse_segment_action,xstart,ystart,x,y);
  166.   case  2: return read_mouse_action(mouse_rect_action,xstart,ystart,x,y);
  167.   case  3: return read_mouse_action(mouse_circle_action,xstart,ystart,x,y);
  168.   default: return read_mouse_action(mouse_default_action,xstart,ystart,x,y);
  169.   }
  170. }
  171. int LEDA_WINDOW::get_button(double &x, double &y)
  172. { mouse_key = 0;
  173.   drawing_mode s = set_mode(xor_mode);
  174.   set_read_gc();
  175.   LEDA_WINDOW* w;
  176.   int e = event_handler(w,0);
  177.   while (e != no_event && e != button_press_event && w != this) 
  178.       e = event_handler(w,0);
  179.   reset_gc();
  180.   set_mode(s);
  181.   x = mouse_xreal;
  182.   y = mouse_yreal;
  183.   return mouse_key;
  184.  }
  185. unsigned long LEDA_WINDOW::button_press_time()  { return mouse_press_time; }
  186. unsigned long LEDA_WINDOW::button_release_time(){ return mouse_release_time; }
  187. int LEDA_WINDOW::read_mouse_action(mouse_action_func_ptr action, double xstart, double ystart, double &x, double &y)
  188. {
  189.   read_window = this;
  190.   mouse_action = action;
  191.   drawing_mode s = set_mode(xor_mode);
  192.   set_read_gc();
  193.   mouse_key = 0;
  194.   mouse_start_xreal = xstart;
  195.   mouse_start_yreal = ystart;
  196.   mouse_start_xpix = xpix(xstart);
  197.   mouse_start_ypix = ypix(ystart);
  198.   if (grid_mode) cursor();
  199.   if (show_coord)
  200.   { char s[64];
  201.     sprintf(s,"%8.2f %8.2f", mouse_xreal,mouse_yreal);
  202.     show_coordinates(draw_win,s);
  203.    }
  204.   if (mouse_action) mouse_action(mouse_last_xreal,mouse_last_yreal);
  205.   LEDA_WINDOW* w;
  206.   while (event_handler(w,1) != button_press_event || w != this);
  207.   if (mouse_action) mouse_action(mouse_xreal,mouse_yreal);
  208.   if (grid_mode) cursor();
  209.   x = mouse_xreal;
  210.   y = mouse_yreal;
  211.   reset_gc();
  212.   set_mode(s);
  213.   mouse_action = mouse_default_action;
  214.   return mouse_key;
  215. }
  216. void LEDA_WINDOW::close() 
  217. { if (draw_win)
  218.   { int i = 0;
  219.     while (window_list[i] != this) i++;
  220.     count--;
  221.     window_list[i] = window_list[count];
  222.     close_window(draw_win); 
  223.     draw_win = 0;
  224.     if (active_window == this) active_window = 0;
  225.    }
  226.  }
  227. LEDA_WINDOW::LEDA_WINDOW(float width, float height, float xpos, float ypos, 
  228.                                                       const char *frame_label)
  229. { ref_count = 1;
  230.   state = 1;
  231.   draw_win = 0;
  232.   open(int(width),int(height),int(xpos),int(ypos),frame_label);
  233.  }
  234. LEDA_WINDOW::LEDA_WINDOW(float width, float height, const char* frame_label)
  235. { ref_count = 1;
  236.   state = 1;
  237.   draw_win = 0;
  238.   open(int(width),int(height),-3,-1,frame_label); 
  239.  }
  240. LEDA_WINDOW::LEDA_WINDOW(const char* frame_label)
  241. { ref_count = 1;
  242.   state = 1;
  243.   draw_win = 0;
  244.   int l  =  screen_height() - 35;
  245.   if (l > 700) l = 700;
  246.   open(l,l,-3,-1,frame_label);
  247.  }
  248. LEDA_WINDOW::LEDA_WINDOW()
  249. { ref_count = 1;
  250.   state = 1;
  251.   draw_win = 0;
  252.   //int l  =  screen_height() - 35;
  253.   //if (l > 700) l = 700;
  254.   //open(l,l,-3,-1,"");
  255.  }
  256. LEDA_WINDOW::LEDA_WINDOW(const LEDA_WINDOW& w)
  257. { state = w.state;
  258.   ref_count = w.ref_count+1;
  259.   draw_win = w.draw_win;
  260.  }
  261. LEDA_WINDOW::~LEDA_WINDOW() 
  262. { if (--ref_count == 0) close();
  263.   if (count==0) close_display(); 
  264.  }
  265. LEDA_WINDOW& LEDA_WINDOW::operator=(const LEDA_WINDOW& w)
  266. { if (&w != this)
  267.   { if (--ref_count == 0) close();
  268.     state = w.state;
  269.     ref_count = w.ref_count + 1;
  270.     draw_win = w.draw_win;
  271.    }
  272.   return *this;
  273.  }
  274. void LEDA_WINDOW::open(int w_width, int w_height, int w_xpos, int w_ypos, 
  275.                        const char *frame_label, int bg_col)
  276. {
  277.   if (draw_win) return;
  278.   active_window = this;
  279.   default_frame_label[0] = 0;
  280.   if (frame_label) strcpy(default_frame_label,frame_label);
  281.   if (count==0) open_display();
  282.   window_list[count] = this;
  283.   count++;
  284.   if (w_xpos < 0)
  285.     switch (w_xpos) {
  286.     case -1 : w_xpos = 0;
  287.               break;
  288.     case -2 : w_xpos = (screen_width() - w_width - 10)/2;
  289.               break;
  290.     case -3 : w_xpos = (screen_width() - w_width - 10);
  291.               break;
  292.     default : w_xpos = 0;
  293.     }
  294.   if (w_ypos < 0)
  295.     switch (w_ypos) {
  296.     case -1 : w_ypos = 0;
  297.               break;
  298.     case -2 : w_ypos = (screen_height() - w_height - 30)/2;
  299.               break;
  300.     case -3 : w_ypos = (screen_height() - w_height - 30);
  301.               break;
  302.     default : w_ypos = 0;
  303.     }
  304.   draw_win = open_window(w_xpos, w_ypos, w_width, w_height, bg_col,
  305.                          default_frame_label,"LEDA WINDOW");
  306.   set_color(black);
  307.   set_text_font();
  308.   set_mode(src_mode);
  309.   set_line_style(solid);
  310.   set_line_width(1);
  311.   set_node_width(12);
  312.   set_text_mode(transparent);
  313.   set_show_coordinates(1);
  314.   redraw = 0;
  315.   depth = display_depth();
  316.   min_xcoord = 0;
  317.   min_ycoord = 0;
  318.   max_xcoord = 0;
  319.   max_ycoord = 0;
  320.   scaling = 1;
  321.   mouse_xpix = 0;
  322.   mouse_ypix = 0;
  323.   mesg_count = 0;
  324.   screen_flush = 1;
  325.   window_xpos = w_xpos;
  326.   window_ypos = w_ypos;
  327.   window_width = w_width;
  328.   window_height = w_height;
  329.   configure(0,100,0,0);  // default coordinates
  330. }
  331. void LEDA_WINDOW::configure(double x0, double x1, double y0, int g_mode)
  332. {
  333.   grid_mode = g_mode;
  334.   xdots = ::window_width(draw_win);
  335.   ydots = ::window_height(draw_win);
  336.   scaling = ((double)xdots)/(x1-x0);
  337.   if ((grid_mode) && (grid_mode*scaling < 2))
  338.   { // at least grid distance of 2 pixels
  339.     grid_mode=0; 
  340.     fprintf(stderr,"warning: grid distance to small.n");
  341.    }
  342.   min_xcoord = x0;
  343.   min_ycoord = y0;
  344.   if (grid_mode)
  345.     { max_xcoord = x0+int(xdots/scaling);
  346.       max_ycoord = y0+int(ydots/scaling);
  347.      }
  348.   else
  349.     { max_xcoord = x0+xdots/scaling;
  350.       max_ycoord = y0+ydots/scaling;
  351.      }
  352.   xorigin = (int)(-x0*scaling);
  353.   yorigin = (int)(ydots+y0*scaling);
  354.   mouse_xreal = 0;
  355.   mouse_yreal = 0;
  356.   drawing_mode save = set_mode(src_mode);
  357.   if (redraw) (*redraw)();
  358.   set_mode(save);
  359. }
  360. void LEDA_WINDOW::init(double x0, double x1, double y0, int g_mode)
  361. { if (x0 >= x1)
  362.   { fprintf(stderr,"illegal arguments in W.init: x0 (%f) >= x1 (%f)n",x0,x1);
  363.     abort();
  364.   }
  365.   configure(x0,x1,y0,g_mode);
  366.   clear(0);
  367.  }
  368. void LEDA_WINDOW::draw_pix(double x, double y, int col)
  369. { set_color(col);
  370.   pixel(draw_win,xpix(x),ypix(y));
  371.   if (screen_flush) flush_display();
  372. }
  373. void LEDA_WINDOW::draw_segment(double x1, double y1, double x2, double y2, int col)
  374. { set_color(col);
  375.   line(draw_win, xpix(x1), ypix(y1), xpix(x2), ypix(y2));
  376.   if (screen_flush) flush_display();
  377. }
  378. void LEDA_WINDOW::draw_point(double x, double y, int col)
  379. { int X = xpix(x);
  380.   int Y = ypix(y);
  381.   set_color(col);
  382. /*
  383.   pixel(draw_win,X-2,Y-2);
  384.   pixel(draw_win,X+2,Y-2);
  385.   pixel(draw_win,X-1,Y-1);
  386.   pixel(draw_win,X+1,Y-1);
  387.   pixel(draw_win,X,Y);
  388.   pixel(draw_win,X-1,Y+1);
  389.   pixel(draw_win,X+1,Y+1);
  390.   pixel(draw_win,X-2,Y+2);
  391.   pixel(draw_win,X+2,Y+2);
  392. */
  393.   point(draw_win,X,Y);
  394.   if (screen_flush) flush_display();
  395. }
  396. void LEDA_WINDOW::draw_arc(double x0, double y0, double r1, double r2, double start, double angle, int col)
  397. { int R1 = (int)(r1*scaling);
  398.   int R2 = (int)(r2*scaling);
  399.   set_color(col);
  400.   arc(draw_win,xpix(x0),ypix(y0),R1,R2,start,angle);
  401.   if (screen_flush) flush_display();
  402. }
  403. void LEDA_WINDOW::draw_filled_arc(double x0, double y0, double r1, double r2, double start, double angle, int col)
  404. { int R1 = (int)(r1*scaling);
  405.   int R2 = (int)(r2*scaling);
  406.   set_color(col);
  407.   fill_arc(draw_win,xpix(x0),ypix(y0),R1,R2,start,angle);
  408.   if (screen_flush) flush_display();
  409. }
  410. void LEDA_WINDOW::draw_node(double x0, double y0, int col)
  411. { int save = set_line_width(1);
  412.   double R = node_width/scaling;
  413.   draw_circle(x0,y0,R,col);
  414.   set_line_width(save);
  415.  }
  416. void LEDA_WINDOW::draw_filled_node(double x0, double y0, int col)
  417. { double R = node_width/scaling;
  418.   if (depth==1 && col!=white) col = black;
  419.   draw_filled_circle(x0,y0,R,col);
  420.   if (col != black) 
  421.   { int save = set_line_width(1);
  422.     draw_circle(x0,y0,R,col);
  423.     draw_circle(x0,y0,R,black);
  424.     set_line_width(save);
  425.    }
  426.  }
  427. void LEDA_WINDOW::draw_text_node(double x0, double y0, char *s, int col)
  428. { text_mode t_save = set_text_mode(transparent);
  429.   set_bold_font();
  430.   if (depth==1 && col!=black) col = white;
  431.   draw_filled_node(x0,y0,col);
  432.   
  433.   draw_ctext(x0,y0,s,col);
  434.   if (col == black) 
  435.      draw_ctext(x0,y0,s,white);
  436.   else
  437.      draw_ctext(x0,y0,s,black);
  438.   set_text_mode(t_save);
  439.   set_text_font();
  440. }
  441. void LEDA_WINDOW::draw_int_node(double x0, double y0, int i, int col)
  442. { char buf[16];
  443.   sprintf(buf,"%d",i);
  444.   draw_text_node(x0,y0,buf,col);
  445.  }
  446. void LEDA_WINDOW::draw_edge(double x1, double y1, double x2, double y2, int col)
  447. { double dx = x2-x1;
  448.   double dy = y2-y1;
  449.   if (dx==0 && dy==0) return;
  450.   double l = node_width/(scaling*HyPot(dx,dy));
  451.   x1 += l*dx;
  452.   x2 -= l*dx;
  453.   y1 += l*dy;
  454.   y2 -= l*dy;
  455.   draw_segment(x1,y1,x2,y2,col);
  456. }
  457. void LEDA_WINDOW::draw_circle(double x0, double y0, double r, int col)
  458. { int R = (int)(r*scaling);
  459.   set_color(col);
  460.   circle(draw_win,xpix(x0),ypix(y0),R);
  461.   if (screen_flush) flush_display();
  462.  }
  463. void LEDA_WINDOW::draw_filled_circle(double x0, double y0, double r, int col)
  464. { int R = (int)(r*scaling);
  465.   set_color(col);
  466.   if (R > 0)
  467.      fill_circle(draw_win,xpix(x0),ypix(y0),R);
  468.   else
  469.      pixel(draw_win,xpix(x0),ypix(y0));
  470.   if (screen_flush) flush_display();
  471.  }
  472. void LEDA_WINDOW::draw_ellipse(double x0, double y0, double a, double b, int col)
  473. { int R1 = (int)(a*scaling);
  474.   int R2 = (int)(b*scaling);
  475.   set_color(col);
  476.   ellipse(draw_win,xpix(x0),ypix(y0),R1,R2);
  477.   if (screen_flush) flush_display();
  478.  }
  479. void LEDA_WINDOW::draw_filled_ellipse(double x0, double y0, double a, double b, int col)
  480. { int R1 = (int)(a*scaling);
  481.   int R2 = (int)(b*scaling);
  482.   set_color(col);
  483.   fill_ellipse(draw_win,xpix(x0),ypix(y0),R1,R2);
  484.   if (screen_flush) flush_display();
  485.  }
  486. void LEDA_WINDOW::plot_xy(double x0, double x1, double (*f) (double), int col)
  487. {
  488.   int *xcoord;
  489.   int *ycoord;
  490.   int x = xpix(x0);
  491.   int y_old = ypix((*f)(x0));
  492.   int i,y_new;
  493.   int size = 0;
  494.   int n = 0;
  495.   set_color(col);
  496.   for(x = xpix(x0)+1; x <= xpix(x1); x++)
  497.   { y_new = ypix((*f)(xreal(x)));
  498.     if (y_new > y_old)
  499.        size += (y_new-y_old+1);
  500.     else
  501.        size += (y_old-y_new+1);
  502.     y_old = y_new;
  503.    }
  504.   xcoord = (int*) malloc(size*sizeof(int));
  505.   ycoord = (int*) malloc(size*sizeof(int));
  506.   y_old = ypix((*f)(x0));
  507.   for(x = xpix(x0)+1; x <= xpix(x1); x++)
  508.   { y_new = ypix((*f)(xreal(x)));
  509.     if (y_new > y_old)
  510.       for(i=y_old; i<=y_new; i++) 
  511.       { xcoord[n] = x;
  512.         ycoord[n] = i;
  513.         n++;
  514.        }
  515.     else
  516.       for(i=y_old; i>=y_new; i--) 
  517.       { xcoord[n] = x;
  518.         ycoord[n] = i;
  519.         n++;
  520.        }
  521.     y_old = y_new;
  522.   }
  523.  pixels(draw_win,size,xcoord,ycoord);
  524.  
  525.  free((char*)xcoord);
  526.  free((char*)ycoord);
  527.   if (screen_flush) flush_display();
  528. }
  529. void LEDA_WINDOW::plot_yx(double y0, double y1, double (*f) (double), int col)
  530. {
  531.   int *xcoord;
  532.   int *ycoord;
  533.   int y;
  534.   int i,x_new;
  535.   int x_old = xpix((*f)(y0));
  536.   int size = 0;
  537.   int n = 0;
  538.   set_color(col);
  539.   for(y = ypix(y0)-1; y >= ypix(y1); y++)
  540.   { x_new = xpix((*f)(yreal(y)));
  541.     if (x_new > x_old)
  542.        size += (x_new-x_old+1);
  543.     else
  544.        size += (x_old-x_new+1);
  545.     x_old = x_new;
  546.    }
  547.   xcoord = (int*) malloc(size*sizeof(int));
  548.   ycoord = (int*) malloc(size*sizeof(int));
  549.   x_old = xpix((*f)(y0));
  550.   for(y = ypix(y0)-1; y >= ypix(y1); y--)
  551.   {
  552.     x_new = xpix((*f)(yreal(y)));
  553.     if (x_new > x_old)
  554.       for(i=x_old; i<=x_new; i++) 
  555.       { xcoord[n] = i;
  556.         ycoord[n] = y;
  557.         n++;
  558.        }
  559.     else
  560.       for(i=x_old; i>=x_new; i--) 
  561.       { xcoord[n] = i;
  562.         ycoord[n] = y;
  563.         n++;
  564.        }
  565.     x_old = x_new;
  566.   }
  567.  pixels(draw_win,size,xcoord,ycoord);
  568.  
  569.  free((char*)xcoord);
  570.  free((char*)xcoord);
  571.  if (screen_flush) flush_display();
  572. }
  573. void LEDA_WINDOW::draw_filled_polygon(int n, double *xcoord, double *ycoord, int col)
  574. {
  575.  int* x = (int*) malloc(n*sizeof(int));
  576.  int* y = (int*) malloc(n*sizeof(int));
  577.  int i;
  578.  for(i=0;i<n;i++)
  579.  { x[i] = xpix(xcoord[i]);
  580.    y[i] = ypix(ycoord[i]);
  581.   }
  582.  set_color(col);
  583.  fill_polygon(draw_win,n,x,y);
  584.  free((char*)x);
  585.  free((char*)y);
  586.   if (screen_flush) flush_display();
  587. }
  588. void LEDA_WINDOW::draw_polygon(int n, double *xcoord, double *ycoord, int col)
  589. { int i;
  590.   for(i=0;i<n-1;i++)
  591.     draw_segment(xcoord[i],ycoord[i], xcoord[i+1],ycoord[i+1],col);
  592.   draw_segment(xcoord[n-1],ycoord[n-1],xcoord[0],ycoord[0],col);
  593.   if (screen_flush) flush_display();
  594.  }
  595. void LEDA_WINDOW::draw_rectangle(double x1, double y1, double x2, double y2, int col)
  596. { set_color(col);
  597.   rectangle(draw_win,xpix(x1),ypix(y1),xpix(x2),ypix(y2));
  598.   if (screen_flush) flush_display();
  599.  }
  600. void LEDA_WINDOW::draw_filled_rectangle(double x1, double y1, double x2, double y2, int col)
  601. { set_color(col);
  602.   box(draw_win,xpix(x1),ypix(y1),xpix(x2),ypix(y2));
  603.   if (screen_flush) flush_display();
  604.  }
  605. void LEDA_WINDOW::copy_rect(double x1, double y1, double x2, double y2, double x, double y)
  606. { copy_pixrect(draw_win,xpix(x1),ypix(y1),xpix(x2),ypix(y2),xpix(x),ypix(y));
  607.   if (screen_flush) flush_display();
  608.  }
  609. void LEDA_WINDOW::insert_bitmap(int width, int height, char* data)
  610. { ::insert_bitmap(draw_win,width,height,data);
  611.   if (screen_flush) flush_display();
  612.  }
  613. void LEDA_WINDOW::clear(int col)
  614. { int x0,y0,n;
  615.   double x,y;
  616.   int *xcoord;
  617.   int *ycoord;
  618.   int save_lw = set_line_width(1);
  619.   line_style save_ls = set_line_style(solid);
  620.  del_messages();
  621.  if (depth == 1) col = black;
  622.  clear_window(draw_win);
  623.  set_color(col);
  624.  if (grid_mode) /* draw grid */
  625.  { n =  (int)((max_xcoord - min_xcoord)/grid_mode + 1);
  626.    n *= (int)((max_ycoord - min_ycoord)/grid_mode + 1);
  627.    x0 = ((int)(min_xcoord/grid_mode))*grid_mode;
  628.    y0 = ((int)(min_ycoord/grid_mode))*grid_mode;
  629.    xcoord = (int *) malloc(n*sizeof(int));
  630.    ycoord = (int *) malloc(n*sizeof(int));
  631.    n = 0;
  632.    for(x = x0; x<=max_xcoord; x+=grid_mode)
  633.    for(y = y0; y<=max_ycoord; y+=grid_mode)
  634.    { if (x == 0 || y == 0)
  635.         fill_circle(draw_win,xpix(x),ypix(y),1);
  636.      else
  637.         { xcoord[n] = xpix(x);
  638.           ycoord[n] = ypix(y);
  639.           n++;
  640.          }
  641.     }
  642.    pixels(draw_win,n,xcoord,ycoord);
  643.    free((char*)xcoord);
  644.    free((char*)ycoord);
  645.   }
  646.  set_line_width(save_lw);
  647.  set_line_style(save_ls);
  648.  flush_display();
  649. }
  650. void LEDA_WINDOW::message(const char *s)
  651. { drawing_mode save = set_mode(xor_mode);
  652.   mesg_list[mesg_count] = (char*)malloc(strlen(s)+1);
  653.   strcpy(mesg_list[mesg_count],s);
  654.   mesg_count++;
  655.   set_color(black);
  656.   set_message_font();
  657.   int h = int(1.3 * text_height("H"));
  658.   put_text(draw_win,20,10+h*mesg_count,s,0);
  659.   set_text_font();
  660.   set_mode(save);
  661.   flush_display();
  662. }
  663. void LEDA_WINDOW::del_messages(void)
  664. { drawing_mode save = set_mode(xor_mode);
  665.   set_color(black);
  666.   set_message_font();
  667.   int h = int(1.3 * text_height("H"));
  668.   while(mesg_count > 0)
  669.   { char* s = mesg_list[mesg_count-1];
  670.     put_text(draw_win,20,10+h*mesg_count,s,0);
  671.     mesg_count--;
  672.     free(s);
  673.    }
  674.   set_text_font();
  675.   set_mode(save);
  676.   flush_display();
  677. }
  678. void LEDA_WINDOW::draw_text(double x, double y, const char *s, int col)
  679. { set_color(col);
  680.   put_text(draw_win,xpix(x),ypix(y),s,text_mo);
  681.   if (screen_flush) flush_display();
  682. }
  683. void LEDA_WINDOW::draw_ctext(double x, double y, const char *s, int col)
  684. { set_color(col);
  685.   put_ctext(draw_win,xpix(x),ypix(y),s,text_mo);
  686.   if (screen_flush) flush_display();
  687. }
  688. void LEDA_WINDOW::cursor(void)
  689. { int X = xpix(mouse_xreal);
  690.   int Y = ypix(mouse_yreal);
  691.   line(draw_win, X,Y,X+10,Y);
  692.   line(draw_win, X,Y,X-10,Y);
  693.   line(draw_win, X,Y,X,Y+10);
  694.   line(draw_win, X,Y,X,Y-10);
  695. }
  696. // define static members
  697. LEDA_WINDOW* LEDA_WINDOW::window_list[32];
  698. LEDA_WINDOW* LEDA_WINDOW::read_window = 0;
  699. LEDA_WINDOW* LEDA_WINDOW::active_window = 0;
  700. int          LEDA_WINDOW::count = 0;
  701. int  LEDA_WINDOW::screen_width(void)  { return display_width(); }
  702. int  LEDA_WINDOW::screen_height(void) { return display_height(); }
  703. int  LEDA_WINDOW::screen_depth(void)  { return display_depth(); }
  704. void LEDA_WINDOW::mouse_default_action(double,double) 
  705. { /* do nothing */}
  706. void LEDA_WINDOW::mouse_segment_action(double x, double y) 
  707. { double x0 = read_window->mouse_start_xreal;
  708.   double y0 = read_window->mouse_start_yreal;
  709.   read_window->draw_segment(x0,y0,x,y,black); 
  710.  }
  711. void LEDA_WINDOW::mouse_rect_action(double x, double y)
  712. { double x0 = read_window->mouse_start_xreal;
  713.   double y0 = read_window->mouse_start_yreal;
  714.   read_window->draw_rectangle(x0,y0,x,y,black);
  715.  }
  716. void LEDA_WINDOW::mouse_circle_action(double x, double y)
  717. { double x0 = read_window->mouse_start_xreal;
  718.   double y0 = read_window->mouse_start_yreal;
  719.   read_window->draw_circle(x0,y0,HyPot(x-x0,y-y0),black);
  720.  }
  721. #include <fstream.h>
  722. char* Read_Leda_Bitmap(const char* fname, int& w, int& h)
  723. {
  724.   ifstream bitmap(fname);
  725.   unsigned char c1,c2;
  726.   bitmap.get(c1);
  727.   bitmap.get(c2);
  728.   w = c2;
  729.   w *= 256;
  730.   w += c1;
  731.   bitmap.get(c1);
  732.   bitmap.get(c2);
  733.   h = c2;
  734.   h *= 256;
  735.   h += c1;
  736.   int n = w*h/8;
  737.   char* map = new char[n];
  738.   int i = 0;
  739.   while(i<n && bitmap) 
  740.   { char c;
  741.     bitmap.get(c);
  742.     if (c == char(0xff) || c == char(0x00))
  743.        { unsigned char j;
  744.          bitmap.get(j);
  745.          while (j--) map[i++] = c;
  746.         }
  747.     else 
  748.        map[i++] = c;
  749.    }
  750.   return map;
  751. }
  752. int LEDA_WINDOW::read_mouse(LEDA_WINDOW*& w, double& x, double& y)
  753. { set_read_gc();
  754.   int e = LEDA_WINDOW::event_handler(w,1);
  755.   reset_gc();
  756.   while (e != button_press_event && e != key_press_event) 
  757.   { set_read_gc();
  758.     e = LEDA_WINDOW::event_handler(w,1);
  759.     reset_gc();
  760.    }
  761.   x = w->mouse_xreal;
  762.   y = w->mouse_yreal;
  763.   return w->mouse_key;
  764. }
  765. /*
  766. LEDA_WINDOW* get_active_window()
  767. { LEDA_WINDOW* w;
  768.   set_read_gc();
  769.   int e = LEDA_WINDOW::event_handler(w,1);
  770.   reset_gc();
  771.   while (e != button_press_event && e != key_press_event) 
  772.   { set_read_gc();
  773.     e = LEDA_WINDOW::event_handler(w,1);
  774.     reset_gc();
  775.    }
  776.   return w;
  777.  }
  778. */
  779. void LEDA_WINDOW::start_batch() { ::start_batch(draw_win); }
  780. void LEDA_WINDOW::end_batch()   { ::end_batch(draw_win); }