/* $Id: Menubutton.c,v 1.4 1997/04/24 12:59:31 archer Exp $
 *	MENUBUTTON.C - Handle Menu Buttons objects
 *
 * The MenuButtons are like command buttons, except that they pop up a menu
 * when depressed.
 */
#include <Widget/MenuButtonP.h>
#include <Widget/MenuP.h>
#include <Widget/drawP.h>
#include <Widget/fontsP.h>


/**
 ** Internal: MENUBUTTON_EXPOSE
 **	Menu buttons never have Focus
 **/
static void menubutton_expose(X11_widget wdg, XEvent *data)
{
/*
 * Differing styles
 */
	if (x11__is_monochrome) {
		x11__draw_start(wdg);
		x11__label_draw_active(W_LABEL);
	} else {
		x11__button_expose(wdg, data);
	}
}


/**
 ** Internal: PRESS
 **	Menu Button was depressed
 **/
static void press(X11_widget wdg, XEvent *e)
{
X11_widget	child;
/*
 * Do we have anything to do?
 */
	if ((child = wdg->child) == 0 ||
	    strcmp(child->class, x11__menu_class) ||
	    wdg->is_disabled)
		return;
/*
 * Change state for the duration of menu mode
 */
	x11__core_press(wdg, e);
	x11__menu_open(child, e);
	x11__core_release(wdg, (XEvent *)0);
}


/**
 ** X11_MENUB_GEOMETRY
 **	How large is this generic menu button
 **/
void x11__menub_geometry(x11__menubutton *wdg)
{
int	m;
/*
 * Basic size is the label
 */
	x11__label_size(W_LABEL);
	m = wdg->button.label.core.shadow_thickness +
	    wdg->button.label.core.margin_inside +
	    wdg->button.label.core.border2 / 2;
/*
 * Special computations
 */
	wdg->left_margin  =
	wdg->right_margin = m + wdg->side_margin;
	m *= 2;
	wdg->button.label.core.w_min = wdg->button.label.w + m + 2 * wdg->side_margin;
	wdg->button.label.core.h_min = wdg->button.label.h + m;
}


/**
 ** Internal: MENUBUTTON_GEOMETRY
 **	Basic geometry
 **/
static void menubutton_geometry(X11_widget wdg, XEvent *dummy)
{
	x11__menub_geometry(W_MENUBUTTON);
	x11__widget_grow(wdg);
}


/**
 ** X11_MENU_BUTTON_RESOURCES
 **	Fetch the resources for a menubutton
 **/
void x11__menub_resources(x11__menubutton *wdg, char *font, char *text)
{
/*
 * Menubutton is subclassed from Label thru Button
 */
	x11__label_resources(&wdg->button.label, font, text);
/*
 * Side margin is a menubutton-only item
 */
	wdg->side_margin = x11__resource_integer("SideMargin",wdg->side_margin);
}


/**
 ** X11_MENU_BUTTON_INIT
 **	Set up for a Menu Button clone
 **/
void x11__menub_init(x11__menubutton *wdg, char *text)
{
/*
 * Initial values
 */
	wdg->button.label.core.event_mask        = ExposureMask|
						PointerMotionMask|
						ButtonPressMask|
						ButtonReleaseMask;
	wdg->button.label.core.on_button_press   = press;
	wdg->button.label.core.on_enter_window   = x11__core_enter;
	wdg->button.label.core.on_exit_window    = x11__core_exit;
	wdg->button.label.core.on_motion_notify  = x11__core_motion;
	wdg->button.label.core.on_destruction    = x11__label_cleanup;
/*
 * Style-dependent
 */
	wdg->button.label.core.margin_inside    = 2;
	wdg->side_margin                        = 2;
	wdg->button.label.core.shadow_thickness = 2;
	wdg->button.label.core.fixed_h          = True;
	wdg->button.label.core.fixed_w          = True;
/*
 * Resources
 */
	x11__menub_resources(wdg, x11__font_helveticabold12, text);
}


/**
 ** X11_MENUBUTTON
 **	Create a MenuButton window
 **/
X11_widget X11_MenuButton(X11_widget parent, char *name, char *text)
{
x11__menubutton	*object;
/*
 * Get resource
 */
	object = allocator(x11__menubutton);
	x11__core_setup(&object->button.label.core, parent, "MenuButton", name);
/*
 * Initialize specific fields
 */
	object->button.label.core.on_expose         = menubutton_expose;
	object->button.label.core.on_geometry_check = menubutton_geometry;
/*
 * Resources
 */
	x11__menub_init(object, text);
/*
 * Ok
 */
	x11__menub_geometry(object);
	return OBJECT_P;
}
