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]

                            












HW2 Solutions Page

Return to homework page.

Below are the .bmp files are used for the assignment (they might not appear too clearly on non-color palms):

largeicon.bmp

smallicon.bmp

NW.bmp

NC.bmp

NE.bmp

W.bmp

C.bmp

E.bmp

SW.bmp

SC.bmp

SE.bmp

Here is the Makefile I used:

# Makefile for CS185c Fall 2002 HW2

APP             = hw2
ICONTEXT        = "Hw2"
APPID           = HW22
RCP             = $(APP).rcp
PRC             = $(APP).prc
SRC             = $(APP).c

CC              = m68k-palmos-gcc
PILRC           = pilrc
OBJRES          = m68k-palmos-obj-res
BUILDPRC        = build-prc


# Uncomment this if you want to build a GDB-debuggable version
 CFLAGS = -O0 -g
#CFLAGS = -O2

all: $(PRC)

$(PRC): grc.stamp bin.stamp;
	$(BUILDPRC) $(PRC) $(ICONTEXT) $(APPID) *.grc *.bin
	ls -l *.prc

grc.stamp: $(APP)
	$(OBJRES) $(APP)
	touch $@

$(APP): $(SRC:.c=.o)
	$(CC) $(CFLAGS) $^ -o $@

bin.stamp: $(RCP)
	$(PILRC) $^ $(BINDIR)
	touch $@

%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@
#               touch $<
# Enable the previous line if you want to compile EVERY time.

depend dep:
	$(CC) -M $(SRC) > .dependencies

clean:
	rm -rf *.o $(APP) *.bin *.grc *.stamp *~

veryclean: clean
	rm -rf *.prc *.bak


Here is the header file containing the various resource IDs used in this
project:

/****************************************************** 
* Copyright (c):   2002, All Rights Reserved. 
* Project:         CS 185C Homework #2 
* File:            hw2Rsc.h 
* Purpose:         header file with resource IDs for hw2 application  
* Start date:      10/12/02
* Programmer:      Chris Pollett 
* 
*******************************************************/

/*
  directional offsets -- by adding these amount to form IDs can move in
  a given direction
*/

#define VOFFSET 3 
#define HOFFSET 1

// Forms
#define FormNW 1001
#define FormNC 1002
#define FormNE 1003
#define FormW 1004
#define FormC 1005
#define FormE 1006
#define FormSW 1007
#define FormSC 1008
#define FormSE 1009

//Buttons

#define WestButton 1101
#define NorthButton 1102
#define SouthButton 1103
#define EastButton 1104

// Menubar
#define MainMenuBar 1200
#define MainOptionsAboutHW2 1201

// Alerts
#define OffWorldAlert 1300
#define AboutAlert 1301

//Bitmaps

#define NW 1401
#define NC 1402
#define NE 1403
#define W 1404
#define C 1405
#define E 1406
#define SW 1407
#define SC 1408
#define SE 1409

Here is the RCP resource file I used:

/****************************************************** 
* Copyright (c):   2002, All Rights Reserved. 
* Project:         CS 185C Homework #2 
* File:            hw2.rcp 
* Purpose:         resource file for hw2 application  
* Start date:      10/12/02
* Programmer:      Chris Pollett 
* 
*******************************************************/

#include "hw2Rsc.h"

APPLICATIONICONNAME ID 100 "Hw2"
APPLICATION         ID 1   "HW22" //Hw2 2002
VERSION             ID 1   "1.0"


// icons for application 
ICON      "largeicon.bmp"
SMALLICON "smallicon.bmp"


/*
   For each direction (NW, NW, NE, W, C, E, SW, SC, SE ) we have a form.
   This is very brute force, considering the formas are essentially the same.
   However, part of this exercise is to learn to switch from one form to 
   another. And at this point the HW was do still did not know much.

*/ 

FORM ID FormNW 0 0 160 160
MENUID MainMenuBar
USABLE
BEGIN
    TITLE "Hw2 NW"

    BUTTON "West" ID WestButton AT (1 147 AUTO AUTO)
    BUTTON "North" ID NorthButton
        AT (PrevRight+5 PrevTop AUTO AUTO)
    BUTTON "South" ID SouthButton
        AT (PrevRight+5 PrevTop AUTO AUTO)
    BUTTON "East" ID EastButton
        AT (PrevRight+5 PrevTop AUTO AUTO)
    FORMBITMAP AT (0 20) BITMAP NW
END

FORM ID FormNC 0 0 160 160
MENUID MainMenuBar
USABLE
BEGIN
    TITLE "Hw2 NC"

    BUTTON "West" ID WestButton AT (1 147 AUTO AUTO)
    BUTTON "North" ID NorthButton
        AT (PrevRight+5 PrevTop AUTO AUTO)
    BUTTON "South" ID SouthButton
        AT (PrevRight+5 PrevTop AUTO AUTO)
    BUTTON "East" ID EastButton
        AT (PrevRight+5 PrevTop AUTO AUTO)
    FORMBITMAP AT (0 20) BITMAP NC
END

