/******************************************************************************

    MODUL
	menucom.c

    DESCRIPTION
	[X]DME command interface for menubase.c

    NOTES
	experimental status

	in zukunft koennte es moeglich sein,
	einen einzien namensprameter zu akzeptieren,
	der dann ueber split_menu_names in seine
	komponenten aufgespalten wuerde

    BUGS
	IMPORTANT
	    we cannot actually react on ERRORs !!

    TODO
	include some error()-calls
	support checks

    EXAMPLES

    description of the user-commands:

	the commands below might get other names;
	if so, please update their descriptions according
	to the names they are used in command.c

*!  MENUES:
*!  ******
*!
*!  the following commands allow access to [X]DME's menustructures
*!  that way it is possible for each user to create an individual
*!  GUI for "his" [X]DME, for "his" programming language and for
*!  his wishes.
*!
*! >MENUDELHDR menuname
*!
*!	delete a menuheader and all associated items (and subitems)
*!
*!	menuheaders are created by using their names in item or
*!	subitem definitions
*!
*! >MENUDEL    menuname itemname
*! >MENUDELBAR menuname
*!
*!	delete a menuitem-entry
*!	DELBAR deletes the first itembar in a menu's itemlist
*!	DEL deletes the menuitem with the name itemname
*!	    if itemname is a subitem-carrier, all subs are deleted, too
*!
*!	You can delete all items of a menu, if You call MENUDELHDR
*!
*! >MENUDELSUB	  menuname itemname subname
*! >MENUDELSUBBAR menuname itemname
*!
*!	delete a submenu-entry
*!	DELSUBBAR deletes the first submenubar in a menuitem's subitemlist
*!	DELSUB deletes the submenu with the name subname
*!
*!	You can delete all an item's subitem if You MENUDEL the carrying item
*!
*! >MENUADD   menuname itemname subname command
*! >MENUBAR   menuname itemname
*! >MENUCHECK menuname itemname subname command
*!
*!	create a menuitem-entry
*!	BAR simply appends an itembar to the item-list
*!	ADD creates a full menuitem-entry, if not already one
*!	    of the same name exists
*!	CHECK also sets the menutoggle flag to an entry created
*!	    like ADD
*!
*!	You cannot add subitems to items that were created with
*!	    MENUADD or MENUCHECK as these have commands added
*!	    while we expect subitem carrier to have no command added
*!	You cannot redefine the type of an menuitem once created
*!	    normal items can't be redefined to checkitems
*!	    or subitem carriers and vice versa
*!
*! >MENUSUBADD	 menuname itemname subname command
*! >MENUSUBBAR	 menuname itemname
*! >MENUSUBCHECK menuname itemname subname command
*!
*!	create a submenu-entry
*!	SUBBAR simply appends an itembar to the subitem-list
*!	SUBADD creates a full subitem-entry, if not already one
*!	    of the same name exists
*!	SUBCHECK also sets the menutoggle flag to an entry created
*!	    like SUBADD
*!
*!	You cannot add subitems to items that were created with
*!	    MENUADD or MENUCHECK as these are managed in a quite
*!	    different way, so You create subitem-carrier simply by
*!	    using their names in a subitem definition
*!	You cannot redefine the type of an subitem once created
*!	    normal subs can't be redefined to subcheck and vice versa
*!
*! >MENUSETITEM menuname itemname	  status
*! >MENUSETSUB	menuname itemname subname status
*! >MENUCHKITEM menuname itemname	  variablename
*! >MENUCHKSUB	menuname itemname subname variablename
*!
*!	these commands set or check the satus of the (/sub)item-checked
*!	flags for (sub)items created with the MENU(SUB)CHECK commands
*!	status is one of 0|1 the value set into variable is also 0|1
*!
*! >MENUCLEAR
*!
*!	clean up the menustrip, i.e. delete all of its
*!	menuheaders
*!
*! >MENUSAVE filename
*!
*!	save the menustrip as a file that can be
*!	read by MENULOAD
*!
*! >MENULOAD filename
*!
*!	clear the menustrip and instead build a new
*!	contents out of the file filename
*!
*! >MENUON
*! >MENUOFF
*!
*!	show or hide the whole menustrip
*!	MENUOFF is stackable, i.e You need as many MENUON calls
*!	to show the menustrip as previously MENUOFF calls
*!
*!
*!  MENUSTRIPS:
*!  **********
*!
*!  the following commands do only make sense, if
*!  You are using multiple menustrips
*!  at [X]DME's start a menustrip called "default" is created;
*!  for "normal" usage, that single strip may suffer.
*!
*!  Please note that the above MENU... commands apply to the
*!  CURRENT menustrip (and only to it)
*!
*! >USEMENUSTRIP name
*!
*!	search for a certain menustrip and use it as the current one
*!
*! >REMMENUSTRIP
*!
*!	delete the current menustrip, if it is not the only one
*!
*! >NEWMENUSTRIP name
*!
*!	if there is already a menustrip called name,
*!	    simply call USEMENUSTRIP name
*!	else create a new menustrip called name and use it
*!

    SEE ALSO
	menustrips.c menu_dme.c menucontrol.c command.c


    INDEX
	$Header : $

    HISTORY
	18 Dec 1992 b_null created
	20 Dec 1992 b_null rewritten & documented
	23 Jan 1993 b_null added menutomacro
	25 Jan 1993 b_null added do_delmenu, do_itemcheck, do_subcheck

******************************************************************************/

