Source

Mirror / src / windowContainer.cpp

Full commit
#include "windowContainer.h"

WindowContainer::WindowContainer():
    focused(0),
    lastFocused(0)
{
}

WindowContainer::~WindowContainer()
{
}

void WindowContainer::addWindow(Window window)
{
    windows.insert(windows.begin() + focused, window);
}

bool WindowContainer::removeWindow(Window window)
{
    for (auto iter = windows.begin(); iter != windows.end(); iter++)
    {
        if (iter->id == window.id)
        {
            windows.erase(iter);
            return true;
        }
    }
    return false;
}

void WindowContainer::focusWindow(Window window)
{
    
    window.focus();
    try 
    {
        if (windows.at(focused).id == window.id)
            return; 
        lastFocused = focused;

        for (int j = 0; j < windows.size(); j++)
        {
            if (windows.at(j).id == window.id)
            {
                focused = j;
                return;
            }
        
        }
    }
    catch(std::out_of_range &e)
    {
        focused = 0;
    }
}


Window& WindowContainer::findWindow(xcb_window_t winId)
{
    for (auto& win : windows)
    {

        if (win.id == winId)
            return win;

    }
    return invalidWin;
}

void WindowContainer::killFocused()
{
    try
    {
       Window &win = windows.at(focused);
    
        win.close();
        focused = lastFocused;
        lastFocused = 0;

    }
    catch (std::out_of_range &e)
    {
    }

}

void WindowContainer::layoutWindows(xcb_rectangle_t area)
{
    xcb_rectangle_t slave, *rect, master;
    slave = master = area;
    bool firstTime = true;
    int winNum = 0;
    for (auto &win:windows)
        if (not win.ignore)
            winNum++;
    if (winNum > 1 )
    { 
        master.width = master.width/2;
        slave.width = master.width;
        slave.x += slave.width;
        slave.height = slave.height/(windows.size()-1);
    }
    rect = &master;
    for (auto &win : windows)
    {
        if (win.ignore)
        {
            continue;
        }
        if (win.haveState("_NET_WM_STATE_FULLSCREEN"))
        {
            rect = &area;
            win.stackAbove();
        }
        
        win.setGeometry(*rect);

        if (firstTime)
        {
            rect = &slave;
            firstTime = false;
        }
        else
        {
            slave.y += slave.height;
        }

    }
}

void WindowContainer::lowerAll()
{
    for (auto &win : windows)
    {
        if (not win.ignore)
        {
            win.stackBelow();
        }
    }
}

void WindowContainer::raiseAll()
{    
    for (auto &win : windows)
    {
        if (not win.ignore)
        {
            win.stackAbove();
        }
    }

}

void WindowContainer::toggleFullscreenFocused()
{
    try
    {
        auto &win = windows.at(focused);
        if (win.haveState("_NET_WM_STATE_FULLSCREEN"))
            win.removeState("_NET_WM_STATE_FULLSCREEN");
        else
            win.addState("_NET_WM_STATE_FULLSCREEN");
    }
    catch(std::out_of_range &e)
    {
        std::cout << "got std::out_of_range " << std::endl;
    }
}