home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 September / PCO_0998.ISO / filesbbs / frei / palmsrc.arj / PALMSRC.ZIP / AppStdIO.c next >
Encoding:
C/C++ Source or Header  |  1998-07-31  |  22.8 KB  |  1,022 lines

  1. /***********************************************************************
  2.  *
  3.  *    Copyright (c) Palm Computing 1997 -- All Rights Reserved ip_rcv
  4.  *
  5.  * PROJECT:  Any Pilot app that wants a stdio window
  6.  * FILE:     AppStdIO.c
  7.  *
  8.  * DESCRIPTION:
  9.  *      This module implements stdin/stdout support for a pilot application.
  10.  * It requires that the application provide a form with a text field and
  11.  * scroll bar
  12.  *
  13.  *   The application code must call StdInit() and pass the resource ID's
  14.  * of the form, field, and scroll bar along with other settings
  15.  * before using any of the other calls.
  16.  *
  17.  *   See "AppStdIO.h" for a complete description of how to use this
  18.  *    module. 
  19.  *
  20.  **********************************************************************/
  21.  
  22.  
  23. #include <SysAll.h>
  24. #include <UIAll.h>
  25. #include <KeyMgr.h>
  26.  
  27. #include <unix_stdarg.h>
  28.  
  29. #include "AppStdIO.h"
  30.  
  31.  
  32.  
  33. #if EMULATION_LEVEL == EMULATION_NONE
  34.  
  35. /*********************************************************************
  36.  * This stuff is needed if building for the device.
  37.  *******************************************************************/
  38.  
  39.  
  40. /*********************************************************************
  41.  * Globals
  42.  * These are initialized by parameters passed to StdInit().
  43.  *******************************************************************/
  44. static VoidHand PrvTextH = NULL;      // holds latest text
  45.  
  46. static Word         PrvFormID = 0;        // Form ID that contains text field
  47. static Word         PrvFieldID = 0;        // Field ID 
  48. static Word         PrvScrollerID = 0;
  49.  
  50. // Routine provided by app for processing commands
  51. void                 (*PrvAppProcessCommand)(Char* cmdP);
  52.  
  53.  
  54.  
  55. /***********************************************************************
  56.  *
  57.  * FUNCTION:        PrvCloneStr
  58.  *
  59.  * DESCRIPTION:    Allocates a handle and stuffs srcStr into it.
  60.  *
  61.  * CALLED BY:         
  62.  *
  63.  * PARAMETERS:        srcStr - pointer to string to initialize handle with
  64.  *
  65.  * RETURNED:         
  66.  *
  67.  ***********************************************************************/
  68. static Handle PrvCloneStr(CharPtr srcStr)
  69. {
  70.     Word        len;
  71.     VoidHand     stringH;
  72.     CharPtr     stringP;
  73.     
  74.     len = StrLen(srcStr);
  75.     if (!len) return NULL;
  76.     
  77.     stringH = MemHandleNew((ULong)(len+1));
  78.     if (stringH) {
  79.         stringP = MemHandleLock(stringH);
  80.         StrCopy(stringP,srcStr);
  81.         MemPtrUnlock(stringP);
  82.         }
  83.     return (Handle)stringH;
  84. }
  85.  
  86.  
  87.  
  88. /***********************************************************************
  89.  *
  90.  * FUNCTION:        PrvHandleStrCat
  91.  *
  92.  * DESCRIPTION:    Catenates a string into an existing handle
  93.  *
  94.  * CALLED BY:         
  95.  *
  96.  * PARAMETERS:        stringH     - existing handle with text
  97.  *                        srcP        - pointer to text to catenate
  98.  *
  99.  * RETURNED:         
  100.  *
  101.  ***********************************************************************/
  102. static void PrvHandleStrCat(VoidHand stringH, CharPtr srcP)
  103. {
  104.     Word newSize,oldLength;
  105.     CharPtr stringP;
  106.     
  107.     newSize = StrLen(srcP) + 1;                    // initialize new size
  108.     
  109.     // Resize the existing handle, if any
  110.     if ( stringH ) {
  111.         stringP = MemHandleLock( stringH );
  112.         oldLength = StrLen(stringP);
  113.         newSize += oldLength;
  114.         MemPtrUnlock( stringP );
  115.         if ( MemHandleResize(stringH, newSize) == 0) {    // Append the new text
  116.             stringP = MemHandleLock( stringH );
  117.             StrCopy( stringP + oldLength, srcP );            // copy the new text
  118.             MemPtrUnlock( stringP );
  119.             }
  120.         }
  121. }
  122.  
  123.  
  124.  
  125. /***********************************************************************
  126.  *
  127.  * FUNCTION:        StdVPrintF
  128.  *
  129.  * DESCRIPTION:    Utility used to implement printf and fprintf
  130.  *
  131.  * CALLED BY:        StdPrintF, StdFPrintF
  132.  *
  133.  * PARAMETERS:        formatStr - format string
  134.  *                        args         - pointer to argument list.
  135.  *
  136.  * RETURNED:        # of characters sent to stdout.
  137.  *
  138.  ***********************************************************************/
  139. int    StdVPrintF(const CharPtr formatStr, va_list args)
  140. {
  141.     SWord                result;
  142.     const    UInt        textLen = 0x1FF;
  143.     static Char        text[0x1FF+1]; // static so we don't eat up stack space
  144.     
  145.     // Crude check for overflow on the formatStr
  146.     if (StrLen(formatStr) < textLen/2) {
  147.         result = StrVPrintF(text, formatStr, args);
  148.         StdPutS(text);
  149.         }
  150.     else {
  151.         StdPutS(formatStr);
  152.         result = StrLen(formatStr);
  153.         }
  154.         
  155.     return result;
  156. }
  157.  
  158.  
  159. /***********************************************************************
  160.  *
  161.  * FUNCTION:    PrvGetObjectPtr
  162.  *
  163.  * DESCRIPTION: This routine returns a pointer to an object in the current
  164.  *              form.
  165.  *
  166.  * PARAMETERS:  objectID - which object.
  167.  *
  168.  * RETURNED:    nothing
  169.  *
  170.  ***********************************************************************/
  171. static VoidPtr PrvGetObjectPtr (Word objectID)
  172. {
  173.     FormPtr frm;
  174.     VoidPtr obj;
  175.     
  176.     frm = FrmGetActiveForm ();
  177.     obj = FrmGetObjectPtr (frm, FrmGetObjectIndex (frm, objectID));
  178.  
  179.     return obj;
  180. }
  181.  
  182.  
  183.  
  184.  
  185. /***********************************************************************
  186.  *
  187.  * FUNCTION:    SetFieldText
  188.  *
  189.  * DESCRIPTION:    Set field object's text handle.  Will reuse an existing
  190.  *                        text handle, if any
  191.  *
  192.  * PARAMETERS:    fldID        -- field object id
  193.  *                    srcP        -- source text pointer
  194.  *                    append    -- if true, the new text will be appended
  195.  *                    redraw    -- if true, field will be redrawn
  196.  *
  197.  * RETURNED:    nothing.
  198.  *
  199.  * REVISION HISTORY:
  200.  *
  201.  ***********************************************************************/
  202. static void PrvSetFieldText(Word fldID, CharPtr srcP, Boolean append,
  203.                                 Boolean redraw)
  204. {
  205.     Handle         stringH;
  206.     UInt            oldLength = 0;
  207.     UInt            newSize;
  208.     CharPtr         stringP;
  209.     FieldPtr        fldP;
  210.  
  211.     fldP = (FieldPtr)PrvGetObjectPtr(fldID);
  212.  
  213.     if (!srcP || !*srcP) {                        // handle clearing field as well
  214.         FldFreeMemory(fldP);
  215.         if ( redraw )     {
  216.             FldEraseField( fldP );
  217.             FldDrawField( fldP );
  218.             }
  219.         return;
  220.         }
  221.     
  222.     
  223.     newSize = StrLen(srcP) + 1;                    // initialize new size
  224.     stringH = FldGetTextHandle( fldP );            // get the current text handle
  225.     FldSetTextHandle( fldP, 0 );                    // release this handle from field
  226.  
  227.     // Resize the existing handle, if any
  228.     if ( stringH ) {
  229.         if ( append ) {
  230.             stringP = MemHandleLock( (VoidHand)stringH );
  231.             oldLength = StrLen(stringP);
  232.             newSize += oldLength;
  233.             MemPtrUnlock( stringP );
  234.             }
  235.         if ( MemHandleResize((VoidHand)stringH, newSize) )
  236.             goto Exit;
  237.         } // Resize the existing handle, if any
  238.     
  239.     // Otherwise, allocate a new handle
  240.     else {
  241.         stringH = (Handle)MemHandleNew( newSize );        // allocate a new chunk
  242.         if ( !stringH )    return;
  243.         }
  244.  
  245.     // Append the new text
  246.     stringP = MemHandleLock( (VoidHand)stringH );
  247.     StrCopy( stringP + oldLength, srcP );        // copy the new text
  248.     MemPtrUnlock( stringP );
  249.     
  250. Exit:
  251.     FldSetTextHandle( fldP, stringH );            // set the new text handle
  252.     if ( redraw )     {
  253.         FldEraseField( fldP );
  254.         FldDrawField( fldP );
  255.         }
  256. }
  257.  
  258.  
  259.  
  260. /***********************************************************************
  261.  *
  262.  * FUNCTION:    PrvFormUpdateScroller
  263.  *
  264.  * DESCRIPTION: This routine draws or erases the status view scrollbar
  265.  *
  266.  * PARAMETERS:  frm             -  pointer to the status form
  267.  *
  268.  * RETURNED:    nothing
  269.  *
  270.  * REVISION HISTORY:
  271.  *            Name    Date        Description
  272.  *            ----    ----        -----------
  273.  *            roger    8/7/95    Initial Revision
  274.  *
  275.  ***********************************************************************/
  276. static void PrvFormUpdateScroller (FormPtr frm)
  277. {
  278.     Word                 scrollPos;
  279.     Word                 textHeight;
  280.     Word                 fieldHeight;
  281.     Short             maxValue;
  282.     FieldPtr         fld;
  283.     ScrollBarPtr     bar;
  284.  
  285.     fld = FrmGetObjectPtr (frm, FrmGetObjectIndex (frm, PrvFieldID));
  286.     bar = FrmGetObjectPtr (frm, FrmGetObjectIndex (frm, PrvScrollerID));
  287.     
  288.     FldGetScrollValues (fld, &scrollPos, &textHeight,  &fieldHeight);
  289.  
  290.     if (textHeight > fieldHeight)
  291.         maxValue = textHeight - fieldHeight;
  292.     else
  293.         maxValue = 0;
  294.  
  295.     SclSetScrollBar (bar, scrollPos, 0, maxValue, fieldHeight-1);
  296. }
  297.  
  298.  
  299.  
  300. /***********************************************************************
  301.  *
  302.  * FUNCTION:    PrvFieldScroll
  303.  *
  304.  * DESCRIPTION: Scrolls the status view a page or a 
  305.  *              line at a time.
  306.  *
  307.  * PARAMETERS:  direction - up or dowm
  308.  *              oneLine   - true if scrolling a single line
  309.  *
  310.  * RETURNED:    nothing
  311.  *
  312.  * REVISION HISTORY:
  313.  *            Name    Date        Description
  314.  *            ----    ----        -----------
  315.  *            roger    8/7/95    Initial Revision
  316.  *
  317.  ***********************************************************************/
  318. static void PrvFieldScroll (SWord linesToScroll)
  319. {
  320.     /*
  321.     fld = PrvGetObjectPtr (PrvFieldID);
  322.     if (FldScrollable (fld, direction)) {
  323.         if (oneLine)
  324.             linesToScroll = 1;
  325.         else
  326.             linesToScroll = FldGetVisibleLines (fld) - 1;
  327.             
  328.         FldScrollField (fld, linesToScroll, direction);
  329.         PrvFormUpdateScroller (FrmGetActiveForm());
  330.         }
  331.     */
  332.  
  333.  
  334.     Word                blankLines;
  335.     Short                min;
  336.     Short                max;
  337.     Short                value;
  338.     Short                pageSize;
  339.     FieldPtr            fld;
  340.     ScrollBarPtr    bar;
  341.     
  342.     fld = PrvGetObjectPtr (PrvFieldID);
  343.  
  344.     if (linesToScroll < 0) {
  345.         blankLines = FldGetNumberOfBlankLines (fld);
  346.         FldScrollField (fld, -linesToScroll, up);
  347.         
  348.         // If there were blank lines visible at the end of the field
  349.         // then we need to update the scroll bar.
  350.         if (blankLines) {
  351.             // Update the scroll bar.
  352.             bar = PrvGetObjectPtr (PrvScrollerID);
  353.             SclGetScrollBar (bar, &value, &min, &max, &pageSize);
  354.             if (blankLines > -linesToScroll)
  355.                 max += linesToScroll;
  356.             else
  357.                 max -= blankLines;
  358.             SclSetScrollBar (bar, value, min, max, pageSize);
  359.             }
  360.         }
  361.  
  362.     else if (linesToScroll > 0)
  363.         FldScrollField (fld, linesToScroll, down);
  364.  
  365. }
  366.  
  367. /***********************************************************************
  368.  *
  369.  * FUNCTION:    PrvFieldPageScroll
  370.  *
  371.  * DESCRIPTION: Scroll the owner message a page up or down.
  372.  *
  373.  * PARAMETERS:  direction - up or down
  374.  *
  375.  * RETURNED:    nothing
  376.  *
  377.  * REVISION HISTORY:
  378.  *            Name    Date        Description
  379.  *            ----    ----        -----------
  380.  *            rsf    8/7/96    Initial Revision
  381.  *
  382.  ***********************************************************************/
  383. static void PrvFieldPageScroll (DirectionType direction)
  384. {
  385.     Short value;
  386.     Short min;
  387.     Short max;
  388.     Short pageSize;
  389.     Word linesToScroll;
  390.     FieldPtr fld;
  391.     ScrollBarPtr bar;
  392.  
  393.     fld = PrvGetObjectPtr (PrvFieldID);
  394.     
  395.     if (FldScrollable (fld, direction)) {
  396.         linesToScroll = FldGetVisibleLines (fld) - 1;
  397.         FldScrollField (fld, linesToScroll, direction);
  398.  
  399.         // Update the scroll bar.
  400.         bar = PrvGetObjectPtr (PrvScrollerID);
  401.         SclGetScrollBar (bar, &value, &min, &max, &pageSize);
  402.  
  403.         if (direction == up)
  404.             value -= linesToScroll;
  405.         else
  406.             value += linesToScroll;
  407.         
  408.         SclSetScrollBar (bar, value, min, max, pageSize);
  409.         return;
  410.         }
  411. }
  412.  
  413.  
  414. /***********************************************************************
  415.  *
  416.  * FUNCTION:    PrvFormInit
  417.  *
  418.  * DESCRIPTION: This routine initializes the "Status View"
  419.  *
  420.  * PARAMETERS:  frm  - a pointer to the Status form
  421.  *
  422.  * RETURNED:    nothing
  423.  *
  424.  * REVISION HISTORY:
  425.  *            Name    Date        Description
  426.  *            ----    ----        -----------
  427.  *            gavin    7/15/96    Initial Revision
  428.  *
  429.  ***********************************************************************/
  430. static void PrvFormInit (FormPtr frm)
  431. {
  432.     FieldAttrType     attr;
  433.     FieldPtr         fldP;
  434.  
  435.  
  436.     if (!PrvTextH)
  437.         PrvTextH = (VoidHand)PrvCloneStr(" ");
  438.         
  439.     // Get the field ptr
  440.     fldP = FrmGetObjectPtr (frm, FrmGetObjectIndex (frm, PrvFieldID));
  441.     
  442.     // Indicate that it has a scroll bar
  443.     FldGetAttributes (fldP, &attr);
  444.     attr.hasScrollBar = true;
  445.     FldSetAttributes (fldP, &attr);
  446.     
  447.     // Initialize it's text
  448.     FldSetTextHandle( fldP, (Handle)PrvTextH );            // set the new text handle
  449.     
  450.     // No longer need background copy
  451.     PrvTextH = 0;
  452.     
  453. }
  454.  
  455.  
  456. /***********************************************************************
  457.  *
  458.  * FUNCTION:    PrvFormClose
  459.  *
  460.  * DESCRIPTION: Closes down the stdio form
  461.  *
  462.  * PARAMETERS:  frm  - a pointer to the Status form
  463.  *
  464.  * RETURNED:    nothing
  465.  *
  466.  * REVISION HISTORY:
  467.  *            Name    Date        Description
  468.  *            ----    ----        -----------
  469.  *            gavin    7/15/96    Initial Revision
  470.  *
  471.  ***********************************************************************/
  472. static void PrvFormClose (FormPtr frm)
  473. {
  474.     FieldPtr fldP;
  475.     
  476.     fldP = FrmGetObjectPtr (frm, FrmGetObjectIndex (frm, PrvFieldID));
  477.     
  478.     // Save the current handle
  479.     if (PrvTextH) MemHandleFree(PrvTextH);
  480.     PrvTextH = (VoidHand)FldGetTextHandle(fldP);
  481.     
  482.     // Take it out of the field object.
  483.     FldSetTextHandle( fldP, NULL );            // remove the handle so it 
  484.                                                         //  isn't freed
  485. }
  486.  
  487.  
  488. /***********************************************************************
  489.  *
  490.  * FUNCTION:    PrvFormDraw
  491.  *
  492.  * DESCRIPTION: This routine draws the "Sync View"
  493.  *
  494.  * PARAMETERS:  frm - pointer to the view form.
  495.  *
  496.  * RETURNED:    nothing.
  497.  *
  498.  * REVISION HISTORY:
  499.  *            Name    Date        Description
  500.  *            ----    ----        -----------
  501.  *            vmk    10/16/95    Initial Revision
  502.  *
  503.  ***********************************************************************/
  504. static void PrvFormDraw(FormPtr frm)
  505. {
  506.     PrvFormUpdateScroller ( frm );
  507. }
  508.  
  509.  
  510.  
  511. /***********************************************************************
  512.  *
  513.  * FUNCTION:    PrvProcessCommand
  514.  *
  515.  * DESCRIPTION: This routine grabs the current line of the text field
  516.  *            and sends it to the application's command processor.
  517.  *
  518.  * PARAMETERS:  void
  519.  *
  520.  * RETURNED:    nothing.
  521.  *
  522.  * REVISION HISTORY:
  523.  *
  524.  ***********************************************************************/
  525. static void PrvProcessCommand(void)
  526. {
  527.     CharPtr         textP,p;
  528.     SWord         i,j;
  529.     const int     cmdBufSize = 50;             // max allowed lenght for a command line
  530.    char             cmdBuf[cmdBufSize+1];   // buffer for holding command line
  531.    Handle        textH;
  532.     FieldPtr        fldP=0;
  533.  
  534.  
  535.     // Get the text handle
  536.     if (FrmGetActiveFormID() == PrvFormID) {
  537.         fldP = PrvGetObjectPtr(PrvFieldID);
  538.         textH = FldGetTextHandle(fldP);
  539.  
  540.         }
  541.     else {
  542.         textH = (Handle)PrvTextH;
  543.         }
  544.  
  545.    // parse text for command line interface here....
  546.    if (!textH) return;
  547.     textP = MemHandleLock((VoidHand)textH);
  548.    
  549.    if (fldP)
  550.        i = FldGetInsPtPosition(fldP) - 2;
  551.    else
  552.        i = StrLen(textP)-2;          // skip null and final linefeed
  553.    
  554.    
  555.    // scan back to previous line or start of text
  556.    while (i>0 && textP[i] != linefeedChr) i--;
  557.    
  558.    if (textP[i] == linefeedChr) i++;          // do not include the linefeed...
  559.    if (textP[i] == '>') i++;                  // skip over prompt...
  560.    
  561.    // copy into command buffer leaving out final linefeed
  562.     for (p=&textP[i],j=0; *p && *p!=linefeedChr && j<cmdBufSize;)
  563.         cmdBuf[j++] = *p++;
  564.     cmdBuf[j] = 0;
  565.     MemHandleUnlock((VoidHand)textH);
  566.     
  567.     
  568.     // Call app's command handler
  569.     if (PrvAppProcessCommand)
  570.         (*PrvAppProcessCommand)(cmdBuf);
  571. }
  572.  
  573.  
  574.  
  575. /***********************************************************************
  576.  *
  577.  * FUNCTION:        StdColumn
  578.  *
  579.  * DESCRIPTION:    Utility used to move the insertion point to the given column
  580.  *
  581.  * CALLED BY:        Applications.
  582.  *
  583.  * PARAMETERS:        col - which column to move to
  584.  *
  585.  * RETURNED:        void
  586.  *
  587.  ***********************************************************************/
  588. void StdColumn (Word column)
  589. {    
  590.  
  591. }
  592.  
  593.  
  594. /***********************************************************************
  595.  *
  596.  * FUNCTION:        StdPutS
  597.  *
  598.  * DESCRIPTION:    Utility used to print a string to stdout
  599.  *
  600.  * CALLED BY:        Applications.
  601.  *
  602.  * PARAMETERS:        str - string to print
  603.  *
  604.  * RETURNED:        void
  605.  *
  606.  ***********************************************************************/
  607. void StdPutS (CharPtr str)
  608. {    
  609.     FieldPtr     fldP;
  610.     Word            maxSize;
  611.     CharPtr        textP;
  612.     Word            pos;
  613.     
  614.     if (FrmGetActiveFormID() == PrvFormID) {
  615.         fldP = (FieldPtr)PrvGetObjectPtr(PrvFieldID);
  616.         
  617.         // Make sure there's room for this new text
  618.         maxSize = FldGetMaxChars(fldP);
  619.         textP = FldGetTextPtr(fldP);
  620.         
  621.         // If not enough room, lop off the top.
  622.         if (textP) {
  623.             if (StrLen(textP) + StrLen(str) >= maxSize) {
  624.                 RectangleType    clipR, savedClipR;
  625.  
  626.                 // Clip off everything so that field package doesn't
  627.                 //  do a redraw during FldDelete()
  628.                 WinGetClip (&savedClipR);
  629.                 clipR = savedClipR;
  630.                 clipR.extent.x = clipR.extent.y = 0;
  631.                 WinSetClip (&clipR);
  632.  
  633.                 // Lop off the top
  634.                 pos = FldGetInsPtPosition(fldP);
  635.                 FldDelete(fldP, 0, maxSize/4);
  636.                 FldSetInsPtPosition(fldP, pos);
  637.  
  638.                 // Restore clip region.
  639.                 WinSetClip (&savedClipR);
  640.                 }
  641.             }
  642.         
  643.         // Insert the new text
  644.         FldInsert(fldP, str, StrLen(str));
  645.         PrvFormUpdateScroller (FrmGetActiveForm());
  646.         }
  647.         
  648.     else  {    
  649.         if (PrvTextH)
  650.             PrvHandleStrCat(PrvTextH, str);
  651.         else
  652.             PrvTextH = (VoidHand)PrvCloneStr(str);
  653.         }
  654. }
  655.  
  656.  
  657.  
  658. /***********************************************************************
  659.  *
  660.  * FUNCTION:        StdClear
  661.  *
  662.  * DESCRIPTION:    Clear the "screen"
  663.  *
  664.  * CALLED BY:        Applications.
  665.  *
  666.  * PARAMETERS:        void
  667.  *
  668.  * RETURNED:        void
  669.  *
  670.  ***********************************************************************/
  671. void StdClear(void)
  672. {
  673.     if (FrmGetActiveFormID() == PrvFormID) {
  674.         PrvSetFieldText(PrvFieldID, NULL, false, true);
  675.         PrvFormUpdateScroller (FrmGetActiveForm());
  676.         }
  677.     else {
  678.         if (PrvTextH) MemHandleFree(PrvTextH);
  679.         PrvTextH = NULL;
  680.         }
  681. }
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688. /******************************************************************************
  689.  * The "printf" routine
  690.  *****************************************************************************/
  691. int    StdPrintF(const CharPtr formatStr, ...)
  692. {
  693.     va_list            args;
  694.     SWord                result;
  695.     
  696.     va_start(args, formatStr);
  697.     result = StdVPrintF(formatStr, args);
  698.     va_end(args);
  699.     
  700.     return result;
  701. }
  702.  
  703.  
  704. /******************************************************************************
  705.  * The "fprintf" routine
  706.  *****************************************************************************/
  707. int    StdFPrintF(unsigned char* fd, const CharPtr formatStr, ...)
  708. {
  709.     va_list            args;
  710.     SWord                result;
  711.     
  712.     va_start(args, formatStr);
  713.     result = StdVPrintF(formatStr, args);
  714.     va_end(args);
  715.     
  716.     return result;
  717. }
  718.  
  719.  
  720. /***********************************************************************
  721.  *
  722.  * FUNCTION:    StdHandleEvent
  723.  *
  724.  * DESCRIPTION: Handles events in the form that contains the
  725.  *        stdio text field and scroll arrows if the event belongs to
  726.  *        the text field or scroll arrows.
  727.  *
  728.  *        This routine should be called from the form event handler
  729.  *        before it does it's own processing with any of the non stdio 
  730.  *        objects in the form.
  731.  *
  732.  * PARAMETERS:  event  - a pointer to an EventType structure
  733.  *
  734.  * RETURNED:    true if the event was handled and should not be 
  735.  *                        processed by the app's own form event handler. 
  736.  *
  737.  * REVISION HISTORY:
  738.  *            Name    Date        Description
  739.  *            ----    ----        -----------
  740.  *            roger    8/7/95    Initial Revision
  741.  *
  742.  ***********************************************************************/
  743. Boolean StdHandleEvent (EventPtr event)
  744. {
  745.     FormPtr             frmP;
  746.     Boolean             handled = false;
  747.  
  748.  
  749.     if (event->eType == keyDownEvent) {
  750.  
  751.         if ( event->data.keyDown.chr == pageUpChr ) {
  752.             PrvFieldPageScroll (up);
  753.             handled = true;
  754.             }
  755.         
  756.         else if ( event->data.keyDown.chr == pageDownChr ) {
  757.             PrvFieldPageScroll (down);
  758.             handled = true;
  759.             }
  760.  
  761.         else {    
  762.             frmP = FrmGetActiveForm ();
  763.             FrmHandleEvent (frmP, event);
  764.             PrvFormUpdateScroller (frmP);
  765.             if ( event->data.keyDown.chr == linefeedChr )    
  766.                 PrvProcessCommand();
  767.             handled = true;
  768.             }
  769.         }
  770.  
  771.  
  772.     else if (event->eType == sclRepeatEvent)
  773.         {
  774.         PrvFieldScroll (event->data.sclRepeat.newValue - 
  775.             event->data.sclRepeat.value);
  776.         }
  777.  
  778.  
  779.  
  780.     else if (event->eType == frmUpdateEvent) {
  781.         PrvFormDraw( FrmGetActiveForm() );
  782.         }
  783.         
  784.     else if (event->eType == fldChangedEvent) {
  785.         PrvFormUpdateScroller( FrmGetActiveForm() );
  786.         handled = true;
  787.         }
  788.         
  789.         
  790.     else if (event->eType == frmOpenEvent) {
  791.         frmP = FrmGetActiveForm();
  792.         PrvFormInit( frmP );
  793.         PrvFormDraw( frmP );
  794.         FrmSetFocus( frmP, FrmGetObjectIndex (frmP, PrvFieldID)) ;
  795.         }
  796.         
  797.         
  798.     else if (event->eType == frmCloseEvent) {
  799.         frmP = FrmGetActiveForm();
  800.         PrvFormClose(frmP);
  801.         }
  802.         
  803.     return (handled);
  804. }
  805.  
  806.  
  807. /***********************************************************************
  808.  *
  809.  * FUNCTION:        StdGetChar
  810.  *
  811.  * DESCRIPTION:    Waits till the user types a character. Ignores all
  812.  *            other types of events. Returns character or -1 if an 
  813.  *            appStop event is encountered or if the user enters the
  814.  *            '~' character.
  815.  *
  816.  * CALLED BY:        Applications.
  817.  *
  818.  * PARAMETERS:         
  819.  *
  820.  * RETURNED:         
  821.  *
  822.  ***********************************************************************/
  823. Int    StdGetChar(Boolean echo)
  824. {
  825.     EventType    event;
  826.     Word            key;
  827.     Char            str[2];
  828.         
  829.     str[1] = 0;
  830.     
  831.     while (1) {
  832.         // Check for system events
  833.         EvtGetEvent(&event, evtWaitForever);
  834.         if (SysHandleEvent(&event)) continue;
  835.         
  836.         // On appStop, return end of file
  837.         if (event.eType == appStopEvent) return -1;
  838.         
  839.         // If not a key event, pass to form handler
  840.         if (event.eType != keyDownEvent) {
  841.             FrmDispatchEvent(&event);
  842.             continue;
  843.             }
  844.         
  845.         // Get the key
  846.         key = event.data.keyDown.chr;
  847.         
  848.         // Treat ~ as end-of-file 
  849.         if (key == '~') return -1;
  850.         
  851.         // If echo, display it
  852.         if (echo) {
  853.             str[0] = key;
  854.             if (key == 8)
  855.                 StdBS();
  856.             else
  857.                 StdPutS(str);
  858.             }
  859.             
  860.         // Return the key
  861.         return key;
  862.         }
  863.         
  864.     
  865. }
  866.  
  867.  
  868.  
  869.  
  870. /***********************************************************************
  871.  *
  872.  * FUNCTION:        StdInit
  873.  *
  874.  * DESCRIPTION:    Initialize the standard IO manager.
  875.  *
  876.  * CALLED BY:        Application during it's startup.
  877.  *
  878.  * PARAMETERS:         
  879.  *
  880.  * RETURNED:         
  881.  *
  882.  ***********************************************************************/
  883. Err    StdInit(Word formID, Word fieldID, Word scrollerID, 
  884.                     void (*appProcessCmd)(CharPtr cmdP) )
  885. {
  886.     
  887.     // Save globals
  888.     PrvFormID = formID;
  889.     PrvFieldID = fieldID;
  890.     PrvScrollerID = scrollerID;
  891.     PrvAppProcessCommand = appProcessCmd;
  892.  
  893.     return 0;
  894. }
  895.  
  896.  
  897. /***********************************************************************
  898.  *
  899.  * FUNCTION:        StdFree
  900.  *
  901.  * DESCRIPTION:    Closes down the standard IO manager.
  902.  *
  903.  * CALLED BY:        Application during it's shutdown.
  904.  *
  905.  * PARAMETERS:         
  906.  *
  907.  * RETURNED:         
  908.  *
  909.  ***********************************************************************/
  910. Err    StdFree(void)
  911. {
  912.     // Free the text handle
  913.     if (PrvTextH) MemHandleFree(PrvTextH);
  914.     PrvTextH = 0;
  915.     
  916.     // Clear globals
  917.     PrvFormID = 0;
  918.     PrvFieldID = 0;
  919.     PrvScrollerID = 0;
  920.     PrvAppProcessCommand = 0;
  921.     
  922.     
  923.     return 0;    
  924.     
  925. }
  926.  
  927. #endif    // EMULATION_LEVEL == EMULATION_NONE
  928.  
  929.  
  930. /***********************************************************************
  931.  *
  932.  * FUNCTION: StdSave (VM)
  933.  *
  934.  * DESCRIPTION:    Saves the content of IO before leave the IO form
  935.  *
  936.  * CALLED BY: Application during before changing to another form with
  937.  *            FrmReturnToForm().
  938.  *
  939.  * PARAMETERS:         
  940.  *
  941.  * RETURNED:         
  942.  *
  943.  ***********************************************************************/
  944. Err    StdSave(void)
  945. {
  946.   PrvFormClose(FrmGetActiveForm());
  947.   return 0;
  948. }
  949.  
  950.  
  951. /***********************************************************************
  952.  *
  953.  * FUNCTION:        StdBS
  954.  *
  955.  * DESCRIPTION:    Utility used to backspace from stdout
  956.  *
  957.  * CALLED BY:        Applications.
  958.  *
  959.  * PARAMETERS:        
  960.  *
  961.  * RETURNED:        void
  962.  *
  963.  ***********************************************************************/
  964. void StdBS (void)
  965. {    
  966.     EventType    event;
  967.     
  968.     event.eType = keyDownEvent;
  969.     event.data.keyDown.chr = 8;
  970.     StdHandleEvent(&event);
  971.     
  972. }
  973.  
  974.  
  975. /***********************************************************************
  976.  *
  977.  * FUNCTION:        StdGetS
  978.  *
  979.  * DESCRIPTION:    Waits till the user types a string. Ignores all
  980.  *            other types of events. Returns -1 of end-of-file is encountered
  981.  *            or 0 otherwise.
  982.  *
  983.  * CALLED BY:        Applications.
  984.  *
  985.  * PARAMETERS:         
  986.  *
  987.  * RETURNED:         
  988.  *
  989.  ***********************************************************************/
  990. Int    StdGetS(CharPtr strP, Boolean echo)
  991. {
  992.     Int            key;
  993.     UInt            index = 0;
  994.     
  995.     // Terminate the string
  996.     strP[0] = 0;
  997.         
  998.     while (1) {
  999.         key = StdGetChar(echo);
  1000.         
  1001.         // Check for end of file
  1002.         if (key == -1) return -1;
  1003.         
  1004.         // If carriage return, exit
  1005.         if (key == '\r' || key == '\n') return 0;
  1006.         
  1007.         // Look for backspace
  1008.         if (key == 8) {
  1009.             index--;
  1010.             strP[index] = 0;
  1011.             continue;
  1012.             }
  1013.         
  1014.         // Append to the string
  1015.         strP[index++] = key;
  1016.         strP[index] = 0;
  1017.         }
  1018.         
  1019.     
  1020. }
  1021.  
  1022.