]> git.decadent.org.uk Git - ion3.git/blob - ioncore/ioncore_misc.lua
3451d7be4329411e7517b85c089465202e2f81e7
[ion3.git] / ioncore / ioncore_misc.lua
1 --
2 -- ion/share/ioncore_misc.lua
3 -- 
4 -- Copyright (c) Tuomo Valkonen 2004-2008.
5 --
6 -- See the included file LICENSE for details.
7 --
8
9 local group_tmpl = { type="WGroupWS" }
10
11 local default_tmpl = { switchto=true }
12
13 local empty = { type="WGroupWS", managed={} }
14
15 local layouts={
16     empty = empty,
17     default = empty,
18 }
19
20 --DOC 
21 -- Define a new workspace layout with name \var{name}, and
22 -- attach/creation parameters given in \var{tab}. The layout
23 -- "empty" may not be defined.
24 function ioncore.deflayout(name, tab)
25     assert(name ~= "empty")
26     
27     if name=="default" and not tab then
28         layouts[name] = empty
29     else
30         layouts[name] = table.join(tab, group_tmpl)
31     end
32 end
33
34 --DOC
35 -- Get named layout (or all of the latter parameter is set,
36 -- but this is for internal use only).
37 function ioncore.getlayout(name, all)
38     if all then
39         return layouts
40     else
41         return layouts[name]
42     end
43 end
44
45 ioncore.set{_get_layout=ioncore.getlayout}
46
47 --DOC
48 -- Create new workspace on screen \var{scr}. The table \var{tmpl}
49 -- may be used to override parts of the layout named with \code{layout}.
50 -- If no \var{layout} is given, "default" is used.
51 function ioncore.create_ws(scr, tmpl, layout)
52     local lo=ioncore.getlayout(layout or "default")
53     
54     assert(lo, TR("Unknown layout"))
55     
56     return scr:attach_new(table.join(tmpl or default_tmpl, lo))
57 end
58
59
60 --DOC
61 -- Find an object with type name \var{t} managing \var{obj} or one of
62 -- its managers.
63 function ioncore.find_manager(obj, t)
64     while obj~=nil do
65         if obj_is(obj, t) then
66             return obj
67         end
68         obj=obj:manager()
69     end
70 end
71
72
73 --DOC
74 -- gettext+string.format
75 function ioncore.TR(s, ...)
76     return string.format(ioncore.gettext(s), ...)
77 end
78
79
80 --DOC
81 -- Attach tagged regions to \var{reg}. The method of attach
82 -- depends on the types of attached regions and whether \var{reg} 
83 -- implements \code{attach_framed} and \code{attach}. If \var{param}
84 -- is not set, the default of \verb!{switchto=true}! is used.
85 -- The function returns \code{true} if all tagged regions were
86 -- succesfully attached, and \code{false} otherwisse.
87 function ioncore.tagged_attach(reg, param)
88     local errors=false
89     if not param then
90         param={switchto=true}
91     end
92     local tagged=function() return ioncore.tagged_first(true) end
93     for r in tagged do
94         local fn=((not obj_is(r, "WWindow") and reg.attach_framed) 
95                   or reg.attach)
96         
97         if not (fn and fn(reg, r, param)) then
98             errors=true
99         end
100     end
101     return not errors
102 end
103