#+TITLE: Chapter 9: Advanced GUI Programming, Controls, and Forms #+AUTHOR: Hermes & Claude #+DATE: 2026-10-07 #+PARENT: [[file:index.org][Index]] #+INCLUDE: goback.org * 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. ** 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. * 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: #+BEGIN_SRC cpp 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(bounds_.topLeftX + 2), static_cast(bounds_.topLeftY + 2), static_cast(bounds_.extentX - 4), static_cast(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; } }; #+END_SRC * Modal Dialogs vs. Modeless Forms Palm OS supports two distinct form paradigms: 1. *Modeless Forms (Application Screens)*: - Loaded into memory with ~FrmInitForm(formId)~. - Made active with ~FrmSetActiveForm(formP)~ and drawn with ~FrmDrawForm(formP)~. - Event processing happens asynchronously via the central ~AppHandleEvent~ and ~FrmDispatchEvent~ loop. - Closed with ~FrmCloseAllForms()~ or when switching to another form via ~FrmGotoForm(nextFormId)~. 2. *Modal Dialogs (~FrmDoDialog~)*: - A modal dialog runs its *own self-contained event loop*! - Invocation: #+BEGIN_SRC cpp FormPtr dialog = FrmInitForm(aboutFormId); UInt16 buttonHit = FrmDoDialog(dialog); FrmDeleteForm(dialog); #+END_SRC - ~FrmDoDialog~ intercepts 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! * 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: #+BEGIN_SRC cpp 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(target) & 0xFF; newEvent.data[1] = (static_cast(target) >> 8) & 0xFF; EvtAddEventToQueue(&newEvent); } AppScreen current() const { return currentScreen_; } }; #+END_SRC #+LF * 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