[comp.windows.x] How do accelerators work?

mansfiel@scrsu1.sdr.slb.COM (Niall Mansfield) (01/26/89)

How do accelerators work?

Can anyone give me a real example of how to use one in real life, i.e. "put
this into a resource spec, and then when you press ... you will get ..."?

(I've read the various docs until I'm blue in the face, and sill have no
idea.  Searching thro' the sources didn't help much either - as far as I
could see nothing uses them at all).

Thanks,

Niall

swick@ATHENA.MIT.EDU (Ralph R. Swick) (01/26/89)

> Can anyone give me a real example of how to use one in real life,

Apply the modification (patch) at the end of this message to
x11r3/examples/Xaw/xboxes.c and add the following resource to
your favorite resource file:

	Demo*quit.accelerators: Ctrl<Key>c: set() notify()

then notice that ^C anywhere in the top-level window activates the
'quit' button.

*** /u1/X/src/examples/Xaw/xboxes.c
--- f.c
***************
*** 54,59 ****
--- 54,60 ----
      char **argv;
  {
      Widget viewport, box, box1, box2, box3;
+     Widget q;
  
      static XtCallbackRec callbackList[] = { {callback, NULL}, {NULL, NULL} };
      static Arg viewArgs[] = { {XtNallowVert, True}, {XtNallowHoriz, True} };
***************
*** 94,103 ****
--- 95,106 ----
      XtCreateManagedWidget("label2", labelWidgetClass, box2, NULL, ZERO);
      XtCreateManagedWidget("label3", labelWidgetClass, box3, NULL, ZERO);
  
+     q =
      XtCreateManagedWidget("quit", commandWidgetClass, box1, arg, ONE);
      XtCreateManagedWidget("command2", commandWidgetClass, box2, NULL, ZERO);
      XtCreateManagedWidget("command3", commandWidgetClass, box3, NULL, ZERO);
      
+     XtInstallAccelerators(toplevel, q);
      XtRealizeWidget(toplevel);
      XtMainLoop();
  }

asente@decwrl.dec.com (Paul Asente) (01/28/89)

In article <8901252034.AA15857@ATHENA.MIT.EDU> mansfiel@scrsu1.sdr.slb.COM (Niall Mansfield) writes:
>
>How do accelerators work?
>
>Can anyone give me a real example of how to use one in real life, i.e. "put
>this into a resource spec, and then when you press ... you will get ..."?
>
>(I've read the various docs until I'm blue in the face, and sill have no
>idea.  

That's what you get for trying to understand how to use something based
upon the specification.  Now, if only someone would write a user's guide...

But seriously.

1)  Pick some widget that you want to define an accelerator for.  Let's
say you pick the findFile widget in your favorite text editor.

2)  Pick the accelerator sequence you want for this widget.  Let's say you
want control-x control-v.

3)  Find out (from looking in the documentation, if there is any!) what
the name of the appropriate action procedure in the widget you want to
accelerate is. Let's say the findFile widget is of class MenuEntry and has
an action procedure Notify that does what you want.

4)  Manage somehow to get the accelerators resource set for your widget.
The best way is in an .Xdefaults file, or, if you are writing an
application and want some accelerators by default, in the applications
default file.  The value of the resource should be a translation table
with your accelerator sequence on the left, and the action procedure on
the right:

myProgram*findFile.accelerators: Ctrl<Key>x, Ctrl<Key>v : Notify()

5)  Now you've specified what the accelerator should be.  You need to
inform the intrinsics where the accelerator is valid.  Conscientious
applications will do this already, but if it doesn't, you'll have to
modify the program, adding a call to XtInstallAccelerator.

6)  XtInstallAccelerator(destWidget, srcWidget) tells the intrinsics to
find the accelerator for srcWidget and make it work if the left hand side
of the translation table is activated in destWidget.  Note that the
parameters are widget ids, not widget names.

7)  More useful is XtInstallAllAccelators(destWidget, srcWidget).  This
tells the intrinsics to look for all descendents of srcWidget, both popup
and normal, and install all of their accelerators in destWidget.  A common
use is to install all the accelerators in the whole application, by
specifying the application shell as the srcWidget, into some destination.

Caveat:  if you happen to be using the DEC widget set (XUI) it does
accelerators slightly differently; check the documentation.

Hope this helps.

	-paul asente
	    asente@decwrl.dec.com	decwrl!asente