---------------------------------------------------------------------------
-- @author Julien Danjou <julien@danjou.info>
-- @copyright 2008 Julien Danjou
-- @release @AWESOME_VERSION@
---------------------------------------------------------------------------

-- Grab environment we need
local ipairs = ipairs
local type = type
local tag = require("awful.tag")
local util = require("awful.util")
local suit = require("awful.layout.suit")
local client = require("awful.client")
local wibox = require("awful.wibox")
local ascreen = require("awful.screen")
local capi =
{
    hooks = hooks,
    screen = screen
}
local hooks = require("awful.hooks")

--- Layout module for awful
module("awful.layout")

--- Get the current layout.
-- @param screen The screen number.
-- @return The layout function.
function get(screen)
    local t = tag.selected(screen)
    return tag.getproperty(t, "layout") or suit.floating
end

--- Change the layout of the current tag.
-- @param layouts A table of layouts.
-- @param i Relative index.
function inc(layouts, i)
    local t = tag.selected()
    if t then
        local curlayout = get()
        local curindex
        local rev_layouts = {}
        for k, v in ipairs(layouts) do
            if v == curlayout then
                curindex = k
                break
            end
        end
        if curindex then
            local newindex = util.cycle(#layouts, curindex + i)
            set(layouts[newindex])
        end
    end
end

--- Set the layout function of the current tag.
-- @param layout Layout name.
function set(layout, t)
    t = t or tag.selected()
    tag.setproperty(t, "layout", layout)
    capi.hooks.arrange()(t.screen)
end

-- Register an arrange hook.
local function on_arrange (screen)
    local t = tag.selected(screen)
    local p = {}
    p.workarea = wibox.get_workarea(screen)
    -- Handle padding
    local padding = ascreen.padding(screen)
    if padding then
        p.workarea.x = p.workarea.x + (padding.left or 0)
        p.workarea.y = p.workarea.y + (padding.top or 0)
        p.workarea.width = p.workarea.width - ((padding.left or 0 ) + (padding.right or 0))
        p.workarea.height = p.workarea.height - ((padding.top or 0) + (padding.bottom or 0))
    end
    p.geometry = capi.screen[screen].geometry
    p.clients = client.tiled(screen)
    p.ncol = tag.getncol(t)
    p.nmaster = tag.getnmaster(t)
    p.mwfact = tag.getmwfact(t)
    p.tagdata = tag.getdata(t)
    p.screen = screen
    get(screen).arrange(p)
end

--- Get the current layout name.
-- @param layout The layout.
-- @return The layout name.
function getname(layout)
    local layout = layout or get()
    return layout.name
end

hooks.arrange.register(on_arrange)
hooks.property.register(function (c, prop)
    if type(c) ~= "client" then return end
    if prop == "size_hints_honor"
        or prop == "struts"
        or prop == "minimized"
        or prop == "sticky"
        or prop == "fullscreen"
        or prop == "maximized_horizontal"
        or prop == "maximized_vertical"
        or prop == "border_width"
        or prop == "hide" then
        on_arrange(c.screen)
    elseif prop == "screen" then
        -- If prop is screen, we do not know what was the previous screen, so
        -- let's arrange all screens :-(
        for screen = 1, capi.screen.count() do
            on_arrange(screen)
        end
    end
end)
hooks.wibox_position.register(function(wibox)
    on_arrange(wibox.screen)
end)

hooks.padding.register(function(screen) on_arrange(screen) end)
hooks.focus.register(function(c) on_arrange(c.screen) end)
hooks.clients.register(function()
                           for screen = 1, capi.screen.count() do
                               on_arrange(screen)
                           end
                       end)
hooks.tags.register(function(screen, tag, action) on_arrange(screen) end)

-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80