Programming and Scripting :: murgaLua tests



Since MaxiJavi introduced murgaLua to these forums, and since John Murga himself was kind enough to build a version which works great in DSL with no additional dependencies, I thought it'd be a crime not to dive into it.  So....as I struggle to learn the differences between this application and Lua-FLTK, I'll be posting what I learn here.

The first bit is a demonstration of standard buttons.  Comparing murgaLua to FLTK, there seem to be only 3 button types that are not supported: shadow, rounded, and diamond.  Those three are, in my opinion, fairly useless anyway =o)

Code Sample
ww=360 --window width
wh=420 --window height

presses = 0 -- used for the repeat button
function button_press(self)
if self == butts[10] or self == butts[11] or self == butts[12] then
if self:value() == 1 then status="activated" else status="deactivated" end
out:value("\""..self:label().."\" was "..status)
presses = 0
elseif self == butts[9] then
presses = presses+1
out:value("\""..self:label().."\" was pressed "..presses.." times")
else
out:value("\""..self:label().."\" was pressed")
presses = 0
end
end

w=fltk:Fl_Window(ww,wh,"murgaLua Button Demo")
butts={}

----------
---- Here are two methods of laying out an array of similar widgets

-- Method 1
--butts[1]=fltk:Fl_Button(10,10,ww-20,25,"Fl_Button default")
--butts[2]=fltk:Fl_Button(10,40,ww-20,25,"Fl_Button down")
--butts[3]=fltk:Fl_Button(10,70,ww-20,25,"Fl_Button thin")
--butts[4]=fltk:Fl_Button(10,100,ww-20,25,"Fl_Button thin down")
--butts[5]=fltk:Fl_Button(10,130,ww-20,25,"Fl_Button engraved")
--butts[6]=fltk:Fl_Button(10,160,ww-20,25,"Fl_Button embossed")
--butts[7]=fltk:Fl_Button(10,190,ww-20,25,"Fl_Button border")

-- Method 2
labels={"default","down","thin","thin down","engraved","embossed","border"}
for i = 1,7 do butts[i]=fltk:Fl_Button(10,30*i-20,ww-20,25,"Fl_Button "..labels[i]) end
----------

butts[2]:box(fltk.FL_DOWN_BOX)
butts[3]:box(fltk.FL_THIN_UP_BOX)
butts[4]:box(fltk.FL_THIN_DOWN_BOX)
butts[5]:box(fltk.FL_ENGRAVED_BOX)
butts[6]:box(fltk.FL_EMBOSSED_BOX)
butts[7]:box(fltk.FL_BORDER_BOX)
butts[8]=fltk:Fl_Return_Button(10,220,ww-20,25,"Fl_Return_Button")
butts[9]=fltk:Fl_Repeat_Button(10,250,ww-20,25,"Fl_Repeat_Button")
butts[10]=fltk:Fl_Light_Button(10,280,ww-20,25,"Fl_Light_Button")
butts[11]=fltk:Fl_Round_Button(10,310,ww-20,25,"Fl_Round_Button")
butts[12]=fltk:Fl_Check_Button(10,340,ww-20,25,"Fl_Check_Button")

for i = 1,12 do
butts[i]:callback(button_press)
end
out=fltk:Fl_Output(10,370,ww-20,25)

w:show()
Fl:run()


The second one is just a clock widget...nothing special or interactive added to it, but i thought it was kind of an interesting widget to include in such a small toolbox.
Code Sample
ww=180 --window width
wh=200 --window height

w=fltk:Fl_Window(ww,wh,"murgaLua Clock Demo")

clock=fltk:Fl_Clock(5,5,ww-10,ww-10,"a clock")
clock:box(fltk.FL_DOWN_BOX)

w:show()
Fl:run()

Do you know if murgaLua is compatable with the version of sqlite in DSL?  I was going to try to check it out but haven't had time.
I have the latest sqlite in dsl-3.1 pre testing.
And murgaLua sql example worked fine.
So stay tuned for that.

Fl_Positioner:
I'm not sure what use I'd have for this this particular widget, but it's pretty cool.
Code Sample
ww=350 --window width
wh=400 --window height

