an example that:
- shows an input screen with an OK and a Cancel button;
This input screen is a dialog with some controls on it (edit - fields,
check boxes, sliders etc...);
- displays the values entered by the user (using Print statements).
You'll need some external files to get the example going. I've packed them
all into one package downloadable. You can get that package here:
http://www.sarmardar.nl/FreeBASIC/fb_lua_iup.zip
In the package you'll find
- A slightly updated version of lua.bi and laux.bi. Unpack these
to FreeBASIC/inc.
- The example (lua_test.bas and test_gui.lua).
- The latest version of the lua interpreter (iup5.1.a). Unpack this
to FreeBASIC/lib/win32.
- A bunch of dlls' needed to get the GUI going. It's a copy of the
files I'm using when combining IUP/LUA/FreeBASIC. Unpack these to
some place where the FreeBASIC compiler can find them.
Time for some code. First of the gui. It's written in LUA using IUP
(IUP = a GUI library)
Code: Select all
-- IupGetParam Example in IupLua
-- Shows a dialog with all possible fields.
require( "iuplua" )
require( "iupluacontrols" )
-- set initial values
pboolean = 1
pinteger = 3456
preal = 3.543
pinteger2 = 192
preal2 = 0.5
pangle = 90
pstring = "string text"
plist = 2
pstring2 = "second text\nsecond line"
cancel = 0
iup.SetLanguage("ENGLISH")
function param_action(dialog, param_index)
if (param_index == -3) then
cancel = 1
end
return 1
end
ret, pboolean, pinteger, preal, pinteger2, preal2, pangle, pstring, plist, pstring2 =
iup.GetParam("LUA/IUP Gui Example", param_action,
"Boolean: %b\n"..
"Integer: %i\n"..
"Real 1: %r\n"..
"Sep1 %t\n"..
"Integer: %i[0,255]\n"..
"Real 2: %r[-1.5,1.5]\n"..
"Sep2 %t\n"..
"Angle: %a[0,360]\n"..
"String: %s\n"..
"List: %l|item1|item2|item3|\n"..
"Sep3 %t\n"..
"Multiline: %m\n",
pboolean, pinteger, preal, pinteger2, preal2, pangle, pstring, plist, pstring2)
if (ret == 0) then
return -1
end
if (not iup.MainLoopLevel or iup.MainLoopLevel()==0) then
iup.MainLoop()
end
As you can see there is not much to the GUI. Just a bunch of variables,
a call to a function called GetParam and a GUI loop. LUA looks a bit like
FreeBASIC mixed with C.
A picture of the resulting gui:
Now for the FreeBASIC part of the example.
Code: Select all
#include "lua/lauxlib.bi"
Sub main()
Dim As lua_State Ptr L
Dim As Integer pboolean
Dim As Integer pinteger
Dim As Double preal
Dim As Integer pinteger2
Dim As Double preal2
Dim As Double pangle
Dim As ZString Ptr pstring
Dim As Integer plist
Dim As ZString Ptr pstring2
L = luaL_newstate()
luaL_openlibs(L)
Var status = luaL_loadfile(L,@"test_gui.lua")
Var result = lua_pcall(L,0,1,0)
If (result <> 0 ) Then
If (result = LUA_ERRRUN) Then
Print "LUA script failed: IUP dynamic link libraries not installed or unreachable"
Else
Print "Error executing test_gui.lua"
End If
lua_close(L)
Return
Else
lua_getfield(L,LUA_GLOBALSINDEX,@"cancel")
Var cancel = lua_tonumber(L,-1)
If (cancel = 1) Then
Print "The user pressed cancel"
lua_pop(L,1)
lua_close(L)
Return
Else
Print "Returned values: "
lua_getfield(L,LUA_GLOBALSINDEX,@"pboolean")
pboolean = lua_tonumber(L,-1)
Print "Boolean = ";pboolean
lua_getfield(L,LUA_GLOBALSINDEX,@"pinteger")
pinteger = lua_tonumber(L,-1)
Print "Integer 1 = ";pinteger
lua_getfield(L,LUA_GLOBALSINDEX,@"preal")
preal = lua_tonumber(L,-1)
Print "Real 1 = ";preal
lua_getfield(L,LUA_GLOBALSINDEX,@"pinteger2")
pinteger2 = lua_tonumber(L,-1)
Print "Integer 2 = ";pinteger2
lua_getfield(L,LUA_GLOBALSINDEX,@"preal2")
preal2 = lua_tonumber(L,-1)
Print "Real 2 = ";preal2
lua_getfield(L,LUA_GLOBALSINDEX,@"pangle")
pangle = lua_tonumber(L,-1)
Print "Angle = ";pangle
lua_getfield(L,LUA_GLOBALSINDEX,@"pstring")
pstring = lua_tostring(L,-1)
Print "String = ";*pstring
lua_getfield(L,LUA_GLOBALSINDEX,@"plist")
plist = lua_tonumber(L,-1)
Print "List = ";plist
lua_getfield(L,LUA_GLOBALSINDEX,@"pstring2")
pstring2 = lua_tostring(L,-1)
Print "String 2 = ";*pstring2
lua_pop(L,10)
lua_close(L)
Print "String 2 = ";*pstring2
End If
Return
End If
Return
End Sub
main()
a variable defined in the LUA script LUA pushes the value of the
variable onto a stack. The FB program uses lua_getfield to instruct
LUA to push the value of a variable onto the stack.
After a value has been pushed FreeBASIC can get the value from the
stack by using lua_tonumber, lua_tostring etc...
Getting the value from the stack does not pop the value from
the stack. For example, lua_tostring(L,-1) means "get the value at the top
of the stack and return it as a string". -1 = 'top of the stack'
(-2 = top of the stack - 1, -3 = top of the stack - 2 etc... ).
After the program is done pushing values to the stack it pops all the values
from the stack again using lua_pop (the second parameter of lua_pop denotes
the number of values that need to be popped from the stack).
Apart from the stack operations the example is an easy way to get
some input from an user.
And it's cross - platform as well (WIN/LIN/UNIX).
If you want to use the above example using Linux go to
http://sourceforge.net/projects/iup/files/
and pick the version of your choice (there are files for linux using the
'old' kernel (2.4) and Linux using the 'new' kernel (2.6).)
A recent version of LUA should be available for download from the package
repository of the Linux distro you are using. IUP is in the Debian package
repository so I'm guessing it's available for Ubuntu (and other Linux distributions)
as well.
Buon fine settimana :)