CS551 - Self-Study Exercises

Exercise 9: Keyboard Shortcuts, Actions, and Menus

This exercise introduces X's mechanisms for "overloading" callbacks with other input actions performing the same function; this allows the user to invoke the callback via keyboard shortcuts. The exercise also demonstrates the use of a useful but relatively unknown X application, xev.

Widget callbacks are rarely enough to support a full range of user interaction in a graphical interface. Action handlers are introduced, as well as the structuring of menus. Although the Athena widget set's support for menus is extremely rudimentary, it demonstrates how a gadget differs from a widget.

You should have read Chapter 8.1 and Chapter 13.1 - 13.2 in Nye and O'Reilly, Vol. 4, before starting this exercise.


  1. Create a new version of bitmaps, calling it shortcuts. Add an action handler so that typing a "q" or "Q" exits the program the same way selecting the Quit button does. Start by writing an action handler named QuitAction. It is not necessary to duplicate the actions of your Quit callback. Instead, invoke the routine, XtCallCallbacks, to simulate user selection of the quit_button widget (see Vol. 5 for its description).

    Why must QuitAction() be added to the forward declarations for handlers at the beginning of the program?

  2. Now create and install an XtActionsRec table specifying that when the translation table shows the string "quit", your action handler should be called. Then add the translations table information to the defaults file, specifying that the translation should be activated whenever the user types the letter "q" or "Q" anywhere in the application window.

    What is the minimum number of translations needed so that the action will work regardless of whether the user types upper or lower case? Show them.

  3. Note that the scope of the action is the entire application.

    How would the performance have been affected if the call to XtCallCallbacks had used w (the widget where the event was detected) instead of quit_button?

  4. Experiment with the X client xev, which displays information on each event processed by the server. Note the events that always occur before the window is even popped up by the window manager. Compare them with those which occur as the window is popped up. Use xev to determine the symbolic names for the following keys (do either one, but you must use an HP or a Sun keyboard):
                      HP                               Sun
    Menu                           Help
    Prev                           Alt
    Extend char                    Alt Graph
    Next                           F1
    
    What happens when you hold a key down instead of releasing it immediately?

  5. Many interfaces use the convention that pressing is the same thing as confirming a choice.

    How could you modify your popup to do this?

  6. Create a new version of shortcuts, calling it menus. Start by adding an action routine which is activated whenever the third (rightmost) button is clicked within the application.

    Does it interfere with the callbacks you have established for some of the widgets? Why or why not?

    Have the action routine print a message indicating the x and y coordinates where the click occurs.

    Are these measured in terms of the root window, or the window where the event occurred?

  7. Now use the SimpleMenu widget class (described in Chapter 13.2.4) to create a drop-down menu for the that will replace the application's two buttons.

    What factors would influence your decision to use buttons or menus in supporting interface features?

    Remove the buttons, replacing them by a single MenuButton that offers access to the two features. The next step is to create a widget of type SimpleMenu, making it a child of the MenuButton. Then you add the menu items; for the first try, just add the Quit capability by creating a single item of SmeBSBObjectClass and hooking it to your callback routine.

    How can you associate a keyboard shortcut with a menu item? How would you indicate to the user that the feature is available?

  8. Once that works, add the second menu item. Experiment with using menu dividers and multiple menu items.

    Suppose you had to code your own "popup menu" in the form of a popup box containing a button for each menu item. How many widgets, callbacks, and calls to Xt routines do you save by using SimpleMenu instead of building your own popup?