Debian Codegraph @

pTk/mTk/generic/tkFocus.c

raw symbols SCIP
1 /*
2 * tkFocus.c --
3 *
4 * This file contains procedures that manage the input
5 * focus for Tk.
6 *
7 * Copyright (c) 1990-1994 The Regents of the University of California.
8 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
9 *
10 * See the file "license.terms" for information on usage and redistribution
11 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 *
13 * RCS: @(#) $Id: tkFocus.c,v 1.10 2002/10/08 19:57:59 hobbs Exp $
14 */
15
16 #include "tkInt.h"
17 #include "tkPort.h"
18
19
20 /*
21 * For each top-level window that has ever received the focus, there
22 * is a record of the following type:
23 */
24
25 typedef struct TkToplevelFocusInfo {
26 TkWindow *topLevelPtr; /* Information about top-level window. */
27 TkWindow *focusWinPtr; /* The next time the focus comes to this
28 * top-level, it will be given to this
29 * window. */
30 struct TkToplevelFocusInfo *nextPtr;
31 /* Next in list of all toplevel focus records
32 * for a given application. */
33 } ToplevelFocusInfo;
34
35 /*
36 * One of the following structures exists for each display used by
37 * each application. These are linked together from the TkMainInfo
38 * structure. These structures are needed because it isn't
39 * sufficient to store a single piece of focus information in each
40 * display or in each application: we need the cross-product.
41 * There needs to be separate information for each display, because
42 * it's possible to have multiple focus windows active simultaneously
43 * on different displays. There also needs to be separate information
44 * for each application, because of embedding: if an embedded
45 * application has the focus, its container application also has
46 * the focus. Thus we keep a list of structures for each application:
47 * the same display can appear in structures for several applications
48 * at once.
49 */
50
51 typedef struct TkDisplayFocusInfo {
52 TkDisplay *dispPtr; /* Display that this information pertains
53 * to. */
54 struct TkWindow *focusWinPtr;
55 /* Window that currently has the focus for
56 * this application on this display, or NULL
57 * if none. */
58 struct TkWindow *focusOnMapPtr;
59 /* This points to a toplevel window that is
60 * supposed to receive the X input focus as
61 * soon as it is mapped (needed to handle the
62 * fact that X won't allow the focus on an
63 * unmapped window). NULL means no delayed
64 * focus op in progress for this display. */
65 int forceFocus; /* Associated with focusOnMapPtr: non-zero
66 * means claim the focus even if some other
67 * application currently has it. */
68 unsigned long focusSerial; /* Serial number of last request this
69 * application made to change the focus on
70 * this display. Used to identify stale
71 * focus notifications coming from the
72 * X server. */
73 struct TkDisplayFocusInfo *nextPtr;
74 /* Next in list of all display focus
75 * records for a given application. */
76 } DisplayFocusInfo;
77
78 /*
79 * The following magic value is stored in the "send_event" field of
80 * FocusIn and FocusOut events that are generated in this file. This
81 * allows us to separate "real" events coming from the server from
82 * those that we generated.
83 */
84
85 #define GENERATED_EVENT_MAGIC ((Bool) 0x547321ac)
86
87 /*
88 * Forward declarations for procedures defined in this file:
89 */
90
91
92 static DisplayFocusInfo *FindDisplayFocusInfo _ANSI_ARGS_((TkMainInfo *mainPtr,
93 TkDisplay *dispPtr));
94 static void FocusMapProc _ANSI_ARGS_((ClientData clientData,
95 XEvent *eventPtr));
96 static void GenerateFocusEvents _ANSI_ARGS_((TkWindow *sourcePtr,
97 TkWindow *destPtr));
98
99 /*
100 *--------------------------------------------------------------
101 *
102 * Tk_FocusObjCmd --
103 *
104 * This procedure is invoked to process the "focus" Tcl command.
105 * See the user documentation for details on what it does.
106 *
107 * Results:
108 * A standard Tcl result.
109 *
110 * Side effects:
111 * See the user documentation.
112 *
113 *--------------------------------------------------------------
114 */
115
116 int
117 Tk_FocusObjCmd(clientData, interp, objc, objv)
118 ClientData clientData; /* Main window associated with
119 * interpreter. */
120 Tcl_Interp *interp; /* Current interpreter. */
121 int objc; /* Number of arguments. */
122 Tcl_Obj *CONST objv[]; /* Argument objects. */
123 {
124 static CONST char *focusOptions[] = {
125 "-displayof", "-force", "-lastfor", (char *) NULL
126 };
127 Tk_Window tkwin = (Tk_Window) clientData;
128 TkWindow *winPtr = (TkWindow *) clientData;
129 TkWindow *newPtr, *focusWinPtr, *topLevelPtr;
130 ToplevelFocusInfo *tlFocusPtr;
131 char *windowName;
132 int index;
133
134 /*
135 * If invoked with no arguments, just return the current focus window.
136 */
137
138 if (objc == 1) {
139 focusWinPtr = TkGetFocusWin(winPtr);
140 if (focusWinPtr != NULL) {
141 Tcl_SetResult(interp, focusWinPtr->pathName, TCL_STATIC);
142 }
143 return TCL_OK;
144 }
145
146 /*
147 * If invoked with a single argument beginning with "." then focus
148 * on that window.
149 */
150
151 if (objc == 2) {
152 windowName = Tcl_GetStringFromObj(objv[1], (int *) NULL);
153
154 /*
155 * The empty string case exists for backwards compatibility.
156 */
157
158 if (windowName[0] == '\0') {
159 return TCL_OK;
160 }
161 if (windowName[0] == '.') {
162 newPtr = (TkWindow *) Tk_NameToWindow(interp, windowName, tkwin);
163 if (newPtr == NULL) {
164 return TCL_ERROR;
165 }
166 if (!(newPtr->flags & TK_ALREADY_DEAD)) {
167 TkSetFocusWin(newPtr, 0);
168 }
169 return TCL_OK;
170 }
171 }
172
173 if (Tcl_GetIndexFromObj(interp, objv[1], focusOptions, "option", 0,
174 &index) != TCL_OK) {
175 return TCL_ERROR;
176 }
177 if (objc != 3) {
178 Tcl_WrongNumArgs(interp, 2, objv, "window");
179 return TCL_ERROR;
180 }
181 switch (index) {
182 case 0: { /* -displayof */
183 windowName = Tcl_GetStringFromObj(objv[2], (int *) NULL);
184 newPtr = (TkWindow *) Tk_NameToWindow(interp, windowName, tkwin);
185 if (newPtr == NULL) {
186 return TCL_ERROR;
187 }
188 newPtr = TkGetFocusWin(newPtr);
189 if (newPtr != NULL) {
190 Tcl_SetResult(interp, newPtr->pathName, TCL_STATIC);
191 }
192 break;
193 }
194 case 1: { /* -force */
195 windowName = Tcl_GetStringFromObj(objv[2], (int *) NULL);
196
197 /*
198 * The empty string case exists for backwards compatibility.
199 */
200
201 if (windowName[0] == '\0') {
202 return TCL_OK;
203 }
204 newPtr = (TkWindow *) Tk_NameToWindow(interp, windowName, tkwin);
205 if (newPtr == NULL) {
206 return TCL_ERROR;
207 }
208 TkSetFocusWin(newPtr, 1);
209 break;
210 }
211 case 2: { /* -lastfor */
212 windowName = Tcl_GetStringFromObj(objv[2], (int *) NULL);
213 newPtr = (TkWindow *) Tk_NameToWindow(interp, windowName, tkwin);
214 if (newPtr == NULL) {
215 return TCL_ERROR;
216 }
217 for (topLevelPtr = newPtr; topLevelPtr != NULL;
218 topLevelPtr = topLevelPtr->parentPtr) {
219 if (topLevelPtr->flags & TK_TOP_HIERARCHY) {
220 for (tlFocusPtr = newPtr->mainPtr->tlFocusPtr;
221 tlFocusPtr != NULL;
222 tlFocusPtr = tlFocusPtr->nextPtr) {
223 if (tlFocusPtr->topLevelPtr == topLevelPtr) {
224 Tcl_SetResult(interp,
225 tlFocusPtr->focusWinPtr->pathName,
226 TCL_STATIC);
227 return TCL_OK;
228 }
229 }
230 Tcl_SetResult(interp, topLevelPtr->pathName, TCL_STATIC);
231 return TCL_OK;
232 }
233 }
234 break;
235 }
236 default: {
237 panic("bad const entries to focusOptions in focus command");
238 }
239 }
240 return TCL_OK;
241 }
242
243 /*
244 *--------------------------------------------------------------
245 *
246 * TkFocusFilterEvent --
247 *
248 * This procedure is invoked by Tk_HandleEvent when it encounters
249 * a FocusIn, FocusOut, Enter, or Leave event.
250 *
251 * Results:
252 * A return value of 1 means that Tk_HandleEvent should process
253 * the event normally (i.e. event handlers should be invoked).
254 * A return value of 0 means that this event should be ignored.
255 *
256 * Side effects:
257 * Additional events may be generated, and the focus may switch.
258 *
259 *--------------------------------------------------------------
260 */
261
262 int
263 TkFocusFilterEvent(winPtr, eventPtr)
264 TkWindow *winPtr; /* Window that focus event is directed to. */
265 XEvent *eventPtr; /* FocusIn, FocusOut, Enter, or Leave
266 * event. */
267 {
268 /*
269 * Design notes: the window manager and X server work together to
270 * transfer the focus among top-level windows. This procedure takes
271 * care of transferring the focus from a top-level or wrapper window
272 * to the actual window within that top-level that has the focus.
273 * We do this by synthesizing X events to move the focus around.
274 * None of the FocusIn and FocusOut events generated by X are ever
275 * used outside of this procedure; only the synthesized events get
276 * through to the rest of the application. At one point (e.g.
277 * Tk4.0b1) Tk used to call X to move the focus from a top-level to
278 * one of its descendants, then just pass through the events
279 * generated by X. This approach didn't work very well, for a
280 * variety of reasons. For example, if X generates the events they
281 * go at the back of the event queue, which could cause problems if
282 * other things have already happened, such as moving the focus to
283 * yet another window.
284 */
285
286 ToplevelFocusInfo *tlFocusPtr;
287 DisplayFocusInfo *displayFocusPtr;
288 TkDisplay *dispPtr = winPtr->dispPtr;
289 TkWindow *newFocusPtr;
290 int retValue, delta;
291
292 /*
293 * If this was a generated event, just turn off the generated
294 * flag and pass the event through to Tk bindings.
295 */
296
297 if (eventPtr->xfocus.send_event == GENERATED_EVENT_MAGIC) {
298 eventPtr->xfocus.send_event = 0;
299 return 1;
300 }
301
302 /*
303 * Check for special events generated by embedded applications to
304 * request the input focus. If this is one of those events, make
305 * the change in focus and return without any additional processing
306 * of the event (note: the "detail" field of the event indicates
307 * whether to claim the focus even if we don't already have it).
308 */
309
310 if ((eventPtr->xfocus.mode == EMBEDDED_APP_WANTS_FOCUS)
311 && (eventPtr->type == FocusIn)) {
312 TkSetFocusWin(winPtr, eventPtr->xfocus.detail);
313 return 0;
314 }
315
316 /*
317 * This was not a generated event. We'll return 1 (so that the
318 * event will be processed) if it's an Enter or Leave event, and
319 * 0 (so that the event won't be processed) if it's a FocusIn or
320 * FocusOut event.
321 */
322
323 retValue = 0;
324 displayFocusPtr = FindDisplayFocusInfo(winPtr->mainPtr, winPtr->dispPtr);
325 if (eventPtr->type == FocusIn) {
326 /*
327 * Skip FocusIn events that cause confusion
328 * NotifyVirtual and NotifyNonlinearVirtual - Virtual events occur
329 * on windows in between the origin and destination of the
330 * focus change. For FocusIn we may see this when focus
331 * goes into an embedded child. We don't care about this,
332 * although we may end up getting a NotifyPointer later.
333 * NotifyInferior - focus is coming to us from an embedded child.
334 * When focus is on an embeded focus, we still think we have
335 * the focus, too, so this message doesn't change our state.
336 * NotifyPointerRoot - should never happen because this is sent
337 * to the root window.
338 *
339 * Interesting FocusIn events are
340 * NotifyAncestor - focus is coming from our parent, probably the root.
341 * NotifyNonlinear - focus is coming from a different branch, probably
342 * another toplevel.
343 * NotifyPointer - implicit focus because of the mouse position.
344 * This is only interesting on toplevels, when it means that the
345 * focus has been set to the root window but the mouse is over
346 * this toplevel. We take the focus implicitly (probably no
347 * window manager)
348 */
349
350 if ((eventPtr->xfocus.detail == NotifyVirtual)
351 || (eventPtr->xfocus.detail == NotifyNonlinearVirtual)
352 || (eventPtr->xfocus.detail == NotifyPointerRoot)
353 || (eventPtr->xfocus.detail == NotifyInferior)) {
354 return retValue;
355 }
356 } else if (eventPtr->type == FocusOut) {
357 /*
358 * Skip FocusOut events that cause confusion.
359 * NotifyPointer - the pointer is in us or a child, and we are losing
360 * focus because of an XSetInputFocus. Other focus events
361 * will set our state properly.
362 * NotifyPointerRoot - should never happen because this is sent
363 * to the root window.
364 * NotifyInferior - focus leaving us for an embedded child. We
365 * retain a notion of focus when an embedded child has focus.
366 *
367 * Interesting events are:
368 * NotifyAncestor - focus is going to root.
369 * NotifyNonlinear - focus is going to another branch, probably
370 * another toplevel.
371 * NotifyVirtual, NotifyNonlinearVirtual - focus is passing through,
372 * and we need to make sure we track this.
373 */
374
375 if ((eventPtr->xfocus.detail == NotifyPointer)
376 || (eventPtr->xfocus.detail == NotifyPointerRoot)
377 || (eventPtr->xfocus.detail == NotifyInferior)) {
378 return retValue;
379 }
380 } else {
381 retValue = 1;
382 if (eventPtr->xcrossing.detail == NotifyInferior) {
383 return retValue;
384 }
385 }
386
387 /*
388 * If winPtr isn't a top-level window than just ignore the event.
389 */
390
391 winPtr = TkWmFocusToplevel(winPtr);
392 if (winPtr == NULL) {
393 return retValue;
394 }
395
396 /*
397 * If there is a grab in effect and this window is outside the
398 * grabbed tree, then ignore the event.
399 */
400
401 if (TkGrabState(winPtr) == TK_GRAB_EXCLUDED) {
402 return retValue;
403 }
404
405 /*
406 * It is possible that there were outstanding FocusIn and FocusOut
407 * events on their way to us at the time the focus was changed
408 * internally with the "focus" command. If so, these events could
409 * potentially cause us to lose the focus (switch it to the window
410 * of the last FocusIn event) even though the focus change occurred
411 * after those events. The following code detects this and ignores
412 * the stale events.
413 *
414 * Note: the focusSerial is only generated by TkpChangeFocus,
415 * whereas in Tk 4.2 there was always a nop marker generated.
416 */
417
418 delta = eventPtr->xfocus.serial - displayFocusPtr->focusSerial;
419 if (delta < 0) {
420 return retValue;
421 }
422
423 /*
424 * Find the ToplevelFocusInfo structure for the window, and make a new one
425 * if there isn't one already.
426 */
427
428 for (tlFocusPtr = winPtr->mainPtr->tlFocusPtr; tlFocusPtr != NULL;
429 tlFocusPtr = tlFocusPtr->nextPtr) {
430 if (tlFocusPtr->topLevelPtr == winPtr) {
431 break;
432 }
433 }
434 if (tlFocusPtr == NULL) {
435 tlFocusPtr = (ToplevelFocusInfo *) ckalloc(sizeof(ToplevelFocusInfo));
436 tlFocusPtr->topLevelPtr = tlFocusPtr->focusWinPtr = winPtr;
437 tlFocusPtr->nextPtr = winPtr->mainPtr->tlFocusPtr;
438 winPtr->mainPtr->tlFocusPtr = tlFocusPtr;
439 }
440 newFocusPtr = tlFocusPtr->focusWinPtr;
441
442 /*
443 * Ignore event if newFocus window is already dead!
444 */
445 if (newFocusPtr->flags & TK_ALREADY_DEAD) {
446 return retValue;
447 }
448
449 if (eventPtr->type == FocusIn) {
450 GenerateFocusEvents(displayFocusPtr->focusWinPtr, newFocusPtr);
451 displayFocusPtr->focusWinPtr = newFocusPtr;
452 dispPtr->focusPtr = newFocusPtr;
453
454 /*
455 * NotifyPointer gets set when the focus has been set to the root
456 * window but we have the pointer. We'll treat this like an implicit
457 * focus in event so that upon Leave events we release focus.
458 */
459
460 if (!(winPtr->flags & TK_EMBEDDED)) {
461 if (eventPtr->xfocus.detail == NotifyPointer) {
462 dispPtr->implicitWinPtr = winPtr;
463 } else {
464 dispPtr->implicitWinPtr = NULL;
465 }
466 }
467 } else if (eventPtr->type == FocusOut) {
468 GenerateFocusEvents(displayFocusPtr->focusWinPtr, (TkWindow *) NULL);
469
470 /*
471 * Reset dispPtr->focusPtr, but only if it currently is the same
472 * as this application's focusWinPtr: this check is needed to
473 * handle embedded applications in the same process.
474 */
475
476 if (dispPtr->focusPtr == displayFocusPtr->focusWinPtr) {
477 dispPtr->focusPtr = NULL;
478 }
479 displayFocusPtr->focusWinPtr = NULL;
480 } else if (eventPtr->type == EnterNotify) {
481 /*
482 * If there is no window manager, or if the window manager isn't
483 * moving the focus around (e.g. the disgusting "NoTitleFocus"
484 * option has been selected in twm), then we won't get FocusIn
485 * or FocusOut events. Instead, the "focus" field will be set
486 * in an Enter event to indicate that we've already got the focus
487 * when the mouse enters the window (even though we didn't get
488 * a FocusIn event). Watch for this and grab the focus when it
489 * happens. Note: if this is an embedded application then don't
490 * accept the focus implicitly like this; the container
491 * application will give us the focus explicitly if it wants us
492 * to have it.
493 */
494
495 if (eventPtr->xcrossing.focus &&
496 (displayFocusPtr->focusWinPtr == NULL)
497 && !(winPtr->flags & TK_EMBEDDED)) {
498 if (dispPtr->focusDebug) {
499 printf("Focussed implicitly on %s\n",
500 newFocusPtr->pathName);
501 }
502
503 GenerateFocusEvents(displayFocusPtr->focusWinPtr, newFocusPtr);
504 displayFocusPtr->focusWinPtr = newFocusPtr;
505 dispPtr->implicitWinPtr = winPtr;
506 dispPtr->focusPtr = newFocusPtr;
507 }
508 } else if (eventPtr->type == LeaveNotify) {
509 /*
510 * If the pointer just left a window for which we automatically
511 * claimed the focus on enter, move the focus back to the root
512 * window, where it was before we claimed it above. Note:
513 * dispPtr->implicitWinPtr may not be the same as
514 * displayFocusPtr->focusWinPtr (e.g. because the "focus"
515 * command was used to redirect the focus after it arrived at
516 * dispPtr->implicitWinPtr)!! In addition, we generate events
517 * because the window manager won't give us a FocusOut event when
518 * we focus on the root.
519 */
520
521 if ((dispPtr->implicitWinPtr != NULL)
522 && !(winPtr->flags & TK_EMBEDDED)) {
523 if (dispPtr->focusDebug) {
524 printf("Defocussed implicit Async\n");
525 }
526 GenerateFocusEvents(displayFocusPtr->focusWinPtr,
527 (TkWindow *) NULL);
528 XSetInputFocus(dispPtr->display, PointerRoot, RevertToPointerRoot,
529 CurrentTime);
530 displayFocusPtr->focusWinPtr = NULL;
531 dispPtr->implicitWinPtr = NULL;
532 }
533 }
534 return retValue;
535 }
536
537 /*
538 *----------------------------------------------------------------------
539 *
540 * TkSetFocusWin --
541 *
542 * This procedure is invoked to change the focus window for a
543 * given display in a given application.
544 *
545 * Results:
546 * None.
547 *
548 * Side effects:
549 * Event handlers may be invoked to process the change of
550 * focus.
551 *
552 *----------------------------------------------------------------------
553 */
554
555 void
556 TkSetFocusWin(winPtr, force)
557 TkWindow *winPtr; /* Window that is to be the new focus for
558 * its display and application. */
559 int force; /* If non-zero, set the X focus to this
560 * window even if the application doesn't
561 * currently have the X focus. */
562 {
563 ToplevelFocusInfo *tlFocusPtr;
564 DisplayFocusInfo *displayFocusPtr;
565 TkWindow *topLevelPtr;
566 int allMapped, serial;
567
568 displayFocusPtr = FindDisplayFocusInfo(winPtr->mainPtr, winPtr->dispPtr);
569
570 /*
571 * If force is set, we should make sure we grab the focus regardless
572 * of the current focus window since under Windows, we may need to
573 * take control away from another application.
574 */
575
576 if (winPtr == displayFocusPtr->focusWinPtr && !force) {
577 return;
578 }
579
580 /*
581 * Find the top-level window for winPtr, then find (or create)
582 * a record for the top-level. Also see whether winPtr and all its
583 * ancestors are mapped.
584 */
585
586 allMapped = 1;
587 for (topLevelPtr = winPtr; ; topLevelPtr = topLevelPtr->parentPtr) {
588 if (topLevelPtr == NULL) {
589 /*
590 * The window is being deleted. No point in worrying about
591 * giving it the focus.
592 */
593 return;
594 }
595 if (!(topLevelPtr->flags & TK_MAPPED)) {
596 allMapped = 0;
597 }
598 if (topLevelPtr->flags & TK_TOP_HIERARCHY) {
599 break;
600 }
601 }
602
603 /*
604 * If the new focus window isn't mapped, then we can't focus on it
605 * (X will generate an error, for example). Instead, create an
606 * event handler that will set the focus to this window once it gets
607 * mapped. At the same time, delete any old handler that might be
608 * around; it's no longer relevant.
609 */
610
611 if (displayFocusPtr->focusOnMapPtr != NULL) {
612 Tk_DeleteEventHandler(
613 (Tk_Window) displayFocusPtr->focusOnMapPtr,
614 StructureNotifyMask, FocusMapProc,
615 (ClientData) displayFocusPtr->focusOnMapPtr);
616 displayFocusPtr->focusOnMapPtr = NULL;
617 }
618 if (!allMapped) {
619 Tk_CreateEventHandler((Tk_Window) winPtr,
620 VisibilityChangeMask, FocusMapProc,
621 (ClientData) winPtr);
622 displayFocusPtr->focusOnMapPtr = winPtr;
623 displayFocusPtr->forceFocus = force;
624 return;
625 }
626
627 for (tlFocusPtr = winPtr->mainPtr->tlFocusPtr; tlFocusPtr != NULL;
628 tlFocusPtr = tlFocusPtr->nextPtr) {
629 if (tlFocusPtr->topLevelPtr == topLevelPtr) {
630 break;
631 }
632 }
633 if (tlFocusPtr == NULL) {
634 tlFocusPtr = (ToplevelFocusInfo *) ckalloc(sizeof(ToplevelFocusInfo));
635 tlFocusPtr->topLevelPtr = topLevelPtr;
636 tlFocusPtr->nextPtr = winPtr->mainPtr->tlFocusPtr;
637 winPtr->mainPtr->tlFocusPtr = tlFocusPtr;
638 }
639 tlFocusPtr->focusWinPtr = winPtr;
640
641 /*
642 * Reset the window system's focus window and generate focus events,
643 * with two special cases:
644 *
645 * 1. If the application is embedded and doesn't currently have the
646 * focus, don't set the focus directly. Instead, see if the
647 * embedding code can claim the focus from the enclosing
648 * container.
649 * 2. Otherwise, if the application doesn't currently have the
650 * focus, don't change the window system's focus unless it was
651 * already in this application or "force" was specified.
652 */
653
654 if ((topLevelPtr->flags & TK_EMBEDDED)
655 && (displayFocusPtr->focusWinPtr == NULL)) {
656 TkpClaimFocus(topLevelPtr, force);
657 } else if ((displayFocusPtr->focusWinPtr != NULL) || force) {
658 /*
659 * Generate events to shift focus between Tk windows.
660 * We do this regardless of what TkpChangeFocus does with
661 * the real X focus so that Tk widgets track focus commands
662 * when there is no window manager. GenerateFocusEvents will
663 * set up a serial number marker so we discard focus events
664 * that are triggered by the ChangeFocus.
665 */
666
667 serial = TkpChangeFocus(TkpGetWrapperWindow(topLevelPtr), force);
668 if (serial != 0) {
669 displayFocusPtr->focusSerial = serial;
670 }
671 GenerateFocusEvents(displayFocusPtr->focusWinPtr, winPtr);
672 displayFocusPtr->focusWinPtr = winPtr;
673 winPtr->dispPtr->focusPtr = winPtr;
674 }
675 }
676
677 /*
678 *----------------------------------------------------------------------
679 *
680 * TkGetFocusWin --
681 *
682 * Given a window, this procedure returns the current focus
683 * window for its application and display.
684 *
685 * Results:
686 * The return value is a pointer to the window that currently
687 * has the input focus for the specified application and
688 * display, or NULL if none.
689 *
690 * Side effects:
691 * None.
692 *
693 *----------------------------------------------------------------------
694 */
695
696 TkWindow *
697 TkGetFocusWin(winPtr)
698 TkWindow *winPtr; /* Window that selects an application
699 * and a display. */
700 {
701 DisplayFocusInfo *displayFocusPtr;
702
703 if (winPtr == NULL) {
704 return (TkWindow *) NULL;
705 }
706
707 displayFocusPtr = FindDisplayFocusInfo(winPtr->mainPtr, winPtr->dispPtr);
708 return displayFocusPtr->focusWinPtr;
709 }
710
711 /*
712 *----------------------------------------------------------------------
713 *
714 * TkFocusKeyEvent --
715 *
716 * Given a window and a key press or release event that arrived for
717 * the window, use information about the keyboard focus to compute
718 * which window should really get the event. In addition, update
719 * the event to refer to its new window.
720 *
721 * Results:
722 * The return value is a pointer to the window that has the input
723 * focus in winPtr's application, or NULL if winPtr's application
724 * doesn't have the input focus. If a non-NULL value is returned,
725 * eventPtr will be updated to refer properly to the focus window.
726 *
727 * Side effects:
728 * None.
729 *
730 *----------------------------------------------------------------------
731 */
732
733 TkWindow *
734 TkFocusKeyEvent(winPtr, eventPtr)
735 TkWindow *winPtr; /* Window that selects an application
736 * and a display. */
737 XEvent *eventPtr; /* X event to redirect (should be KeyPress
738 * or KeyRelease). */
739 {
740 DisplayFocusInfo *displayFocusPtr;
741 TkWindow *focusWinPtr;
742 int focusX, focusY, vRootX, vRootY, vRootWidth, vRootHeight;
743
744 displayFocusPtr = FindDisplayFocusInfo(winPtr->mainPtr, winPtr->dispPtr);
745 focusWinPtr = displayFocusPtr->focusWinPtr;
746
747 /*
748 * The code below is a debugging aid to make sure that dispPtr->focusPtr
749 * is kept properly in sync with the "truth", which is the value in
750 * displayFocusPtr->focusWinPtr.
751 */
752
753 #ifdef TCL_MEM_DEBUG
754 if (focusWinPtr != winPtr->dispPtr->focusPtr) {
755 printf("TkFocusKeyEvent found dispPtr->focusPtr out of sync:\n");
756 printf("expected %s, got %s\n",
757 (focusWinPtr != NULL) ? focusWinPtr->pathName : "??",
758 (winPtr->dispPtr->focusPtr != NULL) ?
759 winPtr->dispPtr->focusPtr->pathName : "??");
760 }
761 #endif
762
763 if ((focusWinPtr != NULL) && (focusWinPtr->mainPtr == winPtr->mainPtr)) {
764 /*
765 * Map the x and y coordinates to make sense in the context of
766 * the focus window, if possible (make both -1 if the map-from
767 * and map-to windows don't share the same screen).
768 */
769
770 if ((focusWinPtr->display != winPtr->display)
771 || (focusWinPtr->screenNum != winPtr->screenNum)) {
772 eventPtr->xkey.x = -1;
773 eventPtr->xkey.y = -1;
774 } else {
775 Tk_GetVRootGeometry((Tk_Window) focusWinPtr, &vRootX, &vRootY,
776 &vRootWidth, &vRootHeight);
777 Tk_GetRootCoords((Tk_Window) focusWinPtr, &focusX, &focusY);
778 eventPtr->xkey.x = eventPtr->xkey.x_root - vRootX - focusX;
779 eventPtr->xkey.y = eventPtr->xkey.y_root - vRootY - focusY;
780 }
781 eventPtr->xkey.window = focusWinPtr->window;
782 return focusWinPtr;
783 }
784
785 /*
786 * The event doesn't belong to us. Perhaps, due to embedding, it
787 * really belongs to someone else. Give the embedding code a chance
788 * to redirect the event.
789 */
790
791 TkpRedirectKeyEvent(winPtr, eventPtr);
792 return (TkWindow *) NULL;
793 }
794
795 /*
796 *----------------------------------------------------------------------
797 *
798 * TkFocusDeadWindow --
799 *
800 * This procedure is invoked when it is determined that
801 * a window is dead. It cleans up focus-related information
802 * about the window.
803 *
804 * Results:
805 * None.
806 *
807 * Side effects:
808 * Various things get cleaned up and recycled.
809 *
810 *----------------------------------------------------------------------
811 */
812
813 void
814 TkFocusDeadWindow(winPtr)
815 register TkWindow *winPtr; /* Information about the window
816 * that is being deleted. */
817 {
818 ToplevelFocusInfo *tlFocusPtr, *prevPtr;
819 DisplayFocusInfo *displayFocusPtr;
820 TkDisplay *dispPtr = winPtr->dispPtr;
821
822 /*
823 * Certain special windows like those used for send and clipboard
824 * have no mainPtr.
825 */
826
827 if (winPtr->mainPtr == NULL)
828 return;
829
830 /*
831 * Search for focus records that refer to this window either as
832 * the top-level window or the current focus window.
833 */
834
835 displayFocusPtr = FindDisplayFocusInfo(winPtr->mainPtr, winPtr->dispPtr);
836 for (prevPtr = NULL, tlFocusPtr = winPtr->mainPtr->tlFocusPtr;
837 tlFocusPtr != NULL;
838 prevPtr = tlFocusPtr, tlFocusPtr = tlFocusPtr->nextPtr) {
839 if (winPtr == tlFocusPtr->topLevelPtr) {
840 /*
841 * The top-level window is the one being deleted: free
842 * the focus record and release the focus back to PointerRoot
843 * if we acquired it implicitly.
844 */
845
846 if (dispPtr->implicitWinPtr == winPtr) {
847 if (dispPtr->focusDebug) {
848 printf("releasing focus to root after %s died\n",
849 tlFocusPtr->topLevelPtr->pathName);
850 }
851 dispPtr->implicitWinPtr = NULL;
852 displayFocusPtr->focusWinPtr = NULL;
853 dispPtr->focusPtr = NULL;
854 }
855 if (displayFocusPtr->focusWinPtr == tlFocusPtr->focusWinPtr) {
856 displayFocusPtr->focusWinPtr = NULL;
857 dispPtr->focusPtr = NULL;
858 }
859 if (prevPtr == NULL) {
860 winPtr->mainPtr->tlFocusPtr = tlFocusPtr->nextPtr;
861 } else {
862 prevPtr->nextPtr = tlFocusPtr->nextPtr;
863 }
864 ckfree((char *) tlFocusPtr);
865 break;
866 } else if (winPtr == tlFocusPtr->focusWinPtr) {
867 /*
868 * The deleted window had the focus for its top-level:
869 * move the focus to the top-level itself.
870 */
871
872 tlFocusPtr->focusWinPtr = tlFocusPtr->topLevelPtr;
873 if ((displayFocusPtr->focusWinPtr == winPtr)
874 && !(tlFocusPtr->topLevelPtr->flags & TK_ALREADY_DEAD)) {
875 if (dispPtr->focusDebug) {
876 printf("forwarding focus to %s after %s died\n",
877 tlFocusPtr->topLevelPtr->pathName,
878 winPtr->pathName);
879 }
880 GenerateFocusEvents(displayFocusPtr->focusWinPtr,
881 tlFocusPtr->topLevelPtr);
882 displayFocusPtr->focusWinPtr = tlFocusPtr->topLevelPtr;
883 dispPtr->focusPtr = tlFocusPtr->topLevelPtr;
884 }
885 break;
886 }
887 }
888
889 if (displayFocusPtr->focusOnMapPtr == winPtr) {
890 displayFocusPtr->focusOnMapPtr = NULL;
891 }
892 }
893
894 /*
895 *----------------------------------------------------------------------
896 *
897 * GenerateFocusEvents --
898 *
899 * This procedure is called to create FocusIn and FocusOut events to
900 * move the input focus from one window to another.
901 *
902 * Results:
903 * None.
904 *
905 * Side effects:
906 * FocusIn and FocusOut events are generated.
907 *
908 *----------------------------------------------------------------------
909 */
910
911 static void
912 GenerateFocusEvents(sourcePtr, destPtr)
913 TkWindow *sourcePtr; /* Window that used to have the focus (may
914 * be NULL). */
915 TkWindow *destPtr; /* New window to have the focus (may be
916 * NULL). */
917
918 {
919 XEvent event;
920 TkWindow *winPtr;
921
922 winPtr = sourcePtr;
923 if (winPtr == NULL) {
924 winPtr = destPtr;
925 if (winPtr == NULL) {
926 return;
927 }
928 }
929
930 event.xfocus.serial = LastKnownRequestProcessed(winPtr->display);
931 event.xfocus.send_event = GENERATED_EVENT_MAGIC;
932 event.xfocus.display = winPtr->display;
933 event.xfocus.mode = NotifyNormal;
934 TkInOutEvents(&event, sourcePtr, destPtr, FocusOut, FocusIn,
935 TCL_QUEUE_MARK);
936 }
937
938 /*
939 *----------------------------------------------------------------------
940 *
941 * FocusMapProc --
942 *
943 * This procedure is called as an event handler for VisibilityNotify
944 * events, if a window receives the focus at a time when its
945 * toplevel isn't mapped. The procedure is needed because X
946 * won't allow the focus to be set to an unmapped window; we
947 * detect when the toplevel is mapped and set the focus to it then.
948 *
949 * Results:
950 * None.
951 *
952 * Side effects:
953 * If this is a map event, the focus gets set to the toplevel
954 * given by clientData.
955 *
956 *----------------------------------------------------------------------
957 */
958
959 static void
960 FocusMapProc(clientData, eventPtr)
961 ClientData clientData; /* Toplevel window. */
962 XEvent *eventPtr; /* Information about event. */
963 {
964 TkWindow *winPtr = (TkWindow *) clientData;
965 DisplayFocusInfo *displayFocusPtr;
966
967 if (eventPtr->type == VisibilityNotify) {
968 displayFocusPtr = FindDisplayFocusInfo(winPtr->mainPtr,
969 winPtr->dispPtr);
970 if (winPtr->dispPtr->focusDebug) {
971 printf("auto-focussing on %s, force %d\n", winPtr->pathName,
972 displayFocusPtr->forceFocus);
973 }
974 Tk_DeleteEventHandler((Tk_Window) winPtr, VisibilityChangeMask,
975 FocusMapProc, clientData);
976 displayFocusPtr->focusOnMapPtr = NULL;
977 TkSetFocusWin(winPtr, displayFocusPtr->forceFocus);
978 }
979 }
980
981 /*
982 *----------------------------------------------------------------------
983 *
984 * FindDisplayFocusInfo --
985 *
986 * Given an application and a display, this procedure locate the
987 * focus record for that combination. If no such record exists,
988 * it creates a new record and initializes it.
989 *
990 * Results:
991 * The return value is a pointer to the record.
992 *
993 * Side effects:
994 * A new record will be allocated if there wasn't one already.
995 *
996 *----------------------------------------------------------------------
997 */
998
999 static DisplayFocusInfo *
1000 FindDisplayFocusInfo(mainPtr, dispPtr)
1001 TkMainInfo *mainPtr; /* Record that identifies a particular
1002 * application. */
1003 TkDisplay *dispPtr; /* Display whose focus information is
1004 * needed. */
1005 {
1006 DisplayFocusInfo *displayFocusPtr;
1007
1008 for (displayFocusPtr = mainPtr->displayFocusPtr;
1009 displayFocusPtr != NULL;
1010 displayFocusPtr = displayFocusPtr->nextPtr) {
1011 if (displayFocusPtr->dispPtr == dispPtr) {
1012 return displayFocusPtr;
1013 }
1014 }
1015
1016 /*
1017 * The record doesn't exist yet. Make a new one.
1018 */
1019
1020 displayFocusPtr = (DisplayFocusInfo *) ckalloc(sizeof(DisplayFocusInfo));
1021 displayFocusPtr->dispPtr = dispPtr;
1022 displayFocusPtr->focusWinPtr = NULL;
1023 displayFocusPtr->focusOnMapPtr = NULL;
1024 displayFocusPtr->forceFocus = 0;
1025 displayFocusPtr->focusSerial = 0;
1026 displayFocusPtr->nextPtr = mainPtr->displayFocusPtr;
1027 mainPtr->displayFocusPtr = displayFocusPtr;
1028 return displayFocusPtr;
1029 }
1030
1031 /*
1032 *----------------------------------------------------------------------
1033 *
1034 * TkFocusFree --
1035 *
1036 * Free resources associated with maintaining the focus.
1037 *
1038 * Results:
1039 * None.
1040 *
1041 * Side effects:
1042 * This mainPtr should no long access focus information.
1043 *
1044 *----------------------------------------------------------------------
1045 */
1046
1047 void
1048 TkFocusFree(mainPtr)
1049 TkMainInfo *mainPtr; /* Record that identifies a particular
1050 * application. */
1051 {
1052 DisplayFocusInfo *displayFocusPtr;
1053 ToplevelFocusInfo *tlFocusPtr;
1054
1055 while (mainPtr->displayFocusPtr != NULL) {
1056 displayFocusPtr = mainPtr->displayFocusPtr;
1057 mainPtr->displayFocusPtr = mainPtr->displayFocusPtr->nextPtr;
1058 ckfree((char *) displayFocusPtr);
1059 }
1060 while (mainPtr->tlFocusPtr != NULL) {
1061 tlFocusPtr = mainPtr->tlFocusPtr;
1062 mainPtr->tlFocusPtr = mainPtr->tlFocusPtr->nextPtr;
1063 ckfree((char *) tlFocusPtr);
1064 }
1065 }