Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > mouseUp not called

mouseUp not called
Thread Tools
iOliverC
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Nov 7, 2004, 06:24 PM
 
Hi guys,

So I have a NSView, and basically I'm trying to replicate NSPopUpButton, but I decided to create my own view due to it being more flexible. Basically, when I click down on my view (mouseDown:) I do:

Code:
- (void)mouseDown:(NSEvent *)event { [self setPressed:YES]; [self setNeedsDisplay:YES]; [self performSelector:@selector(menuShouldOpen)]; }
This works fine, and the view is highlighted. So, on mouseUp, I don't want it to be highlighted, so I do:

Code:
- (void)mouseUp:(NSEvent *)event { [self setPressed:NO]; [self setNeedsDisplay:YES]; }
Now, in certain cases, this works fine. For example, if I click on the view, then click again, mouseUp is called and the code is executed. Now, if I were to click and hold my mouse down on the view, and let go, mouseUp is not called for some reason. So my question is, which event should I use to capture this?

Thanks
     
disco_stu
Junior Member
Join Date: Jun 2002
Location: Springfield
Status: Offline
Reply With Quote
Nov 7, 2004, 07:36 PM
 
I had a problem like this a while ago, too - but I used some sample code from Apple's documentation to find a way around it:

Instead of calling [self setPressed:NO], etc. in mouseUp:, do it in mouseDown:, like this (based on code from Apple's Cocoa documentation at http://developer.apple.com/documenta...useEvents.html:

- (void)mouseDown:(NSEvent *)theEvent
{
BOOL keepOn = YES;
BOOL isInside = YES;
NSPoint mouseLoc;

while (keepOn) {
theEvent = [[self window] nextEventMatchingMask: NSLeftMouseUpMask];
mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
isInside = [self mouse:mouseLoc inRect:[self bounds]];

switch ([theEvent type]) {
case NSLeftMouseUp:
if (isInside) // (ie, if the mouseUp occurred inside the view)
{
/*** You're mouseUp: code HERE***/
[self setPressed:NO];
[self setNeedsDisplay:YES];
}
[self highlight:NO];
keepOn = NO;
break;
default:
/* Ignore any other kind of event. */
break;
}
};
return;
}

So, basically, this code enters a loop while the mouse is down and waits for the mouse to be released. Once released, your code will be called. So, you're mouseUp: method does nothing now, it's all taken care of in mouseDown:

Hope that helps.
     
iOliverC  (op)
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Nov 8, 2004, 03:23 AM
 
Hi,

Thanks for the code, but its still working the same as before, as in, not .

This is the current code:

- (void)mouseDownNSEvent *)theEvent
{
BOOL keepOn = YES;
BOOL isInside = YES;
NSPoint mouseLoc;
[self setPressed:YES];
[self setNeedsDisplay:YES];
[self performSelector:@selector(menuShouldOpen)];
while (keepOn) {
theEvent = [[self window] nextEventMatchingMask: NSLeftMouseUpMask];
mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
isInside = [self mouse:mouseLoc inRect:[self bounds]];

switch ([theEvent type]) {
case NSLeftMouseUp:
if (isInside) // (ie, if the mouseUp occurred inside the view)
{
/*** You're mouseUp: code HERE***/
[self setPressed:NO];
[self setNeedsDisplay:YES];
NSLog(@"U!");
}
//[self setPressed:NO];
//[self setNeedsDisplay:YES];
keepOn = NO;
break;
default:
/* Ignore any other kind of event. */
break;
}
};
return;
}

It seems the same problem as before, mouseUp just isn't called. I am just wondering however if my menuShouldOpen method is to blame:

Code:
- (void)menuShouldOpen { NSPoint viewLocation = [self frame].origin; viewLocation.y = viewLocation.y + 12; [NSMenu popUpContextMenu:objectMenu withEvent:[NSEvent mouseEventWithType:NSLeftMouseDown location:viewLocation modifierFlags:0 timestamp:1 windowNumber:[[NSApp mainWindow] windowNumber] context:[NSGraphicsContext currentContext] eventNumber:1 clickCount:1 pressure:0.0] forView:self]; }
I can't see any problems code wise, but theory wise, maybe the event for the menu is killing the event for the mouseUp.

Thanks again
     
iOliverC  (op)
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Nov 8, 2004, 12:09 PM
 
Turns out it is the menu, if I get rid of the menu thing it works correctly. The question is :-), what is wrong with the code that opens the menu?

Thanks
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 12:46 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,