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")
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
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
)
28 for k
, v
in pairs(widgets
) do
33 w
, h
= width
, height
- pos
34 if k
~= #widgets
or not fill_space
then
41 w
, h
= width
- pos
, height
42 if k
~= #widgets
or not fill_space
then
49 if (dir
== "y" and pos
> height
) or
50 (dir
~= "y" and pos
> width
) then
53 base
.draw_widget(wibox
, cr
, v
, x
, y
, w
, h
)
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
)
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
)
79 height
= height
- in_dir
82 width
= width
- in_dir
84 if max > used_max
then
87 used_in_dir
= used_in_dir
+ in_dir
89 if width
<= 0 or height
<= 0 then
91 used_in_dir
= orig_height
93 used_in_dir
= orig_width
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
)
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()
134 ret
.reset
= fixed
.reset
135 ret
.fill_space
= fixed
.fill_space
137 ret
.get_dir
= function () return dir
end
138 ret
._emit_updated
= function()
139 ret
:emit_signal("widget::updated")
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")
161 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80