/*
  9box, a windows repacker
  Copyright (C) 2001-2002  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$ */
#include "dat.h"
#include "fns.h"

char * b2items[4] = 
{
  "Grab new",
  "Release",
  "Move",
  "Destroy",
};

void
b2menu(Box * box)
{
  switch (menu (box, b2items, 4))
    {
    case 0:
      reparent_window(box);
      break;
      
    case 1:
      release_window(box->boxed_windows[box->active_window]);
      break;

    case 2:
      move_button(box);
      break;

    case 3:
      destroy_window(box->boxed_windows[box->active_window]);
      break;

    default:
      break;
    }
}

int 
menu(Box * box, char ** entries, int nentries)
{
  Window menuwindow;
  XEvent event;
  int width=0, height=0;
  int i, old=-1, current=-1;
  GC xorgc;
  XGCValues xgcv;

  height = box->font->ascent + box->font->descent + 1;
  for (i = 0; i<nentries; i++) {
    int wide;
    wide = XTextWidth(box->font, entries[i], strlen(entries[i])) + 4;
    if (wide > width)
      width = wide;
  }
  width += 4;

  menuwindow = XCreateSimpleWindow(box->dpy, box->win,
				   box->active_window * BUTTON_DEFAULT_WIDTH, 
				   box->box_height-button_height()-nentries*height, 
				   width, nentries*height, 1,
				   BlackPixel(box->dpy,box->sn), 
				   WhitePixel(box->dpy,box->sn));
  XSelectInput(box->dpy, menuwindow, ExposureMask);
  XMapWindow(box->dpy, menuwindow);
  grab(box, menuwindow, ButtonMask|ButtonMotionMask|StructureNotifyMask, 0, 0);

  xgcv.function = GXinvert;
  xgcv.foreground = BlackPixel(box->dpy,box->sn);
  xorgc = XCreateGC (box->dpy, DefaultRootWindow(box->dpy),
		     GCFunction|GCForeground, &xgcv);

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

      switch (event.type) 
	{
	case Expose:
	  for (i=0; i<nentries; i++)
	    {
	      int wide;
	      wide = XTextWidth(box->font, entries[i], strlen(entries[i]));
	      XDrawString(box->dpy, menuwindow, 
			  box->gc, 
			  (width-wide)/2, (i+1)*height-2, 
			  entries[i], strlen(entries[i]));
	    }
	  break;

	case MotionNotify:
	  current=event.xmotion.y/height;
	  if (current != old)
	    {
	      if (old != -1)
		XFillRectangle(box->dpy, menuwindow, xorgc, 0, old*height, width, height);
	      XFillRectangle(box->dpy, menuwindow, xorgc, 0, current*height, width, height);
	      old=current;
	    }
	  break;

	case ButtonRelease:
	  ungrab(box, (XButtonEvent *) &event);
	  XFreeGC(box->dpy, xorgc);
	  XDestroyWindow(box->dpy, menuwindow);
	  return current;

	default:
	  break;
	}
    }
}

