Chris Pollett > CS185C
( Print View )

Student Corner:
  [Grades Sec1]
 
  [Submit Sec1]
 
  [
Lecture Notes]

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 2
 * FILE:     helloMain.c
 * AUTHOR:   Lonnon R. Foster <author@palmosbible.com>
 *
 * MODIFIED BY Chris Pollett
 *
 * DESCRIPTION: Main code for Hello Word 2 sample program
 *
 * From Palm OS Programming Bible, Second Edition
 * http://www.palmosbible.com
 *
 * ©2002 Lonnon R. Foster.  All rights reserved.
 * This source code is not in the public domain, but you are hereby
 * granted permission to use it in any of your own projects,
commercial
 * or otherwise.
 *
 * History:
 *    Name  Date                     Description
 *    ----  ----                     -----------
 *    lrf   2002/04/23 10:47:53 PM   Created
 *    cp    2004/02/18  1:00:00 AM   Modified
 *

***********************************************************************/
#include <PalmOS.h>
#include "helloRsc.h"

/*-----------------------------------------------*/ 
static void SaySomething(UInt16 alertID)
/* 
PURPOSE:    Outputs the passed alert box with the message
            string gotten from the text field where each character
            is output three times in a row. i.e., hello becomes
hhheeellllllooo
RECEIVES:   alertID is ID of either Say Hello or Say Goodbye dialog  

RETURNS:    nothing
REMARKS:    could be more elegant uses all the Mem function required 
by
            the HW.
 
*/ 
{
    FormType   *form = FrmGetActiveForm();
    FieldType  *field;
    MemHandle fieldHandle;
    UInt32 size;



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

    size = FldGetTextLength(field);
    if ( size > 0)
    {
          FldCompactText(field);
          fieldHandle = FldGetTextHandle(field);
          if (fieldHandle)
          {
              Char  *fieldString, *threeCopiesString;
              MemHandle threeCopiesHandle;
              UInt32 i;
 
              threeCopiesHandle = MemHandleNew(size*3+1); 
                    /* size does not include EOL char */

              fieldString = MemHandleLock((void *)fieldHandle);
              threeCopiesString = MemHandleLock((void
*)threeCopiesHandle);

              for( i = 0; i < size; i++)
              {
                 /*copy a character from the field three times
(painfully slow!)
                    easier of course to do using pointing but wanted 
to 
                    do simple demo of MemMove */
                 UInt32 threei = 3*i;

                 MemMove(threeCopiesString+threei,fieldString+i,1); 

MemMove(threeCopiesString+threei+1,fieldString+i,1);

MemMove(threeCopiesString+threei+2,fieldString+i,1);
              }

              /* add end of line (EOL) char */
              MemSet(threeCopiesString+3*size,1,'\0');
               
              FrmCustomAlert(alertID, threeCopiesString, NULL,
NULL);

              MemHandleUnlock((void *)fieldHandle);
              MemHandleUnlock((void *)threeCopiesHandle);
              MemHandleFree(threeCopiesHandle);
          }
    }
    else
    {
        // No text in field, so display a "whoever you are"
        // dialog.
        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 EditUndo:
            FldUndo(field);
            handled = true;
            break;
            
        case EditCut:
            FldCut(field);
            handled = true;
            break;
            
        case EditCopy:
            FldCopy(field);
            handled = true;
            break;
            
        case EditPaste:
            FldPaste(field);
            handled = true;
            break;
            
        case EditSelectAll:
            FldSetSelection(field, 0,
                            FldGetTextLength(field));
            handled = true;
            break;

        case EditKeyboard:
            SysKeyboardDialog(kbdDefault);
            handled = true;
            break;

        case EditGraffitiHelp:
            SysGraffitiReferenceDialog(referenceDefault);
            handled = true;
            break;

        case OptionsAboutHelloWorld2:
            MenuEraseStatus(0);

            form = FrmInitForm(AboutForm);
            FrmDoDialog(form);                    
            FrmDeleteForm(form);

            handled = true;
            break;
        
        default:
            break;
    }
    
    return handled;
}


static Boolean MainFormHandleEvent(EventType *event)
{
    Boolean   handled = false;
    FormType  *form;


    switch (event->eType)
    {
        case frmOpenEvent:
            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;
    }

    return handled;
}


static Boolean AppHandleEvent(EventType *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 AppEventLoop(void)
{
    EventType  event;
    Err        error;

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


static Err AppStart(void)
{
    return errNone;
}


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


UInt32 PilotMain(UInt16 cmd, MemPtr cmdPBP,
                 UInt16 launchFlags)
{
    Err  error;


    switch (cmd)
    {
        case sysAppLaunchCmdNormalLaunch:
            error = AppStart();
            if (error)
                return error;
            
            FrmGotoForm(MainForm);
            AppEventLoop();
            
            AppStop();
            break;

        default:
            break;
    }

    return errNone;
}


Return to homework page.