summaryrefslogtreecommitdiff
path: root/xc/lib/Xt/Keyboard.c
blob: f57204d6528a0b4122d5a4b11ce9a86627dd46df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
/* $TOG: Keyboard.c /main/37 1998/02/06 13:23:52 kaleb $ */

/********************************************************

Copyright 1988 by Hewlett-Packard Company
Copyright 1987, 1988, 1989 by Digital Equipment Corporation, Maynard, Massachusetts
Copyright 1993 by Sun Microsystems, Inc. Mountain View, CA.

Permission to use, copy, modify, and distribute this software 
and its documentation for any purpose and without fee is hereby 
granted, provided that the above copyright notice appear in all 
copies and that both that copyright notice and this permission 
notice appear in supporting documentation, and that the names of 
Hewlett-Packard, Digital, or Sun not be used in advertising or 
publicity pertaining to distribution of the software without specific, 
written prior permission.

DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

SUN DISCLAIMS ALL WARRANTIES WITH REGARD TO  THIS  SOFTWARE,
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-
NESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SUN BE  LI-
ABLE  FOR  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,  DATA  OR
PROFITS,  WHETHER  IN  AN  ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
THE USE OR PERFORMANCE OF THIS SOFTWARE.

********************************************************/

/*

Copyright 1987, 1988, 1994, 1998  The Open Group

All Rights Reserved.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.

*/

#include "IntrinsicI.h"

#include "PassivGraI.h"

#define _GetWindowedAncestor(w) (XtIsWidget(w) ? w : _XtWindowedAncestor(w))
extern void _XtFillAncestorList();
extern void _XtSendFocusEvent();

/* InActiveSubtree cache of the current focus source and its ancestors */
static Widget	*pathTrace = NULL;
static int	pathTraceDepth = 0;
static int	pathTraceMax = 0;

/* FindKeyDestination cache of focus destination and ancestors up to source */
static Widget	*pseudoTrace = NULL;
static int	pseudoTraceDepth = 0;
static int	pseudoTraceMax = 0;

void _XtClearAncestorCache(widget)
    Widget widget;
{
    /* the caller must lock the process lock */
    if (pathTraceDepth && pathTrace[0] == widget)
	pathTraceDepth = 0;
}

static XtServerGrabPtr CheckServerGrabs(event, trace, traceDepth)
    XEvent	*event;
    Widget	*trace;
    Cardinal	traceDepth;
{
    XtServerGrabPtr 	grab;
    Cardinal		i;

    for (i = traceDepth;  i > 0; i--)
      {
	 if (grab = _XtCheckServerGrabsOnWidget(event, trace[i-1], KEYBOARD))
	   return (grab);
     }
    return (XtServerGrabPtr)0;
}

static Boolean IsParent(a, b)
    Widget	a, b;
{
    for (b = XtParent(b); b; b = XtParent(b)) {
      if (b == a) return TRUE;
      if (XtIsShell(b)) return FALSE;
    }
    return FALSE;
}

#define RelRtn(lca, type) {*relTypeRtn = type; return lca;}

static Widget CommonAncestor(a, b, relTypeRtn)
    register Widget a, b;
    XtGeneology  * relTypeRtn;
{
    if (a == b) 
      {
	  RelRtn(a, XtMySelf)
	}
    else if (IsParent(a, b))
      {
	  RelRtn(a, XtMyAncestor)
	}
    else if (IsParent(b, a))
      {
	  RelRtn(b, XtMyDescendant)
	}
    else
      for (b = XtParent(b);
	   b && !XtIsShell(b); 
	   b = XtParent(b))
	if (IsParent(b, a)) 
	  {
	      RelRtn(b, XtMyCousin)
	    }
    RelRtn(NULL, XtUnrelated)
}
#undef RelRtn

	  
    


