[comp.windows.x] keyboard focus

cheung@marble.sw.mcc.com (Po Cheung) (08/08/89)

I have a simple program that pops up an asciiString widget for
a user to enter text.  The program works ok under twm on a
Sun 3.  However, when I run the same program on a DECstation 3100
running Ultrix 2.0 and the DECwindow manager, the popupshell loses 
the keyboard focus, and I cannot enter text at the asciiString widget.

Would some kind soul please tell me what am I doing wrong?

Thank you!

Po Cheung (po@sw.mcc.com)

-----------------------------------------------------------------------------
#include <stdio.h>  
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#include <X11/Cardinals.h>
#include <X11/Shell.h>
#include <X11/AsciiText.h>
#include <X11/TextP.h>
#include <X11/Form.h>
#include <X11/Box.h>
#include <X11/Command.h>

#define MAXARGS 20

Widget 	toplevel, button, popupshell, frame, quit_button, text;
void 	callback(), set_up(), quit_proc();

Arg	args[MAXARGS];
Cardinal n;
char 	buffer[32];

void main(argc, argv) 
    int  argc;
    char **argv;
{ 
    toplevel = XtInitialize("toplevel", "XDemo", NULL, 0, &argc, argv);
    button = XtCreateManagedWidget("Button", commandWidgetClass, toplevel, 
	args, n);
    XtAddCallback(button, XtNcallback, callback, NULL);
    set_up();

    XtRealizeWidget(toplevel);
    XtMainLoop();
}

static void callback(w, client_data, call_data)
    Widget 	w;
    caddr_t 	client_data;
    caddr_t 	call_data;
{
    XtPopup(popupshell, XtGrabNone);
}

static void set_up()
{
    n = 0;
    popupshell = XtCreatePopupShell("popupshell", 
	transientShellWidgetClass, toplevel, args, n);

    n = 0;
    frame = XtCreateManagedWidget("frame", formWidgetClass,
	popupshell, args, n);

    n = 0;
    quit_button = XtCreateManagedWidget("QUIT", commandWidgetClass,
	frame, args, n);
    XtAddCallback(quit_button, XtNcallback, quit_proc, NULL);

    n = 0;
    XtSetArg(args[n], XtNeditType, XttextEdit);			n++;
    XtSetArg(args[n], XtNstring, buffer);			n++;
    XtSetArg(args[n], XtNlength, 32);				n++;
    XtSetArg(args[n], XtNfromVert, quit_button);		n++;
    text = XtCreateManagedWidget("", asciiStringWidgetClass, frame, args, n);
    XtSetKeyboardFocus(frame, text);
}

static void quit_proc(w, client_data, call_data)
    Widget w;
    caddr_t client_data;
    caddr_t call_data;
{
    XtPopdown(XtParent(XtParent(w)));
    XtDestroyWidget(toplevel);
    exit(0);
}

argv%eureka@Sun.COM (Dan Heller) (08/08/89)

In article <2749@marble.sw.mcc.com> cheung@marble.sw.mcc.com (Po Cheung) writes:
> I have a simple program that pops up an asciiString widget for
> a user to enter text.  The program works ok under twm on a
                                                    ^^^
> Sun 3.  However, when I run the same program on a DECstation 3100
> running Ultrix 2.0 and the DECwindow manager, the popupshell loses 
                             ^^^^^^^^^^^^^^^^^
> the keyboard focus, and I cannot enter text at the asciiString widget.

I don't know this for sure, but I -suspect- that it's the window manager.
The widget needs to get keyboard input focus from the window manager and
widgets, by default, do not set keyboard focus.  You need to add to your
widget in one of various methods, XtNinput to be True.  One way is simply
for the application to set it:
    XtSetArg(arg, XtNinput, True);

Explanation: ICCCM window managers don't give keyboard focus to windows
unless they ask for it.  It would seem to "make sense" for a "toolkit"
which provides window manager communication to handle this for the
programmer.  However, the athena toolkit makes no such provisions.  You
don't have this problem with TWM probably it's not ICCCM compliant (at
least in this case and not in the version you're using).  twm is allowing
the window to have input focus despite the fact that the window didn't
ask for it.

Soap box:
I personally feel that it extends beyond the toolkit and into the domain
of the Intrinsics to handle this functionality.  It should be part of the
intrinsics itself to have smarts about whether or not a widget is intended
to get focus.  It can easily be coded to check for the existence of a
translation table and if the widget in question has a keyboard/mouse type
action in the action list.  if so, set the input focus automatically --
otherwise, leave it up to the individual widgets to set it accordingly.

I feel that since the intrinsics handle all other window manager communi-
cations like this, it sets prcedent that the intrinsics should also handle
input focus.

dan <island!argv@sun.com>
-----
My postings reflect my opinion only -- not the opinion of any company.

gustav@arp.anu.OZ.AU (11/11/89)

Dear Xpert,

I have a problem with the keyboard focus in my application. What happens is
as follows:
   On pressing a button in the main "control panel" of my application
   (this "control panel" is also the top level shell of the application
   created by XtInitialize) a new window appears which consists of two 
   dialog boxes, some lists, and a few command widgets - one of them 
   being "close window". There are no problems at this stage with trasferring 
   the keyboard focus between various dialog boxes of the application and 
   between its various top level windows. The focus is given to the window 
   automatically when the cursor enters the area of the window. However, 
   when I try to close the window by pressing the "close window" command 
   button, the window takes the keyboard focus with it, and it cannot be 
   transferred to the main command panel, unless the pop-up window is popped 
   up again.  The popup is popped up using XtPopup (popup_shell, XtGrabNone), 
   and then it is popped down using XtPopdown (popup_shell). The popup_shell 
   itself is a child of the top level shell. The application has been written 
   using Athena Widgets, X11/R3, vanilla distribution.  The widgets used in 
   the offending windows are dialog widget, command widget, list widget, and 
   label widget.  I've been experimenting with various kinds of "grabs" but 
   to no avail.

   Best regards,

   Gustav Meglicki,
   Automated Reasoning Project
   Research School of Social Sciences
   Australian National University,
   GPO Box 4, Canberra, ACT, 2601, Australia

   gustav@arp.anu.oz.au