kochhar@endor.harvard.edu (Sandeep Kochhar) (08/17/89)
Hi! since I've received a few requests for accelerator examples, here's an example of using accelerators -- it's a complete program that shows how to modify the athena dialog widget to accept a carriage return as equivalent to the first button. Note: 1. Thanks to other people who earlier provided me examples. 2. The program below just needs to be compiled with -lXaw -lXt -lXmu -lX11 3. The code was extracted from another program; so it's probably not particulary elegant... Thanks. ------------------cut here-------------------------- /* An example of using accelerators */ /* This example takes the Athena Dialog Wiget and adds */ /* accelerators to allow a carriage return anywhere in the */ /* dialog box to be equivalent to clicking on the first button */ /* -- sandeep kochhar */ #include <stdio.h> #include <X11/Intrinsic.h> #include <X11/Cardinals.h> #include <X11/StringDefs.h> #include <X11/Shell.h> #include <X11/Dialog.h> #include <X11/IntrinsicP.h> #include <X11/CommandP.h> #include <X11/Command.h> #define STR_LEN 80 static Display *display; /* for drawing etc. */ #define MAX_ARGS 5 static Arg args[MAX_ARGS]; static char buf[80]; static Widget Gr_toplevel; static Widget Gr_dial; static Widget Gr_ok; static Widget Gr_canceled; static Widget dial_value; /* label child of dialog */ static void dial_selected(); /* forward declarations */ static void dial_canceled(); /* forward declarations */ /* layout info */ void init_gr() { int arg_count = 0; String src = "#override \n<Key>Return: set() notify() unset()\n\ <Key>Linefeed: set() notify() unset()"; String dest = "<Key>Return: \n <Key>Linefeed: \n"; strcpy(buf, "hello"); Gr_toplevel = XtCreateApplicationShell("datasurf", topLevelShellWidgetClass, NULL, ZERO); XtSetArg(args[arg_count], XtNlabel, "Enter Value:"); arg_count++; XtSetArg(args[arg_count], XtNvalue, buf); arg_count++; XtSetArg(args[arg_count], XtNmaximumLength, 79); arg_count++; Gr_dial = XtCreateManagedWidget("dial", dialogWidgetClass, Gr_toplevel, args, arg_count); XtOverrideTranslations(Gr_dial, XtParseTranslationTable(dest)); arg_count = 0; XtSetArg(args[arg_count], XtNborderWidth, 2); arg_count++; XtSetArg(args[arg_count], XtNaccelerators, XtParseAcceleratorTable(src)); arg_count++;; Gr_ok = XtCreateManagedWidget("ok", commandWidgetClass, Gr_dial, args, arg_count); XtAddCallback(Gr_ok, XtNcallback, dial_selected, NULL); arg_count = 0; Gr_canceled = XtCreateManagedWidget("canceled", commandWidgetClass, Gr_dial, args, arg_count); XtAddCallback(Gr_canceled, XtNcallback, dial_canceled, NULL); /* adding new stuff for newline etc */ dial_value = XtNameToWidget(Gr_dial, "value"); if (!dial_value) { printf("Dial_value Oops!\n"); exit(1); } arg_count = 0; XtSetArg(args[arg_count], XtNvalue, "foobar"); arg_count++; XtSetValues(dial_value, args, arg_count); XtOverrideTranslations(dial_value, XtParseTranslationTable(dest)); XtInstallAccelerators(Gr_dial, Gr_ok); XtInstallAccelerators(dial_value, Gr_ok); XtRealizeWidget(Gr_toplevel); display = XtDisplay(Gr_toplevel); } static void enter_dialog(); /* adding the newline actions to the dialog widgets */ static add_nl_action(w) Widget w; { static XtActionsRec actions[] = { {"enter_dialog", enter_dialog}, }; static char txln[STR_LEN]; static XtTranslations xtxln; XtAddActions(actions, XtNumber(actions)); sprintf(txln, "<Key>Return: enter_dialog()"); xtxln = XtParseTranslationTable(txln); XtOverrideTranslations(w, xtxln); sprintf(txln, "<Key>Linefeed: enter_dialog()"); xtxln = XtParseTranslationTable(txln); XtOverrideTranslations(w, xtxln); } static add_nl_accelerator(src, dest) Widget dest, src; { static String source = "<Key>Return: enter_dialog()"; XtParseAcceleratorTable(source); XtInstallAccelerators(dest, src); } /* user has hit enter */ static void enter_dialog(widget, xev, params, num_params) Widget widget; XEvent *xev; String *params; Cardinal *num_params; { printf("Return Hit!\n"); printf("Dial selected: %s\n", XtDialogGetValueString(Gr_dial)); exit(0); } /* ARGSUSED */ static void dial_selected(widget, client_data, call_data) Widget widget; caddr_t client_data; caddr_t call_data; { printf("Dial selected: %s\n", buf); printf("Dial selected: %s\n", XtDialogGetValueString(Gr_dial)); XFlush(display); usleep(100000); return; } /* ARGSUSED */ static void dial_canceled(widget, client_data, call_data) Widget widget; caddr_t client_data; caddr_t call_data; { printf("Dial canceled: %s\n", XtDialogGetValueString(Gr_dial)); exit(0); } /* the top level controller for the entire system */ main(argc, argv) unsigned int argc; char **argv; { static Widget toplevel; toplevel = XtInitialize("main", "Demo", NULL, ZERO, &argc, argv); init_gr(); XtMainLoop(); } ---------------------cut here---------------------------- Sandeep Kochhar (617) 495-9515 mail: kochhar@harvard.harvard.edu Harvard University kochhar@harvard.csnet 33 Oxford st, kochhar@harvard.uucp Cambridge, Ma 02138 kochhar@harvard.bitnet "If you didn't get this message, please let me know."