static Widget _FindFocusWidget(widget, trace, traceDepth, activeCheck, isTarget)
    Widget 	widget;
    Widget	*trace;
    int		traceDepth;
    Boolean	activeCheck;
    Boolean	*isTarget;
{
    int src;
    Widget dst;
    XtPerWidgetInput	pwi = NULL;
    
    /* For each ancestor, starting at the top, see if it's forwarded */


    /* first check the trace list till done or we go to branch */
    for (src = traceDepth-1, dst = widget; src > 0;)
      {
	  if (pwi = _XtGetPerWidgetInput(trace[src], FALSE))
	    {
		if (pwi->focusKid)
		  {
		      dst = pwi->focusKid;
		      for (src--; src > 0 && trace[src] != dst; src--) {}
		  }
		else dst = trace[--src];
	    }
	  else dst = trace[--src];
      }

    if (isTarget) {
	if (pwi && pwi->focusKid == widget)
	    *isTarget = TRUE;
	else
	    *isTarget = FALSE;
    }

    if (!activeCheck)
      while (XtIsWidget(dst)
	     && (pwi = _XtGetPerWidgetInput(dst, FALSE))
	     && pwi->focusKid)
	dst = pwi->focusKid;

    return dst;
}


static Widget FindFocusWidget(widget, pdi)
    Widget		widget;
    XtPerDisplayInput 	pdi; 
{
    if (pdi->focusWidget) 
      return  pdi->focusWidget;
    else
      return _FindFocusWidget(widget, pdi->trace, pdi->traceDepth, FALSE, NULL);
}

Widget XtGetKeyboardFocusWidget(widget)
    Widget widget;
{
    XtPerDisplayInput pdi;
    Widget retval;
    WIDGET_TO_APPCON(widget);

    LOCK_APP(app);
    pdi = _XtGetPerDisplayInput(XtDisplay(widget));
    retval = FindFocusWidget(widget, pdi);
    UNLOCK_APP(app);
    return retval;
}

static Boolean IsOutside(e, w) 
    XKeyEvent	*e;
    Widget	w;
{
    Position left, right, top, bottom;
    
    /*
     * if the pointer is outside the shell or inside
     * the window try to see if it would recieve the
     * focus 
     */
    XtTranslateCoords(w, 0, 0, &left, &top);
    /* We need to take borders into consideration */
    left = left - w->core.border_width;
    top =  top - w->core.border_width;
    right = left +  w->core.width + w->core.border_width;
    bottom = top +  w->core.height + w->core.border_width;

    if (
	(e->x_root < left) || (e->y_root < top) ||
        (e->x_root > right) || (e->y_root > bottom))
      return TRUE;
    else
      return FALSE;
}

static Widget 	FindKeyDestination(widget, event,
				   prevGrab, prevGrabType,
				   devGrab, devGrabType,
				   pdi)
    Widget		widget;
    XKeyEvent		*event;
    XtServerGrabPtr	prevGrab;
    XtServerGrabType 	prevGrabType;
    XtServerGrabPtr	devGrab;
    XtServerGrabType	devGrabType;
    XtPerDisplayInput	pdi;
{
    
    Widget		dspWidget;
    Widget		focusWidget;

    LOCK_PROCESS;
    dspWidget = 
      focusWidget = 
	pdi->focusWidget = 
	  _GetWindowedAncestor(FindFocusWidget(widget, pdi));
    
    
    /*
     * If a grab is active from a previous activation then dispatch
     * based on owner_events ala protocol but with focus being
     * determined by XtSetKeyboardFocus.
     */
    if 	(IsAnyGrab(prevGrabType))
      {
	  if (prevGrab->ownerEvents) 
	    dspWidget = focusWidget;
	  else
	    dspWidget = prevGrab->widget;
      }
    else 
      {
	  /*
           * If the focus widget is the event widget or a descendant
	   * then we can avoid the rest of this. Else ugh...
	   */
	  if (focusWidget != widget) 
	    {
		XtGeneology 	ewRelFw; /* relationship of event widget to
					    focusWidget */
		Widget		lca;

		lca = CommonAncestor(widget, focusWidget, &ewRelFw);
		    
		/*
		 * if the event widget is an ancestor of focus due to the pointer
		 * and/or the grab being in an ancestor and it's a passive grab
		 * send to grab widget.
		 * we are also dispatching to widget if ownerEvents and the event
		 * is outside the client
		 */
		if ((ewRelFw == XtMyAncestor) &&
		    (devGrabType == XtPassiveServerGrab))
		     {
			 if (IsOutside(event, widget) ||
			     event->type ==KeyPress
			     )
			   dspWidget = devGrab->widget;
		     }
		else 
		  {
		      /*
		       * if the grab widget is not an ancestor of the focus
		       * release the grab in order to avoid locking. There
		       * is a possible case  in that ownerEvents true will fall
		       * through and if synch is set and the event widget
		       * could turn it off we'll lock. check for it ? why not
		       */
		      if ((ewRelFw != XtMyAncestor) 
			  && (devGrabType == XtPassiveServerGrab)
			  && (!IsAnyGrab(prevGrabType))
			  )
			{
			    XtUngrabKeyboard(devGrab->widget,
					     event->time);
			    devGrabType = XtNoServerGrab;
			}
		      /*
		       * if there isn't a grab with then check
		       * for a logical grab that would have been activated
		       * if the server was using Xt focus instead of server
		       * focus 
		       */
		      if (
			  (event->type != KeyPress) ||
			  (event->keycode == 0) /* Xlib XIM composed input */
			  )
			dspWidget = focusWidget;
		      else
			{
			    XtServerGrabPtr	grab;

			    if (!pseudoTraceDepth || 
				!(focusWidget == pseudoTrace[0]) ||
				!(lca == pseudoTrace[pseudoTraceDepth]))
			      {
				  /*
				   * fill ancestor list from lca
				   * (non-inclusive)to focusWidget by
				   * passing in lca as breakWidget
				   */
				  _XtFillAncestorList(&pseudoTrace, 
						   &pseudoTraceMax, 
						   &pseudoTraceDepth,
						   focusWidget,
						   lca);
				  /* ignore lca */
				  pseudoTraceDepth--;
			      }
			    if (grab = CheckServerGrabs((XEvent*)event,
							pseudoTrace,
							pseudoTraceDepth))
			      {
				  XtDevice device = &pdi->keyboard;
				  
				  device->grabType = XtPseudoPassiveServerGrab;
				  pdi->activatingKey = event->keycode;
				  device->grab = *grab;

				  if (grab 
				      )
				    dspWidget = grab->widget;
				  else
				    dspWidget = focusWidget;
			      }
			}
		  }
	    }
      }
    UNLOCK_PROCESS;
    return dspWidget;
}

