home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / d / dir_nm20.zip / Dir_NM_2.0 / NM-Tool.c < prev    next >
C/C++ Source or Header  |  1992-07-16  |  10KB  |  438 lines

  1. /*                           ===  IM-Tool.c  === 
  2.  *
  3.  * int  Rand_Num(Scaler)
  4.  * int  KeyPressed()
  5.  * char GetKey()
  6.  * char getkey()
  7.  * char getch()
  8.  * char *Sub_Str(string,head,length)
  9.  * void Str_Append(Str_head,Str_tail,Length)
  10.  * void SetColor(color)
  11.  * void Line(x1,y1,x2,y2)
  12.  * void DrawRectangle(x_start,y_start,width,height)
  13.  * void FillRectangle(x_start,y_start,width,height)
  14.  * void Rub(x1,y1,width,height,color)
  15.  * void OutTextXY(x,y,string)
  16.  * void DrawEllipse(x,y,width,height,angle1,angle2)
  17.  * void FillEllipse(x,y,width,height,angle1,angle2)
  18.  * void Use_Font(font_name)
  19.  * void Out_I_XY(x,y,I)
  20.  * void Out_D_XY(x,y,D)
  21.  * void Dis(lock)
  22.  * void highlight(x_st,y_st,width,height)
  23.  * void readat(x,y,n,k)
  24.  * void Addline(x,y,z,At_high)
  25.  * void Underline(x,z,n)
  26.  * void Draw_Dot(x,y)
  27.  * void Point(x,y)
  28.  * void Dot(x,y)
  29.  * void Sharp(x,y)
  30.  * void Flat(x,y)
  31.  * void Normal(x,y)
  32.  * void HI_mark(i)
  33.  * void LO_mark(i)
  34.  * void Underline_(x,y,n)
  35.  * void L_Arrow(x,y)
  36.  * void R_Arrow(x,y)
  37.  * void U_Arrow(x,y)
  38.  * void D_Arrow(x,y)
  39.  * void Notice(x,y,dx,dy,on,On_Color,Off_Color)
  40.  *
  41.  */
  42.  
  43. #include "NM.h"
  44.  
  45. int Rand_Num(Scaler)
  46.   int Scaler;
  47. {
  48.   struct timeval tp;
  49.   struct timezone tzp;
  50.  
  51.   if (gettimeofday(&tp,&tzp)==0) srand(tp.tv_sec % 60);else srand(6);
  52.   return rand()%Scaler;
  53. }
  54.  
  55. int KeyPressed()
  56. {
  57.   XEvent report;
  58.  
  59.   XNextEvent(display,&report);
  60.   if (report.type==KeyPress) return (1);else return (0); 
  61. }
  62.  
  63. char GetKey()
  64. {
  65.   XEvent report;
  66.   KeySym ks;
  67.  
  68.   while(1)
  69.     {
  70.       XNextEvent(display,&report);
  71.       if (report.type==KeyPress)
  72.     {
  73.       ks=XLookupKeysym(report,0);
  74.       switch(ks)
  75.         {
  76.           case XK_F1:return '*';
  77.           case XK_F2:return '/';
  78.           case XK_F3:return '+';
  79.           case XK_F4:return '=';
  80.           case XK_F5:return '[';
  81.           case XK_F6:return ']';
  82.           case XK_Return:return '!';
  83.           case XK_Escape:return '&';
  84.           case XK_Left:return '<';
  85.           case XK_Right:return '>';
  86.           case XK_Up:return '?';
  87.           case XK_Down:return '@';
  88.           case 65496:return '#';          /*  Home  */
  89.           case 65502:return '%';          /*  End   */
  90.           case 65498:return '$';          /*  PgUp  */
  91.           case 65504:return '^';          /*  PgDn  */
  92.           case XK_Num_Lock:return '|';
  93.           case XK_Insert:return '{';
  94.           case XK_Delete:return '}';
  95.           default:XLookupString(report,tmpstr,128,&ks,0);
  96.               if (isalpha(tmpstr[0])) return (toupper(tmpstr[0]));
  97.               else return (tmpstr[0]);
  98.         }
  99.     }
  100.     }
  101. }
  102.  
  103. char getkey()
  104. {
  105.   XEvent report;
  106.   KeySym ks;
  107.  
  108.   while(1)
  109.     {
  110.       XNextEvent(display,&report);
  111.       if (report.type==KeyPress)
  112.     {
  113.       ks=XLookupKeysym(report,0);
  114.       switch(ks)
  115.         {
  116.           case XK_F1:return '*';
  117.           case XK_F2:return '/';
  118.           case XK_F3:return '+';
  119.           case XK_F4:return '=';
  120.           case XK_F5:return '[';
  121.           case XK_F6:return ']';
  122.           case XK_Return:return '!';
  123.           case XK_Escape:return '&';
  124.           case XK_Left:return '<';
  125.           case XK_Right:return '>';
  126.           case XK_Up:return '?';
  127.           case XK_Down:return '@';
  128.           case 65496:return '#';          /*  Home  */
  129.           case 65502:return '%';          /*  End   */
  130.           case 65498:return '$';          /*  PgUp  */
  131.           case 65504:return '^';          /*  PgDn  */
  132.           case XK_Num_Lock:return '|';
  133.           case XK_Insert:return '{';
  134.           case XK_Delete:return '}';
  135.           default:XLookupString(report,tmpstr,128,&ks,0);return (tmpstr[0]);
  136.         }
  137.     }
  138.     }
  139. }
  140.  
  141. char getch()
  142. {
  143.   XEvent report;
  144.   KeySym ks;
  145.  
  146.   while(1)
  147.     {
  148.       XNextEvent(display,&report);
  149.       if (report.type==KeyPress)
  150.     { XLookupString(report,tmpstr,1,&ks,0);return (tmpstr[0]); }
  151.     }
  152. }
  153.  
  154. char *Sub_Str(string,head,length)
  155.   char *string;
  156.   int head,length;
  157. {
  158.   char *sub;
  159.   int i;
  160.  
  161.   sub=(char *)malloc(5000);*sub='\0';i=-1;
  162.   if (strlen(string)>head && string[head]!='\n')
  163.     { 
  164.       do 
  165.     { 
  166.       i++;sprintf(sub+strlen(sub),"%c",string[head+i]);
  167.     } while (i<length-1 && string[head+i]!='\n');
  168.     }
  169.   return sub;
  170. }
  171.  
  172. void Str_Append(Str_head,Str_tail,Length)
  173.   char Str_head[256],Str_tail[256];
  174.   int Length;
  175. {
  176.   if (Length<2) sprintf(Str_head+strlen(Str_head),"%c",Str_tail); 
  177.   else sprintf(Str_head+strlen(Str_head),"%s",Str_tail);
  178. }
  179.  
  180. void SetColor(color)
  181.   char *color;
  182. {
  183.   XAllocNamedColor(display,theCmap,color,&exactcolor,&mycolor);  
  184.   XSetForeground(display,gc,mycolor.pixel);
  185. }
  186.  
  187. void Line(x1,y1,x2,y2)
  188.   int x1,y1,x2,y2;
  189. {
  190.   XDrawLine(display,win,gc,x1,y1,x2,y2);
  191. }
  192.  
  193. void DrawRectangle(x_start,y_start,width,height)
  194.   int x_start,y_start,width,height;
  195. {
  196.   XDrawRectangle(display,win,gc,x_start,y_start,width,height);
  197. }
  198.  
  199. void FillRectangle(x_start,y_start,width,height)
  200.   int x_start,y_start,width,height;
  201. {
  202.   XFillRectangle(display,win,gc,x_start,y_start,width,height);
  203. }
  204.  
  205. void Rub(x1,y1,width,height,color)
  206.   int x1,y1,width,height;
  207. char *color;
  208. {
  209.   SetColor(color);XFillRectangle(display,win,gc,x1,y1,width,height);
  210. }
  211.  
  212. void OutTextXY(x,y,string)
  213.   int x,y;
  214.   char *string;
  215. {
  216.   if (string[strlen(string)-1]=='\n')
  217.     XDrawString(display,win,gc,x,y,string,strlen(string)-1);
  218.   else XDrawString(display,win,gc,x,y,string,strlen(string));
  219. }
  220.  
  221. void DrawEllipse(x,y,width,height,angle1,angle2)
  222.   int x,y,width,height,angle1,angle2;
  223. {
  224.   XDrawArc(display,win,gc,x,y,width,height,angle1*64,angle2*64);
  225. }
  226.  
  227. void FillEllipse(x,y,width,height,angle1,angle2)
  228.   int x,y,width,height,angle1,angle2;
  229. {
  230.   XFillArc(display,win,gc,x,y,width,height,angle1*64,angle2*64);
  231. }
  232.  
  233. void Use_Font(font_name)
  234.   char *font_name;
  235. {
  236.   Font font;
  237.  
  238.   font=XLoadFont(display,font_name);XSetFont(display,gc,font);
  239. }
  240.  
  241. void Out_I_XY(x,y,I)
  242.   int x,y,I;
  243. {
  244.   static char MsgText[12];
  245.   
  246.   (void) sprintf(MsgText,"%i",I);
  247.   XDrawString(display,win,gc,x,y,MsgText,strlen(MsgText));
  248. }
  249.  
  250. void Out_D_XY(x,y,D)
  251.   int x,y;
  252.   double D;
  253. {
  254.   static char MsgText[12];
  255.   
  256.   (void) sprintf(MsgText,"%d.2",D);
  257.   XDrawString(display,win,gc,x,y,MsgText,strlen(MsgText));
  258. }
  259.  
  260. void dis(lock)
  261.   int *lock;
  262. {
  263.   *lock=1-*lock;
  264. }
  265.  
  266. void highlight(x_st,y_st,width,height)
  267.   int x_st,y_st,width,height;
  268. {
  269.   SetColor("LimeGreen");XSetFunction(display,gc,GXxor);
  270.   FillRectangle(x_st,y_st,width,height);
  271.   SetColor("blue");DrawRectangle(x_st,y_st,width-1,height-1);
  272.   XSetFunction(display,gc,GXcopy);
  273. }
  274.  
  275. void readat(x,y,n,k)
  276.   int x,y,n,*k;
  277. {
  278.   char ch,st[7],STR1[256];
  279.   int i;
  280.  
  281.   *st='\0';i=0;if (n>5) n=5;
  282.   do {
  283.        highlight(x+i*15,y-21,16,22);
  284.        do { } while (!(ch=GetKey()));
  285.        if (ch=='-' || isdigit(ch)) 
  286.      {
  287.        if (ch=='-') n++;sprintf(st+strlen(st),"%c",ch);
  288.        sprintf(STR1,"%c",ch);highlight(x+i*15,y-21,16,22);
  289.        SetColor("magenta");OutTextXY(x+i*15,y,STR1);i++;
  290.      }
  291.      } while (ch!='!' && i!=n);
  292.   if (i!=n) highlight(x+i*15,y-21,16,22);
  293.   if (*st=='\0') strcpy(st,"0");*k=atoi(st);
  294. }
  295.  
  296. void Addline(x,Y,z,At_high)
  297.   int x,Y,z,At_high;
  298. {
  299.   int i,k,y;
  300.  
  301.   k=0;if (At_high<2) y=Y-V_Ht/4;else y=Y;
  302.   
  303.   if (At_high==2)
  304.     {
  305.       SetColor("white");if (y<11) k=(12-y)/2;else if (y>21) k=(20-y)/2;
  306.       if (k>0) for (i=0;i<k;i++) Line(x+5,z-i*8+76,x+30,z-i*8+76);
  307.       else if (k<0) for (i=0;i<abs(k);i++) Line(x+5,z+i*8+124,x+30,z+i*8+124);
  308.     }
  309.   else if (At_high==1)
  310.          {
  311.        SetColor("white");if (y<11) k=(12-y)/2;else if (y>21) k=(20-y)/2;
  312.        if (k>0) 
  313.          for (i=0;i<k;i++) Line(x+5,z-i*8+76+V_Ht,x+30,z-i*8+76+V_Ht);
  314.        else if (k<0) 
  315.               for (i=0;i<abs(k);i++) 
  316.             Line(x+5,z+i*8+124+V_Ht,x+30,z+i*8+124+V_Ht);
  317.      }
  318.        else if (At_high==0)
  319.           {
  320.         SetColor("grey");if (y<31) k=(32-y)/2;else if (y>41) k=(40-y)/2;
  321.         if (k>0) 
  322.           for (i=0;i<k;i++) 
  323.             Line(x+5,z-i*8+156+V_Ht,x+30,z-i*8+156+V_Ht);
  324.         else if (k<0) 
  325.                        for (i=0;i<abs(k);i++) 
  326.              Line(x+5,z+i*8+204+V_Ht,x+30,z+i*8+204+V_Ht);
  327.      }
  328. }
  329.  
  330. void Underline(x,z,n)
  331.   int x,z,n;
  332. {
  333.   int k;
  334.  
  335.   if (n<0) { n=-n;SetColor("MediumBlue"); } else SetColor("gold");
  336.   for (k=0;k<n;k++) Line(x*20+3,z+k*2+44,x*20+15,z+k*2+44);
  337. }
  338.  
  339. void Draw_Dot(x,y)
  340.   int x,y;
  341. {
  342.   FillEllipse(x,y,2,2,0,360*64);
  343. }
  344.  
  345. void Point(x,y)
  346.   int x,y;
  347. {
  348.   FillEllipse(x,y,6,6,0,360*64);
  349. }
  350.  
  351. void Dot(x,y)
  352.   int x,y;
  353. {
  354.   SetColor("ivory");XSetFunction(display,gc,GXxor);
  355.   FillEllipse(x,y,7,8,0,360*64);XSetFunction(display,gc,GXcopy);
  356. }
  357.  
  358. void Sharp(x,y)
  359.   int x,y;
  360. {
  361.   Line(x,y,x+7,y);Line(x,y+6,x+7,y+6);Line(x+2,y-3,x+2,y+9);
  362.   Line(x+5,y-3,x+5,y+9);
  363. }
  364.  
  365. void Flat(x,y)
  366.   int x,y;
  367. {
  368.   Line(x,y,x,y+12);Line(x+1,y+7,x+5,y+7);Line(x+5,y+7,x+5,y+10);
  369.   Line(x,y+12,x+5,y+10);
  370. }
  371.  
  372. void Normal(x,y)
  373.   int x,y;
  374. {
  375.   Line(x,y,x,y+14);Line(x,y+9,x+4,y+6);Line(x,y+12,x+4,y+9);
  376.   Line(x+4,y+4,x+4,y+18); 
  377. }
  378.  
  379. void HI_mark(i)
  380.   int i;
  381. {
  382.   DrawEllipse(19,i-7,12,11,0,240);DrawEllipse(14,i-12,18,20,0,-270);
  383.   DrawEllipse(6,i-40,22,30,60,-120);DrawEllipse(6,i-41,22,30,60,-120); 
  384.   DrawEllipse(19,i-40,16,30,-123,-127);DrawEllipse(19,i-39,16,30,-123,-127);
  385.   Line(21,i-16,25,i+14);Line(20,i-16,26,i+14);DrawEllipse(17,i+11,8,9,30,-180);
  386.   DrawEllipse(18,i+11,8,9,30,-180);Point(15,i+13);
  387. }
  388.  
  389. void LO_mark(i)
  390.   int i;
  391. {
  392.   Point(15,i+1);DrawEllipse(16,i-2,12,16,0,180);DrawEllipse(8,i-10,20,32,0,-90);
  393.   DrawEllipse(16,i-1,12,16,0,180);DrawEllipse(8,i-9,20,32,0,-90);
  394.   FillEllipse(32,i-3,5,5,0,360*64);FillEllipse(32,i+5,5,5,0,360*64);
  395. }
  396.  
  397. void Underline_(x,y,n)
  398.   int x,y,n;
  399. {
  400.   int k;
  401.  
  402.   for (k=0;k<n;k++) Line(x-2,y+k*4,x+15,y+k*4);
  403. }
  404.  
  405. void L_Arrow(x,y)
  406.   int x,y;
  407. {
  408.   Line(x,y,x+5,y-3);Line(x,y,x+5,y+3);Line(x-1,y,x+13,y);
  409. }
  410.  
  411. void R_Arrow(x,y)
  412.   int x,y;
  413. {
  414.   Line(x,y,x-5,y-3);Line(x,y,x-5,y+3);Line(x+1,y,x-13,y);
  415. }
  416.  
  417. void U_Arrow(x,y)
  418.   int x,y;
  419. {
  420.   Line(x,y,x-3,y+5);Line(x,y,x+3,y+5);Line(x,y-1,x,y+13);
  421. }
  422.  
  423. void D_Arrow(x,y)
  424.   int x,y;
  425. {
  426.   Line(x,y,x-2,y-5);Line(x,y,x+2,y-5);Line(x,y+1,x,y-13);
  427. }
  428.  
  429. void Notice(x,y,dx,dy,on,On_Color,Off_Color)
  430.   int x,y,dx,dy,on;
  431.   char *On_Color,*Off_Color;
  432. {
  433.   XSetFunction(display,gc,GXcopy);
  434.   if (on==1) SetColor(On_Color);else SetColor(Off_Color);
  435.   FillRectangle(x,y,dx,dy);
  436. }
  437.  
  438.