
----------------------------------------------------------------------------------
--# A simple file browser/runner;
----------------------------------------------------------------------------------

include GtkEngine.e
include std/sort.e
include std/datetime.e -- using this requires us to namespace gtk: functions

chdir(login)

constant win = create(GtkWindow,"size=750x550,border=10,$destroy=Quit")
  gtk:set(win,"title","EuGTK Browser - " & login)
  
constant  panel = create(GtkBox,"orientation=VERTICAL") 
  gtk:add(win,panel)

constant  lbl = create(GtkLabel,
 "markup='<small>Use <i>alt-f</i> to search, <i>&lt;enter&gt;</i> or double-click to run!</small>'")
  gtk:add(panel,lbl)

constant  store = create(GtkListStore,{gSTR,gSTR,gINT,gSTR,gINT,gSTR})
 
constant  tv = create(GtkTreeView,{
  {"model",store},    
  {"reorderable",TRUE},
  {"headers clickable",TRUE},
  {"set grid lines",GTK_TREE_VIEW_GRID_LINES_BOTH},
  {"rules hint",TRUE}})

constant cols = { -- below is a new way to define cols and renderers in 1 line;
 create(GtkColumn,"title=Name,type=text,text=1,sort_column_id=2"), --[1]
 create(GtkColumn,"title=Size,type=text,text=3,sort_column_id=3"),
 create(GtkColumn,"title=Date,type=text,text=4,sort_column_id=5"), --[2]
 create(GtkColumn,"title=Description,type=text,markup=6,sort_column_id=6")
} 
--[1] filename in col[1] is sorted by number in col[2]
--[2] date shown in col[4] is sorted on unix date in col[5]

gtk:add(tv,cols)
  
constant  selection = gtk:get(tv,"selection")
 set(selection,"mode",GTK_SELECTION_MULTIPLE)
 
constant  scroller = create(GtkScrolledWindow)
  pack(panel,scroller,TRUE,TRUE,10)
  gtk:add(scroller,tv)

constant  box = create(GtkButtonBox)
 pack_end(panel,box)
 
constant  btn = create(GtkButton,"gtk-quit","Quit")
 gtk:add(box,btn)
 
-- load file list;
object files = dir("*.ex") 

-- convert dates to usable format;
files = apply(files,routine_id("convert_date"))
files = apply(files,routine_id("convert_filename"))

-- load files into listview;
gtk:set(store,"data",files)

-- set up handlers;
gtk:set(tv,"rules hint",TRUE)
gtk:set(tv,"columns autosize")
gtk:set(tv,"search column",6)
gtk:set(tv,"search equal func",_("search_func"))
connect(tv,"row-activated","show_choice")
gtk:set(store,"sort column id",2,GTK_SORT_ASCENDING) -- default startup sort;

-- run the program!
show_all(win)
main()

------------------------------------------------
function convert_filename(object f, object junk) -- allow 'natural' sort order;
------------------------------------------------
object tmp = io:read_lines(f[1])
for i = 1 to length(tmp) do
 if match("--#",tmp[i]) =1 then 
  f[6] = tmp[i][5..$] 
 end if
 end for
f[1] = filebase(f[1]) -- drop extension, build index of #s;
object x = filter(f[1],"out",{'0','9'},"[]")
object n = filter(f[1],"in",{'0','9'},"[]")
f[2] = x & pad_head(n,10,'0')
return f
end function

-----------------------------------------
function convert_date(object a, object b) -- convert dates to unix for sorting;
-----------------------------------------
object dt = datetime:new(a[4],a[5],a[6])   -- convert eu dir() date to datetime;
 a[5] = to_unix(dt)             -- store as UNIX timestamp for sorting purposes;
 a[4] = datetime:format(dt,"%b %d %Y") -- store human-readable date for display;
 a[6] = "?"
return a
end function

-------------------------------------------------------------------------------
function search_func(atom mdl, integer col, object key, atom iter, object data)
-------------------------------------------------------------------------------
key = lower(peek_string(key)) -- key is passed as pointer to string
data = get(mdl,"value",iter,6) -- value from col 6 of liststore (description)
return not match(key,lower(data)) -- find word anywhere in description, 0 = found
end function

-----------------------
global function show_choice()
-----------------------
integer row = gtk:get(selection,"selected row")
object f = gtk:get(store,"col data",row,1)
system("eui " & f,0) -- run it;
return 1
end function