/**************************************
		Includes
**************************************/
#include "defs.h"
#include "menubase.h"


/**************************************
	    Globale Variable
**************************************/
/* menu(item)management */
Prototype void do_delmenu   (void);
Prototype void do_delitem   (void);
Prototype void do_delitembar(void);
Prototype void do_delsub    (void);
Prototype void do_delsubbar (void);
Prototype void do_itembar   (void);
Prototype void do_itemcheck (void);
Prototype void do_itemadd   (void);
Prototype void do_subadd    (void);
Prototype void do_subcheck  (void);
Prototype void do_subbar    (void);
Prototype void do_menuoff   (void);
Prototype void do_menuon    (void);
Prototype void do_menuload  (void);
Prototype void do_menusave  (void);
Prototype char*menutomacro  (char*);
Prototype void do_menuclear (void);

/* modification on checks */
Prototype void do_getcheckitem (void);
Prototype void do_getchecksub  (void);
Prototype void do_setcheckitem (void);
Prototype void do_setchecksub  (void);

/* management for menustrips */
Prototype void do_new_menustrip (void);
Prototype void do_del_menustrip (void);
Prototype void do_use_menustrip (void);


/**************************************
      Interne Defines & Strukturen
**************************************/


/**************************************
	    Interne Variable
**************************************/


/**************************************
	   Interne Prototypes
**************************************/



/*****************************************************************************

    NAME
	do_delmenu

    PARAMETER
	void

    RESULT
	-/-

    RETURN
	void

    DESCRIPTION
	[X]DME command interface for

    NOTES

    BUGS

    EXAMPLES

    SEE ALSO

    INTERNALS

    HISTORY
	25 Jan 1993 b_noll created

******************************************************************************/

void do_delmenu (void)
{
    menuoff (currentmenu(), currentwindow());
    menudel (currentmenu(), GetArg(1));
    menuon  (currentmenu(), currentwindow());
} /* do_delmenu */



/*****************************************************************************

    NAME
	do_itemadd

    PARAMETER
	void

    RESULT
	-/-

    RETURN
	void

    DESCRIPTION
	[X]DME command interface for

    NOTES

    BUGS

    EXAMPLES

    SEE ALSO

    INTERNALS

    HISTORY
	20 Dec 1992 b_noll created

******************************************************************************/

void do_itemadd (void)
{
    menuoff (currentmenu(), currentwindow());
    itemadd (currentmenu(), GetArg(1), GetArg(2), GetArg(3), NULL, 0);
    menuon  (currentmenu(), currentwindow());
} /* do_itemadd */



/*****************************************************************************

    NAME
	do_itemcheck

    PARAMETER
	void

    RESULT
	-/-

    RETURN
	void

    DESCRIPTION
	[X]DME command interface for

    NOTES

    BUGS

    EXAMPLES

    SEE ALSO

    INTERNALS

    HISTORY
	25 Jan 1993 b_noll created

******************************************************************************/

void do_itemcheck (void)
{
    menuoff (currentmenu(), currentwindow());
    itemadd (currentmenu(), GetArg(1), GetArg(2), GetArg(3), NULL, 1);
    menuon  (currentmenu(), currentwindow());
} /* do_itemcheck */



/*****************************************************************************

    NAME
	do_itembar

    PARAMETER
	void

    RESULT
	-/-

    RETURN
	void

    DESCRIPTION
	[X]DME command interface for

    NOTES

    BUGS

    EXAMPLES

    SEE ALSO

    INTERNALS

    HISTORY
	20 Dec 1992 b_noll created

******************************************************************************/

void do_itembar (void)
{
    menuoff (currentmenu(), currentwindow());
    itemadd (currentmenu(), GetArg(1), BAR, NULL, NULL, 0);
    menuon  (currentmenu(), currentwindow());
} /* do_itembar */



