The calculator example

This is the calculator dialog built using XEBOT
 
 

 
This is all the XPE code required to drive the virtual calculator shown above.

proc CALCULATOR::digit_pressed

	define var val

	val = this.frame.calc_window.get_value()

	if val == 0 then
		val = this.digit
	else
		// each digit key on the calculator has a data
		// item in its context called 'digit' which is
		// assigned a number 0..9 corresponding to the
		// digit on the digit key being pressed

		val = $val + "%d":this.digit
	endif

	this.frame.calc_window.set_value(val)

endproc


proc CALCULATOR::point_pressed

	define var val

	val = this.frame.calc_window.get_value()

	val = $val + "."

	this.frame.calc_window.set_value(val)

endproc


proc CALCULATOR::op_pressed

	this.CALCULATOR::equal_pressed()

	define var val

	this.frame.acc = this.frame.calc_window.get_value()

	this.frame.calc_window.set_value(0)

	// each operator key on the calculator has a data
	// item in its context called 'op_type' which is
	// assigned a string corresponding to the operation
	// on the key being pressed. Valid string are
	//    CALCULATOR::do_add
	//    CALCULATOR::do_sub
	//    CALCULATOR::do_mult
	//    CALCULATOR::do_div

	this.frame.calc_op = this.op_type

endproc


proc CALCULATOR::equal_pressed

	define var val

	val = this.frame.calc_window.get_value()

	// the operation performed on the data was set up
	// by CALCULATOR::op_pressed and is the name of one
	// of the functions do_add, do_sub, do_mult or do_div

	val = (this.frame.calc_op)(float(this.frame.acc), float(val))

	this.frame.calc_op = "do_null"
	this.frame.acc     = 0

	this.frame.calc_window.set_value(val)

endproc


proc CALCULATOR::do_add(arg1, arg2)
	return arg1 + arg2
endproc


proc CALCULATOR::do_sub(arg1, arg2)
	return arg1 - arg2
endproc


proc CALCULATOR::do_mult(arg1, arg2)
	return arg1 * arg2
endproc


proc CALCULATOR::do_div(arg1, arg2)
	return arg1 / arg2
endproc


proc CALCULATOR::do_null(arg1, arg2)
	return arg2
endproc


proc CALCULATOR::ce_pressed

	// clear entry

	this.frame.calc_window.set_value(0)
endproc


proc CALCULATOR::ac_pressed

	// all clear

	this.frame.calc_window.set_value(0)
	this.frame.acc = 0
	this.frame.calc_op = "do_null"

endproc


proc CALCULATOR::negate_pressed

	// the +/- key

	define var val

	val = this.frame.calc_window.get_value()

	val = 0 - float(val)

	this.frame.calc_window.set_value(val)

endproc