Slide 8.2: Form demonstrations (cont.)
  Slide 8.4: Line-by-line anatomy of Forms.c
  Home


Forms.c


The following C program is for the application on the previous slides.

 Forms.c 

#include  <PalmOS.h> 
#include  <PalmCompatibility.h>
#include  "Forms.h"

#define   MAX_COUNT  9999
int       cnt = 1000;


UInt32  PilotMain( UInt16 cmd, MemPtr cmdPBP, UInt16 launchFlags ) {
  short      err;
  EventType  e;
  FormType   *pfrm;
  
  short  f = FormFirst;           // Start with 1st form 1st :)

  if ( cmd == sysAppLaunchCmdNormalLaunch ) {

    // Make sure only react to NormalLaunch, not Reset, Beam, Find, GoTo...
    FrmGotoForm( f );
    while ( 1 ) {
      EvtGetEvent( &e, 100 );
      if ( SysHandleEvent ( &e ) )   continue;
      if ( MenuHandleEvent( (void *) 0, &e, &err ) )   continue;
      
      switch ( e.eType ) {

        // if the Prev, Next, or Exit button is clicked on
        case  ctlSelectEvent:
          if ( e.data.ctlSelect.controlID == Prev ) {
            if ( f > FormFirst )   FrmGotoForm( --f );
          }
	  else
            if ( e.data.ctlSelect.controlID == Next ) {
              if ( f < FormLast )   FrmGotoForm( ++f );
            }
	    else
              if ( e.data.ctlSelect.controlID == Exit )   goto _quit;
          goto _default;
			
        // if the repeating button is clicked on
        case  ctlRepeatEvent:
	  if ( e.data.ctlRepeat.controlID == Form3Minus ) {
            if ( cnt > 0 )   UpdateCount( -1 );
          }
          else
	    if ( e.data.ctlRepeat.controlID == Form3Plus ) {
              if ( cnt < MAX_COUNT )   UpdateCount( +1 );
            }
          goto _default;
	      
        case  frmLoadEvent:
          FrmSetActiveForm( FrmInitForm( e.data.frmLoad.formID ) );
          break;

        case  frmOpenEvent:
	  pfrm = FrmGetActiveForm( );
          FrmDrawForm( pfrm );
          // Assign an initial value 1000 to the field value.
	  if ( e.data.frmLoad.formID == Form3 )   UpdateCount( 0 );
          break;
	  
        case menuEvent:
	  break;
	    
        case appStopEvent:
	  goto _quit;
	      
        default:
_default:
          if ( FrmGetActiveForm( ) )
            FrmHandleEvent( FrmGetActiveForm( ), &e );
      }
    }
    
_quit:
    FrmCloseAllForms( );
  }

  return 0;
}


/***********************************************************************
*                                                                      *
*      This function processes the increment and decrement of the      *
*         field value between the two repeating buttons.               *
*                                                                      *
***********************************************************************/
 
void  UpdateCount( UInt16 val ) {
  FormPtr   frm;
  FieldPtr  fld;
  UInt16    obj;
  CharPtr   p;
  VoidHand  h;

  cnt += val;
  frm = FrmGetActiveForm( );
  obj = FrmGetObjectIndex( frm, Form3Count );
  fld = (FieldPtr) FrmGetObjectPtr ( FrmGetActiveForm( ), obj );
  h   = (VoidHand) FldGetTextHandle( fld );

  if( h == NULL ) {
    h = MemHandleNew( FldGetMaxChars( fld )+10 );
    ErrFatalDisplayIf( !h, "No Memory" );
  }
  p = (CharPtr) MemHandleLock( h );
  StrIToA( p, cnt );
  MemHandleUnlock( h );
  FldSetTextHandle( fld, (Handle) h );
  FldDrawField( fld );
}