/*
  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: utils.c,v 1.3 2002/04/23 23:40:21 benj Exp $ */

#include <X11/Xlib.h>
#include <X11/Xutil.h>

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


BoxedWindow *
search_boxedwindow(Box * box, Window window)
{
  int i;
  
  for (i=0; i<MAX_BOXED_WINDOWS; i++)
    {
      if (box->boxed_windows[i] &&
	  (box->boxed_windows[i]->client == window ||
	   box->boxed_windows[i]->button == window))
	{
	  return box->boxed_windows[i];
	}
    }
  return NULL;
}


BoxedWindow *
search_boxedwindow_client(Box * box, Window window)
{
  int i;
  
  for (i=0; i<MAX_BOXED_WINDOWS; i++)
    {
      if (box->boxed_windows[i] &&
	  box->boxed_windows[i]->client == window)
	{
	  return box->boxed_windows[i];
	}
    }
  return NULL;
}


BoxedWindow *
search_boxedwindow_button(Box * box, Window window)
{
  int i;
  
  if (! window)
    return NULL;

  for (i=0; i<MAX_BOXED_WINDOWS; i++)
    {
      if (box->boxed_windows[i] &&
	  box->boxed_windows[i]->button == window)
	{
	  return box->boxed_windows[i];
	}
    }
  return NULL;
}


void
resize_box_for_client(Box * box, int client)
{
  Window root_return;
  int x_return, y_return, width_return, height_return, border_width_return, depth_return;

  XGetGeometry(box->dpy, box->boxed_windows[client]->client,
	       &root_return, &x_return, &y_return, 
	       &width_return, &height_return,
	       &border_width_return, &depth_return);

  XResizeWindow(box->dpy, box->win, width_return, height_return+button_height());
  box->box_width = width_return;
  box->box_height = height_return+button_height();

  XMoveWindow(box->boxed_windows[client]->box->dpy, box->boxed_windows[client]->button,
	      box->boxed_windows[client]->number * BUTTON_DEFAULT_WIDTH, height_return);
}




