lib/wibox: Make signals from drawables available
[awesome.git] / lib / wibox / init.lua.in
blob7df9887fbdfffeb5d1e0ad3da07093a483af1c40
1 ---------------------------------------------------------------------------
2 -- @author Uli Schlachter
3 -- @copyright 2010 Uli Schlachter
4 -- @release @AWESOME_VERSION@
5 ---------------------------------------------------------------------------
7 local capi = {
8 drawin = drawin,
9 root = root,
10 awesome = awesome
12 local setmetatable = setmetatable
13 local pairs = pairs
14 local type = type
15 local table = table
16 local string_format = string.format
17 local color = require("gears.color")
18 local object = require("gears.object")
19 local sort = require("gears.sort")
20 local beautiful = require("beautiful")
21 local surface = require("gears.surface")
22 local cairo = require("lgi").cairo
24 --- This provides widget box windows. Every wibox can also be used as if it were
25 -- a drawin. All drawin functions and properties are also available on wiboxes!
26 -- wibox
27 local wibox = { mt = {} }
28 wibox.layout = require("wibox.layout")
29 wibox.widget = require("wibox.widget")
30 wibox.drawable = require("wibox.drawable")
32 --- Set the widget that the wibox displays
33 function wibox.set_widget(_wibox, widget)
34 _wibox._drawable:set_widget(widget)
35 end
37 --- Set the background of the wibox
38 -- @param wibox The wibox to use
39 -- @param c The background to use. This must either be a cairo pattern object,
40 -- nil or a string that gears.color() understands.
41 function wibox.set_bg(_wibox, c)
42 _wibox._drawable:set_bg(c)
43 end
45 --- Set the foreground of the wibox
46 -- @param wibox The wibox to use
47 -- @param c The foreground to use. This must either be a cairo pattern object,
48 -- nil or a string that gears.color() understands.
49 function wibox.set_fg(_wibox, c)
50 _wibox._drawable:set_fg(c)
51 end
53 --- Helper function to make wibox:buttons() work as expected
54 function wibox.buttons(box, ...)
55 return box.drawin:buttons(...)
56 end
58 --- Helper function to make wibox:struts() work as expected
59 function wibox.struts(box, ...)
60 return box.drawin:struts(...)
61 end
63 --- Helper function to make wibox:geometry() work as expected
64 function wibox.geometry(box, ...)
65 return box.drawin:geometry(...)
66 end
68 local function setup_signals(_wibox)
69 local w = _wibox.drawin
71 local function clone_signal(name)
72 _wibox:add_signal(name)
73 -- When "name" is emitted on wibox.drawin, also emit it on wibox
74 w:connect_signal(name, function(_, ...)
75 _wibox:emit_signal(name, ...)
76 end)
77 end
78 clone_signal("property::border_color")
79 clone_signal("property::border_width")
80 clone_signal("property::buttons")
81 clone_signal("property::cursor")
82 clone_signal("property::height")
83 clone_signal("property::ontop")
84 clone_signal("property::opacity")
85 clone_signal("property::struts")
86 clone_signal("property::visible")
87 clone_signal("property::widgets")
88 clone_signal("property::width")
89 clone_signal("property::x")
90 clone_signal("property::y")
92 local d = _wibox._drawable
93 local function clone_signal(name)
94 _wibox:add_signal(name)
95 -- When "name" is emitted on wibox.drawin, also emit it on wibox
96 d:connect_signal(name, function(_, ...)
97 _wibox:emit_signal(name, ...)
98 end)
99 end
100 clone_signal("button::press")
101 clone_signal("button::release")
102 clone_signal("mouse::enter")
103 clone_signal("mouse::leave")
104 clone_signal("mouse::move")
105 clone_signal("property::surface")
108 local function new(args)
109 local ret = object()
110 local w = capi.drawin(args)
111 ret.drawin = w
112 ret._drawable = wibox.drawable(w.drawable, ret)
114 for k, v in pairs(wibox) do
115 if type(v) == "function" then
116 ret[k] = v
120 setup_signals(ret)
121 ret.draw = ret._drawable.draw
122 ret.widget_at = function(_, widget, x, y, width, height)
123 return ret._drawable:widget_at(widget, x, y, width, height)
126 -- Set the default background
127 ret:set_bg(args.bg or beautiful.bg_normal)
128 ret:set_fg(args.fg or beautiful.fg_normal)
130 -- Make sure the wibox is drawn at least once
131 ret.draw()
133 -- Redirect all non-existing indexes to the "real" drawin
134 setmetatable(ret, {
135 __index = w,
136 __newindex = w
139 return ret
142 --- Redraw a wibox. You should never have to call this explicitely because it is
143 -- automatically called when needed.
144 -- @param wibox
145 -- @name draw
146 -- @class function
148 --- Widget box object.
149 -- Every wibox "inherits" from a drawin and you can use all of drawin's
150 -- functions directly on this as well. When creating a wibox, you can specify a
151 -- "fg" and a "bg" color as keys in the table that is passed to the constructor.
152 -- All other arguments will be passed to drawin's constructor.
153 -- @class table
154 -- @name drawin
156 function wibox.mt:__call(...)
157 return new(...)
160 return setmetatable(wibox, wibox.mt)
162 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80