function thing_cb()
xpos=string.format("%.2f",thing:xvalue()*100)
ypos=string.format("%.2f",thing:yvalue()*100)
out:value("X: "..xpos.."  Y: "..ypos)
--out:value("X: "..thing:xvalue().."  Y: "..thing:yvalue())
end

w=fltk:Fl_Window(ww,wh,"murgaLua Positioner Demo")

frame=fltk:Fl_Box(2,2,ww-4,ww-4); frame:box(fltk.FL_UP_BOX)

thing=fltk:Fl_Positioner(5,5,ww-10,ww-10,"percentage of window size"); thing:box(fltk.FL_DOWN_BOX)
thing:callback(thing_cb)

out=fltk:Fl_Output(5,wh-30,ww-10,25)

w:show()
Fl:run()


Fl_Progress:
A progress bar. It was actually kinda difficult to come up with a demo for this, since lua apparently has no sleep function and the widget apparently cannot be controlled through a single loop....so i just made it a button instead.
Code Sample
ww=350 --window width
wh=100 --window height

w=fltk:Fl_Window(ww,wh,"murgaLua Progress Demo")

thing=fltk:Fl_Progress(10,10,ww-20,30,"0 %"); thing:box(fltk.FL_DOWN_BOX)
thing:selection_color(fltk.FL_BLACK)

function go_cb()
if thing:value() == 100 then
thing:selection_color(fltk.FL_BLACK)
thing:value(0)
else
if thing:value() >= 75 then thing:selection_color(fltk.FL_RED) end
thing:value(thing:value()+1)
end
thing:label(thing:value().." %")
end

go=fltk:Fl_Repeat_Button(10,50,ww-20,30,"hold me down"); go:callback(go_cb)

w:show()
Fl:run()


I probably should start properly terminating my lines, but you get into a habit and end up continuing it....

EDIT: There is a wait feature available in FLTK (i should have assumed that), but at this time I haven't tried anything with it, and not sure how it ties in with Lua scripting.

Icons:
This will display png and gif images in $HOME/.xtdesktop.
There seems to be an issue either with xpm images or with a very large number of images...i haven't discovered which it is yet.
Code Sample
ww=400 --window width
wh=350 --window height

dir=os.getenv("HOME").."/.xtdesktop/" -- ending slash is vital
images = murgaLua.readDirectory(dir)
fltk.fl_register_images()

function get_image_data(self)
for i=0,table.getn(images) do
if self:tooltip() == dir..images[i] then -- find the right image
display:image(icon[i]:copy(128,128)) --show it bigger!
display_label:label(images[i].."\n"..icon[i]:w().."x"..icon[i]:h()) -- show height and width
display:redraw()
break
end
end
end

w=fltk:Fl_Window(ww,wh,"murgaLua Gif/Png Icon Demo")

display=fltk:Fl_Box(120,30,256,256) -- big image
display_label=fltk:Fl_Box(120,290,256,40)
display_label:label("nothing")
display:box(fltk.FL_THIN_DOWN_BOX)

scroll=fltk:Fl_Scroll(5,5,90,330)
pack=fltk:Fl_Pack(5,5,68,300)
pack:spacing(2)
icon={}; butt={}
row=1;col=1
-- since the images table contains more than just images (*.lnk gets in too)
-- i'm building the icons from specific items in the table.
for i=0,table.getn(images) do
if string.find (images[i],".png",-4,plain) or string.find (images[i],".gif",-4,plain)
then
icon[i]=Fl_Shared_Image.get(dir..images[i])
butt[i]=fltk:Fl_Button(0,0,64,64)
butt[i]:align(fltk.FL_ALIGN_CLIP)
butt[i]:image(icon[i])
butt[i]:tooltip(dir..images[i])
butt[i]:callback(get_image_data)
end
end
w:show()
Fl:run()


I don't know if "readDirectory" is something that Murga added, but i didn't see anything about it in the lua or fltk docs.
EDIT: I see now that it is an xml function added to complement Lua.

Next Page...
original here.