/*
  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: buttons.c,v 1.3 2002/04/23 23:41:46 benj Exp $ */

#include "dat.h"
#include "fns.h"
#include <X11/cursorfont.h>

void
draw_button(BoxedWindow * window)
{
  if (!window)
    return;

  XClearWindow(window->box->dpy, window->button);
  XDrawString(window->box->dpy, window->button,
	      window->box->gc, 0, 15, 
	      window->name,
	      strlen(window->name));

  if (window->box->active_window == window->number)
    {
      XFillRectangle(window->box->dpy, window->button,
		     window->box->gc, 0, 0, BUTTON_DEFAULT_WIDTH, 20);
    }
}


/* FIXME: Not very useful for the moment, but will compute buttons
   height using font properties */
int
button_height()
{
  return 20;
}



void
move_button(Box *box)
{
  XEvent event;
  int maxc, old=-1, current=box->active_window;
  BoxedWindow * tmp = NULL;
  GC xorgc;
  XGCValues xgcv;
  XPoint points[4];
		
  for (maxc=0; box->boxed_windows[maxc]; maxc++);		      

  xgcv.subwindow_mode = IncludeInferiors;
  xgcv.function = GXinvert;
  xgcv.foreground = BlackPixel(box->dpy,box->sn);
  xgcv.background = BlackPixel(box->dpy,box->sn);
  xgcv.fill_style = FillSolid;
  xorgc = XCreateGC (box->dpy, DefaultRootWindow(box->dpy),
		     GCFillStyle|GCFunction|GCBackground|GCForeground|GCSubwindowMode, &xgcv);

  grab(box, box->win, 
       ButtonMask|PointerMotionMask|ButtonMotionMask|StructureNotifyMask, 
       XCreateFontCursor(box->dpy,XC_tcross), 0);

    while (1)
    {
      XNextEvent(box->dpy, &event);

      switch (event.type) 
	{
	case MotionNotify:

	  if (event.xmotion.x >= 0)
	    {
	      current = (event.xmotion.x+BUTTON_DEFAULT_WIDTH/2) / BUTTON_DEFAULT_WIDTH;
	      if (current > maxc)
		current = maxc;
	    }

	  if (current != old)
	    {
	      if (old!=-1 && old >= 0)
		{
		  points[0].x = old*100;
		  points[0].y = box->box_height - 20;
		  points[1].x = old*100 - (old>0?5:0);
		  points[1].y = box->box_height - 30;		  
		  points[2].x = old*100 + 5;
		  points[2].y = box->box_height - 30;		  
		  points[3].x = old*100;
		  points[3].y = box->box_height - 20;
		  XFillPolygon(box->dpy, box->win,
			       xorgc, points, 4, Convex,
			       CoordModeOrigin);
		}
	      if (current!=-1 && current >= 0)
		{
		  points[0].x = current*100;
		  points[0].y = box->box_height - 20;
		  points[1].x = current*100 - (current>0?5:0);
		  points[1].y = box->box_height - 30;		  
		  points[2].x = current*100 + 5;
		  points[2].y = box->box_height - 30;		  
		  points[3].x = current*100;
		  points[3].y = box->box_height - 20;
		  XFillPolygon(box->dpy, box->win,
			       xorgc, points, 4, Convex,
			       CoordModeOrigin);
		}
	      
	      old = current;
	    }
	  
	  break;
	  
	case ButtonPress:
	  points[0].x = current*100;
	  points[0].y = box->box_height - 20;
	  points[1].x = current*100 - (current>0?5:0);
	  points[1].y = box->box_height - 30;		  
	  points[2].x = current*100 + 5;
	  points[2].y = box->box_height - 30;		  
	  points[3].x = current*100;
	  points[3].y = box->box_height - 20;
	  XFillPolygon(box->dpy, box->win,
		       xorgc, points, 4, Convex,
		       CoordModeOrigin);

	  ungrab(box, (XButtonEvent *) &event);
	  XFreeGC(box->dpy, xorgc);

	  move_button_to(box, current);
	  return;

	default:
	  printf("Argh\n");
	  break;
	}
    }

}



void 
move_button_to(Box * box, int to)
{
  int i;
  BoxedWindow * tmp;
  
  if (to == box->active_window)
    return;
  if (box->boxed_windows[to] == NULL)
    to -= 1;
  
  tmp = box->boxed_windows[box->active_window];
  
  if (to < box->active_window)
    {
      for (i = box->active_window; i > to ; i--)
	{
	  box->boxed_windows[i] = box->boxed_windows[i-1];
	  box->boxed_windows[i]->number = i;
	}
      box->boxed_windows[to] = tmp;
      box->boxed_windows[to]->number = to;
    }
  else
    {
      for (i = box->active_window; i < to ; i++)
	{
	  box->boxed_windows[i] = box->boxed_windows[i+1];
	  box->boxed_windows[i]->number = i;
	}
      box->boxed_windows[to] = tmp;
      box->boxed_windows[to]->number = to;
    }
  
  box->active_window = to;
  update_buttons_position(box);

}
