Programming and Scripting :: murgaLua tests



256 pretty colors
mouse over them to get the color number
Code Sample
ww = 380
wh = 360
w = fltk:Fl_Window(ww,wh,"FLTK Colors, murgaLua")

colors = {}
row=20;switch=15;bw=20;left=10
for i = 0,255 do
colors[i]=fltk:Fl_Box(left+bw,row,bw,bw)
colors[i]:color(i)
colors[i]:box(fltk.FL_THIN_UP_BOX)
colors[i]:tooltip("color "..i)
left=left+bw
if i == switch then
switch = switch+16
row = row+bw
left = 10
end
end

w:show()
Fl:run()


Here's the same thing written for Lua-FLTK.  It looks like they're almost identical, except for colors 15-31
Code Sample
ww = 380
wh = 360
w = Window{ww,wh,"FLTK Colors, lua-FLTK"}

colors = {}
row=20;switch=15;bw=20;left=10
for i = 0,255 do
colors[i]=Box{left+bw,row,bw,bw;color=i,box=Boxtype.thin_up}
left=left+bw
if i == switch then
switch = switch+16
row = row+bw
left = 10
end
end

w:end_layout()
w:show()

Tabs:
Code Sample
ww=320 --window width
wh=240 --window height

function show2()
if on2:takesevents() == 1 then
on2:hide()
tab2:deactivate()
on1:label("unlock tab2")
else
tab2:activate()
on2:show()
on1:label("lock tab2")
end
end

w=fltk:Fl_Window(ww,wh,"murgaLua Tabs Demo")
tabs=fltk:Fl_Tabs(10,20,ww-20,wh-25,"Tabs")

tab1 = fltk:Fl_Group(10,40,ww-20,wh-45,"Tab1")
on1=fltk:Fl_Button(20,60,100,30,"unlock tab 2");on1:callback(show2)
fltk:Fl_End() --end of tab1 group
tab2 = fltk:Fl_Group(10,40,ww-20,wh-45,"Tab2")
on2=fltk:Fl_Box(20,60,ww-40,wh-85);on2:box(fltk.FL_THIN_DOWN_BOX)
on2:label("Congratulations!\nYou pressed a button!")
fltk:Fl_End()

tab2:deactivate()
on2:hide()
w:show()
Fl:run()

There's a "wizard" widget that's similar to this, but you are restricted to linear navigation (page 1 <-> page 2 <-> page 3, no page 1 <-> page 4 <-> page 2). I can't understand how to build the navigation, though, so that's in the trash for now.

More Colors....

I haven't figured out how to do anything useful with the rgb color picker.  It seems to return unuseable values, even when using the format function (as could be done with Lua-FLTK).  However, there is an interactive color map which works great for the 256 default colors.  I didn't notice this in Lua-FLTK, but maybe it's there...

Code Sample
ww=220 --window width
wh=200 --window height

function cmap()
-- open the color map with the 'pop' button's color selected
newcolor=fltk.fl_show_colormap(pop:color())
-- set button's color and label from the new color
pop:color(newcolor);pop:label("FLTK Color "..newcolor)
end

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

pop=fltk:Fl_Button(20,20,ww-40,ww-80,"push for colors")
pop:align(fltk.FL_ALIGN_BOTTOM)
pop:callback(cmap)

w:show()
Fl:run()


I still haven't yet seen any way to return hex (html) colors or rgb.txt-type colors from murgaLua.

I think I know what was up with the color chooser.  FLTK seems to have two ways to create some of its choosers.  One way is to do Fl_Color_Chooser as a FLTK class, and the other is to do fl_color_chooser as a function.  I'm not sure why there is a distinction, but i got it figured out anyway.

so, next....

ucitool.lua, second generation

This is a work in progress.  I haven't fully worked out how I'm going to layout a uci builder or what features it will have, so that hasn't been added yet.  So far it will umount uci packages like the original version does, plus it will list and mount extensions from a specified directory...specified either from a ~/.ucitool file or by browsing to a directory.
Code Sample
-- i made a few bad choices for variable names in this script =o)

