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 >
Wrap
Text File
|
1997-12-14
|
10KB
|
468 lines
/*
ö─ùpâïü[â`âô FOR PILOT
PRESENTED BY Y.KANAI
970404
*/
#include <Pilot.h> // all the system toolbox headers
#include <FloatMgr.h>
#include <FloatPrv.h>
#define kDrawLeftAligned 0
#define kDrawRightAligned 1
#define kDrawCenterAligned 2
#define CommonInputAlertID 1500
VoidPtr GetObjectPtr (Int objectID);
FieldPtr GetFocusObjectPtr (void);
Word GetFocusObjectId (void);
Boolean GetFldStr (Int FldID, CharPtr Str);
void MyInitList (short listResID, short choice, short disp, short x, short y, short selection, ListDrawDataFuncPtr func);
void MyDrawListItem (RectanglePtr bounds, CharPtr textToDraw);
short MyLstGetSelection (Int ListID);
void MyCtlShowControl (Int resID, Boolean show);
void MyCtlSetLabel (short theControlID, CharPtr theStr);
void MyDrawBitmap (Int resID, Short x, Short y);
void MySetPopupList (short inLST, short inPUT, short inItem);
short MyCompTwoFloatType (FloatType a, FloatType b);
void MySetStrToField (int theFieldID, char *theDrawStr);
void MyDrawString (char *theDrawStr, FontID theFont, short x, short y, short theAlignMode);
void MyStrIToA (CharPtr Str, Long num);
Boolean MyGetFloatFromInputField (int theFieldID, FloatType *theInput);
Boolean MyGetPositiveFloatFromInputField (int theFieldID, FloatType *theInput);
Boolean MyCheckNumericTextString (char *s);
void MyEraseDisplayField (void);
void MyGetStrFromSTRRes (short resID, char *strPtr);
void myFplFToAStr (FloatType inFloat, char* outStr, short fractNum);
void MyFplFToAExp (FloatType inFloat, char* outStr, short fract);
void myFplPosFToAStr (FloatType inFloat, char* outStr, short fractNum);
Long MyStrAToL (char *s);
void MyStrCopyFromPtoC (Ptr theDestStrC, Ptr theSourceStrP);
#define MyClearStr(theStr) theStr[0]='\0'
VoidPtr GetObjectPtr(Int objectID)
{
FormPtr frm;
frm = FrmGetActiveForm();
return(FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, objectID)));
}
FieldPtr GetFocusObjectPtr (void)
{
FormPtr frm;
Word focus;
frm = FrmGetActiveForm ();
focus = FrmGetFocus (frm);
if (focus == noFocus)
return (NULL);
return ((FieldPtr) FrmGetObjectPtr (frm, focus));
}
Word GetFocusObjectId (void)
{
FormPtr frm;
Word focus;
frm = FrmGetActiveForm ();
focus = FrmGetFocus (frm);
if (focus == noFocus)
return (NULL);
return (FrmGetObjectId(frm, focus));
}
Boolean GetFldStr(Int FldID, CharPtr theStr)
{
FieldPtr fld;
CharPtr ptr;
fld = (FieldPtr) GetObjectPtr (FldID);
ptr = FldGetTextPtr (fld);
if (ptr == NULL)
MyClearStr(theStr);
else
StrCopy(theStr, ptr);
return (*theStr != 0);
}
void MyInitList(
short listResID,
short choice,
short disp,
short x, short y,
short selection, ListDrawDataFuncPtr func)
{
ListPtr myListPtr;
myListPtr = (ListPtr) GetObjectPtr (listResID);
LstSetListChoices(myListPtr, NULL, choice);
LstSetPosition(myListPtr, x, y);
LstSetSelection(myListPtr, selection);
LstSetHeight(myListPtr, disp);
LstSetDrawFunction(myListPtr, func);
}
void MyDrawListItem(RectanglePtr bounds, CharPtr textToDraw)
{
UInt x;
Int textLen, lstWidth;
Boolean fits;
// Determine the length of text that will fit within the list bounds.
lstWidth = bounds->extent.x - 2;
textLen = StrLen(textToDraw);
FntCharsInWidth(textToDraw, &lstWidth, &textLen, &fits);
// Now draw the text from the record.
x = bounds->topLeft.x;
WinDrawChars(textToDraw, textLen, x, bounds->topLeft.y);
}
short MyLstGetSelection(Int ListID)
{
return LstGetSelection ((ListPtr) GetObjectPtr (ListID));
}
void MyCtlShowControl(Int resID, Boolean show)
{
ControlPtr myControlPtr = (ControlPtr) GetObjectPtr(resID);
if (show)
CtlShowControl(myControlPtr);
else
CtlHideControl(myControlPtr);
}
void MyCtlSetLabel(short theControlID, CharPtr theStr)
{
CtlSetLabel((ControlPtr) GetObjectPtr(theControlID), theStr);
}
void MyDrawBitmap(Int resID, Short x, Short y)
{
Handle resH;
BitmapPtr resP;
resH = (Handle) DmGetResource( bitmapRsc, resID );
ErrFatalDisplayIf( !resH, "Missing bitmap" );
resP = (BitmapPtr) MemHandleLock(resH);
WinDrawBitmap (resP, x, y);
MemPtrUnlock(resP);
DmReleaseResource( resH );
}
void MySetPopupList(short inLST, short inPUT, short inItem)
{
ListPtr myLstPtr = (ListPtr) GetObjectPtr(inLST);
LstSetSelection (myLstPtr, inItem);
MyCtlSetLabel(inPUT, LstGetSelectionText (myLstPtr, inItem));
}
short MyCompTwoFloatType(FloatType a, FloatType b)
{
ULong mantissa;
Int exponent;
Int sign;
FplBase10Info(FplSub(a, b), &mantissa, &exponent, &sign);
if (mantissa == 0)
return 0;
else
return sign;
}
void MySetStrToField(int theFieldID, char *theDrawStr)
{
Handle myTextH;
CharPtr myTextP;
FieldPtr myFldPtr;
ULong myStrSize = StrLen(theDrawStr)+1;
myTextH = (Handle) MemHandleNew((ULong) myStrSize);
if (myTextH)
{
myTextP = (CharPtr)MemHandleLock(myTextH);
MemMove(myTextP, theDrawStr, myStrSize);
myFldPtr = (FieldPtr) GetObjectPtr (theFieldID);
FldSetTextHandle(myFldPtr, myTextH);
FldEraseField(myFldPtr);
FldDrawField(myFldPtr);
MemHandleUnlock(myTextH);
}
}
void MyDrawString(char *theDrawStr, FontID theFont, short x, short y, short theAlignMode)
{
FontID oldFont;
short myStrLen;
short myStrLineWidth;
oldFont = FntGetFont();
FntSetFont(theFont);
myStrLen = StrLen(theDrawStr);
myStrLineWidth = FntLineWidth (theDrawStr, myStrLen);
switch (theAlignMode)
{
case kDrawLeftAligned:
break;
case kDrawRightAligned:
x -= myStrLineWidth;
break;
case kDrawCenterAligned:
x -= (myStrLineWidth / 2);
break;
}
WinDrawChars(theDrawStr, myStrLen, x, y);
FntSetFont(oldFont);
}
void MyStrIToA (CharPtr Str, Long num)
{
char myWorkStr[12];
if (num < 0)
{
StrCopy(Str, "-");
StrIToA(myWorkStr, (Long) (-num));
StrCat(Str, myWorkStr);
} else {
StrIToA(Str, (Long) num);
}
}
Boolean MyGetFloatFromInputField(int theFieldID, FloatType *theInput)
{
Char textInput[50];
if (!GetFldStr(theFieldID, textInput))
{
FrmCustomAlert(CommonInputAlertID,"Input number", "", "");
return false;
}
if (!MyCheckNumericTextString(textInput))
return false;
*theInput = FplAToF(textInput);
return true;
}
Boolean MyGetPositiveFloatFromInputField(int theFieldID, FloatType *theInput)
{
CharPtr textInput;
ULong mantissa;
Int exponent;
Int sign;
textInput = FldGetTextPtr((FieldPtr) GetObjectPtr (theFieldID));
if (textInput == NULL)
{
FrmCustomAlert(CommonInputAlertID,"Ælé≡ôⁿù═é╡é─é¡é╛é│éó", "", "");
return false;
}
*theInput = FplAToF(textInput);
FplBase10Info(*theInput, &mantissa, &exponent, &sign);
if (sign == -1 || mantissa == 0)
{
FrmCustomAlert(CommonInputAlertID,"É│é╠Ælé≡ôⁿù═é╡é─é¡é╛é│éó", "", "");
return false;
}
return true;
}
Boolean MyCheckNumericTextString(char *s)
{
Boolean ret = true;
while (*s == ' ' || *s == '\n' || *s == '\t') s++;
if (*s == '+' || *s == '-')
s++;
while (*s >= '0' && *s <= '9')
s++;
if (*s == '.')
{
s++;
while (*s >= '0' && *s <= '9')
s++;
}
if (*s == 'e')
{
s++;
if (*s == '-')
s++;
while (*s >= '0' && *s <= '9')
s++;
}
if (*s != '\0')
{
FrmCustomAlert(CommonInputAlertID,"Please input numeric values only", "", "");
ret = false;
}
return ret;
}
void MyEraseDisplayField(void)
{
RectangleType myRect = {0, 18, 160, 112};
WinEraseRectangle(&myRect, 0);
}
void MyGetStrFromSTRRes(short resID, char *strPtr)
{
VoidHand resH;
CharPtr resP;
resH = DmGetResource ('STR ', resID);
ErrFatalDisplayIf( !resH, "Missing STR res" );
resP = (Char*) MemHandleLock(resH);
MyStrCopyFromPtoC(strPtr, resP);
MemHandleUnlock (resH);
}
void myFplFToAStr(FloatType inFloat, char* outStr, short fractNum)
{
char myStr[20];
ULong mantissa;
Int exponent;
Int sign;
FplBase10Info(inFloat, &mantissa, &exponent, &sign);
if (sign == -1)
{
inFloat = FplMul(inFloat, FplAToF("-1"));
myFplPosFToAStr(inFloat, myStr, fractNum);
StrCopy(outStr, "-");
StrCat(outStr, myStr);
} else {
myFplPosFToAStr(inFloat, outStr, fractNum);
}
}
void myFplPosFToAStr(FloatType inFloat, char* outStr, short fractNum)
{
Long longInt, longFrac;
Char textWork[10];
UInt myFracStrLengthToAdd;
Long myFract;
// use only int when inFloat >= 1000
longInt = FplFloatToLong(FplMul(inFloat, FplLongToFloat((Long) 10)));
if (longInt >= 10000)
{
longInt += 5; // cut under 5
longInt /= 10;
StrIToA(outStr, longInt);
return;
}
// use int.fract when inFloat < 1000
// get int part
longInt /= 10;
StrIToA(outStr, longInt);
// get fract part
myFract = 10;
for (short i = 0; i < fractNum; i++)
myFract *= 10;
longFrac = FplFloatToLong(FplMul(inFloat, FplLongToFloat(myFract)));
longFrac -= (longInt * myFract);
longFrac += 5; // cut under 5
longFrac /= 10;
// if fract part == 0, return
if (longFrac == 0)
return;
// if fract part == 1, int++
if (longFrac >= (myFract/10))
{
longInt++;
StrIToA(outStr, longInt);
return;
}
// if 0 < fract part < 1, add fract to int
StrCat (outStr, ".");
StrIToA(textWork, longFrac);
myFracStrLengthToAdd = fractNum - StrLen(textWork);
for (short i = 0; i < myFracStrLengthToAdd; i++)
StrCat (outStr, "0");
StrCat (outStr, textWork);
}
void MyFplFToAExp(FloatType inFloat, char* outStr, short fract)
{
char str[20];
short i = 0;
FplFToA(inFloat, str);
while (str[i] != '.')
*outStr++ = str[i++];
*outStr++ = str[i++];
for (short k = 0; k < fract; k++)
*outStr++ = str[i++];
while (str[i++] != 'e')
;
*outStr++ = 'x';
*outStr++ = '1';
*outStr++ = '0';
*outStr++ = '^';
if (str[i] == '-')
*outStr++ = str[i++];
if (str[i] == '0')
i++;
while ((*outStr++ = str[i++]) != '\0')
;
}
Long MyStrAToL(char *s)
{
Long n = 0, sign = 1;
while (*s == ' ' || *s == '\n' || *s == '\t') s++;
if (*s == '+' || *s == '-')
sign = (*s++ == '+') ? 1:-1;
while (*s >= '0' && *s <= '9')
n = 10 * n + *s++ - '0';
return (sign * n);
}
void MyStrCopyFromPtoC(Ptr theDestStrC, Ptr theSourceStrP)
{
Byte num;
num = *(theSourceStrP++);
for (short i = 0; i < (short) num; i++)
*theDestStrC++ = *theSourceStrP++;
*theDestStrC++ = '\0';
}