Chris Pollett > Old Classes >
CS185c

( Print View )

Grades: [Sec2]

Submit: [Sec2]

Course Info:
  [Texts & Links]
  [Topics]
  [Grading]
  [HW Info]
  [Exam Info]
  [Regrades]
  [Honesty]
  [Announcements]

HW Assignments:
  [Hw1]  [Hw2]  [Hw3]
  [Hw4]  [Hw5]

Practice Exams:
  [Mid1]  [Mid2]  [Final]

                            












HW1 Solutions Page

Return to homework page.

/***********************************************************************
 *
 * PROJECT:  Hello World
 * FILE:     hello.c
 * AUTHOR:   Lonnon R. Foster
 *
 * MODIFIED BY Chris Pollett
 *
 * DESCRIPTION:  Hello World sample application.
 *
 * ORIGINAL VERSION CAN BE FOUND IN:
 * From Palm OS Programming Bible
 * ©2000 Lonnon R. Foster.  All rights reserved.
 *
 * REMARK: Mainly commented code I modified.
 *         Original code had 4 memory leaks which I didn't track
 *         done. My code doesn't add any additional.
 *
 ***********************************************************************/

#include <PalmOS.h>

#ifdef __GNUC__
#include "callback.h"
#endif

#include "helloRsc.h"


static Err StartApplication(void)
{
   FrmGotoForm(MainForm);
   return 0;
}

/*-----------------------------------------------*/ 

static void StopApplication(void)
{
   FrmCloseAllForms();
}

/*-----------------------------------------------*/ 

static void SaySomething(UInt16 alertID)

/* 
PURPOSE:    Outputs the passed alert box with the message
            string gotten from the text field in reversed followed by
            normal order  
RECEIVES:   alertID is ID of either Say Hello or Say Goodbye dialog  
RETURNS:    nothing
REMARKS:    could be more elegant uses all the Mem function requred by
            the HW.
 
*/ 


{
   FormType   *form = FrmGetActiveForm();
   FieldType  *field;
   MemHandle  h;
   UInt32 size;

   field = FrmGetObjectPtr(form, FrmGetObjectIndex(form, MainNameField));

   /* Begin modified code */

   h = FldGetTextHandle(field);
	size = FldGetTextLength(field);

   if (h) 
	{
      Char  *s, *s2,*mid;
      Char i;
      MemHandle handle;

      if(size > 0)
      {
         handle = MemHandleNew(size*2+1); /* size does not inclue EOL char */
         s = MemHandleLock((void *)h);
         s2 = MemHandleLock((void *)handle);
         mid = s+size;

         /* copy the string backwards */

         for( i = 0; i < size; i++)
            *(s2+i)=*(mid-i-1);

         /* then forwards */

         MemMove(s2+size,s,size);

         /* add end of line (EOL) char */
         MemSet(s2+2*size,1,'\0');
         
         if (*s2 != '\0') 
            FrmCustomAlert(alertID, s2, NULL, NULL);
      } 
      else 
      {
         FrmCustomAlert(alertID, "whoever you are", NULL,
                           NULL);
      }

      /* free locks and memory used */

      MemHandleUnlock((void *)h);
      MemHandleUnlock((void *)handle);
      MemHandleFree(handle);

   } 
   else /* End modified code */
   {
      FrmCustomAlert(alertID, "whoever you are", NULL, NULL);
   }
}

/*-----------------------------------------------*/         

static Boolean MainMenuHandleEvent(UInt16 menuID)
{
    Boolean    handled = false;
    FormType   *form;
    FieldType  *field;
    

    form = FrmGetActiveForm();
    field = FrmGetObjectPtr(form,
        FrmGetObjectIndex(form, MainNameField));

    switch (menuID) {
        case MainEditUndo:
            FldUndo(field);
            handled = true;
            break;
        case MainEditCut:
            FldCut(field);
            handled = true;
            break;
        case MainEditCopy:
            FldCopy(field);
            handled = true;
            break;
        case MainEditPaste:
            FldPaste(field);
            handled = true;
            break;
        case MainEditSelectAll:
            FldSetSelection(field, 0,
                            FldGetTextLength(field));
            handled = true;
            break;
            
        case MainEditKeyboard:
            SysKeyboardDialog(kbdDefault);
            handled = true;
            break;
            
        case MainEditGraffitiHelp:
            SysGraffitiReferenceDialog(referenceDefault);
            handled = true;
            break;
            
        case MainOptionsAboutHelloWorld:
            FrmAlert(AboutAlert);
            handled = true;
            break;
            
        default:
            break;
    }

    return handled;
}

/*-----------------------------------------------*/ 

static Boolean MainFormHandleEvent(EventPtr event)
{
    Boolean  handled = false;
    
#ifdef __GNUC__
    CALLBACK_PROLOGUE;
#endif

    switch (event->eType) {
        case frmOpenEvent:
        {
            FormType  *form = FrmGetActiveForm();

            FrmDrawForm(form);
            FrmSetFocus(form, FrmGetObjectIndex(form,
                MainNameField));
            handled = true;
        }
            break;

        case ctlSelectEvent:
            switch (event->data.ctlSelect.controlID) {
                case MainHelloButton:
                    SaySomething(HelloAlert);
                    handled = true;
                    break;
                
                case MainGoodbyeButton:
                    SaySomething(GoodbyeAlert);
                    handled = true;
                    break;

                default:
                    break;
            }
            break;

        case menuEvent:
            handled =
                MainMenuHandleEvent(event->data.menu.itemID);
            break;

        default:
            break;
    }

#ifdef __GNUC__
    CALLBACK_EPILOGUE;
#endif

    return handled;
}

/*-----------------------------------------------*/ 

static Boolean ApplicationHandleEvent(EventPtr event)
{
    FormType  *form;
    UInt16    formID;
    Boolean   handled = false;
    

    if (event->eType == frmLoadEvent) {
        formID = event->data.frmLoad.formID;
        form = FrmInitForm(formID);
        FrmSetActiveForm(form);
        
        switch (formID) {
            case MainForm:
                FrmSetEventHandler(form, MainFormHandleEvent);
                break;

            default:
                break;
        }
        handled = true;
    }

    return handled;
}


static void EventLoop(void)
{
    EventType  event;
    UInt16     error;

    
    do {
        EvtGetEvent(&event, evtWaitForever);
        if (! SysHandleEvent(&event))
            if (! MenuHandleEvent(0, &event, &error))
                if (! ApplicationHandleEvent(&event))
                    FrmDispatchEvent(&event);
    } while (event.eType != appStopEvent);
}


UInt32 PilotMain(UInt16 launchCode, MemPtr cmdPBP,
                 UInt16 launchFlags)
{
    Err  err;


    switch (launchCode) {
        case sysAppLaunchCmdNormalLaunch:
            if ((err = StartApplication()) == 0) {
                EventLoop();
                StopApplication();
            }
            break;

        default:
            break;
    }

    return err;
}