diff --git a/AppKit/CPView.j b/AppKit/CPView.j index a16562a7ac..c5bec64afc 100644 --- a/AppKit/CPView.j +++ b/AppKit/CPView.j @@ -98,6 +98,10 @@ var CPViewFlags = { }, CPViewHasCustomDrawRect = 1 << 0, CPViewHasCustomLayoutSubviews = 1 << 1; +var CPCurrentToolTip, + CPCurrentToolTipTimer, + CPToolTipDelay = 1.0; + /*! @ingroup appkit @class CPView @@ -189,7 +193,10 @@ var CPViewFlags = { }, unsigned _viewClassFlags; // ToolTips - CPString _toolTip @accessors(property=toolTip); + CPString _toolTip @accessors(getter=toolTip); + Function _toolTipFunctionIn; + Function _toolTipFunctionOut; + BOOL _toolTipInstalled; } /* @@ -236,6 +243,13 @@ var CPViewFlags = { }, _viewClassFlags = CPViewFlags[classUID]; } +- (void)setupToolTipHanlders +{ + _toolTipInstalled = NO; + _toolTipFunctionIn = function(e){[self _fireToolTip];} + _toolTipFunctionOut = function(e){[self _invalidateToolTip];}; +} + + (CPSet)keyPathsForValuesAffectingFrame { return [CPSet setWithObjects:@"frameOrigin", @"frameSize"]; @@ -302,6 +316,7 @@ var CPViewFlags = { }, _theme = [CPTheme defaultTheme]; _themeState = CPThemeStateNormal; + [self setupToolTipHanlders]; [self setupViewFlags]; [self _loadThemeAttributes]; @@ -310,6 +325,118 @@ var CPViewFlags = { }, return self; } + +/*! + Sets the tooltip for the receiver. + + @param aToolTip the tooltip +*/ +- (void)setToolTip:(CPString)aToolTip +{ + if (_toolTip == aToolTip) + return; + + _toolTip = aToolTip; + + if (_toolTip) + [self _installToolTipEventHandlers]; + else + [self _uninstallToolTipEventHandlers]; +} + +/*! @ignore + + Install the handlers for the tooltip +*/ +- (void)_installToolTipEventHandlers +{ + if (_toolTipInstalled) + return; + + if (_DOMElement.addEventListener) + { + _DOMElement.addEventListener("mouseover", _toolTipFunctionIn, NO); + _DOMElement.addEventListener("keypress", _toolTipFunctionOut, NO); + _DOMElement.addEventListener("mouseout", _toolTipFunctionOut, NO); + } + else if (_DOMElement.attachEvent) + { + _DOMElement.attachEvent("onmouseover", _toolTipFunctionIn); + _DOMElement.attachEvent("onkeypress", _toolTipFunctionOut); + _DOMElement.attachEvent("onmouseout", _toolTipFunctionOut); + } + _toolTipInstalled = YES; +} + +/*! @ignore + + Uninstall the handlers for the tooltip +*/ +- (void)_uninstallToolTipEventHandlers +{ + if (!_toolTipInstalled) + return; + + if (_DOMElement.removeEventListener) + { + _DOMElement.removeEventListener("mouseover", _toolTipFunctionIn, NO); + _DOMElement.removeEventListener("keypress", _toolTipFunctionOut, NO); + _DOMElement.removeEventListener("mouseout", _toolTipFunctionOut, NO); + } + else if (_DOMElement.detachEvent) + { + _DOMElement.detachEvent("onmouseover", _toolTipFunctionIn); + _DOMElement.detachEvent("onkeypress", _toolTipFunctionOut); + _DOMElement.detachEvent("onmouseout", _toolTipFunctionOut); + } + _toolTipInstalled = NO; +} + +/*! @ignore + Starts the tooltip timer. +*/ +- (void)_fireToolTip +{ + if (CPCurrentToolTipTimer) + { + [CPCurrentToolTipTimer invalidate]; + if (CPCurrentToolTip) + [CPCurrentToolTip close]; + CPCurrentToolTip = nil; + } + + if (_toolTip) + CPCurrentToolTipTimer = [CPTimer scheduledTimerWithTimeInterval:CPToolTipDelay target:self selector:@selector(_showToolTip:) userInfo:nil repeats:NO]; +} + +/*! @ignore + Stop the tooltip timer if any +*/ +- (void)_invalidateToolTip +{ + if (CPCurrentToolTipTimer) + { + [CPCurrentToolTipTimer invalidate]; + CPCurrentToolTipTimer = nil; + } + + if (CPCurrentToolTip) + { + [CPCurrentToolTip close]; + CPCurrentToolTip = nil; + } +} + +/*! @ignore + Actually shows the tooltip if any +*/ +- (void)_showToolTip:(CPTimer)aTimer +{ + if (CPCurrentToolTip) + [CPCurrentToolTip close]; + CPCurrentToolTip = [_CPToolTip toolTipWithString:_toolTip]; +} + /*! Returns the container view of the receiver @return the receiver's containing view @@ -2764,9 +2891,9 @@ var CPViewAutoresizingMaskKey = @"CPViewAutoresizingMask", _hitTests = ![aCoder containsValueForKey:CPViewHitTestsKey] || [aCoder decodeBoolForKey:CPViewHitTestsKey]; -#if PLATFORM(DOM) - [self setToolTip:[aCoder decodeObjectForKey:CPViewToolTipKey]]; -#endif + _toolTip = [aCoder decodeObjectForKey:CPViewToolTipKey]; + if (_toolTip) + [self _installToolTipEventHandlers]; // DOM SETUP #if PLATFORM(DOM) @@ -2795,6 +2922,7 @@ var CPViewAutoresizingMaskKey = @"CPViewAutoresizingMask", [self setBackgroundColor:[aCoder decodeObjectForKey:CPViewBackgroundColorKey]]; [self setupViewFlags]; + [self setupToolTipHanlders]; _theme = [CPTheme defaultTheme]; _themeClass = [aCoder decodeObjectForKey:CPViewThemeClassKey]; @@ -3001,3 +3129,5 @@ var _CPViewGetTransform = function(/*CPView*/ fromView, /*CPView */ toView) return transform; }; + + diff --git a/AppKit/_CPToolTip.j b/AppKit/_CPToolTip.j index cc4591a162..db37f34e3d 100644 --- a/AppKit/_CPToolTip.j +++ b/AppKit/_CPToolTip.j @@ -27,10 +27,7 @@ _CPToolTipWindowMask = 1 << 27; var _CPToolTipHeight = 24.0, - _CPToolTipFontSize = 11.0, - _CPCurrentToolTip, - _CPCurrentToolTipTimer, - _CPToolTipDelay = 1.0; + _CPToolTipFontSize = 11.0; /*! @ingroup appkit This is a basic tooltip that behaves mostly like Cocoa ones. @@ -178,118 +175,4 @@ var _CPToolTipHeight = 24.0, -/*! @ingroup appkit - Add tooltip support to CPView. -*/ -@implementation CPView (toolTips) - -/*! - Sets the tooltip for the receiver. - - @param aToolTip the tooltip -*/ -- (void)setToolTip:(CPString)aToolTip -{ - if (_toolTip == aToolTip) - return; - - _toolTip = aToolTip; - - if (!_DOMElement) - return; - - var fIn = function(e) - { - [self _fireToolTip]; - }, - fOut = function(e) - { - [self _invalidateToolTip]; - }; - - if (_toolTip) - { - if (_DOMElement.addEventListener) - { - _DOMElement.addEventListener("mouseover", fIn, NO); - _DOMElement.addEventListener("keypress", fOut, NO); - _DOMElement.addEventListener("mouseout", fOut, NO); - } - else if (_DOMElement.attachEvent) - { - _DOMElement.attachEvent("onmouseover", fIn); - _DOMElement.attachEvent("onkeypress", fOut); - _DOMElement.attachEvent("onmouseout", fOut); - } - } - else - { - if (_DOMElement.removeEventListener) - { - _DOMElement.removeEventListener("mouseover", fIn, NO); - _DOMElement.removeEventListener("keypress", fOut, NO); - _DOMElement.removeEventListener("mouseout", fOut, NO); - } - else if (_DOMElement.detachEvent) - { - _DOMElement.detachEvent("onmouseover", fIn); - _DOMElement.detachEvent("onkeypress", fOut); - _DOMElement.detachEvent("onmouseout", fOut); - } - } -} - -/*! - Returns the receiver's tooltip. -*/ -- (CPString)toolTip -{ - return _toolTip; -} -/*! @ignore - Starts the tooltip timer. -*/ -- (void)_fireToolTip -{ - if (_CPCurrentToolTipTimer) - { - [_CPCurrentToolTipTimer invalidate]; - if (_CPCurrentToolTip) - [_CPCurrentToolTip close]; - _CPCurrentToolTip = nil; - } - - if (_toolTip) - _CPCurrentToolTipTimer = [CPTimer scheduledTimerWithTimeInterval:_CPToolTipDelay target:self selector:@selector(_showToolTip:) userInfo:nil repeats:NO]; -} - -/*! @ignore - Stop the tooltip timer if any -*/ -- (void)_invalidateToolTip -{ - if (_CPCurrentToolTipTimer) - { - [_CPCurrentToolTipTimer invalidate]; - _CPCurrentToolTipTimer = nil; - } - - if (_CPCurrentToolTip) - { - [_CPCurrentToolTip close]; - _CPCurrentToolTip = nil; - } -} - -/*! @ignore - Actually shows the tooltip if any -*/ -- (void)_showToolTip:(CPTimer)aTimer -{ - if (_CPCurrentToolTip) - [_CPCurrentToolTip close]; - _CPCurrentToolTip = [_CPToolTip toolTipWithString:_toolTip]; -} - -@end