#! ./wafepython
#
# This is a test file for python build with the Athena 
# version of Wafe (renamed to wafepython). The script shows various
# calling structures between Wafe and python (python calling wafe
# and vice versa) and how return arguments can be passed between this
# two languages
# 
# Gustaf Neumann              Mohegan Lake, NY, Sat Jun  4 17:58:27

import wafe

wafe.cmd( """
mergeResources topLevel \
	*Label.borderWidth 0 \
        *background gray90 \
        *foreground black

Form f topLevel translations {<Key>q: exec(quit)}
  Label infoLine f label "Press a Button!"
  Box b f orientation horizontal fromVert infoLine
    Command c1 b \
	label "Python: test1" \
	callback { pythonS test1 {Hello World! Button %w was pressed!} }
    Command c2 b \
	label "Python: test2" \
	callback { puts "result from python is <[pythonS test2 a b c]>" }
    Command c3 b \
	label "Tcl: test3" \
	callback {test3 [string length %w]}
    Command c4 b \
	label "Python: test4" \
	callback { puts stderr "Test4: returns <[pythonS test4 testing...]>" }
    Command quit b \
	callback %w
realize
sV infoLine resizable true width [gV b width]
sV infoLine resizable false

proc test3 {w} {
   puts stderr "Test3: the python routine returns <[pythonS test3s $w]>"
}
""")

# a python function with one argument called via callback
def test1(s):
  print "Test1: ", s


# a python function with three arguments called via callback
# returning a result
def test2(a,b,c):
  print "Test2: <", a, "> <", b, "> <", c, ">"
  return 'ok'


# a python function with one argument called via callback,
# obtaining a string argument from Wafe and returning a result
def test3(i):
  return i+i

# a python function with one argument called via callback, 
# obtaining a string argument from Wafe, convert it into a number and 
# and returning a result back to Wafe; the result is automatically 
# converted into a string by the calling pythonS
def test3s(s):
  i = eval(s)
  return i+i


# A python subroutine with one argument called via callback,
# calling Wafe again via wafe.cmd(), which calls python.
# The return value of the inner python function (test3)
# is returned to Wafe, which returns it to the outer python
# subroutine (test4) that returns a result to the callback

def test4(n):
  print '...... Test4 got', n
  return 'the result <' + wafe.cmd('pythonS test3 123') + '>';

# getting a value from Wafe back to python
print "The width of the topLevel shell is ", wafe.cmd("gV topLevel width"),"!"

wafe.process_events()


