Chapter 9: Advanced GUI Programming, Controls, and Forms
This is an LLM ass-isted document, as are all in the ass/ subdirectory.
Of these, here in the dump/ subdirectory I keep the sloppy & doubtful ones.
I keep these as learning materials for myself, yet to be went thru.
1. The Architecture of Palm OS Forms and Windows
Palm OS user interface programming is distinct from desktop windowing systems. The display is typically 160x160 pixels (or 320x320 on OS 5 High-Density devices) and only one active form (window) receives input at a time. Every pixel and interaction must be deliberate.
1.1. The UI Object Hierarchy
The Palm OS Window Manager and Form Manager organize UI elements into a hierarchy:
- Draw Window (
WinHandle): The offscreen or onscreen bitmap canvas where graphics routines draw pixels, lines, and rectangles. - Form (
FormType/FormPtr): A window containing a collection of interactive UI objects (controls, fields, lists). - UI Objects:
- Controls (
ControlType): Push buttons, repeating buttons, checkboxes, popup triggers, and selector triggers. - Fields (
FieldType): Editable or non-editable text areas with Graffiti handwriting input support. - Lists (
ListType): Scrollable lists of text items. - Tables (
TableType): Multi-column, multi-row spreadsheet-like grids. - Scrollbars (
ScrollBarType): Vertical scroll indicators and thumbs. - Graffiti State Indicators: Shows shift, caps lock, and punctuation states.
- Controls (
2. Designing Complex Custom Controls in C++
While standard buttons can be created in PilRC resource files, professional Palm OS applications frequently create gadgets (custom controls) that handle pen taps and render custom graphics directly.
Let us build an interactive, reusable C++ Toggle Switch control that renders custom bevels and tracks pen state:
using UInt16 = unsigned short; using Int16 = short; using Boolean = unsigned char; struct RectangleType { Int16 topLeftX; Int16 topLeftY; Int16 extentX; Int16 extentY; }; extern "C" { void WinDrawRectangle(const RectangleType* rP, UInt16 cornerDiam); void WinEraseRectangle(const RectangleType* rP, UInt16 cornerDiam); void WinDrawRectangleFrame(UInt16 frameType, const RectangleType* rP); void WinDrawChars(const char* chars, Int16 len, Int16 x, Int16 y); } class ToggleSwitch { RectangleType bounds_; bool state_; const char* label_; public: constexpr ToggleSwitch(Int16 x, Int16 y, Int16 w, Int16 h, const char* label) : bounds_{x, y, w, h}, state_(false), label_(label) {} bool isChecked() const { return state_; } void toggle() { state_ = !state_; } void Draw() const { // Draw standard control frame WinDrawRectangleFrame(1 /* simpleFrame */, &bounds_); // Render interior state RectangleType inner = { static_cast<Int16>(bounds_.topLeftX + 2), static_cast<Int16>(bounds_.topLeftY + 2), static_cast<Int16>(bounds_.extentX - 4), static_cast<Int16>(bounds_.extentY - 4) }; if (state_) { WinDrawRectangle(&inner, 0); // Solid fill when ON } else { WinEraseRectangle(&inner, 0); // Blank when OFF } // Draw label text beside the toggle if (label_) { Int16 textX = bounds_.topLeftX + bounds_.extentX + 6; Int16 textY = bounds_.topLeftY + 2; WinDrawChars(label_, 6, textX, textY); } } bool HandlePen(Int16 penX, Int16 penY) { if (penX >= bounds_.topLeftX && penX <= (bounds_.topLeftX + bounds_.extentX) && penY >= bounds_.topLeftY && penY <= (bounds_.topLeftY + bounds_.extentY)) { toggle(); Draw(); return true; // Handled } return false; } };
3. Modal Dialogs vs. Modeless Forms
Palm OS supports two distinct form paradigms:
- Modeless Forms (Application Screens):
- Loaded into memory with
FrmInitForm(formId). - Made active with
FrmSetActiveForm(formP)and drawn withFrmDrawForm(formP). - Event processing happens asynchronously via the central
AppHandleEventandFrmDispatchEventloop. - Closed with
FrmCloseAllForms()or when switching to another form viaFrmGotoForm(nextFormId).
- Loaded into memory with
- Modal Dialogs (
FrmDoDialog):- A modal dialog runs its own self-contained event loop!
Invocation:
FormPtr dialog = FrmInitForm(aboutFormId); UInt16 buttonHit = FrmDoDialog(dialog); FrmDeleteForm(dialog);
FrmDoDialogintercepts all events, waits until the user taps a button defined with the modal flag, cleans up the window stack, and returns the tapped button's resource ID!
4. Multi-Form Navigation State Pattern in C++
When building complex applications with multiple screens (e.g., Main View, Settings, Record Edit), managing form lifecycles cleanly without leaking form handles requires a state machine:
enum class AppScreen : UInt16 { Main = 1000, Settings = 2000, Detail = 3000 }; class NavigationController { AppScreen currentScreen_ = AppScreen::Main; public: void SwitchTo(AppScreen target) { currentScreen_ = target; // Post a frmGotoEvent to the system event queue EventType newEvent; newEvent.eType = 4; // frmGotoEvent newEvent.data[0] = static_cast<UInt16>(target) & 0xFF; newEvent.data[1] = (static_cast<UInt16>(target) >> 8) & 0xFF; EvtAddEventToQueue(&newEvent); } AppScreen current() const { return currentScreen_; } };
5. Further Reading & Reference Resources
- Books & Official UI Design:
- PalmSource, Inc. Palm OS User Interface Guidelines. Complete guide on button placement, scrollbar behavior, and 1-handed navigation: https://palm.wiki/development/docs/601/UI_Guidelines/
- Neil Rhodes & Julie McKeehan. Palm OS Programming: The Developer's Guide (Chapters 4-7: Forms, Menus, and Controls).
- Custom Controls & Gadgets:
- Aaron Ardiri's Palm OS Game & GUI Development Tutorials: https://ardiri.com
- Table Manager & Custom Draw Procedures: https://palm.wiki/development/docs/601/PalmOSCompanion/Table_Chapter.html