Tk Source Code

View Ticket
Login
Ticket UUID: 3517803
Title: Modality not properly working
Type: Bug Version: None
Submitter: gcramer Created on: 2012-04-14 17:53:49
Subsystem: 56. [grab] Assigned To: hobbs
Priority: 5 Medium Severity:
Status: Open Last Modified: 2012-04-20 20:02:42
Resolution: None Closed By:
    Closed on:
Description:
I have the following case:

.help is a help top level window
.application.fileSelBox is a modal top level window (using local grab).

Currently I cannot use the .help window if .application.fileSelBox is mapped, because .help is catching all mouse events. I think this is a bug, because if I look at the implementation of TkGrabState() - which is used in TkPointerEvent(), which handles the modality - something is weird:

int
TkGrabState(
    TkWindow *winPtr)           /* Window for which grab information is
                                                  * needed. */
{
    TkWindow *grabWinPtr = winPtr->dispPtr->grabWinPtr;

    if (grabWinPtr == NULL) {
        return TK_GRAB_NONE;
    }
    if ((winPtr->mainPtr != grabWinPtr->mainPtr)
            && !(winPtr->dispPtr->grabFlags & GRAB_GLOBAL)) {
        return TK_GRAB_NONE;
    }

    return TkPositionInTree(winPtr, grabWinPtr);
}

The condition "winPtr->mainPtr != grabWinPtr->mainPtr" is always true, because winPtr->mainPtr is "." in every case. I think that this is a bug, and something like "SecondLastToplevel(winPtr) != SecondLastToplevel(grabWinPtr)" was intended. This means that the correct implementation should be:

int
TkGrabState(
    TkWindow *winPtr)           /* Window for which grab information is
                                                  * needed. */
{
    TkWindow *grabWinPtr = winPtr->dispPtr->grabWinPtr;

    if (grabWinPtr == NULL) {
        return TK_GRAB_NONE;
    }
    if (!(winPtr->dispPtr->grabFlags & GRAB_GLOBAL) &&
            winPtr != grabWinPtr && /* this is an often case */
            (SecondLastToplevel(winPtr) != SecondLastToplevel(grabWinPtr))) {
        return TK_GRAB_NONE;
    }

    return TkPositionInTree(winPtr, grabWinPtr);
}

/* SecondLastToplevel() returns the toplevel window which is a descendant
 * of ".", or it returns the argument if such a second last toplevel is
 * not exisiting. */
static TkWindow *
SecondLastToplevel(
    TkWindow *winPtr)           /* Window for which second last
                                                  * toplevel is needed. */
{
    TkWindow *prevTopLevelWinPtr = winPtr->mainPtr->winPtr;
    TkWindow *topLevelWinPtr = winPtr->mainPtr->winPtr;

    while (winPtr && !(winPtr->flags & TK_TOP_HIERARCHY)) {
        topLevelWinPtr = prevTopLevelWinPtr;
        prevTopLevelWinPtr = winPtr;
        winPtr = winPtr->parentPtr;
    }

    return topLevelWinPtr;
}

In my case SecondLastToplevel() will return .help and .application, respectively, which do not match.  Now the application will work as expected, I can use the .help window although the modal window .application.fsbox is mapped, but I cannot use the ancestor .application. The fixed implementation of TkGrabState() takes the tree hierarchy into account, the current implementation in the Tk library is ignoring the tree hierarchy.
User Comments: gcramer added on 2012-04-20 20:02:42:
My first approach with function SecondLastToplevel() is not working in all cases. I've attached a file with a modified version of TkGrabState() which takes the full widget hierarchy into account. I'm using this patch in my project <http://sourceforge.net/projects/scidb/>.

gcramer added on 2012-04-20 20:00:26:

File Added - 441700: TkGrabState.patched.c

Attachments: