Lots of random documentation fixes
[awesome.git] / lib / wibox / layout / fixed.lua.in
bloba42627cae23dc69c988fb7ce2f7394765a2754c9
1 ---------------------------------------------------------------------------
2 -- @author Uli Schlachter
3 -- @copyright 2010 Uli Schlachter
4 -- @release @AWESOME_VERSION@
5 ---------------------------------------------------------------------------
7 local base = require("wibox.layout.base")
8 local widget_base = require("wibox.widget.base")
9 local table = table
10 local pairs = pairs
12 -- wibox.layout.fixed
13 local fixed = {}
15 --- Draw a fixed layout. Each widget gets just the space it asks for.
16 -- @param dir "x" for a horizontal layout and "y" for vertical.
17 -- @param widgets The widgets to draw.
18 -- @param fill_space Use all the available space, giving all that is left to the
19 -- last widget
20 -- @param wibox The wibox that this widget is drawn to.
21 -- @param cr The cairo context to use.
22 -- @param width The available width.
23 -- @param height The available height.
24 -- @return The total space needed by the layout.
25 function fixed.draw_fixed(dir, widgets, fill_space, wibox, cr, width, height)
26 local pos = 0
28 for k, v in pairs(widgets) do
29 local x, y, w, h, _
30 local in_dir
31 if dir == "y" then
32 x, y = 0, pos
33 w, h = width, height - pos
34 if k ~= #widgets or not fill_space then
35 _, h = v:fit(w, h);
36 end
37 pos = pos + h
38 in_dir = h
39 else
40 x, y = pos, 0
41 w, h = width - pos, height
42 if k ~= #widgets or not fill_space then
43 w, _ = v:fit(w, h);
44 end
45 pos = pos + w
46 in_dir = w
47 end
49 if (dir == "y" and pos > height) or
50 (dir ~= "y" and pos > width) then
51 break
52 end
53 base.draw_widget(wibox, cr, v, x, y, w, h)
54 end
55 end
57 --- Add a widget to the given fixed layout
58 function fixed:add(widget)
59 widget_base.check_widget(widget)
60 table.insert(self.widgets, widget)
61 widget:connect_signal("widget::updated", self._emit_updated)
62 self._emit_updated()
63 end
65 --- Fit the fixed layout into the given space
66 -- @param dir "x" for a horizontal layout and "y" for vertical.
67 -- @param widgets The widgets to fit.
68 -- @param orig_width The available width.
69 -- @param orig_height The available height.
70 function fixed.fit_fixed(dir, widgets, orig_width, orig_height)
71 local width, height = orig_width, orig_height
72 local used_in_dir, used_max = 0, 0
74 for k, v in pairs(widgets) do
75 local w, h = v:fit(width, height)
76 local in_dir, max
77 if dir == "y" then
78 max, in_dir = w, h
79 height = height - in_dir
80 else
81 in_dir, max = w, h
82 width = width - in_dir
83 end
84 if max > used_max then
85 used_max = max
86 end
87 used_in_dir = used_in_dir + in_dir
89 if width <= 0 or height <= 0 then
90 if dir == "y" then
91 used_in_dir = orig_height
92 else
93 used_in_dir = orig_width
94 end
95 break
96 end
97 end
99 if dir == "y" then
100 return used_max, used_in_dir
102 return used_in_dir, used_max
105 --- Reset a fixed layout. This removes all widgets from the layout.
106 function fixed:reset()
107 for k, v in pairs(self.widgets) do
108 v:disconnect_signal("widget::updated", self._emit_updated)
110 self.widgets = {}
111 self:emit_signal("widget::updated")
114 --- Set the layout's fill_space property. If this property is true, the last
115 -- widget will get all the space that is left. If this is false, the last widget
116 -- won't be handled specially and there can be space left unused.
117 function fixed:fill_space(val)
118 self._fill_space = val
119 self:emit_signal("widget::updated")
122 local function get_layout(dir)
123 local function draw(layout, ...)
124 fixed.draw_fixed(dir, layout.widgets, layout._fill_space, ...)
126 local function fit(layout, ...)
127 return fixed.fit_fixed(dir, layout.widgets, ...)
130 local ret = widget_base.make_widget()
131 ret.draw = draw
132 ret.fit = fit
133 ret.add = fixed.add
134 ret.reset = fixed.reset
135 ret.fill_space = fixed.fill_space
136 ret.widgets = {}
137 ret.get_dir = function () return dir end
138 ret._emit_updated = function()
139 ret:emit_signal("widget::updated")
142 return ret
145 --- Returns a new horizontal fixed layout. Each widget will get as much space as it
146 -- asks for and each widget will be drawn next to its neighboring widget.
147 -- Widgets can be added via :add().
148 function fixed.horizontal()
149 return get_layout("x")
152 --- Returns a new vertical fixed layout. Each widget will get as much space as it
153 -- asks for and each widget will be drawn next to its neighboring widget.
154 -- Widgets can be added via :add().
155 function fixed.vertical()
156 return get_layout("y")
159 return fixed
161 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80