--- Geoff Thompson <[EMAIL PROTECTED]> wrote:
> I am new to C and to Palm programming. I have the Palm
> manuals, and an assortment of Palm Programming books, and
> an assortment of C books (including K&R), but I must be
> looking in all the wrong places when it comes to basic
> concepts that everyone else seems to take for granted.
> Namely, how to handle handles.
Handles are used by the Palm OS API functions to refer to relocatable
blocks of memory. It really doesn't have much to do with C, so you
won't find anything about it in K&R. You create a chunk of memory and
get a handle to it with MemHandleNew() and you dispose of the chunk
with MemHandleFree(). If you want to do anything with the chunk of
memory, you can't do it while it is movable, so you lock down the chunk
and get a pointer to the memory with MemHandleLock(). Before disposing
of the memory, you should call MemHandleUnlock().
> <snip>
>
> For example - I want to set the text of a Field from the
> label of a Control. I could allocate additional memory
> and do a StrCopy, but wouldn't it be more efficent to set
> the Field handle to the handle of the Control label?
Here are two functions that are useful for setting the text in an
editable field:
// Set text in a field, given a fieldID and a handle to the text
static FieldPtr SetFieldTextFromHandle( UInt16 fieldID, MemHandle txtH
)
{
MemHandle oldTxtH;
FieldPtr fldP;
FormPtr frmP = FrmGetActiveForm();
// get the field and a handle to its text
fldP = FrmGetObjectPtr( frmP, FrmGetObjectIndex( frmP, fieldID ) );
oldTxtH = FldGetTextHandle( fldP );
// set field to new text
FldSetTextHandle( fldP, txtH );
FldDrawField( fldP );
// free old MemHandle
if ( oldTxtH ) MemHandleFree( oldTxtH );
return fldP;
}
// Set text in a field, given a fieldID and a pointer to a string
static FieldPtr SetFieldTextFromStr( UInt16 fieldID, Char *strP )
{
MemHandle txtH;
txtH = MemHandleNew( StrLen( strP ) + 1 );
if ( !txtH )
return NULL;
StrCopy( MemHandleLock( txtH ), strP );
MemHandleUnlock( txtH );
return SetFieldTextFromHandle( fieldID, txtH );
}
__________________________________________________
Do You Yahoo!?
Yahoo! Tax Center - online filing with TurboTax
http://taxes.yahoo.com/
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/support/forums/