Programming and Scripting :: XPM Icon Viewer



I think something like that would probably start by removing most of what is in here, since much of the code is made to handle displaying the directory contents.

Unless you want to have it do what it does now but add stdin as a source for the initially-displayed image, I think  the tool you describe might be a very small and very fast script in comparison.  In fact, it might simply be a wrapper for existing DSL applications...sxpm for display (would remove the troubles of XPM files that murgaLua cannot read), sxpm/xpaint/beaver/vi/nano as various editor choices, and the fact that it would accept a file as a parameter means a file manager or drag-drop can be used instead of fl_dir_chooser.  An application like that could basically just be a menu.

Oh hey...this is easy now =o)
It's not complete, and the refresh is broken, but it only took about ten minutes to turn the browser into a smaller, faster drag-drop viewer.

I'll post later today after I do the refresh and run some tests.

I wasn't thinking of cutting your application way down. What I had in mind, was drag a folder to your app and it opens in that folder, drag an xpm to it and it opens the folder containing this xpm with that xpm displayed. And finally double click an xpm and it works the same as dnd of it. I would like DSLv4 to support both operating modes whenever possible. If you weren't going to do it, I was. But I would rather let you complete your project. Glad to hear that you are looking further into this and thus supporting DSL v4 DND operating mode. I will wait to see what you have. This will be a nice addition to DSL.
I see where you're going. Well, I had already started a different script using basically just the image display from the old one as a base. It currently doesn't support directories at all, and in fact will give a warning message if you drag a folder onto it. It will allow you to drag an XPM file either to the app icon or onto the interface itself (that bit probably needs a decrease in the loop time to detect quick drags), or to browse for an XPM file. The double-click I assume would be controlled by the file manager.

I'll probably look into mashing the two scripts together in the future to see what I can make of them. I also will probably remove sxpm from the edit menu, since it's not an interactive app, and maybe use it elsewhere.

This is my first attempt at any sort of drag and drop, so it will need much testing and debugging.

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()

I wasn't talking about murgaLua drag-n-drop but dfm drag-n-drop as is easily implemented by processing the command line arg(s) no matter what programming language is used. You seem to have both. Not sure how the Lua DND is supposed to work. It didn't for me.

But dragging an .xpm onto the Lua logo icon of your apps works as expected. Then it is a simple matter to associate your program name with *.xpm in .dfmext and you have the double click support.

Next you need to make or find a nifty icon for your app. We don't want to leave it a boring Lua logo.

Next Page...
original here.