FORM ID FormNE 0 0 160 160
MENUID MainMenuBar
USABLE
BEGIN
    TITLE "Hw2 NE"

    BUTTON "West" ID WestButton AT (1 147 AUTO AUTO)
    BUTTON "North" ID NorthButton
        AT (PrevRight+5 PrevTop AUTO AUTO)
    BUTTON "South" ID SouthButton
        AT (PrevRight+5 PrevTop AUTO AUTO)
    BUTTON "East" ID EastButton
        AT (PrevRight+5 PrevTop AUTO AUTO)
    FORMBITMAP AT (0 20) BITMAP NE
END

FORM ID FormW 0 0 160 160
MENUID MainMenuBar
USABLE
BEGIN
    TITLE "Hw2 W"

    BUTTON "West" ID WestButton AT (1 147 AUTO AUTO)
    BUTTON "North" ID NorthButton
        AT (PrevRight+5 PrevTop AUTO AUTO)
    BUTTON "South" ID SouthButton
        AT (PrevRight+5 PrevTop AUTO AUTO)
    BUTTON "East" ID EastButton
        AT (PrevRight+5 PrevTop AUTO AUTO)
    FORMBITMAP AT (0 20) BITMAP W
END

FORM ID FormC 0 0 160 160
MENUID MainMenuBar
USABLE
BEGIN
    TITLE "Hw2 C"

    BUTTON "West" ID WestButton AT (1 147 AUTO AUTO)
    BUTTON "North" ID NorthButton
        AT (PrevRight+5 PrevTop AUTO AUTO)
    BUTTON "South" ID SouthButton
        AT (PrevRight+5 PrevTop AUTO AUTO)
    BUTTON "East" ID EastButton
        AT (PrevRight+5 PrevTop AUTO AUTO)
    FORMBITMAP AT (0 20) BITMAP C
END

FORM ID FormE 0 0 160 160
MENUID MainMenuBar
USABLE
BEGIN
    TITLE "Hw2 E"

    BUTTON "West" ID WestButton AT (1 147 AUTO AUTO)
    BUTTON "North" ID NorthButton
        AT (PrevRight+5 PrevTop AUTO AUTO)
    BUTTON "South" ID SouthButton
        AT (PrevRight+5 PrevTop AUTO AUTO)
    BUTTON "East" ID EastButton
        AT (PrevRight+5 PrevTop AUTO AUTO)
    FORMBITMAP AT (0 20) BITMAP E
END

FORM ID FormSW 0 0 160 160
MENUID MainMenuBar
USABLE
BEGIN
    TITLE "Hw2 SW"

    BUTTON "West" ID WestButton AT (1 147 AUTO AUTO)
    BUTTON "North" ID NorthButton
        AT (PrevRight+5 PrevTop AUTO AUTO)
    BUTTON "South" ID SouthButton
        AT (PrevRight+5 PrevTop AUTO AUTO)
    BUTTON "East" ID EastButton
        AT (PrevRight+5 PrevTop AUTO AUTO)
    FORMBITMAP AT (0 20) BITMAP SW
END

FORM ID FormSC 0 0 160 160
MENUID MainMenuBar
USABLE
BEGIN
    TITLE "Hw2 SC"

    BUTTON "West" ID WestButton AT (1 147 AUTO AUTO)
    BUTTON "North" ID NorthButton
        AT (PrevRight+5 PrevTop AUTO AUTO)
    BUTTON "South" ID SouthButton
        AT (PrevRight+5 PrevTop AUTO AUTO)
    BUTTON "East" ID EastButton
        AT (PrevRight+5 PrevTop AUTO AUTO)
    FORMBITMAP AT (0 20) BITMAP SC
END

FORM ID FormSE 0 0 160 160
MENUID MainMenuBar
USABLE
BEGIN
    TITLE "Hw2 SE"

    BUTTON "West" ID WestButton AT (1 147 AUTO AUTO)
    BUTTON "North" ID NorthButton
        AT (PrevRight+5 PrevTop AUTO AUTO)
    BUTTON "South" ID SouthButton
        AT (PrevRight+5 PrevTop AUTO AUTO)
    BUTTON "East" ID EastButton
        AT (PrevRight+5 PrevTop AUTO AUTO)
    FORMBITMAP AT (0 20) BITMAP SE
END


/*
  We use the same menubar on all our forms
*/

MENU ID MainMenuBar
BEGIN
    PULLDOWN "Options"
    BEGIN
        MENUITEM "About HW2" MainOptionsAboutHW2
    END
END


/*
  We have two kinds of alerts. One indicating we went off the world;
  the other giving about information for this application

*/


ALERT ID OffWorldAlert
WARNING
BEGIN
    TITLE   "Off World"
    MESSAGE "You can't go ^1 \n"\
	    "from this screen."
    BUTTONS "OK"
END

ALERT ID AboutAlert
INFORMATION
BEGIN
    TITLE   "About Hw2"
    MESSAGE "Hw2\n"\
	    "Version 1.0"
    BUTTONS "OK"
END

