Skip to content

Commit

Permalink
Final fix for buttons not working when disabled.
Browse files Browse the repository at this point in the history
[cappuccino#188 state:resolved]

Reviewed by me.
  • Loading branch information
Francisco Ryan Tolmasky I committed Jan 21, 2009
1 parent 99f6492 commit 8de321c
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 233 deletions.
12 changes: 3 additions & 9 deletions AppKit/CPApplication.j
Expand Up @@ -305,7 +305,7 @@ CPRunContinuesResponse = -1002;
_currentSession._state = aCode;
_currentSession = _currentSession._previous;

if (aCode == CPRunAbortedResponse)
// if (aCode == CPRunAbortedResponse)
[self _removeRunModalLoop];
}

Expand All @@ -315,7 +315,7 @@ CPRunContinuesResponse = -1002;
var count = _eventListeners.length;

while (count--)
if (_eventListeners[count]._callback == _CPRunModalLoop)
if (_eventListeners[count]._callback === _CPRunModalLoop)
{
_eventListeners.splice(count, 1);

Expand Down Expand Up @@ -686,18 +686,12 @@ var _CPEventListenerMake = function(anEventMask, aCallback)
var _CPRunModalLoop = function(anEvent)
{
[CPApp setCallback:_CPRunModalLoop forNextEventMatchingMask:CPAnyEventMask untilDate:nil inMode:0 dequeue:NO];

// FIXME: abortModal from event loop?

var theWindow = [anEvent window],
modalSession = CPApp._currentSession;

if (theWindow == modalSession._window || [theWindow worksWhenModal])
[theWindow sendEvent:anEvent];
/*else
[[session modalWindow] makeKeyAndOrderFront:]*/

if (modalSession._state != CPRunContinuesResponse)
[CPApp _removeRunModalLoop];
}

/*!
Expand Down
36 changes: 8 additions & 28 deletions AppKit/CPButton.j
Expand Up @@ -587,6 +587,8 @@ var _CPButtonClassName = nil,
*/
- (void)highlight:(BOOL)aFlag
{
_isHighlighted = aFlag;

[self drawBezelWithHighlight:aFlag];
[self drawContentsWithHighlight:aFlag];
}
Expand All @@ -608,40 +610,18 @@ var _CPButtonClassName = nil,
return _tag;
}

/*!
Called when the user clicks on this button.
@param anEvent the event with information about the click
*/
- (void)mouseDown:(CPEvent)anEvent
{
_isHighlighted = YES;

[self highlight:_isHighlighted];
[super mouseDown:anEvent] ;
}

/*!
Called when the user drags on the button.
@param anEvent the event with information about the drag
*/
- (void)mouseDragged:(CPEvent)anEvent
- (BOOL)startTrackingAt:(CGPoint)aPoint
{
_isHighlighted = CGRectContainsPoint([self bounds], [self convertPoint:[anEvent locationInWindow] fromView:nil]);
[self highlight:YES];

[self highlight:_isHighlighted];
return [super startTrackingAt:aPoint];
}

/*!
Called when the user releases the mouse button
@param anEvent the event with information about the mouse release
*/
- (void)mouseUp:(CPEvent)anEvent
- (void)stopTracking:(CGPoint)lastPoint at:(CGPoint)aPoint mouseIsUp:(BOOL)mouseIsUp
{
_isHighlighted = NO;
[self highlight:NO];

[self highlight:_isHighlighted];

[super mouseUp:anEvent];
[super stopTracking:lastPoint at:aPoint mouseIsUp:mouseIsUp];
}

