]> git.decadent.org.uk Git - ion3.git/blob - ioncore/ioncore_misc.lua
4637279d798fa7c108b32a97a6c4ae58927b3f53
[ion3.git] / ioncore / ioncore_misc.lua
1 --
2 -- ion/share/ioncore_misc.lua
3 -- 
4 -- Copyright (c) Tuomo Valkonen 2004-2007.
5 --
6 -- Ion is free software; you can redistribute it and/or modify it under
7 -- the terms of the GNU Lesser General Public License as published by
8 -- the Free Software Foundation; either version 2.1 of the License, or
9 -- (at your option) any later version.
10 --
11
12 local group_tmpl = { type="WGroupWS" }
13
14 local default_tmpl = { switchto=true }
15
16 local empty = { type="WGroupWS", managed={} }
17
18 local layouts={
19     empty = empty,
20     default = empty,
21 }
22
23 --DOC 
24 -- Define a new workspace layout with name \var{name}, and
25 -- attach/creation parameters given in \var{tab}. The layout
26 -- "empty" may not be defined.
27 function ioncore.deflayout(name, tab)
28     assert(layout ~= "empty")
29     
30     if name=="default" and not tab then
31         layouts[name] = empty
32     else
33         layouts[name] = table.join(tab, group_tmpl)
34     end
35 end
36
37 --DOC
38 -- Get named layout (or all of the latter parameter is set,
39 -- but this is for internal use only).
40 function ioncore.getlayout(name, all)
41     if all then
42         return layouts
43     else
44         return layouts[name]
45     end
46 end
47
48 ioncore.set{_get_layout=ioncore.getlayout}
49
50 --DOC
51 -- Create new workspace on screen \var{scr}. The table \var{tmpl}
52 -- may be used to override parts of the layout named with \code{layout}.
53 -- If no \var{layout} is given, "default" is used.
54 function ioncore.create_ws(scr, tmpl, layout)
55     local lo=ioncore.getlayout(layout or "default")
56     
57     assert(lo, TR("Unknown layout"))
58     
59     return scr:attach_new(table.join(tmpl or default_tmpl, lo))
60 end
61
62
63 --DOC
64 -- Find an object with type name \var{t} managing \var{obj} or one of
65 -- its managers.
66 function ioncore.find_manager(obj, t)
67     while obj~=nil do
68         if obj_is(obj, t) then
69             return obj
70         end
71         obj=obj:manager()
72     end
73 end
74
75
76 --DOC
77 -- gettext+string.format
78 function ioncore.TR(s, ...)
79     return string.format(ioncore.gettext(s), unpack(arg))
80 end
81
82
83 --DOC
84 -- Attach tagged regions to \var{reg}. The method of attach
85 -- depends on the types of attached regions and whether \var{reg} 
86 -- implements \code{attach_framed} and \code{attach}. If \var{param}
87 -- is not set, the default of \verb!{switchto=true}! is used.
88 function ioncore.tagged_attach(reg, param)
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             return false
99         end
100     end
101     return true
102 end
103