Widget _XtProcessKeyboardEvent(event, widget, pdi)
    XKeyEvent  *event;
    Widget widget;
    XtPerDisplayInput pdi;
{    
    XtDevice		device = &pdi->keyboard;
    XtServerGrabPtr	newGrab, devGrab = &device->grab;
    XtServerGrabRec	prevGrabRec;
    XtServerGrabType	prevGrabType = device->grabType;
    Widget		dspWidget = NULL;
    Boolean		deactivateGrab = FALSE;

    prevGrabRec = *devGrab;

    switch (event->type)
      {
	case KeyPress:
	  {
	      if (event->keycode != 0 && /* Xlib XIM composed input */
		  !IsServerGrab(device->grabType) && 
		  (newGrab = CheckServerGrabs((XEvent*)event,
					      pdi->trace,
					      pdi->traceDepth)))
		{
		    /*
		     * honor pseudo-grab from prior event by X
		     * unlocking keyboard. Not Xt Unlock !
		     */
		    if (IsPseudoGrab(prevGrabType))
		      XUngrabKeyboard(XtDisplay(newGrab->widget),
				      event->time);
		    else
		      {
			  /* Activate the grab */
			  device->grab = *newGrab;
			  pdi->activatingKey = event->keycode;
			  device->grabType = XtPassiveServerGrab;
		      }
		}
	    }
	  break;
	  
	case KeyRelease:
	  {
	      if (IsEitherPassiveGrab(device->grabType) && 
		  (event->keycode == pdi->activatingKey))
		deactivateGrab = TRUE;
	  }
	  break;
      }
    dspWidget = FindKeyDestination(widget, event,
				   &prevGrabRec, prevGrabType,
				   devGrab, device->grabType,
				   pdi);
    if (deactivateGrab)
      {
	  /* Deactivate the grab */
	  device->grabType = XtNoServerGrab;
	  pdi->activatingKey = 0;
      }
    return dspWidget;
}    
    
static Widget GetShell(widget)
    Widget	widget;
{
    Widget	shell;

    for (shell = widget; 
	 shell && !XtIsShell(shell);
	 shell = XtParent(shell)){}
    return shell;
}


/*
 * Check that widget really has Xt focus due to it having recieved an
 * event 
 */
