Dynamically Sizing a Scrollable Region

There are times, particularly when you are working with a database application, when you cannot determine how many rows of data will be in your scrollable region until run-time. If you are creating your scrollable region in code, you can make the call to sr_def() after you know how many rows of data to specify. If you create your forms and scrollable regions in the Designer, however, you must specify the number of data rows in the Designer. The changes required differ depending on whether you have other items on the form in addition to the scrollable region. Both solutions are described below.

Solution 1: If the scrollable region is the only object on the form, follow these instructions:

Note: If the number of data rows is less than the number of visible rows in the scrollable region, you should allocate enough memory for all the visible rows in the region to avoid having garbage displayed in the empty rows. The amount of memory required in this case is equal to the memory required for the structure that holds the data for one row times the number of visible rows.

The following is a code fragment which illustrates this. The example is based on the following structure of a scrollable region created in the Designer and placed in an application header file:

typedef struct

    {

      UCHAR name[16];

      int age;

      UCHAR address[65];

    } SRSTRUCT

The following code fragment reads the form from the Designer library:

my_function()
{
SRPTR srp;
SRSTRUCT *datap;
int data_rows;

fmp = dl_fmget("formname", NULLP, funclistp, NULLP, libp);


Note that the code fragment uses a NULLP for the data structure, since we have not yet determined how large a structure we need to allocate.

Set the number of data rows in the scrollable region structure to the correct number of data rows:

srp = (SRPTR) i_namptr("sr_name", fmp);

    srp->datarowq = data_rows;

Use mem_get() to allocate the required amount of memory. Note that we are allocating at least enough memory for all visible rows in the scrollable region.

  datap = (SRSTRUCT *) mem_get(sizeof (SRSTRUCT) * 

        max(data_rows, srp->row_q));

Associate the data structure with the form:

  sfm_datap(datap, fmp);

Solution 2: If there are objects on the form in addition to a scrollable region, follow these instructions:

Use mem_get() to allocate the required amount of memory. The following code fragment illustrates the procedure. The example is based on the following structure of a scrollable region created in the Designer and placed in the application header file.

    typedef struct        /* Scrollable region data structure    */

    {

      UCHAR name[16];

      int  age;

      UCHAR address[65];

    } SRSTRUCT;



    typedef struct        /* Data form data structure        */

    {

      UCHAR company_name[25];

      UCHAR account_num[5];

      UCHAR contact_person[20];

    } WHOLE_FORM;

Following is a code fragment which works with those structures:

fmp = dl_fmget("formname", dataformp, funclistp, NULLP, libp);

my_function()

{

  SRPTR srp;

  SRSTRUCT *datap;

  WHOLE_FORM *dataformp;

  int data_rows = 20;

  dataformp = (WHOLE_FORM*) mem_get(sizeof (WHOLE_FORM));

Now you need to read in the form containing the scrollable region:

fmp = dl_fmget("formname", dataformp, funclistp, NULLP, libp);

Allocate memory for the scrollable region data structure and assign the memory to the scrollable region:

  srp = (SRPTR) i_namptr("sr_name", fmp);

      datap = (SRSTRUCT*) mem_get(sizeof (SRSTRUCT) * 

        max(data_rows, srp->row_q));

      ssr_datap(srp, datap, fmp);

Set the number of data rows in the scrollable region structure to the correct number of data rows:

  srp->datarowq = data_rows;

      . . .

      . . .

    }


Home Contents Previous Next