Mirror / src / windowContainer.cpp

#include "windowContainer.h"

WindowContainer::WindowContainer()
{
    focused = std::make_pair(0,0);
    lastFocused = focused;
    WindowList stack1;
    WindowList stack2;
    stackList.push_back(stack1);
    stackList.push_back(stack2);
    std::cout << stackList.size() << std::endl;
}

WindowContainer::~WindowContainer()
{
}

void WindowContainer::addWindow(Window window)
{
        for (auto& stack : stackList)
    {
        if (stack.empty())
        {
            stack.push_back(window);
            return;
        }
    }
    auto& stack = stackList.at(focused.first);
    stack.insert(stack.begin() + focused.second, window);

}

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

void WindowContainer::focusWindow(Window window)
{

    window.focus();
    if (stackList[focused.first][focused.second].id == window.id)
        return; 
    lastFocused = focused;
    WindowList stack;
    
    for(int i = 0; i<stackList.size(); i++)
    {
        stack = stackList.at(i); 
        for (int j = 0; j < stack.size(); j++)
        {
             if (stack.at(j).id == window.id)
             {
                 focused = std::make_pair(i,j);
                 return;
             }
        }
    }
}


Window& WindowContainer::findWindow(xcb_window_t winId)
{
    for (auto& stack : stackList)
    {
        for (auto& win : stack)
        {
            if (win.id == winId)
                return win;
        }
    }
    return invalidWin;
}

void WindowContainer::killFocused()
{
    if (stackList.at(focused.first).empty())
        return;
    Window &win = stackList.at(focused.first).at(focused.second);
    win.close();
    focused = lastFocused;
    lastFocused = std::make_pair(0,0);
}

void WindowContainer::layoutWindows(xcb_rectangle_t area)
{
    xcb_rectangle_t rect = area;
    xcb_rectangle_t temp;

    int stackNum = stackList.size();
    for (auto& stack : stackList)
    {
        if (stack.empty())
        {
            stackNum--;
        }
    }

    if (stackNum == 0)
        return;

    
    rect.width = rect.width/stackNum;
    rect.y = rect.y + 1;
    lowerAll();
    
    for (auto& stack : stackList)
    {
        if (stack.empty())
        {
            continue;
        }
        temp = rect;
        temp.height = temp.height/stack.size();

        for (auto& win : stack)
        {
            if (win.exempt)
            {
                xcb_rectangle_t exemptRect = win.getGeometry();
                exemptRect.y = rect.y;
                exemptRect.x = rect.x;
                win.setGeometry(exemptRect);

                win.setGeometry(temp);
                temp.y += temp.height;
            }
            if (win.fullscreen)
            {
                win.setGeometry(area);
                win.stackAbove();
            }
            if (not win.fullscreen and not win.exempt)
            {
                win.setGeometry(temp);
                temp.y += temp.height;
            }

        }
        rect.x += rect.width;
    }
}

void WindowContainer::addStack()
{
    stackList.push_back(WindowList());
}

void WindowContainer::raiseAll()
{
    for (auto &stack : stackList)
    {
        if (stack.empty())
        {
            continue;
        }
        for (auto &win : stack)
        {
            win.stackAbove();
        }
    }
}

void WindowContainer::lowerAll()
{
    for (auto & stack : stackList)
    {
        if (stack.empty())
        {
            continue;
        }
        for (auto &win : stack)
        {
            win.stackBelow();
        }
    }

}

void WindowContainer::toggleExemptFocused()
{
    if (stackList.at(focused.first).empty())
        return;

    Window &win = stackList.at(focused.first).at(focused.second);
    if (not win.exempt)
        win.exempt = true;
    else
        win.exempt = false;
}

void WindowContainer::toggleFullscreenFocused()
{
    if (stackList.at(focused.first).empty())
        return;

    Window &win = stackList.at(focused.first).at(focused.second);
    if (not win.fullscreen)
    {
        win.fullscreen = true;
        win.stackAbove();
    }
    else
    {
        win.fullscreen = false; 
        win.stackBelow();
    }
}

std::pair<int, int> WindowContainer::getFocusedIndex()
{
    return focused;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.