/*!
Expand Down
233 changes: 58 additions & 175 deletions AppKit/CPControl.j
Expand Up @@ -101,6 +101,11 @@ var CPControlBlackColor = [CPColor blackColor];

CPDictionary _backgroundColors;
CPString _currentBackgroundColorName;

BOOL _continuousTracking;
BOOL _trackingWasWithinFrame;
unsigned _trackingMouseDownFlags;
CGPoint _previousTrackingLocation;
}

- (id)initWithFrame:(CGRect)aFrame
Expand All @@ -110,6 +115,8 @@ var CPControlBlackColor = [CPColor blackColor];
if (self)
{
_sendActionOn = CPLeftMouseUpMask;
_trackingMouseDownFlags = 0;

_isEnabled = YES;

[self setFont:[CPFont systemFontOfSize:12.0]];
Expand Down Expand Up @@ -256,207 +263,83 @@ var CPControlBlackColor = [CPColor blackColor];
_target = aTarget;
}

- (BOOL)startTrackingAt:(CGPoint)aPoint
- (BOOL)tracksMouseOutsideOfFrame
{
CPLog("Start tracking at x:" + aPoint.x + " y:" + aPoint.y) ;
return YES;
return NO;
}

//- (BOOL)continueTracking:(CGPoint)lastPoint at:(CGPoint)currentPoint inView:(NSView *)view {
- (BOOL)continueTracking:(CGPoint)lastPoint at:(CGPoint)currentPoint // inView:(CPView)aView
- (void)trackMouse:(CPEvent)anEvent
{
CPLog("Continue tracking at x:" + currentPoint.x + " y:" + currentPoint.y) ;
return YES;
}
var type = [anEvent type],
currentLocation = [self convertPoint:[anEvent locationInWindow] fromView:nil];
isWithinFrame = [self tracksOutsideOfFrame] || CGRectContainsPoint([self bounds], currentLocation);

- (void)stopTracking:(CGPoint)lastPoint at:(CGPoint)aPoint mouseIsUp:(BOOL)mouseIsUp
{
CPLog("Stop tracking at x:" + aPoint.x + " y:" + aPoint.y) ;
}

- (void)old__trackMouse:(CPEvent)anEvent untilMouseUp:(BOOL)shouldContinueUntilMouseUp
{
CPLog("start trackMouse") ;
// if (event is mousedown) startTackingAt:event.locationInWIndow
// else if (event is mousedragged && event location withing frame)
// continueTracking:at:....
// else if (event is out of frame) { stopTracking... if (event is mouseup
// || !shouldContinueUntilMouseUp) return;}
// register to receive next event

// if (_sendActionOn & CPLeftMouseUpMask && CPRectContainsPoint([self bounds], [self convertPoint:[anEvent locationInWindow] fromView:nil]))

// doc here : http://objc.toodarkpark.net/AppKit/Classes/NSCell.html#//apple_ref/occ/instm/NSCell/trackMouse:inRect:ofView:untilMouseUp:
// if(![self startTrackingAt:[event locationInWindow] inView:view])
// return NO;

if ([anEvent type] == CPLeftMouseDown)
{
CPLog("startTrackingAt should log something") ;
[self startTrackingAt:[anEvent locationInWindow]] ;
}
else
return NO ;


do {
var currentPoint = [anEvent locationInWindow] ;
var lastPoint = [anEvent locationInWindow] // should test if _lastPoint do not existe = current point else = _lastPoint
var isWithinFrame = CPRectContainsPoint([self bounds], [self convertPoint:[anEvent locationInWindow] fromView:nil]) ;
var eventType = [anEvent type] ;

// if (shouldContinueUntilMouseUp) {
// if (eventType==CPLeftMouseUp) {
// // [self stopTracking:lastPoint at:[event locationInWindow] inView:view mouseIsUp:YES];
// [self stopTracking:lastPoint at:currentPoint mouseIsUp:YES];
// result=YES;
// break;
// }
// }
// else if (isWithinFrame) {
// if(eventType==CPLeftMouseUp){
// // [self stopTracking:lastPoint at:[event locationInWindow] inView:view mouseIsUp:YES];
// [self stopTracking:lastPoint at:currentPoint mouseIsUp:YES];
// result=YES;
// break;
// }
// }
// else {
// // [self stopTracking:lastPoint at:[event locationInWindow] inView:view mouseIsUp:NO];
// [self stopTracking:lastPoint at:currentPoint mouseIsUp:NO];
// result=NO;
// break;
// }
//
// if (isWithinFrame) {
// // if (![self continueTracking:lastPoint at:[event locationInWindow] inView:view])
// if (![self continueTracking:lastPoint at:currentPoint])
// break;
//
// // if([self isContinuous])
// // [(NSControl *)view sendAction:[(NSControl *)view action] to:[(NSControl *)view target]];
// }

// [[view window] flushWindow];
//
// event=[[view window] nextEventMatchingMask:NSLeftMouseUpMask|
// NSLeftMouseDraggedMask];


// === my version now :

// var eventType = [anEvent type] ;
// var point = [anEvent locationInWindow] ;
// var lastPoint = [anEvent locationInWindow] // should test if _lastPoint do not existe = current point else = _lastPoint
// var isWithinFrame = CPRectContainsPoint([self bounds], [self convertPoint:[anEvent locationInWindow] fromView:nil]) ;
if (type === CPLeftMouseUp)
{
[self stopTracking:_previousTrackingLocation at:currentLocation mouseIsUp:YES];

if (eventType == CPLeftMouseDown)
{
// [self startTrackingAt:currentPoint] ;
}
else if (eventType == CPLeftMouseDragged && isWithinFrame)
_trackingMouseDownFlags = 0;
}

else
{
if (type === CPLeftMouseDown)
{
[self continueTracking:lastPoint at:currentPoint];
_trackingMouseDownFlags = [anEvent modifierFlags];
_continuousTracking = [self startTrackingAt:currentLocation];
}
// else if (event is out of frame) {
// stopTracking... if (event is mouseup || !shouldContinueUntilMouseUp) return;
// }
// register to receive next event
else if (!isWithinFrame)
else if (type === CPLeftMouseDragged)
{
[self stopTracking:lastPoint at:currentPoint mouseIsUp:(anEvent == CPLeftMouseUp)];

if (eventType == CPLeftMouseUp || !shouldContinueUntilMouseUp)
if (isWithinFrame)
{
CPLog("This should end") ;
break ;
if (!_trackingWasWithinFrame)
_continuousTracking = [self startTrackingAt:currentLocation];

else if (_continuousTracking)
_continuousTracking = [self continueTracking:_previousTrackingLocation at:currentLocation];
}
else
[self stopTracking:_previousTrackingLocation at:currentLocation mouseIsUp:NO];
}

// CPLog("before force break") ;
// break;
// CPLog("after force break") ;
} while (YES) ;



// register to receive next event
CPLog("end trackMouse 8") ;
}

- (void)trackMouse:(CPEvent)anEvent untilMouseUp:(BOOL)shouldContinueUntilMouseUp
{
CPLog("start trackMouse") ;

// doc here : http://objc.toodarkpark.net/AppKit/Classes/NSCell.html#//apple_ref/occ/instm/NSCell/trackMouse:inRect:ofView:untilMouseUp:

var currentPoint = [anEvent locationInWindow] ;
// var currentPoint = [self convertPoint:[anEvent locationInWindow] fromView:nil] ;
var lastPoint = [anEvent locationInWindow]
var isWithinFrame = CPRectContainsPoint([self bounds], [self convertPoint:[anEvent locationInWindow] fromView:nil]) ;
var eventType = [anEvent type] ;

if (eventType == CPLeftMouseDown)
{
[self startTrackingAt:currentPoint] ;
[CPApp setTarget:self selector:@selector(trackMouse:) forNextEventMatchingMask:CPLeftMouseDraggedMask | CPLeftMouseUpMask untilDate:nil inMode:nil dequeue:YES];
}
else if (eventType == CPLeftMouseDragged && isWithinFrame)
{
[self continueTracking:lastPoint at:currentPoint];
}
else if (eventType == CPLeftMouseUp && isWithinFrame)
{
CPLog("Up in the frame -> shoud fire up action!") ;
// [self stopTracking:lastPoint at:currentPoint mouseIsUp:(anEvent == CPLeftMouseUp)];
return ;
}
else if (!isWithinFrame)
{
// [self stopTracking:lastPoint at:currentPoint mouseIsUp:(anEvent == CPLeftMouseUp)];

if (eventType == CPLeftMouseUp || !shouldContinueUntilMouseUp)
{
[self stopTracking:lastPoint at:currentPoint mouseIsUp:(anEvent == CPLeftMouseUp)];
CPLog("Up out of the frame") ;
return ;
}
}

[CPApp setTarget:self selector:@selector(trackMouse:untilMouseUp:) forNextEventMatchingMask:CPLeftMouseDraggedMask | CPLeftMouseUpMask untilDate:nil inMode:nil dequeue:YES];
if ((_sendActionOn & (1 << type)) && isWithinFrame)
[self sendAction:_action to:_target];

CPLog("end trackMouse") ;
_trackingWasInFrame = isWithinFrame;
_previousTrackingLocation = currentLocation;
}

- (void)mouseDown:(CPEvent)anEvent
- (unsigned)mouseDownFlags
{
// while (getNextEvent)
// if (currentEvent is mousedragged) highlight or unhighlight
// else if (currentEvent is mouseUp) fire action, return
return _trackingMouseDownFlags;
}

// if (not enabled) return; // nothing to do.
// [self trackMouse:anEvent untilMouseUp:YES];
- (BOOL)startTrackingAt:(CGPoint)aPoint
{
return (_sendActionOn & CPPeriodicMask) || (_sendActionOn & CPLeftMouseDraggedMask);
}

CPLog("CPControl > mouseDown") ;
if (!_isEnabled) return ;
[self trackMouse:anEvent untilMouseUp:YES] ;
- (BOOL)continueTracking:(CGPoint)lastPoint at:(CGPoint)aPoint
{
return (_sendActionOn & CPPeriodicMask) || (_sendActionOn & CPLeftMouseDraggedMask);
}

- (void)mouseUp:(CPEvent)anEvent
- (void)stopTracking:(CGPoint)lastPoint at:(CGPoint)aPoint mouseIsUp:(BOOL)mouseIsUp
{
CPLog("CPControl > mouseUp") ;
if (_sendActionOn & CPLeftMouseUpMask && CPRectContainsPoint([self bounds], [self convertPoint:[anEvent locationInWindow] fromView:nil]))
{
CPLog("Will send action because isWithinFrame == " + CPRectContainsPoint([self bounds], [self convertPoint:[anEvent locationInWindow] fromView:nil])) ;
[self sendAction:_action to:_target];
}
else
{
CPLog("Won't send action because isWithinFrame == " + CPRectContainsPoint([self bounds], [self convertPoint:[anEvent locationInWindow] fromView:nil])) ;
}
}

- (void)mouseDown:(CPEvent)anEvent
{
if (!_isEnabled)
return;

[super mouseUp:anEvent];
[self trackMouse:anEvent];
}


/*!
Causes <code>anAction</code> to be sent to <code>anObject</code>.
@param anAction the action to send
Expand All @@ -482,7 +365,7 @@ var CPControlBlackColor = [CPColor blackColor];
- (BOOL)isContinuous
{
// Some subclasses should redefine this with CPLeftMouseDraggedMask
return (_sendActionOn & CPPeriodicMask) != 0;
return (_sendActionOn & CPPeriodicMask) !== 0;
}

/*!
Expand Down

0 comments on commit 8de321c

Please sign in to comment.