home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v92.tgz / v92.tar / v92 / src / runtime / rmacrsc.ri < prev    next >
Text File  |  1996-03-22  |  25KB  |  972 lines

  1. /*
  2.  * File: rmacrsc.ri - Mac specific resource
  3.  */
  4.  
  5. novalue MacMain Params((int argc, char *argv[]));
  6.  
  7. extern MouseInfoType gMouseInfo;
  8. extern char *cmlArgs;
  9. extern StringHandle textHandle;
  10.  
  11. /*
  12.  * allocate a context.  Can't be called until w has a display and window.
  13.  */
  14. wcp alc_context(wbp w)
  15.    {
  16.    int i;
  17.    wcp wc;
  18.  
  19.    GRFX_ALLOC(wc, _wcontext);
  20.    GRFX_LINK(wc, wcntxts);
  21.    
  22.    /* set default values */
  23.    
  24.    wc->contextPtr = malloc(sizeof ContextType);
  25.    SETCONTEXTDEFAULT(wc->contextPtr);
  26.    
  27.    return wc;
  28.    }
  29.    
  30. /*
  31.  * allocate a window state structure
  32.  */
  33. wsp alc_winstate()
  34.    {
  35.    int i;
  36.    wsp ws;
  37.    
  38.    GRFX_ALLOC(ws, _wstate);
  39.    ws->bits = 1024;                /* echo ON; others OFF */
  40.    ws->filep = nulldesc;
  41.    ws->listp = nulldesc;
  42.    GRFX_LINK(ws, wstates);
  43.    return ws;
  44.    }
  45.  
  46. /*
  47.  * free a window state
  48.  */
  49. int free_window(wsp ws)
  50.    {
  51.    ws->refcount--;
  52.    if(ws->refcount == 0) 
  53.       {
  54.       if (ws->theWindow != nil) 
  55.          {
  56.           CloseWindow (ws->theWindow);
  57.           DisposeWindow (ws->theWindow);
  58.          }
  59.       GRFX_UNLINK(ws, wstates);
  60.       }
  61.    return 0;
  62.    }
  63.    
  64.    
  65. /*
  66.  * free a window context
  67.  */
  68. novalue free_context(wcp wc)
  69.    {
  70.    wc->refcount--;
  71.    if(wc->refcount == 0) 
  72.       {
  73.       GRFX_UNLINK(wc, wcntxts);
  74.       }
  75.    }
  76.  
  77. /*
  78.  * drawstrng
  79.  */
  80. void drawstrng(wbp wb, int x, int y, char *str, int slen)
  81. {
  82.   STDLOCALS (wb);
  83.     
  84.   SetPort (ws->theWindow);
  85.   COPYCONTEXT (wc->contextPtr);
  86.   MoveTo (x, y);
  87.   DrawText (str, 0, slen);
  88.   
  89. /*begin quote*/  
  90.   PREPAREGWORLD (ws);
  91.   COPYCONTEXT (wc->contextPtr);
  92.   MoveTo (x, y);
  93.   DrawText (str, 0, slen);
  94.   UnlockPixels (ws->offScreenPMHandle);
  95.   SetGWorld (ws->origPort, ws->origDev);
  96. /*end quote*/
  97. }
  98.  
  99. void hidecrsr (wsp ws)
  100. {
  101. }
  102.  
  103. void showcrsr (wsp ws)
  104. {
  105. }
  106.  
  107. void getfntnam (wbp wb, char *answer)
  108. {
  109.    Str255 fName;
  110.    STDLOCALS(wb);
  111.    
  112.    GetFontName (wc->contextPtr->txFont, fName);
  113.    answer = PtoCstr (fName);
  114. }
  115.  
  116. /*
  117.  * allocate a context, cloning attributes from an existing context
  118.  */
  119. wcp clone_context(wb)
  120. wbp wb;
  121. {
  122.    wcp wc, wc2 = wb->context;
  123.    wsp ws = wb->window;
  124.  
  125.    Protect(wc = alc_context(wb), return NULL);
  126.  
  127.    /*
  128.     * copy over some stuff
  129.     */
  130.  
  131.    memcpy (&(wc->contextPtr->bkPat), &(wc2->contextPtr->bkPat), sizeof Pattern);
  132.    memcpy (&(wc->contextPtr->fillPat), &(wc2->contextPtr->fillPat), sizeof Pattern);
  133.    wc->contextPtr->pnLoc = wc2->contextPtr->pnLoc;
  134.    wc->contextPtr->pnSize = wc2->contextPtr->pnSize;
  135.    wc->contextPtr->pnMode = wc2->contextPtr->pnMode;
  136.    memcpy (&(wc->contextPtr->pnPat), &(wc2->contextPtr->pnPat), sizeof Pattern);
  137.    wc->contextPtr->txFont = wc2->contextPtr->txFont;
  138.    wc->contextPtr->txFace = wc2->contextPtr->txFace;
  139.    wc->contextPtr->txMode = wc2->contextPtr->txMode;
  140.    wc->contextPtr->txSize = wc2->contextPtr->txSize;
  141.    wc->contextPtr->spExtra = wc2->contextPtr->spExtra;
  142.    wc->contextPtr->fgColor = wc2->contextPtr->fgColor;
  143.    wc->contextPtr->bgColor = wc2->contextPtr->bgColor;
  144.    wc->clipx = wc2->clipx;
  145.    wc->clipy = wc2->clipy;
  146.    wc->clipw = wc2->clipw;
  147.    wc->cliph = wc2->cliph;
  148.    wc->font = wc2->font;
  149.    wc->dx = wc2->dx;
  150.    wc->dy = wc2->dy;
  151.    wc->fillstyle = wc2->fillstyle;
  152.    wc->drawop = wc2->drawop;
  153.    
  154.    return wc;
  155. }
  156.  
  157. /*
  158.  * drawrectangles
  159.  *  Parameters - the window binding for output, an array of rectangle
  160.  *               a count of the number of structures -> the number of
  161.  *               rectangles
  162.  */
  163. void drawrectangles(wbp wb, XRectangle *recs, int nrecs)
  164. {
  165.   int i;
  166.   Rect r;
  167.   STDLOCALS (wb);
  168.     
  169.   SetPort (ws->theWindow);
  170.   COPYCONTEXT (wc->contextPtr);
  171.   for (i = 0; i < nrecs; i++) 
  172.     {
  173.     SetRect (&r, recs[i].x - 1,
  174.                  recs[i].y - 1, 
  175.                  recs[i].x - 1 + recs[i].width,            
  176.                  recs[i].y - 1 + recs[i].height);
  177.     FrameRect (&r);
  178.     }
  179.     
  180. /*begin quote*/  
  181.   PREPAREGWORLD (ws);
  182.   COPYCONTEXT (wc->contextPtr);
  183.   for (i = 0; i < nrecs; i++) 
  184.     {
  185.     SetRect (&r, recs[i].x - 1,
  186.                  recs[i].y - 1, 
  187.                  recs[i].x - 1 + recs[i].width,            
  188.                  recs[i].y - 1 + recs[i].height);
  189.     FrameRect (&r);
  190.     }
  191.   UnlockPixels (ws->offScreenPMHandle);
  192.   SetGWorld (ws->origPort, ws->origDev);
  193. /*end quote*/
  194. }
  195.  
  196. /*
  197.  * drawpoints() - 
  198.  *  Parameters - the window binding for output, an array of points (assumed
  199.  *               to be fixed up for bitmap) and the number of points
  200.  */
  201. void drawpoints(wbinding *wb, XPoint *points, int npoints) 
  202. {
  203.    int i;
  204.    STDLOCALS (wb);
  205.   
  206.    SetPort (ws->theWindow);
  207.    COPYCONTEXT(wc->contextPtr);
  208.    for (i = 0; i < npoints; i++) {
  209.       MoveTo (points[i].x-1, points[i].y-1);
  210.       LineTo (points[i].x-1, points[i].y-1);
  211.       }
  212.    
  213. /*begin quote*/  
  214.   PREPAREGWORLD (ws);
  215.   COPYCONTEXT (wc->contextPtr);
  216.   for (i = 0; i < npoints; i++) {
  217.       MoveTo (points[i].x-1, points[i].y-1);
  218.       LineTo (points[i].x-1, points[i].y-1);
  219.       }
  220.   UnlockPixels (ws->offScreenPMHandle);
  221.   SetGWorld (ws->origPort, ws->origDev);
  222. /*end quote*/
  223. }
  224.  
  225. /*
  226.  * drawlines - 
  227.  */
  228. void drawlines(wbinding *wb, XPoint *points, int npoints)
  229. {
  230.   int i;
  231.   STDLOCALS (wb);
  232.     
  233.   SetPort (ws->theWindow);
  234.   COPYCONTEXT (wc->contextPtr);
  235.   for (i = 0; i < (npoints - 1); i++)
  236.     {
  237.     MoveTo (points[i].x - 1, points[i].y - 1);
  238.     LineTo (points[i+1].x - 1, points[i+1].y - 1);
  239.     }
  240.   
  241. /*begin quote*/  
  242.   PREPAREGWORLD (ws);
  243.   COPYCONTEXT (wc->contextPtr);
  244.   for (i = 0; i < (npoints - 1); i++)
  245.     {
  246.     MoveTo (points[i].x - 1, points[i].y - 1);
  247.     LineTo (points[i+1].x - 1, points[i+1].y - 1);
  248.     }
  249.   UnlockPixels (ws->offScreenPMHandle);
  250.   SetGWorld (ws->origPort, ws->origDev);
  251. /*end quote*/
  252. }
  253.  
  254. /*
  255.  * drawsegments() -
  256.  */
  257. void drawsegments(wbinding *wb, XSegment *segs, int nsegs) 
  258. {
  259.   int i;
  260.   STDLOCALS (wb);
  261.     
  262.   SetPort (ws->theWindow);
  263.   COPYCONTEXT (wc->contextPtr);
  264.   for (i = 0; i < nsegs; i++)
  265.     {
  266.     MoveTo (segs[i].x1 - 1, segs[i].y1 - 1);
  267.     LineTo (segs[i].x2 - 1, segs[i].y2 - 1);
  268.     }
  269.     
  270. /*begin quote*/  
  271.   PREPAREGWORLD (ws);
  272.   COPYCONTEXT (wc->contextPtr);
  273.   for (i = 0; i < nsegs; i++)
  274.     {
  275.     MoveTo (segs[i].x1 - 1, segs[i].y1 - 1);
  276.     LineTo (segs[i].x2 - 1, segs[i].y2 - 1);
  277.     }
  278.   UnlockPixels (ws->offScreenPMHandle);
  279.   SetGWorld (ws->origPort, ws->origDev);
  280. /*end quote*/
  281. }
  282.  
  283. /*
  284.  * drawarcs() - assumes x and y are already fixed up for the bitmap
  285.  */
  286. void drawarcs(wbinding *wb, XArc *arcs, int narcs)
  287. {
  288.   int i;
  289.   Rect r;
  290.   STDLOCALS (wb);
  291.   
  292.   SetPort (ws->theWindow);
  293.   COPYCONTEXT (wc->contextPtr);
  294.   for (i = 0; i < narcs; i++)
  295.     {
  296.     SetRect (&r, arcs[i].x - 1,
  297.                  arcs[i].y - 1,
  298.                  arcs[i].x - 1 + arcs[i].width,
  299.                  arcs[i].y - 1 + arcs[i].height);
  300.     /* converts xarc spec to mac arc apec then pass it to FrameArc */
  301.     FrameArc (&r, (360 - arcs[i].angle1*64 + 90), arcs[i].angle2*64 * -1);
  302.     }
  303.     
  304. /*begin quote*/  
  305.   PREPAREGWORLD (ws);
  306.   COPYCONTEXT (wc->contextPtr);
  307.   for (i = 0; i < narcs; i++)
  308.     {
  309.     SetRect (&r, arcs[i].x - 1,
  310.                  arcs[i].y - 1,
  311.                  arcs[i].x - 1 + arcs[i].width,
  312.                  arcs[i].y - 1 + arcs[i].height);
  313.     FrameArc (&r, (360 - arcs[i].angle1*64 + 90), arcs[i].angle2*64 * -1);
  314.     }
  315.   UnlockPixels (ws->offScreenPMHandle);
  316.   SetGWorld (ws->origPort, ws->origDev);
  317. /*end quote*/
  318. }
  319.  
  320. /*
  321.  * fillarcs
  322.  */
  323. void fillarcs(wbp wb, XArc *arcs, int narcs) 
  324. {
  325.   int i;
  326.   Rect r;
  327.   STDLOCALS (wb);
  328.   
  329.   SetPort (ws->theWindow);
  330.   COPYCONTEXT (wc->contextPtr);
  331.   for (i = 0; i < narcs; i++)
  332.     {
  333.     SetRect (&r, arcs[i].x - 1,
  334.                  arcs[i].y - 1,
  335.                  arcs[i].x - 1 + arcs[i].width,
  336.                  arcs[i].y - 1 + arcs[i].height);
  337.     /* converts xarc spec to mac arc apec then pass it to FrameArc */
  338.     FillArc (&r, (360 - arcs[i].angle1*64 + 90), arcs[i].angle2*64 * -1,
  339.        &wc->contextPtr->fillPat);
  340.     }
  341.     
  342. /*begin quote*/  
  343.   PREPAREGWORLD (ws);
  344.   COPYCONTEXT (wc->contextPtr);
  345.   for (i = 0; i < narcs; i++)
  346.     {
  347.     SetRect (&r, arcs[i].x - 1,
  348.                  arcs[i].y - 1,
  349.                  arcs[i].x - 1 + arcs[i].width,
  350.                  arcs[i].y - 1 + arcs[i].height);
  351.     FillArc (&r, (360 - arcs[i].angle1*64 + 90), arcs[i].angle2*64 * -1,
  352.        &wc->contextPtr->fillPat);
  353.     }
  354.   UnlockPixels (ws->offScreenPMHandle);
  355.   SetGWorld (ws->origPort, ws->origDev);
  356. /*end quote*/
  357. }
  358.  
  359. /*
  360.  * fillpolygon
  361.  */
  362. void fillpolygon(wbp wb, XPoint *pts, int npts)
  363. {
  364.   PolyHandle myPoly;
  365.   int i;
  366.   STDLOCALS (wb);
  367.  
  368.   myPoly = OpenPoly ();
  369.   MoveTo (pts[0].x, pts[0].y);
  370.   for (i = 1; i < npts; i++)
  371.     {
  372.     LineTo (pts[i].x, pts[i].y);
  373.     }
  374.   MoveTo (pts[0].x, pts[0].y);
  375.   ClosePoly ();
  376.   
  377.   SetPort (ws->theWindow);
  378.   COPYCONTEXT (wc->contextPtr);
  379.   FillPoly (myPoly, &wc->contextPtr->fillPat);
  380.  
  381. /*begin quote*/  
  382.   PREPAREGWORLD (ws);
  383.   COPYCONTEXT (wc->contextPtr);
  384.   FillPoly (myPoly, &wc->contextPtr->fillPat);
  385.   UnlockPixels (ws->offScreenPMHandle);
  386.   SetGWorld (ws->origPort, ws->origDev);
  387. /*end quote*/
  388.   
  389.   KillPoly (myPoly); 
  390. }
  391.  
  392. /*
  393.  * fillrectangles
  394.  *  Parameters - the window binding for output, an array of rectangle
  395.  *               structures (assumed to be fixed up for the bitmap),
  396.  *               a count of the number of structures -> the number of 
  397.  *               rectangles 
  398.  */
  399. void fillrectangles(wbp wb, XRectangle *recs, int nrecs)
  400. {
  401.   int i;
  402.   Rect r;
  403.   STDLOCALS (wb);
  404.     
  405.   SetPort (ws->theWindow);
  406.   COPYCONTEXT (wc->contextPtr);
  407.   for (i = 0; i < nrecs; i++) 
  408.     {
  409.     SetRect (&r, recs[i].x - 1,
  410.                  recs[i].y - 1, 
  411.                  recs[i].x - 1 + recs[i].width,            
  412.                  recs[i].y - 1 + recs[i].height);
  413.     PaintRect (&r);
  414.     }
  415.     
  416. /*begin quote*/  
  417.   PREPAREGWORLD (ws);
  418.   COPYCONTEXT (wc->contextPtr);
  419.   for (i = 0; i < nrecs; i++) 
  420.     {
  421.     SetRect (&r, recs[i].x - 1,
  422.                  recs[i].y - 1, 
  423.                  recs[i].x - 1 + recs[i].width,            
  424.                  recs[i].y - 1 + recs[i].height);
  425.     PaintRect (&r);
  426.     }
  427.   UnlockPixels (ws->offScreenPMHandle);
  428.   SetGWorld (ws->origPort, ws->origDev);
  429. /*end quote*/
  430. }
  431.  
  432.  
  433. void UpdateCursorPos(wsp ws, wcp wc)
  434. {
  435.    wc->contextPtr->pnLoc.h = ws->x;
  436.    wc->contextPtr->pnLoc.v = ws->y;
  437.    SetPort (ws->theWindow);
  438.    MoveTo (ws->x, ws->y);
  439. }
  440.  
  441. int SetPattern (wbp w, char *name, int len)
  442. {
  443. }
  444.  
  445. /* 
  446.  * Event processing
  447.  */
  448.  
  449. int pollevent ()
  450. {
  451.     GetEvents ();
  452.     return 400;
  453. }
  454.  
  455. void GetEvents ()
  456. {
  457.    EventRecord event;
  458.    Boolean done;
  459.    struct descrip d;
  460.    int eventCode = 0;
  461.    Point newMouseLoc;
  462.    
  463.    done = false;
  464.    while (!done) {
  465.       if (WaitNextEvent (everyEvent, &event, kSleep, nil))
  466.          DoEvent (&event);
  467.       else {
  468.          if (gMouseInfo.wasDown) {
  469.             if (StillDown ()) {
  470.                GetMouse (&newMouseLoc);
  471.                if ((newMouseLoc.h != gMouseInfo.where.h) || (newMouseLoc.v != gMouseInfo.where.v)) {
  472.                   gMouseInfo.when = TickCount ();
  473.                   gMouseInfo.where = newMouseLoc;
  474.                   switch (gMouseInfo.whichButton) {
  475.                      case MOUSELEFT:
  476.                         eventCode = MOUSELEFTDRAG;
  477.                         break;
  478.                      case MOUSEMID:
  479.                         eventCode = MOUSEMIDDRAG;
  480.                         break;
  481.                      case MOUSERIGHT:
  482.                         eventCode = MOUSERIGHTDRAG;
  483.                         break;
  484.                      }
  485.                   MakeInt (eventCode, &d);
  486.                   qevent (gMouseInfo.ws, &d,
  487.                           gMouseInfo.where.h, gMouseInfo.where.v, 
  488.                           gMouseInfo.when, gMouseInfo.modKey);
  489.                   }                    /* if (newMouseLoc ...) */
  490.                }                    /* if (StillDown ...) */
  491.             else {
  492.                gMouseInfo.wasDown = false;
  493.                }
  494.             }                    /* if (wasDown ...) */
  495.          done = true;
  496.          }
  497.       }
  498. }
  499.  
  500. void DoEvent (EventRecord *eventPtr)
  501. {
  502.    char theChar;
  503.    Boolean becomingActive;
  504.   
  505.    switch (eventPtr->what) {
  506.       case mouseDown:
  507.          DoMouseDown (eventPtr);
  508.          break;
  509.       
  510.       case mouseUp:
  511.          DoMouseUp (eventPtr);
  512.          break;
  513.          
  514.       case autoKey:
  515.       case keyDown:
  516.          theChar = eventPtr->message & charCodeMask;
  517.          if ( (eventPtr->modifiers & cmdKey) != 0) 
  518.             HandleMenuChoice (MenuKey (theChar));
  519.          else
  520.             DoKey (eventPtr, FrontWindow ());
  521.          break;
  522.       
  523.       case updateEvt:
  524.          DoUpdate (eventPtr);
  525.          break;
  526.       
  527.       case activateEvt:
  528.          becomingActive = ( (eventPtr->modifiers & activeFlag) == activeFlag);
  529.          DoActivate ( (WindowPtr)eventPtr->message, becomingActive);
  530.          break;
  531.       }
  532. }
  533.  
  534. void DoMouseUp (EventRecord *eventPtr)
  535. {
  536.    WindowPtr   whichWindow;
  537.    short       thePart;
  538.    int         eventCode = 0;
  539.    int         modKey = 0;
  540.    struct descrip d;
  541.    wbp         wb;
  542.    wsp         ws;
  543.    
  544.    thePart = FindWindow (eventPtr->where, &whichWindow);
  545.    if (thePart == inContent) {
  546.       SelectWindow (whichWindow);
  547.       if ( ((eventPtr->modifiers & cmdKey) != 0) && ((eventPtr->modifiers & optionKey) ==0) )
  548.          eventCode = MOUSERIGHTUP;
  549.       if ( ((eventPtr->modifiers & optionKey) !=0) && ((eventPtr->modifiers & cmdKey) == 0) )
  550.          eventCode = MOUSEMIDUP;
  551.       if ( ((eventPtr->modifiers & optionKey) ==0) && ((eventPtr->modifiers & cmdKey) == 0) )
  552.          eventCode = MOUSELEFTUP;
  553.       MakeInt(eventCode,&d);
  554.       
  555.       if ((eventPtr->modifiers & controlKey)!=0) modKey |= ControlMask;
  556.       if ((eventPtr->modifiers & shiftKey)!=0) modKey |= ShiftMask;
  557.       if ((eventPtr->modifiers & optionKey)!=0) modKey |= Mod1Mask;
  558.       
  559.       for (wb = wbndngs; wb; wb = wb->next) {
  560.          ws = wb->window;
  561.          if (ws->theWindow == whichWindow) {
  562.             qevent(ws, &d,
  563.                    eventPtr->where.h, eventPtr->where.v, 
  564.                    (uword)(eventPtr->when), modKey);
  565.             }
  566.          }
  567.       }
  568. }
  569.       
  570. void DoMouseDown (EventRecord *eventPtr)
  571. {
  572.    WindowPtr   whichWindow;
  573.    short       thePart;
  574.    long        menuChoice;
  575.    int         eventCode = 0;
  576.    int         modKey = 0;
  577.    struct descrip d;
  578.    wbp         wb;
  579.    wsp         ws;
  580.    Point       newMouseLoc;
  581.    
  582.    thePart = FindWindow (eventPtr->where, &whichWindow);
  583.    switch (thePart) {
  584.       case inGoAway:
  585.          DisposeWindow (whichWindow);
  586.          break;
  587.             
  588.       case inMenuBar:
  589.          menuChoice = MenuSelect (eventPtr->where);
  590.          HandleMenuChoice (menuChoice);
  591.          break;
  592.     
  593.       case inSysWindow:
  594.          SystemClick (eventPtr, whichWindow);
  595.          break;
  596.       
  597.       case inDrag:
  598.          DragWindow (whichWindow, eventPtr->where, &qd.screenBits.bounds);
  599.          break;
  600.          
  601.       case inGrow:
  602.          DoGrowWindow (eventPtr, whichWindow);
  603.          break;
  604.       
  605.       case inContent:
  606.          SelectWindow (whichWindow);
  607.          SetPort (whichWindow);
  608.          GlobalToLocal (&eventPtr->where);
  609.          
  610.          /* left  = click
  611.             mid   = option-click
  612.             right = command-click */
  613.             
  614.          if ( ((eventPtr->modifiers & cmdKey) != 0) && ((eventPtr->modifiers & optionKey) ==0) )
  615.             eventCode = MOUSERIGHT;
  616.          if ( ((eventPtr->modifiers & optionKey) !=0) && ((eventPtr->modifiers & cmdKey) == 0) )
  617.             eventCode = MOUSEMID;
  618.          if ( ((eventPtr->modifiers & optionKey) ==0) && ((eventPtr->modifiers & cmdKey) == 0) )
  619.             eventCode = MOUSELEFT;
  620.          
  621.          MakeInt(eventCode,&d);
  622.          
  623.          if ((eventPtr->modifiers & controlKey)!=0) modKey |= ControlMask;
  624.          if ((eventPtr->modifiers & shiftKey)!=0) modKey |= ShiftMask;
  625.          if ((eventPtr->modifiers & optionKey)!=0) modKey |= Mod1Mask;
  626.          
  627.          for (wb = wbndngs; wb; wb = wb->next) {
  628.             ws = wb->window;
  629.             if (ws->theWindow == whichWindow) {
  630.                qevent(ws, &d,
  631.                         eventPtr->where.h, eventPtr->where.v, 
  632.                         (uword)(eventPtr->when), modKey);
  633.                gMouseInfo.ws = ws;
  634.                break;
  635.                }
  636.             }
  637.          
  638.          gMouseInfo.wasDown = true;
  639.          gMouseInfo.when = (uword)(eventPtr->when);
  640.          gMouseInfo.where = eventPtr->where;
  641.          gMouseInfo.modKey = modKey;
  642.          gMouseInfo.whichButton = eventCode;
  643.       }
  644. }
  645.  
  646. void DoKey (EventRecord *eventPtr, WindowPtr whichWindow)
  647. {  
  648.    char theChar;
  649.    int  modKey = 0;
  650.    wbp  wb;
  651.    wsp  ws;
  652.    struct descrip d;
  653.    
  654.    if (whichWindow == nil) return;
  655.    
  656.    modKey = 0;
  657.    theChar = eventPtr->message & charCodeMask;
  658.    StrLen(d) = 1;
  659.    StrLoc(d) = (char *)&allchars[theChar];
  660.    
  661.    if ((eventPtr->modifiers & controlKey)!=0) modKey |= ControlMask;
  662.    if ((eventPtr->modifiers & shiftKey)!=0) modKey |= ShiftMask;
  663.    if ((eventPtr->modifiers & optionKey)!=0) modKey |= Mod1Mask;
  664.    
  665.    for (wb = wbndngs; wb; wb = wb->next) {
  666.       ws = wb->window;
  667.       if (ws->theWindow == whichWindow) {
  668.          qevent(ws, &d,
  669.             wb->context->contextPtr->pnLoc.h,
  670.         wb->context->contextPtr->pnLoc.v,
  671.                 (uword)(eventPtr->when), modKey);
  672.          }
  673.       }
  674. }
  675.  
  676. void DoGrowWindow (EventRecord *eventPtr, WindowPtr whichWindow)
  677. {
  678.    long growSize;
  679.    Rect limitRect;
  680.    wbp wb;
  681.    wsp ws;
  682.    wcp wc;
  683.    RgnHandle locUpdateRgn;
  684.    
  685.    SetRect (&limitRect, kMinDocSize, kMinDocSize, kMaxDocSize, kMaxDocSize);
  686.    growSize = GrowWindow (whichWindow, eventPtr->where, &limitRect);
  687.  
  688.    if (growSize != 0) {
  689.       for (wb = wbndngs; wb; wb = wb->next) {
  690.          ws = wb->window;
  691.          wc = wb->context;
  692.          if (ws->theWindow == whichWindow) { 
  693.             SizeWindow (whichWindow, LoWord(growSize), HiWord(growSize), true);
  694.             InvalRect (&(ws->theWindow->portRect));
  695.             return;
  696.             }
  697.          } 
  698.       }
  699. }
  700.  
  701. void GetLocUpdateRgn (WindowPtr whichWindow, RgnHandle localRgn)
  702. {
  703.    CopyRgn (((WindowPeek)(whichWindow))->updateRgn, localRgn);
  704.    OffsetRgn (localRgn, whichWindow->portBits.bounds.left, whichWindow->portBits.bounds.top);
  705. }
  706.  
  707. void HandleMenuChoice (long menuChoice)
  708. {
  709.    short menu;
  710.    short item;
  711.    
  712.    if (menuChoice != 0) {
  713.       menu = HiWord (menuChoice);
  714.       item = LoWord (menuChoice);
  715.       
  716.       switch (menu) {
  717.          case kAppleMenu:
  718.             HandleAppleChoice (item);
  719.             break;
  720.          case kFileMenu:
  721.             HandleFileChoice (item);
  722.             break;
  723.          case kOptionsMenu:
  724.             HandleOptionsChoice (item);
  725.             break;
  726.          }
  727.       HiliteMenu (0);
  728.       }
  729. }
  730.  
  731. void HandleAppleChoice (short item)
  732. {
  733.    MenuHandle  appleMenu;
  734.    Str255      accName;
  735.    short       accNumber;
  736.    
  737.    switch (item) {
  738.       case kAboutMItem:
  739.          SysBeep (20);
  740.          break;
  741.          /* ******************* open a dialog box **************** */
  742.       default:
  743.          appleMenu = GetMHandle (kAppleMenu);
  744.          GetItem (appleMenu, item, accName);
  745.          accNumber = OpenDeskAcc (accName);
  746.          break;
  747.       }
  748. }
  749.  
  750. void HandleFileChoice (short item)
  751. {
  752.    StandardFileReply fileReply;
  753.    SFTypeList typeList;
  754.    short numTypes;
  755.    char *fileName;
  756.    int argc;
  757.    char *argv[kNARGS+1];
  758.    char buf[256];
  759.    char *tmpStr;
  760.    MenuHandle menu;
  761.    
  762.    switch (item) {
  763.       case kQuitMItem:
  764.          abort ();
  765.          break;
  766.       case kRunMItem:
  767.          typeList[0] = 'ICOD';
  768.          numTypes = 1;
  769.          StandardGetFile (nil, numTypes, typeList, &fileReply);
  770.          if (fileReply.sfGood) {
  771.             fileName = PtoCstr (fileReply.sfFile.name);
  772.             menu = GetMHandle (kFileMenu);
  773.             DisableItem (menu, kRunMItem);
  774.             menu = GetMHandle (kOptionsMenu);
  775.             DisableItem (menu, 0);
  776.             textHandle = GetString(kStringID);
  777.             HLock((Handle)textHandle);
  778.             SetString (textHandle, "\p");
  779.             HUnlock((Handle)textHandle);
  780.             ChangedResource((Handle)textHandle);
  781.             WriteResource((Handle)textHandle);
  782.             }
  783.          else
  784.             break;
  785.          
  786.          tmpStr = malloc (strlen ("ICONX ") + strlen (fileName) + 1 + strlen (cmlArgs) + 1);
  787.          strcpy (tmpStr, "ICONX ");
  788.          strcat (tmpStr, fileName);
  789.          strcat (tmpStr, " ");
  790.          strcat (tmpStr, cmlArgs);
  791.          sprintf (buf, "%#s", tmpStr);
  792.          argc = ParseCmdLineStr (buf, tmpStr, argv);
  793.          
  794.          MacMain (argc, argv);
  795.          break;
  796.       }
  797. }
  798.  
  799. void HandleOptionsChoice (short item)
  800. {
  801.    StandardFileReply fileReply;
  802.    SFTypeList typeList;
  803.    short numTypes;
  804.    char *fileName;
  805.    DialogPtr dialog;
  806.    Boolean dialogDone;
  807.    short itemHit, itemType;
  808.    Handle okItemHandle, textItemHandle;
  809.    Rect itemRect;
  810.    char *tmpStr;
  811.    Str255 itemText;
  812.    
  813.    switch (item) {
  814.       case kRInMItem:
  815.          typeList[0] = 'TEXT';
  816.          numTypes = 1;
  817.          StandardGetFile (nil, numTypes, typeList, &fileReply);
  818.          if (fileReply.sfGood) {
  819.             fileName = PtoCstr (fileReply.sfFile.name);
  820.             k_input.fd = fopen (fileName, "r");
  821.             /*
  822.             StrLen (k_input.fname) = strlen (fileName) + 1;
  823.             */
  824.             }
  825.          break;
  826.       case kROutMItem:
  827.          StandardPutFile ("\pSave output file as:", "\poutput.txt", &fileReply);
  828.          if (fileReply.sfGood) {
  829.             fileName = PtoCstr (fileReply.sfFile.name);
  830.             /*
  831.             setfile (fileName, 'TEXT', 'ttxt');
  832.             */
  833.             k_output.fd = freopen (fileName, "w", stdout);
  834.             StrLen (k_output.fname) = strlen (fileName) + 1;
  835.             }
  836.          break;
  837.       case kArgsMItem:
  838.          dialogDone = false;
  839.          dialog = GetNewDialog (kCLArgsDialog, nil, kMoveToFront);
  840.  
  841.          GetDItem (dialog, kArgStringField, &itemType, &textItemHandle, &itemRect);
  842.          GetDItem (dialog, ok, &itemType, &okItemHandle, &itemRect);
  843.          
  844.          textHandle = GetString (kStringID);
  845.          HLock ((Handle) textHandle);
  846.          SetIText (textItemHandle, *textHandle);
  847.          HUnlock ((Handle) textHandle);
  848.          SelIText (dialog, kArgStringField, 0, 32767);
  849.             
  850.          ShowWindow (dialog);
  851.          SetPort (dialog);
  852.          
  853.          SetDialogDefaultItem (dialog, ok);
  854.          SetDialogCancelItem (dialog, cancel);
  855.          SetDialogTracksCursor (dialog, true);
  856.  
  857.          while (!dialogDone) {
  858.             GetIText (textItemHandle, itemText);
  859.             if (itemText [0] == 0)
  860.                HiliteControl ((ControlHandle)okItemHandle, kDisableButton);
  861.             else
  862.                HiliteControl ((ControlHandle)okItemHandle, kEnableButton);
  863.             ModalDialog (nil, &itemHit);
  864.             dialogDone = ((itemHit == ok) || (itemHit == cancel));
  865.             }
  866.          if (itemHit == ok) {
  867.             GetIText (textItemHandle, itemText);
  868.             
  869.             SetHandleSize ((Handle) textHandle, (Size) (itemText[0] + 1));
  870.             HLock ((Handle) textHandle);
  871.             GetIText (textItemHandle, *textHandle);
  872.             HUnlock ((Handle) textHandle);
  873.             ChangedResource ((Handle) textHandle);
  874.             WriteResource ((Handle) textHandle);
  875.             
  876.             tmpStr = PtoCstr (itemText);
  877.             cmlArgs = malloc (strlen (tmpStr) + 1);
  878.             strcpy (cmlArgs, tmpStr);
  879.             DisposeDialog (dialog);
  880.             }
  881.          else {
  882.             DisposeDialog (dialog);
  883.             }
  884.          break;
  885.       }
  886. }
  887.       
  888. void DoUpdate (EventRecord *eventPtr)
  889. {
  890.    WindowPtr  whichWindow;
  891.    
  892.    whichWindow = (WindowPtr)eventPtr->message;
  893.  
  894.    BeginUpdate (whichWindow);
  895.    RedrawWindow (whichWindow);
  896.    EndUpdate (whichWindow);
  897. }
  898.  
  899. void DoActivate (WindowPtr whichWindow, Boolean becomingActive)
  900. {
  901. }
  902.  
  903. void RedrawWindow (WindowPtr whichWindow)
  904. {
  905.    wsp ws;
  906.    wcp wc;
  907.    wbp wb;
  908.    GWorldFlags updateOK;
  909.    
  910.    for (wb = wbndngs; wb; wb = wb->next) {
  911.       ws = wb->window;
  912.       wc = wb->context;
  913.       if (ws->theWindow == whichWindow) {
  914.          updateOK = UpdateGWorld(&(ws->offScreenGWorld), 8, &(ws->GWorldRect), nil, nil, 0);
  915.          SetPort(ws->theWindow);
  916.          EraseRect(&(ws->theWindow->portRect));
  917.          ws->sourceRect=ws->theWindow->portRect;
  918.          ws->destRect=ws->theWindow->portRect;
  919.          ws->lockOK = LockPixels (ws->offScreenPMHandle);
  920.          CopyBits(&(((GrafPtr)(ws->offScreenGWorld))->portBits),
  921.                   &(((GrafPtr)(ws->theWindow))->portBits),
  922.                   &(ws->sourceRect),&(ws->destRect),transparent, nil);
  923.          UnlockPixels(ws->offScreenPMHandle);
  924.          }
  925.       }
  926. }
  927.  
  928. static int ParseCmdLineStr(char *s, char *t, char **argv)
  929. {
  930.     int c, quote = 0, argc = 0;
  931.         
  932.     while (c = *s++) {
  933.         if (c == ' ')
  934.             continue;
  935.         if (argc < kNARGS)
  936.             argv[argc++] = t;
  937.         do {
  938.             if (c == '\\' && *s)
  939.                 c = *s++;
  940.             else if (c == '"' || c == '\'') {
  941.                 if (!quote) {
  942.                     quote = c;
  943.                     continue;
  944.                 }
  945.                 if (c == quote) {
  946.                     quote = 0;
  947.                     continue;
  948.                 }
  949.             }
  950.             *t++ = c;
  951.         } while (*s && ((c = *s++) != ' ' || quote));
  952.         *t++ = 0;
  953.     }
  954.     return(argc);
  955. }
  956.  
  957. /*
  958. void setfile(filename,type,creator)
  959. char *filename;
  960. OSType type,creator;
  961.    {
  962.    FInfo info;
  963.  
  964.    if (getfinfo(filename,0,&info) == 0) {
  965.       info.fdType = type;
  966.       info.fdCreator = creator;
  967.       setfinfo(filename,0,&info);
  968.       }
  969.    return;
  970.    }
  971. */
  972.