2013-05-11 21:02:42 +02:00
|
|
|
local setmetatable = setmetatable
|
2013-07-08 00:18:55 +02:00
|
|
|
local print,pairs = print,pairs
|
|
|
|
local unpack = unpack
|
|
|
|
local util = require( "awful.util" )
|
2013-05-27 02:49:51 +02:00
|
|
|
local checkbox = require( "radical.widgets.checkbox" )
|
2013-07-08 00:18:55 +02:00
|
|
|
local scroll = require( "radical.widgets.scroll" )
|
|
|
|
local filter = require( "radical.widgets.filter" )
|
|
|
|
local fkey = require( "radical.widgets.fkey" )
|
|
|
|
local beautiful = require("beautiful" )
|
|
|
|
local wibox = require( "wibox" )
|
|
|
|
local color = require( "gears.color" )
|
|
|
|
local cairo = require( "lgi" ).cairo
|
2013-05-11 21:02:42 +02:00
|
|
|
|
|
|
|
local module = {}
|
|
|
|
|
|
|
|
local function left(data)
|
|
|
|
if data._current_item._tmp_menu then
|
|
|
|
data = data._current_item._tmp_menu
|
|
|
|
data.items[1][1].selected = true
|
|
|
|
return true,data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function right(data)
|
2013-07-18 06:03:19 +02:00
|
|
|
if data.parent_geometry and data.parent_geometry.is_menu then
|
2013-05-11 21:02:42 +02:00
|
|
|
for k,v in ipairs(data.items) do
|
|
|
|
if v[1]._tmp_menu == data or v[1].sub_menu_m == data then
|
|
|
|
v[1].selected = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
data.visible = false
|
|
|
|
data = data.parent_geometry
|
|
|
|
return true,data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function up(data)
|
|
|
|
data.previous_item.selected = true
|
|
|
|
end
|
|
|
|
|
|
|
|
local function down(data)
|
|
|
|
data.next_item.selected = true
|
|
|
|
end
|
|
|
|
|
|
|
|
function module:setup_key_hooks(data)
|
|
|
|
data:add_key_hook({}, "Up" , "press", up )
|
|
|
|
data:add_key_hook({}, "&" , "press", up )
|
|
|
|
data:add_key_hook({}, "Down" , "press", down )
|
|
|
|
data:add_key_hook({}, "KP_Enter", "press", down )
|
|
|
|
data:add_key_hook({}, "Left" , "press", left )
|
|
|
|
data:add_key_hook({}, "\"" , "press", left )
|
|
|
|
data:add_key_hook({}, "Right" , "press", right )
|
|
|
|
data:add_key_hook({}, "#" , "press", right )
|
|
|
|
end
|
|
|
|
|
|
|
|
--Get preferred item geometry
|
|
|
|
local function item_fit(data,item,...)
|
2013-07-04 07:51:01 +02:00
|
|
|
local w, h = item._internal.cache_w or 1,item._internal.cache_h or 1
|
|
|
|
if item._internal.has_changed and data.visible then
|
|
|
|
w, h = item._private_data._fit(...)
|
|
|
|
item._internal.has_changed = false
|
2013-07-04 19:19:01 +02:00
|
|
|
item._internal.pix_cache = {} --Clear the pimap cache
|
2013-07-04 07:51:01 +02:00
|
|
|
end
|
2013-05-11 21:02:42 +02:00
|
|
|
return w, item._private_data.height or h
|
|
|
|
end
|
|
|
|
|
2013-07-05 07:32:14 +02:00
|
|
|
-- Like an overlay, but under
|
|
|
|
local function paint_underlay(data,item,cr,width,height)
|
|
|
|
cr:save()
|
|
|
|
cr:set_source_surface(item.underlay,width-item.underlay:get_width()-3)
|
|
|
|
cr:paint_with_alpha(data.underlay_alpha)
|
|
|
|
cr:restore()
|
|
|
|
end
|
|
|
|
|
2013-07-04 19:19:01 +02:00
|
|
|
-- As of July 2013, LGI is too slow to redraw big menus at ok speed
|
|
|
|
-- This do a pixmap cache to allow pre-rendering
|
|
|
|
local function cache_pixmap(item)
|
|
|
|
item._internal.pix_cache = {}
|
|
|
|
item.widget._draw = item.widget.draw
|
|
|
|
item.widget.draw = function(self,wibox, cr, width, height)
|
2013-07-08 00:18:55 +02:00
|
|
|
if not wibox.visible or item._hidden then return end
|
2013-07-04 19:19:01 +02:00
|
|
|
if item._internal.pix_cache[10*width+7*height+(item.selected and 8888 or 999)] then
|
|
|
|
cr:set_source_surface(item._internal.pix_cache[10*width+7*height+(item.selected and 8888 or 999)])
|
|
|
|
cr:paint()
|
|
|
|
else
|
|
|
|
local img5 = cairo.ImageSurface.create(cairo.Format.ARGB32, width, height)
|
|
|
|
local cr5 = cairo.Context(img5)
|
|
|
|
item.widget._draw(self,wibox, cr5, width, height)
|
|
|
|
cr:set_source_surface(img5)
|
|
|
|
cr:paint()
|
|
|
|
item._internal.pix_cache[10*width+7*height+(item.selected and 8888 or 999)] = img5
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-05-27 02:49:51 +02:00
|
|
|
function module:setup_item(data,item,args)
|
2013-07-04 19:19:01 +02:00
|
|
|
--Create the background
|
2013-05-27 02:49:51 +02:00
|
|
|
item.widget = wibox.widget.background()
|
2013-07-04 19:19:01 +02:00
|
|
|
cache_pixmap(item)
|
2014-01-04 06:49:20 +01:00
|
|
|
|
2013-05-27 02:49:51 +02:00
|
|
|
data.item_style(data,item,false,false)
|
|
|
|
item.widget:set_fg(item._private_data.fg)
|
2013-07-04 07:51:01 +02:00
|
|
|
item._internal.has_changed = true
|
2013-05-27 02:49:51 +02:00
|
|
|
|
|
|
|
--Event handling
|
|
|
|
item.widget:connect_signal("mouse::enter", function() item.selected = true end)
|
|
|
|
item.widget:connect_signal("mouse::leave", function() item.selected = false end)
|
2013-07-09 03:31:33 +02:00
|
|
|
item.widget:connect_signal("widget::updated", function() item._internal.has_changed = true end)
|
2013-05-27 02:49:51 +02:00
|
|
|
data._internal.layout:add(item)
|
|
|
|
|
|
|
|
--Be sure to always hide sub menus, even when data.visible is set manually
|
|
|
|
data:connect_signal("visible::changed",function(_,vis)
|
|
|
|
if data._tmp_menu and data.visible == false then
|
|
|
|
data._tmp_menu.visible = false
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
data:connect_signal("parent_geometry::changed",function(_,vis)
|
|
|
|
local fit_w,fit_h = data._internal.layout:fit()
|
|
|
|
data.height = fit_h
|
|
|
|
data.style(data)
|
|
|
|
end)
|
|
|
|
|
|
|
|
--Create the main item layout
|
|
|
|
local l,la,lr = wibox.layout.fixed.horizontal(),wibox.layout.align.horizontal(),wibox.layout.fixed.horizontal()
|
|
|
|
local m = wibox.layout.margin(la)
|
|
|
|
m:set_margins (0)
|
|
|
|
m:set_left ( data.item_style.margins.LEFT )
|
|
|
|
m:set_right ( data.item_style.margins.RIGHT )
|
|
|
|
m:set_top ( data.item_style.margins.TOP )
|
|
|
|
m:set_bottom( data.item_style.margins.BOTTOM )
|
|
|
|
local text_w = wibox.widget.textbox()
|
2013-07-05 07:32:14 +02:00
|
|
|
|
|
|
|
text_w.draw = function(self,w, cr, width, height)
|
|
|
|
if item.underlay then
|
|
|
|
paint_underlay(data,item,cr,width,height)
|
|
|
|
end
|
|
|
|
wibox.widget.textbox.draw(self,w, cr, width, height)
|
|
|
|
end
|
|
|
|
text_w.fit = function(self,width,height) return width,height end
|
|
|
|
|
2013-05-27 02:49:51 +02:00
|
|
|
item._private_data._fit = wibox.widget.background.fit
|
|
|
|
m.fit = function(...)
|
2013-07-08 00:18:55 +02:00
|
|
|
if not data.visible or (item.visible == false or item._filter_out == true or item._hidden == true) then
|
|
|
|
return 0,0
|
2013-07-04 07:51:01 +02:00
|
|
|
end
|
|
|
|
return data._internal.layout.item_fit(data,item,...)
|
2013-05-27 02:49:51 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if data.fkeys_prefix == true then
|
2013-07-07 06:39:17 +02:00
|
|
|
l:add(fkey(data,item))
|
2013-05-27 02:49:51 +02:00
|
|
|
m:set_left ( 0 )
|
|
|
|
end
|
|
|
|
|
|
|
|
if args.prefix_widget then
|
|
|
|
l:add(args.prefix_widget)
|
|
|
|
end
|
|
|
|
|
2013-06-23 04:30:27 +02:00
|
|
|
local icon = wibox.widget.imagebox()
|
2013-07-04 08:22:58 +02:00
|
|
|
icon.fit = function(...)
|
|
|
|
local w,h = wibox.widget.imagebox.fit(...)
|
|
|
|
return w+3,h
|
|
|
|
end
|
2013-05-27 02:49:51 +02:00
|
|
|
if args.icon then
|
|
|
|
icon:set_image(args.icon)
|
|
|
|
end
|
2013-06-23 04:30:27 +02:00
|
|
|
l:add(icon)
|
2013-05-27 02:49:51 +02:00
|
|
|
if item._private_data.sub_menu_f or item._private_data.sub_menu_m then
|
|
|
|
local subArrow = wibox.widget.imagebox() --TODO, make global
|
|
|
|
subArrow.fit = function(box, w, h) return subArrow._image:get_width(),item.height end
|
|
|
|
subArrow:set_image( beautiful.menu_submenu_icon )
|
|
|
|
lr:add(subArrow)
|
|
|
|
item.widget.fit = function(box,w,h,...)
|
|
|
|
args.y = data.height-h-data.margins.top
|
2013-07-04 08:22:58 +02:00
|
|
|
return wibox.widget.background.fit(box,w,h)
|
2013-05-27 02:49:51 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
if item.checkable then
|
|
|
|
item._internal.get_map.checked = function()
|
|
|
|
if type(item._private_data.checked) == "function" then
|
|
|
|
return item._private_data.checked()
|
|
|
|
else
|
|
|
|
return item._private_data.checked
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local ck = wibox.widget.imagebox()
|
|
|
|
ck:set_image(item.checked and checkbox.checked() or checkbox.unchecked())
|
|
|
|
lr:add(ck)
|
|
|
|
item._internal.set_map.checked = function (value)
|
|
|
|
item._private_data.checked = value
|
|
|
|
ck:set_image(item.checked and checkbox.checked() or checkbox.unchecked())
|
2013-07-04 07:51:01 +02:00
|
|
|
item._internal.has_changed = true
|
2013-05-27 02:49:51 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
if args.suffix_widget then
|
|
|
|
lr:add(args.suffix_widget)
|
|
|
|
end
|
|
|
|
la:set_left(l)
|
2013-07-05 07:32:14 +02:00
|
|
|
la:set_middle(text_w)
|
2013-05-27 02:49:51 +02:00
|
|
|
la:set_right(lr)
|
|
|
|
item.widget:set_widget(m)
|
|
|
|
local fit_w,fit_h = data._internal.layout:fit()
|
|
|
|
data.width = fit_w
|
|
|
|
data.height = fit_h
|
|
|
|
data.style(data)
|
2013-06-23 04:30:27 +02:00
|
|
|
item._internal.set_map.text = function (value)
|
2013-10-01 04:19:38 +02:00
|
|
|
if data.disable_markup then
|
|
|
|
text_w:set_text(value)
|
|
|
|
else
|
|
|
|
text_w:set_markup(value)
|
|
|
|
end
|
2013-07-02 00:18:25 +02:00
|
|
|
if data.auto_resize then
|
2013-07-05 07:32:14 +02:00
|
|
|
local fit_w,fit_h = wibox.widget.textbox.fit(text_w,9999,9999)
|
2013-07-02 00:18:25 +02:00
|
|
|
local is_largest = item == data._internal.largest_item_w
|
2013-07-04 07:51:01 +02:00
|
|
|
item._internal.has_changed = true
|
2013-07-02 00:18:25 +02:00
|
|
|
if not data._internal.largest_item_w_v or data._internal.largest_item_w_v < fit_w then
|
|
|
|
data._internal.largest_item_w = item
|
|
|
|
data._internal.largest_item_w_v = fit_w
|
|
|
|
end
|
|
|
|
--TODO find new largest is item is smaller
|
|
|
|
-- if data._internal.largest_item_h_v < fit_h then
|
|
|
|
-- data._internal.largest_item_h =item
|
|
|
|
-- data._internal.largest_item_h_v = fit_h
|
|
|
|
-- end
|
|
|
|
end
|
2013-06-23 04:30:27 +02:00
|
|
|
end
|
2013-07-05 06:57:30 +02:00
|
|
|
|
|
|
|
item._internal.set_map.f_key = function(value)
|
|
|
|
item._internal.has_changed = true
|
|
|
|
item._internal.f_key = value
|
|
|
|
data:remove_key_hook("F"..value)
|
|
|
|
data:add_key_hook({}, "F"..value , "press", function()
|
|
|
|
item.button1()
|
|
|
|
data.visible = false
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
item._internal.get_map.f_key = function() return item._internal.f_key end
|
|
|
|
|
2013-06-23 04:30:27 +02:00
|
|
|
item._internal.set_map.icon = function (value)
|
|
|
|
icon:set_image(value)
|
|
|
|
end
|
2013-07-02 00:18:25 +02:00
|
|
|
item._internal.set_map.text(item._private_data.text)
|
2013-08-09 08:34:49 +02:00
|
|
|
|
|
|
|
if data._internal.scroll_w and data.rowcount > data.max_items then
|
|
|
|
data._internal.scroll_w.visible = true
|
|
|
|
data._internal.scroll_w["up"]:emit_signal("widget::updated")
|
|
|
|
data._internal.scroll_w["down"]:emit_signal("widget::updated")
|
|
|
|
end
|
2013-05-27 02:49:51 +02:00
|
|
|
end
|
|
|
|
|
2013-07-04 07:20:46 +02:00
|
|
|
local function compute_geo(data)
|
|
|
|
local w = data.default_width
|
|
|
|
if data.auto_resize and data._internal.largest_item_w then
|
|
|
|
w = data._internal.largest_item_w_v+100 > data.default_width and data._internal.largest_item_w_v+100 or data.default_width
|
|
|
|
end
|
2013-07-08 00:18:55 +02:00
|
|
|
local visblerow = data.filter_string == "" and data.rowcount or data._internal.visible_item_count
|
|
|
|
if data.max_items and data.max_items < data.rowcount then
|
|
|
|
visblerow = data.max_items
|
|
|
|
if data.filter_string ~= "" then
|
|
|
|
local cur,vis = (data._start_at or 1),0
|
|
|
|
while (data._internal.items[cur] and data._internal.items[cur][1]) and cur < data.max_items + (data._start_at or 1) do
|
|
|
|
vis = vis + (data._internal.items[cur][1]._filter_out and 0 or 1)
|
|
|
|
cur = cur +1
|
|
|
|
end
|
|
|
|
visblerow = vis
|
|
|
|
end
|
|
|
|
end
|
2013-07-04 07:20:46 +02:00
|
|
|
if not data._internal.has_widget then
|
2013-08-09 08:34:49 +02:00
|
|
|
return w,(total and total > 0 and total or visblerow*data.item_height) + (data._internal.filter_tb and data.item_height or 0) + (data.max_items and data._internal.scroll_w.visible and (2*data.item_height) or 0)
|
2013-07-04 07:20:46 +02:00
|
|
|
else
|
2013-07-08 00:18:55 +02:00
|
|
|
local h = (visblerow-#data._internal.widgets)*data.item_height
|
2013-07-04 07:20:46 +02:00
|
|
|
for k,v in ipairs(data._internal.widgets) do
|
|
|
|
local fw,fh = v.widget:fit(9999,9999)
|
|
|
|
h = h + fh
|
|
|
|
end
|
|
|
|
return w,h
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-11 21:02:42 +02:00
|
|
|
local function new(data)
|
|
|
|
local l,real_l = wibox.layout.fixed.vertical(),nil
|
2013-07-07 06:39:17 +02:00
|
|
|
real_l = wibox.layout.fixed.vertical()
|
|
|
|
if data.max_items then
|
|
|
|
data._internal.scroll_w = scroll(data)
|
|
|
|
real_l:add(data._internal.scroll_w["up"])
|
|
|
|
end
|
|
|
|
real_l:add(l)
|
2013-05-11 21:02:42 +02:00
|
|
|
if data.show_filter then
|
2013-07-07 06:39:17 +02:00
|
|
|
if data.max_items then
|
|
|
|
real_l:add(data._internal.scroll_w["down"])
|
2013-05-11 21:02:42 +02:00
|
|
|
end
|
2013-07-07 06:39:17 +02:00
|
|
|
local filter_tb = filter(data)
|
|
|
|
real_l:add(filter_tb)
|
|
|
|
data._internal.filter_tb = filter_tb.widget
|
2013-05-11 21:02:42 +02:00
|
|
|
else
|
2013-07-07 06:39:17 +02:00
|
|
|
if data.max_items then
|
|
|
|
real_l:add(data._internal.scroll_w["down"])
|
|
|
|
end
|
2013-05-11 21:02:42 +02:00
|
|
|
end
|
|
|
|
real_l.fit = function(a1,a2,a3)
|
2013-07-04 07:51:01 +02:00
|
|
|
if not data.visible then return 1,1 end
|
2013-05-11 21:02:42 +02:00
|
|
|
local result,r2 = wibox.layout.fixed.fit(a1,99999,99999)
|
|
|
|
local total = data._total_item_height
|
2013-07-04 07:20:46 +02:00
|
|
|
return compute_geo(data)
|
2013-05-11 21:02:42 +02:00
|
|
|
end
|
|
|
|
real_l.add = function(real_l,item)
|
|
|
|
return wibox.layout.fixed.add(l,item.widget)
|
|
|
|
end
|
|
|
|
real_l.item_fit = item_fit
|
2013-05-27 02:49:51 +02:00
|
|
|
real_l.setup_key_hooks = module.setup_key_hooks
|
|
|
|
real_l.setup_item = module.setup_item
|
2013-07-23 08:12:19 +02:00
|
|
|
data._internal.content_layout = l
|
2013-05-11 21:02:42 +02:00
|
|
|
return real_l
|
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(module, { __call = function(_, ...) return new(...) end })
|
|
|
|
-- kate: space-indent on; indent-width 2; replace-tabs on;
|