typedef enum {NotActive = 0, IsActive, IsTarget} ActiveType;
static ActiveType InActiveSubtree(widget)
    Widget	widget;
{
    Boolean		isTarget;
    ActiveType		retval;

    LOCK_PROCESS;
    if (!pathTraceDepth || widget != pathTrace[0]) {
	_XtFillAncestorList(&pathTrace, 
			    &pathTraceMax, 
			    &pathTraceDepth,
			    widget,
			    NULL);
    }
    if (widget == _FindFocusWidget(widget, 
				   pathTrace,
				   pathTraceDepth, 
				   TRUE,
				   &isTarget))
	retval = (isTarget ? IsTarget : IsActive);
    else
	retval = NotActive;
    UNLOCK_PROCESS;
    return retval;
}




/* ARGSUSED */
void _XtHandleFocus(widget, client_data, event, cont)
    Widget widget;
    XtPointer client_data;	/* child who wants focus */
    XEvent *event;
    Boolean *cont;		/* unused */
{
    XtPerDisplayInput 	pdi = _XtGetPerDisplayInput(XtDisplay(widget));
    XtPerWidgetInput	pwi = (XtPerWidgetInput)client_data;
    XtGeneology		oldFocalPoint = pwi->focalPoint;
    XtGeneology		newFocalPoint = pwi->focalPoint;

    switch( event->type ) {
	
      case KeyPress:
      case KeyRelease:
	/*
	 * We're getting the keyevents used to guarantee propagating
         * child interest ala ForwardEvent in R3
	 */
	return;

      case EnterNotify:
      case LeaveNotify:
	
	/*
	 * If operating in a focus driven model, then enter and
	 * leave events do not affect the keyboard focus.
	 */
	if ((event->xcrossing.detail != NotifyInferior)
	    &&	(event->xcrossing.focus))
	  {	      
	      switch (oldFocalPoint)
		{
		  case XtMyAncestor:
		    if (event->type == LeaveNotify)
		      newFocalPoint = XtUnrelated;
		    break;
		  case XtUnrelated:
		    if (event->type == EnterNotify)
		      newFocalPoint = XtMyAncestor;
		    break;
		  case XtMySelf:
		    break;
		  case XtMyDescendant:
		    break;
		    
		}	
	  }
	break;
      case FocusIn:
	switch (event->xfocus.detail)
	  {
	    case NotifyNonlinear:
	    case NotifyAncestor:
	    case NotifyInferior:
	      newFocalPoint = XtMySelf;
	      break;
	    case NotifyNonlinearVirtual:
	    case NotifyVirtual:
	      newFocalPoint = XtMyDescendant;
	      break;
	    case NotifyPointer:
	      newFocalPoint = XtMyAncestor;
	      break;
	  }
	break;
      case FocusOut:
	switch (event->xfocus.detail)
	  {
	    case NotifyPointer:
	    case NotifyNonlinear:
	    case NotifyAncestor:
	    case NotifyNonlinearVirtual:
	    case NotifyVirtual:
	      newFocalPoint = XtUnrelated;
	      break;
	    case NotifyInferior:
	      newFocalPoint = XtMyDescendant;
	      return;
	      break;
	  }
	break;
    }
    
    if (newFocalPoint != oldFocalPoint)
      {
	  Boolean 	add;
	  Widget	descendant = pwi->focusKid;

	  pwi->focalPoint = newFocalPoint;

	  if ((oldFocalPoint == XtUnrelated) &&
	      InActiveSubtree(widget) != NotActive)
	    {
		pdi->focusWidget = NULL; /* invalidate the cache */
		pwi->haveFocus = TRUE;
		add = TRUE;
	    }
	  else if (newFocalPoint == XtUnrelated)
	    {
		pdi->focusWidget = NULL; /* invalidate the cache */
		pwi->haveFocus = FALSE;
		add = FALSE;
	    }
	  else
	    return;

	  if (descendant)
	    {
		if (add)
		  {
		      _XtSendFocusEvent(descendant, FocusIn);
		  }
		else
		  {
		      _XtSendFocusEvent(descendant, FocusOut);
		  }
	    }
      }
}


