Java and the Waba Toolkit
by Al Williams

Listing One
// RPN calculator for CE or Palm
// Al Williams

import waba.ui.*;
import waba.fx.*;
import waba.util.*;

public class alcalc extends MainWindow
{
 final int stacksize=4;  // Set stack depth

// Buttons and display
 Button btns[] = new Button[18];
 Label led = new Label("0.0",Label.RIGHT);

// This is the stack
 float stack[] = new float[stacksize];
 float dp=0.0f;  // decimal point scaling

 int sp=0;  // stack pointer
 int lift=0; // stack lift (new entry) flag

// Scale factors for the current window
 int hi;  
 int wid;

// Set incr==1 to increment; -1 to decrement
// circularizes based on stacksize
 int addsp(int sp,int incr) 
  {
  if (incr==-1 && sp==0) return stacksize-1;
  if (incr==1 && sp==stacksize-1) return 0;
  return sp+incr;
  }

// set up a button
// x,y are row and column NOT pixels
// width is 1 for single-width, 2 for double width
 void setBtn(int i,String lbl,int x,int y, int width)
  {
  btns[i]=new Button(lbl);
  btns[i].setRect(x*(wid+5),y*(hi+5),width*wid,hi);
  add(btns[i]);
  }
// Set up all the work
 public void onStart()
  {
  Rect r;
  r=getRect();

  hi=r.height/7;
  wid=r.width/5;
  stack[sp]=0;
// display
  led.setRect(0,0,r.width-10,hi);
  add(led);
// set up all buttons
  setBtn(0,"0",1,4,1);
  setBtn(1,"1",0,3,1);
  setBtn(2,"2",1,3,1);
  setBtn(3,"3",2,3,1);
  setBtn(4,"4",0,2,1);
  setBtn(5,"5",1,2,1);
  setBtn(6,"6",2,2,1);
  setBtn(7,"7",0,1,1);
  setBtn(8,"8",1,1,1);
  setBtn(9,"9",2,1,1);
  setBtn(10,"Enter",0,5,2);
  setBtn(11,"+",3,1,1);
  setBtn(12,"-",3,2,1);
  setBtn(13,"*",3,3,1);
  setBtn(14,"/",3,4,1);
  setBtn(15,"+/-",0,4,1);
  setBtn(16,".",2,4,1);
  setBtn(17,"x<>y",2,5,2);
  }
// display number at top of stack
void disp(int newlift)
  {
     led.setText(waba.sys.Convert.toString(stack[sp]));
     lift=newlift;
  } 
// Handle key pressses
public void onEvent(Event event)
  {
  int i=-1;
  int leaddp=0;  // leading decimal point?
  int neg=1;  // 1 means positive number
  if (event.type==KeyEvent.KEY_PRESS)
    {
    KeyEvent kevent = (KeyEvent)event;
    if (kevent.key==IKeys.ENTER) kevent.key='\n';  // Enter key
// convert key to button #
    String keys="0123456789\n+-*/ .";  
    for (i=0;i<keys.length();i++)
    if (keys.charAt(i)==kevent.key) break;
    if (i==keys.length()) return;
    }
  else if (event.type == ControlEvent.PRESSED) // button
    {
    for (i=0;btns[i]!=event.target;i++); // scan for button
    }
  if (i!=-1)
    {
    switch (i)

      {
case 16:   // decimal point
      leaddp=1;
      if (dp==0.0f) dp=10.0f;
      if (lift==0) break;  // if not first entry, nothing special
      i=0;    // otherwise, treat like a 0 key
// fall into 0 case
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:  // digits
    if (lift==1)  // new number?
      {
      sp=addsp(sp,1);
      stack[sp]=0;
      lift=0;
      dp=0.0f;
      }
    else
      leaddp=0;  // ignore lead dp flag if old number
    
    if (stack[sp]<0)  // get absolute value of tos
      {
      neg=-1;
      stack[sp]=-stack[sp];
      }
    if (dp==0.0f)   // whole number
      {
      stack[sp]=stack[sp]*10+i;
      }
    else  // fractional number
      {
      stack[sp]=stack[sp]+i/dp;
      dp*=10;
      }
// if number was negative, make it negative again
    stack[sp]=stack[sp]*neg;
    disp(0);
    if (leaddp==1) dp=10.0f;
        break;
// enter key
case 10:
    i=addsp(sp,1);
    stack[i]=stack[sp];
    sp=i;
        lift=1;
    break;
// +
case 11:
   i=sp;
    sp=addsp(sp,-1);
    stack[sp]=stack[sp]+stack[i];
    disp(1);
    break;
// -
case 12:
    i=sp;
    sp=addsp(sp,-1);
    stack[sp]=stack[sp]-stack[i];
    disp(1);
    break;
// *
case 13:
    i=sp;
    sp=addsp(sp,-1);
    stack[sp]=stack[sp]*stack[i];
    disp(1);
    break;
// /
case 14:
    i=sp;
    sp=addsp(sp,-1);
    stack[sp]=stack[sp]/stack[i];
    disp(1);
    break;
// +/-
case 15:
      stack[sp]=-stack[sp];
      disp(lift);
      break;
// x<>y
case 17:
    float tmp;
    i=addsp(sp,-1);
    tmp=stack[sp];
    stack[sp]=stack[i];
    stack[i]=tmp;
    disp(1);
    break;
      }
    }
  }
}


Listing Two
@echo off

rem compile program, build warp files and launchers
javac *.java
warp c /q alcalc *.class
exegen /q /i icon.bmp alcalc alcalc alcalc
echo To test program:
echo java waba.applet.Applet alcalc






4

