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
}

Simple PHP security patch

I’ve created (after some weird experiences with users in my hosting) small php patch (for version 4.3.10), which disables remote includes.This patch doesn’t work with Zend Optimizer enabled unfortunately :(

Download the patch there. After applying, see php.ini-dist and readme.security

example of bad code:

<?php 
  $page = $_GET['page']; 
  include ($page); 
?>

example of better code:

<?php 
  // filter all unneeded characters 
  $page = eregi_replace("[^a-z0-9_]","", $_GET['page']).".inc.php"; 
  // test if $page exists and is file 
  if (strlen($page) && @file_exists($page) && @is_file($page)) { 
    require_once ($page); 
  } 
?> 

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 ””.