/*
 *   Copyright (c) 1998 David Parsons. 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. All advertising materials mentioning features or use of this
 *     software must display the following acknowledgement:
 *     
 *   This product includes software developed by David Parsons
 *   (orc@pell.chi.il.us)
 *
 *  4. My name may not be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *     
 *  THIS SOFTWARE IS PROVIDED BY DAVID PARSONS ``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 DAVID
 *  PARSONS 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.
 */

static const char rcsid[] = "$Id: s.glue.c 1.2 99/04/27 17:59:12-00:00 orc $";

/*
 * glue:  functions to emulate the extensions I hacked into libdialog
 * and didn't bother to port into ndialog
 */

#include <ndialog.h>
#include <dialog.h>


/*
 * form_menu() does a menu with a BACK button.  If neednext is non-zero, a
 * NEXT button is also supplied.
 */
int
form_menu(char* title, char* message, int nritems, ListItem* list,
	  char* selection, int neednext, char* help)
{
    int formwidth, width, tempwidth;
    int ix;
    int dx;
    int rc;
    int formwidth;
    int menudisplaydepth;
    ndObject o_menu, o_next, o_prev;
    ndObject chain;

    for (width = ix = 0; ix < nritems; ix++) {
	tempwidth = strlen(list[ix].id) + strlen(list[ix].item) + 2;
	if (tempwidth > width)
	    width = tempwidth;
    }
    if (prompt)
	formwidth = strwidth(prompt);
    else
	formwidth = width;

    if (formwidth > width)
	dx = (width-formwidth)/2;
    else
	dx = 0;

    menudisplaydepth = (nritems > ROWS-8) ? (ROWS-8) : nritems;

    o_menu = newMenu(dx, 0, -1, menudisplaydepth, nritems, list,
		     0, "", SHOW_IDS, chooseit, help);

    o_prev = newCancelButton(0, "PREV", 0, help);
    chain = ObjChain(o_menu, o_prev);
    if (neednext) {
	o_next = newOKButton(1, "NEXT", 0, help);
	chain = ObjChain(chain, o_next);
    }

    rc = MENU(chain, -1, -1, title, prompt, 0);

    if (rc == MENU_OK) {
	result[0] = 0;
	for (ix = 0; ix < nritems; ix++)
	    if (list[ix].selected) {
		list[ix].selected = 0;
		strcpy(result, list[ix].id);
	    }
    }

    deleteObjChain(chain);

    return (rc == MENU_OK) ? 0 : -1;
} /* form_menu */
