/*
 * dml - Dialog Markup Language
 *
 * $Id: menubutton.c,v 1.3 2003/04/04 10:08:27 malekith Exp $
 * Author: Michal Moskal <malekith@pld-linux.org>
 * include COPYING-GNU
 */

#include "pi.h"

void pi_menubutton_kill(struct pi_menubutton *self)
{
	pi_button_kill((struct pi_button*)self);
	xfree(self->title);
}

void pi_menubutton_key(struct pi_menubutton *self, int key)
{
	char *ret;
	
	if ((key != ' ' && key != '\n') || self->input == 0) {
		pi_object_key((struct pi_object*)self, key);
		return;
	}

	ret = pi_popup_menu(self->menu, self->title);
	pi_input_set_caption(self->input, ret);
	xfree(ret);
	if (!self->input->ghosted) {
		self->super.parent->active = (struct pi_object*)self->input;
		pi_draw((struct pi_object*)self);
		pi_draw((struct pi_object*)self->input);
	} else {
		pi_draw((struct pi_object*)self->input);
		pi_draw((struct pi_object*)self);
	}
}

static struct pi_funcs menubutton_funcs = {
	1,
	(pi_handler_void)pi_button_draw,
	(pi_handler_int)pi_object_cc,
	(pi_handler_int)pi_menubutton_key,
	(pi_handler_void)pi_menubutton_kill,
	(pi_handler_rstring)pi_button_fetch,
};

struct pi_menubutton *pi_new_menubutton(int size)
{
	struct pi_menubutton *self;

	if (size == 0)
		size = sizeof(struct pi_menubutton);
	self = (struct pi_menubutton*)pi_new_button(size);

	self->super.f = &menubutton_funcs;

	return self;
}
