/*
  9box, a windows repacker
  Copyright (C) 2001  Benjamin Drieu <bdrieu@april.org>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/* $Id: manage.c,v 1.5 2002/04/23 23:39:09 benj Exp $ */

#include "dat.h"
#include "fns.h"

void
resize_window(BoxedWindow * window)
{
  Window root_return;
  int x_return, y_return, width_return, height_return, border_width_return, depth_return;
  
  XGetGeometry(window->box->dpy, window->box->win,
	       &root_return, &x_return, &y_return, 
	       &width_return, &height_return,
	       &border_width_return, &depth_return);
  XMoveWindow(window->box->dpy, window->button,
	      window->number * BUTTON_DEFAULT_WIDTH, height_return - button_height());
  printf(">> %d; %d\n", width_return, height_return);
  XResizeWindow(window->box->dpy, window->client, width_return, height_return - button_height());
}


void
select_window(BoxedWindow * window)
{
  int tmp;
  char * tmp_name;
  
  printf("Selection de %s\n", window->name);
  if (window->box->active_window != NO_ACTIVE_WINDOW &&
      window->box->boxed_windows[window->box->active_window])
    {
      XUnmapWindow (window->box->dpy, 
		    window->box->boxed_windows[window->box->active_window]->client);
    }

  XMapWindow (window->box->dpy, window->client); 
  tmp = window->box->active_window;
  window->box->active_window = window->number;

  if (window->box->boxed_windows[window->box->active_window])
    {
      draw_button(window->box->boxed_windows[window->box->active_window]);
    }

  set_title(window->box, window->name);

  set_constraints_from_window(window->box, window->client);

  XSetInputFocus(window->box->dpy, window->client, RevertToParent, CurrentTime);

  if (tmp != NO_ACTIVE_WINDOW)
    {
      draw_button(window->box->boxed_windows[tmp]);
    }
  selecting_window = 1;
}


void
remove_window(BoxedWindow * window)
{
  int i;
  
  XUnmapWindow(window->box->dpy, window->button);
  XDestroyWindow(window->box->dpy, window->button);

  for (i = window->number; window->box->boxed_windows[i] != NULL;
       i++)
    {
      window->box->boxed_windows[i] = window->box->boxed_windows[i+1];
      if (window->box->boxed_windows[i])
	window->box->boxed_windows[i]->number --;
    }

  if (window->box->active_window != NO_ACTIVE_WINDOW &&
      window->number <= window->box->active_window)
    {
      if (window->box->boxed_windows[window->box->active_window] !=
	  NULL)
	{
	  select_window(window->box->boxed_windows[window->box->active_window]);
	}
      else if ((window->box->active_window - 1 >= 0) &&
	       window->box->boxed_windows[window->box->active_window-1] !=
	       NULL)
	{
	  select_window(window->box->boxed_windows[window->box->active_window-1]);
	}
      else
	{
	  set_title(window->box, "<none>");
	  window->box->active_window = NO_ACTIVE_WINDOW;
	}
    }

  update_buttons_position(window->box);
  free(window);
}


void
destroy_window(BoxedWindow * window)
{
  select_window(window);
  XDestroyWindow(window->box->dpy, window->client);
}


void
release_window(BoxedWindow * window)
{
  select_window(window);
  XReparentWindow(window->box->dpy, window->client, DefaultRootWindow(window->box->dpy), 0, 0);
  remove_window(window);
}


void
update_buttons_position(Box * box)
{
  int x_return, y_return, width_return, height_return, border_width_return, depth_return;
  Window root_return;
  int i;
  
  XGetGeometry(box->dpy, box->win,
	       &root_return, &x_return, &y_return, 
	       &width_return, &height_return,
	       &border_width_return, &depth_return);

  for (i=0; i<MAX_BOXED_WINDOWS && box->boxed_windows[i]; i++)
    {
      XMoveWindow(box->dpy, box->boxed_windows[i]->button,
		  i * BUTTON_DEFAULT_WIDTH, height_return - 20);
    }
}