/*****************************************************************************

    NAME
	do_subadd

    PARAMETER
	void

    RESULT
	-/-

    RETURN
	void

    DESCRIPTION
	[X]DME command interface for

    NOTES

    BUGS

    EXAMPLES

    SEE ALSO

    INTERNALS

    HISTORY
	20 Dec 1992 b_noll created

******************************************************************************/

void do_subadd (void)
{
    menuoff (currentmenu(), currentwindow());
    subadd  (currentmenu(), GetArg(1), GetArg(2), GetArg(3), GetArg(4), NULL, 0);
    menuon  (currentmenu(), currentwindow());
} /* do_subadd */



/*****************************************************************************

    NAME
	do_subcheck

    PARAMETER
	void

    RESULT
	-/-

    RETURN
	void

    DESCRIPTION
	[X]DME command interface for

    NOTES

    BUGS

    EXAMPLES

    SEE ALSO

    INTERNALS

    HISTORY
	25 Jan 1993 b_noll created

******************************************************************************/

void do_subcheck (void)
{
    menuoff (currentmenu(), currentwindow());
    subadd  (currentmenu(), GetArg(1), GetArg(2), GetArg(3), GetArg(4), NULL, 1);
    menuon  (currentmenu(), currentwindow());
} /* do_subcheck */



/*****************************************************************************

    NAME
	do_subbar

    PARAMETER
	void

    RESULT
	-/-

    RETURN
	void

    DESCRIPTION
	[X]DME command interface for

    NOTES

    BUGS

    EXAMPLES

    SEE ALSO

    INTERNALS

    HISTORY
	20 Dec 1992 b_noll created

******************************************************************************/

void do_subbar (void)
{
    menuoff (currentmenu(), currentwindow());
    subadd  (currentmenu(), GetArg(1), GetArg(2), BAR, NULL, NULL, 0);
    menuon  (currentmenu(), currentwindow());
} /* do_subbar */



/*****************************************************************************

    NAME
	do_delitem

    PARAMETER
	void

    RESULT
	-/-

    RETURN
	void

    DESCRIPTION
	[X]DME command interface for

    NOTES

    BUGS

    EXAMPLES

    SEE ALSO

    INTERNALS

    HISTORY
	20 Dec 1992 b_noll created

******************************************************************************/

void do_delitem (void)
{
    menuoff (currentmenu(), currentwindow());
    itemdel (currentmenu(), GetArg(1), GetArg(2));
    menuon  (currentmenu(), currentwindow());
} /* do_delitem */



/*****************************************************************************

    NAME
	do_delitembar

    PARAMETER
	void

    RESULT
	-/-

    RETURN
	void

    DESCRIPTION
	[X]DME command interface for

    NOTES

    BUGS

    EXAMPLES

    SEE ALSO

    INTERNALS

    HISTORY
	22 Dec 1992 b_noll created

******************************************************************************/

void do_delitembar (void)
{
    menuoff (currentmenu(), currentwindow());
    itemdel (currentmenu(), GetArg(1), BAR);
    menuon  (currentmenu(), currentwindow());
} /* do_delitembar */



/*****************************************************************************

    NAME
	do_delsub

    PARAMETER
	void

    RESULT
	-/-

    RETURN
	void

    DESCRIPTION
	[X]DME command interface for

    NOTES

    BUGS

    EXAMPLES

    SEE ALSO

    INTERNALS

    HISTORY
	20 Dec 1992 b_noll created

******************************************************************************/

void do_delsub (void)
{
    menuoff (currentmenu(), currentwindow());
    subdel  (currentmenu(), GetArg(1), GetArg(2),GetArg(3));
    menuon  (currentmenu(), currentwindow());
} /* do_del */



/*****************************************************************************

    NAME
	do_delsubbar

    PARAMETER
	void

    RESULT
	-/-

    RETURN
	void

    DESCRIPTION
	[X]DME command interface for

    NOTES

    BUGS

    EXAMPLES

    SEE ALSO

    INTERNALS

    HISTORY
	22 Dec 1992 b_noll created

******************************************************************************/

void do_delsubbar (void)
{
    menuoff (currentmenu(), currentwindow());
    subdel  (currentmenu(), GetArg(1), GetArg(2), BAR);
    menuon  (currentmenu(), currentwindow());
} /* do_delsubbar */



/*****************************************************************************

    NAME
	do_menuclear

    PARAMETER
	void

    RESULT
	-/-

    RETURN
	void

    DESCRIPTION
	[X]DME command interface for

    NOTES

    BUGS

    EXAMPLES

    SEE ALSO

    INTERNALS

    HISTORY
	20 Dec 1992 b_noll created

******************************************************************************/

