home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / pippy-0.6beta-src.tar.gz / pippy-0.6beta-src.tar / pippy-0.6beta-src / src / Palm / libc / io.c < prev    next >
C/C++ Source or Header  |  2001-01-08  |  7KB  |  315 lines

  1. /**********************************************************************/
  2. /*                                                                    */
  3. /* io.c: input/output subsystem                                       */
  4. /*                                                                    */
  5. /* Jeffery D. Collins                                                 */
  6. /* (c) Endeavors Technology, Inc.                                     */
  7. /*                                                                    */
  8. /**********************************************************************/
  9.  
  10. #include "io.h"
  11. #include "PalmCompatibility.h"
  12. #include "set_a4.h"
  13. #include "GLib_interface.h"
  14.  
  15. static void *io_GetObjectPtr(UInt16 objectID) SEG_LIBC;
  16. static void  io_FormInit(FormPtr frm) SEG_LIBC;
  17. static void  io_FormClose(FormPtr frm) SEG_LIBC;
  18. static void  io_FormUpdateScroller(FormPtr frm) SEG_LIBC;
  19. static void  io_FieldScroll(Int16 linesToScroll) SEG_LIBC;
  20. static void  io_FieldPageScroll(WinDirectionType direction) SEG_LIBC;
  21.  
  22. static MemHandle gTextH = NULL;
  23. static UInt16 gFormID = 0;
  24. static UInt16 gFieldID = 0;
  25. static UInt16 gScrollerID = 0;
  26. static UInt16 gMaxChars = 0;
  27.  
  28.  
  29. Err 
  30. ioInit(UInt16 formID, UInt16 fieldID, UInt16 scrollerID, UInt16 maxChars)
  31. {
  32.     gFormID = formID;
  33.     gFieldID = fieldID;
  34.     gScrollerID = scrollerID;
  35.     gMaxChars = maxChars;
  36.  
  37.     return 0;
  38. }
  39.  
  40. static void *
  41. io_GetObjectPtr(UInt16 objectID)
  42. {
  43.     FormPtr    frm;
  44.     void *obj;
  45.     
  46.     frm = FrmGetActiveForm();
  47.     obj = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, objectID));
  48.  
  49.     return obj;
  50. }
  51.  
  52. static MemHandle
  53. initBuffer(UInt16 size) {
  54.     Char *textP;
  55.     MemHandle textH;
  56.  
  57.     textH = MemHandleNew(size);
  58.     if (textH == NULL)
  59.         return NULL;
  60.     textP = MemHandleLock(textH);
  61.     MemSet(textP, size, '\0');
  62.     MemPtrUnlock(textP);
  63.  
  64.     return textH;
  65. }
  66.  
  67. static UInt16 
  68. pushToBuffer(MemHandle textH, Char *s)
  69. {
  70.     Char *textP, *str=s;
  71.     UInt16 res, lenstr;
  72.     
  73.     if (StrLen(str) > gMaxChars)
  74.         str += StrLen(str) - gMaxChars + 1;
  75.         
  76.     lenstr = StrLen(str);
  77.  
  78.     textP = MemHandleLock(textH);
  79.  
  80.     if (StrLen(textP) + lenstr >= gMaxChars) {
  81.         Char *p;
  82.         UInt16 factor = max(gMaxChars/4, lenstr), len;
  83.             
  84.         /* Cut off the top */
  85.         p = textP + factor;
  86.         len = StrLen(p);
  87.  
  88.         /* Move the text up */
  89.         MemMove(textP, p, len);
  90.         MemSet(textP + len, factor, '\0');
  91.     }
  92.  
  93.     /* Add the new text */
  94.     StrCat(textP, str);
  95.     res = StrLen(textP);
  96.     MemPtrUnlock(textP);
  97.     
  98.     return res;
  99. }
  100.  
  101. static void 
  102. io_FormInit(FormPtr frm)
  103. {
  104.     FieldAttrType attr;
  105.     FieldPtr fldP;
  106.     Int16 obj_index; /* for debugging purposes */
  107.     
  108.     obj_index = FrmGetObjectIndex(frm, gFieldID);
  109.     fldP = FrmGetObjectPtr(frm, obj_index);
  110.     if (!gTextH)
  111.         gTextH = initBuffer(gMaxChars);
  112.     
  113.     FldGetAttributes(fldP, &attr);
  114.     attr.hasScrollBar = true;
  115.     FldSetAttributes(fldP, &attr);
  116.     
  117.     FldSetTextHandle(fldP, gTextH);
  118.     
  119.     gTextH = NULL;
  120.     
  121. }
  122.  
  123. static void 
  124. io_FormClose(FormPtr frm)
  125. {
  126.     FieldPtr fldP;
  127.     Int16 obj_index; /* for debugging purposes */
  128.     
  129.     obj_index = FrmGetObjectIndex(frm, gFieldID);
  130.     fldP = FrmGetObjectPtr(frm, obj_index);
  131.     
  132.     if (gTextH) MemHandleFree(gTextH);
  133.     gTextH = FldGetTextHandle(fldP);
  134.     
  135.     FldSetTextHandle(fldP, NULL);
  136. }
  137.  
  138. static void 
  139. io_FormUpdateScroller(FormPtr frm)
  140. {
  141.     UInt16 scrollPos, textHeight, fieldHeight;
  142.     Int16 maxValue;
  143.     FieldPtr fld;
  144.     ScrollBarPtr bar;
  145.     
  146.     fld = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, gFieldID));
  147.     bar = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, gScrollerID));
  148.     
  149.     FldGetScrollValues(fld, &scrollPos, &textHeight,  &fieldHeight);
  150.     
  151.     if (textHeight > fieldHeight)
  152.         maxValue = textHeight - fieldHeight;
  153.     else
  154.         maxValue = 0;
  155.     
  156.     SclSetScrollBar (bar, scrollPos, 0, maxValue, fieldHeight-1);
  157. }
  158.  
  159. static void 
  160. io_FieldScroll(Int16 linesToScroll)
  161. {
  162.  
  163.     UInt16 blankLines;
  164.     Int16 min, max, value, pageSize;
  165.     FieldPtr fld;
  166.     ScrollBarPtr bar;
  167.     
  168.     fld = io_GetObjectPtr(gFieldID);
  169.  
  170.     if (linesToScroll < 0) {
  171.         blankLines = FldGetNumberOfBlankLines(fld);
  172.         FldScrollField(fld, -linesToScroll, winUp);
  173.         
  174.         if (blankLines) {
  175.             bar = io_GetObjectPtr(gScrollerID);
  176.             SclGetScrollBar(bar, &value, &min, &max, &pageSize);
  177.             if (blankLines > -linesToScroll)
  178.                 max += linesToScroll;
  179.             else
  180.                 max -= blankLines;
  181.             SclSetScrollBar(bar, value, min, max, pageSize);
  182.         }
  183.     }
  184.     
  185.     else if (linesToScroll > 0)
  186.         FldScrollField(fld, linesToScroll, winDown);
  187.     
  188. }
  189.  
  190.  
  191. void ioPutS(Char *s)
  192. {    
  193.     FieldPtr fldP;
  194.     UInt16 pos;
  195.     MemHandle textH;
  196.     Char *str = s;
  197.  
  198.     /* If the incoming string is larger than the buffer size,
  199.        then grab the tail portion of the string plus some extra */
  200.  
  201.     if (FrmGetActiveFormID() == gFormID) {
  202.         fldP =(FieldPtr)io_GetObjectPtr(gFieldID);
  203.  
  204.         textH = FldGetTextHandle(fldP);
  205.  
  206.         pos = pushToBuffer(textH, str);
  207.  
  208.         FldSetTextHandle(fldP, textH);
  209.         FldSetScrollPosition(fldP, pos);
  210.         FldDrawField(fldP);
  211.         io_FormUpdateScroller(FrmGetActiveForm());
  212.     }
  213.     else  {    
  214.         if (!gTextH)
  215.             gTextH = initBuffer(gMaxChars);
  216.         pushToBuffer(gTextH, str);
  217.     }
  218. }
  219.  
  220. void ioClear(void)
  221. {
  222.     FieldPtr fldP;
  223.  
  224.     if (FrmGetActiveFormID() == gFormID) {
  225.         fldP =(FieldPtr)io_GetObjectPtr(gFieldID);
  226.         FldFreeMemory(fldP);  /* this should free the memory in gTextH */
  227.         FldEraseField(fldP);
  228.         FldDrawField(fldP);
  229.         io_FormUpdateScroller(FrmGetActiveForm());
  230.  
  231.         gTextH = initBuffer(gMaxChars);
  232.         FldSetTextHandle(fldP, gTextH);
  233.         gTextH = NULL;
  234.     }
  235.     else {
  236.         if (gTextH) MemHandleFree(gTextH);
  237.         gTextH = NULL;
  238.         }
  239. }
  240.  
  241. static void io_FieldPageScroll(WinDirectionType direction)
  242. {
  243.     Int16 value, min, max, pageSize;
  244.     UInt16 linesToScroll;
  245.     FieldPtr fld;
  246.     ScrollBarPtr bar;
  247.  
  248.     fld = io_GetObjectPtr(gFieldID);
  249.     
  250.     if (FldScrollable(fld, direction)) {
  251.         linesToScroll = FldGetVisibleLines(fld) - 1;
  252.         FldScrollField(fld, linesToScroll, direction);
  253.         
  254.         bar = io_GetObjectPtr(gScrollerID);
  255.         SclGetScrollBar(bar, &value, &min, &max, &pageSize);
  256.         
  257.         if (direction == winUp)
  258.             value -= linesToScroll;
  259.         else
  260.             value += linesToScroll;
  261.         
  262.         SclSetScrollBar(bar, value, min, max, pageSize);
  263.         return;
  264.     }
  265. }
  266.  
  267.  
  268. Boolean ioHandleEvent(EventPtr event)
  269. {
  270.     FormPtr frmP;
  271.     Boolean handled;
  272.     
  273.     SET_A4_FOR_GLIB('PyLb');
  274.  
  275.     handled = false;
  276.  
  277.     switch (event->eType) {
  278.     case keyDownEvent:
  279.         if (event->data.keyDown.chr == pageUpChr) {
  280.             io_FieldPageScroll(winUp);
  281.             handled = true;
  282.         }
  283.         else if (event->data.keyDown.chr == pageDownChr) {
  284.             io_FieldPageScroll(winDown);
  285.             handled = true;
  286.         }
  287.         break;
  288.     case sclRepeatEvent:
  289.         io_FieldScroll(event->data.sclRepeat.newValue - 
  290.                 event->data.sclRepeat.value);
  291.         break;
  292.     case frmUpdateEvent:
  293.         io_FormUpdateScroller(FrmGetActiveForm());
  294.         break;
  295.     case fldChangedEvent:
  296.         io_FormUpdateScroller(FrmGetActiveForm());
  297.         handled = true;
  298.         break;
  299.     case frmOpenEvent:
  300.         frmP = FrmGetActiveForm();
  301.         io_FormInit(frmP);
  302.         io_FormUpdateScroller(frmP);
  303.         break;
  304.     case frmCloseEvent:
  305.         frmP = FrmGetActiveForm();
  306.         io_FormClose(frmP);
  307.         break;
  308.     default:
  309.         break;
  310.     }
  311.  
  312.     RESTORE_A4;
  313.     return handled;
  314. }
  315.