.KEYWORD utility
.FLYINGHEAD PROGRAMMING POWER
.TITLE Utility routines for programmers
.DEPT
.SUMMARY If it weren’t for intrepid programmers, we wouldn’t have all the cool Palm software we use every day. Programming is tough work, but someone’s got to do it. If you’re one of those programmers (or if you’re learning how), this article will be a real score. In it, our Programming Power columnist Alan Jay Weiner introduces a number of very simple routines that can improve your programs and save you a ton of programming time. Now, maybe — just maybe — you can get that sleep you’ve been dreaming about.
.AUTHOR Alan Jay Weiner
We looked at resources last month, and started looking at forms and controls. This month, I’m going to skip ahead a bit and get some code into your hands. We can return to examine controls next month.
These are some of the routines from my personal library. They make working with buttons and text a bit easier.
.H1 Pointers to control objects
Many of the APIs require pointers to various control objects rather than simply the identification number you’ve assigned them. The GetObjectPtr routine in Listing A (see http://www.component-net.com/pp-extras/utility1.html) takes the ID number and determines the pointer. It assumes that the control is on the currently displayed form.
This routine is straight out of the Palm examples. Many of the following routines use it to reference control objects.
.H1 Hiding buttons
Sometimes we want to hide a button. For example, to make sure a particular feature isn’t available until some other conditions occur. Or we want to make a button visible because those conditions have been met.
Making a control invisible when the form is displayed is easy. Simply mark it "not usable" when you design the form. Then, when the form is first displayed, the control won’t be drawn.
Listing B (see http://www.component-net.com/pp-extras/utility2.html) shows two routines; MakeButtonInvisible hides a button. The other, MakeButtonVisible, unhides it.
.H1 Changing a button’s text
Listing C (see http://www.component-net.com/pp-extras/utility3.html), ChangeButtonText, changes the text on a button. Use this to make a button mean different things under different circumstances. It’s easier to change the text on a single button than to have multiple buttons in the same place.
.H1 Centering a line of text on the screen
This final routine, in Listing D (see http://www.component-net.com/pp-extras/utility4.html), centers a line of text. Actually, it’s two routines; the first sets the font unless the new font is CURRENT_FONT, or the value -1. While this could be done in-line in the DisplayCentered routine, we’ll see it in future text-display routines. It also updates the font value with the original font, so it can later be restored.
The regular fonts are defined in FONT.H; they are stdFont, boldFont, largeFont, symbolFont, symbol11Font, symbol7Font, and ledFont. The Palm III has added a new font called largeBoldFont.
DisplayCentered itself is fairly simple – it optionally changes to a new font, gets the width of the text string, and draws it in the right place.
One thing to note here; DisplayCentered assumes the screen is 160 pixels wide. That’s true for all current machines, but I’m sure at some point there will be Palm OS devices with more pixels. We really should get the screen width from the OS, so we’ll automatically adapt to hardware changes.
.H1 Next month
I haven’t decided what to write about next month – how about some feedback from you readers? What would you like to see? Would you like info on another small application? Or more details about controls? Or more utility routines? Or something else? Let me know what you’re interested in at alan@ajw.com.
.BIO Alan Weiner writes software. To rephrase the old Harley slogan: live to code, code to live. His email address is alan@ajw.com.
.DISCUSS http://www.component-net.com/webx?13@@.ee6c002


