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

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  _turbo.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.  
  16.  
  17. /* TC graphic  routines */
  18.  
  19. #include <graphics.h>
  20. #include <dos.h>
  21. #include <conio.h>
  22.  
  23. #include <math.h>
  24. #include <string.h>
  25. #include <stdio.h>
  26. #include <values.h>
  27.  
  28. #define KEY_UP 72
  29. #define KEY_DO 80
  30. #define KEY_LE 75
  31. #define KEY_RI 77
  32.  
  33.  
  34. #define XPIX(coord) (int)(xorigin + (coord)*x_draw_scale)
  35. #define YPIX(coord) (int)(yorigin - (coord)*y_draw_scale)
  36.  
  37. #define XREAL(pix)  ((double)(pix-xorigin)/x_draw_scale)
  38. #define YREAL(pix)  ((double)(yorigin-pix)/y_draw_scale)
  39.  
  40.  
  41. static char default_frame_label[128];
  42.  
  43. static char frame_label[80];
  44.  
  45. static char read_frame_format[] = "%8.2f %8.2f      %s";
  46.  
  47. typedef void (*PRD)();
  48. typedef void (*PMA)();
  49.  
  50. static void x_draw_mouse_default_action(x,y) 
  51. double x,y;
  52. { /* do nothing */}
  53.  
  54. PRD x_draw_redraw = 0;
  55. PMA x_draw_mouse_action = x_draw_mouse_default_action;
  56.  
  57. static int x_draw_have_mouse;
  58.  
  59. static int xpix_mode_op;
  60.  
  61. static int xdots,ydots, xorigin,yorigin;  /* pixels          */
  62.  
  63. static int mouse_key,mouse_xpix, mouse_ypix; 
  64. static int mouse_last_xpix, mouse_last_ypix;
  65. static int mouse_start_xpix, mouse_start_ypix;  /* start for segments  ... */
  66. static int mouse_read_kind;
  67.  
  68.  
  69. static double mouse_xreal,mouse_yreal;
  70. static double mouse_last_xreal,mouse_last_yreal;
  71. static double mouse_start_xreal,mouse_start_yreal;
  72.  
  73. static double x_draw_aspect_ratio;
  74. static double y_draw_scale;   
  75.  
  76. /* external variables */
  77.  
  78. int SCREEN_WIDTH  = 740;
  79. int SCREEN_HEIGHT = 360;
  80.    
  81. double x_draw_xmax,x_draw_xmin,x_draw_ymax,x_draw_ymin, x_draw_scale;
  82.  
  83. int    x_draw_grid_mode;
  84. int    x_draw_depth;
  85.  
  86. int    x_draw_line_width, x_draw_node_width, x_draw_line_style, x_draw_color,
  87.        x_draw_text_mode, x_draw_drawing_mode, x_draw_screen_flush;
  88.  
  89.  
  90.  
  91. /* some forward declarations */
  92.  
  93. void x_draw_circle();
  94. void x_draw_rectangle();
  95. void x_draw_clear();
  96. void x_draw_ctext();
  97. void x_draw_text();
  98. void x_draw_cursor();
  99.  
  100.  
  101. void x_draw_flush() { }
  102.  
  103. void x_draw_set_redraw(f)
  104. PRD f;
  105. { x_draw_redraw = f; 
  106.  }
  107.  
  108. void x_draw_set_frame_label(message)
  109. char* message;
  110. { strcpy(frame_label,message);
  111.   }
  112.  
  113. void x_draw_reset_frame_label()
  114. { x_draw_set_frame_label(default_frame_label);
  115.  }
  116.  
  117.  
  118. void x_draw_mouse_segment_action(x,y)
  119. double x,y;
  120. { setcolor(WHITE);
  121.   line(mouse_start_xpix,mouse_start_ypix,XPIX(x),YPIX(y));
  122.  }
  123.  
  124. void x_draw_mouse_rect_action(x,y)
  125. double x,y;
  126.    x_draw_rectangle(mouse_start_xreal,mouse_start_yreal,x,y,WHITE);
  127.  } 
  128.  
  129.  
  130. void x_draw_mouse_circle_action(x,y)
  131. double x,y;
  132. { double r = hypot(x - mouse_start_xreal, y - mouse_start_yreal);
  133.   setcolor(WHITE);
  134.   line(mouse_start_xpix,mouse_start_ypix-(int)(r*y_draw_scale),
  135.        mouse_start_xpix,mouse_start_ypix+(int)(r*y_draw_scale));
  136.   line(mouse_start_xpix+(int)(r*x_draw_scale),mouse_start_ypix,
  137.        mouse_start_xpix-(int)(r*x_draw_scale),mouse_start_ypix);
  138.  }
  139.  
  140.  
  141. void x_draw_set_font(fname)
  142. char* fname;
  143. { }
  144.  
  145.  
  146.  
  147.  
  148. static void x_draw_set_color(col)
  149. int col;
  150. { switch(col) 
  151.   { case 1 : col = WHITE;
  152.              break;
  153.     case 0 : col = BLACK;
  154.              break;
  155.     case 2 : col = LIGHTRED;
  156.              break;
  157.     case 3 : col = LIGHTGREEN;
  158.              break;
  159.     case 4 : col = LIGHTBLUE;
  160.              break;
  161.     case 5 : col = YELLOW;
  162.              break;
  163.     case 6 : col = LIGHTCYAN;
  164.              break;
  165.     case 7 : col = BROWN;
  166.              break;
  167.    }
  168.   setcolor(col);
  169.   setfillstyle(SOLID_FILL,col);
  170. }
  171.  
  172. int x_draw_set_line_width(w)
  173. int w;
  174. { int save = x_draw_line_width;
  175.   x_draw_line_width = w;
  176.   setlinestyle(x_draw_line_style,0,w);
  177.   return save;
  178. }
  179.  
  180.  
  181. int x_draw_set_line_style(s)
  182. int s;
  183. { int old = x_draw_line_style;
  184.   x_draw_line_style = s;
  185.   setlinestyle(s,0,x_draw_line_width);
  186.   return old;
  187. }
  188.  
  189.  
  190. int x_draw_set_mode(m)
  191. int m;
  192. { int old=x_draw_drawing_mode;
  193.   x_draw_drawing_mode=m;
  194.   setwritemode(m);
  195.   return old;
  196. }
  197.  
  198.  
  199. int x_draw_set_node_width(w)
  200. int w;
  201. { int save = x_draw_node_width;
  202.   x_draw_node_width = w;
  203.   return save;
  204.  }
  205.  
  206. int x_draw_set_text_mode(m)
  207. int m;
  208. { int save = x_draw_text_mode;
  209.   x_draw_text_mode = m;
  210.   return save;
  211.  }
  212.  
  213. static void draw_cursor(x,y)
  214. int x,y;
  215. { int save = getcolor();
  216.   int dx = 8;
  217.   int dy = 8*x_draw_aspect_ratio;
  218.   setcolor(WHITE);
  219.   setlinestyle(SOLID_LINE,0,THICK_WIDTH);
  220.   line(x-dx,y,x+dx,y);
  221.   line(x,y-dy,x,y+dy);
  222.   setlinestyle(x_draw_line_style,0,x_draw_line_width);
  223.   setcolor(save);
  224.  }
  225.  
  226.  
  227. int x_read_mouse_action(action, xstart,ystart,x,y)
  228. PMA   action;
  229. double xstart;
  230. double ystart;
  231. double *x;
  232. double *y;
  233.   int taste=0;
  234.   
  235.   union REGS regs;
  236.  
  237.   if (action==0)
  238.     action = x_draw_mouse_default_action; 
  239.  
  240.   setwritemode(XOR_PUT);
  241.  
  242.   mouse_start_xreal = xstart;
  243.   mouse_start_yreal = ystart;
  244.   mouse_start_xpix = XPIX(xstart);
  245.   mouse_start_ypix = YPIX(ystart);
  246.  
  247.   if (x_draw_grid_mode) x_draw_cursor();
  248.  
  249.   action(mouse_last_xreal,mouse_last_yreal);
  250.  
  251.   draw_cursor(mouse_last_xpix, mouse_last_ypix);
  252.  
  253.  
  254.   while (taste == 0)
  255.   { 
  256.     if (x_draw_have_mouse)
  257.     { regs.x.ax=03;
  258.       int86(0x33,®s,®s);
  259.       taste=regs.x.bx;
  260.       mouse_xpix=regs.x.cx;
  261.       mouse_ypix=regs.x.dx;
  262.      }
  263.     else
  264.     { char c;
  265.       c = getch();
  266.       switch(c) {
  267.         case KEY_LE: mouse_xpix -= 8; break;
  268.         case KEY_RI: mouse_xpix += 8; break;
  269.         case KEY_UP: mouse_ypix -= 8*x_draw_aspect_ratio; break;
  270.         case KEY_DO: mouse_ypix += 8*x_draw_aspect_ratio; break;
  271.         case    'j': mouse_ypix +=1; break;
  272.         case    'k': mouse_ypix -=1; break;
  273.         case    'l': mouse_xpix +=1; break;
  274.         case    'h': mouse_xpix -=1; break;
  275.         case    '1': taste = 1; break;
  276.         case    '2': taste = 4; break;
  277.         case    '3': taste = 2; break;
  278.         case     3 : closegraph(); exit(0); break;     /* ctrl-C */
  279.         case    13 : taste = 1; break;                 /* return */
  280.        }
  281.       }
  282.   
  283.     if (x_draw_grid_mode)  x_draw_cursor();
  284.    
  285.     mouse_xreal =  XREAL(mouse_xpix);
  286.     mouse_yreal =  YREAL(mouse_ypix);
  287.    
  288.     if (x_draw_grid_mode) 
  289.     { mouse_xreal = x_draw_grid_mode * (int)(mouse_xreal/x_draw_grid_mode + ((mouse_xreal > 0) ? 0.5 : -0.5));
  290.    
  291.       mouse_yreal = x_draw_grid_mode * (int)(mouse_yreal/x_draw_grid_mode + ((mouse_yreal > 0) ? 0.5 : -0.5));
  292.    
  293.       mouse_xpix  = XPIX(mouse_xreal);
  294.       mouse_ypix  = YPIX(mouse_yreal);
  295.       x_draw_cursor();
  296.     }
  297.           
  298.     if (mouse_last_xpix != mouse_xpix || mouse_last_ypix != mouse_ypix)
  299.     { action(mouse_last_xreal,mouse_last_yreal);
  300.       action(mouse_xreal,mouse_yreal);
  301.       draw_cursor(mouse_last_xpix,mouse_last_ypix);
  302.       draw_cursor(mouse_xpix,mouse_ypix);
  303.      }
  304. /*
  305.     if (x_draw_mouse_action)
  306.     { x_draw_mouse_action(mouse_last_xreal,mouse_last_yreal);
  307.       x_draw_mouse_action(mouse_xreal,mouse_yreal);
  308.      }
  309. */
  310.  
  311.     mouse_last_xpix = mouse_xpix;
  312.     mouse_last_ypix = mouse_ypix;
  313.           
  314.     mouse_last_xreal = mouse_xreal;
  315.     mouse_last_yreal = mouse_yreal;
  316.  
  317.    }
  318.  
  319.   if (x_draw_have_mouse)
  320.    while (regs.x.bx != 0)
  321.     { regs.x.ax=03;
  322.       int86(0x33,®s,®s);
  323.      }
  324.    
  325.    /* if (!x_draw_have_mouse)  */
  326.  
  327.    draw_cursor(mouse_xpix,mouse_ypix);
  328.    action(mouse_xreal,mouse_yreal);
  329.  
  330. /*
  331.     if (x_draw_mouse_action)  
  332.        x_draw_mouse_action(mouse_xreal,mouse_yreal);
  333. */
  334.    
  335.     if (x_draw_grid_mode) x_draw_cursor();
  336.    
  337.    
  338.   *x = mouse_xreal;
  339.   *y = mouse_yreal;
  340.  
  341.  setwritemode(COPY_PUT);
  342.  
  343.  switch (taste) {
  344.  case 1:  return 1;
  345.  case 4:  return 2;
  346.  case 2:  return 3;
  347.  default: return taste;
  348.  }
  349.  
  350. }
  351.  
  352.  
  353. int x_read_mouse(kind, xstart,ystart,x,y)
  354. int    kind;   /* 0: point, 1: segment, 2:rectangle, 3: circle */
  355. double xstart;
  356. double ystart;
  357. double *x;
  358. double *y;
  359.   PMA f;
  360.   int key;
  361.  
  362.   switch(kind) {
  363.  
  364.   case 0 : f = 0; /* x_draw_mouse_default_action; */
  365.           break;
  366.  
  367.   case 1:  f = x_draw_mouse_segment_action;
  368.            break;
  369.  
  370.   case 2:  f = x_draw_mouse_rect_action;
  371.            break;
  372.  
  373.   case 3:  f = x_draw_mouse_circle_action;
  374.            break;
  375.  
  376.   default: f = x_draw_mouse_default_action;
  377.            break;
  378.  
  379.   }
  380.  
  381.   key = x_read_mouse_action(f,xstart,ystart,x,y);
  382.  
  383.   return key;
  384.  
  385. }
  386.  
  387.  
  388. void x_draw_init_window(w_width,w_height,w_xpos,w_ypos,frame_label)
  389. int w_width;
  390. int w_height;
  391. int w_xpos;
  392. int w_ypos;
  393. char* frame_label;
  394.   int  i;
  395.   union REGS regs;
  396.   int fehler;
  397.   int xasp, yasp;
  398.  
  399.  
  400.  // ***** Initialisierung des Grafikfensters
  401.  
  402.    int gdriver=DETECT, gmode, errorcode;
  403.    //initgraph(&gdriver,&gmode,"\\TC\\BGI\\");
  404.  
  405.    initgraph(&gdriver,&gmode,"\\BORLANDC\\BGI\\");
  406.    errorcode=graphresult();
  407.  
  408.    if(errorcode != grOk)
  409.    {  printf(grapherrormsg(errorcode));
  410.       exit(1);
  411.    }
  412.  
  413.  // ***** Initialisierung des Mousetreibers
  414.    regs.x.ax=0;
  415.    int86(0x33,®s,®s);
  416.    fehler=regs.x.ax;
  417.    if (fehler != 0x0ffff)
  418.       x_draw_have_mouse = 0;
  419.     else
  420.       x_draw_have_mouse = 1;
  421.  
  422.  
  423.   strcpy(default_frame_label,frame_label);
  424.  
  425.   x_draw_depth = 1;
  426.  
  427.   x_draw_set_line_style(0);
  428.   x_draw_set_line_width(1);
  429.   x_draw_set_mode(0);
  430.   x_draw_set_node_width(12);
  431.   x_draw_set_text_mode(0); 
  432.  
  433.   x_draw_color = WHITE;
  434.  
  435.   x_draw_xmin = 0;
  436.   x_draw_ymin = 0;
  437.   x_draw_xmax = 0;
  438.   x_draw_ymax = 0;
  439.   x_draw_scale = 1;
  440.   y_draw_scale = 1;
  441.  
  442.   mouse_xpix = 0;
  443.   mouse_ypix = 0;
  444.   mouse_last_xpix = 0;
  445.   mouse_last_ypix = 0;
  446.  
  447.   getaspectratio(&xasp,&yasp);
  448.   x_draw_aspect_ratio = (float)xasp/(float)yasp;
  449.  
  450.   setfillstyle(SOLID_FILL,WHITE);
  451.  
  452.   if (! x_draw_have_mouse)
  453.      x_draw_acknowledge("NO MOUSE ! (use cursor keys)");
  454. }
  455.  
  456. void x_draw_init(x0,x1,y0,g_mode)
  457. double x0,x1,y0;
  458. int g_mode;
  459. {
  460.   if (x0>=x1) 
  461.   {fprintf(stderr,"Illegal arguments in draw_init: x0 (%f) >= x1 (%f)\n",x0,x1);
  462.    abort();
  463.    }
  464.  
  465.   x_draw_grid_mode = g_mode; 
  466.  
  467.   x_draw_reset_frame_label();
  468.  
  469.   xdots = getmaxx();
  470.   ydots = getmaxy();
  471.  
  472.  
  473.   x_draw_scale = ((double)xdots)/(x1-x0);
  474.   y_draw_scale = x_draw_scale * x_draw_aspect_ratio;
  475.  
  476.   /* at least grid distance of 2 pixels */
  477.   if ((x_draw_grid_mode) && (x_draw_grid_mode*x_draw_scale < 2)) 
  478.     x_draw_grid_mode=0;  
  479.  
  480.   if (x_draw_grid_mode) 
  481.   { if (x_draw_scale < 1) x_draw_scale = 1;
  482.     else x_draw_scale = (int)x_draw_scale;
  483.    }
  484.  
  485.   x_draw_xmin = x0;
  486.   x_draw_ymin = y0;
  487.   x_draw_xmax = x0+xdots/x_draw_scale;
  488.   x_draw_ymax = y0+ydots/y_draw_scale;
  489.  
  490.   xorigin = -x0*x_draw_scale;
  491.   yorigin = ydots+y0*y_draw_scale;
  492.  
  493.   mouse_xpix = mouse_last_xpix = getmaxx()/2;
  494.   mouse_ypix = mouse_last_ypix = getmaxy()/2;
  495.  
  496.   mouse_xreal = XREAL(mouse_xpix);
  497.   mouse_yreal = YREAL(mouse_ypix);
  498.  
  499.   x_draw_clear(0);   
  500.  
  501.   if (x_draw_grid_mode)  x_draw_cursor();
  502.  
  503.   if (x_draw_redraw) (*x_draw_redraw)();
  504.  
  505. }
  506.  
  507.  
  508. void x_show_window() 
  509. { }
  510.  
  511. void x_draw_end() { closegraph(); }
  512.  
  513. void x_draw_line(x1, y1, x2, y2, col)
  514. double x1,y1,x2,y2;
  515. int col;
  516. { x_draw_set_color(col);
  517.   line(XPIX(x1), YPIX(y1), XPIX(x2), YPIX(y2));
  518.  }
  519.  
  520.  
  521. void x_draw_point(x,y,col)
  522. double x,y;
  523. int col;
  524. { int X = XPIX(x);
  525.   int Y = YPIX(y);
  526.   x_draw_set_color(col);
  527.   setlinestyle(x_draw_line_style,0,1);
  528.   line(X-2,Y-2,X+2,Y+2);
  529.   line(X-2,Y+2,X+2,Y-2);
  530.   setlinestyle(x_draw_line_style,0,x_draw_line_width);
  531.  }
  532.  
  533. void x_draw_arc(x0,y0,a,b,col)
  534. double x0,y0;
  535. int a,b,col;
  536. { x_draw_set_color(col);
  537.   ellipse(XPIX(x0)-a,YPIX(y0)-b,0,360,2*a,2*b);
  538. }
  539.  
  540. void x_draw_filled_arc(x0,y0,a,b,col)
  541. double x0,y0;
  542. int a,b,col;
  543. { x_draw_set_color(col);
  544.   fillellipse(XPIX(x0)-a,YPIX(y0)-b,2*a,2*b);
  545. }
  546.  
  547. void x_draw_node(x0,y0,col)
  548. double x0,y0;
  549. int col;
  550. { int save = x_draw_set_line_width(1); 
  551.   x_draw_set_color(col);
  552.   circle(XPIX(x0),YPIX(y0),2*x_draw_node_width); 
  553.   x_draw_set_line_width(save); 
  554.  }
  555.  
  556. void x_draw_filled_node(x0,y0,col)
  557. double x0,y0;
  558. int col;
  559. { x_draw_set_color(col);
  560.   fillellipse(XPIX(x0),YPIX(y0),2*x_draw_node_width,
  561.                                 2*x_draw_node_width*x_draw_aspect_ratio); 
  562.  }
  563.  
  564.  
  565. void x_draw_text_node(x0,y0,s,col)
  566. double x0,y0;
  567. int col;
  568. char* s;
  569.   int save = x_draw_text_mode;
  570.   x_draw_text_mode = 0;  /* transparent */
  571.  
  572.   if (x_draw_depth==1 || col == 1)
  573.      x_draw_node(x0,y0,1);
  574.   else
  575.      x_draw_filled_node(x0,y0,col);
  576.  
  577.   x_draw_ctext(x0,y0,s,1);
  578.   x_draw_text_mode = save;
  579.  
  580.  }
  581.  
  582. void x_draw_int_node(x0,y0,i,col)
  583. double x0,y0;
  584. int i,col;
  585. { char buf[16];
  586.   sprintf(buf,"%d",i);
  587.   x_draw_text_node(x0,y0,buf,col);
  588.  }
  589.  
  590. void x_draw_ellipse(x0,y0,a,b,col)
  591. double x0,y0,a,b; 
  592. int col;
  593. { int A = (int)(a*x_draw_scale);
  594.   int B = (int)(b*y_draw_scale);
  595.   x_draw_arc(x0,y0,A,B,col); }
  596.  
  597. void x_draw_filled_ellipse(x0,y0,a,b,col)
  598. double x0,y0,a,b; 
  599. int col;
  600. { int A = (int)(a*x_draw_scale);
  601.   int B = (int)(b*y_draw_scale);
  602.   x_draw_filled_arc(x0,y0,A,B,col); }
  603.  
  604.  
  605. void x_draw_circle(x0,y0,r,col)
  606. double x0,y0,r; 
  607. int col;
  608. { int R = (int)(r*x_draw_scale);
  609.   x_draw_set_color(col);
  610.   circle(XPIX(x0),YPIX(y0),R); 
  611.  }
  612.  
  613.  
  614. void x_draw_filled_circle(x0,y0,r,col)
  615. double x0,y0,r; 
  616. int col;
  617. { int A = (int)(r*x_draw_scale);
  618.   int B = (int)(r*y_draw_scale);
  619.   x_draw_set_color(col);
  620.   fillellipse(XPIX(x0),YPIX(y0),A,B); 
  621.  }
  622.  
  623.  
  624. int x_draw_xpix(x)
  625. double x;
  626. { return XPIX(x); }
  627.  
  628. int x_draw_ypix(x)
  629. double x;
  630. { return YPIX(x); }
  631.  
  632. void x_draw_pix(x,y,col)
  633. double x,y;
  634. int col;
  635.   putpixel(XPIX(x),YPIX(y),col);
  636.  }
  637.  
  638.  
  639. void x_draw_plot_xy(x0,x1,f,col)
  640. double x0,x1;
  641. double (*f)();
  642. int col;
  643. { } 
  644.  
  645. void x_draw_plot_yx(y0,y1,f,col)
  646. double y0,y1;
  647. double (*f)();
  648. int col;
  649. { }
  650.  
  651.  
  652. void x_draw_filled_polygon(n,xcoord,ycoord,col)
  653. int col, n;
  654. double *xcoord, *ycoord;
  655. {
  656.  int* points;
  657.  int  i;
  658.  
  659.  points = (int *) malloc(2*n*sizeof(int));
  660.  
  661.  for (i=0; i<n; i++)
  662.  { points[2*i]   = XPIX(xcoord[i]);
  663.    points[2*i+1] = YPIX(ycoord[i]);
  664.   }
  665.  
  666.  x_draw_set_color(col);
  667.  
  668.  fillpoly(n,points);
  669.  
  670.  free((char*)points);
  671.  
  672.  }
  673.  
  674. void x_draw_polygon(n,xcoord,ycoord,col)
  675. int col, n;
  676. double *xcoord, *ycoord;
  677. {
  678.  int* points;
  679.  int  i;
  680.  
  681.  points = (int *) malloc(2*(n+1)*sizeof(int));
  682.  
  683.  for (i=0; i<n; i++)
  684.  { points[2*i]   = XPIX(xcoord[i]);
  685.    points[2*i+1] = YPIX(ycoord[i]);
  686.   }
  687.  
  688.  points[2*n]   = points[0];
  689.  points[2*n+1] = points[1];
  690.  
  691.  x_draw_set_color(col);
  692.  
  693.  drawpoly(n+1,points);
  694.  
  695.  free((char*)points);
  696.  
  697.  }
  698.  
  699. void x_draw_rectangle(x1,y1,x2,y2,col)
  700. double x1,y1,x2,y2;
  701. int col;
  702. {
  703.  x_draw_line(x1,y1,x1,y2,col);
  704.  x_draw_line(x1,y2,x2,y2,col);
  705.  x_draw_line(x2,y2,x2,y1,col);
  706.  x_draw_line(x2,y1,x1,y1,col);
  707.  
  708.  }
  709.  
  710.  
  711. void x_draw_filled_rectangle(x1,y1,x2,y2,col)
  712. double x1,y1,x2,y2; 
  713. int col;
  714. { double xcoord[4], ycoord[4];
  715.  
  716.  xcoord[0] = x1;   
  717.  ycoord[0] = y1;   
  718.  xcoord[1] = x1;  
  719.  ycoord[1] = y2;   
  720.  xcoord[2] = x2; 
  721.  ycoord[2] = y2;   
  722.  xcoord[3] = x2;
  723.  ycoord[3] = y1;
  724.  
  725.  x_draw_filled_polygon(4,xcoord,ycoord,col);
  726.  
  727.  }
  728.  
  729.  
  730. void x_draw_copy_rect(x1,y1,x2,y2,x,y)
  731. double x1,y1,x2,y2,x,y;
  732. { int X1 = XPIX(x1);
  733.   int Y1 = XPIX(y1);
  734.   int X2 = XPIX(x2);
  735.   int Y2 = XPIX(y2);
  736.   int p[16000];
  737.   if (X1 > X2 || Y1 > Y2) return;
  738.   if ((X2-X1+1)*(Y2-Y1+1)/8 > 16000) return;
  739.   getimage(X1,Y1,X2,Y2,p);
  740.   putimage(XPIX(x),YPIX(y),p,COPY_PUT);
  741. }
  742.  
  743.  
  744. /*
  745. void x_draw_move_rect(x1,y1,x2,y2,x,y)
  746. double x1,y1,x2,y2,x,y;
  747. {}
  748. */
  749.  
  750.  
  751.  
  752. void x_draw_clear(col)
  753. int col;
  754. { clearviewport(); 
  755.   setcolor(WHITE);
  756.   outtextxy(30,1,frame_label);
  757.  } 
  758.  
  759.  
  760. /* TEXT  */
  761.  
  762. static void x_draw_clear_text(x,y,s,center)
  763. int x,y;
  764. char * s;
  765. int center;
  766. { int width = textwidth(s);
  767.   int height = textheight(s);
  768.   int frame[10];
  769.   if (center != 0)
  770.   { frame[0] = x-width/2;
  771.     frame[1] = y-height/2;
  772.    }
  773.   else
  774.   { frame[0]=x; 
  775.     frame[1]=y;
  776.    }
  777.   frame[2]=frame[0]+width; 
  778.   frame[3]=frame[1];
  779.   frame[4]=frame[0]+width; 
  780.   frame[5]=frame[1]+height;
  781.   frame[6]=frame[0]; 
  782.   frame[7]=frame[1]+height;
  783.   frame[8]=frame[0]; 
  784.   frame[9]=frame[1];
  785.  
  786.  setcolor(BLACK);
  787.  setfillstyle(SOLID_FILL,BLACK);
  788.  fillpoly(5,frame);
  789. }
  790.   
  791.  
  792. void x_draw_message(line,s,col)
  793. int line;
  794. char * s;
  795. int col;
  796. { int x = 5*textwidth("H");
  797.   int y = (int)(1.25*textheight("H")*(line+1));
  798.   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
  799.   settextjustify(LEFT_TEXT,TOP_TEXT);
  800.   setcolor(WHITE);
  801.   outtextxy(x,y,s);
  802.  }
  803.  
  804. void x_draw_ctext(x,y,s,col)
  805. double x,y;
  806. char * s;
  807. int col;
  808. { if (x_draw_text_mode != 0) x_draw_clear_text(x,y,s,1);
  809.   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
  810.   settextjustify(CENTER_TEXT,CENTER_TEXT);
  811.   x_draw_set_color(col);
  812.   outtextxy(x,y,s);
  813. }
  814.  
  815. void x_draw_text(x,y,s,col)
  816. double x,y;
  817. char * s;
  818. int col;
  819. { if (x_draw_text_mode != 0) x_draw_clear_text(x,y,s,0);
  820.   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
  821.   settextjustify(LEFT_TEXT,TOP_TEXT);
  822.   x_draw_set_color(col);
  823.   outtextxy(XPIX(x),YPIX(y),s);
  824. }
  825.  
  826. void x_draw_int(x0,y0,i,col)
  827. double x0,y0;
  828. int i,col;
  829. { char buf[16];
  830.   sprintf(buf,"%d",i);
  831.   x_draw_ctext(x0,y0,buf,col);
  832.  }
  833.  
  834.  
  835. void x_draw_cursor()
  836.   int X,Y;
  837.  
  838.   X = XPIX(mouse_xreal);
  839.   Y = YPIX(mouse_yreal);
  840.  
  841.   line(X,Y,X+10,Y);
  842.   line(X,Y,X-10,Y);
  843.   line(X,Y,X,Y+10);
  844.   line(X,Y,X,Y-10);
  845.  
  846. }
  847.  
  848.  
  849.  
  850. int x_draw_confirm(header)
  851. char *header;
  852. { /* ask for yes or no */
  853.   char* s[2];
  854.   s[0] = "NO";
  855.   s[1] = "YES";
  856.   return x_draw_read_panel(header,2,s,0);
  857. }
  858.  
  859. int x_draw_acknowledge(header)
  860. char *header;
  861. { /* ask for ok */
  862.   char* s[1];
  863.   s[0] = "OK";
  864.  
  865.   mouse_last_xpix = mouse_xpix = getmaxx()/2;
  866.   mouse_last_ypix = mouse_ypix = getmaxy()/2 + 10;
  867.  
  868.   return x_draw_read_panel(header,1,s,0);
  869. }
  870.  
  871. int x_draw_screen_width() { return(getmaxx()); }
  872.  
  873.  
  874. int x_draw_screen_height() { return(getmaxx()); }
  875.  
  876. static void x_draw_putch(x,y,ch,col)
  877. int x;
  878. int y;
  879. char ch;
  880. int col;
  881. { char out[2];
  882.   out[0] = ch;
  883.   out[1] = 0;
  884.   setcolor(col);
  885.   outtextxy(x,y,out);
  886. }
  887.  
  888. char* x_draw_read_text_panel(message,menu_label,n,items)
  889. char    *message;
  890. char    *menu_label;
  891. int      n;
  892. char   **items;
  893. { union REGS regs;
  894.  
  895.   int i;
  896.   int tw,mw;
  897.   int x1,x2,y1,y2;
  898.   int p[5000];
  899.   char c;
  900.  
  901.   int cw = textwidth("H");
  902.  
  903.   int loop=0;
  904.   char* result = (char*)malloc(32);
  905.   int maxx=getmaxx();
  906.   int maxy=getmaxy();
  907.   int save_color=getcolor();
  908.   int poly[10];
  909.  
  910.   n = 0;
  911.   menu_label = "";
  912.   items = 0;
  913.  
  914.   if (strlen(message) == 0) message = "STRING INPUT";
  915.   if (strlen(message) > 31) message[31] = 0;
  916.  
  917.   mw = textwidth(message);
  918.   tw = 24*cw;
  919.  
  920.   setcolor(BLACK);
  921.  
  922.   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
  923.   settextjustify(LEFT_TEXT,TOP_TEXT);
  924.   setwritemode(COPY_PUT);
  925.  
  926.   x1 = (maxx-tw-mw-30)/2;
  927.   x2 = x1+tw+mw+30;
  928.   y1 = maxy/2-15;
  929.   y2 = maxy/2+15;
  930.  
  931.   getimage(x1,y1,x2,y2,p);
  932.   poly[0]=x1; poly[1]=y1;
  933.   poly[2]=x2; poly[3]=y1;
  934.   poly[4]=x2; poly[5]=y2;
  935.   poly[6]=x1; poly[7]=y2;
  936.   poly[8]=x1; poly[9]=y1;
  937.   setfillstyle(SOLID_FILL,LIGHTBLUE);
  938.   fillpoly(5,poly);
  939.   outtextxy(x1+10,y1+12,message);
  940.  
  941.   line(x1+10,y1+20,x2-10,y1+20);
  942.  
  943.   x2 = x1 + mw + 20;
  944.   y2 = y1+12;
  945.  
  946.  
  947.   x_draw_putch(x2,y2,'|',BLACK);
  948.  
  949.   c=getch();
  950.  
  951.   while(c != 13)
  952.   { int x = x2+loop*cw;
  953.     if (loop < 23 && isprint(c))
  954.     { result[loop]=c;
  955.       x_draw_putch(x,y2,'|',LIGHTBLUE);
  956.       x_draw_putch(x,y2,c,BLACK);
  957.       loop++;
  958.       x_draw_putch(x+cw,y2,'|',BLACK);
  959.     }
  960.     if(c==8 && loop>0)
  961.     { loop--;
  962.       x_draw_putch(x,y2,'|',LIGHTBLUE);
  963.       x_draw_putch(x-cw,y2,result[loop],LIGHTBLUE);
  964.       x_draw_putch(x-cw,y2,'|',BLACK);
  965.     }
  966.     c=getch();
  967.   }
  968.  
  969.   result[loop]='\0';
  970.  
  971.   putimage(x1,y1,p,COPY_PUT);
  972.  
  973.   setcolor(save_color);
  974.  
  975.   return result;
  976. }
  977.  
  978. int x_draw_read_panel(message,n,labels,vertical)
  979. char    *message;
  980. int n;
  981. char    **labels;
  982. int    vertical;
  983.   union REGS regs;
  984.   unsigned int size;
  985.   int answer= -1;
  986.   int save_color;
  987.   int i;
  988.   int panel_width;
  989.   int frame[10],but[10];
  990.   int buffer[6000];
  991.  
  992.   int L[16]; 
  993.   int R[16]; 
  994.  
  995.   int panel_height = 54;
  996.   int mes_y = 10;
  997.   int y0 = 30;
  998.   int xskip  = 30;
  999.   int xspace = 5;
  1000.   int yspace = 3;
  1001.   int panel_y = (getmaxy()-panel_height)/2;
  1002.   int panel_x;
  1003.   int mes_x;
  1004.   int mes_width = textwidth(message);
  1005.  
  1006.   int y1 = y0 + textheight("H") + 2*yspace;
  1007.  
  1008.   if (vertical !=0 ) vertical = 0;
  1009.  
  1010.   panel_width = xskip;
  1011.  
  1012.   for(i=0;i<n;i++)
  1013.   { L[i] = panel_width;
  1014.     panel_width += 2*xspace+textwidth(labels[i]);
  1015.     R[i] = panel_width;
  1016.     panel_width += xskip;
  1017.    }
  1018.  
  1019.  
  1020.   mes_x = (panel_width-mes_width)/2;
  1021.  
  1022.   if (mes_x < xskip)
  1023.   { int d = xskip - mes_x;
  1024.     for(i=0;i<n;i++)
  1025.     { L[i] += d;
  1026.       R[i] += d;
  1027.      }
  1028.     panel_width += 2*d;
  1029.     mes_x = xskip;
  1030.   }
  1031.  
  1032.   panel_x = (getmaxx()-panel_width)/2;
  1033.  
  1034.   frame[0]=panel_x; 
  1035.   frame[1]=panel_y;
  1036.   frame[2]=panel_x+panel_width; 
  1037.   frame[3]=panel_y;
  1038.   frame[4]=panel_x+panel_width; 
  1039.   frame[5]=panel_y+panel_height;
  1040.   frame[6]=panel_x; 
  1041.   frame[7]=panel_y+panel_height;
  1042.   frame[8]=panel_x; 
  1043.   frame[9]=panel_y;
  1044.  
  1045.   /* save picture */
  1046.  
  1047.   getimage(frame[0],frame[1],frame[4],frame[5],buffer);
  1048.  
  1049.  
  1050.   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
  1051.   settextjustify(LEFT_TEXT,TOP_TEXT);
  1052.   setwritemode(COPY_PUT);
  1053.   setlinestyle(SOLID_LINE,0,2);
  1054.  
  1055.   save_color = getcolor();
  1056.   setcolor(LIGHTBLUE); 
  1057.   setfillstyle(SOLID_FILL,LIGHTBLUE);
  1058.  
  1059.   fillpoly(5,frame);
  1060.  
  1061.   setcolor(BLACK);
  1062.   for (i=0;i<n;i++)
  1063.   { rectangle(panel_x+L[i],panel_y+y0,panel_x+R[i],panel_y+y1);
  1064.     outtextxy(panel_x+L[i]+xspace,panel_y+y0+yspace,labels[i]);
  1065.    }
  1066.  
  1067.   outtextxy(panel_x+mes_x,panel_y+mes_y,message);
  1068.  
  1069.   setcolor(save_color);
  1070.  
  1071.   while (answer == -1)
  1072.   { 
  1073.     double x,y;
  1074.     int xpix,ypix;
  1075.  
  1076.     x_read_mouse(0,0.0,0.0,&x,&y);
  1077.  
  1078.     xpix = XPIX(x) - panel_x;
  1079.     ypix = YPIX(y) - panel_y;
  1080.  
  1081.     if ( y0 < ypix  && ypix < y1) 
  1082.     { int i = 0;
  1083.       while (i < n && R[i] < xpix) i++;
  1084.       if (i < n && L[i] < xpix)  answer = i;
  1085.      }
  1086.    }
  1087.  
  1088.  /* highlight pressed button */
  1089.  
  1090.  but[0] = panel_x+L[answer];
  1091.  but[1] = panel_y+y0;
  1092.  but[2] = panel_x+R[answer];
  1093.  but[3] = panel_y+y0;
  1094.  but[4] = panel_x+R[answer];
  1095.  but[5] = panel_y+y1;
  1096.  but[6] = panel_x+L[answer];
  1097.  but[7] = panel_y+y1;
  1098.  but[8] = panel_x+L[answer];
  1099.  but[9] = panel_y+y0;
  1100.  
  1101.  setfillstyle(SOLID_FILL,BLACK);
  1102.  fillpoly(5,but);
  1103.  outtextxy(panel_x+L[answer]+xspace,panel_y+y0+yspace,labels[answer]);
  1104.  setfillstyle(SOLID_FILL,LIGHTBLUE);
  1105.  delay(200);
  1106.  
  1107.   putimage(frame[0],frame[1],buffer,COPY_PUT);
  1108.  
  1109.   return answer;
  1110. }
  1111.  
  1112. void x_draw_message_panel(argc,argv)
  1113. int argc;
  1114. char** argv;
  1115. { }
  1116.  
  1117. void x_draw_notice()
  1118. { }
  1119.