static void AddFocusHandler(widget, descendant, pwi, psi, pdi, oldEventMask)
    Widget widget, descendant;
    XtPerWidgetInput pwi;
    XtPerWidgetInput psi;
    XtPerDisplayInput pdi;
    EventMask oldEventMask;
{
    EventMask	 	eventMask, targetEventMask;
    Widget		target;

    /*
     * widget must now select for key events if the descendant is
     * interested in them.
     *
     * shell borders are not occluded by the child, they're occluded
     * by reparenting window managers. !!!
     */
    target = descendant ? _GetWindowedAncestor(descendant) : NULL;
    targetEventMask = XtBuildEventMask(target);
    eventMask = targetEventMask & (KeyPressMask | KeyReleaseMask);
    eventMask |= FocusChangeMask | EnterWindowMask | LeaveWindowMask;

    if (oldEventMask) {
	oldEventMask &= KeyPressMask | KeyReleaseMask;
	oldEventMask |= FocusChangeMask | EnterWindowMask | LeaveWindowMask;

	if (oldEventMask != eventMask)
	    XtRemoveEventHandler(widget, (oldEventMask & ~eventMask), 
				 False, _XtHandleFocus, (XtPointer)pwi);
    }

    if (oldEventMask != eventMask)
	XtAddEventHandler(widget, eventMask, False, 
			  _XtHandleFocus, (XtPointer)pwi);

    /* What follows is too much grief to go through if the
     * target doesn't actually care about focus change events,
     * so just invalidate the focus cache & refill it when
     * the next input event actually arrives.
     */

    if (!(targetEventMask & FocusChangeMask)) {
	pdi->focusWidget = NULL;
	return;
    }

    if (XtIsRealized(widget) && !pwi->haveFocus) {
	if (psi->haveFocus) {
	    Window root, child;
	    int root_x, root_y, win_x, win_y;
	    int left, right, top, bottom;
	    unsigned int modMask;
	    ActiveType act;

	    /*
	     * If the shell has the focus but the source widget
	     * doesn't, it may only be because the source widget
	     * wasn't previously tracking focus or crossing events.
	     * If the target wants focus events, we have to
	     * now determine whether the source has the focus.
	     */

	    if ((act = InActiveSubtree(widget)) == IsTarget)
		pwi->haveFocus = TRUE;
	    else if (act == IsActive) {
		/*
		 * An ancestor contains the focus, so if source
		 * contains the pointer, then source has the focus.
		 */

		if (XQueryPointer(XtDisplay(widget), XtWindow(widget),
				  &root, &child,
				  &root_x, &root_y, &win_x, &win_y, &modMask))
		{
		    /* We need to take borders into consideration */
		    left = top = -((int) widget->core.border_width);
		    right = (int) (widget->core.width + (widget->core.border_width << 1));
		    bottom = (int) (widget->core.height + (widget->core.border_width << 1));

		    if (win_x >= left && win_x < right &&
			win_y >= top && win_y < bottom)
			pwi->haveFocus = TRUE;
		}
	    }
	}
    }
    if (pwi->haveFocus) {
	  pdi->focusWidget = NULL; /* invalidate the cache */
	  _XtSendFocusEvent(target, FocusIn);
    }
}


/* ARGSUSED */
static void QueryEventMask(widget, client_data, event, cont)
    Widget widget;		/* child who gets focus */
    XtPointer client_data;	/* ancestor giving it */
    XEvent *event;
    Boolean *cont;		/* unused */
{
    /* widget was once the target of an XtSetKeyboardFocus but
     * was unrealized at the time.   Make sure ancestor still wants
     * focus set here then install the handler now that we know the
     * complete event mask.
     */
    Widget ancestor = (Widget)client_data;
    XtPerWidgetInput pwi = _XtGetPerWidgetInput(ancestor, FALSE);
    Widget target = pwi->queryEventDescendant;

    /* use of 'target' is non-standard hackery; allows focus to non-widget */
    if (pwi && (pwi->focusKid == target)) {
	AddFocusHandler(ancestor, target, pwi,
			_XtGetPerWidgetInput(GetShell(ancestor), TRUE),
			_XtGetPerDisplayInput(XtDisplay(ancestor)),
			(EventMask)0);
    }
    XtRemoveEventHandler(widget, XtAllEvents, True,
			 QueryEventMask, client_data);
    pwi->map_handler_added = FALSE;
}


