Home Hobby Work Code WWW
wmail Palm route newpic zeef Euro
Tutorial RSI Clock Convert
prevtocnextindex

Positions

If you click the button a lot, the screen gets overfilled with Hello worlds. To prevent we are going to define a maximum number of Hello worlds that can be displayed. If the button is clicked more times, the Hello World that has been on the screen for the longest time gets removed.

To do this a global array is defined that holds the position of the Hello Worlds. I use a define for the maximum of Hello worlds, so this number can be changed easy. A special type for this array with position is also defined.

global.h
#ifndef GLOBAL_H
#define GLOBAL_H

// Maximal number of strings on the screen
#define MAX_STRING 5
// Positions of the stings

typedef struct {
  int x;
  int y;
} tPosition;

typedef tPosition tarrPositions[MAX_STRING];

tarrPositions arrPositions;

#endif 

The array with positions has to be initialised. The StartApplication function will be used for that. In this function, the x-coordinate will be set to -1, as a sign the position isn't filled yet.

StartApplication.c
#include "StartApplication.h"

// Initialises the application
Boolean StartApplication(void) {
  int i;
  // clear the positions
  for (i = 0; i < MAX_STRING; i++) {
    arrPositions[i].x = -1;
  }
  // Start the main form
  FrmGotoForm(MainForm);
  return false;
} // StartApplication

To use the array with position, the MainFormHandleEvent function must be adapted. A function AddHelloWorld has been defined to add a new position to the array, and remove the last position from that array. The other function DrawHelloWorlds draws the Hello words on the screen, after the screen was cleared first. These functions will be called if the button is pressed.

MainFormHandleEvent.c
#include "MainFormHandleEvent.h"

void AddHelloWorld(void) {
  int i;
  // move the positions one place up
  for (i = MAX_STRING - 1; i > 0; i--) {
    arrPositions[i].x = arrPositions[i-1].x;
    arrPositions[i].y = arrPositions[i-1].y;
  }
  // add a new position
  arrPositions[0].x = SysRandom(0) % 110;
  arrPositions[0].y = SysRandom(0) % 100 + 20;
} // AddHelloWorld

void DrawHelloWorlds(void) {
  RectangleType scrrectangle = { {0,15}, {160, 120} };
  int i;
  // clear the screen
  WinEraseRectangle(&scrrectangle, 0);
  // draw each hello world
  for (i = MAX_STRING - 1; i >= 0; i--) {
    if (arrPositions[i].x >= 0) {
      WinDrawChars("Hello world!", 12,
                   arrPositions[i].x,
                   arrPositions[i].y);
     }
  }
} // DrawHelloWorlds


Boolean MainFormHandleEvent(EventPtr event) {
  FormPtr frm;      
  Boolean handled = false;

  switch (event->eType) {
    case ctlSelectEvent:
      if (event->data.ctlEnter.controlID == MainHelloButton) {
        AddHelloWorld();
        DrawHelloWorlds();
        handled = true;         
      } // MainHelloButton
    break; // ctlSelectEvent:

    case frmOpenEvent:
      frm = FrmGetActiveForm();
      FrmDrawForm(frm);
      handled = true;
    break; // frmOpenEvent:

    // It was a menu event
    case menuEvent:
      // But which (I know there is only one option, 
      // but I might extend the program in the future)
      switch (event->data.menu.itemID) { 
        case HelloAboutMenuItem:
          frm = FrmInitForm(AboutForm);
          FrmDoDialog(frm);
          FrmDeleteForm(frm);
        break;
      }
      handled = 1;
    break;
  } // event->eType
  return handled;
} // MainFormHandleEvent

prevtocnextindex
Tutorial RSI Clock Convert
wmail Palm route newpic zeef Euro
Home Hobby Work Code WWW

Toni Cornelissen
13 September 2004
toni@dse.nl
Lid van de Technetium groep
Daily horoscope NetStat W3C Validator