/*
 *  ADD (add keyboard shortcuts to menus)
 *
 *  Copyright ((c)) 1987, John Russell
 *  This program is freely redistributable. Nobody may impose extra
 *  limitations on its distribution.
 *
 *  Please distribute at least documentation with the executable. The
 *  source is straightforward, but could be informative for those who get
 *  blurred vision from reading those structure definitions in intuition.h.
 *
 *  This program is shareware, and if you feel that it is useful to you
 *  (I *know* it is to me :-) a small contribution would be appreciated
 *  (see documentation).
 */

/* Manx users compile with "+l" just in case */

#include <intuition/intuition.h>
#include <stdio.h>

/* include all the standard stuff for _intuition_ */

struct IntuitionBase *IntuitionBase;

main(argc,argv)
int argc;
char *argv[];
{
    struct Screen *screen;
    struct Window *window;
    struct MenuItem *item;
    struct Menu *menu,*main_menu;
    char answer,*gets();
    int x,cut;

    cut = argc-1; /* *argv[cut] == key to assign */

    if ((IntuitionBase=(struct IntuitionBase *)
        OpenLibrary("intuition.library",0L))==NULL) {
            puts("Where is Intuition gone???\n");
            exit(0);
    }



    Forbid(); /* the following line was outside the Forbid-Permit loop in the
posted executable, but should be inside */

    screen = IntuitionBase->FirstScreen; /* address of Workbench screen 
structure */

    while (screen != NULL) {     /* do all screens */
        window = screen->FirstWindow;
        while (window != NULL) {    /* do all windows */
            if (compare(argv[1],window->Title)==0) {  /* search for name */

                x=atoi(argv[2]);  /* menu # */

                menu = window->MenuStrip;
                main_menu = menu; /* main menu of window for SetMenuStrip */

                while (--x > 0) {  /* scan menu list */
                    if ((menu = menu->NextMenu) == NULL)
                        goto quit;
                }

                x = atoi(argv[3]);  /* item # */
                item = menu->FirstItem;
submenu:
                while (--x > 0) {  /* scan item list */
                    if ((item = item->NextItem) == NULL)
                        goto quit;
                }

                if (argc==6) { /* descend into sub-menu? */
                    argc=0;
                    item=item->SubItem;
                    x=atoi(argv[4]); /* sub-item # */
                    goto submenu;
                }

                ClearMenuStrip(window); /* disable menu structure */

                if (!compare(argv[cut],"-kill")) {
                    item->Flags &= -(COMMSEQ+1); /* turn off bit */
                    puts("Deleted shortcut.");
                }
                else {
                    item->Flags |= COMMSEQ; /* add shortcut flag */
                    item->Command = *argv[cut];  /* which key */
                    puts("Added shortcut."); /* took out stray newline */
                }

                SetMenuStrip(window,main_menu); /* give it back */
            }
        window = window->NextWindow;
        }
    screen = screen->NextScreen;
    }

    quit:
    Permit();
    CloseLibrary(IntuitionBase);

}

compare(string1,string2) /* if spaces in windowname, only check first word */
char *string1,*string2;
{
    while ((*string1 != '\0') && (*string1 != ' ')) {
        if (*string1 != *string2) /* space and null both end conditions */
            return(1);
        string1++;
        string2++;
    }
    return(0);  /* return weird values like strcmp() */
}
