/*-
 * Copyright (c) 2001 Jordan DeLong
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the author nor the names of contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
#include "menu.h"
#include "parseparams.h"

char *fore_colour;
char *back_colour;
char *highlight_colour;
char *highlight_type;

/* the root menu */
static menu_t	*rootmenu	= NULL;

/* the button that opens the root menu */
static int	menu_button	= Button3;

/* root window presses */
static int root_button(int pcall, screen_t *screen, XButtonEvent *e)
{
   log( 0, "root_button");
   
   if (e->type == ButtonPress && e->button == menu_button)
   {      
      menu_use(rootmenu, screen);
   }

   return PLUGIN_OK;
}

/* entry for x events */
int simple_menus_xevent_handler(XEvent *e) {
	client_t *client;
	menu_t *menu;
   XClientMessageEvent *cmsg;

	/* try to get client and menu_t */
	if (XFindContext(display, e->xany.window, client_context, (XPointer *) &client))
		return PLUGIN_OK;
	if (XFindContext(display, client->frame, menu_context, (XPointer *) &menu))
		return PLUGIN_OK;

	/* dispatch the event to a handler routine */
	switch (e->type)
   {
	case ButtonPress:
		menu_click(menu, client, &e->xbutton);
		break;
	case Expose:
		menu_expose(menu, client, &e->xexpose);
		break;
   case ClientMessage:
      cmsg = (XClientMessageEvent *) e;
      if( cmsg->message_type == WM_PROTOCOLS )
      {
         if( cmsg->data.l[0] == WM_DELETE_WINDOW )
         {
            menu_use(rootmenu, client->screen);
         }
      }
      break;
	}

	return PLUGIN_OK;
}

/* plugin intialization */
int simple_menus_init() {
	param_t *rmparam;
	dgroup_t *dgroup;
	pixmap_t *submenu_bullet;
	char *fontname;

	/* get parameters */
	OPTIONAL_PARAM(&plugin_this->params,  "menu_button", int, menu_button, Button3);
	OPTIONAL_PARAM(&plugin_this->params,  "menu_font", string, fontname, NULL);
	OPTIONAL_PARAM(&plugin_this->params,  "submenu_bullet", pixmap, submenu_bullet, NULL);
	OPTIONAL_PARAM(&plugin_this->params,  "menu_dgroup", dgroup, dgroup, NULL);
	OPTIONAL_PARAM( &plugin_this->params, "menu_stacklayer", stacklayer, menu_stacklayer,
                   STACKLAYER_ABOVE);

   OPTIONAL_PARAM( &plugin_this->params, "fore_colour", string, fore_colour,
                   "#FFFFFF" );
   OPTIONAL_PARAM( &plugin_this->params, "back_colour", string, back_colour,
                   "#000000" );
   OPTIONAL_PARAM( &plugin_this->params, "highlight_colour", string, highlight_colour,
                   "#FFFFFF" );
   OPTIONAL_PARAM( &plugin_this->params, "highlight_type", string, highlight_type,
                   "system" );

	/* get the menu system up */
	if (menu_init( fontname,
                  back_colour,
                  fore_colour,
                  highlight_colour,
                  highlight_type,
                  dgroup, submenu_bullet) != 0)
		goto failure;

	/* build the rootmenu */
	if ((rootmenu = menu_create()) == NULL)
		goto failure;

	/* build the rootmenu from parameters */
	if ((rmparam = plugin_find_param(&plugin_this->params, "rootmenu")) == NULL)
		PERR("required block 'rootmenu' not present");
	menu_parseparams(rootmenu, rmparam);

	if (fontname)
		free(fontname);
	return PLUGIN_OK;

failure:
	if (fontname)
		free(fontname);
	return PLUGIN_UNLOAD;
}

/* clean up */
void simple_menus_shutdown() {
	if (rootmenu)
		menu_delete(rootmenu);
	menu_shutdown();
}

/* realize the menus */
int simple_menus_start() {
	/* register callback */
	plugin_callback_add(plugin_this, PCALL_ROOT_BUTTON, root_button);

	if (menu_realize(rootmenu) == -1)
		PERR("unable to realize menus");

	return PLUGIN_OK;
}
