home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / leda / src / graphics / _win_inp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-15  |  6.1 KB  |  342 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  _win_input.c
  7. +
  8. +
  9. +  Copyright (c) 1991  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #include <LEDA/window.h>
  16. #include <math.h>
  17. #include "global.c"
  18.  
  19.  
  20. //------------------------------------------------------------------------------
  21. // INPUT
  22. //------------------------------------------------------------------------------
  23.  
  24. int  window::confirm(string s)     { return x_draw_confirm(~s); }
  25. void window::acknowledge(string s) { x_draw_acknowledge(~s); }
  26.  
  27.  
  28. int window::read_mouse()
  29. { double X,Y;
  30.   return x_read_mouse(0,0.0,0.0,&X,&Y);
  31. }
  32.  
  33.  
  34. int  window::read_mouse(real& x, real& y)
  35. { double X,Y;
  36.   int key = x_read_mouse(0,0.0,0.0,&X,&Y);
  37.   x =  X;
  38.   y =  Y;
  39.   return key;
  40. }
  41.  
  42. int  window::read_mouse(point& q)
  43. { double X,Y;
  44.   int key = x_read_mouse(0,0.0,0.0,&X,&Y);
  45.   q = point(X,Y);
  46.   return key;
  47. }
  48.  
  49.  
  50.  
  51. int  window::read_mouse_seg(double x0, double y0, real& x, real& y)
  52. { double X,Y;
  53.   int key = x_read_mouse(1,x0,y0,&X,&Y);
  54.   x =  X;
  55.   y =  Y;
  56.   return key;
  57. }
  58.  
  59. int  window::read_mouse_seg(point p, point& q)
  60. { double X,Y;
  61.   int key = x_read_mouse(1,p.xcoord(),p.ycoord(),&X,&Y);
  62.   q = point(X,Y);
  63.   return key;
  64. }
  65.  
  66.  
  67. int  window::read_mouse_rect(double x0, double y0, real& x, real& y)
  68. { double X,Y;
  69.   int key = x_read_mouse(2,x0,y0,&X,&Y);
  70.   x =  X;
  71.   y =  Y;
  72.   return key;
  73. }
  74.  
  75.  
  76. int  window::read_mouse_rect(point p, point& q)
  77. { double X,Y;
  78.   int key = x_read_mouse(2,p.xcoord(),p.ycoord(),&X,&Y);
  79.   q = point(X,Y);
  80.   return key;
  81. }
  82.  
  83.  
  84. int  window::read_mouse_circle(double x0, double y0, real& x, real& y)
  85. { double X,Y;
  86.   int key = x_read_mouse(3,x0,y0,&X,&Y);
  87.   x =  X;
  88.   y =  Y;
  89.   return key;
  90. }
  91.  
  92. int  window::read_mouse_circle(point p, point& q)
  93. { double X,Y;
  94.   int key = x_read_mouse(3,p.xcoord(),p.ycoord(),&X,&Y);
  95.   q = point(X,Y);
  96.   return key;
  97. }
  98.  
  99.  
  100. int window::read_mouse_action(mouse_action_func_ptr f, real& x, real& y)
  101. { double X,Y;
  102.   int key = x_read_mouse_action(f,0.0,0.0,&X,&Y);
  103.   x =  X;
  104.   y =  Y;
  105.   return key;
  106. }
  107.  
  108. int window::read_mouse_action(mouse_action_func_ptr f, point& q)
  109. { double X,Y;
  110.   int key = x_read_mouse_action(f,0.0,0.0,&X,&Y);
  111.   q = point(X,Y);
  112.   return key;
  113. }
  114.  
  115.  
  116.  
  117. window& window::read(point& p)
  118. { real x,y;
  119.   state = 1;
  120.   int k;
  121.   while ((k = read_mouse(x,y)) != 1) 
  122.    if (k == 3) 
  123.     { state = 0;
  124.       return *this;
  125.      }
  126.   p = point(x,y);
  127.   return *this;
  128.  }
  129.  
  130.  
  131. window& window::read(segment& s)
  132. { real x,y;
  133.   point p;
  134.   int key = 0;
  135.   state = 1;
  136.  
  137.   if (!read(p)) 
  138.    return *this;
  139.  
  140.   while ((key=read_mouse_seg(p.xcoord(),p.ycoord(),x,y)) != 1)
  141.   { if (key== 3)  
  142.      { state = 0;
  143.        break; 
  144.       }
  145.  
  146.     if (key==-1)
  147.       if (!read(p)) break;
  148.    }
  149.  
  150.    if (state) s = segment(p.xcoord(),p.ycoord(),x,y);
  151.  
  152.   return *this;
  153. }
  154.  
  155. window& window::read(line& l)
  156. { segment s;
  157.   state = 1;
  158.   read(s);
  159.   if (state) l = line(s);
  160.   return *this;
  161.  }
  162.  
  163.  
  164. window& window::read(circle& c)
  165. { real x,y;
  166.   point p;
  167.   int key = 0;
  168.   state = 1;
  169.  
  170.   if (!read(p)) 
  171.    return *this;
  172.  
  173.   drawing_mode save = set_mode(xor_mode);
  174.   draw(p);
  175.  
  176.   while ((key=read_mouse_circle(p.xcoord(),p.ycoord(),x,y)) != 1)
  177.   { if (key== 3)  
  178.      { state = 0;
  179.        break; 
  180.       }
  181.  
  182.     if (key==-1)
  183.     { draw(p);
  184.       if (!read(p)) break;
  185.       draw(p);
  186.      }
  187.    }
  188.    if (state) 
  189.    { double dx = x-p.xcoord();
  190.      double dy = y-p.ycoord();
  191.      c = circle(p,hypot(dx,dy));
  192.      draw(p);
  193.     }
  194.  
  195.   set_mode(save);
  196.   return *this;
  197. }
  198.  
  199. window& window::read(polygon& P)
  200. { real x,y;
  201.   int key = 0;
  202.   state = 1;
  203.   point p;
  204.  
  205.   list(point) pl;
  206.  
  207.   if (!read(p)) return *this;
  208.  
  209.   pl.append(p);
  210.  
  211.   drawing_mode save = set_mode(xor_mode);
  212.  
  213.   while ((key = read_mouse_seg(p.xcoord(),p.ycoord(),x,y)) !=2)
  214.   { 
  215.     if (key==3) break;
  216.  
  217.  
  218.     if (key==-1 && (pl.length() > 1 )) 
  219.     { point l = pl.Pop();
  220.       draw_segment(pl.tail(),l);
  221.       p = pl.tail();
  222.      }
  223.  
  224.     if (key==1)
  225.     { pl.append(point(x,y));
  226.       draw_segment(p,pl.tail());
  227.       p = pl.tail();
  228.      }
  229.   }
  230.  
  231.   point first = pl.head();
  232.  
  233.   draw_segment(pl.tail(),pl.head());
  234.  
  235.   P = polygon(pl);
  236.  
  237.   draw(P);
  238.  
  239.   if (key==3)
  240.   { state = 0;
  241.     P = 0;
  242.    }
  243.  
  244.   set_mode(save);
  245.  
  246.   return *this;
  247.  
  248. }
  249.  
  250.  
  251. window& window::operator>>(point& p)    
  252. { x_draw_set_frame_label(">> POINT");
  253.   read(p); 
  254.   x_draw_reset_frame_label();
  255.   return *this; 
  256.  }
  257.  
  258. window& window::operator>>(segment& s)  
  259. { x_draw_set_frame_label(">> SEGMENT");
  260.   read(s); 
  261.   x_draw_reset_frame_label();
  262.   return *this; 
  263.  }
  264.  
  265. window& window::operator>>(line& l)     
  266. { x_draw_set_frame_label(">> LINE");
  267.   read(l); 
  268.   x_draw_reset_frame_label();
  269.   return *this; 
  270.  }
  271.  
  272. window& window::operator>>(circle& C)   
  273. { x_draw_set_frame_label(">> CIRCLE");
  274.   read(C); 
  275.   x_draw_reset_frame_label();
  276.   return *this; 
  277.  }
  278.  
  279. window& window::operator>>(polygon& P)  
  280. { x_draw_set_frame_label(">>POLYGON");
  281.   read(P); 
  282.   x_draw_reset_frame_label();
  283.   return *this; 
  284.  }
  285.  
  286.  
  287.  
  288.  
  289.  
  290. int  window::read_panel(string header, int n, string* L)
  291. { char** p = new char*[n];
  292.   for(int i = 0; i < n; i++) p[i] = ~L[i];
  293.   return x_draw_read_panel(~header,n,p,0);
  294.  }
  295.  
  296.  
  297. int  window::read_vpanel(string header, int n, string* L)
  298. { char** p = new char*[n];
  299.   for(int i = 0; i < n; i++) p[i] = ~L[i];
  300.   return x_draw_read_panel(~header,n,p,1);
  301.  }
  302.  
  303.  
  304.  
  305. string  window::read_string(string header)
  306. { return x_draw_read_text_panel(~header,0,0,0); }
  307.  
  308.  
  309. string  window::read_string(string header,string menu_label,list(string)& L)
  310. {  char** p = new char*[L.length()];
  311.    int    i = 0;
  312.    string s;
  313.    forall(s,L) 
  314.     if (s.length() > 0) p[i++] = ~s;
  315.    return x_draw_read_text_panel(~header,~menu_label,i,p); 
  316. }
  317.  
  318. string  window::read_string(string header,list(string)& L)
  319. {  char** p = new char*[L.length()];
  320.    int    i = 0;
  321.    string s;
  322.    forall(s,L) 
  323.     if (s.length() > 0) p[i++] = ~s;
  324.    return x_draw_read_text_panel(~header,"",i,p); 
  325. }
  326.  
  327. int  window::read_int(string s)
  328. { string_istream in(x_draw_read_text_panel(~s,0,0,0));
  329.   int res;
  330.   in >> res;
  331.   return res;
  332.  }
  333.  
  334. real  window::read_real(string s)
  335. { string_istream in(x_draw_read_text_panel(~s,0,0,0));
  336.   double res;
  337.   in >> res;
  338.   return res;
  339.  }
  340.  
  341.