ww=400; wh=300;      -- window size
tx=2; ty=27;         -- tab position
tw=ww-4; th=wh-ty-2; -- tab size
bw=120; bh=30;       -- button size
tempfile="/tmp/"..os.getenv("USER")..".uci2ool"
tooltips=1 -- 0 disables tooltips
--ucidir="/mnt/hda3/home/dsl/mydsl/optional" -- path to a dir containing ucis
ucidir="/cdrom/mydsl/optional"

-- if you have a $HOME/.ucitool file with the variables
-- above, your values will override the defaults
rcfile=os.getenv("HOME").."/.ucitool";
if io.open(rcfile,"r") then dofile(rcfile) end;


function build_uci_lists()
-- runs at startup, each time you add or remove a uci, and when "refresh" is pressed.
-- list mounted uci files from mtab
os.execute("grep \"^.*.uci \" /etc/mtab | awk '{print $1}' >"..tempfile)
ufiles:clear()
for line in io.lines(tempfile) do ufiles:add(line) end -- print output to mntout
ufiles:redraw()
-- build list of available unmounted uci files.
-- I don't know why they're not listed alphabetically
if murgaLua.isDirectory(ucidir) then
input=io.open(tempfile,"r")
mounted=input:read("*a")
io.close(input)
allfiles=murgaLua.readDirectory(ucidir)
mfiles:clear()
for i=0,table.getn(allfiles) do
if string.find(allfiles[i],".uci",-4,plain) then
-- compare available files to mounted files
if not string.find(mounted,allfiles[i].."\n") then mfiles:add(ucidir.."/"..allfiles[i]) end
end
end
mfiles:redraw()
os.remove(tempfile)
end
end


function browse_cb()
new_ucidir=fltk.fl_dir_chooser("choose a uci directory",ucidir,"*",0)
if murgaLua.isDirectory(new_ucidir) then
-- return a new ucidir, without a trailing slash
ucidir=string.gsub(new_ucidir,"%/$","")
build_uci_lists() end
end

function mount_cb()
-- run mydsl-load on selected files
for i = 1,mfiles:nitems() do
if mfiles:checked(i)==1 then
os.execute("mydsl-load "..mfiles:text(i).." &>"..tempfile)
for line in io.lines(tempfile) do mntout:add(line) end -- print output to mntout
mntout:bottomline(mntout:size())
end
end
build_uci_lists()
end

function umount_cb()
-- run mydsl-load on selected files
-- this is nearly identical to mount_cb, so i should try to merge them
for i = 1,ufiles:nitems() do
if ufiles:checked(i)==1 then
os.execute("mydsl-load "..ufiles:text(i).." &>"..tempfile)
for line in io.lines(tempfile) do umntout:add(line) end -- print output to umntout
umntout:bottomline(umntout:size())
end
end
build_uci_lists()
end

w=fltk:Fl_Double_Window(ww,wh,"UCI Tool");

tabs=fltk:Fl_Tabs(tx,2,ww-4,wh-4);
tabs:selection_color(51);
tabs:color(1);

tab1 = fltk:Fl_Group(tx,ty,tw,th,"  create  ");
frame=fltk:Fl_Box(tx+2,ty+2,tw-4,th-4); -- just a frame, but it hides a strip of selection color
frame:box(fltk.FL_THIN_DOWN_BOX);
fltk:Fl_End();

tab2 = fltk:Fl_Group(tx,ty,tw,th,"  mount  ");
-- checklist of unmounted ucis
mfiles=fltk:Fl_Check_Browser(tx+2,ty+2,tw-4,th-bh-80); mfiles:box(fltk.FL_THIN_DOWN_BOX);
-- output from mydsl-load
mntout=fltk:Fl_Browser(tx+2,ty+th-bh-76,tw-4,76); mntout:format_char(0);
mount=fltk:Fl_Button(tx,wh-bh-2,bw,bh,"mount"); mount:box(fltk.FL_THIN_UP_BOX);
mount:callback(mount_cb);
browse=fltk:Fl_Button(ww-bw-2,wh-bh-2,bw,bh,"browse"); browse:box(fltk.FL_THIN_UP_BOX);
browse:callback(browse_cb);
fltk:Fl_End();

