home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / dev / alst-3.04.lha / ALSt-3.04 / src / winprim.c < prev   
Encoding:
C/C++ Source or Header  |  1994-05-14  |  9.5 KB  |  415 lines

  1. /* 
  2.     stdwin window primitives
  3.     written by tim budd, january 1989
  4. */
  5.  
  6. # include "stdwin.h"
  7. /* undefine guido's NOARGS so it doesn't conflict with mine */
  8. # undef NOARGS
  9. # include <stdio.h>
  10. # include "env.h"
  11. # include "memory.h"
  12. # include "names.h"
  13.  
  14. extern WINDOW *syswin; /* this is a bit of exposed internals from stdwin */
  15.  
  16. extern object trueobj, falseobj;
  17. extern boolean parseok;
  18. extern int initial;
  19.  
  20. /* report a fatal system error */
  21. noreturn sysError(s1, s2)
  22. char *s1, *s2;
  23. {    char buffer[1024];
  24.  
  25.     if (initial) {
  26.         ignore fprintf(stderr,"%s\n%s\n", s1, s2);
  27.         }
  28.     else {
  29.         ignore sprintf(buffer,"%s %s", s1, s2);
  30.         wperror(buffer);
  31.         }
  32.     ignore abort();
  33. }
  34.  
  35. /* report a fatal system error */
  36. noreturn sysWarn(s1, s2)
  37. char *s1, *s2;
  38. {    char buffer[1024];
  39.  
  40.     if (initial) {
  41.         ignore fprintf(stderr,"%s\n%s\n", s1, s2);
  42.         }
  43.     else {
  44.         ignore sprintf(buffer,"%s %s", s1, s2);
  45.         wperror(buffer);
  46.         }
  47. }
  48.  
  49. compilWarn(selector, str1, str2)
  50. char *selector, *str1, *str2;
  51. {    char buffer[1024];
  52.  
  53.     if (initial) {
  54.         ignore fprintf(stderr,"compiler warning: Method %s : %s %s\n", 
  55.             selector, str1, str2);
  56.         }
  57.     else {
  58.         ignore sprintf(buffer,"warn: %s %s", str1, str2);
  59.         wmessage(buffer);
  60.         }
  61. }
  62.  
  63. compilError(selector, str1, str2)
  64. char *selector, *str1, *str2;
  65. {    char buffer[1024];
  66.  
  67.     if (initial) {
  68.         ignore fprintf(stderr,"compiler error: Method %s : %s %s\n", 
  69.             selector, str1, str2);
  70.         }
  71.     else {
  72.         ignore sprintf(buffer,"error: %s %s", str1, str2);
  73.         wmessage(buffer);
  74.         }
  75.     parseok = false;
  76. }
  77.  
  78. noreturn dspMethod(cp, mp)
  79. char *cp, *mp;
  80. {
  81.     /*ignore fprintf(stderr,"%s %s\n", cp, mp);*/
  82. }
  83.  
  84. givepause()
  85. {    char buffer[80];
  86.  
  87.     if (initial) {
  88.         ignore fprintf(stderr,"push return to continue\n");
  89.         ignore gets(buffer);
  90.         }
  91.     else
  92.         wmessage("wait to continue");
  93. }
  94.  
  95. static object newPoint(x, y)
  96. int x, y;
  97. {    object newObj;
  98.  
  99.     newObj = allocObject(2);
  100.     setClass(newObj, globalSymbol("Point"));
  101.     basicAtPut(newObj, 1, newInteger(x));
  102.     basicAtPut(newObj, 2, newInteger(y));
  103.     return newObj;
  104. }
  105.  
  106. /* windows and text edit buffers are maintained in
  107.    a single structure */
  108. # define WINDOWMAX 15
  109. static struct {
  110.     WINDOW *w;
  111.     TEXTEDIT *tp;
  112.     } ws[WINDOWMAX];
  113.  
  114. /* menus are maintained in a similar structure */
  115. # define MENUMAX 20
  116. static MENU *mu[MENUMAX];
  117.  
  118. /* current event record */
  119. static EVENT evrec;
  120.  
  121. static int findWindow(w)
  122. WINDOW *w;
  123. {    int i;
  124.     for (i = 0; i < WINDOWMAX; i++)
  125.         { if (w == ws[i].w) return(i);
  126.         }
  127.     sysError("can't find window","");
  128.     return(0);
  129. }
  130.  
  131. static void drawproc(w, left, top, right, bottom)
  132. WINDOW *w;
  133. int left, top, right, bottom;
  134. {    int i;
  135.  
  136.     i = findWindow(w);
  137.     if (ws[i].tp) tedraw(ws[i].tp);
  138. }
  139.  
  140. object sysPrimitive(primitiveNumber, arguments)
  141. int primitiveNumber;
  142. object *arguments;
  143. {    int i, j, k;
  144.     int p1, p2, p3, p4;
  145.     char *c;
  146.     WINDOW *w;
  147.     object returnedObject;
  148.  
  149.     returnedObject = nilobj;
  150.  
  151.     switch(primitiveNumber) {
  152.     case 160:    /* window open */
  153.         i = intValue(arguments[0]); /* win number */
  154.         if (ws[i].w) break;    /* already open */
  155.         c = charPtr(arguments[1]);  /* title */
  156.         j = intValue(arguments[2]); /* text or not */
  157.         if (j) {
  158.             ws[i].w = w = wopen(c, drawproc);
  159.             wgetwinsize(w, &j, &k);
  160.             ws[i].tp = tecreate(w, 0, 0, j, k);
  161.             }
  162.         else {
  163.             ws[i].w = wopen(c, NULL);
  164.             ws[i].tp = 0;
  165.             }    
  166.         break;
  167.         
  168.     case 161:    /* variety of simple actions */
  169.         i = intValue(arguments[0]); /* win number */
  170.         if (! (w = ws[i].w)) break;    /* return if no open */
  171.         j = intValue(arguments[1]); /* action */
  172.         switch(j) {
  173.             case 1: ws[i].w = NULL; wclose(w); break;
  174.             case 2: wbegindrawing(w); break;
  175.             case 3: wenddrawing(w); break; 
  176.             case 4: wsetactive(w); break;
  177.             case 5: if (ws[i].tp) tedraw(ws[i].tp); break;
  178.             case 6: wgetwinsize(w, &i, &j);
  179.                 returnedObject = newPoint(i, j); break;
  180.         }
  181.         break;
  182.  
  183.     case 162:    /* one int arg actions */
  184.         i = intValue(arguments[0]); /* win number */
  185.         if (! (w = ws[i].w)) break;    /* return if no open */
  186.         i = intValue(arguments[1]); /* action */
  187.         j = intValue(arguments[2]); /* x */
  188.         switch(i) {
  189.             case 1:  /* set timer */
  190.                 wsettimer(w, j); break;
  191.             case 2:    /* menu attach */
  192.                 wmenuattach(w, mu[j]); break;
  193.             case 3:    /* menu detach */
  194.                 wmenudetach(w, mu[j]); break;
  195.         }
  196.         break;
  197.  
  198.     case 163:    /* two int arg actions */
  199.         i = intValue(arguments[0]); /* win number */
  200.         if (! (w = ws[i].w)) break;    /* return if no open */
  201.         i = intValue(arguments[1]); /* action */
  202.         j = intValue(arguments[2]); /* x */
  203.         k = intValue(arguments[3]); /* y */
  204.         switch(i) {
  205.             case 2: wsetdocsize(w, j, k); break;
  206.             case 3: wsetorigin(w, j, k); break;
  207.             case 4: wsetcaret(w, j, k); break;
  208.         }
  209.         break;
  210.  
  211.     case 164:    /* title */
  212.         i = intValue(arguments[0]); /* win number */
  213.         if (! (w = ws[i].w)) break;    /* return if no open */
  214.         c = charPtr(arguments[1]);
  215.         wsettitle(w, c);
  216.         break;
  217.  
  218.     case 165:    /* get text */
  219.         i = intValue(arguments[0]);
  220.         if (ws[i].tp)
  221.         returnedObject = newStString(tegettext(ws[i].tp));
  222.         break;
  223.  
  224.     case 166:    /* replace text */
  225.         i = intValue(arguments[0]);
  226.         if (ws[i].tp) {
  227.             tereplace(ws[i].tp, charPtr(arguments[1]));
  228.             /* add newline */
  229.             tereplace(ws[i].tp, "\n");
  230.             }
  231.         break;
  232.  
  233.     case 170: getevent:    /* get next event */
  234.     /*    wgetevent(&evrec);    */
  235.         wpollevent(&evrec);
  236.         if (evrec.window != syswin) {
  237.             i = findWindow(evrec.window);
  238.             if (ws[i].tp && teevent(ws[i].tp, &evrec))
  239.                 goto getevent;
  240.             }
  241.         else goto getevent;
  242. /*fprintf(stderr,"returning event type %d %d\n", evrec.type, evrec.u.where.clicks);*/
  243.         returnedObject = newInteger(evrec.type);
  244.         break;
  245.  
  246.     case 171:    /* integer event info */
  247.         i = intValue(arguments[0]);
  248.         switch(i) {
  249.         case 1:     /* event window */
  250.             j = findWindow(evrec.window); break;
  251.         case 2:        /* event menu */
  252.             j = evrec.u.m.id; break;
  253.         case 3:        /* menu item */
  254.             j = evrec.u.m.item + 1; break;
  255.         case 4:        /* char typed */
  256.             j = evrec.u.character; break;
  257.         case 5:        /* mouse y */
  258.             j = evrec.u.where.v; break;
  259.         case 6:        /* mouse button */
  260.             j = evrec.u.where.button; break;
  261.         case 7:        /* mouse click number */
  262.             j = evrec.u.where.clicks;
  263.         case 8:        /* char typed */
  264.             j = evrec.u.character;
  265.         case 9:        /* command typed */
  266.             j = evrec.u.command;
  267.         }
  268.         returnedObject = newInteger(j);
  269.         break;
  270.  
  271.     case 172:    /* more general event info */
  272.         i = intValue(arguments[0]);
  273.         switch(i) {
  274.         case 1:        /* mouse down point */
  275.             returnedObject = newPoint(evrec.u.where.h, 
  276.                 evrec.u.where.v); 
  277.             break;
  278.         }
  279.         break;
  280.  
  281.     case 180:    /* new menu */
  282.         i = intValue(arguments[0]); /* win number */
  283.         c = charPtr(arguments[1]); /* title */
  284.         mu[i] = wmenucreate(i, c);
  285.         break;
  286.  
  287.     case 181:    /* menu item */
  288.         i = intValue(arguments[0]); /* menu number */
  289.         c = charPtr(arguments[1]); /* title */
  290.         if (isInteger(arguments[2]))
  291.             j = intValue(arguments[2]);
  292.         else j = -1;
  293.         wmenuadditem(mu[i], c, j);
  294.         break;
  295.  
  296.     case 182:    /* check menu items */
  297.         i = intValue(arguments[0]); /* menu number */
  298.         j = intValue(arguments[1]); /* item number */
  299.         k = intValue(arguments[2]); /* action */
  300.         p1 = intValue(arguments[3]); /* flag */
  301.         switch(k) {
  302.             case 1:    /* enable/disable */
  303.                 wmenuenable(mu[i],j-1,p1); break;
  304.             case 2: /* check/no check */
  305.                 wmenucheck(mu[i],j-1,p1); break;
  306.         }
  307.         break;
  308.  
  309.     case 190:    /* print text graphics */
  310.         i = intValue(arguments[0]); /* x */
  311.         j = intValue(arguments[1]); /* y */
  312.         c = charPtr(arguments[2]); /* text */
  313.         wdrawtext(i, j, c, strlen(c));
  314.         break;
  315.  
  316.     case 192:    /* points */
  317.         i = intValue(arguments[0]); /* action */
  318.         p1 = intValue(arguments[1]);
  319.         p2 = intValue(arguments[2]);
  320.         p3 = intValue(arguments[3]);
  321.         p4 = intValue(arguments[4]);
  322.         switch(i) {
  323.             case 1:  /* draw line */
  324.                 wdrawline(p1, p2, p3, p4); break;
  325.         }
  326.         break;
  327.  
  328.     case 193:    /* circles and the like */
  329.         i = intValue(arguments[0]); /* action */
  330.         p1 = intValue(arguments[1]);
  331.         p2 = intValue(arguments[2]);
  332.         p3 = intValue(arguments[3]);
  333.         switch(i) {
  334.             case 1:        /* draw circle */
  335.                 wdrawcircle(p1,p2,p3); break;
  336.             case 2:        /* draw char */
  337.                 wdrawchar(p1,p2,p3); break;
  338.         }
  339.         break;
  340.  
  341.     case 194:    /* rectangles */
  342.         i = intValue(arguments[0]); /* action */
  343.         p1 = intValue(arguments[1]);
  344.         p2 = intValue(arguments[2]);
  345.         p3 = intValue(arguments[3]);
  346.         p4 = intValue(arguments[4]);
  347.         switch(i) {
  348.             case 1:        /* draw box */
  349.                 wdrawbox(p1,p2,p3,p4); break;
  350.             case 2:     /* paint */
  351.                 wpaint(p1,p2,p3,p4); break;
  352.             case 3:        /* erase */
  353.                 werase(p1,p2,p3,p4); break;
  354.             case 4:        /* invert */
  355.                 winvert(p1,p2,p3,p4); break;
  356.         }
  357.         break;
  358.  
  359.     case 195:    /* shading */
  360.         i = intValue(arguments[0]); /* action */
  361.         p1 = intValue(arguments[1]);
  362.         p2 = intValue(arguments[2]);
  363.         p3 = intValue(arguments[3]);
  364.         p4 = intValue(arguments[4]);
  365.         j = intValue(arguments[5]);
  366.         switch(i) {
  367.             case 1:        /* shading */
  368.                 wshade(p1,p2,p3,p4,j); break;
  369.             }
  370.         break;
  371.  
  372.     case 200:    /* issue a message */
  373.         c = charPtr(arguments[0]);
  374.         wmessage(c);
  375.         break;
  376.  
  377.     case 201:    /* ask a question */
  378.         { char replybuffer[120];
  379.         strcpy(replybuffer, charPtr(arguments[1]));
  380.         if (waskstr(charPtr(arguments[0]), replybuffer, 120))
  381.             returnedObject = newStString(replybuffer);
  382.         }
  383.         break;
  384.     
  385.     case 202:    /* asky a binary question */
  386.         i = waskync(charPtr(arguments[0]), intValue(arguments[1]));
  387.         if (i == 1) returnedObject = trueobj;
  388.         else if (i == 0) returnedObject = falseobj;
  389.         break;
  390.  
  391.     case 203:    /* ask for a file */
  392.         { char replybuffer[120];
  393.         strcpy(replybuffer, charPtr(arguments[1]));
  394.         if (waskfile(charPtr(arguments[0]), replybuffer, 120, intValue(arguments[2])))
  395.             returnedObject = newStString(replybuffer);
  396.         }
  397.         break;
  398.  
  399.     case 204:    /* error message */
  400.         wperror(charPtr(arguments[0]));
  401.         break;
  402.         
  403.     case 205:    /* beep */
  404.         wfleep();
  405.         break;
  406.  
  407.     default:
  408.     fprintf(stderr,"primitive not implmented yet %d\n",
  409.         primitiveNumber);
  410.     sysError("primitive not done","");
  411.     }
  412.  
  413.     return returnedObject;
  414. }
  415.