Programming for Palm OS/C/Fields

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Fields can be described for PilRC like this:

FORM ID MainForm AT (2 2 156 156)
BEGIN
  FIELD ID ArtistField  AT (4 14      160-8 14) NONEDITABLE
  FIELD ID ServerField  AT (4 PREVTOP 160-8 14) UNDERLINED
END

If text fields can receive focus but not accept any input, maybe the application is accidentally handling keyDown events so that they never make it to FrmDispatchEvent at the end of the chain of command in the event loop.

Although having a field edit a MemHandle (such as a database record) is easy (with FldSetTextHandle), changing the text in a text field is rather more involved:

void FldChangeText( FieldType *field, char *format, ...)
{
  MemHandle  fieldChunk;
  char       *fieldStore;
  UInt32     roomNeeded;
  Err        e; 
  char       text[ 99];
  va_list    args; 
  
  // format the text
  va_start( args, format);
  StrVPrintF( text, format, args);
  va_end( args);
  
  roomNeeded = StrLen(text) + sizeof(NUL);
  
  // get the handle for the string and unlock it by removing it from the field
  fieldChunk = FldGetTextHandle( field);
  FldSetTextHandle( field, NULL);
  if ( NULL == fieldChunk)
  {
    fieldChunk = MemHandleNew( roomNeeded);
    if ( NULL == fieldChunk) goto tidyUp;
  }
  else {
    // resize the chunk if necessary
    if ( MemHandleSize( fieldChunk) < roomNeeded)
    {
      e = MemHandleResize( fieldChunk, roomNeeded);
      if ( e != errNone) goto tidyUp;
    }
  }
  // lock the chunk, write to it and unlock it
  fieldStore = MemHandleLock( fieldChunk);
  StrCopy( fieldStore, text);
  MemHandleUnlock( fieldChunk);

 tidyUp:
  // update the text in the field
  FldSetTextHandle( field, fieldChunk);
  FldDrawField( field);
}