Path: utzoo!attcan!uunet!mcvax!unido!laura!exunido!v40f00 From: v40f00@exunido.uucp (Franz-Josef Stewing) Newsgroups: comp.windows.x Subject: Re: How do I create popup menus with Xt? Message-ID: <509@laura.UUCP> Date: 26 Jul 88 13:24:17 GMT References: <6005@spool.cs.wisc.edu> <8807191717.AA00552@LYRE.MIT.EDU> Sender: root@laura.UUCP Reply-To: v40f00@exunido.UUCP (Franz-Josef Stewing) Organization: University of Dortmund, W-Germany Lines: 181 Hello, Let us add some more on the guessing about how to create popup menus with Xt: With the help of the notes by Marvin Solomon, H.David Scarbro and Robert Blumen we found following solution: ===================================================================== #include #include #include #include #include #include #include #include /* Command line options table. Only resources are entered here...there is a pass over the remaining options after XtParseCommand is let loose. */ static XrmOptionDescRec options[] = { {"-label", "*label", XrmoptionSepArg, NULL} }; Widget toplevel, menu_bar, button, popupshell, menu, popupbutton; /* * Report the syntax for calling xlabel */ Syntax(call) char *call; { fprintf( stderr, "Usage: %s\n", call ); } /* ARGSUSED */ void callback(w, str, dummy) Widget w; char *str; int dummy; { printf("%s button\n", str); fflush(stdout); } /* ARGSUSED */ void done(w, str, dummy) Widget w; char *str; int dummy; { exit(0); } /* ARGSUSED */ void set_pos(w, event, params, num_params) Widget w; XEvent *event; String params; Cardinal *num_params; { Position x, y; Arg args[2]; Window win; XTranslateCoordinates(XtDisplay(w), XtWindow(w), XDefaultRootWindow(XtDisplay(w)), event->xbutton.x, event->xbutton.y, &x, &y, &win); XtMoveWidget(popupshell, x, y); } void main(argc, argv) unsigned int argc; char **argv; { XtTranslations button_actions, popup_actions, popdown_actions; Arg arg; static XtActionsRec action_table[] = { {"set_pos", (XtActionProc)set_pos} }; XtAddActions(action_table, ONE); toplevel = XtInitialize( "popup_test", "XPopupTest", options, XtNumber(options), &argc, argv ); if (argc != 1) Syntax(argv[0]); button_actions = XtParseTranslationTable( ": highlight() set()\n"); popup_actions = XtParseTranslationTable( ": set() set_pos() MenuPopup(menu)\n"); popdown_actions = XtParseTranslationTable( ": MenuPopdown()\n"); /**** menu bar ****/ menu_bar = XtCreateManagedWidget( "menu_bar", boxWidgetClass, toplevel, (Arg *)0, ZERO); /**** menu bar items ****/ button = XtCreateManagedWidget( "yes", commandWidgetClass, menu_bar, (Arg *)0, ZERO ); XtAddCallback(button, XtNcallback, callback, (caddr_t)"yes"); popupbutton = XtCreateManagedWidget( "ShowMenu", commandWidgetClass, menu_bar, (Arg *)0, ZERO ); XtOverrideTranslations(popupbutton, popup_actions); /**** popup menu ****/ popupshell = XtCreatePopupShell("menu", overrideShellWidgetClass, toplevel, (Arg *)0, ZERO); XtOverrideTranslations(popupshell, popdown_actions); menu = XtCreateManagedWidget( "PopupMenu", boxWidgetClass, popupshell, (Arg *)0, ZERO); /**** popup menu items ****/ /* menu option 1 */ button = XtCreateManagedWidget( "choice1", commandWidgetClass, menu, (Arg *)0, ZERO ); XtAddCallback(button, XtNcallback, callback, (caddr_t)"button1"); XtOverrideTranslations(button, button_actions); /* menu option 2 */ button = XtCreateManagedWidget( "choice2", commandWidgetClass, menu, (Arg *)0, ZERO ); XtAddCallback(button, XtNcallback, callback, (caddr_t)"button2"); XtOverrideTranslations(button, button_actions); /* menu option 3 */ button = XtCreateManagedWidget( "quit", commandWidgetClass, menu, (Arg *)0, ZERO ); XtAddCallback(button, XtNcallback, done, (caddr_t)"done"); XtOverrideTranslations(button, button_actions); XtRealizeWidget(toplevel); XtMainLoop(); } ========================================================================== The main changes to Marvin's second program were - changing the Callback function "set_menu_position" to the function "set_pos" which was added to a translation table and - using XtMoveWidget to let the menu popup at an appropiate place. This programm should work properly, i.e. let menus disappear when releasing the button _and_ appear where you expect them to. (Of course only with the change to the Toolkit function "XtConvertStringToBoolean" which David Scarbro suggested) Since we are rather new at the Toolkit (and didn't understand Chapter 7) we expect our solution a bit clumsy and we hope for some suggestions how to improve it. Lutz Wiggershaus University of Dortmund, Informatics IV. email: wiggersh@balduin.ls4.informatik.uni-dortmund.de