Next: PullDown Menus Up: The MenuBar Previous: The MenuBar

A simple MenuBar

The creation of a fully functional pulldown MenuBar typical of most applications is fairly complex. So we will break it down into two stages.

Firstly we will look at how we create a simple MenuBar that lets us select actions directly from the bar. Having achieved this we will see how to attach pulldown menus.

A MenuBar widget is really a RowColumn widget under another name. The way we create a MenuBar is to use one of the XmCreate or XtVaCreate Menu options. There are several of them.

We then attach some CascadeButton widgets as the application requires.

Fig. 5.1 shows the output of the menu_cascade.c program that simply creates a simple MenuBar widget and attaches two CascadeButton widgets - help and quit.

The full program listing of menu_cascade.c is:


#include <Xm/Xm.h>
#include <Xm/MainW.h>
#include <Xm/CascadeB.h>

void quit_call(), help_call(); /* callbacks */

main(int argc, char *argv[])

{   Widget top_wid, main_win, menu_bar, quit, help;
    XtAppContext app;
    
    /* create application, main and menubar widgets */
    
    top_wid = XtVaAppInitialize(&app, "menu_cascade",
        NULL, 0, &argc, argv, NULL, NULL);

    main_win = XtVaCreateManagedWidget("main_window",
        xmMainWindowWidgetClass,   top_wid,
        NULL);

    menu_bar = XmCreateMenuBar(main_win, "main_list", 
        NULL, 0);        
    XtManageChild(menu_bar);

    
     /* create quit widget + callback */
        
    quit = XtVaCreateManagedWidget( "Quit",
        xmCascadeButtonWidgetClass, menu_bar,
        XmNmnemonic, 'Q',
        NULL);
        
    
    XtAddCallback(quit, XmNactivateCallback, quit_call, NULL);

    
    /* create help widget + callback */
        
    help = XtVaCreateManagedWidget( "Help",
        xmCascadeButtonWidgetClass, menu_bar,
        XmNmnemonic, 'H',
        NULL);
        
    
    XtAddCallback(help, XmNactivateCallback, help_call, NULL);
    
        
    XtRealizeWidget(top_wid);
    XtAppMainLoop(app);
}


void quit_call()

{   printf("Quitting program\n");
    exit(0);
}

void help_call()

{   printf("Sorry, I'm Not Much Help\n");
}

The first steps of the main function should now be familiar. We initialise the application and create the top_wid top level widget.

We then create a MainWindow widget, main_win.

We also create a MenuBar, menu_bar, widget as a child of main_win.

We then attach two child CascadeButton widgets, quit and help.

A CascadeButton widget is usually used to hold the name of a pulldown menu on the MenuBar. This widget is similar to the PushButton widget and can have call back functions attached to its activateCallback function.

However its main use to link a menu bar with a menu.

The program above simply attaches callback functions to the CascadeButtons. The callback functions do not do much except quit the program and print to standard output.

You can also associate a mnemonic to a particular menu selection. This means that you can use ``hot keys'' on the keyboard as a short cut to selection. You need to press Meta key plus the key in question.

In this program, we allow Meta-Q and Meta-H for the selection of Quit and Help. Note that Motif displays the appropriate Meta key by underlining the letter concerned on the menu bar (or menu item).



Next: PullDown Menus Up: The MenuBar Previous: The MenuBar


Dave.Marshall@cm.cf.ac.uk
Tue May 24 16:52:56 BST 1994