#include "acidlaunch.h"

/* when invoked (via signal delete_event), terminates the application.
 */
static gint close_application(GtkWidget* widget, GdkEvent* event, gpointer data)
{
    gtk_main_quit();
    return(FALSE);
}

/* grab key press events from the notebook */
static gint key_press(GtkWidget* widget, GdkEvent* e, gpointer data)
{
	GdkEventKey* event = (GdkEventKey *)e;
	GtkWidget* o = (GtkWidget*)data;	

	switch(event->keyval)
	{
		case GDK_Escape:
		case GDK_q:
		case GDK_Q:
			gtk_main_quit();
			break;

		case GDK_a:
		case GDK_A:
		case GDK_Tab:
			gtk_notebook_next_page(GTK_NOTEBOOK(o));
			break;

		case GDK_z:
		case GDK_Z:
			gtk_notebook_prev_page(GTK_NOTEBOOK(o));
			break;
	}

	return(FALSE);
}

/* create the GTK window and prepare it for adding tabs to */
LauncherWindow::LauncherWindow()
{
	/* create the main window */
	_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_signal_connect( GTK_OBJECT (_window), "delete_event",
                        GTK_SIGNAL_FUNC (close_application), NULL );
	gtk_container_set_border_width( GTK_CONTAINER (_window), 2 );
	
	/* set main window to auto-resize to hold it's widgets, but to not be
	   user resizable */
	gtk_window_set_policy (GTK_WINDOW (_window), FALSE, FALSE, FALSE);
	gtk_widget_realize(_window);
	
	/* ditch the window decorations */
	gdk_window_set_decorations(_window->window, (GdkWMDecoration)0);

	/* create the notebook for the main window */
	_notebook = gtk_notebook_new();
	gtk_container_add(GTK_CONTAINER(_window), _notebook);
	gtk_signal_connect(GTK_OBJECT(_window), "key_press_event",
		GTK_SIGNAL_FUNC(key_press), _notebook);

	/* hide the notebook's border and tabs */
	gtk_notebook_set_show_tabs(GTK_NOTEBOOK(_notebook), FALSE);
	gtk_notebook_set_show_border(GTK_NOTEBOOK(_notebook), FALSE);
}

LauncherWindow::~LauncherWindow()
{
	deleteTabs();
}

void LauncherWindow::show()
{
	gtk_widget_show_all(_window);
	
	/* if this window has a fixed x and y location, set it now */
	if((_xpos != -1) && (_ypos != -1))
	{
		gdk_window_move(_window->window, _xpos, _ypos);
	}
}

void LauncherWindow::deleteTabs()
{
	for(int i=0; i < _tabs.size(); i++)
	{
		delete _tabs[i];
	}
	_tabs.erase(_tabs.begin(), _tabs.end());
}

void LauncherWindow::setPrefs(LauncherNode* launcher)
{
	list<ConfigNode*>::const_iterator iter;

	/* delete all current tabs */
	deleteTabs();

	/* parse the tabs */
	for(iter=launcher->children().begin(); iter!=launcher->children().end(); iter++)
	{
		append((LauncherTabNode*)(*iter), launcher);
	}

	/* set the window to withdrawn mode (for blackbox slit) */
	if(launcher->isWithdrawn())
	{
		setWithdrawn(true);
	}

	/* if the window has a set x and y location, store it locally */
	_xpos = launcher->xPos();
	_ypos = launcher->yPos();
	
	
}

void LauncherWindow::append(LauncherTabNode* tab, LauncherNode* launcher)
{
	ButtonBar* bar = new ButtonBar(tab, launcher->isVertical(),
		launcher->xSize(), launcher->ySize(), launcher->width(), launcher->bgColor());
	_tabs.push_back(bar);
	gtk_notebook_append_page(GTK_NOTEBOOK(_notebook),bar->widget(),
		NULL);
}

void LauncherWindow::setWithdrawn(bool state)
{
	if(state==true)
	{
		XWMHints mywmhints;
		mywmhints.initial_state = WithdrawnState;
		mywmhints.flags=StateHint;

		XSetWMHints(GDK_DISPLAY(), GDK_WINDOW_XWINDOW(_window->window),
			&mywmhints);
	} else {
		/* set non-withdrawn mode here somehow */
	}
}



syntax highlighted by Code2HTML, v. 0.9.1