CocoaDev

Edit AllPages

I’m trying to implement menu-like behaviour using a pop-up window. It 99% works, but there’s one thing causing problems. I will usually want to pop up a menu on a mouseDown event in some view. Having figured out where to place the “menu” window, its size and all that, I hand over to its own internal event tracking method. That implements a modal loop which basically hands out mouseDown, dragged and mouseUp events to a nominated view inside that window. The idea is that you could put any view here and it will be none the wiser - it will just get mouse events as usual.

My problem is that when I go from the “outer” mouseDown event handler to my tracking loop, the mouse location values I receive are for the parent window, not for the new popped-up window. My second level tracking loop operates in two ways, to mimic usual menu behaviour - if I click once to pop it up, then click-drag-click to work on its contents, all is well. If I hold the mouse down and drag through the new window, I get the parent window coordinates. I’ve tried everything I can think of to let the window server know I’ve switched into a new window (makeMainWindow, flushing events, etc), but unless the mouse is released, it never seems to notice. This has to be doable, as real pop-up menus must do the same.

Any help on this would be gratefully received. –GrahamCox.

Essence of the code posted below. Note that the test view I add is unimportant - any view will do but the test view makes it easy to see where I think my mouse location is.

@interface GCWindowMenu : NSWindow { IBOutlet NSView* _mainView; }

@end

#define kGCDefaultWindowMenuSize (NSMakeRect(0, 0, 100, 28 ))

//=====================================================================================

#import “GCWindowMenu.h”

#define MAKE_TEST_VIEW 0

@end


I have found one solution, but it strikes me as very hackish. I can detect the bad events because [event window] ! == self. In that case I can remake the event using a converted point that goes from windowA->global->windowB. Still looking for a cleaner approach if possible! -GC.


Random guesses - try sending an NSEvent enterExitEventWithType, or look at NSView removeTrackingRect.