Here is what I do for a generic routine that sets a field to text:


Shannon


/*********************************************************************

FieldToText

Copyright 1999 Hands High Software, Inc.


Sets a field to a copy of the given string. If text is already allocated,
it changes
the text. Otherwise, new text is created.

Will not reallocate database memory in older OS 2.0 systems, but will still
work in general.

Limits text to the maximum size of the field too.

*********************************************************************/

void    FieldToText (FieldPtr pField, CharPtr   pStr)
{
        Long    len = StrLen (pStr);
        VoidHand        hText;
        CharPtr         pText, pChunk;
        Word            offset,
                                curBlockSize;
        Err                     err;
        UInt            charCount, fldMax;
        Char            zero = 0;
        Boolean         wasAttached = false;

        hText = FldGetTextHandle (pField);


        if (hText) {
                pText = FldGetTextPtr (pField);

                // legally get the pointer from the handle. Slow, but
prevents debug ROMs from complaining
                pChunk = (CharPtr)MemHandleLock (hText);
                MemHandleUnlock (hText);
                ASSERT (pChunk != 0);
                offset = pText - pChunk;        // get start of chunk.
Since chunk is part of field, it is locked.
                curBlockSize = FldGetTextAllocatedSize (pField);
                FldSetTextHandle (pField, NULL);        // unlocks the text
handle
                wasAttached = true;
                CHECK (MemHandleLockCount (hText), 0);
        } else {
                hText = MemNewHandleClear (1);
                offset = 0;
                curBlockSize = 1;
        }

        charCount = StrLen (pStr);
        fldMax = FldGetMaxChars (pField);

        if (hText) {
                if (charCount <= fldMax) {
                        err = MemHandleInsert (hText, offset, curBlockSize,
pStr, charCount + 1); // already null terminated
                } else {
                        err = MemHandleInsert (hText, offset, curBlockSize,
pStr, fldMax + 1); // limit to the field length
                        if (!err) {
                                MemHandleInsert (hText, offset + fldMax, 1,
&zero, 1);// null terminate
                        }
                        charCount = fldMax;
                }

                if (!err) {
                        FldSetText (pField, hText, offset, charCount + 1);
                        FldSetDirty (pField, true);
                } else {
                        if (wasAttached) {
                                FldSetText (pField, hText, offset,
curBlockSize);  // reattach old text
                        } else {
                                // dispose of allocated text
                                MemHandleFree (hText);
                        }
                }
        }
}

/*********************************************************************

MemHandleInsert

Copyright 1999 Hands High Software, Inc.

Inserts data into a handle at a given place, replacing the given range.
Works on both data and temporary memory, however, it is not recommended
for data memory, since it will fail before it tries to reallocate
memory in a different heap. On the Palm III, since there is only
one big heap, this is not a problem.

Returns an err if it was not successful.

*********************************************************************/

Err     MemHandleInsert (VoidHand hDest,
                                                Long offset,
                                                Long len,
                                                VoidPtr pSrc,
                                                Long srcLen)
{
        Long    newSize;
        Long    oldSize;
        Err             err = noErr;
        CharPtr p;
        Boolean dataStore;

        CHECK (MemHandleLockCount (hDest), 0);

        oldSize = MemHandleSize (hDest);

        ASSERT (offset <= oldSize);

        newSize = oldSize - len + srcLen;

        if (srcLen > len) {
                err = MemHandleResize (hDest, newSize);
        }
        if (err) return err;

        dataStore = MemHandleDataStorage (hDest);

        p = (Char*)UtilLockHandle (hDest);

        // move the old data
        if (dataStore) {
                DmWrite (p, offset + srcLen, p + offset + len, oldSize -
offset - len);
        } else {
                MemMove (p + offset + srcLen, p + offset + len, oldSize -
offset - len);
        }

        // move in the new data
        if (pSrc) {
                if (dataStore) {
                        DmWrite (p, offset, pSrc, srcLen);
                } else {
                        MemMove (p + offset, pSrc, srcLen);
                }
        }
        MemPtrUnlock (p);

        MemHandleResize (hDest, newSize);       // in case it got smaller

        return err;
}



>Hello there Palm Pilot Gurus!
>
>       I am new to Palm Pilot Programming and was wondering if anyone could
>point me in the right direction in duplicating the strcpy command to a field
>on the
>PalmPilot.   I have started a few tidbits below but am unsure on how to
>proceed
>after I have a handle and pointer.  I sure wish this was treated in a more
>Java "TextField"
>type of way to free the novice like me of all this
>pointer->handle->memory-lock
>stuff.  It seems like a lot of trouble to just display a long string of
>text.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Hands High Software
Award winning software for the Palm(tm) Computing platform

<http://www.handshigh.com/>

[EMAIL PROTECTED]

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Reply via email to