tab3 = fltk:Fl_Group(tx,ty,tw,th,"  umount  ");
-- checklist of mounted ucis
ufiles=fltk:Fl_Check_Browser(tx+2,ty+2,tw-4,th-bh-80); ufiles:box(fltk.FL_THIN_DOWN_BOX);
-- output from mydsl-load
umntout=fltk:Fl_Browser(tx+2,ty+th-bh-76,tw-4,76);
umount=fltk:Fl_Button(tx,wh-bh-2,bw,bh,"umount"); umount:box(fltk.FL_THIN_UP_BOX);
umount:callback(umount_cb);
refresh=fltk:Fl_Button(ww-bw-2,wh-bh-2,bw,bh,"refresh"); refresh:box(fltk.FL_THIN_UP_BOX);
refresh:callback(build_uci_lists);
fltk:Fl_End();

tab4 = fltk:Fl_Group(tx,ty,tw,th,"  about  ");
about=fltk:Fl_Box(tx+2,ty+2,tw-4,th-4);
about:box(fltk.FL_THIN_DOWN_BOX);
about:label("UCITOOL 2: THE REMOUNTENING\n\n\
A MurgaLua tool for creating and managing\
*.uci packages in Damn Small Linux.\
INCOMPLETE TEST RELEASE!\
\n\n(c)2006 mikshaw [mrblog|yahoo|com]");
fltk:Fl_End();

if tooltips ~= 0 then
mfiles:tooltip("If you have any *.uci files in \n"..ucidir.."\nthey will be listed here");
ufiles:tooltip("These are the currently mounted uci packages");
mount:tooltip("mount all checked files");
mntout:tooltip("output from the mydsl-load command");
umntout:tooltip("output from the mydsl-load command");
browse:tooltip("select a new directory");
umount:tooltip("umount all checked files");
refresh:tooltip("refresh the list of mounted files");
end

build_uci_lists();

w:show();
Fl:run();

Playing a little with themes....

FLTK currently includes one 'scheme' in addition to the default, which is called "plastic". This scheme gives the widgets a more glossy look, similar to some themes found in other widget sets.  It's not really an amazing thing, but it provides an option if flat colors are not your taste.
You can access this scheme in MurgaLua with Fl:scheme("plastic"), or with a combination of Fl:scheme(nil) and the environment variable FLTK_SCHEME="plastic".  I assume this second method would allow the developer to include that option in his application without forcing the scheme on the user.

Color schemes can be created fairly quickly by setting Fl:background, Fl:background2, and Fl:foreground. The first is the typical background color, and the second is the background of text fields and other widgets that are typically white. The foreground is the text. The syntax is Fl:background(red_value,green_value,blue_value), but for now I prefer to combine it with Fl:getcolor so i can simply use the numbered colors displayed in the colormap:

Fl:background(Fl:get_color(16,r,g,b))
Fl:background2(Fl:get_color(17,r,g,b))
Fl:foreground(Fl:get_color(18,r,g,b))

Setting the default font can be done with Fl:set_font(fltk.FL_HELVETICA,"fontname") or Fl:set_font(0,"fontname"), where fontname is an alias or full X font name.  The same can be done with other default fonts:
Quote (documentation @ fltk.org)
The following constants define the standard FLTK fonts:

   * FL_HELVETICA - Helvetica (or Arial) normal (0).
   * FL_HELVETICA_BOLD - Helvetica (or Arial) bold.
   * FL_HELVETICA_ITALIC - Helvetica (or Arial) oblique.
   * FL_HELVETICA_BOLD_ITALIC - Helvetica (or Arial) bold-oblique.
   * FL_COURIER - Courier normal.
   * FL_COURIER_BOLD - Courier bold.
   * FL_COURIER_ITALIC - Courier italic.
   * FL_COURIER_BOLD_ITALIC - Courier bold-italic.
   * FL_TIMES - Times roman.
   * FL_TIMES_BOLD - Times bold.
   * FL_TIMES_ITALIC - Times italic.
   * FL_TIMES_BOLD_ITALIC - Times bold-italic.
   * FL_SYMBOL - Standard symbol font.
   * FL_SCREEN - Default monospaced screen font.
   * FL_SCREEN_BOLD - Default monospaced bold screen font.
   * FL_ZAPF_DINGBATS - Zapf-dingbats font.

IMPORTANT: This works only partially with Xft. The font names are different (i have no idea where the names come from, but they are not standard X names or aliases), and some fonts will crash murgaLua.  Fortunately DSL has a Xft-disabled version, so these probelms will occur only if you are coding for another system.

Next Page...
original here.