void do_menuclear (void)
{
    menuoff   (currentmenu(), currentwindow());
    menuclear (currentmenu());
    menuon    (currentmenu(), currentwindow());
} /* do_menuclear */



/*****************************************************************************

    NAME
	do_menuon

    PARAMETER
	void

    RESULT
	-/-

    RETURN
	void

    DESCRIPTION
	[X]DME command interface for

    NOTES

    BUGS

    EXAMPLES

    SEE ALSO

    INTERNALS

    HISTORY
	20 Dec 1992 b_noll created

******************************************************************************/

void do_menuon (void)
{
    menuon (currentmenu(), currentwindow());
} /* do_menuon */



/*****************************************************************************

    NAME
	do_menuoff

    PARAMETER
	void

    RESULT
	-/-

    RETURN
	void

    DESCRIPTION
	[X]DME command interface for

    NOTES

    BUGS

    EXAMPLES

    SEE ALSO

    INTERNALS

    HISTORY
	20 Dec 1992 b_noll created

******************************************************************************/

void do_menuoff (void)
{
    menuoff (currentmenu(), currentwindow());
} /* do_menuoff */



/*****************************************************************************

    NAME
	do_menusave

    PARAMETER
	void

    RESULT
	-/-

    RETURN
	void

    DESCRIPTION
	[X]DME command interface for

    NOTES

    BUGS

    EXAMPLES

    SEE ALSO

    INTERNALS

    HISTORY
	20 Dec 1992 b_noll created

******************************************************************************/

void do_menusave (void)
{
    menuoff   (currentmenu(), currentwindow());
    menusave  (currentmenu(), GetArg(1));
    menuon    (currentmenu(), currentwindow());
} /* do_menusave */



/*****************************************************************************

    NAME
	do_menuload

    PARAMETER
	void

    RESULT
	-/-

    RETURN
	void

    DESCRIPTION
	[X]DME command interface for

    NOTES

    BUGS

    EXAMPLES

    SEE ALSO

    INTERNALS

    HISTORY
	20 Dec 1992 b_noll created

******************************************************************************/

void do_menuload (void)
{
    menuoff   (currentmenu(), currentwindow());
    menuload  (currentmenu(), GetArg(1));
    menuon    (currentmenu(), currentwindow());
} /* do_menuload */



/*****************************************************************************

    NAME
	do_menutomacro

    PARAMETER
	char * str

    RESULT
	the command that is attached to the menu referred with str

    RETURN
	char *

    DESCRIPTION
	[X]DME vars interface for menu2macro

    NOTES
	that function was added 'cause in vars.c we use a
	table of all search-functions, and all the others
	do have only 1 argument

    BUGS
	never tested

    EXAMPLES

    SEE ALSO

    INTERNALS

    HISTORY
	22 Jan 1993 b_noll created

******************************************************************************/

char * menutomacro (char * str)
{
    return (menu2macro (currentmenu(), str));
} /* menutomacro */



/*****************************************************************************

    NAME
	do_getcheckitem

    PARAMETER
	void

    RESULT
	-/-

    RETURN
	void

    DESCRIPTION
	[X]DME vars interface for

    NOTES

    BUGS
	never tested

    EXAMPLES

    SEE ALSO

    INTERNALS

    HISTORY
	25 Jan 1993 b_noll created

******************************************************************************/

void do_getcheckitem (void)
{
    int  v;
    char vv[2] = "0";

    v = chkitemcheck (currentmenu(), GetArg(1), GetArg(2));

    if (v >= 0) {
	vv[0] = v+48;
	SetTypedVar (GetArg(3), vv, VAR_GV);
    } else {
	error ("%s:\n no check item of that name:\n %s-%s", CommandName(), GetArg(1), GetArg(2));
    } /* if */
} /* do_getcheckitem */



/*****************************************************************************

    NAME
	do_getchecksub

    PARAMETER
	void

    RESULT
	-/-

    RETURN
	void

    DESCRIPTION
	[X]DME vars interface for

    NOTES

    BUGS
	never tested

    EXAMPLES

    SEE ALSO

    INTERNALS

    HISTORY
	25 Jan 1993 b_noll created

******************************************************************************/

void do_getchecksub (void)
{
    int  v;
    char vv[2] = "0";

    v = chksubcheck (currentmenu(), GetArg(1), GetArg(2), GetArg(3));

    if (v >= 0) {
	vv[0] = v+48;
	SetTypedVar (GetArg(4), vv, VAR_GV);
    } else {
	error ("%s:\n no check sub item of that name:\n %s-%s-%s", CommandName(), GetArg(1), GetArg(2), GetArg(3));
    } /* if */
} /* do_getchecksub */



