/*
 * windowmenu.cc
 * Copyright (C) 2000 Frank Hale
 * frankhale@yahoo.com
 * http://sapphire.sourceforge.net/
 *
 * Updated: 18 Nov 2001
 *
 * 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 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.
 */
 
#include "aewm.hh"
#include "windowmanager.hh"

WindowMenu::WindowMenu() : GenericMenu()
{
	sendToDesktop = new WindowMenu(this);
	sendToDesktop->updateMenu();
	windowMaxMenu = new WindowMenu(this);
	windowMaxMenu->updateMenu();
	updateWindowMenu();
}

WindowMenu::WindowMenu(WindowMenu *parentMenu) : GenericMenu (parentMenu)
{
}

WindowMenu::~WindowMenu()
{
}

void WindowMenu::updateWindowMenu()
{
	char* temp = new char[wm->getMaxDesktops()];
	for(int i=0; i<wm->getMaxDesktops(); i++)
	{
		sprintf(temp, "%d", i);

		sendToDesktop->insert(temp,"", SEND_TO_DESKTOP);
	}

	delete [] temp;

	insert("Send To Desktop", "", sendToDesktop, -1);
	insert("Sticky Toggle", "", STICKY);
	insert("separator", "", -1);
	
	windowMaxMenu->insert("Max Vert Toggle", "", MAX_VERT);
	windowMaxMenu->insert("Max Horz Toggle", "", MAX_HORZ);
	windowMaxMenu->insert("Max Both Toggle", "", MAX_BOTH);
	
	insert("Maximize", "", windowMaxMenu, MAX_BOTH);
	insert("Minimize", "", MINIMIZE);
	insert("Shade Toggle", "", SHADE);
//	insert("On Top", "", ON_TOP);
	
	insert("separator", "", -1);

	insert("Destroy", "", CLOSE);
	
	updateMenu();
	addToMenuList(this);
	
	windowMaxMenu->updateMenu();
	addToMenuList(windowMaxMenu);
	sendToDesktop->updateMenu();
	addToMenuList(sendToDesktop);
	
}

void WindowMenu::handle_button_release_event(XButtonEvent *e)
{
	int desktop=0;

	GenericMenu::handle_button_release_event(e);

	if ( getParentMenu() != NULL ) {
		client = ((WindowMenu *)(getParentMenu()))->client;
	}

	switch (e->button)
	{
		case Button1:
			if (curr)
			{
				switch(	curr->function )
				{
					case SEND_TO_DESKTOP:
						desktop = atoi(curr->name.c_str());
						client->set_desktop(desktop);
						hideAllVisibleSubmenus();
						break;
					case MAX_VERT:
						client->maximize_vertical();
						client->warpToTitlebarCenter();
						break;
					case MAX_HORZ:
						client->maximize_horizontal();
						client->warpToTitlebarCenter();
						break;
					case MAX_BOTH:
						client->maximize();
						client->warpToTitlebarCenter();
						break;
					case MINIMIZE:
						client->iconify();
						break;
					case STICKY:
						client->stick(DISALLOW_STICKY_TRANS);
						break;
					case SHADE:
						client->shade();
						client->warpToTitlebarCenter();
						break;
					case CLOSE:
						client->send_wm_delete();
						break;
				}
			}
		break;
		case Button2:
		case Button3:
			hide();
		break;
	}

	switch(wm->getFocusModel())
	{
		case FOCUS_FOLLOW:

		case FOCUS_SLOPPY:
			wm->setFocusedClientUnderPointer();
			break;

		case FOCUS_CLICK:
			break;
	}

	if (getParentMenu() != NULL) {
		getParentMenu()->hide();
		client = NULL;
	}
}


syntax highlighted by Code2HTML, v. 0.9.1