Progress bar

from palm-dev e-mail list, credit goes to Logan Shaw

typedef struct
{
    RectangleType bounds;
    WinHandle savedbits;
} ProgBar;

void OpenProgressBar (ProgBar *progbar)
{
    UInt16 savebitserror;

    progbar->savedbits = WinSaveBits (& progbar->bounds, & savebitserror);
    WinEraseRectangle (& progbar->bounds, 0);
}

void CloseProgressBar (ProgBar *progbar)
{
    if (progbar->savedbits)
    {
        WinRestoreBits (
          progbar->bounds.topLeft.x, progbar->bounds.topLeft.y,
          progbar->savedbits);
    }
    else
    {
  /* maybe should enqueue a frmUpdateEvent instead, maybe not */
        WinEraseRectangle (& progbar->bounds, 0);
    }
}

void UpdateProgressBar (ProgBar *progbar, Int32 numerator, Int32 denominator)
{
    RectangleType fillrect;

    RctCopyRectangle (& progbar->bounds, & fillrect);
    fillrect.extent.x = progbar->bounds.extent.x * numerator / denominator;

    WinDrawRectangle (& fillrect, 0);
}

void TestProgressBar ()
{
    Int32 i;
    const Int32 max = 25;
    ProgBar progbar;

    progbar.bounds.topLeft.x = 20;
    progbar.bounds.topLeft.y = 70;
    progbar.extent.x = 120;
    progbar.extent.y = 20;

    OpenProgressBar (& progbar);
    for (i = 0; i <= max; i++)
    {
        UpdateProgressBar (& progbar, i, max);
        SysTaskDelay (SysTicksPerSecond() / 5);
    }
    CloseProgressBar (& progbar);
}

Using libraries

Load and initializing the library

#include
int MathLibRef = -1;
...
// is library already loaded?
err = SysLibFind("MathLib", &MathLibRef);
if (err != 0) {
  // negative, load
  err = SysLibLoad('libr', 'MthL', &MathLibRef);
  if (err == 0) {
    err = MathLibOpen (MathLibRef, 1);
  }
}

Closing library

if (MathLibRef != -1) {
  Err err;
  UInt16 usecount;
  err = MathLibClose (MathLibRef, &usecount);
  if (usecount == 0) {
    SysLibRemove (MathLibRef);
  }
}

Preferences

Preferences structure definition

typedef struct {
  int skeletonData;
} Prefs;
Prefs prefs;

Open preferences

void startApp() {
  Int16 prefSize = sizeof(Prefs);
  if ((PrefGetAppPreferences (AppCreator,
           1000, // pref database id
           &prefs,
           &prefSize,
            true) // saved during Hotsync
    == noPreferenceFound)
    || (prefSize != sizeof(Prefs))) {
          // default initialization, since discovered
          // Prefs was missing or old.
    prefs.skeletonData=1;
  }
}

Write preferences

void stopApp() {
  PrefSetAppPreferences (AppCreator,
        1000,  // pref database id
        1,   // version of pref database
        &prefs,
        sizeof(Prefs),
        true);  // saved during hotsync
}

Changing graphics mode

Boolean SetScreenColorDepth(int depth)
{
    UInt16 reqDepth=depth;
    Err err = WinScreenMode(winScreenModeSet,NULL,NULL,&reqDepth,NULL);
    return !err;
}

Boolean RestoreScreenColorDepth(void)
{
    Err err = WinScreenMode(winScreenModeSetToDefaults,NULL,NULL,NULL,NULL);
    return !err;
}

You must set color depth before any window is created. Check error codes for unsupported modes.

Displaying an alert window

resource – .h file:

#define CustomAlert 1000

resource – .rcp file:

ALERT ID CustomAlert
BEGIN
  TITLE "custom alert"
  MESSAGE "^1\n^2\n^3"
  BUTTONS "OK"
END

code:

Char alertMsg[64];
StrPrintF(alertMsg, "Your value is %d", yourValue);
FrmCustomAlert(CustomAlert, alertMsg, "", "");

do NOT pass NULL value as param in FrmCustomAlert – always use ””.

Double buffering

This is an example, how to do and use double buffering (offscreen window) to create smooth animations

WinHandle displayWindow;
WinHandle offscreenWindow;

void CreateOffScreen() {
    Err err;
    offscreenWindow = WinCreateOffscreenWindow(160,160, screenFormat, &err);
}

void PaintOffScreen() {
    WinSetDrawWindow(offscreenWindow);
    // draw there
}

void Show() {
    RectangleType bounds;

    WinSetDrawWindow(displayWindow);
    WinGetBounds(displayWindow, &bounds);
    WinCopyRectangle (offscreenWindow, 0, &bounds, 0, 0, 0);
}