/*
 *  neercs        console-based window manager
 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
 *                2008 Jean-Yves Lamoureux <jylam@lnxscene.org>
 *                All Rights Reserved
 *
 *  $Id: wm.c 2360 2008-06-11 16:23:46Z jylam $
 *
 *  This program is free software. It comes without any warranty, to
 *  the extent permitted by applicable law. You can redistribute it
 *  and/or modify it under the terms of the Do What The Fuck You Want
 *  To Public License, Version 2, as published by Sam Hocevar. See
 *  http://sam.zoy.org/wtfpl/COPYING for more details.
 */

#include <stdio.h>
#include <cucul.h>
#include <caca.h>
#include <stdlib.h>

#include "neercs.h"


void resize_screen(struct screen *s, int w, int h)
{
    cucul_canvas_t *old, *new;

    if(w==s->w && h==s->h) return;
    if(w <= 0 || h <= 0) return;

    s->w = w;
    s->h = h;

    /* cucul_set_canvas_boundaries() is bugged as hell,
     * so let's resize it by hands */
    old = s->cv;
    new = cucul_create_canvas(w, h);
    cucul_blit(new, 0, 0, old, NULL);
    s->cv = new;
    cucul_free_canvas(old);
    set_tty_size(s->fd, w, h);
}

void update_windows_props(cucul_canvas_t *cv, struct screen_list *screen_list)
{
    if(!screen_list->count) return;

    switch(screen_list->wm_type)
    {
    case WM_CARD:
        update_windows_props_cards(cv, screen_list);
        break;
    case WM_HSPLIT:
        update_windows_props_hsplit(cv, screen_list);
        break;
    case WM_VSPLIT:
        update_windows_props_vsplit(cv, screen_list);
        break;
    case WM_FULL:
    default:
        update_windows_props_full(cv, screen_list);
        break;
    }
}

void update_windows_props_hsplit(cucul_canvas_t *cv, struct screen_list *screen_list)
{
    int i;
    int w = (screen_list->width / screen_list->count) - 1;
    int h = (screen_list->height) - 2;

    for(i = 0; i < screen_list->count; i++)
    {
        screen_list->screen[i]->x = (i*w)+1;
        screen_list->screen[i]->y = 1;
        screen_list->screen[i]->visible = 1;
        if(i != screen_list->count -1)
        {
            resize_screen(screen_list->screen[i],
                          w - 1, h);
        } else {
            resize_screen(screen_list->screen[i],
                          screen_list->width - i*w - 1,
                          h);
        }
    }
}

void update_windows_props_vsplit(cucul_canvas_t *cv, struct screen_list *screen_list)
{
    int i;
    int w = screen_list->width - 2;
    int h = (screen_list->height / screen_list->count);

    for(i = 0; i < screen_list->count; i++)
    {
        screen_list->screen[i]->x = 1;
        screen_list->screen[i]->y = (i*h) + 1;
        screen_list->screen[i]->visible = 1;
        if(i != screen_list->count -1)
        {
            resize_screen(screen_list->screen[i],
                          w, h - 2);
        } else {
            resize_screen(screen_list->screen[i],
                          w,
                          screen_list->height - i*h - 2);
        }
    }
}


void update_windows_props_full(cucul_canvas_t *cv, struct screen_list *screen_list)
{
    int i;
    int w = screen_list->width - 2;
    int h = screen_list->height - 2;

    for(i = 0; i < screen_list->count; i++)
    {
        screen_list->screen[i]->visible = 0;
        screen_list->screen[i]->x = 1;
        screen_list->screen[i]->y = 1;

        resize_screen(screen_list->screen[i],
                      w, h);
    }
    screen_list->screen[screen_list->pty]->visible = 1;
}


void update_windows_props_cards(cucul_canvas_t *cv, struct screen_list *screen_list)
{
    int i;
    int w = (screen_list->width  - screen_list->count) - 3;
    int h = (screen_list->height - screen_list->count) - 4;
    int x = (screen_list->width  - w) - (3*screen_list->count) + 4;
    int y = 2 + (1*screen_list->count);

    for(i = 0; i < screen_list->count; i++)
    {
        screen_list->screen[i]->visible = 1;
        screen_list->screen[i]->x = x;
        screen_list->screen[i]->y = y;

        resize_screen(screen_list->screen[i],
                      w, h);
        x += 3;
        y--;
    }
}