/* ARGSUSED */
static void FocusDestroyCallback(widget, closure, call_data)
    Widget  widget;
    XtPointer closure;		/* Widget */
    XtPointer call_data;
{
    XtSetKeyboardFocus((Widget)closure, None);
}

void XtSetKeyboardFocus(widget, descendant)
    Widget widget;
    Widget descendant;
{
    XtPerDisplayInput pdi;
    XtPerWidgetInput pwi;
    Widget oldDesc, oldTarget, target, hookobj;
    WIDGET_TO_APPCON(widget);

    LOCK_APP(app);
    LOCK_PROCESS;
    pdi = _XtGetPerDisplayInput(XtDisplay(widget));
    pwi = _XtGetPerWidgetInput(widget, TRUE);
    oldDesc = pwi->focusKid;

    if (descendant == widget) descendant = (Widget)None;

    target = descendant ? _GetWindowedAncestor(descendant) : NULL;
    oldTarget = oldDesc ? _GetWindowedAncestor(oldDesc) : NULL;
    
    if (descendant != oldDesc) {
	
	/* update the forward path */
	pwi->focusKid = descendant;


	/* all the rest handles focus ins and focus outs and misc gunk */
	
	if (oldDesc) {
	    /* invalidate FindKeyDestination's ancestor list */
	    if (pseudoTraceDepth && oldTarget == pseudoTrace[0])
		pseudoTraceDepth = 0;

	    XtRemoveCallback(oldDesc, XtNdestroyCallback, 
			     FocusDestroyCallback, (XtPointer)widget);

	    if (!oldTarget->core.being_destroyed) {
		if (pwi->map_handler_added) {
		    XtRemoveEventHandler(oldTarget, XtAllEvents, True,
					 QueryEventMask, (XtPointer)widget);
		    pwi->map_handler_added = FALSE;
		}
		if (pwi->haveFocus) {
		    _XtSendFocusEvent( oldTarget, FocusOut);
		}
	    }
	    else if (pwi->map_handler_added) {
		pwi->map_handler_added = FALSE;
	    }

	    if (pwi->haveFocus)
		pdi->focusWidget = NULL; /* invalidate cache */

	    /*
	     * If there was a forward path then remove the handler if
	     * the path is being set to null and it isn't a shell.
	     * shells always have a handler for tracking focus for the
	     * hierarchy.
	     *
	     * Keep the pwi record on the assumption that the client
	     * will continue to dynamically assign focus for this widget.
	     */
	    if (!XtIsShell(widget) && !descendant) {
	      XtRemoveEventHandler(widget, XtAllEvents, True, 
				   _XtHandleFocus, (XtPointer)pwi);
	      pwi->haveFocus = FALSE;
	  }
	}
	
	if (descendant) {
	    Widget shell = GetShell(widget);
	    XtPerWidgetInput psi = _XtGetPerWidgetInput(shell, TRUE);
	    XtAddCallback (descendant, XtNdestroyCallback, 
			   FocusDestroyCallback, (XtPointer) widget);

	    AddFocusHandler(widget, descendant, pwi, psi, pdi,
			    oldTarget ? XtBuildEventMask(oldTarget) : 0);

	    if (widget != shell)
		XtAddEventHandler(
			shell,
			FocusChangeMask | EnterWindowMask | LeaveWindowMask,
			False,
			_XtHandleFocus,
			(XtPointer)psi
		       );

	    if (! XtIsRealized(target)) {
		XtAddEventHandler(target, (EventMask)StructureNotifyMask,
				  False, QueryEventMask, (XtPointer)widget);
		pwi->map_handler_added = TRUE;
		pwi->queryEventDescendant = descendant;
	    }
	}
    }
    hookobj = XtHooksOfDisplay(XtDisplay(widget));
    if (XtHasCallbacks(hookobj, XtNchangeHook) == XtCallbackHasSome) {
	XtChangeHookDataRec call_data;

	call_data.type = XtHsetKeyboardFocus;
	call_data.widget = widget;
	call_data.event_data = (XtPointer) descendant;
	XtCallCallbackList(hookobj, 
		((HookObject)hookobj)->hooks.changehook_callbacks, 
		(XtPointer)&call_data);
    }
    UNLOCK_PROCESS;
    UNLOCK_APP(app);
}