Monday, June 1, 1998

Resources, forms and controls


By Alan Jay Weiner

Last month, we walked through a "simple" "hello, world" application. While we covered the code in almost tedious, thorough detail, I glossed over the resources somewhat. Resources are as important as the code itself; they're intricately entwined together. So this month we'll look at resources, forms, and controls. Grab a cup of coffee and let's begin!

What are resources?

Resources are "things" that a program can use. A resource could be as simple as a text string or as complicated as a screen form with menus, buttons, fields, and other resource "things." Forms and controls are types of resources.

Resources are essentially data structures tacked onto the end of the program file. Some Palm OS APIs expect to use certain resources; others you use by requesting a pointer and using the data at that address. For example, you might want to display a bitmap with your company logo. With the bitmap attached as a resource, you'd get a pointer to it by calling DmGet1Resource(), then display the bitmap by calling WinDrawBitmap(). Attaching the bitmap as a resource saves you from using up precious limited data and program space. While the resource could simply be in a separate data file, attaching them to the program is convenient - they're always there when you need them.

Resources can also be modified without changing the program itself. For example, if an application's text messages are all resources, changing the messages to a different language involves just using a different set of resources, rather than recompiling the program.

Some resources can refer to other resources. For example, a form resource is built out of a collection of control resources (buttons, fields, lists, etc.). Rather than using each resource individually, you'd simply say "display the form" and the OS's form-displayer knows to display all the other resources. That form might also have a "help" message which would point to a string resource. When the user hits the "i" on the menu bar, the form handler automatically displays the string resource.

After the program is compiled and linked, another tool builds the necessary data structures from a description of the resources, then attaches those structures to the program file. The program carries the resources on its back like a camper with a backpack full of supplies.

"Hello world" only used a few resources. It uses a couple of forms, a couple of menus, and a bitmap. Most programs use many more.

In fact, even the program's code and data are stored as resources; the .PRC file is a header structure followed by everything else as various resources.