/*****************************************************************************

    NAME
	do_setcheckitem

    PARAMETER
	void

    RESULT
	-/-

    RETURN
	void

    DESCRIPTION
	[X]DME vars interface for

    NOTES

    BUGS
	never tested
	toggle wont work

    EXAMPLES

    SEE ALSO

    INTERNALS

    HISTORY
	25 Jan 1993 b_noll created

******************************************************************************/

void do_setcheckitem (void)
{
    int v;
    v = setitemcheck (currentmenu(), GetArg(1), GetArg(2), test_arg(GetArg(3),1));
    if (v == RET_FAIL) {
	error ("%s:\n no check item of that name:\n %s-%s", CommandName(), GetArg(1), GetArg(2));
    } else {
	menu_strip (currentmenu(), Ep->win);
    } /* if */
} /* do_setcheckitem */



/*****************************************************************************

    NAME
	do_setchecksub

    PARAMETER
	void

    RESULT
	-/-

    RETURN
	void

    DESCRIPTION
	[X]DME vars interface for

    NOTES

    BUGS
	never tested
	toggle wont work

    EXAMPLES

    SEE ALSO

    INTERNALS

    HISTORY
	25 Jan 1993 b_noll created

******************************************************************************/

void do_setchecksub (void)
{
    int v;
    v = setsubcheck (currentmenu(), GetArg(1), GetArg(2), GetArg(3), test_arg(GetArg(4),1));
    if (v == RET_FAIL) {
	error ("%s:\n no check sub item of that name:\n %s-%s-%s", CommandName(), GetArg(1), GetArg(2), GetArg(3));
    } else {
	menu_strip (currentmenu(), Ep->win);
    } /* if */
} /* do_setchecksub */


/*****************************************************************************

    NAME
	do_new_menustrip

    PARAMETER
	void

    RESULT
	-/-

    RETURN
	void

    DESCRIPTION
	command interface for keyaddes/qualifier

    NOTES
	<never tested>

    BUGS
	<none known>

    EXAMPLES

    SEE ALSO

    INTERNALS

    HISTORY
	28 Jan 1993  b_null created

******************************************************************************/

void do_new_menustrip (void)
{
    MENUSTRIP * ms = new_menustrip (GetArg(1), 1);

    if (ms) {
	/* menuoff (currentmenu(), Ep->win); */
	Ep->menustrip = ms;
	menu_strip (currentmenu(), Ep->win);
	/* menuon  (currentmenu(), Ep->win); */
    } /* if */
} /* do_new_menustrip */



/*****************************************************************************

    NAME
	do_del_menustrip

    PARAMETER
	void

    RESULT
	-/-

    RETURN
	void

    DESCRIPTION
	command interface for keyaddes/qualifier

    NOTES
	<never tested>
	that function is highly dangerous!


    BUGS
	<none known>

    EXAMPLES

    SEE ALSO

    INTERNALS

    HISTORY
	28 Jan 1993  b_null created

******************************************************************************/

void do_del_menustrip (void)
{
    MENUSTRIP * ms = currentmenu ();
    MENUSTRIP * ns;
    ED	      * ep;

    menuoff (ms, Ep->win);          /* remove all strips */

    delete_menustrip (ms, 0);
    ns = get_menustrip (NULL);

    for (ep = GetHead (&DBase); ep; ep = GetSucc (ep)) {
	if (ep->menustrip == ms) {
	    ep->menustrip = NULL; /* ns */
	} /* if */
    } /* for */

    menuoff (ns, Ep->win);          /* this is def_too != 0 since */
    menuon  (ns, Ep->win);          /* ns == get_menustrip(NULL)  */
} /* do_del_menustrip */



/*****************************************************************************

    NAME
	do_use_menustrip

    PARAMETER
	void

    RESULT
	-/-

    RETURN
	void

    DESCRIPTION
	command interface for keyaddes/qualifier

    NOTES
	<never tested>

    BUGS
	<none known>

    EXAMPLES

    SEE ALSO

    INTERNALS

    HISTORY
	28 Jan 1993  b_null created

******************************************************************************/

void do_use_menustrip (void)
{
    MENUSTRIP * ms = get_menustrip (GetArg (1));

    if (ms) {
	Ep->menustrip = ms;
	menu_strip (ms, Ep->win);
    } /* if */
} /* do_use_menustrip */






/******************************************************************************
*****  ENDE menucom.c
******************************************************************************/
