Click here to Skip to main content
15,894,343 members
Articles / Mobile Apps

The Palm Memory Manager API

Rate me:
Please Sign up or sign in to vote.
4.33/5 (3 votes)
5 Nov 2002CPOL5 min read 76.3K   123   20  
How to use Palm's Memory Manager API for dynamic memory allocation in palm handheld applications.
/******************************************************************************
 *
 * Copyright (c) 1999 Palm Computing, Inc. or its subsidiaries.
 * All rights reserved.
 *
 * File: Starter.cpp
 *
 *****************************************************************************/

#include <PalmOS.h>
#include "StarterRsc.h"

// need a global handle for demo purposes
static MemHandle myHandle = NULL;
static short nCount = 0;

/***********************************************************************
 *
 * FUNCTION:    DrawText
 *
 * DESCRIPTION: This routine centers text on the form
 *
 * PARAMETERS:  none
 *
 * RETURNED:    none
 *
 ***********************************************************************/
static void DrawText(Char* pText)
{
	short nCharWidth = 0;
	short width = 0, height = 0;
	
	// get the width of the string		
	nCharWidth = FntCharsWidth(pText, StrLen(pText));  
	
	// get the width and height of the string
	WinGetWindowExtent(&width, &height);
	
	// draw the text in the center
	WinDrawChars(pText, StrLen(pText), (width/2) - (nCharWidth/2), height/2);
}


/***********************************************************************
 *
 * FUNCTION:    SayHello
 *
 * DESCRIPTION: This routine draws "Hello World!" on the form
 *
 * PARAMETERS:  none
 *
 * RETURNED:    none
 *
 ***********************************************************************/
static void SayHello()
{
	if (MemHandleSize(myHandle) == 13 || 
		MemHandleResize(myHandle, 13) == 0)
	{
		// lock the handle before use 
		Char* pText = (Char*) MemHandleLock(myHandle);  
		
		// make sure none of the previous string is left in
		MemSet(pText, 13, 0);	
		
		// copy Hello World! into the char ptr
		StrCopy(pText, "Hello World!");

		// pass the pointer and draw it on the screen
		DrawText(pText);
	
		// unlock the handle
		MemHandleUnlock(myHandle);
	}
}


/***********************************************************************
 *
 * FUNCTION:    SayGoodbye
 *
 * DESCRIPTION: This routine draws "Goodbye!" on the form
 *
 * PARAMETERS:  none
 *
 * RETURNED:    none
 *
 ***********************************************************************/
static void SayGoodbye()
{
	nCount++;
	
	if (MemHandleSize(myHandle) == 15 || 
		MemHandleResize(myHandle, 15) == 0)
	{
		// lock the handle before use  
		Char* pText = (Char*) MemHandleLock(myHandle);  
		
		// make sure none of the previous string is left in	
		MemSet(pText, 15, 0);	

		// copy Goodbye World! into the char ptr
		StrCopy(pText, "Goodbye World!");
		
		if (nCount%2 == 0)
		{
			Char* pTemp = (Char*) MemPtrNew(15);
			
			StrCopy(pTemp, "Bye bye World!");
			
			MemMove(pText, pTemp, 7);
			
			MemPtrFree(pTemp);
			pTemp = NULL;
		}
				
		// pass the pointer and draw it on the screen
		DrawText(pText);
		
		// unlock the pointer in this function
		MemPtrUnlock(pText);
	}
}


/***********************************************************************
 *
 * FUNCTION:    MainFormInit
 *
 * DESCRIPTION: This routine initializes the MainForm form.
 *
 * PARAMETERS:  frm - pointer to the MainForm form.
 *
 * RETURNED:    nothing
 *
 ***********************************************************************/
static void MainFormInit(FormType* frmP)
{
	// we're going to lock this handle in our SayWhat functions
	// then we'll pass the pointer to the DrawText function
	myHandle = MemHandleNew(20);  // allocate 20 bytes
}


/***********************************************************************
 *
 * FUNCTION:    MainFormClose
 *
 * DESCRIPTION: This routine closes the MainForm form.
 *
 * PARAMETERS:  none
 *
 * RETURNED:    nothing
 *
 ***********************************************************************/
static void MainFormClose()
{
	// free the handle
	if (myHandle != NULL) 
	{
		MemHandleFree(myHandle);
		myHandle = NULL;
	}
}


/***********************************************************************
 *
 * FUNCTION:    MainFormButtonHandler
 *
 * DESCRIPTION: This routine is the event handler for buttons on the 
 *				Main form.
 *
 ***********************************************************************/
static Boolean MainFormButtonHandler(FormPtr formP, EventPtr eventP)
{
	Boolean handled = false;
	
	switch (eventP->data.ctlEnter.controlID)
	{
		case MainSayHelloButton:  // the Say Hello button
			SayHello();
			handled = true;
			break;
			
		case MainSayGoodbyeButton:  // the Say Goodbye button
			SayGoodbye();
			handled = true;
			break;
			
		default:
			break;      
	}
	
	return handled;
}


/***********************************************************************
 *
 * FUNCTION:    MainFormHandleEvent
 *
 * DESCRIPTION: This routine is the event handler for the 
 *              "MainForm" of this application.
 *
 * PARAMETERS:  eventP  - a pointer to an EventType structure
 *
 * RETURNED:    true if the event has handle and should not be passed
 *              to a higher level handler.
 *
 ***********************************************************************/
static Boolean MainFormHandleEvent(EventPtr eventP)
{
   	Boolean handled = false;
   	FormPtr frmP = FrmGetActiveForm();
   	
	switch (eventP->eType) 
	{
		case frmOpenEvent:
			MainFormInit(frmP);  // initialize the main form
			FrmDrawForm(frmP);
			handled = true;
			break;
				
		case frmCloseEvent:
			MainFormClose();  // free dynamically allocated memory here
			handled = true;
			break;
		
		case ctlSelectEvent:
			WinEraseWindow();  // erase the form
			FrmDrawForm(frmP);  // redraw the forms GUI controls
			handled = MainFormButtonHandler(frmP, eventP);
			break;
			
		default:
			break;	
	}
	
	return handled;
}


/***********************************************************************
 *
 * FUNCTION:    AppHandleEvent
 *
 * DESCRIPTION: This routine loads form resources and set the event
 *              handler for the form loaded.
 *
 * PARAMETERS:  event  - a pointer to an EventType structure
 *
 * RETURNED:    true if the event has handle and should not be passed
 *              to a higher level handler.
 *
 ***********************************************************************/
static Boolean AppHandleEvent(EventPtr eventP)
{
	UInt16 formId;
	FormPtr frmP;
	Boolean bRetVal = false;

	if (eventP->eType == frmLoadEvent)
	{
		// Load the form resource.
		formId = eventP->data.frmLoad.formID;
		frmP = FrmInitForm(formId);
		FrmSetActiveForm(frmP);

		// Set the event handler for the form.  The handler of the currently
		// active form is called by FrmHandleEvent each time is receives an
		// event.
		switch (formId)
		{
			case MainForm:
				FrmSetEventHandler(frmP, MainFormHandleEvent);
				break;
	
			default:
				break;
		}
		
		bRetVal = true;
	}
	
	return bRetVal;
}


/***********************************************************************
 *
 * FUNCTION:    AppEventLoop
 *
 * DESCRIPTION: This routine is the event loop for the application.  
 *
 * PARAMETERS:  nothing
 *
 * RETURNED:    nothing
 *
 ***********************************************************************/
static void AppEventLoop(void)
{
	UInt16 error;
	EventType event;

	do 
	{
		EvtGetEvent(&event, evtWaitForever);

		if (! SysHandleEvent(&event))
			if (! MenuHandleEvent(0, &event, &error))
				if (! AppHandleEvent(&event))
					FrmDispatchEvent(&event);

	} while (event.eType != appStopEvent);
}


/***********************************************************************
 *
 * FUNCTION:    PilotMain
 *
 * DESCRIPTION: This is the main entry point for the application.
 *
 * PARAMETERS:  cmd - word value specifying the launch code. 
 *              cmdPB - pointer to a structure that is associated with the launch code. 
 *              launchFlags -  word value providing extra information about the launch.
 * RETURNED:    Result of launch
 *
 ***********************************************************************/
UInt32 PilotMain( UInt16 cmd, MemPtr cmdPBP, UInt16 launchFlags)
{
	switch (cmd)
	{
		case sysAppLaunchCmdNormalLaunch:
			FrmGotoForm(MainForm);
			AppEventLoop();
			FrmCloseAllForms();
			break;
		
		default:
			break;
	}
	
	return errNone;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
I have been a professional developer since 1996. I live in Illinois, in the USA. I am married and have four children.

Comments and Discussions