Index: macosx/tkMacOSXMouseEvent.c ================================================================== --- macosx/tkMacOSXMouseEvent.c +++ macosx/tkMacOSXMouseEvent.c @@ -51,10 +51,11 @@ { NSWindow *eventWindow = [theEvent window]; NSEventType eventType = [theEvent type]; TkWindow *winPtr, *grabWinPtr; Tk_Window tkwin; + NSPoint local, global; #if 0 NSTrackingArea *trackingArea = nil; NSInteger eventNumber, clickCount, buttonNumber; #endif @@ -81,66 +82,59 @@ break; default: /* Unrecognized mouse event. */ return theEvent; } - /* - * Remember the window in case we need it next time. - */ - - if (eventWindow && eventWindow != _windowWithMouse) { - if (_windowWithMouse) { - [_windowWithMouse release]; - } - _windowWithMouse = eventWindow; - [_windowWithMouse retain]; - } - /* * Compute the mouse position in Tk screen coordinates (global) and in the - * Tk coordinates of its containing Tk Window. + * Tk coordinates of its containing Tk Window (local). If a grab is in effect, + * the local coordinates should be relative to the grab window. */ - NSPoint global, local = [theEvent locationInWindow]; - - /* - * If the event has no NSWindow, try using the cached NSWindow from the - * last mouse event. - */ - - if (eventWindow == NULL) { - eventWindow = _windowWithMouse; - } if (eventWindow) { - /* - * Set the local mouse position to its NSWindow flipped coordinates, - * with the origin at top left, and the global mouse position to the - * flipped screen coordinates. - */ - + local = [theEvent locationInWindow]; global = [eventWindow tkConvertPointToScreen: local]; + tkwin = TkMacOSXGetCapture(); + if (tkwin) { + winPtr = (TkWindow *) tkwin; + eventWindow = TkMacOSXDrawableWindow(winPtr->window); + if (eventWindow) { + local = [eventWindow tkConvertPointFromScreen: global]; + } else { + return theEvent; + } + } local.y = [eventWindow frame].size.height - local.y; global.y = tkMacOSXZeroScreenHeight - global.y; } else { - /* - * As a last resort, with no NSWindow to work with, set both local and - * global to the screen coordinates. - */ - - local.y = tkMacOSXZeroScreenHeight - local.y; - global = local; - } - - /* - * Find the toplevel which corresponds to the event NSWindow. - */ - - winPtr = TkMacOSXGetTkWindow(eventWindow); - if (winPtr == NULL) { - tkwin = TkMacOSXGetCapture(); - winPtr = (TkWindow *) tkwin; - } else { + + /* + * If the event has no NSWindow, the location is in screen coordinates. + */ + + global = [theEvent locationInWindow]; + tkwin = TkMacOSXGetCapture(); + if (tkwin) { + winPtr = (TkWindow *) tkwin; + eventWindow = TkMacOSXDrawableWindow(winPtr->window); + } else { + eventWindow = [NSApp mainWindow]; + } + if (!eventWindow) { + return theEvent; + } + local = [eventWindow tkConvertPointFromScreen: global]; + local.y = [eventWindow frame].size.height - local.y; + global.y = tkMacOSXZeroScreenHeight - global.y; + } + + /* + * Make sure tkwin is the toplevel which should receive the event. + */ + + if (!tkwin) { + winPtr = TkMacOSXGetTkWindow(eventWindow); tkwin = (Tk_Window) winPtr; } if (!tkwin) { #ifdef TK_MAC_DEBUG_EVENTS TkMacOSXDbgMsg("tkwin == NULL"); Index: macosx/tkMacOSXPrivate.h ================================================================== --- macosx/tkMacOSXPrivate.h +++ macosx/tkMacOSXPrivate.h @@ -261,11 +261,10 @@ Tcl_Interp *_eventInterp; NSMenu *_servicesMenu; TKMenu *_defaultMainMenu, *_defaultApplicationMenu; NSArray *_defaultApplicationMenuItems, *_defaultWindowsMenuItems; NSArray *_defaultHelpMenuItems; - NSWindow *_windowWithMouse; NSAutoreleasePool *_mainPool; #ifdef __i386__ /* The Objective C runtime used on i386 requires this. */ int _poolLock; int _macMinorVersion; Index: macosx/tkMacOSXWindowEvent.c ================================================================== --- macosx/tkMacOSXWindowEvent.c +++ macosx/tkMacOSXWindowEvent.c @@ -196,14 +196,10 @@ #endif TkWindow *winPtr = TkMacOSXGetTkWindow(w); if (winPtr) { TkGenWMDestroyEvent((Tk_Window) winPtr); - if (_windowWithMouse == w) { - _windowWithMouse = nil; - [w release]; - } } /* * If necessary, TkGenWMDestroyEvent() handles [close]ing the window, so * can always return NO from -windowShouldClose: for a Tk window. Index: macosx/tkMacOSXWm.c ================================================================== --- macosx/tkMacOSXWm.c +++ macosx/tkMacOSXWm.c @@ -338,22 +338,16 @@ return [self convertScreenToBase:point]; } #else - (NSPoint) tkConvertPointToScreen: (NSPoint) point { - NSRect pointrect; - pointrect.origin = point; - pointrect.size.width = 0; - pointrect.size.height = 0; + NSRect pointrect = {point, {0,0}}; return [self convertRectToScreen:pointrect].origin; } - (NSPoint) tkConvertPointFromScreen: (NSPoint)point { - NSRect pointrect; - pointrect.origin = point; - pointrect.size.width = 0; - pointrect.size.height = 0; + NSRect pointrect = {point, {0,0}}; return [self convertRectFromScreen:pointrect].origin; } #endif @end