home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / demos / xgc / interpret.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-09  |  10.6 KB  |  428 lines

  1. /*
  2. ** interpret.c
  3. **
  4. ** interprets and executes lines in the Xgc syntax.
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <X11/Intrinsic.h>
  9. #include <X11/StringDefs.h>
  10. #include "xgc.h"
  11. #include "tile"
  12.  
  13. void change_text();
  14. void GC_change_function();
  15. void GC_change_foreground();
  16. void GC_change_background();
  17. void GC_change_linewidth();
  18. void GC_change_linestyle();
  19. void GC_change_capstyle();
  20. void GC_change_joinstyle();
  21. void GC_change_fillstyle();
  22. void GC_change_fillrule();
  23. void GC_change_arcmode();
  24. void GC_change_dashlist();
  25. void GC_change_planemask();
  26. void GC_change_font();
  27. void change_test();
  28. void change_percent();
  29.  
  30. extern void update_dashlist();
  31. extern void update_planemask();
  32. extern void update_slider();
  33. extern void select_button();
  34. extern void run_test();
  35. extern void print_if_recording();
  36.  
  37. extern XgcStuff TestStuff;
  38. extern XgcStuff FunctionStuff;
  39. extern XgcStuff LinestyleStuff;
  40. extern XgcStuff CapstyleStuff;
  41. extern XgcStuff JoinstyleStuff;
  42. extern XgcStuff FillstyleStuff;
  43. extern XgcStuff FillruleStuff;
  44. extern XgcStuff ArcmodeStuff;
  45.  
  46. extern XStuff X;
  47. extern ChoiceDesc *GCdescs[];
  48. extern ChoiceDesc *testchoicedesc;
  49. extern Widget test;
  50. extern Widget GCform;
  51. extern Widget foregroundtext;
  52. extern Widget backgroundtext;
  53. extern Widget linewidthtext;
  54. extern Widget fonttext;
  55. extern Widget dashlistchoice;
  56. extern Widget planemaskchoice;
  57. extern Widget testchoiceform;
  58.  
  59. extern int fildes[2];
  60. extern FILE *outend;
  61. extern FILE *yyin;
  62.  
  63. /* interpret(string)
  64. ** -----------------
  65. ** Takes string, which is a line written in the xgc syntax, figures
  66. ** out what it means, and passes the buck to the right procedure.
  67. ** That procedure gets called with feedback set to FALSE; interpret()
  68. ** is only called if the user is selecting things interactively.
  69. **
  70. ** This procedure will go away when I can figure out how to make yacc
  71. ** and lex read from strings as well as files.
  72. */
  73.  
  74. void
  75. interpret(string)
  76.      String string;
  77. {
  78.   char word1[20], word2[80];
  79.   int i;
  80.  
  81.   sscanf(string,"%s",word1);
  82.   if (!strcmp(word1,"run")) run_test();
  83.  
  84.   else {
  85.     sscanf(string,"%s %s",word1,word2);
  86.     print_if_recording(string);
  87.  
  88.     /* So word1 is the first word on the line and word2 is the second.
  89.        Now the fun begins... */
  90.     
  91.     if (!strcmp(word1,TestStuff.choice.text))  {
  92.       for (i=0;i<NUM_TESTS;++i) {
  93.     if (!strcmp(word2,(TestStuff.data)[i].text)) {
  94.       change_test((TestStuff.data)[i].code,FALSE);
  95.       break;
  96.     }
  97.       }
  98.     }
  99.     else if (!strcmp(word1,FunctionStuff.choice.text)) {
  100.       for (i=0;i<NUM_FUNCTIONS;++i) {
  101.     if (!strcmp(word2,(FunctionStuff.data)[i].text)) {
  102.       GC_change_function((FunctionStuff.data)[i].code,FALSE);
  103.       break;
  104.     }
  105.       }
  106.     }
  107.     else if (!strcmp(word1,LinestyleStuff.choice.text)) {
  108.       for (i=0;i<NUM_LINESTYLES;++i) {
  109.     if (!strcmp(word2,(LinestyleStuff.data)[i].text)) {
  110.       GC_change_linestyle((LinestyleStuff.data)[i].code,FALSE);
  111.       break;
  112.     }
  113.       }
  114.     }
  115.     else if (!strcmp(word1,"linewidth"))
  116.       GC_change_linewidth(atoi(word2),FALSE);
  117.     else if (!strcmp(word1,CapstyleStuff.choice.text)) {
  118.       for (i=0;i<NUM_CAPSTYLES;++i) {
  119.     if (!strcmp(word2,(CapstyleStuff.data)[i].text)) {
  120.       GC_change_capstyle((CapstyleStuff.data)[i].code,FALSE);
  121.       break;
  122.     }
  123.       }
  124.     }
  125.     else if (!strcmp(word1,JoinstyleStuff.choice.text)) {
  126.       for (i=0;i<NUM_JOINSTYLES;++i) {
  127.     if (!strcmp(word2,(JoinstyleStuff.data)[i].text)) {
  128.       GC_change_joinstyle((JoinstyleStuff.data)[i].code,FALSE);
  129.       break;
  130.     }
  131.       }
  132.     }
  133.     else if (!strcmp(word1,FillstyleStuff.choice.text)) {
  134.       for (i=0;i<NUM_FILLSTYLES;++i) {
  135.     if (!strcmp(word2,(FillstyleStuff.data)[i].text)) {
  136.       GC_change_fillstyle((FillstyleStuff.data)[i].code,FALSE);
  137.       break;
  138.     }
  139.       }
  140.     }
  141.     else if (!strcmp(word1,FillruleStuff.choice.text)) {
  142.       for (i=0;i<NUM_FILLRULES;++i) {
  143.     if (!strcmp(word2,(FillruleStuff.data)[i].text)) {
  144.       GC_change_fillrule((FillruleStuff.data)[i].code,FALSE);
  145.       break;
  146.     }
  147.       }
  148.     }
  149.     else if (!strcmp(word1,ArcmodeStuff.choice.text)) {
  150.       for (i=0;i<NUM_ARCMODES;++i) {
  151.     if (!strcmp(word2,(ArcmodeStuff.data)[i].text)) {
  152.       GC_change_arcmode((ArcmodeStuff.data)[i].code,FALSE);
  153.       break;
  154.     }
  155.       }
  156.     }
  157.     else if (!strcmp(word1,"planemask")) 
  158.       GC_change_planemask((unsigned int) atoi(word2),FALSE);
  159.     else if (!strcmp(word1,"dashlist"))
  160.       GC_change_dashlist(atoi(word2),FALSE);
  161.     else if (!strcmp(word1,"font"))
  162.       GC_change_font(word2,FALSE);
  163.     else if (!strcmp(word1,"foreground"))
  164.       GC_change_foreground((unsigned int) atoi(word2),FALSE);
  165.     else if (!strcmp(word1,"background"))
  166.       GC_change_background((unsigned int) atoi(word2),FALSE);
  167.     else if (!strcmp(word1,"percent"))
  168.       change_percent(atoi(word2), FALSE);
  169.     else fprintf(stderr,"Ack... %s %s\n",word1,word2);
  170.   }
  171. }
  172.  
  173. #ifdef notdef
  174. void
  175. interpret(instring)
  176.      char *instring;
  177. {
  178.   FILE *inend;
  179.   
  180.   print_if_recording(instring);
  181.   yyin = outend;
  182.   inend = fdopen(fildes[1],"w");
  183.   fprintf(inend,"%s",instring);
  184.   fclose(inend);
  185.   yyparse();
  186. }
  187. #endif
  188.  
  189. #define select_correct_button(which,number) \
  190.   select_button(GCdescs[(which)],(number));
  191.  
  192. /* GC_change_blahzee(foo,feedback)
  193. ** ---------------------
  194. ** Changes the blahzee field in xgc's GC to foo.  If feedback is TRUE,
  195. ** changes the display to reflect this (makes it look like the user
  196. ** selected the button, or typed in the text, or whatever).
  197. */
  198.  
  199. void
  200. GC_change_function(function,feedback)
  201.      int function;
  202.      Boolean feedback;
  203. {
  204.   XSetFunction(X.dpy,X.gc,function);
  205.   X.gcv.function = function;
  206.   if (feedback) select_correct_button(CFunction,function);
  207. }
  208.  
  209. void
  210. GC_change_foreground(foreground,feedback)
  211.      unsigned long foreground;
  212.      Boolean feedback;
  213. {
  214.   char text[40];
  215.  
  216.   XSetForeground(X.dpy,X.miscgc,foreground);
  217.   XCopyPlane(X.dpy,X.stipple,X.tile,X.miscgc,0,0,tile_width,tile_height,0,0,1);
  218.   XSetForeground(X.dpy,X.gc,foreground);
  219.   X.gcv.foreground = foreground;
  220.   XSetTile(X.dpy,X.gc,X.tile);
  221.   XSetTile(X.dpy,X.miscgc,X.tile);
  222.   if (feedback) {
  223.     sprintf(text,"%d",foreground);
  224.     change_text(foregroundtext,text);
  225.   }
  226. }
  227.  
  228. void
  229. GC_change_background(background,feedback)
  230.      unsigned long background;
  231.      Boolean feedback;
  232. {
  233.   char text[40];
  234.  
  235.   XSetBackground(X.dpy,X.miscgc,background);
  236.   XCopyPlane(X.dpy,X.stipple,X.tile,X.miscgc,0,0,tile_width,tile_height,0,0,1);
  237.   XSetBackground(X.dpy,X.gc,background);
  238.   X.gcv.background = background;
  239.   XSetTile(X.dpy,X.gc,X.tile);
  240.   XSetTile(X.dpy,X.miscgc,X.tile);
  241.  
  242.   /* Update the background of the test window NOW. */
  243.  
  244.   XSetWindowBackground(X.dpy,XtWindow(test),background);
  245.   XClearWindow(X.dpy,XtWindow(test));
  246.  
  247.   if (feedback) {
  248.     sprintf(text,"%d",background);
  249.     change_text(backgroundtext,text);
  250.   }
  251. }
  252.  
  253. void
  254. GC_change_linewidth(linewidth,feedback)
  255.      int linewidth;
  256.      Boolean feedback;
  257. {
  258.   char text[40];
  259.  
  260.   X.gcv.line_width = linewidth;
  261.   XChangeGC(X.dpy,X.gc,GCLineWidth,&X.gcv);
  262.   if (feedback) {
  263.     sprintf(text,"%d",linewidth);
  264.     change_text(linewidthtext,text);
  265.   }
  266. }
  267.  
  268. void
  269. GC_change_linestyle(linestyle,feedback)
  270.      int linestyle;
  271.      Boolean feedback;
  272. {
  273.   X.gcv.line_style = linestyle;
  274.   XChangeGC(X.dpy,X.gc,GCLineStyle,&X.gcv);
  275.   if (feedback) select_correct_button(CLinestyle,linestyle);
  276. }
  277.  
  278. void
  279. GC_change_capstyle(capstyle,feedback)
  280.      int capstyle;
  281.      Boolean feedback;
  282. {
  283.   X.gcv.cap_style = capstyle;
  284.   XChangeGC(X.dpy,X.gc,GCCapStyle,&X.gcv);
  285.   if (feedback) select_correct_button(CCapstyle,capstyle);
  286. }
  287.  
  288. void
  289. GC_change_joinstyle(joinstyle,feedback)
  290.      int joinstyle;
  291.      Boolean feedback;
  292. {
  293.   X.gcv.join_style = joinstyle;
  294.   XChangeGC(X.dpy,X.gc,GCJoinStyle,&X.gcv);
  295.   if (feedback) select_correct_button(CJoinstyle,joinstyle);
  296. }
  297.  
  298. void
  299. GC_change_fillstyle(fillstyle,feedback)
  300.      int fillstyle;
  301.      Boolean feedback;
  302. {
  303.   XSetFillStyle(X.dpy,X.gc,fillstyle);
  304.   X.gcv.fill_style = fillstyle;
  305.   if (feedback) select_correct_button(CFillstyle,fillstyle);
  306. }
  307.  
  308. void
  309. GC_change_fillrule(fillrule,feedback)
  310.      int fillrule;
  311.      Boolean feedback;
  312. {
  313.   XSetFillRule(X.dpy,X.gc,fillrule);
  314.   X.gcv.fill_rule = fillrule;
  315.   if (feedback) select_correct_button(CFillrule,fillrule);
  316. }
  317.  
  318. void
  319. GC_change_arcmode(arcmode,feedback)
  320.      int arcmode;
  321.      Boolean feedback;
  322. {
  323.   XSetArcMode(X.dpy,X.gc,arcmode);
  324.   X.gcv.arc_mode = arcmode;
  325.   if (feedback) select_correct_button(CArcmode,arcmode);
  326. }
  327.  
  328. /* GC_change_dashlist(dashlist)
  329. ** ----------------------------
  330. ** Now this one's a bit tricky.  dashlist gets passed in as an int, but we
  331. ** want to change it to an array of chars, like the GC likes it.
  332. ** For example:
  333. **     119 => XXX_XXX_ => [3,1,3,1]
  334. */
  335.  
  336. void
  337. GC_change_dashlist(dashlist,feedback) 
  338.      int dashlist;
  339.      Boolean feedback;
  340. {
  341.   char dasharray[DASHLENGTH];    /* what we're gonna pass to XSetDashes */
  342.   int dashnumber = 0;        /* which element of dasharray we're currently
  343.                    modifying */
  344.   int i;            /* which bit of the dashlist we're on */
  345.   int state = 1;        /* whether the list bit we checked was
  346.                    on (1) or off (0) */
  347.                   
  348.   /* Initialize the dasharray */
  349.  
  350.   for (i = 0; i < DASHLENGTH; ++i) dasharray[i] = 0;
  351.  
  352.   if (dashlist == 0) return;    /* having no dashes at all is bogus */
  353.  
  354.   /* XSetDashes expects the dashlist to start with an on bit, so if it
  355.   ** doesn't, we keep on rotating it until it does */
  356.  
  357.   while (!(dashlist&1)) dashlist /= 2;
  358.  
  359.   /* Go through all the bits in dashlist, and update the dasharray
  360.   ** accordingly */
  361.  
  362.   for (i = 0; i < DASHLENGTH; ++i) {
  363.     /* the following if statements checks to see if the bit we're looking
  364.     ** at as the same on or offness as the one before it (state).  If
  365.     ** so, we increment the length of the current dash. */
  366.  
  367.     if (((dashlist&1<<i) && state) || (!(dashlist&1<<i) && !state))
  368.       ++dasharray[dashnumber];
  369.     else {            
  370.       state = state^1;        /* reverse the state */
  371.       ++dasharray[++dashnumber]; /* start a new dash */
  372.     }
  373.   } 
  374.  
  375.   XSetDashes(X.dpy,X.gc,0,dasharray,dashnumber+1);
  376.   X.gcv.dashes = dashlist;
  377.  
  378.   if (feedback) update_dashlist(dashlist);
  379. }
  380.  
  381. void
  382. GC_change_planemask(planemask,feedback)
  383.      unsigned long planemask;
  384.      Boolean feedback;
  385. {
  386.   XSetPlaneMask(X.dpy,X.gc,planemask);
  387.   X.gcv.plane_mask = planemask;
  388.   if (feedback) update_planemask((long)planemask);
  389. }
  390.  
  391. void
  392. change_test(test,feedback) 
  393.      int test;
  394.      Boolean feedback;
  395. {
  396.   X.test = test;
  397.   if (feedback) select_button(testchoicedesc,test);
  398. }
  399.  
  400. void
  401. GC_change_font(str,feedback)
  402.      String str;
  403.      Boolean feedback;
  404. {
  405.   int num_fonts;        /* number of fonts that match the string */
  406.  
  407.   XListFonts(X.dpy,str,1,&num_fonts); /* see if the font exists */
  408.  
  409.   if (num_fonts) {
  410.     XSetFont(X.dpy,X.gc,XLoadFont(X.dpy,str));
  411.     if (feedback) change_text(fonttext,str);
  412.   }
  413. }
  414.  
  415. void
  416. change_percent(percent,feedback)
  417.      int percent;
  418.      Boolean feedback;
  419. {
  420.   /* Make sure that percent is valid */
  421.  
  422.   if (percent < 1 || percent > 100) return;
  423.  
  424.   X.percent = (float) percent / 100.0;
  425.  
  426.   if (feedback) update_slider(percent);
  427. }
  428.