home *** CD-ROM | disk | FTP | other *** search
/ Hand Held Organizer Toolkit / walnutcreekcdrom-handheldorganizertoolkit-march1998.iso / PalmPilot / travel_entertainment / jinfo.sit / J-Info(2.6) / Common.cp next >
Text File  |  1997-12-14  |  10KB  |  468 lines

  1. /*
  2.     ö─ùpâïü[â`âô FOR PILOT
  3.         PRESENTED BY Y.KANAI
  4.         970404
  5. */
  6.  
  7. #include <Pilot.h>                // all the system toolbox headers
  8. #include <FloatMgr.h>
  9. #include <FloatPrv.h>
  10.  
  11. #define    kDrawLeftAligned     0
  12. #define    kDrawRightAligned    1
  13. #define kDrawCenterAligned    2
  14. #define    CommonInputAlertID    1500
  15.  
  16. VoidPtr     GetObjectPtr                        (Int objectID);
  17. FieldPtr     GetFocusObjectPtr                     (void);
  18. Word         GetFocusObjectId                     (void);
  19. Boolean     GetFldStr                            (Int FldID, CharPtr Str);
  20. void         MyInitList                            (short listResID, short choice, short disp, short x, short y, short selection, ListDrawDataFuncPtr func);
  21. void         MyDrawListItem                        (RectanglePtr bounds, CharPtr textToDraw);
  22. short         MyLstGetSelection                    (Int ListID);
  23. void         MyCtlShowControl                    (Int resID, Boolean show);
  24. void         MyCtlSetLabel                        (short theControlID, CharPtr theStr);
  25. void         MyDrawBitmap                        (Int resID, Short x, Short y);
  26. void        MySetPopupList                        (short inLST, short inPUT, short inItem);
  27. short         MyCompTwoFloatType                    (FloatType a, FloatType b);
  28. void         MySetStrToField                        (int theFieldID, char *theDrawStr);
  29. void         MyDrawString                        (char *theDrawStr, FontID theFont, short x, short y, short theAlignMode);
  30. void        MyStrIToA                             (CharPtr Str, Long num);
  31. Boolean     MyGetFloatFromInputField            (int theFieldID, FloatType *theInput);
  32. Boolean     MyGetPositiveFloatFromInputField    (int theFieldID, FloatType *theInput);
  33. Boolean     MyCheckNumericTextString            (char *s);
  34. void         MyEraseDisplayField                    (void);
  35. void         MyGetStrFromSTRRes                    (short resID, char *strPtr);
  36. void         myFplFToAStr                        (FloatType inFloat, char* outStr, short fractNum);
  37. void         MyFplFToAExp                        (FloatType inFloat, char* outStr, short fract);
  38. void         myFplPosFToAStr                        (FloatType inFloat, char* outStr, short fractNum);
  39. Long         MyStrAToL                            (char *s);
  40. void         MyStrCopyFromPtoC                    (Ptr theDestStrC, Ptr theSourceStrP);
  41. #define    MyClearStr(theStr) theStr[0]='\0'
  42.  
  43. VoidPtr GetObjectPtr(Int objectID)
  44. {
  45.     FormPtr frm;
  46.     
  47.     frm = FrmGetActiveForm();
  48.     return(FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, objectID)));
  49. }
  50.  
  51. FieldPtr GetFocusObjectPtr (void)
  52. {
  53.     FormPtr frm;
  54.     Word focus;
  55.     
  56.     frm = FrmGetActiveForm ();
  57.     focus = FrmGetFocus (frm);
  58.     if (focus == noFocus)
  59.         return (NULL);
  60.         
  61.     return ((FieldPtr) FrmGetObjectPtr (frm, focus));
  62. }
  63.  
  64. Word GetFocusObjectId (void)
  65. {
  66.     FormPtr frm;
  67.     Word focus;
  68.     
  69.     frm = FrmGetActiveForm ();
  70.     focus = FrmGetFocus (frm);
  71.     if (focus == noFocus)
  72.         return (NULL);
  73.         
  74.     return (FrmGetObjectId(frm, focus));
  75. }
  76.  
  77. Boolean GetFldStr(Int FldID, CharPtr theStr)
  78. {
  79.     FieldPtr         fld;
  80.     CharPtr            ptr;
  81.         
  82.     fld = (FieldPtr) GetObjectPtr (FldID);
  83.     ptr = FldGetTextPtr (fld);
  84.     if (ptr == NULL)
  85.         MyClearStr(theStr);
  86.     else
  87.         StrCopy(theStr, ptr);
  88.         
  89.     return (*theStr != 0);
  90. }
  91.  
  92. void MyInitList(
  93.     short listResID, 
  94.     short choice, 
  95.     short disp, 
  96.     short x, short y, 
  97.     short selection, ListDrawDataFuncPtr func)
  98. {
  99.     ListPtr        myListPtr;
  100.     
  101.     myListPtr = (ListPtr) GetObjectPtr (listResID);
  102.     LstSetListChoices(myListPtr, NULL, choice);
  103.     LstSetPosition(myListPtr, x, y);
  104.     LstSetSelection(myListPtr, selection);
  105.     LstSetHeight(myListPtr, disp);
  106.     LstSetDrawFunction(myListPtr, func);
  107. }
  108.  
  109. void MyDrawListItem(RectanglePtr bounds, CharPtr textToDraw)
  110. {
  111.     UInt        x;
  112.     Int            textLen, lstWidth;
  113.     Boolean        fits;
  114.  
  115.     // Determine the length of text that will fit within the list bounds.
  116.     lstWidth = bounds->extent.x - 2;
  117.     textLen = StrLen(textToDraw);
  118.     FntCharsInWidth(textToDraw, &lstWidth, &textLen, &fits);
  119.     
  120.     // Now draw the text from the record.
  121.     x = bounds->topLeft.x;
  122.     WinDrawChars(textToDraw, textLen, x, bounds->topLeft.y);
  123. }
  124.  
  125. short MyLstGetSelection(Int ListID)
  126. {
  127.     return LstGetSelection ((ListPtr)  GetObjectPtr (ListID));
  128. }
  129.  
  130. void MyCtlShowControl(Int resID, Boolean show)
  131. {
  132.     ControlPtr    myControlPtr = (ControlPtr) GetObjectPtr(resID);
  133.     
  134.     if (show)    
  135.         CtlShowControl(myControlPtr);
  136.     else
  137.         CtlHideControl(myControlPtr);
  138. }
  139.  
  140. void MyCtlSetLabel(short theControlID, CharPtr theStr)
  141. {
  142.     CtlSetLabel((ControlPtr) GetObjectPtr(theControlID), theStr);
  143. }
  144.  
  145.  
  146. void MyDrawBitmap(Int resID, Short x, Short y)
  147. {
  148.     Handle        resH;
  149.     BitmapPtr    resP;
  150.  
  151.     resH = (Handle) DmGetResource( bitmapRsc, resID );
  152.     ErrFatalDisplayIf( !resH, "Missing bitmap" );
  153.     resP = (BitmapPtr) MemHandleLock(resH);
  154.     WinDrawBitmap (resP, x, y);
  155.     MemPtrUnlock(resP);
  156.     DmReleaseResource( resH );
  157. }
  158.  
  159. void MySetPopupList(short inLST, short inPUT, short inItem)
  160. {
  161.     ListPtr    myLstPtr = (ListPtr) GetObjectPtr(inLST);
  162.     
  163.     LstSetSelection (myLstPtr, inItem);
  164.     MyCtlSetLabel(inPUT, LstGetSelectionText (myLstPtr, inItem));
  165. }
  166.  
  167. short MyCompTwoFloatType(FloatType a, FloatType b)
  168. {
  169.     ULong    mantissa;
  170.     Int        exponent;
  171.     Int        sign;
  172.     
  173.     FplBase10Info(FplSub(a, b), &mantissa, &exponent, &sign);
  174.     if (mantissa == 0)
  175.         return 0;
  176.     else
  177.         return sign;
  178. }
  179.  
  180. void MySetStrToField(int theFieldID, char *theDrawStr)
  181. {
  182.     Handle        myTextH;
  183.     CharPtr        myTextP;
  184.     FieldPtr    myFldPtr;
  185.     ULong        myStrSize = StrLen(theDrawStr)+1;
  186.     
  187.     myTextH = (Handle) MemHandleNew((ULong) myStrSize); 
  188.     if (myTextH)
  189.     {
  190.         myTextP = (CharPtr)MemHandleLock(myTextH);
  191.         MemMove(myTextP, theDrawStr, myStrSize); 
  192.         myFldPtr = (FieldPtr) GetObjectPtr (theFieldID);
  193.         FldSetTextHandle(myFldPtr, myTextH);
  194.           FldEraseField(myFldPtr);
  195.            FldDrawField(myFldPtr);    
  196.         MemHandleUnlock(myTextH);
  197.     }    
  198. }
  199.  
  200. void MyDrawString(char *theDrawStr, FontID theFont, short x, short y, short theAlignMode)
  201. {
  202.     FontID    oldFont;
  203.     short     myStrLen;    
  204.     short    myStrLineWidth;
  205.     
  206.     oldFont = FntGetFont();
  207.     FntSetFont(theFont);
  208.     myStrLen = StrLen(theDrawStr);
  209.     myStrLineWidth = FntLineWidth (theDrawStr, myStrLen);
  210.     switch (theAlignMode)
  211.     {
  212.         case kDrawLeftAligned:
  213.             break;
  214.         
  215.         case kDrawRightAligned:
  216.             x -= myStrLineWidth;
  217.             break;
  218.             
  219.         case kDrawCenterAligned:
  220.             x -= (myStrLineWidth / 2);
  221.             break;
  222.     }
  223.     WinDrawChars(theDrawStr, myStrLen, x, y);
  224.  
  225.     FntSetFont(oldFont);
  226. }
  227.  
  228. void MyStrIToA (CharPtr Str, Long num)
  229. {
  230.     char myWorkStr[12];
  231.     
  232.     if (num < 0)
  233.     {
  234.         StrCopy(Str, "-");
  235.         StrIToA(myWorkStr, (Long) (-num));
  236.         StrCat(Str, myWorkStr);
  237.     } else {
  238.         StrIToA(Str, (Long) num);
  239.     }
  240. }
  241.  
  242. Boolean MyGetFloatFromInputField(int theFieldID, FloatType *theInput)
  243. {
  244.     Char    textInput[50];
  245.  
  246.     if (!GetFldStr(theFieldID, textInput))
  247.     {
  248.         FrmCustomAlert(CommonInputAlertID,"Input number", "",  "");
  249.         return false;
  250.     }    
  251.     
  252.     if (!MyCheckNumericTextString(textInput))
  253.         return false;
  254.     
  255.     *theInput = FplAToF(textInput);
  256.     return true;
  257. }
  258.  
  259. Boolean MyGetPositiveFloatFromInputField(int theFieldID, FloatType *theInput)
  260. {
  261.     CharPtr    textInput;
  262.     ULong    mantissa;
  263.     Int        exponent;
  264.     Int        sign;
  265.  
  266.     textInput = FldGetTextPtr((FieldPtr) GetObjectPtr (theFieldID));
  267.     if (textInput == NULL)
  268.     {
  269.         FrmCustomAlert(CommonInputAlertID,"Ælé≡ôⁿù═é╡é─é¡é╛é│éó", "",  "");
  270.         return false;
  271.     }    
  272.     *theInput = FplAToF(textInput);
  273.     FplBase10Info(*theInput, &mantissa, &exponent, &sign);
  274.     if (sign == -1 || mantissa == 0)
  275.     {
  276.         FrmCustomAlert(CommonInputAlertID,"É│é╠Ælé≡ôⁿù═é╡é─é¡é╛é│éó", "",  "");
  277.         return false;
  278.     }        
  279.     return true;
  280. }
  281.  
  282. Boolean MyCheckNumericTextString(char *s)
  283. {
  284.     Boolean    ret = true;
  285.  
  286.     while (*s == ' ' || *s == '\n' || *s == '\t') s++;
  287.     
  288.     if (*s == '+' || *s == '-')
  289.         s++;
  290.     
  291.     while (*s >= '0' && *s <= '9')
  292.         s++;
  293.     
  294.     if (*s == '.')
  295.     {
  296.         s++;
  297.         while (*s >= '0' && *s <= '9')
  298.             s++;
  299.     }
  300.     
  301.     if (*s == 'e')
  302.     {
  303.         s++;
  304.         if (*s == '-')
  305.             s++;
  306.         while (*s >= '0' && *s <= '9')
  307.             s++;        
  308.     }
  309.         
  310.     if (*s != '\0')
  311.     {    
  312.         FrmCustomAlert(CommonInputAlertID,"Please input numeric values only", "",  "");
  313.         ret = false;
  314.     }
  315.     
  316.     return ret;
  317. }
  318.  
  319. void MyEraseDisplayField(void)
  320. {
  321.     RectangleType    myRect = {0, 18, 160, 112};    
  322.     WinEraseRectangle(&myRect, 0);
  323. }
  324.  
  325. void MyGetStrFromSTRRes(short resID, char *strPtr)
  326. {
  327.     VoidHand    resH;
  328.     CharPtr        resP;
  329.     
  330.     resH = DmGetResource ('STR ', resID);
  331.     ErrFatalDisplayIf( !resH, "Missing STR res" );
  332.     resP = (Char*) MemHandleLock(resH);
  333.     MyStrCopyFromPtoC(strPtr, resP);
  334.     MemHandleUnlock (resH);
  335. }
  336.  
  337. void myFplFToAStr(FloatType inFloat, char* outStr, short fractNum)
  338. {
  339.     char        myStr[20];
  340.     ULong        mantissa;
  341.     Int            exponent;
  342.     Int            sign;
  343.  
  344.     FplBase10Info(inFloat, &mantissa, &exponent, &sign);
  345.     
  346.     if (sign == -1)
  347.     {
  348.         inFloat = FplMul(inFloat, FplAToF("-1"));
  349.         myFplPosFToAStr(inFloat, myStr, fractNum);
  350.         StrCopy(outStr, "-");
  351.         StrCat(outStr, myStr);
  352.     } else {    
  353.         myFplPosFToAStr(inFloat, outStr, fractNum);
  354.     }
  355. }    
  356.  
  357.  
  358. void myFplPosFToAStr(FloatType inFloat, char* outStr, short fractNum)
  359. {
  360.     Long        longInt, longFrac;
  361.     Char        textWork[10];
  362.     UInt        myFracStrLengthToAdd;
  363.     Long        myFract;
  364.     
  365. // use only int when inFloat >= 1000
  366.     longInt = FplFloatToLong(FplMul(inFloat, FplLongToFloat((Long) 10)));    
  367.     if (longInt >= 10000)
  368.     {
  369.         longInt += 5;    // cut under 5
  370.         longInt /= 10;
  371.         StrIToA(outStr, longInt);
  372.         return;
  373.     }
  374.             
  375. // use int.fract when inFloat < 1000
  376.     // get int part
  377.     longInt /= 10;
  378.     StrIToA(outStr, longInt);
  379.  
  380.     // get fract part
  381.     myFract = 10;
  382.     for (short i = 0; i < fractNum; i++)
  383.         myFract *= 10;
  384.             
  385.     longFrac = FplFloatToLong(FplMul(inFloat, FplLongToFloat(myFract)));
  386.     longFrac -= (longInt * myFract);
  387.     longFrac += 5;    // cut under 5
  388.     longFrac /= 10;
  389.     
  390.     // if fract part == 0, return
  391.     if (longFrac == 0)
  392.         return;
  393.         
  394.     // if fract part == 1, int++
  395.     if (longFrac >= (myFract/10))
  396.     {
  397.         longInt++;
  398.         StrIToA(outStr, longInt);
  399.         return;
  400.     }
  401.  
  402.     //     if 0 < fract part < 1, add fract to int
  403.     StrCat (outStr, ".");            
  404.     StrIToA(textWork, longFrac);
  405.     myFracStrLengthToAdd = fractNum - StrLen(textWork);
  406.     for (short i = 0; i < myFracStrLengthToAdd; i++)
  407.         StrCat (outStr, "0");
  408.     StrCat (outStr, textWork);
  409. }
  410.  
  411. void MyFplFToAExp(FloatType inFloat, char* outStr, short fract)
  412. {
  413.     char    str[20];
  414.     short    i = 0;
  415.     
  416.     FplFToA(inFloat, str);
  417.     
  418.     while (str[i] != '.')
  419.         *outStr++ = str[i++];
  420.     
  421.     *outStr++ = str[i++];
  422.     
  423.     for (short k = 0; k < fract; k++)
  424.         *outStr++ = str[i++];
  425.  
  426.     while (str[i++] != 'e')
  427.         ;
  428.     
  429.     *outStr++ = 'x';
  430.     *outStr++ = '1';
  431.     *outStr++ = '0';
  432.     *outStr++ = '^';
  433.  
  434.     if (str[i] == '-')
  435.         *outStr++ = str[i++];
  436.         
  437.     if (str[i] == '0')
  438.         i++;
  439.         
  440.     while ((*outStr++ = str[i++]) != '\0')
  441.         ;
  442. }
  443.  
  444. Long MyStrAToL(char *s)
  445. {
  446.     Long    n = 0, sign = 1;
  447.     
  448.     while (*s == ' ' || *s == '\n' || *s == '\t') s++;
  449.     
  450.     if (*s == '+' || *s == '-')
  451.         sign = (*s++ == '+') ? 1:-1;
  452.     
  453.     while (*s >= '0' && *s <= '9')
  454.         n = 10 * n + *s++ - '0';
  455.         
  456.     return (sign * n);
  457. }
  458.  
  459. void MyStrCopyFromPtoC(Ptr theDestStrC, Ptr theSourceStrP)
  460. {
  461.     Byte    num;
  462.     
  463.     num = *(theSourceStrP++);
  464.     for (short i = 0; i < (short) num; i++)
  465.         *theDestStrC++ = *theSourceStrP++;
  466.     *theDestStrC++ = '\0';
  467. }
  468.