//Bitmaps
BITMAPCOLOR NW "NW.bmp" FORCECOMPRESS
BITMAPCOLOR NC "NC.bmp" FORCECOMPRESS
BITMAPCOLOR NE "NE.bmp" FORCECOMPRESS
BITMAPCOLOR W "W.bmp" FORCECOMPRESS
BITMAPCOLOR C "C.bmp" FORCECOMPRESS
BITMAPCOLOR E "E.bmp" FORCECOMPRESS
BITMAPCOLOR SW "SW.bmp" FORCECOMPRESS
BITMAPCOLOR SC "SC.bmp" FORCECOMPRESS
BITMAPCOLOR SE "SE.bmp" FORCECOMPRESS

Finally, we have the hw2.c file for the project:

/***********************************************************************
*
* Copyright (c): 2002 All Rights Reserved. 
* Project:  CS185c Fall 2002 HW2
* File:     hw2.c
* Purpose: allows a user to scroll over a image that is too large to fit on
*          one Palm screen
* Start Date: 10/12/02 
* Programmer:   Chris Pollett
*
***********************************************************************/

#include <PalmOS.h>

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

#include "hw2Rsc.h"

/*-----------------------------------------------*/ 
static Err StartApplication(void)
/*
PURPOSE: called when appiclation first started
RECEIVES: nothing 
RETURNS: nothing
REMARKS: NONE
*/
{
    FrmGotoForm(FormC);
    return 0;
}

/*-----------------------------------------------*/ 
static void StopApplication(void)
/*
PURPOSE: called when application stopped (closes all open forms)
RECEIVES: nothing 
RETURNS: nothing
REMARKS: NONE
*/
{
	  FrmCloseAllForms();
}

/*-----------------------------------------------*/ 
static Boolean MainMenuHandleEvent(UInt16 menuID)
/*
PURPOSE: handle menu events (only about menu item to handle in this case)
RECEIVES: ID of the menu item selected 
RETURNS: true if handled correctly
REMARKS: NONE
*/
{
   Boolean    handled = false;
   FormType   *form;   

   form = FrmGetActiveForm();

   switch (menuID) 
   {            
      case MainOptionsAboutHW2:
         FrmAlert(AboutAlert);
         handled = true;
      break;
            
      default:
      break;
   }

   return handled;
}

/*-----------------------------------------------*/ 
static Boolean MainFormHandleEvent(EventPtr event)
/*
PURPOSE: Callback function used to handle events related to our forms
   here buttons clicks are handled to allow us to move between forms
RECEIVES: a pointer to the event that occurred  
RETURNS: true -- if event could be handled
REMARKS: the form IDs are number so that HOFFSET and VOFFSET can be
   used to switch between one form and its adjacent one
*/
{
   Boolean  handled = false;
   FormType  *form = FrmGetActiveForm();
   UInt16 formID = FrmGetFormId(form);

#ifdef __GNUC__
    CALLBACK_PROLOGUE;
#endif

	
   switch (event->eType) 
   {
      case frmOpenEvent:

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

      case ctlSelectEvent:

         switch (event->data.ctlSelect.controlID) 
         {
            case WestButton:
               if(formID == FormNW || formID == FormW || formID == FormSW)
                  FrmCustomAlert(OffWorldAlert, "west", NULL, NULL);
               else
                  FrmGotoForm(formID-HOFFSET);
                    
               handled = true;
            break;
                
            case NorthButton:
               if(formID == FormNW || formID == FormNC || formID == FormNE)
                  FrmCustomAlert(OffWorldAlert, "north", NULL, NULL);
               else
                  FrmGotoForm(formID-VOFFSET);

               handled = true;
            break;

            case SouthButton:

               if(formID == FormSW || formID == FormSC || formID == FormSE)
                  FrmCustomAlert(OffWorldAlert, "south", NULL, NULL);
               else
                  FrmGotoForm(formID+VOFFSET);

               handled = true;

            break;
                
            case EastButton:

               if(formID == FormNE || formID == FormE || formID == FormSE)
                  FrmCustomAlert(OffWorldAlert, "east", NULL, NULL);
               else
                  FrmGotoForm(formID+HOFFSET);

               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)
/*
PURPOSE: called mainly to load forms. This method sets the callback for that
   form
RECEIVES: the current event and process it if it is a frmLoadEvent  
RETURNS: true if could process
REMARKS: NONE
*/
{
   FormType  *form;
   UInt16    formID;
   Boolean   handled = false;
    

   if (event->eType == frmLoadEvent) 
   {
      formID = event->data.frmLoad.formID;
      form = FrmInitForm(formID);
      FrmSetActiveForm(form);
        
      if (FormNW <= formID && formID <= FormSE)
	   FrmSetEventHandler(form, MainFormHandleEvent);

      handled = true;
   }

   return handled;
}

/*-----------------------------------------------*/ 
static void EventLoop(void)
/*
PURPOSE: event loop for our app gets events and processes them
RECEIVES: nothing 
RETURNS: nothing
REMARKS: NONE
*/
{
   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)
/*
PURPOSE: main entry point into our application
RECEIVES:  launchCode to say how app launched,
RETURNS: error type of how application end (Hopefully errNone)
REMARKS: NONE
*/
{
   Err  err;

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

      default:
      break;
    }

    return err;
}