Code Sample |
#!/bin/murgaLua -- XPM icon viewer -- mikshaw 2007 dir="/usr/share/dfm/icons" editor="xpaint" --xpm_size=32 broken_xpm_data=[[ /* XPM */ static char *broken_image[] = { /* columns rows colors chars-per-pixel */ "14 16 16 1", " c black", ". c #800000", "X c #008000", "o c #808000", "O c navy", "+ c #800080", "@ c #008080", "# c #C0C0C0", "$ c #808080", "% c red", "& c green", "* c None", "= c blue", "- c magenta", "; c cyan", ": c gray100", /* pixels */ " $***", " :::::::::$$**", " :########$:$*", " :###XX###$::$", " :##X&X ## ", " :##XXX ####: ", " :### #####: ", " :######=== :*", " :#%####=;= :*", " :#-%###==O***", " :#--%## **: ", " :#---%#***#: ", " :# *****#: ", " :##*****###: ", " ::*****::::: ", " ****** " }; ]] xpm_tempfile=os.tmpname() xpm_write=io.open(xpm_tempfile,"w") if xpm_write then xpm_write:write(broken_xpm_data) xpm_write:close() broken_xpm=fltk:Fl_XPM_Image(xpm_tempfile) os.remove(xpm_tempfile) end myname=fltk.fl_filename_name(arg[0])..": " edvar=os.getenv("XPM_EDITOR") if edvar then editor=edvar end function find_stupid_space(infile) local data=io.open(infile) if data then local found=0 for line in data:lines() do if string.find(line,"^%s") then found=1; break end end data:close() return found end end function get_image_data(image) local old_data=display:image() if old_data then old_data:uncache() end local image_data=fltk:Fl_XPM_Image(image) if image_data and find_stupid_space(image)==0 then display:image(image_data:copy(128,128)) --show it bigger! else display:image(broken_xpm) end current_file=image -- full path filename=fltk.fl_filename_name(image) -- filename only display:label(filename.."\n"..image_data:w().."x"..image_data:h().." | "..lfs.attributes(current_file).size.." bytes") display:redraw() end function menu_cb() local v=i_menu:value() if v==1 then menu_open_newfile() elseif v==2 and current_file then get_image_data(current_file) elseif v==3 then os.exit(0) elseif v>=4 and current_file then os.execute(i_menu:text().." "..current_file.." &") end end function menu_open_newfile() local filename=fltk.fl_file_chooser("XPM file...","*.xpm",dir) if filename then get_image_data(filename) end end function dnd_check() if Fl:event() == fltk.FL_DND_RELEASE then Fl:paste(w) Fl:wait() local dropped_file=Fl:event_text() if dropped_file then cut_stupid_space=string.gsub(dropped_file,"^%s*","") local new_file=string.gsub(cut_stupid_space,"^~",os.getenv("HOME")) open_file(new_file) end end Fl:wait() timer:doWait(0.05) end function open_file(dfile) if lfs.attributes(dfile).mode == "file" then local filename=io.open(dfile) if filename then local data=filename:read("*l") if string.find(data,"/%*%s*XPM%s*%*/") then filename:close() get_image_data(dfile) end else print(myname.."can't open "..dfile) end else print(myname..dfile..": not a regular file") end end ww=200; wh=200; mh=30 images={}; icon={}; butt={} w=fltk:Fl_Window(ww,wh,"XPM Viewer") file_items={"&Open","&Reload","&Quit"} edit_items={"xpaint","sxpm","beaver","aterm -e vi"} i_menu=fltk:Fl_Menu_Bar(0,0,ww,mh) i_menu:callback(menu_cb) i_menu:selection_color(fltk.FL_WHITE) for i,v in ipairs(file_items) do i_menu:add("&File/"..v) end for i,v in ipairs(edit_items) do i_menu:add("&Edit/"..v) end timer=murgaLua.createFltkTimer() display=fltk:Fl_Box(0,mh,ww,ww-mh) -- big image if arg[1] then open_file(arg[1]) end timer:callback(dnd_check) timer:do_callback() w:show() Fl:run() |